xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_signal.c (revision b0bb0d63258be430b0e22afcb1581974bd7b568e)
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
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 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 == SIG_DFL || handler == SIG_IGN) {
55 		act.sa_handler = handler;
56 		act.sa_flags = SA_RESTART;
57 	} else {
58 		act.sa_handler = sig_stub;
59 		act.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
60 	}
61 
62 	(void) sigemptyset(&act.sa_mask);
63 
64 	if (sig == SIGWINCH || sig == SIGTSTP) {
65 		(void) sigaddset(&act.sa_mask, SIGWINCH);
66 		(void) sigaddset(&act.sa_mask, SIGTSTP);
67 		(void) sigaddset(&act.sa_mask, SIGHUP);
68 		(void) sigaddset(&act.sa_mask, SIGTERM);
69 	}
70 
71 	if ((status = sigaction(sig, &act, NULL)) == 0)
72 		(void) mdb_signal_unblock(sig);
73 
74 	return (status);
75 }
76 
77 mdb_signal_f *
78 mdb_signal_gethandler(int sig, void **datap)
79 {
80 	if (datap != NULL)
81 		*datap = sig_data[sig];
82 
83 	return (sig_handlers[sig]);
84 }
85 
86 int
87 mdb_signal_raise(int sig)
88 {
89 	return (kill(getpid(), sig));
90 }
91 
92 int
93 mdb_signal_pgrp(int sig)
94 {
95 	return (kill(0, sig));
96 }
97 
98 int
99 mdb_signal_block(int sig)
100 {
101 	sigset_t set;
102 
103 	(void) sigemptyset(&set);
104 	(void) sigaddset(&set, sig);
105 
106 	return (sigprocmask(SIG_BLOCK, &set, NULL));
107 }
108 
109 int
110 mdb_signal_unblock(int sig)
111 {
112 	sigset_t set;
113 
114 	(void) sigemptyset(&set);
115 	(void) sigaddset(&set, sig);
116 
117 	return (sigprocmask(SIG_UNBLOCK, &set, NULL));
118 }
119 
120 int
121 mdb_signal_blockall(void)
122 {
123 	sigset_t set;
124 
125 	(void) sigfillset(&set);
126 	return (sigprocmask(SIG_BLOCK, &set, NULL));
127 }
128 
129 int
130 mdb_signal_unblockall(void)
131 {
132 	sigset_t set;
133 
134 	(void) sigfillset(&set);
135 	return (sigprocmask(SIG_UNBLOCK, &set, NULL));
136 }
137