Skip to content

drivers/serial, sched/signal: Discard Ctrl-C and Ctrl-Z; fix kill() to a process group. - #21144

Merged
xiaoxiang781216 merged 4 commits into
apache:masterfrom
casaroli:g9-console-up
Nov 2, 2026
Merged

xiaoxiang781216 merged 4 commits into
apache:masterfrom
casaroli:g9-console-up

Conversation

@casaroli

@casaroli casaroli commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes two parts of job control on a console: the Ctrl-C and Ctrl-Z characters that send a signal, and kill() to a process group.

  • drivers/serial: Discard the INTR and SUSP characters that send a signal. When ISIG is set and the terminal knows its foreground process, INTR (Ctrl-C) and SUSP (Ctrl-Z) send SIGINT and SIGTSTP. The serial driver also put the character into the receive buffer, so the next read() gave it to the program: after Ctrl-C, a shell read ^C in front of the next command line. POSIX (General Terminal Interface, 11.1.9 "Special Characters") says that the terminal discards these characters when ISIG is set; the pty and telnet drivers already do. Now uart_check_special() removes every special character that it acts on: INTR and SUSP when they send a signal, and the CONFIG_TTY_LAUNCH and CONFIG_TTY_FORCE_PANIC characters. Its size argument is now in and out (the number of characters that are left). All receive paths use it: receive(), recvbuf() and the DMA path (uart_recvchars_done(): sim with CONFIG_SIM_UART_DMA, virtio-serial, CDC/ACM, 16550 RX DMA, rpmsg), where the characters that are left in the second region move to follow the first region. A character that sends no signal (no foreground process, or ISIG not set) stays in the data. uart_check_special() now handles every character of a batch (before: only up to the first special character) and returns the signal of the first INTR or SUSP; uart_recvchars() keeps the first signal of a batch: the check of each later character set the signal back to 0, so Ctrl-C followed by more characters in the same interrupt sent no signal.
  • Documentation: Say that the serial driver discards Ctrl-C and Ctrl-Z. One sentence in the job control paragraph of serial.rst.
  • sched/signal: Send kill() to a process group as one process. kill() failed with ENOSYS for a negative pid (a process group), and pid 0 (the process group of the caller) sent the signal to the idle task. In NuttX a process group is one process: getpgid() and setpgid() use the process ID as the group ID, and killpg() already sends the signal to that process. Now pid 0 is the caller and -pid is the process pid; pid -1 (all processes) still fails with ENOSYS. A shell with job control sends kill %1 to the process group of the job. clock_getcpuclockid(), setpgid(), waitpid() and waitid() used kill(pid, 0) to find a process; they now reject a negative pid themselves, so their result does not change (setpgid() gives ESRCH, before: ENOSYS).
  • Documentation: Say that kill() sends to a process group of one process. The kill() reference and serial.rst said that a signal to a process group is not supported.

Impact

Behaviour change, every build mode (flat too):

  • Serial consoles with CONFIG_TTY_SIGINT or CONFIG_TTY_SIGTSTP and a foreground process: the program no longer reads the Ctrl-C or Ctrl-Z character that sent the signal. With ISIG set, the program also no longer reads the CONFIG_TTY_LAUNCH and CONFIG_TTY_FORCE_PANIC characters. Without a foreground process nothing changes: NSH sets the foreground process only while a foreground command runs (TIOCSCTTY, then TIOCNOTTY), so at the NSH prompt the readline editor still gets Ctrl-C.
  • uart_check_special() (include/nuttx/serial/serial.h) now takes the size as a pointer (in and out) and changes the buffer; only drivers/serial calls it.
  • kill(0, sig) now signals the caller (before: the idle task). kill(-pid, sig) now signals process pid (before: ENOSYS); a group that does not exist gives ESRCH (before: ENOSYS). kill(-1, sig) still fails with ENOSYS.

In-tree users, nuttx and nuttx-apps master: examples/nanoxterm calls kill(-pid, SIGHUP); it now reaches the child. NSH's kill command accepts only a pid >= 0, so kill -9 0 now signals NSH itself (before: the idle task). All other kill() and nxsig_kill() callers pass a pid > 0 (a child, a daemon, the foreground PID of a terminal). The only ^C reader in nuttx-apps, system/readline (Ctrl-C cancels a Ctrl-R search), runs at the NSH prompt.

Testing

Tested on master 3122150 and on this PR rebased on it (again after the review changes, on master c7bde63), with QEMU (HVF), CONFIG_TTY_SIGINT, CONFIG_TTY_SIGTSTP and CONFIG_SIG_DEFAULT: qemu-armv8a:knsh (kernel build, programs on v9fs) and qemu-armv8a:nsh (flat build, the programs as NSH built-ins on the serial console).

isigtest.c catches SIGINT and reads a line from the console; the test types Ctrl-C, then abc and Enter. The same result in the kernel and the flat build:

Case master this PR
one SIGINT 1: PASS 1: PASS
the line that read() gives 03 61 62 63 0a (the Ctrl-C first): FAIL 61 62 63 0a: PASS

killpgrp.c; the child is the program itself, started with posix_spawn(). The same result in the kernel and the flat build:

Case master this PR
getpgid(child) == child PASS PASS
kill(-child, SIGTERM) -1, ENOSYS: FAIL 0: PASS
the child ends (waitpid()) PASS (the test kills it) PASS
kill(0, SIGUSR1) runs the handler of the caller no (the idle task got it): FAIL PASS
kill(0, 0) 0: PASS 0: PASS
kill(-1, 0) -1, ENOSYS: PASS -1, ENOSYS: PASS
kill(-30000, 0) (no such group) -1, ENOSYS: FAIL -1, ESRCH: PASS
clock_getcpuclockid(-2) and (-getpid()) -1: PASS -1: PASS
setpgid(-getpid(), 0) -1, ENOSYS: FAIL -1, ESRCH: PASS
waitpid(-getpid(), WNOHANG) with SA_NOCLDWAIT -1, ECHILD: PASS -1, ECHILD: PASS
waitid(P_PID, -getpid(), WNOHANG) with SA_NOCLDWAIT -1, ECHILD: PASS -1, ECHILD: PASS

The LTP test ltp_interfaces_clock_getcpuclockid_6_1 (its source run on qemu-armv8a:knsh and :nsh): PASS.

ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh, on master and with this PR.

A host test builds uart_check_special() and the DMA-path function from the source with stubs: INTR/SUSP removed and the first signal kept, the launch and panic characters removed (panic at the repeat count), no change without a foreground PID, with _POSIX_VDISABLE or without ISIG, and DMA data in one region and in two regions (characters removed in either, all, or none): 21/21 PASS.

Builds, each OK and without new warnings: lm3s6965-ek:nsh (flat, Cortex-M3) with CONFIG_SERIAL_TERMIOS, CONFIG_TTY_SIGINT and CONFIG_TTY_SIGTSTP; lm3s6965-ek:qemu-protected (protected, Cortex-M3); rv-virt:pnsh64 (protected, RISC-V) and rv-virt:knsh64 (kernel, RISC-V) with CONFIG_SERIAL_TERMIOS, CONFIG_TTY_SIGINT and CONFIG_TTY_SIGTSTP; qemu-armv8a:nsh with each of CONFIG_TTY_SIGINT, CONFIG_TTY_SIGTSTP, CONFIG_TTY_FORCE_PANIC, CONFIG_TTY_LAUNCH alone, none of them, and all of them with the DMA receive path (CONFIG_DRIVERS_VIRTIO_SERIAL); sim:nsh with CONFIG_SIM_UART_DMA (macOS host). lm3s6965-ek:qemu-protected overflows its kernel flash with the local GCC 13.2 on master too (288 bytes; this PR: 320).

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

@github-actions github-actions Bot added Area: Drivers Drivers issues Size: M The size of the change in this PR is medium labels Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +36 B (+0.0%, 128,364 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +36 B (+0.0%, 94,244 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +36 B (+0.0%, 85,060 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +8 B (+0.0%, 67,368 B / 131,072 B, total: 51% used)

qemu-armv8a

  • Code: .text.nxsched_waitpid -4 B, .text.nxsig_kill +44 B, .text.uart_recvchars -4 B, .text.waitid -4 B (+0.0%, 354,688 B)

qemu-intel64

  • Code: .text +59 B (+0.0%, 8,688,379 B)

rx65n-rsk2mb

  • ROM: .text +48 B (+0.1%, 90,456 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +224 B (+0.1%, 385,696 B)

stm32-nucleo-f103rb

  • flash: .text +4 B (+0.0%, 34,272 B / 131,072 B, total: 26% used)

No memory changes detected for:


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

Comment thread drivers/serial/serial_io.c Resolved
Comment thread drivers/serial/serial_io.c Resolved
Comment thread drivers/serial/serial_dma.c Resolved
Comment thread drivers/serial/serial.c Resolved
Comment thread drivers/serial/serial.c Resolved
Comment thread drivers/serial/serial.c Resolved
Comment thread drivers/serial/serial_dma.c Resolved
@casaroli
casaroli marked this pull request as draft November 1, 2026 11:28
@casaroli
casaroli marked this pull request as ready for review November 1, 2026 11:28
Comment thread drivers/serial/serial_dma.c Resolved
When ISIG is set and the terminal knows its foreground process, the
INTR (Ctrl-C) and SUSP (Ctrl-Z) characters send SIGINT and SIGTSTP.
The driver also put the character into the receive buffer, so the next
read() gave it to the program: after Ctrl-C a shell got "^C" in front
of the next command line.  POSIX says that the terminal discards such a
character when it processes it.

Now uart_check_special() removes every special character that it acts
on: INTR and SUSP when they send a signal, and the LAUNCH and
FORCE_PANIC characters.  Its size argument is now in and out: it
returns the number of characters that are left.  All receive paths use
it: receive(), recvbuf() and the DMA path, where the characters that
are left in the second region move to follow the first region.  A
character that sends no signal (no foreground process, or ISIG not
set) stays as before.

uart_check_special() now handles every character of the batch, not
only the ones before the first special character, and returns the
signal of the first INTR or SUSP character.  uart_recvchars() keeps the
first signal of a batch: the check of each later character set it back
to 0, so a Ctrl-C with more characters in the same interrupt sent no
signal.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
The serial driver no longer passes on the INTR and SUSP characters that
send SIGINT and SIGTSTP.  Say it in the job control paragraph.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
kill() failed with ENOSYS for a negative pid (a process group), and for
pid 0 (the process group of the caller) it sent the signal to the idle
task.  In NuttX a process group is one process: getpgid() and
setpgid() use the process ID as the group ID, and killpg() already
sends the signal to that process.  A shell with job control sends
"kill %1" to the process group of the job and failed.

Now pid 0 is the caller and -pid is the process pid, as killpg() does.
pid -1 (all processes) still fails with ENOSYS.

Some functions used kill(pid, 0) to find a process, and a negative pid
failed there.  So that it still fails: clock_getcpuclockid() and
setpgid() (ESRCH) reject a negative pid, and waitpid() and waitid()
look for the child with nxsched_get_tcb(), which is what kill(pid, 0)
did.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
The kill() reference and the serial driver page said that kill() to a
process group is not supported.  It now sends the signal to the one
process of the group.  pid -1 (all processes) still gives ENOSYS.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@casaroli
casaroli marked this pull request as draft November 1, 2026 18:58
@casaroli
casaroli marked this pull request as ready for review November 1, 2026 18:58
@xiaoxiang781216
xiaoxiang781216 merged commit 207531f into apache:master Nov 2, 2026
87 of 91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants