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

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

Issue 23536032: Revert "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 waits for child 112 // The exit code handler sets up a separate thread which is signalled
113 // processes to die. That separate thread can then get the exit code from 113 // on SIGCHLD. 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 // Start thread that handles process exits when waitpid returns. 128 // Allocate a pipe that the signal handler can write a byte to and
129 int result = dart::Thread::Start(ExitCodeHandlerEntry, 0); 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
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]);
130 if (result != 0) { 140 if (result != 0) {
131 FATAL1("Failed to start exit code handler worker thread %d", result); 141 FATAL1("Failed to start exit code handler worker thread %d", result);
132 } 142 }
133 143
144 // Mark write end non-blocking.
145 FDUtils::SetNonBlocking(sig_chld_fds_[1]);
146
134 // Thread started and the ExitCodeHandler is initialized. 147 // Thread started and the ExitCodeHandler is initialized.
135 initialized_ = true; 148 initialized_ = true;
136 return true; 149 return true;
137 } 150 }
138 151
152 // Get the write end of the pipe.
153 static int WakeUpFd() {
154 return sig_chld_fds_[1];
155 }
156
139 static void TerminateExitCodeThread() { 157 static void TerminateExitCodeThread() {
140 MutexLocker locker(mutex_); 158 MutexLocker locker(mutex_);
141 if (!initialized_) { 159 if (!initialized_) {
142 return; 160 return;
143 } 161 }
144 162
145 thread_terminate_monitor_->Enter(); 163 uint8_t data = kThreadTerminateByte;
146 164 ssize_t result =
147 terminate_ = true; 165 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1));
148 // Fork to wake up waitpid. 166 if (result < 1) {
149 if (TEMP_FAILURE_RETRY(fork()) == 0) { 167 perror("Failed to write to wake-up fd to terminate exit code thread");
150 exit(0);
151 } 168 }
152 169
153 thread_terminate_monitor_->Wait(dart::Monitor::kNoTimeout); 170 {
154 thread_terminate_monitor_->Exit(); 171 MonitorLocker terminate_locker(thread_terminate_monitor_);
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();
155 } 182 }
156 183
157 private: 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
158 // Entry point for the separate exit code handler thread started by 223 // Entry point for the separate exit code handler thread started by
159 // the ExitCodeHandler. 224 // the ExitCodeHandler.
160 static void ExitCodeHandlerEntry(uword param) { 225 static void ExitCodeHandlerEntry(uword param) {
161 pid_t pid = 0; 226 struct pollfd pollfds;
162 int status = 0; 227 pollfds.fd = param;
163 while (!terminate_) { 228 pollfds.events = POLLIN;
164 if ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0))) > 0) { 229 while (true) {
165 int exit_code = 0; 230 int result = TEMP_FAILURE_RETRY(poll(&pollfds, 1, -1));
166 int negative = 0; 231 if (result == -1) {
167 if (WIFEXITED(status)) { 232 ASSERT(EAGAIN == EWOULDBLOCK);
168 exit_code = WEXITSTATUS(status); 233 if (errno != EWOULDBLOCK) {
234 perror("ExitCodeHandler poll failed");
169 } 235 }
170 if (WIFSIGNALED(status)) { 236 } else {
171 exit_code = WTERMSIG(status); 237 // Read the byte from the wake-up fd.
172 negative = 1; 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");
173 } 243 }
174 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid); 244 if (data == ExitCodeHandler::kThreadTerminateByte) {
175 if (exit_code_fd != 0) { 245 ExitCodeThreadTerminated();
176 int message[2] = { exit_code, negative }; 246 return;
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);
189 } 247 }
248 // Get the exit code from all processes that have died.
249 GetProcessExitCodes();
190 } 250 }
191 } 251 }
192 thread_terminate_monitor_->Enter();
193 thread_terminate_monitor_->Notify();
194 thread_terminate_monitor_->Exit();
195 } 252 }
196 253
197 static dart::Mutex* mutex_; 254 static dart::Mutex* mutex_;
198 static bool initialized_; 255 static bool initialized_;
199 static bool terminate_; 256 static int sig_chld_fds_[2];
257 static bool thread_terminated_;
200 static dart::Monitor* thread_terminate_monitor_; 258 static dart::Monitor* thread_terminate_monitor_;
201 }; 259 };
202 260
203 261
204 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex(); 262 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex();
205 bool ExitCodeHandler::initialized_ = false; 263 bool ExitCodeHandler::initialized_ = false;
206 bool ExitCodeHandler::terminate_ = false; 264 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 };
265 bool ExitCodeHandler::thread_terminated_ = false;
207 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor(); 266 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor();
208 267
209 268
210 static void SetChildOsErrorMessage(char** os_error_message) { 269 static void SetChildOsErrorMessage(char** os_error_message) {
211 const int kBufferSize = 1024; 270 const int kBufferSize = 1024;
212 char error_message[kBufferSize]; 271 char error_message[kBufferSize];
213 strerror_r(errno, error_message, kBufferSize); 272 strerror_r(errno, error_message, kBufferSize);
214 *os_error_message = strdup(error_message); 273 *os_error_message = strdup(error_message);
215 } 274 }
216 275
217 276
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
218 static void ReportChildError(int exec_control_fd) { 292 static void ReportChildError(int exec_control_fd) {
219 // In the case of failure in the child process write the errno and 293 // In the case of failure in the child process write the errno and
220 // the OS error message to the exec control pipe and exit. 294 // the OS error message to the exec control pipe and exit.
221 int child_errno = errno; 295 int child_errno = errno;
222 const int kBufferSize = 1024; 296 const int kBufferSize = 1024;
223 char os_error_message[kBufferSize]; 297 char os_error_message[kBufferSize];
224 strerror_r(errno, os_error_message, kBufferSize); 298 strerror_r(errno, os_error_message, kBufferSize);
225 ASSERT(sizeof(child_errno) == sizeof(errno)); 299 ASSERT(sizeof(child_errno) == sizeof(errno));
226 int bytes_written = 300 int bytes_written =
227 FDUtils::WriteToBlocking( 301 FDUtils::WriteToBlocking(
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 404
331 char** program_environment = NULL; 405 char** program_environment = NULL;
332 if (environment != NULL) { 406 if (environment != NULL) {
333 program_environment = new char*[environment_length + 1]; 407 program_environment = new char*[environment_length + 1];
334 for (int i = 0; i < environment_length; i++) { 408 for (int i = 0; i < environment_length; i++) {
335 program_environment[i] = environment[i]; 409 program_environment[i] = environment[i];
336 } 410 }
337 program_environment[environment_length] = NULL; 411 program_environment[environment_length] = NULL;
338 } 412 }
339 413
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 }
340 pid = TEMP_FAILURE_RETRY(fork()); 421 pid = TEMP_FAILURE_RETRY(fork());
341 if (pid < 0) { 422 if (pid < 0) {
342 SetChildOsErrorMessage(os_error_message); 423 SetChildOsErrorMessage(os_error_message);
343 delete[] program_arguments; 424 delete[] program_arguments;
344 VOID_TEMP_FAILURE_RETRY(close(read_in[0])); 425 VOID_TEMP_FAILURE_RETRY(close(read_in[0]));
345 VOID_TEMP_FAILURE_RETRY(close(read_in[1])); 426 VOID_TEMP_FAILURE_RETRY(close(read_in[1]));
346 VOID_TEMP_FAILURE_RETRY(close(read_err[0])); 427 VOID_TEMP_FAILURE_RETRY(close(read_err[0]));
347 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); 428 VOID_TEMP_FAILURE_RETRY(close(read_err[1]));
348 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); 429 VOID_TEMP_FAILURE_RETRY(close(write_out[0]));
349 VOID_TEMP_FAILURE_RETRY(close(write_out[1])); 430 VOID_TEMP_FAILURE_RETRY(close(write_out[1]));
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 700
620 701
621 intptr_t Process::CurrentProcessId() { 702 intptr_t Process::CurrentProcessId() {
622 return static_cast<intptr_t>(getpid()); 703 return static_cast<intptr_t>(getpid());
623 } 704 }
624 705
625 } // namespace bin 706 } // namespace bin
626 } // namespace dart 707 } // namespace dart
627 708
628 #endif // defined(TARGET_OS_MACOS) 709 #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