Apache Mesos
os.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_HPP__
14 #define __STOUT_OS_HPP__
15 
16 #include <fcntl.h>
17 #include <limits.h>
18 #include <signal.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <glog/logging.h>
24 #include <sys/types.h>
25 
26 #include <list>
27 #include <queue>
28 #include <set>
29 #include <string>
30 
31 #include <stout/bytes.hpp>
32 #include <stout/duration.hpp>
33 #include <stout/error.hpp>
34 #include <stout/exit.hpp>
35 #include <stout/foreach.hpp>
36 #include <stout/none.hpp>
37 #include <stout/nothing.hpp>
38 #include <stout/option.hpp>
39 #include <stout/path.hpp>
40 #include <stout/result.hpp>
41 #include <stout/strings.hpp>
42 #include <stout/try.hpp>
43 #include <stout/version.hpp>
44 
45 #include <stout/os/access.hpp>
46 #include <stout/os/bootid.hpp>
47 #include <stout/os/chdir.hpp>
48 #include <stout/os/chroot.hpp>
49 #include <stout/os/dup.hpp>
50 #include <stout/os/exec.hpp>
51 #include <stout/os/exists.hpp>
52 #include <stout/os/fcntl.hpp>
53 #include <stout/os/getenv.hpp>
54 #include <stout/os/int_fd.hpp>
55 #include <stout/os/kill.hpp>
56 #include <stout/os/ls.hpp>
57 #include <stout/os/lseek.hpp>
58 #include <stout/os/lsof.hpp>
59 #include <stout/os/mkdir.hpp>
60 #include <stout/os/mkdtemp.hpp>
61 #include <stout/os/mktemp.hpp>
62 #include <stout/os/os.hpp>
63 #include <stout/os/pagesize.hpp>
64 #include <stout/os/pipe.hpp>
65 #include <stout/os/process.hpp>
66 #include <stout/os/rename.hpp>
67 #include <stout/os/rm.hpp>
68 #include <stout/os/rmdir.hpp>
69 #include <stout/os/shell.hpp>
70 #include <stout/os/stat.hpp>
71 #include <stout/os/su.hpp>
72 #include <stout/os/temp.hpp>
73 #include <stout/os/touch.hpp>
74 #include <stout/os/utime.hpp>
75 #include <stout/os/wait.hpp>
76 #include <stout/os/xattr.hpp>
77 
78 #include <stout/os/raw/argv.hpp>
80 
81 // For readability, we minimize the number of #ifdef blocks in the code by
82 // splitting platform specific system calls into separate directories.
83 #ifdef __WINDOWS__
84 #include <stout/windows/os.hpp>
85 #else
86 #include <stout/posix/os.hpp>
87 #endif // __WINDOWS__
88 
89 
90 namespace os {
91 namespace libraries {
92 namespace Library {
93 
94 // Library prefix; e.g., the `lib` in `libprocess`. NOTE: there is no prefix
95 // on Windows; `libprocess.a` would be `process.lib`.
96 constexpr const char* prefix =
97 #ifdef __WINDOWS__
98  "";
99 #else
100  "lib";
101 #endif // __WINDOWS__
102 
103 
104 // The suffix for a shared library; e.g., `.so` on Linux.
105 constexpr const char* extension =
106 #ifdef __APPLE__
107  ".dylib";
108 #elif defined(__WINDOWS__)
109  ".dll";
110 #else
111  ".so";
112 #endif // __APPLE__
113 
114 
115 // The name of the environment variable that contains paths on which the
116 // linker should search for libraries. NOTE: Windows does not have an
117 // environment variable that controls the paths the linker searches through.
118 constexpr const char* ldPathEnvironmentVariable =
119 #ifdef __APPLE__
120  "DYLD_LIBRARY_PATH";
121 #elif defined(__WINDOWS__)
122  "";
123 #else
124  "LD_LIBRARY_PATH";
125 #endif
126 
127 } // namespace Library {
128 
129 // Returns the full library name by adding prefix and extension to
130 // library name.
131 inline std::string expandName(const std::string& libraryName)
132 {
133  return Library::prefix + libraryName + Library::extension;
134 }
135 
136 
137 // Returns the current value of LD_LIBRARY_PATH environment variable.
138 inline std::string paths()
139 {
141  return path.isSome() ? path.get() : std::string();
142 }
143 
144 
145 // Updates the value of LD_LIBRARY_PATH environment variable.
146 // Note that setPaths has an effect only for child processes
147 // launched after calling it.
148 inline void setPaths(const std::string& newPaths)
149 {
151 }
152 
153 
154 // Append newPath to the current value of LD_LIBRARY_PATH environment
155 // variable.
156 inline void appendPaths(const std::string& newPaths)
157 {
158  if (paths().empty()) {
159  setPaths(newPaths);
160  } else {
161  setPaths(paths() + ":" + newPaths);
162  }
163 }
164 
165 } // namespace libraries {
166 
167 
168 #ifdef __WINDOWS__
169 inline Try<std::string> sysname() = delete;
170 #else
171 // Return the operating system name (e.g. Linux).
173 {
174  Try<UTSInfo> info = uname();
175  if (info.isError()) {
176  return Error(info.error());
177  }
178 
179  return info->sysname;
180 }
181 #endif // __WINDOWS__
182 
183 
185 {
186  const Try<std::set<pid_t>> pids = os::pids();
187  if (pids.isError()) {
188  return Error(pids.error());
189  }
190 
191  std::list<Process> result;
192  foreach (pid_t pid, pids.get()) {
193  const Result<Process> process = os::process(pid);
194 
195  // Ignore any processes that disappear between enumeration and now.
196  if (process.isSome()) {
197  result.push_back(process.get());
198  }
199  }
200  return result;
201 }
202 
203 
205  pid_t pid,
206  const std::list<Process>& processes)
207 {
208  foreach (const Process& process, processes) {
209  if (process.pid == pid) {
210  return process;
211  }
212  }
213  return None();
214 }
215 
216 
217 inline std::set<pid_t> children(
218  pid_t pid,
219  const std::list<Process>& processes,
220  bool recursive = true)
221 {
222  // Perform a breadth first search for descendants.
223  std::set<pid_t> descendants;
224  std::queue<pid_t> parents;
225  parents.push(pid);
226 
227  do {
228  pid_t parent = parents.front();
229  parents.pop();
230 
231  // Search for children of parent.
232  foreach (const Process& process, processes) {
233  if (process.parent == parent) {
234  // Have we seen this child yet?
235  if (descendants.insert(process.pid).second) {
236  parents.push(process.pid);
237  }
238  }
239  }
240  } while (recursive && !parents.empty());
241 
242  return descendants;
243 }
244 
245 
246 inline Try<std::set<pid_t>> children(pid_t pid, bool recursive = true)
247 {
249 
250  if (processes.isError()) {
251  return Error(processes.error());
252  }
253 
254  return children(pid, processes.get(), recursive);
255 }
256 
257 } // namespace os {
258 
259 #endif // __STOUT_OS_HPP__
Definition: path.hpp:29
Try< std::string > sysname()
Definition: os.hpp:172
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
constexpr const char * extension
Definition: os.hpp:105
Try< std::list< Process > > processes()
Definition: os.hpp:184
constexpr const char * prefix
Definition: os.hpp:96
std::string paths()
Definition: os.hpp:138
Definition: posix_signalhandler.hpp:23
void setenv(const std::string &key, const std::string &value, bool overwrite=true)
Definition: os.hpp:157
Definition: check.hpp:30
bool isSome() const
Definition: option.hpp:116
void setPaths(const std::string &newPaths)
Definition: os.hpp:148
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
void appendPaths(const std::string &newPaths)
Definition: os.hpp:156
const pid_t pid
Definition: process.hpp:53
const T & get() const &
Definition: option.hpp:119
Option< std::string > getenv(const std::string &key)
Definition: getenv.hpp:29
constexpr const char * ldPathEnvironmentVariable
Definition: os.hpp:118
Try< UTSInfo > uname()
Definition: os.hpp:297
static Try error(const E &e)
Definition: try.hpp:43
const pid_t parent
Definition: process.hpp:54
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:78
T & get()&
Definition: result.hpp:116
std::set< pid_t > children(pid_t, const std::list< Process > &, bool)
Definition: os.hpp:217
Definition: executor.hpp:48
bool isSome() const
Definition: result.hpp:112
std::string expandName(const std::string &libraryName)
Definition: os.hpp:131
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62