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