bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: EXIT trap definition order


From: Chet Ramey
Subject: Re: EXIT trap definition order
Date: Mon, 20 Mar 2023 11:19:42 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.8.0

On 3/20/23 11:02 AM, Grisha Levit wrote:
On Mon, Mar 20, 2023 at 10:57 AM Chet Ramey <chet.ramey@case.edu> wrote:

On 3/17/23 1:28 PM, Grisha Levit wrote:
It seems that if a trap handler for a terminating signal resends its
own signal (after resetting the signal disposition), any configured
EXIT trap will be executed (as I think is expected), but only if an
EXIT trap had already been set prior to the the first instance of a
trap having been set for the terminating signal in question.

If the shell is handling a terminating signal and receives another instance
of that signal while doing so, it takes that as an indication to exit
immediately.

Thanks, that makes sense. But then the first script should _not_ print
EXIT, right?

i.e. if both TERM and EXIT traps are defined, it shouldn't matter
which one happened to be defined first.

The EXIT trap definition causes the signal handler to be installed for the
other terminating signals, since the expectation is that it will be run
on a, say, SIGHUP, but reverting the signal disposition to the default
ends up with the disposition being set to SIG_DFL instead of the shell's
terminating signal handler.

When the shell installs the signal handler for the trap, it saves the old
signal disposition to restore if the signal is reverted. In the first case,
that's the shell's terminating signal handler (since the exit trap is
there). In the second case, it's SIG_DFL, since the shell hasn't installed
any handlers for terminating signals yet.

So what happens on the second script is that the handler for SIGTERM gets
set to SIG_DFL by the trap handler, which causes the kill to immediately
kill it. There is already a trap for SIGTERM when the EXIT trap installer
goes to install handlers for the terminating signals, so it doesn't bother.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]