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

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

Issue 23717038: Simplify process exit-code handling on Posix, take 2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Simplify monitor logic. 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
« runtime/bin/process_linux.cc ('K') | « 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 terminate_ = false;
129 // that the exit handler thread can poll.
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 129
137 // Start thread that polls the pipe and handles process exits when 130 // Start thread that handles process exits when waitpid returns.
138 // data is received on the pipe. 131 int result = dart::Thread::Start(ExitCodeHandlerEntry, 0);
139 result = dart::Thread::Start(ExitCodeHandlerEntry, sig_chld_fds_[0]);
140 if (result != 0) { 132 if (result != 0) {
141 FATAL1("Failed to start exit code handler worker thread %d", result); 133 FATAL1("Failed to start exit code handler worker thread %d", result);
142 } 134 }
143 135
144 // Mark write end non-blocking.
145 FDUtils::SetNonBlocking(sig_chld_fds_[1]);
146
147 // Thread started and the ExitCodeHandler is initialized. 136 // Thread started and the ExitCodeHandler is initialized.
148 initialized_ = true; 137 initialized_ = true;
149 return true; 138 return true;
150 } 139 }
151 140
152 // Get the write end of the pipe.
153 static int WakeUpFd() {
154 return sig_chld_fds_[1];
155 }
156
157 static void TerminateExitCodeThread() { 141 static void TerminateExitCodeThread() {
158 MutexLocker locker(mutex_); 142 MutexLocker locker(mutex_);
159 if (!initialized_) { 143 if (!initialized_) {
160 return; 144 return;
161 } 145 }
162 146
163 uint8_t data = kThreadTerminateByte; 147 thread_terminate_monitor_->Enter();
164 ssize_t result = 148
165 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1)); 149 terminate_ = true;
166 if (result < 1) { 150 // Fork to wake up waitpid.
167 perror("Failed to write to wake-up fd to terminate exit code thread"); 151 if (TEMP_FAILURE_RETRY(fork()) == 0) {
152 exit(0);
168 } 153 }
169 154
170 { 155 while (!initialized_) {
171 MonitorLocker terminate_locker(thread_terminate_monitor_); 156 thread_terminate_monitor_->Wait(dart::Monitor::kNoTimeout);
172 while (!thread_terminated_) {
173 terminate_locker.Wait();
174 }
175 } 157 }
176 } 158 thread_terminate_monitor_->Exit();
177
178 static void ExitCodeThreadTerminated() {
179 MonitorLocker locker(thread_terminate_monitor_);
180 thread_terminated_ = true;
181 locker.Notify();
182 } 159 }
183 160
184 private: 161 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 162 // Entry point for the separate exit code handler thread started by
224 // the ExitCodeHandler. 163 // the ExitCodeHandler.
225 static void ExitCodeHandlerEntry(uword param) { 164 static void ExitCodeHandlerEntry(uword param) {
226 struct pollfd pollfds; 165 pid_t pid = 0;
227 pollfds.fd = param; 166 int status = 0;
228 pollfds.events = POLLIN; 167 while (!terminate_) {
229 while (true) { 168 if ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0))) > 0) {
230 int result = TEMP_FAILURE_RETRY(poll(&pollfds, 1, -1)); 169 int exit_code = 0;
231 if (result == -1) { 170 int negative = 0;
232 ASSERT(EAGAIN == EWOULDBLOCK); 171 if (WIFEXITED(status)) {
233 if (errno != EWOULDBLOCK) { 172 exit_code = WEXITSTATUS(status);
234 perror("ExitCodeHandler poll failed");
235 } 173 }
236 } else { 174 if (WIFSIGNALED(status)) {
237 // Read the byte from the wake-up fd. 175 exit_code = WTERMSIG(status);
238 ASSERT(result = 1); 176 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 } 177 }
244 if (data == ExitCodeHandler::kThreadTerminateByte) { 178 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
245 ExitCodeThreadTerminated(); 179 if (exit_code_fd != 0) {
180 int message[2] = { exit_code, negative };
181 ssize_t result =
182 FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
183 // If the process has been closed, the read end of the exit
184 // pipe has been closed. It is therefore not a problem that
185 // write fails with a broken pipe error. Other errors should
186 // not happen.
187 if (result != -1 && result != sizeof(message)) {
188 FATAL("Failed to write entire process exit message");
189 } else if (result == -1 && errno != EPIPE) {
190 FATAL1("Failed to write exit code: %d", errno);
191 }
192 ProcessInfoList::RemoveProcess(pid);
193 }
194 } else if (errno == ECHILD) {
195 if (mutex_->TryLock()) {
196 initialized_ = false;
197 terminate_ = true;
198 mutex_->Unlock();
246 return; 199 return;
247 } 200 }
248 // Get the exit code from all processes that have died.
249 GetProcessExitCodes();
250 } 201 }
251 } 202 }
203 thread_terminate_monitor_->Enter();
204 initialized_ = false;
205 thread_terminate_monitor_->Notify();
206 thread_terminate_monitor_->Exit();
252 } 207 }
253 208
254 static dart::Mutex* mutex_; 209 static dart::Mutex* mutex_;
255 static bool initialized_; 210 static bool initialized_;
256 static int sig_chld_fds_[2]; 211 static bool terminate_;
257 static bool thread_terminated_;
258 static dart::Monitor* thread_terminate_monitor_; 212 static dart::Monitor* thread_terminate_monitor_;
259 }; 213 };
260 214
261 215
262 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex(); 216 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex();
263 bool ExitCodeHandler::initialized_ = false; 217 bool ExitCodeHandler::initialized_ = false;
264 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 }; 218 bool ExitCodeHandler::terminate_ = false;
265 bool ExitCodeHandler::thread_terminated_ = false;
266 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor(); 219 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor();
267 220
268 221
269 static void SetChildOsErrorMessage(char** os_error_message) { 222 static void SetChildOsErrorMessage(char** os_error_message) {
270 const int kBufferSize = 1024; 223 const int kBufferSize = 1024;
271 char error_message[kBufferSize]; 224 char error_message[kBufferSize];
272 strerror_r(errno, error_message, kBufferSize); 225 strerror_r(errno, error_message, kBufferSize);
273 *os_error_message = strdup(error_message); 226 *os_error_message = strdup(error_message);
274 } 227 }
275 228
276 229
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) { 230 static void ReportChildError(int exec_control_fd) {
293 // In the case of failure in the child process write the errno and 231 // 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. 232 // the OS error message to the exec control pipe and exit.
295 int child_errno = errno; 233 int child_errno = errno;
296 const int kBufferSize = 1024; 234 const int kBufferSize = 1024;
297 char os_error_message[kBufferSize]; 235 char os_error_message[kBufferSize];
298 strerror_r(errno, os_error_message, kBufferSize); 236 strerror_r(errno, os_error_message, kBufferSize);
299 ASSERT(sizeof(child_errno) == sizeof(errno)); 237 ASSERT(sizeof(child_errno) == sizeof(errno));
300 int bytes_written = 238 int bytes_written =
301 FDUtils::WriteToBlocking( 239 FDUtils::WriteToBlocking(
(...skipping 19 matching lines...) Expand all
321 intptr_t* id, 259 intptr_t* id,
322 intptr_t* exit_event, 260 intptr_t* exit_event,
323 char** os_error_message) { 261 char** os_error_message) {
324 pid_t pid; 262 pid_t pid;
325 int read_in[2]; // Pipe for stdout to child process. 263 int read_in[2]; // Pipe for stdout to child process.
326 int read_err[2]; // Pipe for stderr to child process. 264 int read_err[2]; // Pipe for stderr to child process.
327 int write_out[2]; // Pipe for stdin to child process. 265 int write_out[2]; // Pipe for stdin to child process.
328 int exec_control[2]; // Pipe to get the result from exec. 266 int exec_control[2]; // Pipe to get the result from exec.
329 int result; 267 int result;
330 268
331 bool initialized = ExitCodeHandler::EnsureInitialized();
332 if (!initialized) {
333 SetChildOsErrorMessage(os_error_message);
334 Log::PrintErr("Error initializing exit code handler: %s\n",
335 *os_error_message);
336 return errno;
337 }
338
339 result = TEMP_FAILURE_RETRY(pipe(read_in)); 269 result = TEMP_FAILURE_RETRY(pipe(read_in));
340 if (result < 0) { 270 if (result < 0) {
341 SetChildOsErrorMessage(os_error_message); 271 SetChildOsErrorMessage(os_error_message);
342 Log::PrintErr("Error pipe creation failed: %s\n", *os_error_message); 272 Log::PrintErr("Error pipe creation failed: %s\n", *os_error_message);
343 return errno; 273 return errno;
344 } 274 }
345 FDUtils::SetCloseOnExec(read_in[0]); 275 FDUtils::SetCloseOnExec(read_in[0]);
346 276
347 result = TEMP_FAILURE_RETRY(pipe(read_err)); 277 result = TEMP_FAILURE_RETRY(pipe(read_err));
348 if (result < 0) { 278 if (result < 0) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 334
405 char** program_environment = NULL; 335 char** program_environment = NULL;
406 if (environment != NULL) { 336 if (environment != NULL) {
407 program_environment = new char*[environment_length + 1]; 337 program_environment = new char*[environment_length + 1];
408 for (int i = 0; i < environment_length; i++) { 338 for (int i = 0; i < environment_length; i++) {
409 program_environment[i] = environment[i]; 339 program_environment[i] = environment[i];
410 } 340 }
411 program_environment[environment_length] = NULL; 341 program_environment[environment_length] = NULL;
412 } 342 }
413 343
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()); 344 pid = TEMP_FAILURE_RETRY(fork());
422 if (pid < 0) { 345 if (pid < 0) {
423 SetChildOsErrorMessage(os_error_message); 346 SetChildOsErrorMessage(os_error_message);
424 delete[] program_arguments; 347 delete[] program_arguments;
425 VOID_TEMP_FAILURE_RETRY(close(read_in[0])); 348 VOID_TEMP_FAILURE_RETRY(close(read_in[0]));
426 VOID_TEMP_FAILURE_RETRY(close(read_in[1])); 349 VOID_TEMP_FAILURE_RETRY(close(read_in[1]));
427 VOID_TEMP_FAILURE_RETRY(close(read_err[0])); 350 VOID_TEMP_FAILURE_RETRY(close(read_err[0]));
428 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); 351 VOID_TEMP_FAILURE_RETRY(close(read_err[1]));
429 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); 352 VOID_TEMP_FAILURE_RETRY(close(write_out[0]));
430 VOID_TEMP_FAILURE_RETRY(close(write_out[1])); 353 VOID_TEMP_FAILURE_RETRY(close(write_out[1]));
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 FDUtils::SetNonBlocking(read_in[0]); 477 FDUtils::SetNonBlocking(read_in[0]);
555 *in = read_in[0]; 478 *in = read_in[0];
556 VOID_TEMP_FAILURE_RETRY(close(read_in[1])); 479 VOID_TEMP_FAILURE_RETRY(close(read_in[1]));
557 FDUtils::SetNonBlocking(write_out[1]); 480 FDUtils::SetNonBlocking(write_out[1]);
558 *out = write_out[1]; 481 *out = write_out[1];
559 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); 482 VOID_TEMP_FAILURE_RETRY(close(write_out[0]));
560 FDUtils::SetNonBlocking(read_err[0]); 483 FDUtils::SetNonBlocking(read_err[0]);
561 *err = read_err[0]; 484 *err = read_err[0];
562 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); 485 VOID_TEMP_FAILURE_RETRY(close(read_err[1]));
563 486
487 // Be sure to listen for exit-codes, now we have a child-process.
488 bool initialized = ExitCodeHandler::EnsureInitialized();
489 if (!initialized) {
490 SetChildOsErrorMessage(os_error_message);
491 Log::PrintErr("Error initializing exit code handler: %s\n",
492 *os_error_message);
493 return errno;
494 }
495
564 *id = pid; 496 *id = pid;
565 return 0; 497 return 0;
566 } 498 }
567 499
568 500
569 class BufferList: public BufferListBase { 501 class BufferList: public BufferListBase {
570 public: 502 public:
571 bool Read(int fd, intptr_t available) { 503 bool Read(int fd, intptr_t available) {
572 // Read all available bytes. 504 // Read all available bytes.
573 while (available > 0) { 505 while (available > 0) {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 632
701 633
702 intptr_t Process::CurrentProcessId() { 634 intptr_t Process::CurrentProcessId() {
703 return static_cast<intptr_t>(getpid()); 635 return static_cast<intptr_t>(getpid());
704 } 636 }
705 637
706 } // namespace bin 638 } // namespace bin
707 } // namespace dart 639 } // namespace dart
708 640
709 #endif // defined(TARGET_OS_MACOS) 641 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« runtime/bin/process_linux.cc ('K') | « runtime/bin/process_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698