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