Add alarm(3) support to chpst#45
Conversation
Update chpst.check, chpst.dist, chpst.8.md
g-pape
left a comment
There was a problem hiding this comment.
Looks good! I'm curious, how you intend to use this, do you have examples?
It's infrequent, but usually for misbehaving code in testing, or the odd third-party production binary I don't trust. I've for years used a perl/alarm/exec stanza as an ersatz GNU timeout, but would rather have the same in the all-in-one chpst. |
|
Ok. Although I won't use it much, it looks useful, well implemented, and chpst already has many options anyway. I'll play with it a bit, also together with the new -F and -I options, and probably merge soon. To be consistent with -t, |
|
Gotcha; updated the |
| if (nostdout) if (close(1) == -1) fatal("unable to close stdout"); | ||
| if (nostderr) if (close(2) == -1) fatal("unable to close stderr"); | ||
| slimit(); | ||
| if (alrmsecs >= 0) alarm((unsigned int)alrmsecs); |
There was a problem hiding this comment.
With -F the alarm is set after forking pid 1 and 2 in the new namespace, and so the signal is sent to prog (pid 2) only, not its children:
sudo command/chpst -FA1 sh -c 'exec sleep 2'; echo $? # ok
sudo command/chpst -FA1 sh -c 'sleep 2'; echo $?
sudo command/chpst -FA1 sh -c 'sleep 2 &'; echo $?
Calling alarm() before newpid1() sets the alarm in the main process, which relays the signal to all processes in the new namespace, including children of prog. That's what I expect when combining -A with -F.
There was a problem hiding this comment.
I'll take a look soon, thanks.
There was a problem hiding this comment.
OK 45b7fde moves the alarm(2) before newpid1(), meaning in the common case ( chpst -A 1 prog ), the target program will inherit as before. In the relay cases (-I, -F), the (grand)parent's alarm will be relayed to the target program.
Merge 40b967b (merging main updates) dropped N from USAGE_MAIN and the getopt string while keeping case 'N' and all -N code. Restore both. Regenerate chpst.dist via check-dist; the old file had inconsistent usage lines and stale test ordering. Restore -A alarm tests from 61a487d that were also lost in the merge. Document -A + -F interaction in chpst.8.md: when combined with -F, the alarm is set before entering the new namespace so SIGALRM is relayed through chpst to all processes in prog's namespace.
Move alarm() from after slimit() to before newpid1(), arming it once in the initial chpst process. With -F, the relay inherits the alarm; when it fires, sig_handler forwards SIGALRM to namespace pid 1, and sig_handler_pid1 fans it to kill(-1, sig) so the entire namespace -- prog and all its children -- receives it. Without -F/-I, exec() inherits the alarm into the target as before. Tested with -FA1 across exec, forked child, and background child.
chpst -A secs ...sets or clears an alarm. Tests and md docs updated, too.