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
This commit is contained in:
Pierre-Clément Tosi 2024-02-12 15:39:52 +00:00
parent 3b79ada7ef
commit b1d92c6508

View file

@ -69,9 +69,8 @@ static void RunScript() {
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 @@ void StartConsole(const std::string& cmdline) {
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;
}