1 /* $NetBSD: raise_default_signal.c,v 1.2 2007/09/28 09:07:16 lukem Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 __RCSID("$NetBSD: raise_default_signal.c,v 1.2 2007/09/28 09:07:16 lukem Exp $");
47 #if ! HAVE_RAISE_DEFAULT_SIGNAL
49 * raise_default_signal sig
50 * Raise the default signal handler for sig, by
52 * - set the signal handler to SIG_DFL
54 * - unblock the signal to deliver it
56 * The original signal mask and signal handler is restored on exit
57 * (whether successful or not).
59 * Returns 0 on success, or -1 on failure with errno set to
60 * on of the values for sigemptyset(), sigaddset(), sigprocmask(),
61 * sigaction(), or raise().
64 raise_default_signal(int sig
)
66 struct sigaction origact
, act
;
67 sigset_t origmask
, fullmask
, mask
;
72 /* Setup data structures */
73 /* XXX memset(3) isn't async-safe according to signal(7) */
74 (void)memset(&act
, 0, sizeof(act
));
75 act
.sa_handler
= SIG_DFL
;
77 if ((sigemptyset(&act
.sa_mask
) == -1) ||
78 (sigfillset(&fullmask
) == -1) ||
79 (sigemptyset(&mask
) == -1) ||
80 (sigaddset(&mask
, sig
) == -1))
83 /* Block all signals */
84 if (sigprocmask(SIG_BLOCK
, &fullmask
, &origmask
) == -1)
86 /* (use 'goto restore_mask' to restore state) */
88 /* Enable the SIG_DFL handler */
89 if (sigaction(sig
, &act
, &origact
) == -1)
91 /* (use 'goto restore_act' to restore state) */
93 /* Raise the signal, and unblock the signal to deliver it */
94 if ((raise(sig
) == -1) ||
95 (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1))
98 /* Flag successful raise() */
101 /* Restore the original handler */
104 (void)sigaction(sig
, &origact
, NULL
);
107 /* Restore the original mask */
110 (void)sigprocmask(SIG_SETMASK
, &origmask
, NULL
);
117 #endif /* ! HAVE_RAISE_DEFAULT_SIGNAL */