Skip to content

Add alarm(3) support to chpst#45

Open
pilcrow wants to merge 5 commits into
g-pape:mainfrom
pilcrow:chpst-alarm
Open

Add alarm(3) support to chpst#45
pilcrow wants to merge 5 commits into
g-pape:mainfrom
pilcrow:chpst-alarm

Conversation

@pilcrow

@pilcrow pilcrow commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

chpst -A secs ... sets or clears an alarm. Tests and md docs updated, too.

Update chpst.check, chpst.dist, chpst.8.md

@g-pape g-pape left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'm curious, how you intend to use this, do you have examples?

@pilcrow

pilcrow commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

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.

@g-pape

g-pape commented Jun 27, 2026

Copy link
Copy Markdown
Owner

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, sed -i s/secs/seconds/ md/chpst.8.md

@pilcrow

pilcrow commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

Gotcha; updated the .md

Comment thread src/chpst.c Outdated
Comment on lines +428 to +436
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);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look soon, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

pilcrow added 3 commits July 13, 2026 08:09
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants