Subject: Re: Sane exit from a program on receipt of a signal
To: Christos Zoulas <>
From: Luke Mewburn <>
List: tech-userlevel
Date: 07/20/2007 10:59:12
Content-Type: multipart/mixed; boundary="UoPmpPX/dBe4BELn"
Content-Disposition: inline

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Wed, Jul 18, 2007 at 08:27:35PM +0000, Christos Zoulas wrote:
  | - It should return int with an error.

Good idea; changed to returning -1 on error, 0 if ok.

  | - I think that you want to make sure that you exit with that signal and
  |   not another. See: /usr/src/lib/libc/sys/stack_protector.c

As der Mouse pointed out, this function could be useful for
raising the default handler for signals that don't terminate
the process (e.g, SIGTSTP).  As such, I'll leave it to the
caller to invoke raise_default_unblock() and then explicitly
exit depending upon the caller's needs.

  | - call it raise4sure() :-) or raise_default_unblock()?

raise_default_unblock() sounds good.

I've attached my second attempt.

Look ok?
Put in libutil? (or somewhere else?)

Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="raise_default_unblock.c"

#include <signal.h>
#include <stdio.h>
#include <string.h>

raise_default_unblock(int sig)
	struct sigaction act;
	sigset_t mask;

		/* Setup structures to use */
	memset(&act, 0, sizeof(act));
	act.sa_handler = SIG_DFL;
	if ((sigemptyset(&act.sa_mask) == -1) ||
	    (sigemptyset(&mask) == -1) ||
	    (sigaddset(&mask, sig) == -1))
		return -1;

		/* Restore the default handler, unblock the signal */
	if ((sigaction(sig, &act, NULL) == -1) ||
	    (sigprocmask(SIG_UNBLOCK, &mask, 0) == -1))
		return -1;

		/* Raise the signal, returning the result */
	return raise(sig);


Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.4.7 (NetBSD)