bug-bash
[Top][All Lists]
Advanced

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

Re: a kill -TERM causes the EXIT trap to be executed


From: Bob Proulx
Subject: Re: a kill -TERM causes the EXIT trap to be executed
Date: Thu, 26 Jan 2006 23:34:31 -0700
User-agent: Mutt/1.5.9i

Vincent Lefevre wrote:
>   An EXIT trap is executed when the process is killed, e.g. by the
>   SIGTERM signal, which breaks traditional sh compatibility

A lot of POSIX required behavior breaks traditional Bourne sh
compatibility.

>   and, IMHO, is not POSIX-compliant (see the explanations in the
>   script below).  Other shells, such as dash, ksh and zsh are not
>   affected by this problem.

ksh handles this the same way as bash.  Perhaps you tested with pdksh?

>   With the following script, "sh trapexitbug" (under Linux) and
>   "bash trapexitbug" cause the EXIT trap to be executed.

It has been a long time POSIX shell requirement that the EXIT trap is
executed upon shell exit regardless of why that shell exited.  This is
intentional.

Note that in addition to bash that HP-UX /bin/sh and /bin/ksh both
execute the EXIT trap upon receiving SIGTERM.  I imagine that other
systems behave this way too.  Changing this would break compatibility
with many scripts.  This has been relied upon for a long time.

> # http://www.opengroup.org/onlinepubs/009695399/basedefs/signal.h.html says:
> #     SIGTERM  T  Termination signal.
> # and for T:
> #     Abnormal termination of the process.  The process is terminated
> #     with all the consequences of _exit() except that the status made
> #     available to wait() and waitpid() indicates abnormal termination
> #     by the specified signal.
> #
> # http://www.opengroup.org/onlinepubs/009695399/functions/_exit.html says:
> #     The _Exit() [CX] and _exit() functions shall not call functions
> #     registered with atexit() nor any registered signal handlers.

That is the default action.  But once you set an EXIT trap you change
the default action to the one specified in the trap.  So those
requirements no longer apply.

I believe bash (and ksh too) is behaving correctly here.

Bob




reply via email to

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