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

Abierto
#546 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
18/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
c

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
287
Forks
33
Merge medio
2 d 17 h
PR fusionados (30 d)
18

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de sysprog21/elfuse

Todos los issues de sysprog21/elfuse

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.