Unleashed v1.4
[unleashed.git] / usr / src / cmd / nscd / nscd_smfmonitor.c
bloba38154557ca8c0f4269f9749cc132b6eeb22efae
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.
26 #include <stdlib.h>
27 #include <libscf.h>
28 #include <string.h>
29 #include "nscd_switch.h"
30 #include "nscd_log.h"
31 #include "nscd_door.h"
33 extern int _whoami;
36 * Service states monitored by nscd. Protected by
37 * readers/writer lock nscd_smf_service_state_lock
39 nscd_smf_state_t *nscd_smf_service_state;
40 static rwlock_t nscd_smf_service_state_lock = DEFAULTRWLOCK;
42 * init service state table
44 nscd_rc_t
45 _nscd_alloc_service_state_table()
47 int i;
49 nscd_smf_service_state = calloc(NSCD_NUM_SMF_FMRI,
50 sizeof (nscd_smf_state_t));
52 if (nscd_smf_service_state == NULL)
53 return (NSCD_NO_MEMORY);
55 for (i = 1; i < NSCD_NUM_SMF_FMRI; i++)
56 NSCD_SMF_SVC_STATE(i) = NSCD_SVC_STATE_UNINITED;
58 return (NSCD_SUCCESS);
61 static int
62 query_smf_state(int srci)
65 int ret = NSCD_SVC_STATE_UNINITED;
66 char *state = NULL;
67 char *me = "query_smf_state";
69 state = smf_get_state(NSCD_SMF_SVC_FMRI(srci));
70 if (state == NULL)
71 return (ret);
73 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
74 (me, "%s -- %s\n", state, NSCD_SMF_SVC_FMRI(srci));
76 (void) rw_wrlock(&nscd_smf_service_state_lock);
78 if (nscd_smf_service_state[srci].src_name == NULL)
79 nscd_smf_service_state[srci].src_name =
80 NSCD_NSW_SRC_NAME(srci);
82 if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
83 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_UNINIT;
84 else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
85 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_MAINT;
86 else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
87 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_OFFLINE;
88 else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
89 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_DISABLED;
90 else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
91 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_ONLINE;
92 else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
93 NSCD_SMF_SVC_STATE(srci) = SCF_STATE_DEGRADED;
95 ret = NSCD_SMF_SVC_STATE(srci);
96 (void) rw_unlock(&nscd_smf_service_state_lock);
98 free(state);
99 return (ret);
102 /* ARGSUSED */
103 static void *
104 set_smf_state(void *arg)
107 int i;
108 int st;
111 * the forker nscd needs not monitor the state
112 * of the client services
114 if (_whoami == NSCD_FORKER)
115 thr_exit(0);
117 /*CONSTCOND*/
118 while (1) {
120 /* skip the first service which is nscd */
121 for (i = 1; i < NSCD_NUM_SMF_FMRI; i++) {
122 st = query_smf_state(i);
123 if (st == NSCD_SVC_STATE_UNINITED)
124 break;
127 (void) sleep(NSCD_SW_CFG_G.check_smf_state_interval_g);
129 /* NOTREACHED */
130 /*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
133 nscd_rc_t
134 _nscd_init_smf_monitor() {
136 int errnum;
137 char *me = "_nscd_init_smf_monitor";
139 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_DEBUG)
140 (me, "initializing the smf monitor\n");
143 * start a thread to check the state of the client services
145 if (thr_create(NULL, 0, set_smf_state,
146 NULL, THR_DETACHED, NULL) != 0) {
147 errnum = errno;
148 _NSCD_LOG(NSCD_LOG_SMF_MONITOR, NSCD_LOG_LEVEL_ERROR)
149 (me, "thr_create: %s\n", strerror(errnum));
150 return (NSCD_THREAD_CREATE_ERROR);
153 return (NSCD_SUCCESS);
157 _nscd_get_smf_state(int srci, int dbi, int recheck)
159 int s;
160 char *n;
162 n = NSCD_NSW_SRC_NAME(srci);
164 /* the files, compat, and dns backends are always available */
165 if ((*n == 'f' || *n == 'c' || *n == 'd' || *n == 'a') &&
166 (strcmp(NSCD_NSW_SRC_NAME(srci), "files") == 0 ||
167 strcmp(NSCD_NSW_SRC_NAME(srci), "compat") == 0 ||
168 strcmp(NSCD_NSW_SRC_NAME(srci), "ad") == 0 ||
169 strcmp(NSCD_NSW_SRC_NAME(srci), "dns") == 0)) {
170 return (SCF_STATE_ONLINE);
174 * for the printer database and user backend, treat the
175 * backend as a unsupported one, as nscd can not access
176 * the home directory of the user
178 if (*n == 'u' && strcmp(NSCD_NSW_SRC_NAME(srci), "user") == 0) {
179 if (strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_PRINTERS) == 0)
180 return (NSCD_SVC_STATE_UNSUPPORTED_SRC);
181 else
182 return (SCF_STATE_ONLINE);
186 * Foreign backend is not supported by nscd unless
187 * the backend supports the nss2 interface (global
188 * symbol _nss_<backname name>_version is present),
189 * tell the switch engine to return NSS_TRYLOCAL
190 * if needed via rc NSCD_SVC_STATE_FOREIGN_SRC.
192 if (srci >= _nscd_cfg_num_nsw_src)
193 return (NSCD_SVC_STATE_FOREIGN_SRC);
195 if (recheck == 1)
196 return (query_smf_state(srci));
198 (void) rw_rdlock(&nscd_smf_service_state_lock);
199 s = NSCD_SMF_SVC_STATE(srci);
200 (void) rw_unlock(&nscd_smf_service_state_lock);
203 * if the state has been queried at least once but is
204 * still not online, query one more time
206 if (s != NSCD_SVC_STATE_UNINITED && s < SCF_STATE_ONLINE)
207 s = query_smf_state(srci);
209 return (s);