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

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

Issue 10127008: Support passing an environment variable map to child processes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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
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 "bin/process.h" 5 #include "bin/process.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <poll.h> 9 #include <poll.h>
10 #include <signal.h> 10 #include <signal.h>
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 } 300 }
301 TEMP_FAILURE_RETRY(close(exec_control_fd)); 301 TEMP_FAILURE_RETRY(close(exec_control_fd));
302 exit(1); 302 exit(1);
303 } 303 }
304 304
305 305
306 int Process::Start(const char* path, 306 int Process::Start(const char* path,
307 char* arguments[], 307 char* arguments[],
308 intptr_t arguments_length, 308 intptr_t arguments_length,
309 const char* working_directory, 309 const char* working_directory,
310 char* environment[],
311 intptr_t environment_length,
310 intptr_t* in, 312 intptr_t* in,
311 intptr_t* out, 313 intptr_t* out,
312 intptr_t* err, 314 intptr_t* err,
313 intptr_t* id, 315 intptr_t* id,
314 intptr_t* exit_event, 316 intptr_t* exit_event,
315 char* os_error_message, 317 char* os_error_message,
316 int os_error_message_len) { 318 int os_error_message_len) {
317 pid_t pid; 319 pid_t pid;
318 int read_in[2]; // Pipe for stdout to child process. 320 int read_in[2]; // Pipe for stdout to child process.
319 int read_err[2]; // Pipe for stderr to child process. 321 int read_err[2]; // Pipe for stderr to child process.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 TEMP_FAILURE_RETRY(close(read_err[1])); 385 TEMP_FAILURE_RETRY(close(read_err[1]));
384 TEMP_FAILURE_RETRY(close(write_out[0])); 386 TEMP_FAILURE_RETRY(close(write_out[0]));
385 TEMP_FAILURE_RETRY(close(write_out[1])); 387 TEMP_FAILURE_RETRY(close(write_out[1]));
386 TEMP_FAILURE_RETRY(close(exec_control[0])); 388 TEMP_FAILURE_RETRY(close(exec_control[0]));
387 TEMP_FAILURE_RETRY(close(exec_control[1])); 389 TEMP_FAILURE_RETRY(close(exec_control[1]));
388 fprintf(stderr, "fcntl failed: %s\n", os_error_message); 390 fprintf(stderr, "fcntl failed: %s\n", os_error_message);
389 return errno; 391 return errno;
390 } 392 }
391 393
392 char** program_arguments = new char*[arguments_length + 2]; 394 char** program_arguments = new char*[arguments_length + 2];
393 program_arguments[0] = const_cast<char *>(path); 395 program_arguments[0] = const_cast<char*>(path);
394 for (int i = 0; i < arguments_length; i++) { 396 for (int i = 0; i < arguments_length; i++) {
395 program_arguments[i + 1] = arguments[i]; 397 program_arguments[i + 1] = arguments[i];
396 } 398 }
397 program_arguments[arguments_length + 1] = NULL; 399 program_arguments[arguments_length + 1] = NULL;
398 400
401 char** program_environment = NULL;
402 if (environment != NULL) {
403 program_environment = new char*[environment_length + 1];
404 for (int i = 0; i < environment_length; i++) {
405 program_environment[i] = environment[i];
406 }
407 program_environment[environment_length] = NULL;
408 }
409
399 struct sigaction act; 410 struct sigaction act;
400 bzero(&act, sizeof(act)); 411 bzero(&act, sizeof(act));
401 act.sa_sigaction = SigChldHandler; 412 act.sa_sigaction = SigChldHandler;
402 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; 413 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
403 if (sigaction(SIGCHLD, &act, 0) != 0) { 414 if (sigaction(SIGCHLD, &act, 0) != 0) {
404 perror("Process start: setting signal handler failed"); 415 perror("Process start: setting signal handler failed");
405 } 416 }
406 pid = TEMP_FAILURE_RETRY(fork()); 417 pid = TEMP_FAILURE_RETRY(fork());
407 if (pid < 0) { 418 if (pid < 0) {
408 SetChildOsErrorMessage(os_error_message, os_error_message_len); 419 SetChildOsErrorMessage(os_error_message, os_error_message_len);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 if (TEMP_FAILURE_RETRY(dup2(read_err[1], STDERR_FILENO)) == -1) { 454 if (TEMP_FAILURE_RETRY(dup2(read_err[1], STDERR_FILENO)) == -1) {
444 ReportChildError(exec_control[1]); 455 ReportChildError(exec_control[1]);
445 } 456 }
446 TEMP_FAILURE_RETRY(close(read_err[1])); 457 TEMP_FAILURE_RETRY(close(read_err[1]));
447 458
448 if (working_directory != NULL && 459 if (working_directory != NULL &&
449 TEMP_FAILURE_RETRY(chdir(working_directory)) == -1) { 460 TEMP_FAILURE_RETRY(chdir(working_directory)) == -1) {
450 ReportChildError(exec_control[1]); 461 ReportChildError(exec_control[1]);
451 } 462 }
452 463
453 TEMP_FAILURE_RETRY( 464 if (environment != NULL) {
454 execvp(path, const_cast<char* const*>(program_arguments))); 465 TEMP_FAILURE_RETRY(
466 execve(path,
467 const_cast<char* const*>(program_arguments),
468 program_environment));
469 } else {
470 TEMP_FAILURE_RETRY(
471 execvp(path, const_cast<char* const*>(program_arguments)));
472 }
455 ReportChildError(exec_control[1]); 473 ReportChildError(exec_control[1]);
456 } 474 }
457 475
458 // The arguments for the spawned process are not needed any longer. 476 // The arguments and environment for the spawned process are not needed
477 // any longer.
459 delete[] program_arguments; 478 delete[] program_arguments;
479 delete[] program_environment;
460 480
461 int event_fds[2]; 481 int event_fds[2];
462 result = TEMP_FAILURE_RETRY(pipe(event_fds)); 482 result = TEMP_FAILURE_RETRY(pipe(event_fds));
463 if (result < 0) { 483 if (result < 0) {
464 SetChildOsErrorMessage(os_error_message, os_error_message_len); 484 SetChildOsErrorMessage(os_error_message, os_error_message_len);
465 TEMP_FAILURE_RETRY(close(read_in[0])); 485 TEMP_FAILURE_RETRY(close(read_in[0]));
466 TEMP_FAILURE_RETRY(close(read_in[1])); 486 TEMP_FAILURE_RETRY(close(read_in[1]));
467 TEMP_FAILURE_RETRY(close(read_err[0])); 487 TEMP_FAILURE_RETRY(close(read_err[0]));
468 TEMP_FAILURE_RETRY(close(read_err[1])); 488 TEMP_FAILURE_RETRY(close(read_err[1]));
469 TEMP_FAILURE_RETRY(close(write_out[0])); 489 TEMP_FAILURE_RETRY(close(write_out[0]));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 if (result == -1) { 556 if (result == -1) {
537 return false; 557 return false;
538 } 558 }
539 return true; 559 return true;
540 } 560 }
541 561
542 562
543 void Process::TerminateExitCodeHandler() { 563 void Process::TerminateExitCodeHandler() {
544 ExitCodeHandler::TerminateExitCodeThread(); 564 ExitCodeHandler::TerminateExitCodeThread();
545 } 565 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698