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 "chrome/common/process_watcher.h" | 5 #include "chrome/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_wrappers.h" |
12 #include "base/platform_thread.h" | 13 #include "base/platform_thread.h" |
13 | 14 |
14 // Return true if the given child is dead. This will also reap the process. | 15 // Return true if the given child is dead. This will also reap the process. |
15 // Doesn't block. | 16 // Doesn't block. |
16 static bool IsChildDead(pid_t child) { | 17 static bool IsChildDead(pid_t child) { |
17 const int result = waitpid(child, NULL, WNOHANG); | 18 const int result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); |
18 if (result == -1) { | 19 if (result == -1) { |
19 NOTREACHED(); | 20 NOTREACHED(); |
20 } else if (result > 0) { | 21 } else if (result > 0) { |
21 // The child has died. | 22 // The child has died. |
22 return true; | 23 return true; |
23 } | 24 } |
24 | 25 |
25 return false; | 26 return false; |
26 } | 27 } |
27 | 28 |
(...skipping 17 matching lines...) Expand all Loading... |
45 // Waits 0.5 * 4 = 2 seconds. | 46 // Waits 0.5 * 4 = 2 seconds. |
46 for (unsigned i = 0; i < 4; ++i) { | 47 for (unsigned i = 0; i < 4; ++i) { |
47 PlatformThread::Sleep(500); // 0.5 seconds | 48 PlatformThread::Sleep(500); // 0.5 seconds |
48 if (IsChildDead(child_)) | 49 if (IsChildDead(child_)) |
49 return; | 50 return; |
50 } | 51 } |
51 | 52 |
52 if (kill(child_, SIGKILL) == 0) { | 53 if (kill(child_, SIGKILL) == 0) { |
53 // SIGKILL is uncatchable. Since the signal was delivered, we can | 54 // SIGKILL is uncatchable. Since the signal was delivered, we can |
54 // just wait for the process to die now in a blocking manner. | 55 // just wait for the process to die now in a blocking manner. |
55 int result; | 56 HANDLE_EINTR(waitpid(child_, NULL, 0)); |
56 do { | |
57 result = waitpid(child_, NULL, 0); | |
58 } while (result == -1 && errno == EINTR); | |
59 } else { | 57 } else { |
60 LOG(ERROR) << "While waiting for " << child_ << " to terminate we" | 58 LOG(ERROR) << "While waiting for " << child_ << " to terminate we" |
61 << " failed to deliver a SIGKILL signal (" << errno << ")."; | 59 << " failed to deliver a SIGKILL signal (" << errno << ")."; |
62 } | 60 } |
63 } | 61 } |
64 | 62 |
65 private: | 63 private: |
66 const pid_t child_; | 64 const pid_t child_; |
67 | 65 |
68 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); | 66 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); |
69 }; | 67 }; |
70 | 68 |
71 // static | 69 // static |
72 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { | 70 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { |
73 // If the child is already dead, then there's nothing to do | 71 // If the child is already dead, then there's nothing to do |
74 if (IsChildDead(process)) | 72 if (IsChildDead(process)) |
75 return; | 73 return; |
76 | 74 |
77 BackgroundReaper* reaper = new BackgroundReaper(process); | 75 BackgroundReaper* reaper = new BackgroundReaper(process); |
78 PlatformThread::CreateNonJoinable(0, reaper); | 76 PlatformThread::CreateNonJoinable(0, reaper); |
79 } | 77 } |
OLD | NEW |