| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/process/kill.h" | 5 #include "base/process/kill.h" |
| 6 | 6 |
| 7 #include <signal.h> | 7 #include <signal.h> |
| 8 #include <sys/event.h> | 8 #include <sys/event.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/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/scoped_file.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const int kWaitBeforeKillSeconds = 2; | 21 const int kWaitBeforeKillSeconds = 2; |
| 21 | 22 |
| 22 // Reap |child| process. This call blocks until completion. | 23 // Reap |child| process. This call blocks until completion. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 DCHECK(timeout > 0); | 70 DCHECK(timeout > 0); |
| 70 | 71 |
| 71 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that | 72 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that |
| 72 // |child| has been reaped. Specifically, even if a kqueue, kevent, or other | 73 // |child| has been reaped. Specifically, even if a kqueue, kevent, or other |
| 73 // call fails, this function should fall back to the last resort of trying | 74 // call fails, this function should fall back to the last resort of trying |
| 74 // to kill and reap the process. Not observing this rule will resurrect | 75 // to kill and reap the process. Not observing this rule will resurrect |
| 75 // zombies. | 76 // zombies. |
| 76 | 77 |
| 77 int result; | 78 int result; |
| 78 | 79 |
| 79 int kq = HANDLE_EINTR(kqueue()); | 80 ScopedFD kq(HANDLE_EINTR(kqueue())); |
| 80 if (kq == -1) { | 81 if (!kq.is_valid()) { |
| 81 DPLOG(ERROR) << "kqueue()"; | 82 DPLOG(ERROR) << "kqueue()"; |
| 82 } else { | 83 } else { |
| 83 file_util::ScopedFD auto_close_kq(&kq); | |
| 84 | |
| 85 struct kevent change = {0}; | 84 struct kevent change = {0}; |
| 86 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); | 85 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); |
| 87 result = HANDLE_EINTR(kevent(kq, &change, 1, NULL, 0, NULL)); | 86 result = HANDLE_EINTR(kevent(kq.get(), &change, 1, NULL, 0, NULL)); |
| 88 | 87 |
| 89 if (result == -1) { | 88 if (result == -1) { |
| 90 if (errno != ESRCH) { | 89 if (errno != ESRCH) { |
| 91 DPLOG(ERROR) << "kevent (setup " << child << ")"; | 90 DPLOG(ERROR) << "kevent (setup " << child << ")"; |
| 92 } else { | 91 } else { |
| 93 // At this point, one of the following has occurred: | 92 // At this point, one of the following has occurred: |
| 94 // 1. The process has died but has not yet been reaped. | 93 // 1. The process has died but has not yet been reaped. |
| 95 // 2. The process has died and has already been reaped. | 94 // 2. The process has died and has already been reaped. |
| 96 // 3. The process is in the process of dying. It's no longer | 95 // 3. The process is in the process of dying. It's no longer |
| 97 // kqueueable, but it may not be waitable yet either. Mark calls | 96 // kqueueable, but it may not be waitable yet either. Mark calls |
| (...skipping 15 matching lines...) Expand all Loading... |
| 113 } | 112 } |
| 114 } else { | 113 } else { |
| 115 // Keep track of the elapsed time to be able to restart kevent if it's | 114 // Keep track of the elapsed time to be able to restart kevent if it's |
| 116 // interrupted. | 115 // interrupted. |
| 117 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); | 116 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); |
| 118 TimeTicks deadline = TimeTicks::Now() + remaining_delta; | 117 TimeTicks deadline = TimeTicks::Now() + remaining_delta; |
| 119 result = -1; | 118 result = -1; |
| 120 struct kevent event = {0}; | 119 struct kevent event = {0}; |
| 121 while (remaining_delta.InMilliseconds() > 0) { | 120 while (remaining_delta.InMilliseconds() > 0) { |
| 122 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); | 121 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); |
| 123 result = kevent(kq, NULL, 0, &event, 1, &remaining_timespec); | 122 result = kevent(kq.get(), NULL, 0, &event, 1, &remaining_timespec); |
| 124 if (result == -1 && errno == EINTR) { | 123 if (result == -1 && errno == EINTR) { |
| 125 remaining_delta = deadline - TimeTicks::Now(); | 124 remaining_delta = deadline - TimeTicks::Now(); |
| 126 result = 0; | 125 result = 0; |
| 127 } else { | 126 } else { |
| 128 break; | 127 break; |
| 129 } | 128 } |
| 130 } | 129 } |
| 131 | 130 |
| 132 if (result == -1) { | 131 if (result == -1) { |
| 133 DPLOG(ERROR) << "kevent (wait " << child << ")"; | 132 DPLOG(ERROR) << "kevent (wait " << child << ")"; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 164 } | 163 } |
| 165 } | 164 } |
| 166 | 165 |
| 167 } // namespace | 166 } // namespace |
| 168 | 167 |
| 169 void EnsureProcessTerminated(ProcessHandle process) { | 168 void EnsureProcessTerminated(ProcessHandle process) { |
| 170 WaitForChildToDie(process, kWaitBeforeKillSeconds); | 169 WaitForChildToDie(process, kWaitBeforeKillSeconds); |
| 171 } | 170 } |
| 172 | 171 |
| 173 } // namespace base | 172 } // namespace base |
| OLD | NEW |