Leaving SIG_IGN for SIGCHLD reaps an earlier zombie where Linux keeps it

Aperta
#546 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
18/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c

Direzione di ricerca

Start with signal_rt_sigaction in src/syscall/signal.c and proc_autoreap_exited_children in src/syscall/proc.c, where the reap runs for every exited child. Run the reproduction in the issue, then check the ign case in tests/test-sigchld-after-wait.c, which currently accepts ECHILD. Done means waitpid returns the pre-existing zombie after SIG_IGN is set and then restored. That needs a per-child record of the disposition at exit, which is a design choice, so confirm the approach with maintainers first.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What happens

When a new SIGCHLD action replaces one that reaps children by itself (SIG_IGN or SA_NOCLDWAIT, see sigaction_autoreaps_sigchld), signal_rt_sigaction in src/syscall/signal.c calls proc_autoreap_exited_children in src/syscall/proc.c. That reaps every child that has exited, including one that was already a zombie before SIG_IGN was installed, so a later waitpid for it returns ECHILD. Linux reaps automatically only a child whose exit finds SIG_IGN or SA_NOCLDWAIT in force (do_notify_parent in kernel/signal.c); a child that is already a zombie stays one, and waitpid returns it.

Evidence

The probe below, run with elfuse-probe-diff:

build waitpid after SIG_IGN and the handler again
elfuse, upstream/main c57282f ECHILD
elfuse, #538 at 850d08a ECHILD
Linux 6.18.63 (QEMU) the child, exit status 7

Run for SIG_IGN only; SA_NOCLDWAIT takes the same path by reading, not run.

Cause

Checked by reading: the reap in signal_rt_sigaction runs whenever the old action reaps automatically, and proc_autoreap_exited_children takes every child in a terminal lifecycle state. It cannot tell a child that exited under SIG_IGN from one that was a zombie before it, because no record says which disposition the exit found (the mark #538 adds says only that the SIGCHLD was discarded, which SIG_DFL also does). The comment above the reap says it closes a race where the guest restores SIG_DFL after it sees the child's teardown but before the exit's record is drained.

Reproduce

/* A child exits under a SIGCHLD handler; SIG_IGN, then the handler again,
 * while the child is still a zombie. Linux keeps the zombie for waitpid. */
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

static void on_chld(int sig) { (void) sig; }

int main(void)
{
    struct sigaction catch, ign;
    memset(&catch, 0, sizeof(catch));
    sigemptyset(&catch.sa_mask);
    catch.sa_handler = on_chld;
    ign = catch;
    ign.sa_handler = SIG_IGN;
    sigaction(SIGCHLD, &catch, NULL);
    pid_t pid = fork();
    if (pid == 0)
        _exit(7);
    siginfo_t si;
    memset(&si, 0, sizeof(si));
    waitid(P_PID, pid, &si, WEXITED | WNOWAIT); /* now a zombie */
    sigaction(SIGCHLD, &ign, NULL);
    sigaction(SIGCHLD, &catch, NULL);
    int status = 0;
    pid_t r = waitpid(pid, &status, 0);
    if (r == pid)
        printf("waitpid: the child, exit status %d\n", WEXITSTATUS(status));
    else
        printf("waitpid: %s\n", errno == ECHILD ? "ECHILD" : strerror(errno));
    return 0;
}

Possible fix

Reap only the children whose exit found the auto-reap disposition. Since #538 the exiting child reads its parent's SIGCHLD flag file at the exit; a second value there ("the parent reaps automatically") would let the child's record say so, and the parent would reap only those children.

Origin

Follow-up from #538 (https://github.com/sysprog21/elfuse/pull/538), whose ign case in tests/test-sigchld-after-wait.c accepts ECHILD from its last waitpid for this reason.

Lingua principale
C
Stelle
287
Fork
33
Merge medio
2g 17h
PR unite (30g)
18

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sysprog21/elfuse

Tutte le issue di sysprog21/elfuse

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.