xref: /illumos-gate/usr/src/cmd/nscd/nscd_smfmonitor.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2018 Joyent, Inc.
26  */
27 
28 #include <stdlib.h>
29 #include <libscf.h>
30 #include <string.h>
31 #include "nscd_switch.h"
32 #include "nscd_log.h"
33 #include "nscd_door.h"
34 
35 extern int	_whoami;
36 
37 /*
38  * Service states monitored by nscd. Protected by
39  * readers/writer lock nscd_smf_service_state_lock
40  */
41 nscd_smf_state_t *nscd_smf_service_state;
42 static rwlock_t nscd_smf_service_state_lock = DEFAULTRWLOCK;
43 /*
44  * init service state table
45  */
46 nscd_rc_t
47 _nscd_alloc_service_state_table()
48 {
49 	int i;
50 
51 	nscd_smf_service_state = calloc(NSCD_NUM_SMF_FMRI,
52 	    sizeof (nscd_smf_state_t));
53 
54 	if (nscd_smf_service_state == NULL)
55 		return (NSCD_NO_MEMORY);
56 
57 	for (i = 1; i < NSCD_NUM_SMF_FMRI; i++)
58 		NSCD_SMF_SVC_STATE(i) = NSCD_SVC_STATE_UNINITED;
59 
60 	return (NSCD_SUCCESS);
61 }
62 
63 static int
64 query_smf_state(int srci)
65 {
66 
67 	int	ret = NSCD_SVC_STATE_UNINITED;
68 	char	*state = NULL;
69 	char	*me = "query_smf_state";
70 
71 	state = smf_get_state(NSCD_SMF_SVC_FMRI(srci));
72 	if (state == NULL)
73 		return (ret);
74 
75 	_NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
76 		(me, "%s -- %s\n", state, NSCD_SMF_SVC_FMRI(srci));
77 
78 	(void) rw_wrlock(&nscd_smf_service_state_lock);
79 
80 	if (nscd_smf_service_state[srci].src_name == NULL)
81 		nscd_smf_service_state[srci].src_name =
82 		    NSCD_NSW_SRC_NAME(srci);
83 
84 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
85 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_UNINIT;
86 	else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
87 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_MAINT;
88 	else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
89 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_OFFLINE;
90 	else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
91 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_DISABLED;
92 	else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
93 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_ONLINE;
94 	else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
95 		NSCD_SMF_SVC_STATE(srci) = SCF_STATE_DEGRADED;
96 
97 	ret = NSCD_SMF_SVC_STATE(srci);
98 	(void) rw_unlock(&nscd_smf_service_state_lock);
99 
100 	free(state);
101 	return (ret);
102 }
103 
104 /* ARGSUSED */
105 static void *
106 set_smf_state(void *arg)
107 {
108 
109 	int	i;
110 	int	st;
111 
112 	(void) thr_setname(thr_self(), "set_smf_state");
113 
114 	/*
115 	 * the forker nscd needs not monitor the state
116 	 * of the client services
117 	 */
118 	if (_whoami == NSCD_FORKER)
119 		thr_exit(0);
120 
121 	/*CONSTCOND*/
122 	while (1) {
123 
124 		/* skip the first service which is nscd */
125 		for (i = 1; i < NSCD_NUM_SMF_FMRI; i++) {
126 			st = query_smf_state(i);
127 			if (st == NSCD_SVC_STATE_UNINITED)
128 				break;
129 		}
130 
131 		(void) sleep(NSCD_SW_CFG_G.check_smf_state_interval_g);
132 	}
133 	/* NOTREACHED */
134 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
135 }
136 
137 nscd_rc_t
138 _nscd_init_smf_monitor(void)
139 {
140 
141 	int	errnum;
142 	char	*me = "_nscd_init_smf_monitor";
143 
144 	_NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
145 	(me, "initializing the smf monitor\n");
146 
147 	/*
148 	 * start a thread to check the state of the client services
149 	 */
150 	if (thr_create(NULL, 0, set_smf_state,
151 	    NULL, THR_DETACHED, NULL) != 0) {
152 		errnum = errno;
153 		_NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_ERROR)
154 		(me, "thr_create: %s\n", strerror(errnum));
155 		return (NSCD_THREAD_CREATE_ERROR);
156 	}
157 
158 	return (NSCD_SUCCESS);
159 }
160 
161 int
162 _nscd_get_smf_state(int srci, int dbi, int recheck)
163 {
164 	int	s;
165 	char	*n;
166 
167 	n = NSCD_NSW_SRC_NAME(srci);
168 
169 	/* the files, compat, and dns backends are always available */
170 	if ((*n == 'f' || *n == 'c' || *n == 'd' || *n == 'a') &&
171 	    (strcmp(NSCD_NSW_SRC_NAME(srci), "files") == 0 ||
172 	    strcmp(NSCD_NSW_SRC_NAME(srci), "compat") == 0 ||
173 	    strcmp(NSCD_NSW_SRC_NAME(srci), "ad") == 0 ||
174 	    strcmp(NSCD_NSW_SRC_NAME(srci), "dns") == 0)) {
175 		return (SCF_STATE_ONLINE);
176 	}
177 
178 	/*
179 	 * for the printer database and user backend, treat the
180 	 * backend as a unsupported one, as nscd can not access
181 	 * the home directory of the user
182 	 */
183 	if (*n == 'u' && strcmp(NSCD_NSW_SRC_NAME(srci), "user") == 0) {
184 		if (strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_PRINTERS) == 0)
185 			return (NSCD_SVC_STATE_UNSUPPORTED_SRC);
186 		else
187 			return (SCF_STATE_ONLINE);
188 	}
189 
190 	/*
191 	 * Foreign backend is not supported by nscd unless
192 	 * the backend supports the nss2 interface (global
193 	 * symbol _nss_<backname name>_version is present),
194 	 * tell the switch engine to return NSS_TRYLOCAL
195 	 * if needed via rc NSCD_SVC_STATE_FOREIGN_SRC.
196 	 */
197 	if (srci >= _nscd_cfg_num_nsw_src)
198 		return (NSCD_SVC_STATE_FOREIGN_SRC);
199 
200 	if (recheck == 1)
201 		return (query_smf_state(srci));
202 
203 	(void) rw_rdlock(&nscd_smf_service_state_lock);
204 	s = NSCD_SMF_SVC_STATE(srci);
205 	(void) rw_unlock(&nscd_smf_service_state_lock);
206 
207 	/*
208 	 * if the state has been queried at least once but is
209 	 * still not online, query one more time
210 	 */
211 	if (s != NSCD_SVC_STATE_UNINITED && s < SCF_STATE_ONLINE)
212 		s = query_smf_state(srci);
213 
214 	return (s);
215 }
216