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/exists.hpp>
51 #include <stout/os/fcntl.hpp>
52 #include <stout/os/getenv.hpp>
53 #include <stout/os/int_fd.hpp>
54 #include <stout/os/kill.hpp>
55 #include <stout/os/ls.hpp>
56 #include <stout/os/lseek.hpp>
57 #include <stout/os/lsof.hpp>
58 #include <stout/os/mkdir.hpp>
59 #include <stout/os/mkdtemp.hpp>
60 #include <stout/os/mktemp.hpp>
61 #include <stout/os/os.hpp>
62 #include <stout/os/pagesize.hpp>
63 #include <stout/os/pipe.hpp>
64 #include <stout/os/process.hpp>
65 #include <stout/os/rename.hpp>
66 #include <stout/os/rm.hpp>
67 #include <stout/os/rmdir.hpp>
68 #include <stout/os/shell.hpp>
69 #include <stout/os/stat.hpp>
70 #include <stout/os/su.hpp>
71 #include <stout/os/temp.hpp>
72 #include <stout/os/touch.hpp>
73 #include <stout/os/utime.hpp>
74 #include <stout/os/wait.hpp>
75 #include <stout/os/xattr.hpp>
76 
77 #include <stout/os/raw/argv.hpp>
79 
80 // For readability, we minimize the number of #ifdef blocks in the code by
81 // splitting platform specific system calls into separate directories.
82 #ifdef __WINDOWS__
83 #include <stout/windows/os.hpp>
84 #else
85 #include <stout/posix/os.hpp>
86 #endif // __WINDOWS__
87 
88 
89 namespace os {
90 namespace libraries {
91 namespace Library {
92 
93 // Library prefix; e.g., the `lib` in `libprocess`. NOTE: there is no prefix
94 // on Windows; `libprocess.a` would be `process.lib`.
95 constexpr const char* prefix =
96 #ifdef __WINDOWS__
97  "";
98 #else
99  "lib";
100 #endif // __WINDOWS__
101 
102 
103 // The suffix for a shared library; e.g., `.so` on Linux.
104 constexpr const char* extension =
105 #ifdef __APPLE__
106  ".dylib";
107 #elif defined(__WINDOWS__)
108  ".dll";
109 #else
110  ".so";
111 #endif // __APPLE__
112 
113 
114 // The name of the environment variable that contains paths on which the
115 // linker should search for libraries. NOTE: Windows does not have an
116 // environment variable that controls the paths the linker searches through.
117 constexpr const char* ldPathEnvironmentVariable =
118 #ifdef __APPLE__
119  "DYLD_LIBRARY_PATH";
120 #elif defined(__WINDOWS__)
121  "";
122 #else
123  "LD_LIBRARY_PATH";
124 #endif
125 
126 } // namespace Library {
127 
128 // Returns the full library name by adding prefix and extension to
129 // library name.
130 inline std::string expandName(const std::string& libraryName)
131 {
132  return Library::prefix + libraryName + Library::extension;
133 }
134 
135 
136 // Returns the current value of LD_LIBRARY_PATH environment variable.
137 inline std::string paths()
138 {
140  return path.isSome() ? path.get() : std::string();
141 }
142 
143 
144 // Updates the value of LD_LIBRARY_PATH environment variable.
145 // Note that setPaths has an effect only for child processes
146 // launched after calling it.
147 inline void setPaths(const std::string& newPaths)
148 {
150 }
151 
152 
153 // Append newPath to the current value of LD_LIBRARY_PATH environment
154 // variable.
155 inline void appendPaths(const std::string& newPaths)
156 {
157  if (paths().empty()) {
158  setPaths(newPaths);
159  } else {
160  setPaths(paths() + ":" + newPaths);
161  }
162 }
163 
164 } // namespace libraries {
165 
166 
167 #ifdef __WINDOWS__
168 inline Try<std::string> sysname() = delete;
169 #else
170 // Return the operating system name (e.g. Linux).
172 {
173  Try<UTSInfo> info = uname();
174  if (info.isError()) {
175  return Error(info.error());
176  }
177 
178  return info->sysname;
179 }
180 #endif // __WINDOWS__
181 
182 
184 {
185  const Try<std::set<pid_t>> pids = os::pids();
186  if (pids.isError()) {
187  return Error(pids.error());
188  }
189 
190  std::list<Process> result;
191  foreach (pid_t pid, pids.get()) {
192  const Result<Process> process = os::process(pid);
193 
194  // Ignore any processes that disappear between enumeration and now.
195  if (process.isSome()) {
196  result.push_back(process.get());
197  }
198  }
199  return result;
200 }
201 
202 
204  pid_t pid,
205  const std::list<Process>& processes)
206 {
207  foreach (const Process& process, processes) {
208  if (process.pid == pid) {
209  return process;
210  }
211  }
212  return None();
213 }
214 
215 
216 inline std::set<pid_t> children(
217  pid_t pid,
218  const std::list<Process>& processes,
219  bool recursive = true)
220 {
221  // Perform a breadth first search for descendants.
222  std::set<pid_t> descendants;
223  std::queue<pid_t> parents;
224  parents.push(pid);
225 
226  do {
227  pid_t parent = parents.front();
228  parents.pop();
229 
230  // Search for children of parent.
231  foreach (const Process& process, processes) {
232  if (process.parent == parent) {
233  // Have we seen this child yet?
234  if (descendants.insert(process.pid).second) {
235  parents.push(process.pid);
236  }
237  }
238  }
239  } while (recursive && !parents.empty());
240 
241  return descendants;
242 }
243 
244 
245 inline Try<std::set<pid_t>> children(pid_t pid, bool recursive = true)
246 {
248 
249  if (processes.isError()) {
250  return Error(processes.error());
251  }
252 
253  return children(pid, processes.get(), recursive);
254 }
255 
256 } // namespace os {
257 
258 #endif // __STOUT_OS_HPP__
Definition: path.hpp:29
Try< std::string > sysname()
Definition: os.hpp:171
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
constexpr const char * extension
Definition: os.hpp:104
Try< std::list< Process > > processes()
Definition: os.hpp:183
constexpr const char * prefix
Definition: os.hpp:95
std::string paths()
Definition: os.hpp:137
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:147
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
void appendPaths(const std::string &newPaths)
Definition: os.hpp:155
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:117
Try< UTSInfo > uname()
Definition: os.hpp:318
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:216
Definition: executor.hpp:48
bool isSome() const
Definition: result.hpp:112
std::string expandName(const std::string &libraryName)
Definition: os.hpp:130
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62