xref: /minix3/lib/libutil/raise_default_signal.c (revision 0c3983b25a88161cf074524e5c94585a2582ae82)
1*0c3983b2SBen Gras /*	$NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $	 */
2*0c3983b2SBen Gras 
3*0c3983b2SBen Gras /*-
4*0c3983b2SBen Gras  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0c3983b2SBen Gras  * All rights reserved.
6*0c3983b2SBen Gras  *
7*0c3983b2SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
8*0c3983b2SBen Gras  * by Luke Mewburn.
9*0c3983b2SBen Gras  *
10*0c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
11*0c3983b2SBen Gras  * modification, are permitted provided that the following conditions
12*0c3983b2SBen Gras  * are met:
13*0c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*0c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*0c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
18*0c3983b2SBen Gras  *
19*0c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0c3983b2SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0c3983b2SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0c3983b2SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0c3983b2SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0c3983b2SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0c3983b2SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0c3983b2SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0c3983b2SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0c3983b2SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0c3983b2SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
30*0c3983b2SBen Gras  */
31*0c3983b2SBen Gras 
32*0c3983b2SBen Gras #if HAVE_NBTOOL_CONFIG_H
33*0c3983b2SBen Gras #include "nbtool_config.h"
34*0c3983b2SBen Gras #endif
35*0c3983b2SBen Gras 
36*0c3983b2SBen Gras #include <sys/cdefs.h>
37*0c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
38*0c3983b2SBen Gras __RCSID("$NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $");
39*0c3983b2SBen Gras #endif
40*0c3983b2SBen Gras 
41*0c3983b2SBen Gras #include <errno.h>
42*0c3983b2SBen Gras #include <signal.h>
43*0c3983b2SBen Gras #include <stdio.h>
44*0c3983b2SBen Gras #include <string.h>
45*0c3983b2SBen Gras #include <util.h>
46*0c3983b2SBen Gras 
47*0c3983b2SBen Gras #if ! HAVE_RAISE_DEFAULT_SIGNAL
48*0c3983b2SBen Gras /*
49*0c3983b2SBen Gras  * raise_default_signal sig
50*0c3983b2SBen Gras  *	Raise the default signal handler for sig, by
51*0c3983b2SBen Gras  *	- block all signals
52*0c3983b2SBen Gras  *	- set the signal handler to SIG_DFL
53*0c3983b2SBen Gras  *	- raise the signal
54*0c3983b2SBen Gras  *	- unblock the signal to deliver it
55*0c3983b2SBen Gras  *
56*0c3983b2SBen Gras  *	The original signal mask and signal handler is restored on exit
57*0c3983b2SBen Gras  *	(whether successful or not).
58*0c3983b2SBen Gras  *
59*0c3983b2SBen Gras  *	Returns 0 on success, or -1 on failure with errno set to
60*0c3983b2SBen Gras  *	on of the values for sigemptyset(), sigaddset(), sigprocmask(),
61*0c3983b2SBen Gras  *	sigaction(), or raise().
62*0c3983b2SBen Gras  */
63*0c3983b2SBen Gras int
raise_default_signal(int sig)64*0c3983b2SBen Gras raise_default_signal(int sig)
65*0c3983b2SBen Gras {
66*0c3983b2SBen Gras 	struct sigaction origact, act;
67*0c3983b2SBen Gras 	sigset_t origmask, fullmask, mask;
68*0c3983b2SBen Gras 	int retval, oerrno;
69*0c3983b2SBen Gras 
70*0c3983b2SBen Gras 	retval = -1;
71*0c3983b2SBen Gras 
72*0c3983b2SBen Gras 		/* Setup data structures */
73*0c3983b2SBen Gras 		/* XXX memset(3) isn't async-safe according to signal(7) */
74*0c3983b2SBen Gras 	(void)memset(&act, 0, sizeof(act));
75*0c3983b2SBen Gras 	act.sa_handler = SIG_DFL;
76*0c3983b2SBen Gras 	act.sa_flags = 0;
77*0c3983b2SBen Gras 	if ((sigemptyset(&act.sa_mask) == -1) ||
78*0c3983b2SBen Gras 	    (sigfillset(&fullmask) == -1) ||
79*0c3983b2SBen Gras 	    (sigemptyset(&mask) == -1) ||
80*0c3983b2SBen Gras 	    (sigaddset(&mask, sig) == -1))
81*0c3983b2SBen Gras 		goto restore_none;
82*0c3983b2SBen Gras 
83*0c3983b2SBen Gras 		/* Block all signals */
84*0c3983b2SBen Gras 	if (sigprocmask(SIG_BLOCK, &fullmask, &origmask) == -1)
85*0c3983b2SBen Gras 		goto restore_none;
86*0c3983b2SBen Gras 		/* (use 'goto restore_mask' to restore state) */
87*0c3983b2SBen Gras 
88*0c3983b2SBen Gras 		/* Enable the SIG_DFL handler */
89*0c3983b2SBen Gras 	if (sigaction(sig, &act, &origact) == -1)
90*0c3983b2SBen Gras 		goto restore_mask;
91*0c3983b2SBen Gras 		/* (use 'goto restore_act' to restore state) */
92*0c3983b2SBen Gras 
93*0c3983b2SBen Gras 		/* Raise the signal, and unblock the signal to deliver it */
94*0c3983b2SBen Gras 	if ((raise(sig) == -1) ||
95*0c3983b2SBen Gras 	    (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1))
96*0c3983b2SBen Gras 		goto restore_act;
97*0c3983b2SBen Gras 
98*0c3983b2SBen Gras 		/* Flag successful raise() */
99*0c3983b2SBen Gras 	retval = 0;
100*0c3983b2SBen Gras 
101*0c3983b2SBen Gras 		/* Restore the original handler */
102*0c3983b2SBen Gras  restore_act:
103*0c3983b2SBen Gras 	oerrno = errno;
104*0c3983b2SBen Gras 	(void)sigaction(sig, &origact, NULL);
105*0c3983b2SBen Gras 	errno = oerrno;
106*0c3983b2SBen Gras 
107*0c3983b2SBen Gras 		/* Restore the original mask */
108*0c3983b2SBen Gras  restore_mask:
109*0c3983b2SBen Gras 	oerrno = errno;
110*0c3983b2SBen Gras 	(void)sigprocmask(SIG_SETMASK, &origmask, NULL);
111*0c3983b2SBen Gras 	errno = oerrno;
112*0c3983b2SBen Gras 
113*0c3983b2SBen Gras  restore_none:
114*0c3983b2SBen Gras 	return retval;
115*0c3983b2SBen Gras }
116*0c3983b2SBen Gras 
117*0c3983b2SBen Gras #endif	/* ! HAVE_RAISE_DEFAULT_SIGNAL */
118