Apache Mesos
killtree.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_WINDOWS_KILLTREE_HPP__
14 #define __STOUT_OS_WINDOWS_KILLTREE_HPP__
15 
16 #include <stout/os.hpp>
17 #include <stout/try.hpp>
18 #include <stout/windows.hpp> // For `SharedHandle` and `pid_t`.
19 
21 
22 namespace os {
23 
24 // Terminate the "process tree" rooted at the specified pid.
25 // Since there is no process tree concept on Windows,
26 // internally this function looks up the job object for the given pid
27 // and terminates the job. This is possible because `name_job`
28 // provides an idempotent one-to-one mapping from pid to name.
30  pid_t pid,
31  int signal,
32  bool groups = false,
33  bool sessions = false)
34 {
35  const Try<std::wstring> name = os::name_job(pid);
36  if (name.isError()) {
37  return Error("Failed to determine job object name: " + name.error());
38  }
39 
40  Try<SharedHandle> handle =
41  os::open_job(JOB_OBJECT_TERMINATE, false, name.get());
42  if (handle.isError()) {
43  return Error("Failed to open job object: " + handle.error());
44  }
45 
46  Try<Nothing> killJobResult = os::kill_job(handle.get());
47  if (killJobResult.isError()) {
48  return Error("Failed to delete job object: " + killJobResult.error());
49  }
50 
51  // NOTE: This return value is unused. A future refactor
52  // may change the return type to `Try<None>`.
53  std::list<ProcessTree> process_tree_list;
54  return process_tree_list;
55 }
56 
57 } // namespace os {
58 
59 #endif // __STOUT_OS_WINDOWS_KILLTREE_HPP__
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
Definition: posix_signalhandler.hpp:23
Try< std::wstring > name_job(pid_t pid)
Definition: jobobject.hpp:39
Try< SharedHandle > open_job(const DWORD desired_access, const BOOL inherit_handles, const std::wstring &name)
Definition: jobobject.hpp:54
DWORD pid_t
Definition: windows.hpp:181
Try< std::list< ProcessTree > > killtree(pid_t pid, int signal, bool groups=false, bool sessions=false)
Definition: killtree.hpp:58
Try< Nothing > kill_job(SharedHandle job_handle)
Definition: jobobject.hpp:381
static Try error(const E &e)
Definition: try.hpp:42
bool isError() const
Definition: try.hpp:71
constexpr const char * name
Definition: shell.hpp:43