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> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 DCHECK(timeout > 0); | 69 DCHECK(timeout > 0); |
70 | 70 |
71 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that | 71 // 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 | 72 // |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 | 73 // 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 | 74 // to kill and reap the process. Not observing this rule will resurrect |
75 // zombies. | 75 // zombies. |
76 | 76 |
77 int result; | 77 int result; |
78 | 78 |
79 int kq = HANDLE_EINTR(kqueue()); | 79 ScopedFD kq(HANDLE_EINTR(kqueue())); |
80 if (kq == -1) { | 80 if (!kq) { |
81 DPLOG(ERROR) << "kqueue()"; | 81 DPLOG(ERROR) << "kqueue()"; |
82 } else { | 82 } else { |
83 file_util::ScopedFD auto_close_kq(&kq); | |
84 | |
85 struct kevent change = {0}; | 83 struct kevent change = {0}; |
86 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); | 84 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); |
87 result = HANDLE_EINTR(kevent(kq, &change, 1, NULL, 0, NULL)); | 85 result = HANDLE_EINTR(kevent(kq.get(), &change, 1, NULL, 0, NULL)); |
88 | 86 |
89 if (result == -1) { | 87 if (result == -1) { |
90 if (errno != ESRCH) { | 88 if (errno != ESRCH) { |
91 DPLOG(ERROR) << "kevent (setup " << child << ")"; | 89 DPLOG(ERROR) << "kevent (setup " << child << ")"; |
92 } else { | 90 } else { |
93 // At this point, one of the following has occurred: | 91 // At this point, one of the following has occurred: |
94 // 1. The process has died but has not yet been reaped. | 92 // 1. The process has died but has not yet been reaped. |
95 // 2. The process has died and has already been reaped. | 93 // 2. The process has died and has already been reaped. |
96 // 3. The process is in the process of dying. It's no longer | 94 // 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 | 95 // kqueueable, but it may not be waitable yet either. Mark calls |
(...skipping 15 matching lines...) Expand all Loading... |
113 } | 111 } |
114 } else { | 112 } else { |
115 // Keep track of the elapsed time to be able to restart kevent if it's | 113 // Keep track of the elapsed time to be able to restart kevent if it's |
116 // interrupted. | 114 // interrupted. |
117 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); | 115 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); |
118 TimeTicks deadline = TimeTicks::Now() + remaining_delta; | 116 TimeTicks deadline = TimeTicks::Now() + remaining_delta; |
119 result = -1; | 117 result = -1; |
120 struct kevent event = {0}; | 118 struct kevent event = {0}; |
121 while (remaining_delta.InMilliseconds() > 0) { | 119 while (remaining_delta.InMilliseconds() > 0) { |
122 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); | 120 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); |
123 result = kevent(kq, NULL, 0, &event, 1, &remaining_timespec); | 121 result = kevent(kq.get(), NULL, 0, &event, 1, &remaining_timespec); |
124 if (result == -1 && errno == EINTR) { | 122 if (result == -1 && errno == EINTR) { |
125 remaining_delta = deadline - TimeTicks::Now(); | 123 remaining_delta = deadline - TimeTicks::Now(); |
126 result = 0; | 124 result = 0; |
127 } else { | 125 } else { |
128 break; | 126 break; |
129 } | 127 } |
130 } | 128 } |
131 | 129 |
132 if (result == -1) { | 130 if (result == -1) { |
133 DPLOG(ERROR) << "kevent (wait " << child << ")"; | 131 DPLOG(ERROR) << "kevent (wait " << child << ")"; |
(...skipping 30 matching lines...) Expand all Loading... |
164 } | 162 } |
165 } | 163 } |
166 | 164 |
167 } // namespace | 165 } // namespace |
168 | 166 |
169 void EnsureProcessTerminated(ProcessHandle process) { | 167 void EnsureProcessTerminated(ProcessHandle process) { |
170 WaitForChildToDie(process, kWaitBeforeKillSeconds); | 168 WaitForChildToDie(process, kWaitBeforeKillSeconds); |
171 } | 169 } |
172 | 170 |
173 } // namespace base | 171 } // namespace base |
OLD | NEW |