casaroli opened a new pull request, #22524:
URL: https://github.com/apache/nuttx/pull/22524
## Summary
`kill(pid, 0)` and `/proc` do not follow POSIX and Linux for a process that
ended:
- A child that `waitpid()` reaped is still found. The exit wakes the parent
before `group_leave()` and before the release of the TCB, and the TCB stays in
the table of threads until it is released. When the release blocks (a close in
`group_leave()`, for example) or runs on another CPU, `waitpid()` returns
first. Then `kill(pid, 0)` gives 0, `readdir()` of `/proc` lists the pid, and
`/proc/<pid>` can be opened and `stat()`ed.
- A zombie (a child that ended and that its parent did not wait for yet) is
not found when its TCB is gone: `kill(pid, 0)` gives `ESRCH`. POSIX and Linux:
a zombie exists until its parent waits for it.
- `readdir()` of `/proc` stops at a pid of the `opendir()` snapshot whose
task ended since: the later tasks and the static entries (`self`, `meminfo`,
...) are not listed.
This PR:
1. `fs/procfs`: `readdir()` skips the pids of the snapshot that no longer
refer to a task and goes on with the next entry, as Linux does.
2. `sched/signal`: the new `nxsig_kill_target()` decides if a pid exists for
`kill()`. A thread that is not in its exit exists. A thread in its exit exists
until it leaves its group (`group_leave()`), because the exit can still block
there, as before. But a process does not exist once a wait of its parent has
reported its exit: `waitid()`, `waitpid()` and `wait()` mark its TCB with the
new `TCB_FLAG_REAPED` (`nxsched_reaped()`) when the TCB is still in the table,
with and without `CONFIG_SCHED_CHILD_STATUS` and `CONFIG_SCHED_HAVE_PARENT`
(not with `WNOWAIT`). A zombie exists until its parent waits for it, also when
its TCB is gone. A signal to a zombie or to a thread in its exit succeeds and
has no effect; a reaped child gives `ESRCH`.
3. `fs/procfs`: `readdir()`, `open()`, `opendir()` and `stat()` use the same
test, so `kill(pid, 0)` and `/proc` agree. A task in its exit that no parent
has waited for stays visible, as before. A zombie whose TCB is gone stays out
of the listing, as before (procfs has no data to show for it).
## Impact
Every build mode. Visible changes, all toward POSIX and Linux: `kill()`
succeeds for a zombie (it gave `ESRCH` when the TCB was gone); `kill()`,
`/proc/<pid>` and the listing give `ESRCH`/`ENOENT` for a reaped child at once;
a walk of `/proc` lists every task; `kill(pid, SIGTERM)` to a task in its exit
gives 0 with no effect (it gave `ESRCH`, while `kill(pid, 0)` gave 0). A task
in its exit that no wait has reported (the parent ended, or it ignores
`SIGCHLD`) stays visible until it leaves its group, as before. In-tree users of
`kill(pid, 0)` (thttpd CGI supervision, ostest `suspend` and `robust`, nxpkg,
the mm leak dump): ostest suppresses child status (`SA_NOCLDWAIT`), `robust`
waits for a thread, and no configuration enables thttpd with
`CONFIG_SCHED_CHILD_STATUS`. Cost: one bit in `shared_flags`, set by a wait
that reports an exit; `nxsig_kill_target()` returns at its first test for a
thread that runs or is in its exit, and scans the table of threads for a record
only for a pid
that has no thread or that was waited for. No ABI change.
## Testing
Two test programs (not in this PR).
The first one needs no change to the kernel. A child starts an `aio_read()`
on an empty pipe and returns. Its exit wakes its parent and then waits in
`aio_cleanup()` until the read ends, that is until the test writes one byte to
the pipe. `CONFIG_FS_AIO`, children by `task_create()`.
| case | before (master) | after |
|---|---|---|
| orphan: the parent of the child ended, the child waits in its exit:
`kill(c, 0)`, `stat("/proc/<c>")`, `open("/proc/<c>/status")`, listed in
`/proc`; then the byte, the child ends | 0, 0, 0, listed; then `ESRCH`, not
listed | the same |
| reaped: the test is the parent; before `waitpid()`: `kill(c, 0)`, listed;
after `waitpid()` returned, with the child still in its exit: `kill(c, 0)`,
`kill(c, SIGTERM)`, `stat()`, `open()`, listed | 0, listed; 0, `ESRCH`, 0, 0,
listed | 0, listed; `ESRCH`, `ESRCH`, `ENOENT`, `ENOENT`, not listed |
| reapwait: as reaped, but the test is in `waitpid()` before the child exits
| 0, 0, 0, listed | `ESRCH`, `ENOENT`, `ENOENT`, not listed |
qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF), :nsh (flat, 1 CPU, HVF),
qemu-intel64:citest_smp (flat, 4 CPUs, KVM): before and after 3 rounds each,
all as in the table. qemu-armv8a:nsh without `CONFIG_SCHED_CHILD_STATUS`, and
without `CONFIG_SCHED_HAVE_PARENT`: orphan and reapwait as in the table, 3
rounds each (the reaped case does not apply: `waitpid()` of a child that is
already in its exit gives `ECHILD` there).
The second program uses a test-only change to `nxtask_exithook()` (not in
this PR: a task that exits with status 90 sleeps 300 ms after it woke its
parent, before `group_leave()`; this holds open the window between `waitpid()`
and the release of the TCB). Children by `task_create()`,
`CONFIG_SCHED_HAVE_PARENT`, `CONFIG_SCHED_CHILD_STATUS`, `CONFIG_SCHED_WAITPID`.
| case | before (master) | after |
|---|---|---|
| reaped: after `waitpid()` returned, `kill(pid, 0)`, `stat("/proc/<pid>")`,
`open("/proc/<pid>/status")`, `readdir()` of `/proc` (a snapshot from before
and a new one) | `kill()` 0, `stat()` and `open()` succeed, the pid is listed
in both | `ESRCH`, `ENOENT`, `ENOENT`, not listed |
| zombie: the child ended 300 ms ago, no `waitpid()` yet: `kill(pid, 0)`,
`kill(pid, SIGTERM)`; after `waitpid()`: `kill(pid, 0)` | `ESRCH`, `ESRCH`;
`ESRCH` | 0, 0; `ESRCH` |
| thread: `kill(tid, 0)` after `pthread_join()` (control) | `ESRCH` |
`ESRCH` |
| readdir: children A and B, `opendir("/proc")`, A reaped and gone, then the
walk | 1 of 2 rounds on arm64 and 2 of 2 on x86_64: B and `self` missing (the
walk stops at A) | B and `self` listed, A not listed |
qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF) and qemu-intel64:citest_smp (flat, 4
CPUs, KVM): before and after 2 rounds each, all cases as in the table. Without
the test-only change (qemu-armv8a:nsh), the zombie, thread and readdir cases
are as in the table.
Linux 7.2 (the same cases as a host program, `fork()` for `task_create()`,
no hook): all four cases give the "after" column.
ostest (it has the signal, waitpid, suspend and robust cases) passes on
qemu-armv8a:nsh_smp (flat, 2 CPUs, HVF), :nsh (flat, HVF), :nsh without
`CONFIG_SCHED_CHILD_STATUS` and without `CONFIG_SCHED_HAVE_PARENT` (flat, HVF),
:knsh (kernel, 1 CPU, TCG), :knsh with 4 CPUs (kernel, TCG, 3 runs) and
qemu-intel64:knsh_romfs (kernel, KVM). On qemu-armv8a:knsh with 4 CPUs,
`cond_test` sometimes prints `ERROR waiter state = 1 != COND_WAITING` and
ostest still exits with status 0: 1 of 3 runs after, 1 of 3 runs on master. On
qemu-intel64:citest_smp ostest passes the signal cases and then stops in its
`wdog` test, on master too.
Builds with `-Werror`: qemu-armv8a:nsh_smp, :nsh, :knsh, :knsh with 4 CPUs,
:nsh without `CONFIG_SCHED_CHILD_STATUS`, :nsh without
`CONFIG_SCHED_HAVE_PARENT` (Arm GNU 13.2); rv-virt:nsh64 (flat), :pnsh64
(protected), :knsh64 (kernel), qemu-intel64:nsh, :citest_smp, :knsh_romfs (CI
image). rv-virt:nsh64 with `CONFIG_DISABLE_ALL_SIGNALS`: every object compiles
(the link fails for want of `init_main`, on master too).
--
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]