8158 Want named threads API
[unleashed.git] / usr / src / cmd / nscd / nscd_smfmonitor.c
blobc5d5731ecd4c82aca132eae2b603c7b971e28977
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2018 Joyent, Inc.
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"
35 extern int _whoami;
38 * Service states monitored by nscd. Protected by
39 * readers/writer lock nscd_smf_service_state_lock
41 nscd_smf_state_t *nscd_smf_service_state;
42 static rwlock_t nscd_smf_service_state_lock = DEFAULTRWLOCK;
44 * init service state table
46 nscd_rc_t
47 _nscd_alloc_service_state_table()
49 int i;
51 nscd_smf_service_state = calloc(NSCD_NUM_SMF_FMRI,
52 sizeof (nscd_smf_state_t));
54 if (nscd_smf_service_state == NULL)
55 return (NSCD_NO_MEMORY);
57 for (i = 1; i < NSCD_NUM_SMF_FMRI; i++)
58 NSCD_SMF_SVC_STATE(i) = NSCD_SVC_STATE_UNINITED;
60 return (NSCD_SUCCESS);
63 static int
64 query_smf_state(int srci)
67 int ret = NSCD_SVC_STATE_UNINITED;
68 char *state = NULL;
69 char *me = "query_smf_state";
71 state = smf_get_state(NSCD_SMF_SVC_FMRI(srci));
72 if (state == NULL)
73 return (ret);
75 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
76 (me, "%s -- %s\n", state, NSCD_SMF_SVC_FMRI(srci));
78 (void) rw_wrlock(&nscd_smf_service_state_lock);
80 if (nscd_smf_service_state[srci].src_name == NULL)
81 nscd_smf_service_state[srci].src_name =
82 NSCD_NSW_SRC_NAME(srci);
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;
97 ret = NSCD_SMF_SVC_STATE(srci);
98 (void) rw_unlock(&nscd_smf_service_state_lock);
100 free(state);
101 return (ret);
104 /* ARGSUSED */
105 static void *
106 set_smf_state(void *arg)
109 int i;
110 int st;
112 (void) thr_setname(thr_self(), "set_smf_state");
115 * the forker nscd needs not monitor the state
116 * of the client services
118 if (_whoami == NSCD_FORKER)
119 thr_exit(0);
121 /*CONSTCOND*/
122 while (1) {
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;
131 (void) sleep(NSCD_SW_CFG_G.check_smf_state_interval_g);
133 /* NOTREACHED */
134 /*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
137 nscd_rc_t
138 _nscd_init_smf_monitor() {
140 int errnum;
141 char *me = "_nscd_init_smf_monitor";
143 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
144 (me, "initializing the smf monitor\n");
147 * start a thread to check the state of the client services
149 if (thr_create(NULL, NULL, set_smf_state,
150 NULL, THR_DETACHED, NULL) != 0) {
151 errnum = errno;
152 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_ERROR)
153 (me, "thr_create: %s\n", strerror(errnum));
154 return (NSCD_THREAD_CREATE_ERROR);
157 return (NSCD_SUCCESS);
161 _nscd_get_smf_state(int srci, int dbi, int recheck)
163 int s;
164 char *n;
166 n = NSCD_NSW_SRC_NAME(srci);
168 /* the files, compat, and dns backends are always available */
169 if ((*n == 'f' || *n == 'c' || *n == 'd' || *n == 'a') &&
170 (strcmp(NSCD_NSW_SRC_NAME(srci), "files") == 0 ||
171 strcmp(NSCD_NSW_SRC_NAME(srci), "compat") == 0 ||
172 strcmp(NSCD_NSW_SRC_NAME(srci), "ad") == 0 ||
173 strcmp(NSCD_NSW_SRC_NAME(srci), "dns") == 0)) {
174 return (SCF_STATE_ONLINE);
178 * for the printer database and user backend, treat the
179 * backend as a unsupported one, as nscd can not access
180 * the home directory of the user
182 if (*n == 'u' && strcmp(NSCD_NSW_SRC_NAME(srci), "user") == 0) {
183 if (strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_PRINTERS) == 0)
184 return (NSCD_SVC_STATE_UNSUPPORTED_SRC);
185 else
186 return (SCF_STATE_ONLINE);
190 * Foreign backend is not supported by nscd unless
191 * the backend supports the nss2 interface (global
192 * symbol _nss_<backname name>_version is present),
193 * tell the switch engine to return NSS_TRYLOCAL
194 * if needed via rc NSCD_SVC_STATE_FOREIGN_SRC.
196 if (srci >= _nscd_cfg_num_nsw_src)
197 return (NSCD_SVC_STATE_FOREIGN_SRC);
199 if (recheck == 1)
200 return (query_smf_state(srci));
202 (void) rw_rdlock(&nscd_smf_service_state_lock);
203 s = NSCD_SMF_SVC_STATE(srci);
204 (void) rw_unlock(&nscd_smf_service_state_lock);
207 * if the state has been queried at least once but is
208 * still not online, query one more time
210 if (s != NSCD_SVC_STATE_UNINITED && s < SCF_STATE_ONLINE)
211 s = query_smf_state(srci);
213 return (s);