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