first_stage_console: Fix waitpid() as SA_NOCLDWAIT
From wait(2):
POSIX.1-2001 specifies that if [...] the SA_NOCLDWAIT flag is set for
SIGCHLD, then children that terminate do not become zombies and a call
to [...] waitpid() will block until all children have terminated, and
then fail with errno set to ECHILD.
As we call sigaction(SIGCHLD, { SIG_DFL, SA_NOCLDWAIT }), running
pid_t w = waitpid(pid, &status, 0);
LOG(INFO) << "..." << status << " " << w << " " << errno;
shows that the calls consistently return (status=0, w=-1, errno=ECHILD).
Therefore, clarify the parent code by prefering wait(2) over waitpid(2),
as SA_NOCLDWAIT makes the kernel ignore the passed PID, and stop logging
the irrelevant status, to avoid confusion when the logs say the exit
status was 0 but the child actually returned an error.
Test: run first_stage_console
Change-Id: I54df888e38b947e206e374ad28ebb044c70c6640
diff --git a/init/first_stage_console.cpp b/init/first_stage_console.cpp
index 0076764..cb8caab 100644
--- a/init/first_stage_console.cpp
+++ b/init/first_stage_console.cpp
@@ -69,9 +69,8 @@
LOG(INFO) << "Attempting to run /first_stage.sh...";
pid_t pid = fork();
if (pid != 0) {
- int status;
- waitpid(pid, &status, 0);
- LOG(INFO) << "/first_stage.sh exited with status " << status;
+ wait(NULL);
+ LOG(INFO) << "/first_stage.sh exited";
return;
}
const char* path = "/system/bin/sh";
@@ -94,9 +93,8 @@
sigaction(SIGCHLD, &chld_act, nullptr);
pid_t pid = fork();
if (pid != 0) {
- int status;
- waitpid(pid, &status, 0);
- LOG(ERROR) << "console shell exited with status " << status;
+ wait(NULL);
+ LOG(ERROR) << "console shell exited";
return;
}