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