OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/process_watcher.h" | 5 #include "content/common/process_watcher.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
11 | 11 |
12 #include "base/eintr_wrapper.h" | 12 #include "base/eintr_wrapper.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 | 15 |
16 // Return true if the given child is dead. This will also reap the process. | 16 // Return true if the given child is dead. This will also reap the process. |
17 // Doesn't block. | 17 // Doesn't block. |
18 static bool IsChildDead(pid_t child) { | 18 static bool IsChildDead(pid_t child) { |
19 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); | 19 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); |
20 if (result == -1) { | 20 if (result == -1) { |
21 PLOG(ERROR) << "waitpid(" << child << ")"; | 21 DPLOG(ERROR) << "waitpid(" << child << ")"; |
22 NOTREACHED(); | 22 NOTREACHED(); |
23 } else if (result > 0) { | 23 } else if (result > 0) { |
24 // The child has died. | 24 // The child has died. |
25 return true; | 25 return true; |
26 } | 26 } |
27 | 27 |
28 return false; | 28 return false; |
29 } | 29 } |
30 | 30 |
31 // A thread class which waits for the given child to exit and reaps it. | 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. | 32 // If the child doesn't exit within a couple of seconds, kill it. |
33 class BackgroundReaper : public base::PlatformThread::Delegate { | 33 class BackgroundReaper : public base::PlatformThread::Delegate { |
34 public: | 34 public: |
35 BackgroundReaper(pid_t child, unsigned timeout) | 35 BackgroundReaper(pid_t child, unsigned timeout) |
36 : child_(child), | 36 : child_(child), |
37 timeout_(timeout) { | 37 timeout_(timeout) { |
38 } | 38 } |
39 | 39 |
40 void ThreadMain() { | 40 void ThreadMain() { |
41 WaitForChildToDie(); | 41 WaitForChildToDie(); |
42 delete this; | 42 delete this; |
43 } | 43 } |
44 | 44 |
45 void WaitForChildToDie() { | 45 void WaitForChildToDie() { |
46 // Wait forever case. | 46 // Wait forever case. |
47 if (timeout_ == 0) { | 47 if (timeout_ == 0) { |
48 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0)); | 48 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0)); |
49 if (r != child_) { | 49 if (r != child_) { |
50 PLOG(ERROR) << "While waiting for " << child_ | 50 DPLOG(ERROR) << "While waiting for " << child_ |
51 << " to terminate, we got the following result: " << r; | 51 << " to terminate, we got the following result: " << r; |
52 } | 52 } |
53 return; | 53 return; |
54 } | 54 } |
55 | 55 |
56 // There's no good way to wait for a specific child to exit in a timed | 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. | 57 // fashion. (No kqueue on Linux), so we just loop and sleep. |
58 | 58 |
59 // Wait for 2 * timeout_ 500 milliseconds intervals. | 59 // Wait for 2 * timeout_ 500 milliseconds intervals. |
60 for (unsigned i = 0; i < 2 * timeout_; ++i) { | 60 for (unsigned i = 0; i < 2 * timeout_; ++i) { |
61 base::PlatformThread::Sleep(500); // 0.5 seconds | 61 base::PlatformThread::Sleep(500); // 0.5 seconds |
62 if (IsChildDead(child_)) | 62 if (IsChildDead(child_)) |
63 return; | 63 return; |
64 } | 64 } |
65 | 65 |
66 if (kill(child_, SIGKILL) == 0) { | 66 if (kill(child_, SIGKILL) == 0) { |
67 // SIGKILL is uncatchable. Since the signal was delivered, we can | 67 // SIGKILL is uncatchable. Since the signal was delivered, we can |
68 // just wait for the process to die now in a blocking manner. | 68 // just wait for the process to die now in a blocking manner. |
69 if (HANDLE_EINTR(waitpid(child_, NULL, 0)) < 0) | 69 if (HANDLE_EINTR(waitpid(child_, NULL, 0)) < 0) |
70 PLOG(WARNING) << "waitpid"; | 70 DPLOG(WARNING) << "waitpid"; |
71 } else { | 71 } else { |
72 LOG(ERROR) << "While waiting for " << child_ << " to terminate we" | 72 DLOG(ERROR) << "While waiting for " << child_ << " to terminate we" |
73 << " failed to deliver a SIGKILL signal (" << errno << ")."; | 73 << " failed to deliver a SIGKILL signal (" << errno << ")."; |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 private: | 77 private: |
78 const pid_t child_; | 78 const pid_t child_; |
79 // Number of seconds to wait, if 0 then wait forever and do not attempt to | 79 // Number of seconds to wait, if 0 then wait forever and do not attempt to |
80 // kill |child_|. | 80 // kill |child_|. |
81 const unsigned timeout_; | 81 const unsigned timeout_; |
82 | 82 |
83 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); | 83 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); |
(...skipping 12 matching lines...) Expand all Loading... |
96 | 96 |
97 // static | 97 // static |
98 void ProcessWatcher::EnsureProcessGetsReaped(base::ProcessHandle process) { | 98 void ProcessWatcher::EnsureProcessGetsReaped(base::ProcessHandle process) { |
99 // If the child is already dead, then there's nothing to do. | 99 // If the child is already dead, then there's nothing to do. |
100 if (IsChildDead(process)) | 100 if (IsChildDead(process)) |
101 return; | 101 return; |
102 | 102 |
103 BackgroundReaper* reaper = new BackgroundReaper(process, 0); | 103 BackgroundReaper* reaper = new BackgroundReaper(process, 0); |
104 base::PlatformThread::CreateNonJoinable(0, reaper); | 104 base::PlatformThread::CreateNonJoinable(0, reaper); |
105 } | 105 } |
OLD | NEW |