Apache Mesos
pstree.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_PSTREE_HPP__
14 #define __STOUT_OS_PSTREE_HPP__
15 
16 #include <list>
17 #include <set>
18 
19 #include <stout/error.hpp>
20 #include <stout/foreach.hpp>
21 #include <stout/none.hpp>
22 #include <stout/option.hpp>
23 #include <stout/stringify.hpp>
24 #include <stout/try.hpp>
25 
26 #include <stout/os/process.hpp>
27 
28 
29 namespace os {
30 
31 // Forward declaration.
33 
34 
35 // Returns a process tree rooted at the specified pid using the
36 // specified list of processes (or an error if one occurs).
38  pid_t pid,
39  const std::list<Process>& processes)
40 {
41  std::list<ProcessTree> children;
42  foreach (const Process& process, processes) {
43  if (process.parent == pid) {
44  Try<ProcessTree> tree = pstree(process.pid, processes);
45  if (tree.isError()) {
46  return Error(tree.error());
47  }
48  children.push_back(tree.get());
49  }
50  }
51 
52  foreach (const Process& process, processes) {
53  if (process.pid == pid) {
54  return ProcessTree(process, children);
55  }
56  }
57 
58  return Error("No process found at " + stringify(pid));
59 }
60 
61 
62 // Returns a process tree for the specified pid (or all processes if
63 // pid is none or the current process if pid is 0).
65 {
66  if (pid.isNone()) {
67  pid = 1;
68  } else if (pid.get() == 0) {
69  pid = getpid();
70  }
71 
73 
74  if (processes.isError()) {
75  return Error(processes.error());
76  }
77 
78  return pstree(pid.get(), processes.get());
79 }
80 
81 
82 // Returns the minimum list of process trees that include all of the
83 // specified pids using the specified list of processes.
85  const std::set<pid_t>& pids,
86  const std::list<Process>& processes)
87 {
88  std::list<ProcessTree> trees;
89 
90  foreach (pid_t pid, pids) {
91  // First, check if the pid is already connected to one of the
92  // process trees we've constructed.
93  bool disconnected = true;
94  foreach (const ProcessTree& tree, trees) {
95  if (tree.contains(pid)) {
96  disconnected = false;
97  break;
98  }
99  }
100 
101  if (disconnected) {
102  Try<ProcessTree> tree = pstree(pid, processes);
103  if (tree.isError()) {
104  return Error(tree.error());
105  }
106 
107  // Now see if any of the existing process trees are actually
108  // contained within the process tree we just created and only
109  // include the disjoint process trees.
110  // C++11:
111  // trees = trees.filter([](const ProcessTree& t) {
112  // return tree.get().contains(t);
113  // });
114  std::list<ProcessTree> trees_ = trees;
115  trees.clear();
116  foreach (const ProcessTree& t, trees_) {
117  if (tree->contains(t.process.pid)) {
118  continue;
119  }
120  trees.push_back(t);
121  }
122  trees.push_back(tree.get());
123  }
124  }
125 
126  return trees;
127 }
128 
129 } // namespace os {
130 
131 #endif // __STOUT_OS_PSTREE_HPP__
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
bool contains(pid_t pid) const
Definition: process.hpp:96
const Process process
Definition: process.hpp:111
Try< std::list< ProcessTree > > pstrees(const std::set< pid_t > &, const std::list< Process > &)
Definition: pstree.hpp:84
Try< std::list< Process > > processes()
Definition: os.hpp:182
Definition: posix_signalhandler.hpp:23
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
const pid_t pid
Definition: process.hpp:53
static Try error(const E &e)
Definition: try.hpp:42
const pid_t parent
Definition: process.hpp:54
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
Try< ProcessTree > pstree(pid_t pid, const std::list< Process > &processes)
Definition: pstree.hpp:37
std::string stringify(int flags)
Definition: process.hpp:74
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62