casaroli opened a new pull request, #21929:
URL: https://github.com/apache/nuttx/pull/21929

   depends-on: apache/nuttx/pull/21898
   
   ## Summary
   
   A child that stops on `SIGSTOP`, `SIGTSTP`, `SIGTTIN` or `SIGTTOU`, or that 
continues on `SIGCONT`, is not reported to its parent today: the parent gets no 
`SIGCHLD`, and `waitpid()` with `WUNTRACED` or `WCONTINUED` waits for the exit. 
`WIFSTOPPED()`, `WSTOPSIG()` and `WIFCONTINUED()` are always 0. `waitpid()` 
with a pid of 0 or less than -1 fails with `ECHILD`, and `waitid()` with 
`P_PGID` fails with `ENOSYS`. A shell with job control waits with `WUNTRACED` 
for its foreground job, so after Ctrl-Z it waits for ever.
   
   This PR builds on #21904 (process groups and sessions): its branch contains 
the 4 commits of #21904, so it is a draft and merges after #21904. #21904 needs 
#21898 first (the `depends-on` line above).
   
   Changes (no Kconfig option, as the dev@ discussion planned for this part):
   
   - With `CONFIG_SCHED_HAVE_PARENT` and `CONFIG_SIG_SIGSTOP_ACTION`, the stop 
action sends `SIGCHLD` with `CLD_STOPPED` (and the stop signal in `si_status`) 
to the parent, and `SIGCONT` to a stopped process sends `CLD_CONTINUED`. With 
`SA_NOCLDSTOP`, no `SIGCHLD` is sent, but a thread of the parent that waits in 
`waitpid()` or `waitid()` wakes up.
   - With `CONFIG_SIG_SIGSTOP_ACTION`, `SIGKILL` also ends a stopped process 
(it stayed stopped).
   - With `CONFIG_SCHED_CHILD_STATUS`, the child status keeps the event until a 
wait reports it once; `WNOWAIT` keeps it.
   - `waitpid()` is now `waitid()` with a status in place of the `siginfo_t`: 
one wait loop instead of two (most of the diff). The loop keeps master's rules 
for a parent without the child status (`SA_NOCLDWAIT`, `SIG_IGN`): it waits 
until the children exit, then gives `ECHILD`. A stop or a continue is still 
reported.
   - The status of a stopped child is `(sig << 8) | 0x7f`, of a continued child 
`0xffff`; `WIFSTOPPED()`, `WSTOPSIG()` and `WIFCONTINUED()` decode them. The 
other macros and the option values do not change.
   - `waitpid(0)`, `waitpid(-pgid)` and `waitid(P_PGID)` select the children of 
a process group: the groups of `setpgid()` with `CONFIG_SCHED_PROCESS_GROUPS`, 
else a group is one process. An exited child keeps the group it had at its exit.
   - `waitid()` gives `EINVAL` for a wrong idtype or option, also without 
`CONFIG_DEBUG_FEATURES`.
   
   Commits: (1) the scheduler change; (2) Documentation (`03_task_control.rst`: 
`waitpid()`, `waitid()`; `07_signals.rst`: the stop and continue events, 
`SA_NOCLDSTOP`).
   
   ## Impact
   
   Every build with `CONFIG_SCHED_WAITPID` gets the new behaviour; it is POSIX. 
A program that compiles on master still compiles: no name is removed, no option 
value changes, and the three macros keep their names.
   
   Behaviour that a program can see, also in a flat build:
   
   - `waitpid()` with `WUNTRACED` returns when the child stops (with 
`CONFIG_SCHED_HAVE_PARENT`). In the tree, NSH (`nsh_builtin.c`, 
`nsh_fileapps.c`) and `testing/cmocka` wait with `WUNTRACED`. NSH now gives the 
prompt back when a foreground command stops, as its comment says it should; it 
waited for ever before (tested on the console, below). `$?` of the stopped 
command is 1. cmocka goes to the next test only if a test stops itself.
   - Without `CONFIG_SCHED_HAVE_PARENT`, the status of a stopped child was 0 
and is now `(sig << 8) | 0x7f`: NSH gives `$?` 1, not 0, for a stopped command.
   - A parent with a `SIGCHLD` handler also gets `SIGCHLD` when a child stops 
or continues, unless it sets `SA_NOCLDSTOP`. examples/nanoxterm exits on any 
`SIGCHLD`, so it also exits when its child stops. nxinit and telnetd are not 
affected (`WNOHANG` without `WUNTRACED`; `SIG_IGN`).
   - `waitpid()` with a pid of 0 or less than -1 waits for a process group 
instead of failing with `ECHILD`. No caller in the tree passes such a pid.
   - `waitid()` checks the options in every build. No caller in the tree passes 
a wrong option.
   
   Size: an image without `CONFIG_SCHED_HAVE_PARENT` and `CONFIG_SIG_DEFAULT` 
(most small boards) does not change. With them, the wait loop and the stop 
events add about 600 bytes of code (table below). A task group gets one `bool`; 
a child status gets the stop signal in its padding and, with 
`CONFIG_SCHED_PROCESS_GROUPS`, a `pid_t`.
   
   The stop and continue events exist only with `CONFIG_SIG_DEFAULT` (default 
n): 148 defconfigs set it, 63 of them with `CONFIG_SCHED_HAVE_PARENT`.
   
   Not in this PR (as in the dev@ plan): `SA_RESTART` (a `read()` interrupted 
by a stop still fails with `EINTR`), orphaned process groups, a blocked 
`SIGCONT`, `WIFSIGNALED()`.
   
   ## Testing
   
   New test program `testing/sched/waitstop` (apache/nuttx-apps#TBD), 31 
checks. The children are the program itself, started with `posix_spawn()`, so 
it runs in a flat build. Each wait fails after 5 s, so a missing report is a 
FAIL, not a hang.
   
   | Configuration | #21904 (the wait code of master) | this PR |
   |---|---|---|
   | qemu-armv8a:knsh (kernel, arm64, QEMU with HVF), 
`CONFIG_SCHED_PROCESS_GROUPS=y` | 24 FAIL (28 checks printed; 3 cases stop at 
their first failure) | 31 PASS |
   | qemu-armv8a:nsh (flat, Make build) + `SIG_DEFAULT`, `SCHED_PROCESS_GROUPS` 
| 24 FAIL | 31 PASS |
   | qemu-armv8a:nsh (flat) + `SIG_DEFAULT`, no process groups | - | 29 PASS, 1 
SKIP (`waitpid(0)` needs process groups) |
   
   NSH on the console: a foreground command that stops itself (a file app on 
knsh, a builtin on the flat nsh). With #21904 only, NSH waits for ever. With 
this PR, NSH gives the prompt back and `ps` shows the command `Stopped`.
   
   ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh (the 
defconfigs), and in the flat runs above.
   
   Size (`text / data / bss`, GCC), #21904 (= master with its option off) and 
this PR:
   
   | Configuration | #21904 | this PR | delta |
   |---|---|---|---|
   | nucleo-f072rb:nsh (flat, Cortex-M0) | 89631 / 332 / 1416 | 89631 / 332 / 
1416 | 0 |
   | stm32f4discovery:nsh (flat, Cortex-M4) | 94068 / 904 / 4904 | 94068 / 904 
/ 4904 | 0 |
   | qemu-armv8a:knsh (kernel, arm64) | 210804 / 19456 / 60088 | 211188 / 19456 
/ 60088 | +384 text |
   | nucleo-f072rb:nsh + `SCHED_HAVE_PARENT`, `SCHED_CHILD_STATUS`, 
`SIG_DEFAULT` | 94987 / 332 / 1752 | 95643 / 332 / 1752 | +656 text |
   | stm32f4discovery:nsh + the same | 96220 / 904 / 5464 | 96820 / 904 / 5464 
| +600 text |
   
   In the CI image (its GCC and clang, `-Werror`), the boards with little flash 
left: lm3s6965-ek:qemu-protected +16 bytes (kernel flash 99.60%, it fits); 
b-g431b-esc1:nsh, nucleo-g431kb:nsh, nucleo-g431rb:nsh, nucleo-l432kc:wgen, 
nucleo-l432kc:nsh, weact-stm32h750:sdcard, ea3131:pgnsh and nucleo-f072rb:nsh: 
no change. No defconfig changes.
   
   Build without new warnings: rv-virt:knsh64 (kernel, rv64), rv-virt:pnsh64 
(protected, rv64), and rv-virt:pnsh64 with `SIG_DEFAULT`, 
`SCHED_PROCESS_GROUPS` and the test.
   
   Not run: a protected build at run time, SMP, other architectures at run 
time. The change has no architecture code.
   
   `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]

Reply via email to