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
3 // found in the LICENSE file.
5 #include "base/process/kill.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_file.h"
14 #include "base/logging.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/process/process_iterator.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/platform_thread.h"
24 TerminationStatus
GetTerminationStatusImpl(ProcessHandle handle
,
28 const pid_t result
= HANDLE_EINTR(waitpid(handle
, &status
,
29 can_block
? 0 : WNOHANG
));
31 DPLOG(ERROR
) << "waitpid(" << handle
<< ")";
34 return TERMINATION_STATUS_NORMAL_TERMINATION
;
35 } else if (result
== 0) {
36 // the child hasn't exited yet.
39 return TERMINATION_STATUS_STILL_RUNNING
;
45 if (WIFSIGNALED(status
)) {
46 switch (WTERMSIG(status
)) {
52 return TERMINATION_STATUS_PROCESS_CRASHED
;
56 return TERMINATION_STATUS_PROCESS_WAS_KILLED
;
62 if (WIFEXITED(status
) && WEXITSTATUS(status
) != 0)
63 return TERMINATION_STATUS_ABNORMAL_TERMINATION
;
65 return TERMINATION_STATUS_NORMAL_TERMINATION
;
70 #if !defined(OS_NACL_NONSFI)
71 bool KillProcessGroup(ProcessHandle process_group_id
) {
72 bool result
= kill(-1 * process_group_id
, SIGKILL
) == 0;
74 DPLOG(ERROR
) << "Unable to terminate process group " << process_group_id
;
77 #endif // !defined(OS_NACL_NONSFI)
79 TerminationStatus
GetTerminationStatus(ProcessHandle handle
, int* exit_code
) {
80 return GetTerminationStatusImpl(handle
, false /* can_block */, exit_code
);
83 TerminationStatus
GetKnownDeadTerminationStatus(ProcessHandle handle
,
85 bool result
= kill(handle
, SIGKILL
) == 0;
88 DPLOG(ERROR
) << "Unable to terminate process " << handle
;
90 return GetTerminationStatusImpl(handle
, true /* can_block */, exit_code
);
93 #if !defined(OS_NACL_NONSFI)
94 bool WaitForProcessesToExit(const FilePath::StringType
& executable_name
,
96 const ProcessFilter
* filter
) {
99 // TODO(port): This is inefficient, but works if there are multiple procs.
100 // TODO(port): use waitpid to avoid leaving zombies around
102 TimeTicks end_time
= TimeTicks::Now() + wait
;
104 NamedProcessIterator
iter(executable_name
, filter
);
105 if (!iter
.NextProcessEntry()) {
109 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
110 } while ((end_time
- TimeTicks::Now()) > TimeDelta());
115 bool CleanupProcesses(const FilePath::StringType
& executable_name
,
118 const ProcessFilter
* filter
) {
119 bool exited_cleanly
= WaitForProcessesToExit(executable_name
, wait
, filter
);
121 KillProcesses(executable_name
, exit_code
, filter
);
122 return exited_cleanly
;
125 #if !defined(OS_MACOSX)
129 // Return true if the given child is dead. This will also reap the process.
131 static bool IsChildDead(pid_t child
) {
132 const pid_t result
= HANDLE_EINTR(waitpid(child
, NULL
, WNOHANG
));
134 DPLOG(ERROR
) << "waitpid(" << child
<< ")";
136 } else if (result
> 0) {
137 // The child has died.
144 // A thread class which waits for the given child to exit and reaps it.
145 // If the child doesn't exit within a couple of seconds, kill it.
146 class BackgroundReaper
: public PlatformThread::Delegate
{
148 BackgroundReaper(pid_t child
, unsigned timeout
)
153 // Overridden from PlatformThread::Delegate:
154 void ThreadMain() override
{
159 void WaitForChildToDie() {
160 // Wait forever case.
162 pid_t r
= HANDLE_EINTR(waitpid(child_
, NULL
, 0));
164 DPLOG(ERROR
) << "While waiting for " << child_
165 << " to terminate, we got the following result: " << r
;
170 // There's no good way to wait for a specific child to exit in a timed
171 // fashion. (No kqueue on Linux), so we just loop and sleep.
173 // Wait for 2 * timeout_ 500 milliseconds intervals.
174 for (unsigned i
= 0; i
< 2 * timeout_
; ++i
) {
175 PlatformThread::Sleep(TimeDelta::FromMilliseconds(500));
176 if (IsChildDead(child_
))
180 if (kill(child_
, SIGKILL
) == 0) {
181 // SIGKILL is uncatchable. Since the signal was delivered, we can
182 // just wait for the process to die now in a blocking manner.
183 if (HANDLE_EINTR(waitpid(child_
, NULL
, 0)) < 0)
184 DPLOG(WARNING
) << "waitpid";
186 DLOG(ERROR
) << "While waiting for " << child_
<< " to terminate we"
187 << " failed to deliver a SIGKILL signal (" << errno
<< ").";
193 // Number of seconds to wait, if 0 then wait forever and do not attempt to
195 const unsigned timeout_
;
197 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper
);
202 void EnsureProcessTerminated(Process process
) {
203 // If the child is already dead, then there's nothing to do.
204 if (IsChildDead(process
.Pid()))
207 const unsigned timeout
= 2; // seconds
208 BackgroundReaper
* reaper
= new BackgroundReaper(process
.Pid(), timeout
);
209 PlatformThread::CreateNonJoinable(0, reaper
);
212 void EnsureProcessGetsReaped(ProcessId pid
) {
213 // If the child is already dead, then there's nothing to do.
214 if (IsChildDead(pid
))
217 BackgroundReaper
* reaper
= new BackgroundReaper(pid
, 0);
218 PlatformThread::CreateNonJoinable(0, reaper
);
221 #endif // !defined(OS_MACOSX)
222 #endif // !defined(OS_NACL_NONSFI)