xref: /illumos-gate/usr/src/cmd/syseventd/daemons/syseventd/sysevent_signal.c (revision 59d65d3175825093531e82f44269d948ed510a00)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <signal.h>
28 #include <thread.h>
29 #include <unistd.h>
30 #include <errno.h>
31 
32 #include "sysevent_signal.h"
33 
34 static se_signal_f *sig_handlers[NSIG];
35 static void *sig_data[NSIG];
36 
37 static void
38 sig_stub(int sig, siginfo_t *sip, void *ucp)
39 {
40 	sig_handlers[sig](sig, sip, (ucontext_t *)ucp, sig_data[sig]);
41 }
42 
43 int
44 se_signal_sethandler(int sig, se_signal_f *handler, void *data)
45 {
46 	struct sigaction act;
47 	int status;
48 
49 	sig_handlers[sig] = handler;
50 	sig_data[sig] = data;
51 
52 	if (handler == SIG_DFL || handler == SIG_IGN) {
53 		act.sa_handler = handler;
54 		act.sa_flags = SA_RESTART;
55 	} else {
56 		act.sa_sigaction = sig_stub;
57 		act.sa_flags = SA_SIGINFO | SA_RESTART;
58 	}
59 
60 	(void) sigfillset(&act.sa_mask);
61 
62 	if ((status = sigaction(sig, &act, NULL)) == 0)
63 		(void) se_signal_unblock(sig);
64 
65 	return (status);
66 }
67 
68 int
69 se_signal_unblock(int sig)
70 {
71 	sigset_t set;
72 
73 	(void) sigemptyset(&set);
74 	(void) sigaddset(&set, sig);
75 
76 	return (thr_sigsetmask(SIG_UNBLOCK, &set, NULL));
77 }
78 
79 int
80 se_signal_blockall(void)
81 {
82 	sigset_t set;
83 
84 	(void) sigfillset(&set);
85 	return (thr_sigsetmask(SIG_BLOCK, &set, NULL));
86 }
87 
88 int
89 se_signal_unblockall(void)
90 {
91 	sigset_t set;
92 
93 	(void) sigfillset(&set);
94 	return (thr_sigsetmask(SIG_UNBLOCK, &set, NULL));
95 }
96