xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_signal.c (revision 306990466301759c88d84c51c57b57ca930a3635)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <signal.h>
28 #include <unistd.h>
29 #include <errno.h>
30 
31 #include <mdb/mdb_signal.h>
32 #include <mdb/mdb_debug.h>
33 
34 static mdb_signal_f *sig_handlers[NSIG];
35 static void *sig_data[NSIG];
36 
37 static void
sig_stub(int sig,siginfo_t * sip,void * ucp)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
mdb_signal_sethandler(int sig,mdb_signal_f * handler,void * data)44 mdb_signal_sethandler(int sig, mdb_signal_f *handler, void *data)
45 {
46 	struct sigaction act;
47 	int status;
48 
49 	ASSERT(sig > 0 && sig < NSIG && sig != SIGKILL && sig != SIGSTOP);
50 
51 	sig_handlers[sig] = handler;
52 	sig_data[sig] = data;
53 
54 	if (handler == MDB_SIG_DFL) {
55 		act.sa_handler = SIG_DFL;
56 		act.sa_flags = SA_RESTART;
57 	} else if (handler == MDB_SIG_IGN) {
58 		act.sa_handler = SIG_IGN;
59 		act.sa_flags = SA_RESTART;
60 	} else {
61 		act.sa_sigaction = sig_stub;
62 		act.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
63 	}
64 
65 	(void) sigemptyset(&act.sa_mask);
66 
67 	if (sig == SIGWINCH || sig == SIGTSTP) {
68 		(void) sigaddset(&act.sa_mask, SIGWINCH);
69 		(void) sigaddset(&act.sa_mask, SIGTSTP);
70 		(void) sigaddset(&act.sa_mask, SIGHUP);
71 		(void) sigaddset(&act.sa_mask, SIGTERM);
72 	}
73 
74 	if ((status = sigaction(sig, &act, NULL)) == 0)
75 		(void) mdb_signal_unblock(sig);
76 
77 	return (status);
78 }
79 
80 mdb_signal_f *
mdb_signal_gethandler(int sig,void ** datap)81 mdb_signal_gethandler(int sig, void **datap)
82 {
83 	if (datap != NULL)
84 		*datap = sig_data[sig];
85 
86 	return (sig_handlers[sig]);
87 }
88 
89 int
mdb_signal_raise(int sig)90 mdb_signal_raise(int sig)
91 {
92 	return (kill(getpid(), sig));
93 }
94 
95 int
mdb_signal_pgrp(int sig)96 mdb_signal_pgrp(int sig)
97 {
98 	return (kill(0, sig));
99 }
100 
101 int
mdb_signal_block(int sig)102 mdb_signal_block(int sig)
103 {
104 	sigset_t set;
105 
106 	(void) sigemptyset(&set);
107 	(void) sigaddset(&set, sig);
108 
109 	return (sigprocmask(SIG_BLOCK, &set, NULL));
110 }
111 
112 int
mdb_signal_unblock(int sig)113 mdb_signal_unblock(int sig)
114 {
115 	sigset_t set;
116 
117 	(void) sigemptyset(&set);
118 	(void) sigaddset(&set, sig);
119 
120 	return (sigprocmask(SIG_UNBLOCK, &set, NULL));
121 }
122 
123 int
mdb_signal_blockall(void)124 mdb_signal_blockall(void)
125 {
126 	sigset_t set;
127 
128 	(void) sigfillset(&set);
129 	return (sigprocmask(SIG_BLOCK, &set, NULL));
130 }
131 
132 int
mdb_signal_unblockall(void)133 mdb_signal_unblockall(void)
134 {
135 	sigset_t set;
136 
137 	(void) sigfillset(&set);
138 	return (sigprocmask(SIG_UNBLOCK, &set, NULL));
139 }
140