Apache Mesos
kill.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_KILL_HPP__
14 #define __STOUT_OS_WINDOWS_KILL_HPP__
15 
16 #include <glog/logging.h>
17 
18 #include <stout/os.hpp>
19 #include <stout/windows.hpp>
20 
21 namespace os {
22 
23 const int KILL_PASS = 0; // Success return for `kill` function.
24 const int KILL_FAIL = -1; // Error return for `kill` function.
25 
26 namespace internal {
27 
28 inline int kill_process(pid_t pid)
29 {
30  HANDLE process_handle = ::OpenProcess(PROCESS_TERMINATE, FALSE, pid);
31  if (process_handle == nullptr) {
32  LOG(ERROR) << "os::kill_process(): Failed call to OpenProcess";
33 
34  return KILL_FAIL;
35  }
36 
37  SharedHandle safe_process_handle(process_handle, ::CloseHandle);
38 
39  if (::TerminateProcess(safe_process_handle.get_handle(), 1) == 0) {
40  LOG(ERROR) << "os::kill_process(): Failed call to TerminateProcess";
41 
42  return KILL_FAIL;
43  }
44 
45  return KILL_PASS;
46 }
47 
48 } // namespace internal {
49 
50 
51 inline int kill(pid_t pid, int sig)
52 {
53  // SIGCONT and SIGSTOP are not supported.
54  // SIGKILL calls TerminateProcess.
55  // SIGTERM has the same behaviour as SIGKILL.
56 
57  if (sig == SIGKILL || sig == SIGTERM) {
58  return os::internal::kill_process(pid);
59  }
60 
61  LOG(ERROR) << "Failed call to os::kill(): "
62  << "Signal value: '" << sig << "' is not handled. "
63  << "Valid Signal values for Windows os::kill() are "
64  << "'SIGTERM' and 'SIGKILL'";
65 
66  return KILL_FAIL;
67 }
68 
69 } // namespace os {
70 
71 #endif // __STOUT_OS_WINDOWS_KILL_HPP__
HANDLE get_handle() const
Definition: windows.hpp:90
Definition: windows.hpp:72
const mode_t SIGKILL
Definition: windows.hpp:335
const int KILL_PASS
Definition: kill.hpp:23
const int KILL_FAIL
Definition: kill.hpp:24
Definition: posix_signalhandler.hpp:23
DWORD pid_t
Definition: windows.hpp:181
constexpr Handle HANDLE
Definition: ingress.hpp:37
int kill_process(pid_t pid)
Definition: kill.hpp:28
int kill(pid_t pid, int sig)
Definition: kill.hpp:21
Definition: attributes.hpp:24