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