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

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