xref: /titanic_50/usr/src/lib/fm/libldom/sparc/ldom_utils.c (revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * ldom_utils.c
28  *
29  * Common functions within the library
30  *
31  */
32 
33 #include <stdio.h>
34 #include <signal.h>
35 #include <pthread.h>
36 
37 /*
38  * ldom_find_thr_sig()
39  * Description:
40  *     Find an unmasked signal which is used for terminating the thread
41  *
42  *     If the libldom.so thread is started before all fmd modules are loaded,
43  *     all signals are unmasked. Either SIGTERM or SIGUSR1 can be used to
44  *     stop the thread.
45  *     If the thread is started by a fmd module, fmd has masked all signals
46  *     except the client.thrsig and a list of reserver/non-catchable signals.
47  *     The fmd client.thrsig signal must be used to stop the thread. The default
48  *     value of the client.thrsig is SIGUSR1.
49  *
50  *     This fucntion first tries to check if the SIGTERM, SIGUSR1 or SIGUSR2
51  *     signal is umasked. If so, select this signal.
52  *     Otherwise, go through all the signals and find an umasked one.
53  */
54 int
55 ldom_find_thr_sig(void)
56 {
57 	int i;
58 	sigset_t oset, rset;
59 	int sig[] = {SIGTERM, SIGUSR1, SIGUSR2};
60 	int sig_sz = sizeof (sig) / sizeof (int);
61 	int rc = SIGTERM;
62 
63 	/* prefered set of signals that are likely used to terminate threads */
64 	(void) sigemptyset(&oset);
65 	(void) pthread_sigmask(SIG_SETMASK, NULL, &oset);
66 	for (i = 0; i < sig_sz; i++) {
67 		if (sigismember(&oset, sig[i]) == 0) {
68 			return (sig[i]);
69 		}
70 	}
71 
72 	/* reserved set of signals that are not allowed to terminate thread */
73 	(void) sigemptyset(&rset);
74 	(void) sigaddset(&rset, SIGABRT);
75 	(void) sigaddset(&rset, SIGKILL);
76 	(void) sigaddset(&rset, SIGSTOP);
77 	(void) sigaddset(&rset, SIGCANCEL);
78 
79 	/* Find signal that is not masked and not in the reserved list. */
80 	for (i = 1; i < MAXSIG; i++) {
81 		if (sigismember(&rset, i) == 1) {
82 			continue;
83 		}
84 		if (sigismember(&oset, i) == 0) {
85 			return (i);
86 		}
87 	}
88 
89 	return (rc);
90 }
91