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

Side by Side Diff: runtime/bin/process_android.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: Remove now unneeded code in platform_*.cc 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/platform_macos.cc ('k') | runtime/bin/process_linux.cc » ('j') | 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_ANDROID) 6 #if defined(TARGET_OS_ANDROID)
7 7
8 #include "bin/process.h" 8 #include "bin/process.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
11 #include <fcntl.h> // NOLINT 11 #include <fcntl.h> // NOLINT
12 #include <poll.h> // NOLINT 12 #include <poll.h> // NOLINT
13 #include <signal.h> // NOLINT
14 #include <stdio.h> // NOLINT 13 #include <stdio.h> // NOLINT
15 #include <stdlib.h> // NOLINT 14 #include <stdlib.h> // NOLINT
16 #include <string.h> // NOLINT 15 #include <string.h> // NOLINT
17 #include <sys/wait.h> // NOLINT 16 #include <sys/wait.h> // NOLINT
18 #include <unistd.h> // NOLINT 17 #include <unistd.h> // NOLINT
19 18
20 #include "bin/fdutils.h" 19 #include "bin/fdutils.h"
21 #include "bin/log.h" 20 #include "bin/log.h"
22 #include "bin/thread.h" 21 #include "bin/thread.h"
23 22
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 // Mutex protecting all accesses to the linked list of active 103 // Mutex protecting all accesses to the linked list of active
105 // processes. 104 // processes.
106 static dart::Mutex* mutex_; 105 static dart::Mutex* mutex_;
107 }; 106 };
108 107
109 108
110 ProcessInfo* ProcessInfoList::active_processes_ = NULL; 109 ProcessInfo* ProcessInfoList::active_processes_ = NULL;
111 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex(); 110 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex();
112 111
113 112
114 // The exit code handler sets up a separate thread which is signalled 113 // The exit code handler sets up a separate thread which waits for child
115 // on SIGCHLD. That separate thread can then get the exit code from 114 // processes to die. That separate thread can then get the exit code from
116 // processes that have exited and communicate it to Dart through the 115 // processes that have exited and communicate it to Dart through the
117 // event loop. 116 // event loop.
118 class ExitCodeHandler { 117 class ExitCodeHandler {
119 public: 118 public:
120 // Ensure that the ExitCodeHandler has been initialized. 119 // Ensure that the ExitCodeHandler has been initialized.
121 static bool EnsureInitialized() { 120 static bool EnsureInitialized() {
122 // Multiple isolates could be starting processes at the same 121 // Multiple isolates could be starting processes at the same
123 // time. Make sure that only one of them initializes the 122 // time. Make sure that only one of them initializes the
124 // ExitCodeHandler. 123 // ExitCodeHandler.
125 MutexLocker locker(mutex_); 124 MutexLocker locker(mutex_);
126 if (initialized_) { 125 if (initialized_) {
127 return true; 126 return true;
128 } 127 }
129 128
130 // Allocate a pipe that the signal handler can write a byte to and 129 terminate_ = false;
131 // that the exit handler thread can poll.
132 int result = TEMP_FAILURE_RETRY(pipe(sig_chld_fds_));
133 if (result < 0) {
134 return false;
135 }
136 FDUtils::SetCloseOnExec(sig_chld_fds_[0]);
137 FDUtils::SetCloseOnExec(sig_chld_fds_[1]);
138 130
139 // Start thread that polls the pipe and handles process exits when 131 // Start thread that handles process exits when waitpid returns.
140 // data is received on the pipe. 132 int result = dart::Thread::Start(ExitCodeHandlerEntry, 0);
141 result = dart::Thread::Start(ExitCodeHandlerEntry, sig_chld_fds_[0]);
142 if (result != 0) { 133 if (result != 0) {
143 FATAL1("Failed to start exit code handler worker thread %d", result); 134 FATAL1("Failed to start exit code handler worker thread %d", result);
144 } 135 }
145 136
146 // Mark write end non-blocking.
147 FDUtils::SetNonBlocking(sig_chld_fds_[1]);
148
149 // Thread started and the ExitCodeHandler is initialized. 137 // Thread started and the ExitCodeHandler is initialized.
150 initialized_ = true; 138 initialized_ = true;
151 return true; 139 return true;
152 } 140 }
153 141
154 // Get the write end of the pipe.
155 static int WakeUpFd() {
156 return sig_chld_fds_[1];
157 }
158
159 static void TerminateExitCodeThread() { 142 static void TerminateExitCodeThread() {
160 MutexLocker locker(mutex_); 143 MutexLocker locker(mutex_);
161 if (!initialized_) { 144 if (!initialized_) {
162 return; 145 return;
163 } 146 }
164 147
165 uint8_t data = kThreadTerminateByte; 148 thread_terminate_monitor_->Enter();
166 ssize_t result = 149
167 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1)); 150 terminate_ = true;
168 if (result < 1) { 151 // Fork to wake up waitpid.
169 perror("Failed to write to wake-up fd to terminate exit code thread"); 152 if (TEMP_FAILURE_RETRY(fork()) == 0) {
153 exit(0);
170 } 154 }
171 155
172 { 156 while (!initialized_) {
Mads Ager (google) 2013/09/09 16:35:11 Shouldn't you be looping while initialized_ is tru
Anders Johnsen 2013/09/09 17:55:36 You are absolutely right, I'll rename to better ex
173 MonitorLocker terminate_locker(thread_terminate_monitor_); 157 thread_terminate_monitor_->Wait(dart::Monitor::kNoTimeout);
174 while (!thread_terminated_) {
175 terminate_locker.Wait();
176 }
177 } 158 }
178 } 159 thread_terminate_monitor_->Exit();
179
180 static void ExitCodeThreadTerminated() {
181 MonitorLocker locker(thread_terminate_monitor_);
182 thread_terminated_ = true;
183 locker.Notify();
184 } 160 }
185 161
186 private: 162 private:
187 static const uint8_t kThreadTerminateByte = 1;
188
189 // GetProcessExitCodes is called on a separate thread when a SIGCHLD
190 // signal is received to retrieve the exit codes and post them to
191 // dart.
192 static void GetProcessExitCodes() {
193 pid_t pid = 0;
194 int status = 0;
195 while ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG))) > 0) {
196 int exit_code = 0;
197 int negative = 0;
198 if (WIFEXITED(status)) {
199 exit_code = WEXITSTATUS(status);
200 }
201 if (WIFSIGNALED(status)) {
202 exit_code = WTERMSIG(status);
203 negative = 1;
204 }
205 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
206 if (exit_code_fd != 0) {
207 int message[2] = { exit_code, negative };
208 ssize_t result =
209 FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
210 // If the process has been closed, the read end of the exit
211 // pipe has been closed. It is therefore not a problem that
212 // write fails with a broken pipe error. Other errors should
213 // not happen.
214 if (result != -1 && result != sizeof(message)) {
215 FATAL("Failed to write entire process exit message");
216 } else if (result == -1 && errno != EPIPE) {
217 FATAL1("Failed to write exit code: %d", errno);
218 }
219 ProcessInfoList::RemoveProcess(pid);
220 }
221 }
222 }
223
224
225 // Entry point for the separate exit code handler thread started by 163 // Entry point for the separate exit code handler thread started by
226 // the ExitCodeHandler. 164 // the ExitCodeHandler.
227 static void ExitCodeHandlerEntry(uword param) { 165 static void ExitCodeHandlerEntry(uword param) {
228 struct pollfd pollfds; 166 pid_t pid = 0;
229 pollfds.fd = param; 167 int status = 0;
230 pollfds.events = POLLIN; 168 while (!terminate_) {
231 while (true) { 169 if ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0))) > 0) {
232 int result = TEMP_FAILURE_RETRY(poll(&pollfds, 1, -1)); 170 int exit_code = 0;
233 if (result == -1) { 171 int negative = 0;
234 ASSERT(EAGAIN == EWOULDBLOCK); 172 if (WIFEXITED(status)) {
235 if (errno != EWOULDBLOCK) { 173 exit_code = WEXITSTATUS(status);
236 perror("ExitCodeHandler poll failed");
237 } 174 }
238 } else { 175 if (WIFSIGNALED(status)) {
239 // Read the byte from the wake-up fd. 176 exit_code = WTERMSIG(status);
240 ASSERT(result = 1); 177 negative = 1;
241 intptr_t data = 0;
242 ssize_t read_bytes = FDUtils::ReadFromBlocking(pollfds.fd, &data, 1);
243 if (read_bytes < 1) {
244 perror("Failed to read from wake-up fd in exit-code handler");
245 } 178 }
246 if (data == ExitCodeHandler::kThreadTerminateByte) { 179 intptr_t exit_code_fd = ProcessInfoList::LookupProcessExitFd(pid);
247 ExitCodeThreadTerminated(); 180 if (exit_code_fd != 0) {
181 int message[2] = { exit_code, negative };
182 ssize_t result =
183 FDUtils::WriteToBlocking(exit_code_fd, &message, sizeof(message));
184 // If the process has been closed, the read end of the exit
185 // pipe has been closed. It is therefore not a problem that
186 // write fails with a broken pipe error. Other errors should
187 // not happen.
188 if (result != -1 && result != sizeof(message)) {
189 FATAL("Failed to write entire process exit message");
190 } else if (result == -1 && errno != EPIPE) {
191 FATAL1("Failed to write exit code: %d", errno);
192 }
193 ProcessInfoList::RemoveProcess(pid);
194 }
195 } else if (errno == ECHILD) {
196 if (mutex_->TryLock()) {
197 initialized_ = false;
198 terminate_ = true;
Mads Ager (google) 2013/09/09 16:35:11 I guess setting terminate_ here doesn't really do
Anders Johnsen 2013/09/09 17:55:36 As it's able to take the lock, it know that it's _
199 mutex_->Unlock();
248 return; 200 return;
249 } 201 }
250 // Get the exit code from all processes that have died.
251 GetProcessExitCodes();
252 } 202 }
253 } 203 }
204 thread_terminate_monitor_->Enter();
205 initialized_ = false;
Mads Ager (google) 2013/09/09 16:35:11 I don't like that we have collapsed thread_termina
Anders Johnsen 2013/09/09 17:55:36 Yes, the rename should have made this much clearer
206 thread_terminate_monitor_->Notify();
207 thread_terminate_monitor_->Exit();
254 } 208 }
255 209
256 static dart::Mutex* mutex_; 210 static dart::Mutex* mutex_;
257 static bool initialized_; 211 static bool initialized_;
258 static int sig_chld_fds_[2]; 212 static bool terminate_;
259 static bool thread_terminated_;
260 static dart::Monitor* thread_terminate_monitor_; 213 static dart::Monitor* thread_terminate_monitor_;
261 }; 214 };
262 215
263 216
264 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex(); 217 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex();
265 bool ExitCodeHandler::initialized_ = false; 218 bool ExitCodeHandler::initialized_ = false;
266 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 }; 219 bool ExitCodeHandler::terminate_ = false;
267 bool ExitCodeHandler::thread_terminated_ = false;
268 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor(); 220 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor();
269 221
270 222
271 static void SetChildOsErrorMessage(char** os_error_message) { 223 static void SetChildOsErrorMessage(char** os_error_message) {
272 const int kBufferSize = 1024; 224 const int kBufferSize = 1024;
273 char error_message[kBufferSize]; 225 char error_message[kBufferSize];
274 strerror_r(errno, error_message, kBufferSize); 226 strerror_r(errno, error_message, kBufferSize);
275 *os_error_message = strdup(error_message); 227 *os_error_message = strdup(error_message);
276 } 228 }
277 229
278 230
279 static void SigChldHandler(int process_signal, siginfo_t* siginfo, void* tmp) {
280 // Save errno so it can be restored at the end.
281 int entry_errno = errno;
282 // Signal the exit code handler where the actual processing takes
283 // place.
284 ssize_t result =
285 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), "", 1));
286 if (result < 1) {
287 perror("Failed to write to wake-up fd in SIGCHLD handler");
288 }
289 // Restore errno.
290 errno = entry_errno;
291 }
292
293
294 static void ReportChildError(int exec_control_fd) { 231 static void ReportChildError(int exec_control_fd) {
295 // In the case of failure in the child process write the errno and 232 // In the case of failure in the child process write the errno and
296 // the OS error message to the exec control pipe and exit. 233 // the OS error message to the exec control pipe and exit.
297 int child_errno = errno; 234 int child_errno = errno;
298 const int kBufferSize = 1024; 235 const int kBufferSize = 1024;
299 char os_error_message[kBufferSize]; 236 char os_error_message[kBufferSize];
300 strerror_r(errno, os_error_message, kBufferSize); 237 strerror_r(errno, os_error_message, kBufferSize);
301 ASSERT(sizeof(child_errno) == sizeof(errno)); 238 ASSERT(sizeof(child_errno) == sizeof(errno));
302 int bytes_written = 239 int bytes_written =
303 FDUtils::WriteToBlocking( 240 FDUtils::WriteToBlocking(
(...skipping 19 matching lines...) Expand all
323 intptr_t* id, 260 intptr_t* id,
324 intptr_t* exit_event, 261 intptr_t* exit_event,
325 char** os_error_message) { 262 char** os_error_message) {
326 pid_t pid; 263 pid_t pid;
327 int read_in[2]; // Pipe for stdout to child process. 264 int read_in[2]; // Pipe for stdout to child process.
328 int read_err[2]; // Pipe for stderr to child process. 265 int read_err[2]; // Pipe for stderr to child process.
329 int write_out[2]; // Pipe for stdin to child process. 266 int write_out[2]; // Pipe for stdin to child process.
330 int exec_control[2]; // Pipe to get the result from exec. 267 int exec_control[2]; // Pipe to get the result from exec.
331 int result; 268 int result;
332 269
333 bool initialized = ExitCodeHandler::EnsureInitialized();
334 if (!initialized) {
335 SetChildOsErrorMessage(os_error_message);
336 Log::PrintErr("Error initializing exit code handler: %s\n",
337 *os_error_message);
338 return errno;
339 }
340
341 result = TEMP_FAILURE_RETRY(pipe(read_in)); 270 result = TEMP_FAILURE_RETRY(pipe(read_in));
342 if (result < 0) { 271 if (result < 0) {
343 SetChildOsErrorMessage(os_error_message); 272 SetChildOsErrorMessage(os_error_message);
344 Log::PrintErr("Error pipe creation failed: %s\n", *os_error_message); 273 Log::PrintErr("Error pipe creation failed: %s\n", *os_error_message);
345 return errno; 274 return errno;
346 } 275 }
347 FDUtils::SetCloseOnExec(read_in[0]); 276 FDUtils::SetCloseOnExec(read_in[0]);
348 277
349 result = TEMP_FAILURE_RETRY(pipe(read_err)); 278 result = TEMP_FAILURE_RETRY(pipe(read_err));
350 if (result < 0) { 279 if (result < 0) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 335
407 char** program_environment = NULL; 336 char** program_environment = NULL;
408 if (environment != NULL) { 337 if (environment != NULL) {
409 program_environment = new char*[environment_length + 1]; 338 program_environment = new char*[environment_length + 1];
410 for (int i = 0; i < environment_length; i++) { 339 for (int i = 0; i < environment_length; i++) {
411 program_environment[i] = environment[i]; 340 program_environment[i] = environment[i];
412 } 341 }
413 program_environment[environment_length] = NULL; 342 program_environment[environment_length] = NULL;
414 } 343 }
415 344
416 struct sigaction act;
417 bzero(&act, sizeof(act));
418 act.sa_sigaction = SigChldHandler;
419 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
420 if (sigaction(SIGCHLD, &act, 0) != 0) {
421 perror("Process start: setting signal handler failed");
422 }
423 pid = TEMP_FAILURE_RETRY(fork()); 345 pid = TEMP_FAILURE_RETRY(fork());
424 if (pid < 0) { 346 if (pid < 0) {
425 SetChildOsErrorMessage(os_error_message); 347 SetChildOsErrorMessage(os_error_message);
426 delete[] program_arguments; 348 delete[] program_arguments;
427 TEMP_FAILURE_RETRY(close(read_in[0])); 349 TEMP_FAILURE_RETRY(close(read_in[0]));
428 TEMP_FAILURE_RETRY(close(read_in[1])); 350 TEMP_FAILURE_RETRY(close(read_in[1]));
429 TEMP_FAILURE_RETRY(close(read_err[0])); 351 TEMP_FAILURE_RETRY(close(read_err[0]));
430 TEMP_FAILURE_RETRY(close(read_err[1])); 352 TEMP_FAILURE_RETRY(close(read_err[1]));
431 TEMP_FAILURE_RETRY(close(write_out[0])); 353 TEMP_FAILURE_RETRY(close(write_out[0]));
432 TEMP_FAILURE_RETRY(close(write_out[1])); 354 TEMP_FAILURE_RETRY(close(write_out[1]));
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 FDUtils::SetNonBlocking(read_in[0]); 478 FDUtils::SetNonBlocking(read_in[0]);
557 *in = read_in[0]; 479 *in = read_in[0];
558 TEMP_FAILURE_RETRY(close(read_in[1])); 480 TEMP_FAILURE_RETRY(close(read_in[1]));
559 FDUtils::SetNonBlocking(write_out[1]); 481 FDUtils::SetNonBlocking(write_out[1]);
560 *out = write_out[1]; 482 *out = write_out[1];
561 TEMP_FAILURE_RETRY(close(write_out[0])); 483 TEMP_FAILURE_RETRY(close(write_out[0]));
562 FDUtils::SetNonBlocking(read_err[0]); 484 FDUtils::SetNonBlocking(read_err[0]);
563 *err = read_err[0]; 485 *err = read_err[0];
564 TEMP_FAILURE_RETRY(close(read_err[1])); 486 TEMP_FAILURE_RETRY(close(read_err[1]));
565 487
488 // Be sure to listen for exit-codes, now we have a child-process.
489 bool initialized = ExitCodeHandler::EnsureInitialized();
490 if (!initialized) {
491 SetChildOsErrorMessage(os_error_message);
492 Log::PrintErr("Error initializing exit code handler: %s\n",
493 *os_error_message);
494 return errno;
495 }
496
566 *id = pid; 497 *id = pid;
567 return 0; 498 return 0;
568 } 499 }
569 500
570 501
571 class BufferList: public BufferListBase { 502 class BufferList: public BufferListBase {
572 public: 503 public:
573 bool Read(int fd, intptr_t available) { 504 bool Read(int fd, intptr_t available) {
574 // Read all available bytes. 505 // Read all available bytes.
575 while (available > 0) { 506 while (available > 0) {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 627
697 628
698 intptr_t Process::CurrentProcessId() { 629 intptr_t Process::CurrentProcessId() {
699 return static_cast<intptr_t>(getpid()); 630 return static_cast<intptr_t>(getpid());
700 } 631 }
701 632
702 } // namespace bin 633 } // namespace bin
703 } // namespace dart 634 } // namespace dart
704 635
705 #endif // defined(TARGET_OS_ANDROID) 636 #endif // defined(TARGET_OS_ANDROID)
OLDNEW
« no previous file with comments | « runtime/bin/platform_macos.cc ('k') | runtime/bin/process_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698