casaroli opened a new pull request, #3968: URL: https://github.com/apache/nuttx-apps/pull/3968
## Summary `$!` is the PID of the last command that the shell started in the background. NSH changed it after each command: `nsh_command()` set it to the PID of NSH after each NSH command, and a program in the foreground (`nsh_builtin()`, `nsh_fileapp()`) set it to its own PID. So: - `cmd &`, then `echo $!`, then `wait $!` waited for NSH itself. `pthread_join()` of the calling thread failed, `cmd_wait()` did not report it, and `$?` was 0 instead of the status of `cmd`. - `cmd &`, then a program in the foreground, then `wait $!` failed with `nsh: wait: wait failed: 2`: `$!` was the PID of the program that had exited. This PR sets `np_lastpid` only where NSH starts a command in the background (the code that prints `cmd [pid:priority]`). depends-on: apache/nuttx/pull/21436 ## Impact Only `$!`. It now changes only when NSH starts a command in the background, as POSIX says. `wait` and `$?` do not change. Flat, protected and kernel builds (builtin apps and file apps). A script that used `$!` to get the PID of NSH or of a foreground command sees the PID of the last background command now. The NSH documentation does not describe `$!`; a separate apache/nuttx commit (`Documentation/nsh: Describe the built-in variable $!.`) adds it. ## Testing NSH commands on qemu-armv8a:nsh (flat) under HVF with `CONFIG_SIG_DEFAULT`, `CONFIG_SCHED_CHILD_STATUS` and `CONFIG_CANCELLATION_POINTS`: | Commands | `$?` with apps master | `$?` with this PR | |---|---|---| | `sh -c "sleep 1; exit 5" &`, `echo $!`, `wait $!`, `echo $?` | 0 (waited for NSH) | 1 | | `sh -c "sleep 1; exit 5" &`, `wait $!`, `echo $?` | 1 | 1 | | `sh -c "sleep 1; exit 5" &`, `hello`, `wait $!`, `echo $?` | `wait failed: 2`, then 0 | 1 | The column with apps master gives the same results on rv-virt:citest (flat, rv32). ostest passes (`Exiting with status 0`) on qemu-armv8a:nsh and on qemu-armv8a:knsh (kernel build) with this PR. `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]
