Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(668)

Side by Side Diff: content/common/process_watcher_posix.cc

Issue 8674003: Move the ProcessWatcher methods out of content/common/process_watcher into base/process_util, alo... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 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.
4
5 #include "content/common/process_watcher.h"
6
7 #include <errno.h>
8 #include <signal.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 #include "base/eintr_wrapper.h"
13 #include "base/logging.h"
14 #include "base/threading/platform_thread.h"
15
16 // Return true if the given child is dead. This will also reap the process.
17 // Doesn't block.
18 static bool IsChildDead(pid_t child) {
19 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
20 if (result == -1) {
21 DPLOG(ERROR) << "waitpid(" << child << ")";
22 NOTREACHED();
23 } else if (result > 0) {
24 // The child has died.
25 return true;
26 }
27
28 return false;
29 }
30
31 // A thread class which waits for the given child to exit and reaps it.
32 // If the child doesn't exit within a couple of seconds, kill it.
33 class BackgroundReaper : public base::PlatformThread::Delegate {
34 public:
35 BackgroundReaper(pid_t child, unsigned timeout)
36 : child_(child),
37 timeout_(timeout) {
38 }
39
40 void ThreadMain() {
41 WaitForChildToDie();
42 delete this;
43 }
44
45 void WaitForChildToDie() {
46 // Wait forever case.
47 if (timeout_ == 0) {
48 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0));
49 if (r != child_) {
50 DPLOG(ERROR) << "While waiting for " << child_
51 << " to terminate, we got the following result: " << r;
52 }
53 return;
54 }
55
56 // There's no good way to wait for a specific child to exit in a timed
57 // fashion. (No kqueue on Linux), so we just loop and sleep.
58
59 // Wait for 2 * timeout_ 500 milliseconds intervals.
60 for (unsigned i = 0; i < 2 * timeout_; ++i) {
61 base::PlatformThread::Sleep(500); // 0.5 seconds
62 if (IsChildDead(child_))
63 return;
64 }
65
66 if (kill(child_, SIGKILL) == 0) {
67 // SIGKILL is uncatchable. Since the signal was delivered, we can
68 // just wait for the process to die now in a blocking manner.
69 if (HANDLE_EINTR(waitpid(child_, NULL, 0)) < 0)
70 DPLOG(WARNING) << "waitpid";
71 } else {
72 DLOG(ERROR) << "While waiting for " << child_ << " to terminate we"
73 << " failed to deliver a SIGKILL signal (" << errno << ").";
74 }
75 }
76
77 private:
78 const pid_t child_;
79 // Number of seconds to wait, if 0 then wait forever and do not attempt to
80 // kill |child_|.
81 const unsigned timeout_;
82
83 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper);
84 };
85
86 // static
87 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) {
88 // If the child is already dead, then there's nothing to do.
89 if (IsChildDead(process))
90 return;
91
92 const unsigned timeout = 2; // seconds
93 BackgroundReaper* reaper = new BackgroundReaper(process, timeout);
94 base::PlatformThread::CreateNonJoinable(0, reaper);
95 }
96
97 // static
98 void ProcessWatcher::EnsureProcessGetsReaped(base::ProcessHandle process) {
99 // If the child is already dead, then there's nothing to do.
100 if (IsChildDead(process))
101 return;
102
103 BackgroundReaper* reaper = new BackgroundReaper(process, 0);
104 base::PlatformThread::CreateNonJoinable(0, reaper);
105 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698