Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / base / process / kill_posix.cc
blob0e303c67d3cffb431bee2899c88ebbbdea9328e5
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"
7 #include <signal.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <unistd.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"
20 namespace base {
22 namespace {
24 TerminationStatus GetTerminationStatusImpl(ProcessHandle handle,
25 bool can_block,
26 int* exit_code) {
27 int status = 0;
28 const pid_t result = HANDLE_EINTR(waitpid(handle, &status,
29 can_block ? 0 : WNOHANG));
30 if (result == -1) {
31 DPLOG(ERROR) << "waitpid(" << handle << ")";
32 if (exit_code)
33 *exit_code = 0;
34 return TERMINATION_STATUS_NORMAL_TERMINATION;
35 } else if (result == 0) {
36 // the child hasn't exited yet.
37 if (exit_code)
38 *exit_code = 0;
39 return TERMINATION_STATUS_STILL_RUNNING;
42 if (exit_code)
43 *exit_code = status;
45 if (WIFSIGNALED(status)) {
46 switch (WTERMSIG(status)) {
47 case SIGABRT:
48 case SIGBUS:
49 case SIGFPE:
50 case SIGILL:
51 case SIGSEGV:
52 return TERMINATION_STATUS_PROCESS_CRASHED;
53 case SIGINT:
54 case SIGKILL:
55 case SIGTERM:
56 return TERMINATION_STATUS_PROCESS_WAS_KILLED;
57 default:
58 break;
62 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
63 return TERMINATION_STATUS_ABNORMAL_TERMINATION;
65 return TERMINATION_STATUS_NORMAL_TERMINATION;
68 } // namespace
70 #if !defined(OS_NACL_NONSFI)
71 bool KillProcessGroup(ProcessHandle process_group_id) {
72 bool result = kill(-1 * process_group_id, SIGKILL) == 0;
73 if (!result)
74 DPLOG(ERROR) << "Unable to terminate process group " << process_group_id;
75 return result;
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,
84 int* exit_code) {
85 bool result = kill(handle, SIGKILL) == 0;
87 if (!result)
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,
95 TimeDelta wait,
96 const ProcessFilter* filter) {
97 bool result = false;
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;
103 do {
104 NamedProcessIterator iter(executable_name, filter);
105 if (!iter.NextProcessEntry()) {
106 result = true;
107 break;
109 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
110 } while ((end_time - TimeTicks::Now()) > TimeDelta());
112 return result;
115 bool CleanupProcesses(const FilePath::StringType& executable_name,
116 TimeDelta wait,
117 int exit_code,
118 const ProcessFilter* filter) {
119 bool exited_cleanly = WaitForProcessesToExit(executable_name, wait, filter);
120 if (!exited_cleanly)
121 KillProcesses(executable_name, exit_code, filter);
122 return exited_cleanly;
125 #if !defined(OS_MACOSX)
127 namespace {
129 // Return true if the given child is dead. This will also reap the process.
130 // Doesn't block.
131 static bool IsChildDead(pid_t child) {
132 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
133 if (result == -1) {
134 DPLOG(ERROR) << "waitpid(" << child << ")";
135 NOTREACHED();
136 } else if (result > 0) {
137 // The child has died.
138 return true;
141 return false;
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 {
147 public:
148 BackgroundReaper(pid_t child, unsigned timeout)
149 : child_(child),
150 timeout_(timeout) {
153 // Overridden from PlatformThread::Delegate:
154 void ThreadMain() override {
155 WaitForChildToDie();
156 delete this;
159 void WaitForChildToDie() {
160 // Wait forever case.
161 if (timeout_ == 0) {
162 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0));
163 if (r != child_) {
164 DPLOG(ERROR) << "While waiting for " << child_
165 << " to terminate, we got the following result: " << r;
167 return;
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_))
177 return;
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";
185 } else {
186 DLOG(ERROR) << "While waiting for " << child_ << " to terminate we"
187 << " failed to deliver a SIGKILL signal (" << errno << ").";
191 private:
192 const pid_t child_;
193 // Number of seconds to wait, if 0 then wait forever and do not attempt to
194 // kill |child_|.
195 const unsigned timeout_;
197 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper);
200 } // namespace
202 void EnsureProcessTerminated(Process process) {
203 // If the child is already dead, then there's nothing to do.
204 if (IsChildDead(process.Pid()))
205 return;
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))
215 return;
217 BackgroundReaper* reaper = new BackgroundReaper(pid, 0);
218 PlatformThread::CreateNonJoinable(0, reaper);
221 #endif // !defined(OS_MACOSX)
222 #endif // !defined(OS_NACL_NONSFI)
224 } // namespace base