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