Skip to content

Commit 9a01a22

Browse files
author
Anders Johnsen
committed
Revert "Clean up process spawning."
This reverts commits - bbdc57e - bd25e64 - 5cc8fca - ed72caa BUG= Review URL: https://codereview.chromium.org//1182423003.
1 parent bb45376 commit 9a01a22

File tree

5 files changed

+124
-80
lines changed

5 files changed

+124
-80
lines changed

runtime/bin/process_android.cc

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "platform/signal_blocker.h"
2525

2626

27+
extern char **environ;
28+
29+
2730
namespace dart {
2831
namespace bin {
2932

@@ -151,7 +154,7 @@ class ExitCodeHandler {
151154

152155
// Fork to wake up waitpid.
153156
if (TEMP_FAILURE_RETRY(fork()) == 0) {
154-
_exit(0);
157+
exit(0);
155158
}
156159

157160
monitor_->Notify();
@@ -411,7 +414,7 @@ class ProcessStarter {
411414
int bytes_read = FDUtils::ReadFromBlocking(read_in_[0], &msg, sizeof(msg));
412415
if (bytes_read != sizeof(msg)) {
413416
perror("Failed receiving notification message");
414-
_exit(1);
417+
exit(1);
415418
}
416419
if (mode_ == kNormal) {
417420
ExecProcess();
@@ -440,14 +443,12 @@ class ProcessStarter {
440443
}
441444

442445
if (program_environment_ != NULL) {
443-
VOID_TEMP_FAILURE_RETRY(
444-
execvpe(path_, const_cast<char* const*>(program_arguments_),
445-
program_environment_));
446-
} else {
447-
VOID_TEMP_FAILURE_RETRY(
448-
execvp(path_, const_cast<char* const*>(program_arguments_)));
446+
environ = program_environment_;
449447
}
450448

449+
VOID_TEMP_FAILURE_RETRY(
450+
execvp(path_, const_cast<char* const*>(program_arguments_)));
451+
451452
ReportChildError();
452453
}
453454

@@ -499,13 +500,13 @@ class ProcessStarter {
499500
execvp(path_, const_cast<char* const*>(program_arguments_)));
500501
ReportChildError();
501502
} else {
502-
// Exit the intermediate process.
503-
_exit(0);
503+
// Exit the intermeiate process.
504+
exit(0);
504505
}
505506
}
506507
} else {
507-
// Exit the intermediate process.
508-
_exit(0);
508+
// Exit the intermeiate process.
509+
exit(0);
509510
}
510511
}
511512

@@ -535,7 +536,7 @@ class ProcessStarter {
535536
FDUtils::ReadFromBlocking(
536537
exec_control_[0], &child_errno, sizeof(child_errno));
537538
if (bytes_read == sizeof(child_errno)) {
538-
SetOSErrorMessage(child_errno);
539+
ReadChildError();
539540
return child_errno;
540541
} else if (bytes_read == -1) {
541542
return errno;
@@ -559,7 +560,7 @@ class ProcessStarter {
559560
} else if (bytes_read == 2 * sizeof(int)) {
560561
*pid = result[0];
561562
child_errno = result[1];
562-
SetOSErrorMessage(child_errno);
563+
ReadChildError();
563564
return child_errno;
564565
} else if (bytes_read == -1) {
565566
return errno;
@@ -649,12 +650,21 @@ class ProcessStarter {
649650

650651

651652
void ReportChildError() {
652-
// In the case of failure in the child process write the errno to the exec
653-
// control pipe and exit.
653+
// In the case of failure in the child process write the errno and
654+
// the OS error message to the exec control pipe and exit.
654655
int child_errno = errno;
655-
FDUtils::WriteToBlocking(
656-
exec_control_[1], &child_errno, sizeof(child_errno));
657-
_exit(1);
656+
const int kBufferSize = 1024;
657+
char os_error_message[kBufferSize];
658+
strerror_r(errno, os_error_message, kBufferSize);
659+
int bytes_written =
660+
FDUtils::WriteToBlocking(
661+
exec_control_[1], &child_errno, sizeof(child_errno));
662+
if (bytes_written == sizeof(child_errno)) {
663+
FDUtils::WriteToBlocking(
664+
exec_control_[1], os_error_message, strlen(os_error_message) + 1);
665+
}
666+
VOID_TEMP_FAILURE_RETRY(close(exec_control_[1]));
667+
exit(1);
658668
}
659669

660670

@@ -668,16 +678,16 @@ class ProcessStarter {
668678
}
669679

670680

671-
void SetOSErrorMessage(int child_errno) {
681+
void ReadChildError() {
672682
const int kMaxMessageSize = 256;
673-
char* message = static_cast<char*>(calloc(kMaxMessageSize, 0));
674-
char* os_error_message = strerror_r(
675-
child_errno, message, kMaxMessageSize - 1);
676-
if (message == os_error_message) {
683+
char* message = static_cast<char*>(malloc(kMaxMessageSize));
684+
if (message != NULL) {
685+
FDUtils::ReadFromBlocking(exec_control_[0], message, kMaxMessageSize);
686+
message[kMaxMessageSize - 1] = '\0';
677687
*os_error_message_ = message;
678688
} else {
679-
free(message);
680-
*os_error_message_ = strdup(os_error_message);
689+
// Could not get error message. It will be NULL.
690+
ASSERT(*os_error_message_ == NULL);
681691
}
682692
}
683693

runtime/bin/process_linux.cc

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "bin/thread.h"
2424

2525

26+
extern char **environ;
27+
28+
2629
namespace dart {
2730
namespace bin {
2831

@@ -150,7 +153,7 @@ class ExitCodeHandler {
150153

151154
// Fork to wake up waitpid.
152155
if (TEMP_FAILURE_RETRY(fork()) == 0) {
153-
_exit(0);
156+
exit(0);
154157
}
155158

156159
monitor_->Notify();
@@ -410,7 +413,7 @@ class ProcessStarter {
410413
int bytes_read = FDUtils::ReadFromBlocking(read_in_[0], &msg, sizeof(msg));
411414
if (bytes_read != sizeof(msg)) {
412415
perror("Failed receiving notification message");
413-
_exit(1);
416+
exit(1);
414417
}
415418
if (mode_ == kNormal) {
416419
ExecProcess();
@@ -439,14 +442,12 @@ class ProcessStarter {
439442
}
440443

441444
if (program_environment_ != NULL) {
442-
VOID_TEMP_FAILURE_RETRY(
443-
execvpe(path_, const_cast<char* const*>(program_arguments_),
444-
program_environment_));
445-
} else {
446-
VOID_TEMP_FAILURE_RETRY(
447-
execvp(path_, const_cast<char* const*>(program_arguments_)));
445+
environ = program_environment_;
448446
}
449447

448+
VOID_TEMP_FAILURE_RETRY(
449+
execvp(path_, const_cast<char* const*>(program_arguments_)));
450+
450451
ReportChildError();
451452
}
452453

@@ -498,13 +499,13 @@ class ProcessStarter {
498499
execvp(path_, const_cast<char* const*>(program_arguments_)));
499500
ReportChildError();
500501
} else {
501-
// Exit the intermediate process.
502-
_exit(0);
502+
// Exit the intermeiate process.
503+
exit(0);
503504
}
504505
}
505506
} else {
506-
// Exit the intermediate process.
507-
_exit(0);
507+
// Exit the intermeiate process.
508+
exit(0);
508509
}
509510
}
510511

@@ -534,7 +535,7 @@ class ProcessStarter {
534535
FDUtils::ReadFromBlocking(
535536
exec_control_[0], &child_errno, sizeof(child_errno));
536537
if (bytes_read == sizeof(child_errno)) {
537-
SetOSErrorMessage(child_errno);
538+
ReadChildError();
538539
return child_errno;
539540
} else if (bytes_read == -1) {
540541
return errno;
@@ -558,7 +559,7 @@ class ProcessStarter {
558559
} else if (bytes_read == 2 * sizeof(int)) {
559560
*pid = result[0];
560561
child_errno = result[1];
561-
SetOSErrorMessage(child_errno);
562+
ReadChildError();
562563
return child_errno;
563564
} else if (bytes_read == -1) {
564565
return errno;
@@ -647,12 +648,21 @@ class ProcessStarter {
647648

648649

649650
void ReportChildError() {
650-
// In the case of failure in the child process write the errno to the exec
651-
// control pipe and exit.
651+
// In the case of failure in the child process write the errno and
652+
// the OS error message to the exec control pipe and exit.
652653
int child_errno = errno;
653-
FDUtils::WriteToBlocking(
654-
exec_control_[1], &child_errno, sizeof(child_errno));
655-
_exit(1);
654+
const int kBufferSize = 1024;
655+
char error_buf[kBufferSize];
656+
char* os_error_message = strerror_r(errno, error_buf, kBufferSize);
657+
int bytes_written =
658+
FDUtils::WriteToBlocking(
659+
exec_control_[1], &child_errno, sizeof(child_errno));
660+
if (bytes_written == sizeof(child_errno)) {
661+
FDUtils::WriteToBlocking(
662+
exec_control_[1], os_error_message, strlen(os_error_message) + 1);
663+
}
664+
VOID_TEMP_FAILURE_RETRY(close(exec_control_[1]));
665+
exit(1);
656666
}
657667

658668

@@ -666,16 +676,16 @@ class ProcessStarter {
666676
}
667677

668678

669-
void SetOSErrorMessage(int child_errno) {
679+
void ReadChildError() {
670680
const int kMaxMessageSize = 256;
671-
char* message = static_cast<char*>(calloc(kMaxMessageSize, 0));
672-
char* os_error_message = strerror_r(
673-
child_errno, message, kMaxMessageSize - 1);
674-
if (message == os_error_message) {
681+
char* message = static_cast<char*>(malloc(kMaxMessageSize));
682+
if (message != NULL) {
683+
FDUtils::ReadFromBlocking(exec_control_[0], message, kMaxMessageSize);
684+
message[kMaxMessageSize - 1] = '\0';
675685
*os_error_message_ = message;
676686
} else {
677-
free(message);
678-
*os_error_message_ = strdup(os_error_message);
687+
// Could not get error message. It will be NULL.
688+
ASSERT(*os_error_message_ == NULL);
679689
}
680690
}
681691

runtime/bin/process_macos.cc

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include "platform/signal_blocker.h"
2828

2929

30-
extern char** environ;
31-
3230

3331
namespace dart {
3432
namespace bin {
@@ -157,7 +155,7 @@ class ExitCodeHandler {
157155

158156
// Fork to wake up waitpid.
159157
if (TEMP_FAILURE_RETRY(fork()) == 0) {
160-
_exit(0);
158+
exit(0);
161159
}
162160

163161
monitor_->Notify();
@@ -425,7 +423,7 @@ class ProcessStarter {
425423
int bytes_read = FDUtils::ReadFromBlocking(read_in_[0], &msg, sizeof(msg));
426424
if (bytes_read != sizeof(msg)) {
427425
perror("Failed receiving notification message");
428-
_exit(1);
426+
exit(1);
429427
}
430428
if (mode_ == kNormal) {
431429
ExecProcess();
@@ -516,13 +514,13 @@ class ProcessStarter {
516514
execvp(path_, const_cast<char* const*>(program_arguments_)));
517515
ReportChildError();
518516
} else {
519-
// Exit the intermediate process.
520-
_exit(0);
517+
// Exit the intermeiate process.
518+
exit(0);
521519
}
522520
}
523521
} else {
524-
// Exit the intermediate process.
525-
_exit(0);
522+
// Exit the intermeiate process.
523+
exit(0);
526524
}
527525
}
528526

@@ -554,7 +552,7 @@ class ProcessStarter {
554552
FDUtils::ReadFromBlocking(
555553
exec_control_[0], &child_errno, sizeof(child_errno));
556554
if (bytes_read == sizeof(child_errno)) {
557-
SetOSErrorMessage(child_errno);
555+
ReadChildError();
558556
return child_errno;
559557
} else if (bytes_read == -1) {
560558
return errno;
@@ -578,7 +576,7 @@ class ProcessStarter {
578576
} else if (bytes_read == 2 * sizeof(int)) {
579577
*pid = result[0];
580578
child_errno = result[1];
581-
SetOSErrorMessage(child_errno);
579+
ReadChildError();
582580
return child_errno;
583581
} else if (bytes_read == -1) {
584582
return errno;
@@ -660,17 +658,29 @@ class ProcessStarter {
660658

661659

662660
void SetChildOsErrorMessage() {
663-
SetOSErrorMessage(errno);
661+
const int kBufferSize = 1024;
662+
char error_message[kBufferSize];
663+
strerror_r(errno, error_message, kBufferSize);
664+
*os_error_message_ = strdup(error_message);
664665
}
665666

666667

667668
void ReportChildError() {
668-
// In the case of failure in the child process write the errno to the exec
669-
// control pipe and exit.
669+
// In the case of failure in the child process write the errno and
670+
// the OS error message to the exec control pipe and exit.
670671
int child_errno = errno;
671-
FDUtils::WriteToBlocking(
672-
exec_control_[1], &child_errno, sizeof(child_errno));
673-
_exit(1);
672+
const int kBufferSize = 1024;
673+
char os_error_message[kBufferSize];
674+
strerror_r(errno, os_error_message, kBufferSize);
675+
int bytes_written =
676+
FDUtils::WriteToBlocking(
677+
exec_control_[1], &child_errno, sizeof(child_errno));
678+
if (bytes_written == sizeof(child_errno)) {
679+
FDUtils::WriteToBlocking(
680+
exec_control_[1], os_error_message, strlen(os_error_message) + 1);
681+
}
682+
VOID_TEMP_FAILURE_RETRY(close(exec_control_[1]));
683+
exit(1);
674684
}
675685

676686

@@ -684,11 +694,17 @@ class ProcessStarter {
684694
}
685695

686696

687-
void SetOSErrorMessage(int child_errno) {
688-
const int kBufferSize = 1024;
689-
char error_message[kBufferSize];
690-
strerror_r(child_errno, error_message, kBufferSize);
691-
*os_error_message_ = strdup(error_message);
697+
void ReadChildError() {
698+
const int kMaxMessageSize = 256;
699+
char* message = static_cast<char*>(malloc(kMaxMessageSize));
700+
if (message != NULL) {
701+
FDUtils::ReadFromBlocking(exec_control_[0], message, kMaxMessageSize);
702+
message[kMaxMessageSize - 1] = '\0';
703+
*os_error_message_ = message;
704+
} else {
705+
// Could not get error message. It will be NULL.
706+
ASSERT(*os_error_message_ == NULL);
707+
}
692708
}
693709

694710

0 commit comments

Comments
 (0)