Skip to content

sched/task: Give the child of fork() the signal actions of the parent. - #21095

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fork-signals-up
Oct 29, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fork-signals-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

The child of fork() and vfork() started with no signal actions. The action list of its new task group was empty, and so was the set of the signals that have the default action (only nxtask_start() fills it). So no signal had its default action in the child: a child that had not called exec ignored SIGTERM, SIGINT and even SIGKILL, and a handler of the parent did not run in the child. A shell forks for a subshell, for a pipeline and for a command in the background, so kill could not stop these.

POSIX says that the child has the signal actions of the parent, the signal mask of the calling thread and no pending signals. This PR adds nxsig_dupactions(), which copies the action list and the set of the signals with the default action to the new group, and calls it in nxtask_setup_fork(). The signal mask is already copied by nxtask_setup_scheduler().

Impact

All build modes and architectures with fork() or vfork() (CONFIG_ARCH_HAVE_FORK) and CONFIG_ENABLE_ALL_SIGNALS. A child that calls exec gets the actions that exec sets, as before. No interface or structure changes.

Testing

Tested on master aeab6fb and on this PR rebased on it.

Test program forksig.c on qemu-armv8a:knsh (QEMU with HVF, CONFIG_SCHED_CHILD_STATUS=y). A child that does not end sleeps 5 s and exits with 99.

Case master this PR
SIGTERM to a child that sleeps child runs on, exits with 99 after 4.85 s child ends at once
SIGINT to a child that sleeps child runs on, exits with 99 child ends at once
SIGKILL to a child that sleeps child runs on, exits with 99 child ends at once
a SIGUSR1 handler of the parent runs in the child no yes
SIGTERM that the parent ignores is ignored in the child yes yes
a signal that the parent blocks is blocked in the child yes yes
forkkill.c: waitpid() of a fork() child that SIGTERM ends hangs (EINTR after a 5 s alarm) returns the child
ostest, qemu-armv8a:knsh and qemu-armv8a:nsh status 0 status 0

forkkill.c also checks WTERMSIG() of the status: that part needs the exit status fix for a child ended by a signal, which is not in this PR.

The flat build has only vfork(), where the parent waits for the child; there ostest (with its vfork() test) is the check.

Build, each OK: qemu-armv8a:nsh (flat, arm64), qemu-armv8a:knsh (kernel, arm64), lm3s6965-ek:qemu-protected (protected, armv7-m), qemu-armv7a:knsh (kernel, armv7-a), rv-virt:pnsh64 (protected, rv64), rv-virt:knsh64 (kernel, rv64).

tools/checkpatch.sh -c -u -m -g passes.

The child of fork() and vfork() started with no signal actions: the
action list of its new task group was empty, and so was the set of the
signals that have the default action, which only nxtask_start() fills.
So no signal had its default action in the child.  A child that had not
called exec ignored SIGTERM, SIGINT and even SIGKILL, and a handler of
the parent did not run in the child.  A shell forks for a subshell, for
a pipeline and for a command in the background, so these could not be
stopped with kill.

POSIX says that the child has the signal actions of the parent, the
signal mask of the calling thread (nxtask_setup_scheduler() copies it
already) and no pending signals.  Add nxsig_dupactions(), which copies
the action list and the set of the signals with the default action to
the new group, and call it in nxtask_setup_fork().

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Oct 28, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +12 B (+0.0%, 127,784 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +12 B (+0.0%, 93,760 B / 3,342,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.nxsig_action -208 B, .text.nxtask_setup_fork +12 B (+0.1%, 353,696 B)

qemu-intel64

  • Code: .text +256 B (+0.0%, 8,686,983 B)

rx65n-rsk2mb

  • ROM: .text +128 B (+0.1%, 89,912 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +416 B (+0.1%, 382,592 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit 93c7e87 into apache:master Oct 29, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants