OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/kill.h" | 5 #include "base/process/kill.h" |
6 | 6 |
7 #include <signal.h> | 7 #include <signal.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/wait.h> | 9 #include <sys/wait.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
16 #include "base/process/process_iterator.h" | 16 #include "base/process/process_iterator.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
19 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 TerminationStatus GetTerminationStatusImpl(ProcessHandle handle, | 24 TerminationStatus GetTerminationStatusImpl(ProcessHandle handle, |
26 bool can_block, | 25 bool can_block, |
27 int* exit_code) { | 26 int* exit_code) { |
28 int status = 0; | 27 int status = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 61 |
63 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) | 62 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
64 return TERMINATION_STATUS_ABNORMAL_TERMINATION; | 63 return TERMINATION_STATUS_ABNORMAL_TERMINATION; |
65 | 64 |
66 return TERMINATION_STATUS_NORMAL_TERMINATION; | 65 return TERMINATION_STATUS_NORMAL_TERMINATION; |
67 } | 66 } |
68 | 67 |
69 } // namespace | 68 } // namespace |
70 | 69 |
71 #if !defined(OS_NACL_NONSFI) | 70 #if !defined(OS_NACL_NONSFI) |
72 // Attempts to kill the process identified by the given process | |
73 // entry structure. Ignores specified exit_code; posix can't force that. | |
74 // Returns true if this is successful, false otherwise. | |
75 bool KillProcess(ProcessHandle process_id, int exit_code, bool wait) { | |
76 DCHECK_GT(process_id, 1) << " tried to kill invalid process_id"; | |
77 if (process_id <= 1) | |
78 return false; | |
79 bool result = kill(process_id, SIGTERM) == 0; | |
80 if (result && wait) { | |
81 int tries = 60; | |
82 | |
83 if (RunningOnValgrind()) { | |
84 // Wait for some extra time when running under Valgrind since the child | |
85 // processes may take some time doing leak checking. | |
86 tries *= 2; | |
87 } | |
88 | |
89 unsigned sleep_ms = 4; | |
90 | |
91 // The process may not end immediately due to pending I/O | |
92 bool exited = false; | |
93 while (tries-- > 0) { | |
94 pid_t pid = HANDLE_EINTR(waitpid(process_id, NULL, WNOHANG)); | |
95 if (pid == process_id) { | |
96 exited = true; | |
97 break; | |
98 } | |
99 if (pid == -1) { | |
100 if (errno == ECHILD) { | |
101 // The wait may fail with ECHILD if another process also waited for | |
102 // the same pid, causing the process state to get cleaned up. | |
103 exited = true; | |
104 break; | |
105 } | |
106 DPLOG(ERROR) << "Error waiting for process " << process_id; | |
107 } | |
108 | |
109 usleep(sleep_ms * 1000); | |
110 const unsigned kMaxSleepMs = 1000; | |
111 if (sleep_ms < kMaxSleepMs) | |
112 sleep_ms *= 2; | |
113 } | |
114 | |
115 // If we're waiting and the child hasn't died by now, force it | |
116 // with a SIGKILL. | |
117 if (!exited) | |
118 result = kill(process_id, SIGKILL) == 0; | |
119 } | |
120 | |
121 if (!result) | |
122 DPLOG(ERROR) << "Unable to terminate process " << process_id; | |
123 | |
124 return result; | |
125 } | |
126 | |
127 bool KillProcessGroup(ProcessHandle process_group_id) { | 71 bool KillProcessGroup(ProcessHandle process_group_id) { |
128 bool result = kill(-1 * process_group_id, SIGKILL) == 0; | 72 bool result = kill(-1 * process_group_id, SIGKILL) == 0; |
129 if (!result) | 73 if (!result) |
130 DPLOG(ERROR) << "Unable to terminate process group " << process_group_id; | 74 DPLOG(ERROR) << "Unable to terminate process group " << process_group_id; |
131 return result; | 75 return result; |
132 } | 76 } |
133 #endif // !defined(OS_NACL_NONSFI) | 77 #endif // !defined(OS_NACL_NONSFI) |
134 | 78 |
135 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { | 79 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
136 return GetTerminationStatusImpl(handle, false /* can_block */, exit_code); | 80 return GetTerminationStatusImpl(handle, false /* can_block */, exit_code); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 return; | 215 return; |
272 | 216 |
273 BackgroundReaper* reaper = new BackgroundReaper(pid, 0); | 217 BackgroundReaper* reaper = new BackgroundReaper(pid, 0); |
274 PlatformThread::CreateNonJoinable(0, reaper); | 218 PlatformThread::CreateNonJoinable(0, reaper); |
275 } | 219 } |
276 | 220 |
277 #endif // !defined(OS_MACOSX) | 221 #endif // !defined(OS_MACOSX) |
278 #endif // !defined(OS_NACL_NONSFI) | 222 #endif // !defined(OS_NACL_NONSFI) |
279 | 223 |
280 } // namespace base | 224 } // namespace base |
OLD | NEW |