timeout(1): Document the reaper implementation and behaivor

If timeout(1) runs with the --foreground option, it becomes the reaper
of the command and its descendants and will wait for all of them to
terminate.  This behavior is different from the old FreeBSD version and
GNU version.

For example, if there is a descendant running in the background, like:
$ timeout -s INT 2 sh -c 'sleep 4 & sleep 5'
when timeout(1) sends the SIGINT to all descendants, the child 'sh'
process and the foreground 'sleep 5' process will immediately terminate,
but the background 'sleep 4' will be reparented to the timeout(1)
itself.  Because a background process ignores SIGINT and SIGQUIT,
the whole command completes until the background 'sleep 4' finishes.

In comparison, the old FreeBSD version and GNU version will just
terminate itself, letting the background 'sleep 4' process be reparented
to an upper reaper like init.

The POSIX.1-2024 standard doesn't specify the required behavior in such
cases, so I decided to keep the current implementation.  Nonetheless,
the updated timeout(1) has changed a lot in order to conform to the
standard.

Obtained-from: DragonFly BSD
This commit is contained in:
Aaron LI 2025-04-03 10:54:49 +08:00 committed by Baptiste Daroussin
parent a25b037917
commit c362781c8d

View file

@ -1,6 +1,7 @@
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.\" Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
.\" Copyright (c) 2025 Aaron LI <aly@aaronly.me>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
@ -101,6 +102,9 @@ The options are as follows:
Only time out the
.Ar command
itself, but do not propagate signals to its descendants.
See the
.Sx IMPLEMENTATION NOTES
section for more details.
.It Fl k Ar time , Fl -kill-after Ar time
Send a
.Dv SIGKILL
@ -145,6 +149,30 @@ hours
.It Cm d
days
.El
.Sh IMPLEMENTATION NOTES
If the
.Fl -foreground
option is not specified,
.Nm
runs as the reaper (see also
.Xr procctl 2 )
of the
.Ar command
and its descendants, and will wait for all the descendants to terminate.
This behavior might cause surprises if there are descendants running
in the background, because they will ignore
.Dv SIGINT
and
.Dv SIGQUIT
signals.
For example, the following command that sends a
.Dv SIGTERM
signal will complete in 2 seconds:
.Dl $ timeout -s TERM 2 sh -c 'sleep 4 & sleep 5'
However, this command that sends a
.Dv SIGINT
signal will complete in 4 seconds:
.Dl $ timeout -s INT 2 sh -c 'sleep 4 & sleep 5'
.Sh EXIT STATUS
If the time limit was reached and the
.Fl -preserve-status
@ -247,9 +275,9 @@ The
command first appeared in
.Fx 10.3 .
.Pp
The
The initial
.Fx
work is compatible with GNU
work was compatible with GNU
.Nm
by
.An Padraig Brady ,
@ -258,6 +286,7 @@ The
.Nm
utility first appeared in GNU Coreutils 7.0.
.Sh AUTHORS
.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org
and
.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org ,
.An Vsevolod Stakhov Aq Mt vsevolod@FreeBSD.org
and
.An Aaron LI Aq Mt aly@aaronly.me