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