RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
PosixSystem.hpp
Go to the documentation of this file.
1 /*
2  * PosixSystem.hpp
3  *
4  * Copyright (C) 2022 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_POSIX_SYSTEM_HPP
25 #define LAUNCHER_PLUGINS_POSIX_SYSTEM_HPP
26 
27 #include <functional>
28 
29 #include <Optional.hpp>
30 #include <Error.hpp>
31 
32 namespace rstudio {
33 namespace launcher_plugins {
34 namespace system {
35 
36 class User;
37 
38 } // namespace system
39 } // namespace launcher_plugins
40 } // namespace rstudio
41 
42 namespace rstudio {
43 namespace launcher_plugins {
44 namespace system {
45 namespace posix {
46 
55 struct IpAddress
56 {
58  std::string Name;
59 
61  std::string Address;
62 };
63 
70 
78 std::string getEnvironmentVariable(const std::string& in_name);
79 
88 launcher_plugins::Error getIpAddresses(std::vector<IpAddress>& out_addresses, bool in_includeIPv6 = false);
89 
97 Error ignoreSignal(int in_signal);
98 
110 template <typename T>
111 T posixCall(const std::function<T()>& in_posixFunction)
112 {
113  const T ERR = -1;
114 
115  T result;
116  while (true)
117  {
118  result = in_posixFunction();
119 
120  if (result == ERR && errno == EINTR)
121  continue;
122  else
123  break;
124  }
125 
126  return result;
127 }
128 
142 template <typename T>
143 Error posixCall(const std::function<T()>& in_posixFunction,
144  const ErrorLocation& in_errorLocation,
145  T* out_result = nullptr)
146 {
147  const T ERR = -1;
148 
149  // make the call
150  T result = posixCall<T>(in_posixFunction);
151 
152  // set out param (if requested)
153  if (out_result)
154  *out_result = result;
155 
156  // return status
157  if (result == ERR)
158  return systemError(errno, in_errorLocation);
159  else
160  return Success();
161 }
162 
168 bool realUserIsRoot();
169 
175 Error restoreRoot();
176 
182 Error restorePrivileges();
183 
192 Error temporarilyDropPrivileges(const User& in_user, const Optional<GidType>& in_group);
193 
194 } // namespace posix
195 } // namespace system
196 } // namespace launcher_plugins
197 } // namespace rstudio
198 
199 #endif
rstudio::launcher_plugins::ErrorLocation
Class which represents the location of an error.
Definition: Error.hpp:74
rstudio::launcher_plugins::system::posix::IpAddress::Name
std::string Name
Definition: PosixSystem.hpp:58
rstudio::launcher_plugins::system::User
Class which represents a system user.
Definition: User.hpp:55
rstudio::launcher_plugins::system::posix::posixCall
T posixCall(const std::function< T()> &in_posixFunction)
Makes a posix call and handles EINTR retries.
Definition: PosixSystem.hpp:111
rstudio::launcher_plugins::system::posix::IpAddress::Address
std::string Address
Definition: PosixSystem.hpp:61
rstudio::launcher_plugins::system::posix::getIpAddresses
launcher_plugins::Error getIpAddresses(std::vector< IpAddress > &out_addresses, bool in_includeIPv6=false)
Gets the IP addresses of the machine running this process.
rstudio::launcher_plugins::system::posix::ignoreSignal
Error ignoreSignal(int in_signal)
Ignores a particular signal for this process.
rstudio::launcher_plugins::Success
Class which represents a successful operation (i.e. no error).
Definition: Error.hpp:437
rstudio::launcher_plugins::Error
Class which represents an error.
Definition: Error.hpp:174
rstudio::launcher_plugins::system::posix::IpAddress
Represents an IP address.
Definition: PosixSystem.hpp:55
rstudio::launcher_plugins::Optional
Container class which represents a value that may or may not be set.
Definition: Optional.hpp:38
rstudio::launcher_plugins::system::posix::enableCoreDumps
Error enableCoreDumps()
Enables core dumps for this process.
rstudio::launcher_plugins::system::posix::getEnvironmentVariable
std::string getEnvironmentVariable(const std::string &in_name)
Gets an environment variable from the system.