casaroli opened a new pull request, #21904:
URL: https://github.com/apache/nuttx/pull/21904
depends-on: apache/nuttx/pull/21898
## Summary
NuttX has no POSIX process groups and sessions: a process group and a
session are one process. `setpgid()` fails with `EPERM` for each process group
ID that is not the process ID, `setsid()` does not start a session, and
`kill(-pgrp)` reaches only the process `pgrp`. A shell with job control puts
the processes of a pipeline into the process group of the first process, so it
prints an error for each pipeline, and Ctrl-C reaches only the first process.
This PR adds `CONFIG_SCHED_PROCESS_GROUPS` (default n, depends on
`SCHED_HAVE_PARENT`). With it, each task group has a process group ID and a
session ID:
- A task that a user task starts (`fork()`, `posix_spawn()`,
`task_create()`) gets the process group and the session of that task, and
`exec()` keeps them. A task that the kernel starts (for example the init task)
is the leader of a new session.
- `setpgid()`, `getpgid()`, `setsid()` and `getsid()` move from the C
library to the kernel, with the POSIX errors (`ESRCH`, `EACCES` after `exec()`,
`EPERM`, `EINVAL`). In the protected and kernel builds they are system calls at
the end of `syscall_lookup.h`, so no other system call number changes.
- `kill()` with a pid of 0 or less than -1, and `killpg()`, send the signal
to each process of the process group. `getpgrp()` calls `getpgid(0)`.
- `posix_spawn()` supports `POSIX_SPAWN_SETPGROUP`
(`posix_spawnattr_setpgroup()` and `posix_spawnattr_getpgroup()` gave `ENOSYS`)
and `POSIX_SPAWN_SETSID`. A program without `fork()` needs these flags.
- The INTR and SUSP characters and a window size change of a serial
terminal, a pseudo-terminal and a Telnet session send `SIGINT`, `SIGTSTP` and
`SIGWINCH` to each process of the foreground process group. An ID that no
process group has is a process ID (the `TIOCSCTTY` of NSH saves one), and that
process gets the signal as before.
A signal to a process group goes through the task list with
`nxsched_foreach()` in a critical section. A list per process group would not
need the walk, but it adds two pointers to each task group.
On a kernel build, a spawn attribute that fails (for example
`POSIX_SPAWN_SETPGROUP` with a process group of another session) reaches a bug
in the error path of `exec_module()`. #21898 fixes it, so this PR is a draft
until #21898 merges.
The design was discussed on dev@ ("[DISCUSS] POSIX job control: real process
groups, sessions and controlling terminals", 2026-11-05). This PR is part 1 of
that plan. The only reply was a review of the impact on small systems: with the
option off, the code is the same as master (see the size table below).
Commits: (1) process groups, sessions and the system calls; (2) the spawn
flags; (3) the terminal signals; (4) Documentation.
## Impact
With `CONFIG_SCHED_PROCESS_GROUPS` off (the default), nothing changes: no
new function, no new system call, the same code and the same image size (table
below). It is not a breaking change: no API of the flat build changes when the
option is off.
With the option on: each task group gets two IDs and a flag. `kill(0)`,
`kill(-pgid)` and `killpg()` reach every process of the group, which is POSIX.
A shell that calls `setpgid()` for a pipeline no longer gets `EPERM`.
Not in this PR (later parts of the plan): `_POSIX_JOB_CONTROL` and
`sysconf(_SC_JOB_CONTROL)` (a program that sees them expects stopped children
and the terminal signals of a background group), stopped and continued children
for `waitpid()`, `SA_RESTART`, controlling terminals (`/dev/tty`, `SIGTTIN`,
`SIGTTOU`, `SIGHUP`), orphaned process groups, `kill(-1)`, and `waitpid()` of a
process group (pid 0 or less than -1).
Documentation: `Documentation/reference/user/` (spawn flags, `kill()`,
`waitpid()`), `Documentation/os/drivers/character/serial.rst`, the POSIX tables
in `Documentation/standards/posix.rst`, and the signal guide are updated in the
fourth commit.
## Testing
Size (`text / data / bss`), master 4606644096682 and this PR with the option
off (the defconfig as it is):
| Configuration | master | this PR, option off |
|---|---|---|
| nucleo-f072rb:nsh (flat, Cortex-M0) | 89631 / 332 / 1416 | 89631 / 332 /
1416 |
| stm32f4discovery:nsh (flat, Cortex-M4) | 94068 / 904 / 4904 | 94068 / 904
/ 4904 |
| lm3s6965-ek:qemu-protected, kernel (protected, Cortex-M3) | 129084 / 1460
/ 14376 | 129084 / 1460 / 14376 |
| lm3s6965-ek:qemu-protected, user | 120272 / 692 / 1868 | 120272 / 692 /
1868 |
| qemu-armv8a:knsh (kernel, arm64) | 210804 / 19456 / 60088 | 210804 / 19456
/ 60088 |
sim:nsh does not build on macOS on master either (`sim_hostuart.c`:
`TIOCGPTN`), so it is not in the table.
With the option on (`SCHED_HAVE_PARENT` on in both columns):
| Configuration | option off | option on | delta |
|---|---|---|---|
| nucleo-f072rb:nsh | 93627 / 332 / 1496 | 94347 / 332 / 1504 | +720 text,
+8 bss |
| stm32f4discovery:nsh | 94468 / 904 / 5176 | 94948 / 904 / 5192 | +480
text, +16 bss |
| qemu-armv8a:knsh | 210804 / 19456 / 60088 | 212188 / 19456 / 60104 | +1384
text, +16 bss |
| rv-virt:knsh64 (kernel, rv64) | 150617 / 1725 / 34592 | 151697 / 1725 /
34624 | +1080 text, +32 bss |
| rv-virt:pnsh64 (protected, rv64), kernel | 149272 / 1836 / 9872 | 150328 /
1836 / 9888 | +1056 text, +16 bss |
Each task group also gets two `pid_t` and a `bool` on the heap.
Test program `testing/sched/pgrptest` (apache/nuttx-apps#TBD), 35 checks.
The children are the program itself, started with `posix_spawn()`, so most
checks run without `fork()`:
| Configuration | master | this PR, option on |
|---|---|---|
| qemu-armv8a:knsh (kernel, QEMU with HVF), with #21898 | 10 of 35 FAIL (for
example `setpgid()` of the parent succeeds, `setsid()` of a group leader
succeeds, `posix_spawnattr_setpgroup()` gives `ENOSYS`) | 35 PASS |
| qemu-armv8a:nsh (flat) | the test cannot be enabled | 32 PASS, 1 SKIP (no
`fork()`) |
| qemu-armv8a:citest (flat, CMake build, QEMU TCG) | the test cannot be
enabled | 31 PASS, 2 SKIP (no `fork()`, no pseudo-terminal) |
With the option off, the knsh results are the same failures as on master.
ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh with the
option off and with it on.
Build with the option on, each without new warnings: rv-virt:knsh64 (kernel,
rv64) and rv-virt:pnsh64 (protected, rv64, with the test).
`tools/checkpatch.sh -c -u -m -g` passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]