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_POSIX_OS_HPP__
14 #define __STOUT_POSIX_OS_HPP__
15 
16 #include <errno.h>
17 #ifdef __sun
18 #include <sys/loadavg.h>
19 #define dirfd(dir) ((dir)->d_fd)
20 #ifndef NAME_MAX
21 #define NAME_MAX MAXNAMLEN
22 #endif // NAME_MAX
23 #else
24 #include <fts.h>
25 #endif // __sun
26 #include <glob.h>
27 #include <grp.h>
28 #include <limits.h>
29 #include <netdb.h>
30 #include <pwd.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <utime.h>
37 
38 #include <sys/ioctl.h>
39 
40 #ifdef __linux__
41 #include <linux/version.h>
42 #include <sys/sysinfo.h>
43 #endif // __linux__
44 
45 #include <sys/ioctl.h>
46 #include <sys/utsname.h>
47 #include <sys/wait.h>
48 
49 #include <list>
50 #include <map>
51 #include <mutex>
52 #include <set>
53 #include <string>
54 #include <vector>
55 
56 #include <stout/synchronized.hpp>
57 
58 #include <stout/os/close.hpp>
59 #include <stout/os/environment.hpp>
60 #include <stout/os/fcntl.hpp>
61 #include <stout/os/find.hpp>
62 #include <stout/os/fork.hpp>
63 #include <stout/os/getcwd.hpp>
64 #include <stout/os/killtree.hpp>
65 #include <stout/os/os.hpp>
66 #include <stout/os/permissions.hpp>
67 #include <stout/os/read.hpp>
68 #include <stout/os/rename.hpp>
69 #include <stout/os/sendfile.hpp>
70 #include <stout/os/signals.hpp>
71 #include <stout/os/strerror.hpp>
72 #include <stout/os/write.hpp>
73 
74 #ifdef __FreeBSD__
75 #include <stout/os/freebsd.hpp>
76 #endif
77 #ifdef __linux__
78 #include <stout/os/linux.hpp>
79 #endif // __linux__
80 #include <stout/os/open.hpp>
81 #ifdef __APPLE__
82 #include <stout/os/osx.hpp>
83 #endif // __APPLE__
84 #ifdef __sun
85 #include <stout/os/sunos.hpp>
86 #endif // __sun
87 
88 #include <stout/os/posix/chown.hpp>
90 
91 #include <stout/os/shell.hpp>
92 
93 namespace os {
94 
95 // Import `::gmtime_r` into `os::` namespace.
97 
98 // Import `::hstrerror` into `os::` namespace.
100 
101 // Import `::random` into `os::` namespace.
103 
104 // Forward declarations.
105 inline Try<Nothing> utime(const std::string&);
107 
108 
109 // Suspends execution of the calling process until a child specified by `pid`
110 // has changed state. Unlike the POSIX standard function `::waitpid`, this
111 // function does not use -1 and 0 to signify errors and nonblocking return.
112 // Instead, we return `Result<pid_t>`:
113 // * In case of error, we return `Error` rather than -1. For example, we
114 // would return an `Error` in case of `EINVAL`.
115 // * In case of nonblocking return, we return `None` rather than 0. For
116 // example, if we pass `WNOHANG` in the `options`, we would expect 0 to be
117 // returned in the case that children specified by `pid` exist, but have
118 // not changed state yet. In this case we return `None` instead.
119 //
120 // NOTE: There are important differences between the POSIX and Windows
121 // implementations of this function:
122 // * On POSIX, `pid_t` is a signed number, but on Windows, PIDs are `DWORD`,
123 // which is `unsigned long`. Thus, if we use `DWORD` to represent the `pid`
124 // argument, we would not be able to pass -1 as the `pid`.
125 // * Since it is important to be able to detect -1 has been passed to
126 // `os::waitpid`, as a matter of practicality, we choose to:
127 // (1) Use `long` to represent the `pid` argument.
128 // (2) Disable using any value <= 0 for `pid` on Windows.
129 // * This decision is pragmatic. The reasoning is:
130 // (1) The Windows code paths call `os::waitpid` in only a handful of
131 // places, and in none of these conditions do we need `-1` as a value.
132 // (2) Since PIDs virtually never take on values outside the range of
133 // vanilla signed `long` it is likely that an accidental conversion
134 // will never happen.
135 // (3) Even though it is not formalized in the C specification, the
136 // implementation of `long` on the vast majority of production servers
137 // is 2's complement, so we expect that when we accidentally do
138 // implicitly convert from `unsigned long` to `long`, we will "wrap
139 // around" to negative values. And since we've disabled the negative
140 // `pid` in the Windows implementation, we should error out.
141 inline Result<pid_t> waitpid(pid_t pid, int* status, int options)
142 {
143  const pid_t child_pid = ::waitpid(pid, status, options);
144 
145  if (child_pid == 0) {
146  return None();
147  } else if (child_pid < 0) {
148  return ErrnoError("os::waitpid: Call to `waitpid` failed");
149  } else {
150  return child_pid;
151  }
152 }
153 
154 
155 // Sets the value associated with the specified key in the set of
156 // environment variables.
157 inline void setenv(const std::string& key,
158  const std::string& value,
159  bool overwrite = true)
160 {
161  ::setenv(key.c_str(), value.c_str(), overwrite ? 1 : 0);
162 }
163 
164 
165 // Unsets the value associated with the specified key in the set of
166 // environment variables.
167 inline void unsetenv(const std::string& key)
168 {
169  // NOTE: This function explicitly does not clear from
170  // `/proc/$pid/environ` because that is defined to be the initial
171  // environment that was set when the process was started, so don't
172  // use this to delete secrets! Instead, see `os::eraseenv`.
173  ::unsetenv(key.c_str());
174 }
175 
176 
177 // Erases the value associated with the specified key from the set of
178 // environment variables. By erase, we even clear it from
179 // `/proc/$pid/environ` so that sensitive information conveyed via
180 // environment variables (such as secrets) can be cleaned up.
181 inline void eraseenv(const std::string& key)
182 {
183  char* value = ::getenv(key.c_str());
184 
185  // Erase the old value so that on Linux it can't be inspected
186  // through `/proc/$pid/environ`, useful for secrets.
187  if (value) {
188  ::memset(value, '\0', ::strlen(value));
189  }
190 
191  ::unsetenv(key.c_str());
192 }
193 
194 
195 inline Try<Nothing> chmod(const std::string& path, int mode)
196 {
197  if (::chmod(path.c_str(), mode) < 0) {
198  return ErrnoError();
199  }
200 
201  return Nothing();
202 }
203 
204 
206  const std::string& path,
207  mode_t mode,
208  dev_t dev)
209 {
210  if (::mknod(path.c_str(), mode, dev) < 0) {
211  return ErrnoError();
212  }
213 
214  return Nothing();
215 }
216 
217 
218 // Suspends execution for the given duration.
219 inline Try<Nothing> sleep(const Duration& duration)
220 {
221  timespec remaining;
222  remaining.tv_sec = static_cast<long>(duration.secs());
223  remaining.tv_nsec =
224  static_cast<long>((duration - Seconds(remaining.tv_sec)).ns());
225 
226  while (nanosleep(&remaining, &remaining) == -1) {
227  if (errno == EINTR) {
228  continue;
229  } else {
230  return ErrnoError();
231  }
232  }
233 
234  return Nothing();
235 }
236 
237 
238 // Returns the list of files that match the given (shell) pattern.
239 inline Try<std::list<std::string>> glob(const std::string& pattern)
240 {
241  glob_t g;
242  int status = ::glob(pattern.c_str(), GLOB_NOSORT, nullptr, &g);
243 
244  std::list<std::string> result;
245 
246  if (status != 0) {
247  if (status == GLOB_NOMATCH) {
248  return result; // Empty list.
249  } else {
250  return ErrnoError();
251  }
252  }
253 
254  for (size_t i = 0; i < g.gl_pathc; ++i) {
255  result.push_back(g.gl_pathv[i]);
256  }
257 
258  globfree(&g); // Best-effort free of dynamically allocated memory.
259 
260  return result;
261 }
262 
263 
264 // Returns the total number of cpus (cores).
265 inline Try<long> cpus()
266 {
267  long cpus = sysconf(_SC_NPROCESSORS_ONLN);
268 
269  if (cpus < 0) {
270  return ErrnoError();
271  }
272  return cpus;
273 }
274 
275 
276 // Returns load struct with average system loads for the last
277 // 1, 5 and 15 minutes respectively.
278 // Load values should be interpreted as usual average loads from
279 // uptime(1).
281 {
282  double loadArray[3];
283  if (getloadavg(loadArray, 3) == -1) {
284  return ErrnoError("Failed to determine system load averages");
285  }
286 
287  Load load;
288  load.one = loadArray[0];
289  load.five = loadArray[1];
290  load.fifteen = loadArray[2];
291 
292  return load;
293 }
294 
295 
296 // Return the system information.
298 {
299  struct utsname name;
300 
301  if (::uname(&name) < 0) {
302  return ErrnoError();
303  }
304 
305  UTSInfo info;
306  info.sysname = name.sysname;
307  info.nodename = name.nodename;
308  info.release = name.release;
309  info.version = name.version;
310  info.machine = name.machine;
311  return info;
312 }
313 
314 
315 // Overload of os::pids for filtering by groups and sessions.
316 // A group / session id of 0 will fitler on the group / session ID
317 // of the calling process.
319 {
320  if (group.isNone() && session.isNone()) {
321  return os::pids();
322  } else if (group.isSome() && group.get() < 0) {
323  return Error("Invalid group");
324  } else if (session.isSome() && session.get() < 0) {
325  return Error("Invalid session");
326  }
327 
329 
330  if (processes.isError()) {
331  return Error(processes.error());
332  }
333 
334  // Obtain the calling process group / session ID when 0 is provided.
335  if (group.isSome() && group.get() == 0) {
336  group = getpgid(0);
337  }
338  if (session.isSome() && session.get() == 0) {
339  session = getsid(0);
340  }
341 
342  std::set<pid_t> result;
343  foreach (const Process& process, processes.get()) {
344  // Group AND Session (intersection).
345  if (group.isSome() && session.isSome()) {
346  if (group.get() == process.group &&
347  process.session.isSome() &&
348  session.get() == process.session.get()) {
349  result.insert(process.pid);
350  }
351  } else if (group.isSome() && group.get() == process.group) {
352  result.insert(process.pid);
353  } else if (session.isSome() && process.session.isSome() &&
354  session.get() == process.session.get()) {
355  result.insert(process.pid);
356  }
357  }
358 
359  return result;
360 }
361 
362 
363 // Creates a tar 'archive' with gzip compression, of the given 'path'.
364 inline Try<Nothing> tar(const std::string& path, const std::string& archive)
365 {
366  Try<std::string> tarOut =
367  os::shell("tar %s %s %s", "-czf", archive.c_str(), path.c_str());
368 
369  if (tarOut.isError()) {
370  return Error("Failed to archive " + path + ": " + tarOut.error());
371  }
372 
373  return Nothing();
374 }
375 
376 
377 // Return the OS release numbers.
379 {
380  Try<UTSInfo> info = uname();
381  if (info.isError()) {
382  return Error(info.error());
383  }
384 
385  int major, minor, patch = 0;
386 #ifndef __FreeBSD__
387  // TODO(karya): Replace sscanf with Version::parse() once Version
388  // starts supporting labels and build metadata.
389  if (::sscanf(
390  info->release.c_str(),
391  "%d.%d.%d",
392  &major,
393  &minor,
394  &patch) != 3) {
395  return Error("Failed to parse: " + info->release);
396  }
397 #else
398  // TODO(dforsyth): Handle FreeBSD patch versions (-pX).
399  if (::sscanf(info->release.c_str(), "%d.%d-%*s", &major, &minor) != 2) {
400  return Error("Failed to parse: " + info->release);
401  }
402 #endif
403  return Version(major, minor, patch);
404 }
405 
406 
408 {
409  return "/var";
410 }
411 
412 
413 inline Try<Nothing> dup2(int oldFd, int newFd)
414 {
415  while (::dup2(oldFd, newFd) == -1) {
416  if (errno == EINTR) {
417  continue;
418  } else {
419  return ErrnoError();
420  }
421  }
422  return Nothing();
423 }
424 
425 
427 {
428  // 'ptsname' is not thread safe. Therefore, we use mutex here to
429  // make this method thread safe.
430  // TODO(jieyu): Consider using ptsname_r for linux.
431  static std::mutex* mutex = new std::mutex;
432 
433  synchronized (mutex) {
434  const char* slavePath = ::ptsname(master);
435  if (slavePath == nullptr) {
436  return ErrnoError();
437  }
438  return slavePath;
439  }
440 }
441 
442 
443 inline Try<Nothing> setctty(int fd)
444 {
445  if (ioctl(fd, TIOCSCTTY, nullptr) == -1) {
446  return ErrnoError();
447  }
448 
449  return Nothing();
450 }
451 
452 
453 // Update the window size for
454 // the terminal represented by fd.
456  int fd,
457  unsigned short rows,
458  unsigned short columns)
459 {
460  struct winsize winsize;
461  winsize.ws_row = rows;
462  winsize.ws_col = columns;
463 
464  if (ioctl(fd, TIOCSWINSZ, &winsize) != 0) {
465  return ErrnoError();
466  }
467 
468  return Nothing();
469 }
470 
471 
472 // Returns a host-specific default for the `PATH` environment variable, based
473 // on the configuration of the host.
474 inline std::string host_default_path()
475 {
476  return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
477 }
478 
479 } // namespace os {
480 
481 #endif // __STOUT_POSIX_OS_HPP__
Try< Nothing > dup2(int oldFd, int newFd)
Definition: os.hpp:413
Definition: path.hpp:29
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Try< std::string > patch(const std::string &s, const Diff &diff)
Definition: svn.hpp:157
Definition: master.hpp:27
Try< Nothing > chmod(const std::string &path, int mode)
Definition: os.hpp:195
Definition: check.hpp:33
Try< Nothing > sleep(const Duration &duration)
Definition: os.hpp:219
const pid_t group
Definition: process.hpp:55
Try< std::list< Process > > processes()
Definition: os.hpp:184
Try< Nothing > mknod(const std::string &path, mode_t mode, dev_t dev)
Definition: os.hpp:205
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
const Option< pid_t > session
Definition: process.hpp:56
void setenv(const std::string &key, const std::string &value, bool overwrite=true)
Definition: os.hpp:157
void unsetenv(const std::string &key)
Definition: os.hpp:167
Definition: duration.hpp:32
Definition: check.hpp:30
std::string host_default_path()
Definition: os.hpp:474
bool isSome() const
Definition: option.hpp:116
std::string release
Definition: os.hpp:48
Definition: os.hpp:25
std::string version
Definition: os.hpp:49
Try< Load > loadavg()
Definition: os.hpp:280
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
int mode_t
Definition: windows.hpp:177
Try< dev_t > dev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:181
const pid_t pid
Definition: process.hpp:53
Try< Nothing > utime(const std::string &path)
Definition: utime.hpp:32
Try< long > cpus()
Definition: os.hpp:265
Definition: duration.hpp:207
Try< Nothing > tar(const std::string &path, const std::string &archive)
Definition: os.hpp:364
std::string hstrerror(int err)=delete
Try< Nothing > setctty(int fd)
Definition: os.hpp:443
const T & get() const &
Definition: option.hpp:119
Option< std::string > getenv(const std::string &key)
Definition: getenv.hpp:29
int random()
Definition: os.hpp:580
Try< Version > release()
Definition: os.hpp:378
double secs() const
Definition: duration.hpp:49
Result< pid_t > waitpid(pid_t pid, int *status, int options)
Definition: os.hpp:141
std::string sysname
Definition: os.hpp:46
Try< UTSInfo > uname()
Definition: os.hpp:297
static Try error(const E &e)
Definition: try.hpp:43
Try< std::string > shell(const std::string &fmt, const T &...t)
Definition: shell.hpp:49
Definition: grp.hpp:26
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:78
std::string nodename
Definition: os.hpp:47
Definition: ns.hpp:86
Definition: executor.hpp:48
void eraseenv(const std::string &key)
Definition: os.hpp:181
Definition: version.hpp:41
double fifteen
Definition: os.hpp:28
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:168
bool isNone() const
Definition: option.hpp:117
Try< std::string > var()
Definition: os.hpp:407
Try< std::list< std::string > > glob(const std::string &pattern)
Definition: os.hpp:239
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62
double five
Definition: os.hpp:27
std::string machine
Definition: os.hpp:50
Try< Nothing > setWindowSize(int fd, unsigned short rows, unsigned short columns)
Definition: os.hpp:455
Definition: os.hpp:44
tm * gmtime_r(const time_t *timep, tm *result)
Definition: os.hpp:431
double one
Definition: os.hpp:26
Try< std::string > ptsname(int master)
Definition: os.hpp:426