This source file includes following definitions.
- WaitpidWithTimeout
- GetTerminationStatusImpl
- KillProcess
- KillProcessGroup
- GetTerminationStatus
- GetKnownDeadTerminationStatus
- WaitForExitCode
- WaitForExitCodeWithTimeout
- WaitForProcessesToExit
- WaitForSingleNonChildProcess
- WaitForSingleProcess
- CleanupProcesses
- IsChildDead
- timeout_
- ThreadMain
- WaitForChildToDie
- EnsureProcessTerminated
- EnsureProcessGetsReaped
#include "base/process/kill.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "base/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/process_iterator.h"
#include "base/synchronization/waitable_event.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/platform_thread.h"
namespace base {
namespace {
int WaitpidWithTimeout(ProcessHandle handle,
int64 wait_milliseconds,
bool* success) {
int status = -1;
pid_t ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG));
static const int64 kMaxSleepInMicroseconds = 1 << 18;
int64 max_sleep_time_usecs = 1 << 10;
int64 double_sleep_time = 0;
TimeTicks wakeup_time = TimeTicks::Now() +
TimeDelta::FromMilliseconds(wait_milliseconds);
while (ret_pid == 0) {
TimeTicks now = TimeTicks::Now();
if (now > wakeup_time)
break;
int64 sleep_time_usecs = (wakeup_time - now).InMicroseconds();
if (sleep_time_usecs > max_sleep_time_usecs)
sleep_time_usecs = max_sleep_time_usecs;
usleep(sleep_time_usecs);
ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG));
if ((max_sleep_time_usecs < kMaxSleepInMicroseconds) &&
(double_sleep_time++ % 4 == 0)) {
max_sleep_time_usecs *= 2;
}
}
if (success)
*success = (ret_pid != -1);
return status;
}
TerminationStatus GetTerminationStatusImpl(ProcessHandle handle,
bool can_block,
int* exit_code) {
int status = 0;
const pid_t result = HANDLE_EINTR(waitpid(handle, &status,
can_block ? 0 : WNOHANG));
if (result == -1) {
DPLOG(ERROR) << "waitpid(" << handle << ")";
if (exit_code)
*exit_code = 0;
return TERMINATION_STATUS_NORMAL_TERMINATION;
} else if (result == 0) {
if (exit_code)
*exit_code = 0;
return TERMINATION_STATUS_STILL_RUNNING;
}
if (exit_code)
*exit_code = status;
if (WIFSIGNALED(status)) {
switch (WTERMSIG(status)) {
case SIGABRT:
case SIGBUS:
case SIGFPE:
case SIGILL:
case SIGSEGV:
return TERMINATION_STATUS_PROCESS_CRASHED;
case SIGINT:
case SIGKILL:
case SIGTERM:
return TERMINATION_STATUS_PROCESS_WAS_KILLED;
default:
break;
}
}
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
return TERMINATION_STATUS_ABNORMAL_TERMINATION;
return TERMINATION_STATUS_NORMAL_TERMINATION;
}
}
bool KillProcess(ProcessHandle process_id, int exit_code, bool wait) {
DCHECK_GT(process_id, 1) << " tried to kill invalid process_id";
if (process_id <= 1)
return false;
bool result = kill(process_id, SIGTERM) == 0;
if (result && wait) {
int tries = 60;
if (RunningOnValgrind()) {
tries *= 2;
}
unsigned sleep_ms = 4;
bool exited = false;
while (tries-- > 0) {
pid_t pid = HANDLE_EINTR(waitpid(process_id, NULL, WNOHANG));
if (pid == process_id) {
exited = true;
break;
}
if (pid == -1) {
if (errno == ECHILD) {
exited = true;
break;
}
DPLOG(ERROR) << "Error waiting for process " << process_id;
}
usleep(sleep_ms * 1000);
const unsigned kMaxSleepMs = 1000;
if (sleep_ms < kMaxSleepMs)
sleep_ms *= 2;
}
if (!exited)
result = kill(process_id, SIGKILL) == 0;
}
if (!result)
DPLOG(ERROR) << "Unable to terminate process " << process_id;
return result;
}
bool KillProcessGroup(ProcessHandle process_group_id) {
bool result = kill(-1 * process_group_id, SIGKILL) == 0;
if (!result)
DPLOG(ERROR) << "Unable to terminate process group " << process_group_id;
return result;
}
TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
return GetTerminationStatusImpl(handle, false , exit_code);
}
TerminationStatus GetKnownDeadTerminationStatus(ProcessHandle handle,
int* exit_code) {
bool result = kill(handle, SIGKILL) == 0;
if (!result)
DPLOG(ERROR) << "Unable to terminate process " << handle;
return GetTerminationStatusImpl(handle, true , exit_code);
}
bool WaitForExitCode(ProcessHandle handle, int* exit_code) {
int status;
if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) {
NOTREACHED();
return false;
}
if (WIFEXITED(status)) {
*exit_code = WEXITSTATUS(status);
return true;
}
DCHECK(WIFSIGNALED(status));
return false;
}
bool WaitForExitCodeWithTimeout(ProcessHandle handle,
int* exit_code,
base::TimeDelta timeout) {
bool waitpid_success = false;
int status = WaitpidWithTimeout(handle, timeout.InMilliseconds(),
&waitpid_success);
if (status == -1)
return false;
if (!waitpid_success)
return false;
if (WIFSIGNALED(status)) {
*exit_code = -1;
return true;
}
if (WIFEXITED(status)) {
*exit_code = WEXITSTATUS(status);
return true;
}
return false;
}
bool WaitForProcessesToExit(const FilePath::StringType& executable_name,
base::TimeDelta wait,
const ProcessFilter* filter) {
bool result = false;
base::TimeTicks end_time = base::TimeTicks::Now() + wait;
do {
NamedProcessIterator iter(executable_name, filter);
if (!iter.NextProcessEntry()) {
result = true;
break;
}
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
} while ((end_time - base::TimeTicks::Now()) > base::TimeDelta());
return result;
}
#if defined(OS_MACOSX)
static bool WaitForSingleNonChildProcess(ProcessHandle handle,
base::TimeDelta wait) {
DCHECK_GT(handle, 0);
DCHECK(wait.InMilliseconds() == base::kNoTimeout || wait > base::TimeDelta());
ScopedFD kq(kqueue());
if (!kq.is_valid()) {
DPLOG(ERROR) << "kqueue";
return false;
}
struct kevent change = {0};
EV_SET(&change, handle, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
int result = HANDLE_EINTR(kevent(kq.get(), &change, 1, NULL, 0, NULL));
if (result == -1) {
if (errno == ESRCH) {
return true;
}
DPLOG(ERROR) << "kevent (setup " << handle << ")";
return false;
}
bool wait_forever = wait.InMilliseconds() == base::kNoTimeout;
base::TimeDelta remaining_delta;
base::TimeTicks deadline;
if (!wait_forever) {
remaining_delta = wait;
deadline = base::TimeTicks::Now() + remaining_delta;
}
result = -1;
struct kevent event = {0};
while (wait_forever || remaining_delta > base::TimeDelta()) {
struct timespec remaining_timespec;
struct timespec* remaining_timespec_ptr;
if (wait_forever) {
remaining_timespec_ptr = NULL;
} else {
remaining_timespec = remaining_delta.ToTimeSpec();
remaining_timespec_ptr = &remaining_timespec;
}
result = kevent(kq.get(), NULL, 0, &event, 1, remaining_timespec_ptr);
if (result == -1 && errno == EINTR) {
if (!wait_forever) {
remaining_delta = deadline - base::TimeTicks::Now();
}
result = 0;
} else {
break;
}
}
if (result < 0) {
DPLOG(ERROR) << "kevent (wait " << handle << ")";
return false;
} else if (result > 1) {
DLOG(ERROR) << "kevent (wait " << handle << "): unexpected result "
<< result;
return false;
} else if (result == 0) {
return false;
}
DCHECK_EQ(result, 1);
if (event.filter != EVFILT_PROC ||
(event.fflags & NOTE_EXIT) == 0 ||
event.ident != static_cast<uintptr_t>(handle)) {
DLOG(ERROR) << "kevent (wait " << handle
<< "): unexpected event: filter=" << event.filter
<< ", fflags=" << event.fflags
<< ", ident=" << event.ident;
return false;
}
return true;
}
#endif
bool WaitForSingleProcess(ProcessHandle handle, base::TimeDelta wait) {
ProcessHandle parent_pid = GetParentProcessId(handle);
ProcessHandle our_pid = Process::Current().handle();
if (parent_pid != our_pid) {
#if defined(OS_MACOSX)
return WaitForSingleNonChildProcess(handle, wait);
#else
NOTIMPLEMENTED();
#endif
}
bool waitpid_success;
int status = -1;
if (wait.InMilliseconds() == base::kNoTimeout) {
waitpid_success = (HANDLE_EINTR(waitpid(handle, &status, 0)) != -1);
} else {
status = WaitpidWithTimeout(
handle, wait.InMilliseconds(), &waitpid_success);
}
if (status != -1) {
DCHECK(waitpid_success);
return WIFEXITED(status);
} else {
return false;
}
}
bool CleanupProcesses(const FilePath::StringType& executable_name,
base::TimeDelta wait,
int exit_code,
const ProcessFilter* filter) {
bool exited_cleanly = WaitForProcessesToExit(executable_name, wait, filter);
if (!exited_cleanly)
KillProcesses(executable_name, exit_code, filter);
return exited_cleanly;
}
#if !defined(OS_MACOSX)
namespace {
static bool IsChildDead(pid_t child) {
const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
if (result == -1) {
DPLOG(ERROR) << "waitpid(" << child << ")";
NOTREACHED();
} else if (result > 0) {
return true;
}
return false;
}
class BackgroundReaper : public PlatformThread::Delegate {
public:
BackgroundReaper(pid_t child, unsigned timeout)
: child_(child),
timeout_(timeout) {
}
virtual void ThreadMain() OVERRIDE {
WaitForChildToDie();
delete this;
}
void WaitForChildToDie() {
if (timeout_ == 0) {
pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0));
if (r != child_) {
DPLOG(ERROR) << "While waiting for " << child_
<< " to terminate, we got the following result: " << r;
}
return;
}
for (unsigned i = 0; i < 2 * timeout_; ++i) {
PlatformThread::Sleep(TimeDelta::FromMilliseconds(500));
if (IsChildDead(child_))
return;
}
if (kill(child_, SIGKILL) == 0) {
if (HANDLE_EINTR(waitpid(child_, NULL, 0)) < 0)
DPLOG(WARNING) << "waitpid";
} else {
DLOG(ERROR) << "While waiting for " << child_ << " to terminate we"
<< " failed to deliver a SIGKILL signal (" << errno << ").";
}
}
private:
const pid_t child_;
const unsigned timeout_;
DISALLOW_COPY_AND_ASSIGN(BackgroundReaper);
};
}
void EnsureProcessTerminated(ProcessHandle process) {
if (IsChildDead(process))
return;
const unsigned timeout = 2;
BackgroundReaper* reaper = new BackgroundReaper(process, timeout);
PlatformThread::CreateNonJoinable(0, reaper);
}
void EnsureProcessGetsReaped(ProcessHandle process) {
if (IsChildDead(process))
return;
BackgroundReaper* reaper = new BackgroundReaper(process, 0);
PlatformThread::CreateNonJoinable(0, reaper);
}
#endif
}