Subject: Re: bug#5995: [PATCH] Fix exit status of signal handlers in shell scripts

From: "Dmitry V. Levin" <[email-masked]>
Date: Thu, 22 Apr 2010 15:53:03 +0400

On Wed, Apr 21, 2010 at 04:04:29PM +0200, Jim Meyering wrote:
[...]
> I'd prefer a solution like the one used in automake:
> 
> http://git.savannah.gnu.org/cgit/automake.git/commit/?id=dbfabdfc6521979
> 
>     +am__trap='rm -f '\''$(abs_builddir)/$@-t'\''; (exit $$st); exit $$st'; \
>     +trap "st=129; $$am__trap" 1; trap "st=130; $$am__trap" 2;      \
>     +trap "st=141; $$am__trap" 13; trap "st=143; $$am__trap" 15; \
> 
> since it preserves signals, rather than mapping all to one,
> which would inevitably produce misleading results some of the time.

Something like this?

From 5c36d056cbd46c43cb0bb54175fcc06b1b6069be Mon Sep 17 00:00:00 2001
From: Dmitry V. Levin <[email-masked]>
Date: Sat, 30 Jan 2010 16:02:36 +0000
Subject: [PATCH] Fix exit status of signal handlers in shell scripts

The value of `$?' on entrance to signal handlers in shell scripts
cannot be relied upon, so set the exit code explicitly.

* cfg.mk (sc_always_defined_macros, sc_system_h_headers): Set
the exit code in signal handler explicitly to 128 + SIG<SIGNAL>.
* src/Makefile.am (sc_tight_scope): Likewise.
* tests/test-lib.sh: Likewise.
---
 cfg.mk            |   10 ++++++++--
 src/Makefile.am   |    5 ++++-
 tests/test-lib.sh |    5 ++++-
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index 7533930..71bcb55 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -134,7 +134,10 @@ headers_with_interesting_macro_defs = \
 # Don't define macros that we already get from gnulib header files.
 sc_always_defined_macros: .re-defmac
 	@if test -f $(srcdir)/src/system.h; then			\
-	  trap 'rc=$$?; rm -f .re-defmac; exit $$rc' 0 1 2 3 15;	\
+	  trap 'rc=$$?; rm -f .re-defmac; exit $$rc' 0;			\
+	  am__exit='(exit $rc); exit $rc';				\
+	  trap "rc=129; $$am__exit" 1; trap "rc=130; $$am__exit" 2;	\
+	  trap "rc=131; $$am__exit" 3; trap "rc=143; $$am__exit" 15;	\
 	  grep -f .re-defmac $$($(VC_LIST))				\
 	    && { echo '$(ME): define the above via some gnulib .h file'	\
 		  1>&2;  exit 1; } || :;				\
@@ -153,7 +156,10 @@ sc_always_defined_macros: .re-defmac
 # the headers already included via system.h.
 sc_system_h_headers: .re-list
 	@if test -f $(srcdir)/src/system.h; then			\
-	  trap 'rc=$$?; rm -f .re-list; exit $$rc' 0 1 2 3 15;		\
+	  trap 'rc=$$?; rm -f .re-list; exit $$rc' 0;			\
+	  am__exit='(exit $rc); exit $rc';				\
+	  trap "rc=129; $$am__exit" 1; trap "rc=130; $$am__exit" 2;	\
+	  trap "rc=131; $$am__exit" 3; trap "rc=143; $$am__exit" 15;	\
 	  grep -nE -f .re-list						\
 	      $$($(VC_LIST_EXCEPT) | grep '^src/')			\
 	    && { echo '$(ME): the above are already included via system.h'\
diff --git a/src/Makefile.am b/src/Makefile.am
index 20b306d..db5359b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -721,7 +721,10 @@ sc_check-AUTHORS: $(all_programs)
 .PHONY: sc_tight_scope
 sc_tight_scope: $(bin_PROGRAMS)
 	@t=exceptions-$$$$;						\
-	trap "s=$$?; rm -f $$t; exit $$s" 0 1 2 13 15;			\
+	trap 's=$$?; rm -f $$t; exit $$s' 0;				\
+	am__exit='(exit $s); exit $s';					\
+	trap "s=129; $$am__exit" 1; trap "s=130; $$am__exit" 2;		\
+	trap "s=141; $$am__exit" 13; trap "s=143; $$am__exit" 15;	\
 	src=`for f in $(SOURCES); do					\
 	       test -f $$f && d= || d=$(srcdir)/; echo $$d$$f; done`;	\
 	hdr=`for f in $(noinst_HEADERS); do				\
diff --git a/tests/test-lib.sh b/tests/test-lib.sh
index 7ad4331..8bf5601 100644
--- a/tests/test-lib.sh
+++ b/tests/test-lib.sh
@@ -408,7 +408,10 @@ remove_tmp_()
 # Run each test from within a temporary sub-directory named after the
 # test itself, and arrange to remove it upon exception or normal exit.
 trap remove_tmp_ 0
-trap 'Exit $?' 1 2 13 15
+trap 'Exit 129' 1
+trap 'Exit 130' 2
+trap 'Exit 141' 13
+trap 'Exit 143' 15
 
 cd "$t_" || error_ "failed to cd to $t_"

P.S.  I wonder why cfg.mk installs a signal handler for SIGQUIT while
src/Makefile.am installs a signal handler for SIGPIPE instead.

-- 
ldv