Home | History | Annotate | Download | only in sh
History log of /src/bin/sh/trap.h
RevisionDateAuthorComments
 1.26  09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.25  03-Dec-2018  martin branches: 1.25.12;
Make pendingsigs forward declaration match the definition.
 1.24  03-Dec-2018  kre Cleanup traps a bit - attempt to handle weird uses in traps, such
as traps that issue break/continue/return to cause the loop/function
executing when the trap occurred to break/continue/return, and
generating the correct exit code from the shell including when a
signal is caught, but the trap handler for it exits.

All that from FreeBSD.

Also make
T=$(trap)
work as it is supposed to (also trap -p).

For now this is handled by the same technique as $(jobs) - rather
than clearing the traps in subshells, just mark them invalid, and
then whenever they're invalid, clear them before executing anything
other than the special blessed "trap" command. Eventually we will
handle these using non-subshell command substitution instead (not
creating a subshell environ when the commands in a command-sub alter
nothing in the environment).
 1.23  19-Aug-2018  kre PR bin/48875 (is related, and ameliorated, but not exactly "fixed")

Import a whole set of tree evaluation enhancements from FreeBSD.

With these, before forking, the shell predicts (often) when all it will
have to do after forking (in the parent) is wait for the child and then
exit with the status from the child, and in such a case simply does not
fork, but rather allows the child to take over the parent's role.

This turns out to handle the particular test case from PR bin/48875 in
such a way that it works as hoped, rather than as it did (the delay there
was caused by an extra copy of the shell hanging around waiting for the
background child to complete ... and keeping the command substitution
stdout open, so the "real" parent had to wait in case more output appeared).

As part of doing this, redirection processing for compound commands gets
moved out of evalsubshell() and into a new evalredir(), which allows us
to properly handle errors occurring while performing those redirects,
and not mishandle (as in simply forget) fd's which had been moved out
of the way temporarily.

evaltree() has its degree of recursion reduced by making it loop to
handle the subsequent operation: that is instead of (for any binop
like ';' '&&' (etc)) where it used to
evaltree(node->left);
evaltree(node->right);
return;
it now does (kind of)
next = node;
while ((node = next) != NULL) {
next = NULL;

if (node is a binary op) {
evaltree(node->left);
if appropriate /* if && test for success, etc */
next = node->right;
continue;
}
/* similar for loops, etc */
}
which can be a good saving, as while the left side (now) tends to be
(usually) a simple (or simpleish) command, the right side can be many
commands (in a command sequence like a; b; c; d; ... the node at the
top of the tree will now have "a" as its left node, and the tree for
b; c; d; ... as its right node - until now everything was evaluated
recursively so it made no difference, and the tree was constructed
the other way).

if/while/... statements are done similarly, recurse to evaluate the
condition, then if the (or one of the) body parts is to be evaluated,
set next to that, and loop (previously it recursed).

There is more to do in this area (particularly in the way that case
statements are processed - we can avoid recursion there as well) but
that can wait for another day.

While doing all of this we keep much better track of when the shell is
just going to exit once the current tree is evaluated (with a new
predicate at_eof() to tell us that we have, for sure, reached the end
of the input stream, that is, this shell will, for certain, not be reading
more command input) and use that info to avoid unneeded forks. For that
we also need another new predicate (have_traps()) to determine of there
are any caught traps which might occur - if there are, we need to remain
to (potentially) handle them, so these optimisations will not occur (to
make the issue in PR 48875 appear again, run the same code, but with a
trap set to execute some code when a signal (or EXIT) occurs - note that
the trap must be set in the appropriate level of sub-shell to have this
effect, any caught traps are cleared in a subshell whenever one is created).

There is still work to be done to handle traps properly, whatever
weirdness they do (some of which is related to some of this.)

These changes do not need man page updates, but 48875 does - an update
to sh.1 will be forthcoming once it is decided what it should say...

Once again, all the heavy lifting for this set of changes comes directly
(with thanks) from the FreeBSD shell.

XXX pullup-8 (but not very soon)
 1.22  22-Aug-2015  christos branches: 1.22.14; 1.22.16;
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.21  22-Aug-2015  christos Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
 1.20  15-Mar-2012  joerg branches: 1.20.12;
Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.19  18-Jun-2011  christos branches: 1.19.2; 1.19.4;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.18  11-Jul-2005  christos branches: 1.18.40;
make setsig() return sig_t
 1.17  07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16  24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.15  27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.14  22-May-2000  elric branches: 1.14.6;
Back out previous vfork changes.
 1.13  13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.12  28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.11  16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.10  07-Jun-1995  christos branches: 1.10.6;
Ignore result of sigaction when setting traps. Traps will succeed even
on SIGKILL or SIGSTOP. This is what other bourne shells do. (suggested
by mycroft)
 1.9  11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8  21-Mar-1995  cgd convert to new RCS id conventions.
 1.7  23-Dec-1994  cgd be more careful with casts.
 1.6  11-Jun-1994  mycroft Add RCS ids.
 1.5  11-May-1994  jtc sync with 4.4lite
 1.4  01-Aug-1993  mycroft Add RCS identifiers.
 1.3  23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2  22-Mar-1993  cgd added rcs ids to all files
 1.1  21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2  11-May-1994  jtc 44lite code
 1.1.1.1  21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.6.1  26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.14.6.1  27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.18.40.1  23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.19.4.1  15-Nov-2015  bouyer Pull up following revision(s) (requested by christos in ticket #1323):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.19.2.1  17-Apr-2012  yamt sync with head
 1.20.12.1  04-Nov-2015  riz Pull up following revision(s) (requested by christos in ticket #964):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.22.16.3  21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.16.2  21-Apr-2020  martin Sync with HEAD
 1.22.16.1  10-Jun-2019  christos Sync with HEAD
 1.22.14.2  26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.22.14.1  06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.25.12.1  02-Aug-2025  perseant Sync with HEAD

RSS XML Feed