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


--UoPmpPX/dBe4BELn
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?)

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

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

int
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);
}

--UoPmpPX/dBe4BELn--

--Dzs2zDY0zgkG72+7
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (NetBSD)

iD8DBQFGoAjgpBhtmn8zJHIRApL8AJ9IQ51Q8AeOgTvrS9l6U/PjjJrC2gCgz76c
r3ZgewKf3tF8wk7aNTpX0O0=
=FUeR
-----END PGP SIGNATURE-----

--Dzs2zDY0zgkG72+7--