Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: runtime/bin/process_macos.cc

Issue 24024007: Simplify process exit-code handling on Posix. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_MACOS) 6 #if defined(TARGET_OS_MACOS)
7 7
8 #include "bin/process.h" 8 #include "bin/process.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 // Mutex protecting all accesses to the linked list of active 102 // Mutex protecting all accesses to the linked list of active
103 // processes. 103 // processes.
104 static dart::Mutex* mutex_; 104 static dart::Mutex* mutex_;
105 }; 105 };
106 106
107 107
108 ProcessInfo* ProcessInfoList::active_processes_ = NULL; 108 ProcessInfo* ProcessInfoList::active_processes_ = NULL;
109 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex(); 109 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex();
110 110
111 111
112 // The exit code handler sets up a separate thread which is signalled 112 // The exit code handler sets up a separate thread which waits for child
113 // on SIGCHLD. That separate thread can then get the exit code from 113 // processes to die. That separate thread can then get the exit code from
114 // processes that have exited and communicate it to Dart through the 114 // processes that have exited and communicate it to Dart through the
115 // event loop. 115 // event loop.
116 class ExitCodeHandler { 116 class ExitCodeHandler {
117 public: 117 public:
118 // Ensure that the ExitCodeHandler has been initialized. 118 // Ensure that the ExitCodeHandler has been initialized.
119 static bool EnsureInitialized() { 119 static bool EnsureInitialized() {
120 // Multiple isolates could be starting processes at the same 120 // Multiple isolates could be starting processes at the same
121 // time. Make sure that only one of them initializes the 121 // time. Make sure that only one of them initializes the
122 // ExitCodeHandler. 122 // ExitCodeHandler.
123 MutexLocker locker(mutex_); 123 MutexLocker locker(mutex_);
124 if (initialized_) { 124 if (initialized_) {
125 return true; 125 return true;
126 } 126 }
127 127
128 // Allocate a pipe that the signal handler can write a byte to and 128 // Start thread that handles process exits when waitpid returns.
129 // that the exit handler thread can poll. 129 int result = dart::Thread::Start(ExitCodeHandlerEntry, 0);
130 int result = TEMP_FAILURE_RETRY(pipe(sig_chld_fds_));
131 if (result < 0) {
132 return false;
133 }
134 FDUtils::SetCloseOnExec(sig_chld_fds_[0]);
135 FDUtils::SetCloseOnExec(sig_chld_fds_[1]);
136
137 // Start thread that polls the pipe and handles process exits when
138 // data is received on the pipe.
139 result = dart::Thread::Start(ExitCodeHandlerEntry, sig_chld_fds_[0]);
140 if (result != 0) { 130 if (result != 0) {
141 FATAL1("Failed to start exit code handler worker thread %d", result); 131 FATAL1("Failed to start exit code handler worker thread %d", result);
142 } 132 }
143 133
144 // Mark write end non-blocking.
145 FDUtils::SetNonBlocking(sig_chld_fds_[1]);
146
147 // Thread started and the ExitCodeHandler is initialized. 134 // Thread started and the ExitCodeHandler is initialized.
148 initialized_ = true; 135 initialized_ = true;
149 return true; 136 return true;
150 } 137 }
151 138
152 // Get the write end of the pipe.
153 static int WakeUpFd() {
154 return sig_chld_fds_[1];
155 }
156
157 static void TerminateExitCodeThread() { 139 static void TerminateExitCodeThread() {
158 MutexLocker locker(mutex_); 140 MutexLocker locker(mutex_);
159 if (!initialized_) { 141 if (!initialized_) {
160 return; 142 return;
161 } 143 }
162 144
163 uint8_t data = kThreadTerminateByte; 145 thread_terminate_monitor_->Enter();
164 ssize_t result = 146
165 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1)); 147 terminate_ = true;
166 if (result < 1) { 148 // Fork to wake up waitpid.
167 perror("Failed to write to wake-up fd to terminate exit code thread"); 149 if (TEMP_FAILURE_RETRY(fork()) == 0) {
150 exit(0);
168 } 151 }
169 152
170 { 153 thread_terminate_monitor_->Wait(dart::Monitor::kNoTimeout);
171 MonitorLocker terminate_locker(thread_terminate_monitor_); 154 thread_terminate_monitor_->Exit();
172 while (!thread_terminated_) {
173 terminate_locker.Wait();
174 }
175 }
176 }
177
178 static void ExitCodeThreadTerminated() {
179 MonitorLocker locker(thread_terminate_monitor_);
180 thread_terminated_ = true;
181 locker.Notify();
182 } 155 }
183 156
184 private: 157 private:
185 static const uint8_t kThreadTerminateByte = 1;
186
187 // GetProcessExitCodes is called on a separate thread when a SIGCHLD
188 // signal is received to retrieve the exit codes and post them to
189 // dart.
190 static void GetProcessExitCodes() {
191 pid_t pid = 0;
192 int status = 0;
193 while ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG))) > 0) {
194 int exit_code = 0;
195 int negative = 0;
196 if (WIFEXITED(status)) {
197 exit_code = WEXITSTATUS(status);
198 }
199 if (WIFSIGNALED(status)) {
200 exit_code = WTERMSIG(status);
201 negative = 1;
202 }
203 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
204 if (exit_code_fd != 0) {
205 int message[2] = { exit_code, negative };
206 ssize_t result =
207 FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
208 // If the process has been closed, the read end of the exit
209 // pipe has been closed. It is therefore not a problem that
210 // writennnj fails with a broken pipe error. Other errors should
211 // not happen.
212 if (result != -1 && result != sizeof(message)) {
213 FATAL("Failed to write entire process exit message");
214 } else if (result == -1 && errno != EPIPE) {
215 FATAL1("Failed to write exit code: %d", errno);
216 }
217 ProcessInfoList::RemoveProcess(pid);
218 }
219 }
220 }
221
222
223 // Entry point for the separate exit code handler thread started by 158 // Entry point for the separate exit code handler thread started by
224 // the ExitCodeHandler. 159 // the ExitCodeHandler.
225 static void ExitCodeHandlerEntry(uword param) { 160 static void ExitCodeHandlerEntry(uword param) {
226 struct pollfd pollfds; 161 pid_t pid = 0;
227 pollfds.fd = param; 162 int status = 0;
228 pollfds.events = POLLIN; 163 while (!terminate_) {
229 while (true) { 164 if ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0))) > 0) {
230 int result = TEMP_FAILURE_RETRY(poll(&pollfds, 1, -1)); 165 int exit_code = 0;
231 if (result == -1) { 166 int negative = 0;
232 ASSERT(EAGAIN == EWOULDBLOCK); 167 if (WIFEXITED(status)) {
233 if (errno != EWOULDBLOCK) { 168 exit_code = WEXITSTATUS(status);
234 perror("ExitCodeHandler poll failed");
235 } 169 }
236 } else { 170 if (WIFSIGNALED(status)) {
237 // Read the byte from the wake-up fd. 171 exit_code = WTERMSIG(status);
238 ASSERT(result = 1); 172 negative = 1;
239 intptr_t data = 0;
240 ssize_t read_bytes = FDUtils::ReadFromBlocking(pollfds.fd, &data, 1);
241 if (read_bytes < 1) {
242 perror("Failed to read from wake-up fd in exit-code handler");
243 } 173 }
244 if (data == ExitCodeHandler::kThreadTerminateByte) { 174 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
245 ExitCodeThreadTerminated(); 175 if (exit_code_fd != 0) {
246 return; 176 int message[2] = { exit_code, negative };
177 ssize_t result =
178 FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
179 // If the process has been closed, the read end of the exit
180 // pipe has been closed. It is therefore not a problem that
181 // write fails with a broken pipe error. Other errors should
182 // not happen.
183 if (result != -1 && result != sizeof(message)) {
184 FATAL("Failed to write entire process exit message");
185 } else if (result == -1 && errno != EPIPE) {
186 FATAL1("Failed to write exit code: %d", errno);
187 }
188 ProcessInfoList::RemoveProcess(pid);
247 } 189 }
248 // Get the exit code from all processes that have died.
249 GetProcessExitCodes();
250 } 190 }
251 } 191 }
192 thread_terminate_monitor_->Enter();
193 thread_terminate_monitor_->Notify();
194 thread_terminate_monitor_->Exit();
252 } 195 }
253 196
254 static dart::Mutex* mutex_; 197 static dart::Mutex* mutex_;
255 static bool initialized_; 198 static bool initialized_;
256 static int sig_chld_fds_[2]; 199 static bool terminate_;
257 static bool thread_terminated_;
258 static dart::Monitor* thread_terminate_monitor_; 200 static dart::Monitor* thread_terminate_monitor_;
259 }; 201 };
260 202
261 203
262 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex(); 204 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex();
263 bool ExitCodeHandler::initialized_ = false; 205 bool ExitCodeHandler::initialized_ = false;
264 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 }; 206 bool ExitCodeHandler::terminate_ = false;
265 bool ExitCodeHandler::thread_terminated_ = false;
266 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor(); 207 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor();
267 208
268 209
269 static void SetChildOsErrorMessage(char** os_error_message) { 210 static void SetChildOsErrorMessage(char** os_error_message) {
270 const int kBufferSize = 1024; 211 const int kBufferSize = 1024;
271 char error_message[kBufferSize]; 212 char error_message[kBufferSize];
272 strerror_r(errno, error_message, kBufferSize); 213 strerror_r(errno, error_message, kBufferSize);
273 *os_error_message = strdup(error_message); 214 *os_error_message = strdup(error_message);
274 } 215 }
275 216
276 217
277 static void SigChldHandler(int process_signal, siginfo_t* siginfo, void* tmp) {
278 // Save errno so it can be restored at the end.
279 int entry_errno = errno;
280 // Signal the exit code handler where the actual processing takes
281 // place.
282 ssize_t result =
283 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), "", 1));
284 if (result < 1) {
285 perror("Failed to write to wake-up fd in SIGCHLD handler");
286 }
287 // Restore errno.
288 errno = entry_errno;
289 }
290
291
292 static void ReportChildError(int exec_control_fd) { 218 static void ReportChildError(int exec_control_fd) {
293 // In the case of failure in the child process write the errno and 219 // In the case of failure in the child process write the errno and
294 // the OS error message to the exec control pipe and exit. 220 // the OS error message to the exec control pipe and exit.
295 int child_errno = errno; 221 int child_errno = errno;
296 const int kBufferSize = 1024; 222 const int kBufferSize = 1024;
297 char os_error_message[kBufferSize]; 223 char os_error_message[kBufferSize];
298 strerror_r(errno, os_error_message, kBufferSize); 224 strerror_r(errno, os_error_message, kBufferSize);
299 ASSERT(sizeof(child_errno) == sizeof(errno)); 225 ASSERT(sizeof(child_errno) == sizeof(errno));
300 int bytes_written = 226 int bytes_written =
301 FDUtils::WriteToBlocking( 227 FDUtils::WriteToBlocking(
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 330
405 char** program_environment = NULL; 331 char** program_environment = NULL;
406 if (environment != NULL) { 332 if (environment != NULL) {
407 program_environment = new char*[environment_length + 1]; 333 program_environment = new char*[environment_length + 1];
408 for (int i = 0; i < environment_length; i++) { 334 for (int i = 0; i < environment_length; i++) {
409 program_environment[i] = environment[i]; 335 program_environment[i] = environment[i];
410 } 336 }
411 program_environment[environment_length] = NULL; 337 program_environment[environment_length] = NULL;
412 } 338 }
413 339
414 struct sigaction act;
415 bzero(&act, sizeof(act));
416 act.sa_sigaction = SigChldHandler;
417 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
418 if (sigaction(SIGCHLD, &act, 0) != 0) {
419 perror("Process start: setting signal handler failed");
420 }
421 pid = TEMP_FAILURE_RETRY(fork()); 340 pid = TEMP_FAILURE_RETRY(fork());
422 if (pid < 0) { 341 if (pid < 0) {
423 SetChildOsErrorMessage(os_error_message); 342 SetChildOsErrorMessage(os_error_message);
424 delete[] program_arguments; 343 delete[] program_arguments;
425 VOID_TEMP_FAILURE_RETRY(close(read_in[0])); 344 VOID_TEMP_FAILURE_RETRY(close(read_in[0]));
426 VOID_TEMP_FAILURE_RETRY(close(read_in[1])); 345 VOID_TEMP_FAILURE_RETRY(close(read_in[1]));
427 VOID_TEMP_FAILURE_RETRY(close(read_err[0])); 346 VOID_TEMP_FAILURE_RETRY(close(read_err[0]));
428 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); 347 VOID_TEMP_FAILURE_RETRY(close(read_err[1]));
429 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); 348 VOID_TEMP_FAILURE_RETRY(close(write_out[0]));
430 VOID_TEMP_FAILURE_RETRY(close(write_out[1])); 349 VOID_TEMP_FAILURE_RETRY(close(write_out[1]));
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 619
701 620
702 intptr_t Process::CurrentProcessId() { 621 intptr_t Process::CurrentProcessId() {
703 return static_cast<intptr_t>(getpid()); 622 return static_cast<intptr_t>(getpid());
704 } 623 }
705 624
706 } // namespace bin 625 } // namespace bin
707 } // namespace dart 626 } // namespace dart
708 627
709 #endif // defined(TARGET_OS_MACOS) 628 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698