Apache Mesos
sunos.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_SUNOS_HPP__
14 #define __STOUT_OS_SUNOS_HPP__
15 
16 // This file contains Solaris-only OS utilities.
17 #ifndef __sun
18 #error "stout/os/sunos.hpp is only available on Solaris systems."
19 #endif
20 
21 #include <fcntl.h>
22 #include <procfs.h>
23 #include <sys/types.h> // For pid_t.
24 
25 #include <list>
26 #include <set>
27 #include <string>
28 
29 #include <stout/error.hpp>
30 #include <stout/foreach.hpp>
31 #include <stout/option.hpp>
32 #include <stout/result.hpp>
33 #include <stout/try.hpp>
34 
35 #include <stout/os/int_fd.hpp>
36 #include <stout/os/open.hpp>
37 #include <stout/os/process.hpp>
38 
39 namespace os {
40 
41 inline Result<Process> process(pid_t pid)
42 {
43  std::string fn = "/proc/" + stringify(pid) + "/status";
44  struct pstatus pstatus;
45 
46  Try<int_fd> fd = os::open(fn, O_RDONLY);
47  if (fd.isError()) {
48  return ErrnoError("Cannot open " + fn);
49  }
50 
51  if (::read(fd.get(), &pstatus, sizeof(pstatus)) != sizeof(pstatus)) {
52  os::close(fd.get());
53  return Error("Cannot read from " + fn);
54  }
55  os::close(fd.get());
56 
57  fn = "/proc/" + stringify(pid) + "/psinfo";
58  struct psinfo psinfo;
59 
60  fd = os::open(fn, O_RDONLY);
61  if (fd.isError()) {
62  return ErrnoError("Cannot open " + fn);
63  }
64 
65  if (::read(fd.get(), &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
66  os::close(fd.get());
67  return Error("Cannot read from " + fn);
68  }
69  os::close(fd.get());
70 
72  Seconds(pstatus.pr_utime.tv_sec) + Nanoseconds(pstatus.pr_utime.tv_nsec);
73  Try<Duration> stime =
74  Seconds(pstatus.pr_stime.tv_sec) + Nanoseconds(pstatus.pr_stime.tv_nsec);
75 
76  return Process(pstatus.pr_pid,
77  pstatus.pr_ppid,
78  pstatus.pr_ppid,
79  pstatus.pr_sid,
80  None(),
81  utime.isSome() ? utime.get() : Option<Duration>::none(),
82  stime.isSome() ? stime.get() : Option<Duration>::none(),
83  psinfo.pr_fname,
84  (psinfo.pr_nzomb == 0) &&
85  (psinfo.pr_nlwp == 0) &&
86  (psinfo.pr_lwp.pr_lwpid == 0));
87 }
88 
89 // Reads from /proc and returns a list of all running processes.
90 inline Try<std::set<pid_t>> pids()
91 {
92  std::set<pid_t> pids;
93 
94  Try<std::list<std::string>> entries = os::ls("/proc");
95  if (entries.isError()) {
96  return Error("Failed to list files in /proc: " + entries.error());
97  }
98 
99  foreach (const std::string& entry, entries.get()) {
100  Try<pid_t> pid = numify<pid_t>(entry);
101  if (pid.isSome()) {
102  pids.insert(pid.get()); // Ignore entries that can't be numified.
103  }
104  }
105 
106  if (!pids.empty()) {
107  return pids;
108  }
109 
110  return Error("Failed to determine pids from /proc");
111 }
112 
113 
114 // Returns the total size of main and free memory.
115 inline Try<Memory> memory()
116 {
117  return Error("Cannot determine the size of total and free memory");
118 }
119 
120 } // namespace os {
121 
122 #endif // __STOUT_OS_SUNOS_HPP__
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
Try< int_fd > open(const std::string &path, int oflag, mode_t mode=0)
Definition: open.hpp:35
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
Definition: check.hpp:30
DWORD pid_t
Definition: windows.hpp:181
Try< Nothing > close(int fd)
Definition: close.hpp:24
Try< Nothing > utime(const std::string &path)
Definition: utime.hpp:32
Definition: duration.hpp:207
static Option< T > none()
Definition: option.hpp:32
bool isSome() const
Definition: try.hpp:77
static Try error(const E &e)
Definition: try.hpp:43
Result< std::string > read(int_fd fd, size_t size)
Definition: read.hpp:55
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:78
Definition: duration.hpp:165
std::string stringify(int flags)
Try< Memory > memory()
Definition: freebsd.hpp:78
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62
Try< std::list< std::string > > ls(const std::string &directory)
Definition: ls.hpp:29