| 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> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 void ThreadMain() { | 39 void ThreadMain() { |
| 40 WaitForChildToDie(); | 40 WaitForChildToDie(); |
| 41 delete this; | 41 delete this; |
| 42 } | 42 } |
| 43 | 43 |
| 44 void WaitForChildToDie() { | 44 void WaitForChildToDie() { |
| 45 // Wait forever case. | 45 // Wait forever case. |
| 46 if (timeout_ == 0) { | 46 if (timeout_ == 0) { |
| 47 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0)); | 47 pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0)); |
| 48 if (r != child_) { | 48 if (r != child_) { |
| 49 LOG(ERROR) << "While waiting for " << child_ | 49 PLOG(ERROR) << "While waiting for " << child_ |
| 50 << " to terminate, we got the following result: " << r; | 50 << " to terminate, we got the following result: " << r; |
| 51 } | 51 } |
| 52 return; | 52 return; |
| 53 } | 53 } |
| 54 | 54 |
| 55 // There's no good way to wait for a specific child to exit in a timed | 55 // There's no good way to wait for a specific child to exit in a timed |
| 56 // fashion. (No kqueue on Linux), so we just loop and sleep. | 56 // fashion. (No kqueue on Linux), so we just loop and sleep. |
| 57 | 57 |
| 58 // Wait for 2 * timeout_ 500 milliseconds intervals. | 58 // Wait for 2 * timeout_ 500 milliseconds intervals. |
| 59 for (unsigned i = 0; i < 2 * timeout_; ++i) { | 59 for (unsigned i = 0; i < 2 * timeout_; ++i) { |
| 60 PlatformThread::Sleep(500); // 0.5 seconds | 60 PlatformThread::Sleep(500); // 0.5 seconds |
| (...skipping 15 matching lines...) Expand all Loading... |
| 76 const pid_t child_; | 76 const pid_t child_; |
| 77 // Number of seconds to wait, if 0 then wait forever and do not attempt to | 77 // Number of seconds to wait, if 0 then wait forever and do not attempt to |
| 78 // kill |child_|. | 78 // kill |child_|. |
| 79 const unsigned timeout_; | 79 const unsigned timeout_; |
| 80 | 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); | 81 DISALLOW_COPY_AND_ASSIGN(BackgroundReaper); |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 // static | 84 // static |
| 85 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { | 85 void ProcessWatcher::EnsureProcessTerminated(base::ProcessHandle process) { |
| 86 // If the child is already dead, then there's nothing to do | 86 // If the child is already dead, then there's nothing to do. |
| 87 if (IsChildDead(process)) | 87 if (IsChildDead(process)) |
| 88 return; | 88 return; |
| 89 | 89 |
| 90 const unsigned timeout = 2; // seconds | 90 const unsigned timeout = 2; // seconds |
| 91 BackgroundReaper* reaper = new BackgroundReaper(process, timeout); | 91 BackgroundReaper* reaper = new BackgroundReaper(process, timeout); |
| 92 PlatformThread::CreateNonJoinable(0, reaper); | 92 PlatformThread::CreateNonJoinable(0, reaper); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // static | 95 // static |
| 96 void ProcessWatcher::EnsureProcessGetsReaped(base::ProcessHandle process) { | 96 void ProcessWatcher::EnsureProcessGetsReaped(base::ProcessHandle process) { |
| 97 // If the child is already dead, then there's nothing to do | 97 // If the child is already dead, then there's nothing to do. |
| 98 if (IsChildDead(process)) | 98 if (IsChildDead(process)) |
| 99 return; | 99 return; |
| 100 | 100 |
| 101 BackgroundReaper* reaper = new BackgroundReaper(process, 0); | 101 BackgroundReaper* reaper = new BackgroundReaper(process, 0); |
| 102 PlatformThread::CreateNonJoinable(0, reaper); | 102 PlatformThread::CreateNonJoinable(0, reaper); |
| 103 } | 103 } |
| OLD | NEW |