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

未关闭
#546 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
18/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
c

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C
星标
287
派生
33
平均合并
2 天 17 小时
30 天内合并 PR
18

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sysprog21/elfuse 的其他 Issue

查看 sysprog21/elfuse 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。