xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_conf.c (revision 11202:9f0b4aa8d573)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11202SStephen.Hanson@Sun.COM  * Common Development and Distribution License (the "License").
6*11202SStephen.Hanson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211414Scindi 
220Sstevel@tonic-gate /*
23*11202SStephen.Hanson@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <pthread.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <signal.h>
300Sstevel@tonic-gate #include <inttypes.h>
310Sstevel@tonic-gate #include <alloca.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <fmd_conf.h>
370Sstevel@tonic-gate #include <fmd_alloc.h>
380Sstevel@tonic-gate #include <fmd_error.h>
390Sstevel@tonic-gate #include <fmd_subr.h>
400Sstevel@tonic-gate #include <fmd_string.h>
410Sstevel@tonic-gate #include <fmd.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate const char FMD_PROP_SUBSCRIPTIONS[] = "_subscriptions";
440Sstevel@tonic-gate const char FMD_PROP_DICTIONARIES[] = "_dictionaries";
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * The property formals defined in _fmd_conf_defv[] are added to every config
480Sstevel@tonic-gate  * dictionary that is created.  Here we define several special FMD_PROP_*
490Sstevel@tonic-gate  * properties that are used to implement the config file keyword actions, as
500Sstevel@tonic-gate  * well as properties that should be inherited by fmd_conf_t's from fmd.d_conf.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static const fmd_conf_formal_t _fmd_conf_defv[] = {
530Sstevel@tonic-gate 	{ FMD_PROP_SUBSCRIPTIONS, &fmd_conf_list, "" },
540Sstevel@tonic-gate 	{ FMD_PROP_DICTIONARIES, &fmd_conf_list, "" },
550Sstevel@tonic-gate 	{ "fmd.isaname", &fmd_conf_parent, "isaname" },
560Sstevel@tonic-gate 	{ "fmd.machine", &fmd_conf_parent, "machine" },
570Sstevel@tonic-gate 	{ "fmd.platform", &fmd_conf_parent, "platform" },
580Sstevel@tonic-gate 	{ "fmd.rootdir", &fmd_conf_parent, "rootdir" },
590Sstevel@tonic-gate };
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static const int _fmd_conf_defc =
620Sstevel@tonic-gate     sizeof (_fmd_conf_defv) / sizeof (_fmd_conf_defv[0]);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static int
set_bool(fmd_conf_param_t * pp,const char * s)650Sstevel@tonic-gate set_bool(fmd_conf_param_t *pp, const char *s)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	if (strcasecmp(s, "true") == 0)
680Sstevel@tonic-gate 		pp->cp_value.cpv_num = 1;
690Sstevel@tonic-gate 	else if (strcasecmp(s, "false") == 0)
700Sstevel@tonic-gate 		pp->cp_value.cpv_num = 0;
710Sstevel@tonic-gate 	else
720Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	return (0);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static void
get_bool(const fmd_conf_param_t * pp,void * ptr)780Sstevel@tonic-gate get_bool(const fmd_conf_param_t *pp, void *ptr)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	*((int *)ptr) = (int)pp->cp_value.cpv_num;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static int
set_i32x(fmd_conf_param_t * pp,const char * s,int64_t min,int64_t max)841193Smws set_i32x(fmd_conf_param_t *pp, const char *s, int64_t min, int64_t max)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	int64_t val;
870Sstevel@tonic-gate 	char *end;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	errno = 0;
900Sstevel@tonic-gate 	val = strtoll(s, &end, 0);
910Sstevel@tonic-gate 
921193Smws 	if (errno == EOVERFLOW || val < min || val > max)
930Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (errno != 0 || end == s || *end != '\0')
960Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
990Sstevel@tonic-gate 	return (0);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1021193Smws static int
set_i8(fmd_conf_param_t * pp,const char * s)1031193Smws set_i8(fmd_conf_param_t *pp, const char *s)
1041193Smws {
1051193Smws 	return (set_i32x(pp, s, INT8_MIN, INT8_MAX));
1061193Smws }
1071193Smws 
1081193Smws static int
set_i16(fmd_conf_param_t * pp,const char * s)1091193Smws set_i16(fmd_conf_param_t *pp, const char *s)
1101193Smws {
1111193Smws 	return (set_i32x(pp, s, INT16_MIN, INT16_MAX));
1121193Smws }
1131193Smws 
1141193Smws static int
set_i32(fmd_conf_param_t * pp,const char * s)1151193Smws set_i32(fmd_conf_param_t *pp, const char *s)
1161193Smws {
1171193Smws 	return (set_i32x(pp, s, INT32_MIN, INT32_MAX));
1181193Smws }
1191193Smws 
1200Sstevel@tonic-gate static void
get_i32(const fmd_conf_param_t * pp,void * ptr)1210Sstevel@tonic-gate get_i32(const fmd_conf_param_t *pp, void *ptr)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	*((int32_t *)ptr) = (int32_t)pp->cp_value.cpv_num;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate static int
set_ui32x(fmd_conf_param_t * pp,const char * s,uint64_t max)1271193Smws set_ui32x(fmd_conf_param_t *pp, const char *s, uint64_t max)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	uint64_t val;
1300Sstevel@tonic-gate 	char *end;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	errno = 0;
1330Sstevel@tonic-gate 	val = strtoull(s, &end, 0);
1340Sstevel@tonic-gate 
1351193Smws 	if (errno == EOVERFLOW || val > max)
1360Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (errno != 0 || end == s || *end != '\0')
1390Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
1420Sstevel@tonic-gate 	return (0);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1451193Smws static int
set_ui8(fmd_conf_param_t * pp,const char * s)1461193Smws set_ui8(fmd_conf_param_t *pp, const char *s)
1471193Smws {
1481193Smws 	return (set_ui32x(pp, s, UINT8_MAX));
1491193Smws }
1501193Smws 
1511193Smws static int
set_ui16(fmd_conf_param_t * pp,const char * s)1521193Smws set_ui16(fmd_conf_param_t *pp, const char *s)
1531193Smws {
1541193Smws 	return (set_ui32x(pp, s, UINT16_MAX));
1551193Smws }
1561193Smws 
1571193Smws static int
set_ui32(fmd_conf_param_t * pp,const char * s)1581193Smws set_ui32(fmd_conf_param_t *pp, const char *s)
1591193Smws {
1601193Smws 	return (set_ui32x(pp, s, UINT32_MAX));
1611193Smws }
1621193Smws 
1630Sstevel@tonic-gate static void
get_ui32(const fmd_conf_param_t * pp,void * ptr)1640Sstevel@tonic-gate get_ui32(const fmd_conf_param_t *pp, void *ptr)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	*((uint32_t *)ptr) = (uint32_t)pp->cp_value.cpv_num;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static int
set_i64(fmd_conf_param_t * pp,const char * s)1700Sstevel@tonic-gate set_i64(fmd_conf_param_t *pp, const char *s)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int64_t val;
1730Sstevel@tonic-gate 	char *end;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	errno = 0;
1760Sstevel@tonic-gate 	val = strtoll(s, &end, 0);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (errno == EOVERFLOW)
1790Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (errno != 0 || end == s || *end != '\0')
1820Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
1850Sstevel@tonic-gate 	return (0);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static void
get_i64(const fmd_conf_param_t * pp,void * ptr)1890Sstevel@tonic-gate get_i64(const fmd_conf_param_t *pp, void *ptr)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	*((int64_t *)ptr) = (int64_t)pp->cp_value.cpv_num;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
set_ui64(fmd_conf_param_t * pp,const char * s)1950Sstevel@tonic-gate set_ui64(fmd_conf_param_t *pp, const char *s)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	uint64_t val;
1980Sstevel@tonic-gate 	char *end;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	errno = 0;
2010Sstevel@tonic-gate 	val = strtoull(s, &end, 0);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (errno == EOVERFLOW)
2040Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (errno != 0 || end == s || *end != '\0')
2070Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
2100Sstevel@tonic-gate 	return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static void
get_ui64(const fmd_conf_param_t * pp,void * ptr)2140Sstevel@tonic-gate get_ui64(const fmd_conf_param_t *pp, void *ptr)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	*((uint64_t *)ptr) = pp->cp_value.cpv_num;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static int
set_str(fmd_conf_param_t * pp,const char * s)2200Sstevel@tonic-gate set_str(fmd_conf_param_t *pp, const char *s)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	fmd_strfree(pp->cp_value.cpv_str);
2230Sstevel@tonic-gate 	pp->cp_value.cpv_str = fmd_strdup(s, FMD_SLEEP);
2240Sstevel@tonic-gate 	return (0);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static void
get_str(const fmd_conf_param_t * pp,void * ptr)2280Sstevel@tonic-gate get_str(const fmd_conf_param_t *pp, void *ptr)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	*((const char **)ptr) = pp->cp_value.cpv_str;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static void
free_str(fmd_conf_param_t * pp)2340Sstevel@tonic-gate free_str(fmd_conf_param_t *pp)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	fmd_strfree(pp->cp_value.cpv_str);
2370Sstevel@tonic-gate 	pp->cp_value.cpv_str = NULL;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static int
set_path(fmd_conf_param_t * pp,const char * value)2410Sstevel@tonic-gate set_path(fmd_conf_param_t *pp, const char *value)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	size_t len = strlen(value);
2440Sstevel@tonic-gate 	char *s = alloca(len + 1);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	char **patv = alloca(sizeof (char *) * len / 2);
2470Sstevel@tonic-gate 	int patc = 0;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	static const char *const percent_sign = "%";
2500Sstevel@tonic-gate 	char *p, *q;
2510Sstevel@tonic-gate 	int c, i;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	static const struct fmd_conf_token {
2540Sstevel@tonic-gate 		char tok_tag;
2550Sstevel@tonic-gate 		const char *const *tok_val;
2560Sstevel@tonic-gate 	} tokens[] = {
2570Sstevel@tonic-gate 		{ 'i', &fmd.d_platform },
2580Sstevel@tonic-gate 		{ 'm', &fmd.d_machine },
2590Sstevel@tonic-gate 		{ 'p', &fmd.d_isaname },
2600Sstevel@tonic-gate 		{ 'r', &fmd.d_rootdir },
2610Sstevel@tonic-gate 		{ '%', &percent_sign },
2620Sstevel@tonic-gate 		{ 0, NULL }
2630Sstevel@tonic-gate 	};
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	const struct fmd_conf_token *tok;
2660Sstevel@tonic-gate 	fmd_conf_path_t *pap;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	pp->cp_formal->cf_ops->co_free(pp);
2690Sstevel@tonic-gate 	(void) strcpy(s, value);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	for (p = strtok_r(s, ":", &q); p != NULL; p = strtok_r(NULL, ":", &q))
2720Sstevel@tonic-gate 		patv[patc++] = p;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	pap = fmd_alloc(sizeof (fmd_conf_path_t), FMD_SLEEP);
2750Sstevel@tonic-gate 	pap->cpa_argv = fmd_alloc(sizeof (char *) * patc, FMD_SLEEP);
2760Sstevel@tonic-gate 	pap->cpa_argc = patc;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	for (i = 0; i < patc; i++) {
2790Sstevel@tonic-gate 		for (len = 0, p = patv[i]; (c = *p) != '\0'; p++, len++) {
2800Sstevel@tonic-gate 			if (c != '%' || (c = p[1]) == '\0')
2810Sstevel@tonic-gate 				continue;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			for (tok = tokens; tok->tok_tag != 0; tok++) {
2840Sstevel@tonic-gate 				if (c == tok->tok_tag) {
2850Sstevel@tonic-gate 					len += strlen(*tok->tok_val) - 1;
2860Sstevel@tonic-gate 					p++;
2870Sstevel@tonic-gate 					break;
2880Sstevel@tonic-gate 				}
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		pap->cpa_argv[i] = q = fmd_alloc(len + 1, FMD_SLEEP);
2930Sstevel@tonic-gate 		q[len] = '\0';
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		for (p = patv[i]; (c = *p) != '\0'; p++) {
2960Sstevel@tonic-gate 			if (c != '%' || (c = p[1]) == '\0') {
2970Sstevel@tonic-gate 				*q++ = c;
2980Sstevel@tonic-gate 				continue;
2990Sstevel@tonic-gate 			}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			for (tok = tokens; tok->tok_tag != 0; tok++) {
3020Sstevel@tonic-gate 				if (c == tok->tok_tag) {
3030Sstevel@tonic-gate 					(void) strcpy(q, *tok->tok_val);
3040Sstevel@tonic-gate 					q += strlen(q);
3050Sstevel@tonic-gate 					p++;
3060Sstevel@tonic-gate 					break;
3070Sstevel@tonic-gate 				}
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			if (tok->tok_tag == 0)
3110Sstevel@tonic-gate 				*q++ = c;
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = pap;
3160Sstevel@tonic-gate 	return (0);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static int
set_lst(fmd_conf_param_t * pp,const char * value)3200Sstevel@tonic-gate set_lst(fmd_conf_param_t *pp, const char *value)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	fmd_conf_path_t *old;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	old = pp->cp_value.cpv_ptr;
3250Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = NULL;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (set_path(pp, value) != 0) {
3280Sstevel@tonic-gate 		pp->cp_value.cpv_ptr = old;
3290Sstevel@tonic-gate 		return (-1); /* errno is set for us */
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (old != NULL) {
3330Sstevel@tonic-gate 		fmd_conf_path_t *new = pp->cp_value.cpv_ptr;
3340Sstevel@tonic-gate 		int i, totc = old->cpa_argc + new->cpa_argc;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 		int new_argc = new->cpa_argc;
3370Sstevel@tonic-gate 		const char **new_argv = new->cpa_argv;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		new->cpa_argc = 0;
3400Sstevel@tonic-gate 		new->cpa_argv = fmd_alloc(sizeof (char *) * totc, FMD_SLEEP);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		for (i = 0; i < old->cpa_argc; i++)
3430Sstevel@tonic-gate 			new->cpa_argv[new->cpa_argc++] = old->cpa_argv[i];
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		for (i = 0; i < new_argc; i++)
3460Sstevel@tonic-gate 			new->cpa_argv[new->cpa_argc++] = new_argv[i];
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		ASSERT(new->cpa_argc == totc);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		fmd_free(new_argv, sizeof (char *) * new_argc);
3510Sstevel@tonic-gate 		fmd_free(old->cpa_argv, sizeof (char *) * old->cpa_argc);
3520Sstevel@tonic-gate 		fmd_free(old, sizeof (fmd_conf_path_t));
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	return (0);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate static int
del_lst(fmd_conf_param_t * pp,const char * value)3590Sstevel@tonic-gate del_lst(fmd_conf_param_t *pp, const char *value)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	fmd_conf_path_t *pap = pp->cp_value.cpv_ptr;
3620Sstevel@tonic-gate 	const char **new_argv;
3630Sstevel@tonic-gate 	int i, new_argc;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
3660Sstevel@tonic-gate 		if (strcmp(pap->cpa_argv[i], value) == 0)
3670Sstevel@tonic-gate 			break;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (i == pap->cpa_argc)
3710Sstevel@tonic-gate 		return (fmd_set_errno(ENOENT));
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	fmd_strfree((char *)pap->cpa_argv[i]);
3740Sstevel@tonic-gate 	pap->cpa_argv[i] = NULL;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	new_argc = 0;
3770Sstevel@tonic-gate 	new_argv = fmd_alloc(sizeof (char *) * (pap->cpa_argc - 1), FMD_SLEEP);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
3800Sstevel@tonic-gate 		if (pap->cpa_argv[i] != NULL)
3810Sstevel@tonic-gate 			new_argv[new_argc++] = pap->cpa_argv[i];
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	fmd_free(pap->cpa_argv, sizeof (char *) * pap->cpa_argc);
3850Sstevel@tonic-gate 	pap->cpa_argv = new_argv;
3860Sstevel@tonic-gate 	pap->cpa_argc = new_argc;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static void
get_path(const fmd_conf_param_t * pp,void * ptr)3920Sstevel@tonic-gate get_path(const fmd_conf_param_t *pp, void *ptr)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	*((fmd_conf_path_t **)ptr) = (fmd_conf_path_t *)pp->cp_value.cpv_ptr;
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static void
free_path(fmd_conf_param_t * pp)3980Sstevel@tonic-gate free_path(fmd_conf_param_t *pp)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	fmd_conf_path_t *pap = pp->cp_value.cpv_ptr;
4010Sstevel@tonic-gate 	int i;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (pap == NULL)
4040Sstevel@tonic-gate 		return; /* no value was ever set */
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++)
4070Sstevel@tonic-gate 		fmd_strfree((char *)pap->cpa_argv[i]);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	fmd_free(pap->cpa_argv, sizeof (char *) * pap->cpa_argc);
4100Sstevel@tonic-gate 	fmd_free(pap, sizeof (fmd_conf_path_t));
4110Sstevel@tonic-gate 	pp->cp_value.cpv_ptr = NULL;
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate static int
set_time(fmd_conf_param_t * pp,const char * s)4150Sstevel@tonic-gate set_time(fmd_conf_param_t *pp, const char *s)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	static const struct {
4180Sstevel@tonic-gate 		const char *name;
4190Sstevel@tonic-gate 		hrtime_t mul;
4200Sstevel@tonic-gate 	} suffix[] = {
4210Sstevel@tonic-gate 		{ "ns", 	NANOSEC / NANOSEC },
4220Sstevel@tonic-gate 		{ "nsec",	NANOSEC / NANOSEC },
4230Sstevel@tonic-gate 		{ "us",		NANOSEC / MICROSEC },
4240Sstevel@tonic-gate 		{ "usec",	NANOSEC / MICROSEC },
4250Sstevel@tonic-gate 		{ "ms",		NANOSEC / MILLISEC },
4260Sstevel@tonic-gate 		{ "msec",	NANOSEC / MILLISEC },
4270Sstevel@tonic-gate 		{ "s",		NANOSEC / SEC },
4280Sstevel@tonic-gate 		{ "sec",	NANOSEC / SEC },
4290Sstevel@tonic-gate 		{ "m",		NANOSEC * (hrtime_t)60 },
4300Sstevel@tonic-gate 		{ "min",	NANOSEC * (hrtime_t)60 },
4310Sstevel@tonic-gate 		{ "h",		NANOSEC * (hrtime_t)(60 * 60) },
4320Sstevel@tonic-gate 		{ "hour",	NANOSEC * (hrtime_t)(60 * 60) },
4330Sstevel@tonic-gate 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
4340Sstevel@tonic-gate 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
4350Sstevel@tonic-gate 		{ "hz",		0 },
4360Sstevel@tonic-gate 		{ NULL }
4370Sstevel@tonic-gate 	};
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	hrtime_t val, mul = 1;
4400Sstevel@tonic-gate 	char *end;
4410Sstevel@tonic-gate 	int i;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	errno = 0;
4440Sstevel@tonic-gate 	val = strtoull(s, &end, 0);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (errno == EOVERFLOW)
4470Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (errno != 0 || end == s)
4500Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	for (i = 0; suffix[i].name != NULL; i++) {
4530Sstevel@tonic-gate 		if (strcasecmp(suffix[i].name, end) == 0) {
4540Sstevel@tonic-gate 			mul = suffix[i].mul;
4550Sstevel@tonic-gate 			break;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (suffix[i].name == NULL && *end != '\0')
4600Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	if (mul == 0) {
4630Sstevel@tonic-gate 		if (val != 0)
4640Sstevel@tonic-gate 			val = NANOSEC / val; /* compute val as value per sec */
4650Sstevel@tonic-gate 	} else
4660Sstevel@tonic-gate 		val *= mul;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
4690Sstevel@tonic-gate 	return (0);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static int
set_size(fmd_conf_param_t * pp,const char * s)4730Sstevel@tonic-gate set_size(fmd_conf_param_t *pp, const char *s)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	size_t len = strlen(s);
4760Sstevel@tonic-gate 	uint64_t val, mul = 1;
4770Sstevel@tonic-gate 	char *end;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	switch (s[len - 1]) {
4800Sstevel@tonic-gate 	case 't':
4810Sstevel@tonic-gate 	case 'T':
4820Sstevel@tonic-gate 		mul *= 1024;
4830Sstevel@tonic-gate 		/*FALLTHRU*/
4840Sstevel@tonic-gate 	case 'g':
4850Sstevel@tonic-gate 	case 'G':
4860Sstevel@tonic-gate 		mul *= 1024;
4870Sstevel@tonic-gate 		/*FALLTHRU*/
4880Sstevel@tonic-gate 	case 'm':
4890Sstevel@tonic-gate 	case 'M':
4900Sstevel@tonic-gate 		mul *= 1024;
4910Sstevel@tonic-gate 		/*FALLTHRU*/
4920Sstevel@tonic-gate 	case 'k':
4930Sstevel@tonic-gate 	case 'K':
4940Sstevel@tonic-gate 		mul *= 1024;
4950Sstevel@tonic-gate 		/*FALLTHRU*/
4960Sstevel@tonic-gate 	default:
4970Sstevel@tonic-gate 		break;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	errno = 0;
5010Sstevel@tonic-gate 	val = strtoull(s, &end, 0) * mul;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (errno == EOVERFLOW)
5040Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OVERFLOW));
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if ((mul != 1 && end != &s[len - 1]) ||
5070Sstevel@tonic-gate 	    (mul == 1 && *end != '\0') || errno != 0)
5080Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	pp->cp_value.cpv_num = val;
5110Sstevel@tonic-gate 	return (0);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate static int
set_sig(fmd_conf_param_t * pp,const char * s)5150Sstevel@tonic-gate set_sig(fmd_conf_param_t *pp, const char *s)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	int sig;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (strncasecmp(s, "SIG", 3) == 0)
5200Sstevel@tonic-gate 		s += 3; /* be friendlier than strsig() and permit the prefix */
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (str2sig(s, &sig) != 0)
5230Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_INVAL));
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	pp->cp_value.cpv_num = sig;
5260Sstevel@tonic-gate 	return (0);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate static void
get_par(const fmd_conf_param_t * pp,void * ptr)5300Sstevel@tonic-gate get_par(const fmd_conf_param_t *pp, void *ptr)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	if (fmd_conf_getprop(fmd.d_conf, pp->cp_formal->cf_default, ptr) != 0) {
5330Sstevel@tonic-gate 		fmd_panic("fmd.d_conf does not define '%s' (inherited as %s)\n",
5340Sstevel@tonic-gate 		    (char *)pp->cp_formal->cf_default, pp->cp_formal->cf_name);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*ARGSUSED*/
5390Sstevel@tonic-gate static int
set_par(fmd_conf_param_t * pp,const char * s)5400Sstevel@tonic-gate set_par(fmd_conf_param_t *pp, const char *s)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	return (fmd_set_errno(EFMD_CONF_RDONLY));
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * Utility routine for callers who define custom ops where a list of string
5470Sstevel@tonic-gate  * tokens are translated into a bitmask.  'cmp' should be set to point to an
5480Sstevel@tonic-gate  * array of fmd_conf_mode_t's where the final element has cm_name == NULL.
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate int
fmd_conf_mode_set(const fmd_conf_mode_t * cma,fmd_conf_param_t * pp,const char * value)5511193Smws fmd_conf_mode_set(const fmd_conf_mode_t *cma,
5520Sstevel@tonic-gate     fmd_conf_param_t *pp, const char *value)
5530Sstevel@tonic-gate {
5541193Smws 	const fmd_conf_mode_t *cmp;
5550Sstevel@tonic-gate 	char *p, *q, *s = fmd_strdup(value, FMD_SLEEP);
5560Sstevel@tonic-gate 	size_t len = value ? strlen(value) + 1 : 0;
5570Sstevel@tonic-gate 	uint_t mode = 0;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (s == NULL) {
5600Sstevel@tonic-gate 		pp->cp_value.cpv_num = 0;
5610Sstevel@tonic-gate 		return (0);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	for (p = strtok_r(s, ",", &q); p != NULL; p = strtok_r(NULL, ",", &q)) {
5651193Smws 		for (cmp = cma; cmp->cm_name != NULL; cmp++) {
5660Sstevel@tonic-gate 			if (strcmp(cmp->cm_name, p) == 0) {
5670Sstevel@tonic-gate 				mode |= cmp->cm_bits;
5680Sstevel@tonic-gate 				break;
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (cmp->cm_name == NULL) {
5730Sstevel@tonic-gate 			fmd_free(s, len);
5740Sstevel@tonic-gate 			return (fmd_set_errno(EFMD_CONF_INVAL));
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	pp->cp_value.cpv_num = mode;
5790Sstevel@tonic-gate 	fmd_free(s, len);
5800Sstevel@tonic-gate 	return (0);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate void
fmd_conf_mode_get(const fmd_conf_param_t * pp,void * ptr)5840Sstevel@tonic-gate fmd_conf_mode_get(const fmd_conf_param_t *pp, void *ptr)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	*((uint_t *)ptr) = (uint_t)pp->cp_value.cpv_num;
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*ARGSUSED*/
5900Sstevel@tonic-gate int
fmd_conf_notsup(fmd_conf_param_t * pp,const char * value)5910Sstevel@tonic-gate fmd_conf_notsup(fmd_conf_param_t *pp, const char *value)
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate 	return (fmd_set_errno(ENOTSUP));
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*ARGSUSED*/
5970Sstevel@tonic-gate void
fmd_conf_nop(fmd_conf_param_t * pp)5980Sstevel@tonic-gate fmd_conf_nop(fmd_conf_param_t *pp)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	/* no free required for integer-type parameters */
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate #define	CONF_DEFINE(name, a, b, c, d) \
6040Sstevel@tonic-gate 	const fmd_conf_ops_t name = { a, b, c, d }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate CONF_DEFINE(fmd_conf_bool, set_bool, get_bool, fmd_conf_notsup, fmd_conf_nop);
6071193Smws CONF_DEFINE(fmd_conf_int8, set_i8, get_i32, fmd_conf_notsup, fmd_conf_nop);
6081193Smws CONF_DEFINE(fmd_conf_uint8, set_ui8, get_ui32, fmd_conf_notsup, fmd_conf_nop);
6091193Smws CONF_DEFINE(fmd_conf_int16, set_i16, get_i32, fmd_conf_notsup, fmd_conf_nop);
6101193Smws CONF_DEFINE(fmd_conf_uint16, set_ui16, get_ui32, fmd_conf_notsup, fmd_conf_nop);
6110Sstevel@tonic-gate CONF_DEFINE(fmd_conf_int32, set_i32, get_i32, fmd_conf_notsup, fmd_conf_nop);
6120Sstevel@tonic-gate CONF_DEFINE(fmd_conf_uint32, set_ui32, get_ui32, fmd_conf_notsup, fmd_conf_nop);
6130Sstevel@tonic-gate CONF_DEFINE(fmd_conf_int64, set_i64, get_i64, fmd_conf_notsup, fmd_conf_nop);
6140Sstevel@tonic-gate CONF_DEFINE(fmd_conf_uint64, set_ui64, get_ui64, fmd_conf_notsup, fmd_conf_nop);
6150Sstevel@tonic-gate CONF_DEFINE(fmd_conf_string, set_str, get_str, fmd_conf_notsup, free_str);
6160Sstevel@tonic-gate CONF_DEFINE(fmd_conf_path, set_path, get_path, fmd_conf_notsup, free_path);
6170Sstevel@tonic-gate CONF_DEFINE(fmd_conf_list, set_lst, get_path, del_lst, free_path);
6180Sstevel@tonic-gate CONF_DEFINE(fmd_conf_time, set_time, get_ui64, fmd_conf_notsup, fmd_conf_nop);
6190Sstevel@tonic-gate CONF_DEFINE(fmd_conf_size, set_size, get_ui64, fmd_conf_notsup, fmd_conf_nop);
6200Sstevel@tonic-gate CONF_DEFINE(fmd_conf_signal, set_sig, get_i32, fmd_conf_notsup, fmd_conf_nop);
6210Sstevel@tonic-gate CONF_DEFINE(fmd_conf_parent, set_par, get_par, fmd_conf_notsup, fmd_conf_nop);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate static char *
fmd_conf_skipstr(char * s)6240Sstevel@tonic-gate fmd_conf_skipstr(char *s)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	int c;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	while ((c = *s) != '\0') {
6290Sstevel@tonic-gate 		if (c == '\\')
6300Sstevel@tonic-gate 			s++;
6310Sstevel@tonic-gate 		else if (c == '"')
6320Sstevel@tonic-gate 			break;
6330Sstevel@tonic-gate 		s++;
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	return (s);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate static char *
fmd_conf_skipnws(char * s)6400Sstevel@tonic-gate fmd_conf_skipnws(char *s)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	while (strchr("\f\n\r\t\v ", *s) == NULL)
6430Sstevel@tonic-gate 		s++;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	return (s);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate static int
fmd_conf_tokenize(char * s,char * tokv[])6490Sstevel@tonic-gate fmd_conf_tokenize(char *s, char *tokv[])
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	int c, tokc = 0;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	while ((c = *s) != '\0') {
6540Sstevel@tonic-gate 		switch (c) {
6550Sstevel@tonic-gate 		case '"':
6560Sstevel@tonic-gate 			tokv[tokc] = s + 1;
6570Sstevel@tonic-gate 			s = fmd_conf_skipstr(s + 1);
6580Sstevel@tonic-gate 			*s++ = '\0';
6590Sstevel@tonic-gate 			(void) fmd_stresc2chr(tokv[tokc++]);
6600Sstevel@tonic-gate 			continue;
6610Sstevel@tonic-gate 		case '\f': case '\n': case '\r':
6620Sstevel@tonic-gate 		case '\t': case '\v': case ' ':
6630Sstevel@tonic-gate 			s++;
6640Sstevel@tonic-gate 			continue;
6650Sstevel@tonic-gate 		default:
6660Sstevel@tonic-gate 			tokv[tokc++] = s;
6670Sstevel@tonic-gate 			s = fmd_conf_skipnws(s);
6680Sstevel@tonic-gate 			*s++ = '\0';
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	return (tokc);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate static int
fmd_conf_exec_setprop(fmd_conf_t * cfp,int argc,char * argv[])6760Sstevel@tonic-gate fmd_conf_exec_setprop(fmd_conf_t *cfp, int argc, char *argv[])
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	if (argc != 2)
6790Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_USAGE));
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	return (fmd_conf_setprop(cfp, argv[0], argv[1]));
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate static int
fmd_conf_exec_subscribe(fmd_conf_t * cfp,int argc,char * argv[])6850Sstevel@tonic-gate fmd_conf_exec_subscribe(fmd_conf_t *cfp, int argc, char *argv[])
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	if (argc != 1)
6880Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_USAGE));
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	return (fmd_conf_setprop(cfp, FMD_PROP_SUBSCRIPTIONS, argv[0]));
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate static int
fmd_conf_exec_dictionary(fmd_conf_t * cfp,int argc,char * argv[])6940Sstevel@tonic-gate fmd_conf_exec_dictionary(fmd_conf_t *cfp, int argc, char *argv[])
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	if (argc != 1)
6970Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_USAGE));
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	return (fmd_conf_setprop(cfp, FMD_PROP_DICTIONARIES, argv[0]));
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate static int
fmd_conf_parse(fmd_conf_t * cfp,const char * file)7030Sstevel@tonic-gate fmd_conf_parse(fmd_conf_t *cfp, const char *file)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	static const fmd_conf_verb_t verbs[] = {
7060Sstevel@tonic-gate 		{ "setprop", fmd_conf_exec_setprop },
7070Sstevel@tonic-gate 		{ "subscribe", fmd_conf_exec_subscribe },
7080Sstevel@tonic-gate 		{ "dictionary", fmd_conf_exec_dictionary },
7090Sstevel@tonic-gate 		{ NULL, NULL }
7100Sstevel@tonic-gate 	};
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	int line, errs = 0;
7130Sstevel@tonic-gate 	char buf[BUFSIZ];
7140Sstevel@tonic-gate 	FILE *fp;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
717*11202SStephen.Hanson@Sun.COM 		if (errno == EMFILE)
718*11202SStephen.Hanson@Sun.COM 			fmd_error(EFMD_EXIT, "failed to open %s: %s\n",
719*11202SStephen.Hanson@Sun.COM 			    file, fmd_strerror(errno));
720*11202SStephen.Hanson@Sun.COM 		else
721*11202SStephen.Hanson@Sun.COM 			fmd_error(EFMD_CONF_OPEN, "failed to open %s: %s\n",
722*11202SStephen.Hanson@Sun.COM 			    file, fmd_strerror(errno));
7230Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_OPEN));
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	for (line = 1; fgets(buf, sizeof (buf), fp) != NULL; line++) {
7270Sstevel@tonic-gate 		char *tokv[sizeof (buf) / 2 + 1];
7280Sstevel@tonic-gate 		int tokc = fmd_conf_tokenize(buf, tokv);
7290Sstevel@tonic-gate 		const fmd_conf_verb_t *vp;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		if (tokc == 0 || tokv[0][0] == '#')
7320Sstevel@tonic-gate 			continue; /* skip blank lines and comment lines */
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 		for (vp = verbs; vp->cv_name != NULL; vp++) {
7350Sstevel@tonic-gate 			if (strcmp(tokv[0], vp->cv_name) == 0)
7360Sstevel@tonic-gate 				break;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		if (vp->cv_name == NULL) {
7400Sstevel@tonic-gate 			fmd_error(EFMD_CONF_KEYWORD, "\"%s\", line %d: "
7410Sstevel@tonic-gate 			    "invalid configuration file keyword: %s\n",
7420Sstevel@tonic-gate 			    file, line, tokv[0]);
7430Sstevel@tonic-gate 			errs++;
7440Sstevel@tonic-gate 			continue;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		if (vp->cv_exec(cfp, tokc - 1, tokv + 1) != 0) {
7480Sstevel@tonic-gate 			fmd_error(errno, "\"%s\", line %d", file, line);
7490Sstevel@tonic-gate 			errs++;
7500Sstevel@tonic-gate 			continue;
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (ferror(fp) != 0 || fclose(fp) != 0)
7550Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_IO));
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (errs != 0)
7580Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_CONF_ERRS));
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	return (0);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate static void
fmd_conf_fill(fmd_conf_t * cfp,fmd_conf_param_t * ppbuf,int argc,const fmd_conf_formal_t * argv,int checkid)7640Sstevel@tonic-gate fmd_conf_fill(fmd_conf_t *cfp, fmd_conf_param_t *ppbuf,
7650Sstevel@tonic-gate     int argc, const fmd_conf_formal_t *argv, int checkid)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	int i;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	for (i = 0; i < argc; i++, argv++) {
7700Sstevel@tonic-gate 		fmd_conf_param_t *op, *pp = ppbuf + i;
7710Sstevel@tonic-gate 		const char *name = argv->cf_name;
7720Sstevel@tonic-gate 		ulong_t h = fmd_strhash(name) % cfp->cf_parhashlen;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		if (fmd_strbadid(name, checkid) != NULL) {
7750Sstevel@tonic-gate 			fmd_error(EFMD_CONF_PROPNAME, "ignoring invalid formal "
7760Sstevel@tonic-gate 			    "property %s\n", name);
7770Sstevel@tonic-gate 			continue;
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		for (op = cfp->cf_parhash[h]; op != NULL; op = op->cp_next) {
7810Sstevel@tonic-gate 			if (strcmp(op->cp_formal->cf_name, name) == 0) {
7820Sstevel@tonic-gate 				fmd_error(EFMD_CONF_PROPDUP, "ignoring "
7830Sstevel@tonic-gate 				    "duplicate formal property %s\n", name);
7840Sstevel@tonic-gate 				break;
7850Sstevel@tonic-gate 			}
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 		if (op != NULL)
7890Sstevel@tonic-gate 			continue;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		pp->cp_formal = argv;
7920Sstevel@tonic-gate 		pp->cp_next = cfp->cf_parhash[h];
7930Sstevel@tonic-gate 		cfp->cf_parhash[h] = pp;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 		if (argv->cf_default && argv->cf_ops != &fmd_conf_parent &&
7960Sstevel@tonic-gate 		    fmd_conf_setprop(cfp, name, argv->cf_default) != 0) {
7970Sstevel@tonic-gate 			fmd_error(EFMD_CONF_DEFAULT, "ignoring invalid default "
7980Sstevel@tonic-gate 			    "<%s> for property %s: %s\n", argv->cf_default,
7990Sstevel@tonic-gate 			    name, fmd_strerror(errno));
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate fmd_conf_t *
fmd_conf_open(const char * file,int argc,const fmd_conf_formal_t * argv,uint_t flag)8051193Smws fmd_conf_open(const char *file, int argc,
8061193Smws     const fmd_conf_formal_t *argv, uint_t flag)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	fmd_conf_t *cfp = fmd_alloc(sizeof (fmd_conf_t), FMD_SLEEP);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	(void) pthread_rwlock_init(&cfp->cf_lock, NULL);
8110Sstevel@tonic-gate 	cfp->cf_argv = argv;
8120Sstevel@tonic-gate 	cfp->cf_argc = argc;
8131193Smws 	cfp->cf_flag = flag;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	cfp->cf_params = fmd_zalloc(
8160Sstevel@tonic-gate 	    sizeof (fmd_conf_param_t) * (_fmd_conf_defc + argc), FMD_SLEEP);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	cfp->cf_parhashlen = fmd.d_str_buckets;
8190Sstevel@tonic-gate 	cfp->cf_parhash = fmd_zalloc(
8200Sstevel@tonic-gate 	    sizeof (fmd_conf_param_t *) * cfp->cf_parhashlen, FMD_SLEEP);
8210Sstevel@tonic-gate 
8221193Smws 	cfp->cf_defer = NULL;
8231193Smws 
8240Sstevel@tonic-gate 	fmd_conf_fill(cfp, cfp->cf_params, _fmd_conf_defc, _fmd_conf_defv, 0);
8250Sstevel@tonic-gate 	fmd_conf_fill(cfp, cfp->cf_params + _fmd_conf_defc, argc, argv, 1);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (file != NULL && fmd_conf_parse(cfp, file) != 0) {
8280Sstevel@tonic-gate 		fmd_conf_close(cfp);
8290Sstevel@tonic-gate 		return (NULL);
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	return (cfp);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate void
fmd_conf_merge(fmd_conf_t * cfp,const char * file)8360Sstevel@tonic-gate fmd_conf_merge(fmd_conf_t *cfp, const char *file)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	(void) fmd_conf_parse(cfp, file);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate void
fmd_conf_propagate(fmd_conf_t * src,fmd_conf_t * dst,const char * scope)8421193Smws fmd_conf_propagate(fmd_conf_t *src, fmd_conf_t *dst, const char *scope)
8431193Smws {
8441193Smws 	size_t len = strlen(scope);
8451193Smws 	fmd_conf_defer_t *cdp;
8461193Smws 
8471193Smws 	(void) pthread_rwlock_rdlock(&src->cf_lock);
8481193Smws 
8491193Smws 	for (cdp = src->cf_defer; cdp != NULL; cdp = cdp->cd_next) {
8501193Smws 		if (len == (size_t)(strchr(cdp->cd_name, ':') - cdp->cd_name) &&
8511193Smws 		    strncmp(cdp->cd_name, scope, len) == 0 && fmd_conf_setprop(
8521193Smws 		    dst, cdp->cd_name + len + 1, cdp->cd_value) != 0) {
8531193Smws 			fmd_error(EFMD_CONF_DEFER,
8541193Smws 			    "failed to apply deferred property %s to %s: %s\n",
8551193Smws 			    cdp->cd_name, scope, fmd_strerror(errno));
8561193Smws 		}
8571193Smws 	}
8581193Smws 
8591193Smws 	(void) pthread_rwlock_unlock(&src->cf_lock);
8601193Smws }
8611193Smws 
8621193Smws void
fmd_conf_close(fmd_conf_t * cfp)8630Sstevel@tonic-gate fmd_conf_close(fmd_conf_t *cfp)
8640Sstevel@tonic-gate {
8650Sstevel@tonic-gate 	fmd_conf_param_t *pp = cfp->cf_params;
8660Sstevel@tonic-gate 	int i, nparams = _fmd_conf_defc + cfp->cf_argc;
8671193Smws 	fmd_conf_defer_t *cdp, *ndp;
8681193Smws 
8691193Smws 	for (cdp = cfp->cf_defer; cdp != NULL; cdp = ndp) {
8701193Smws 		ndp = cdp->cd_next;
8711193Smws 		fmd_strfree(cdp->cd_name);
8721193Smws 		fmd_strfree(cdp->cd_value);
8731193Smws 		fmd_free(cdp, sizeof (fmd_conf_defer_t));
8741193Smws 	}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	fmd_free(cfp->cf_parhash,
8770Sstevel@tonic-gate 	    sizeof (fmd_conf_param_t *) * cfp->cf_parhashlen);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	for (i = 0; i < nparams; i++, pp++) {
8800Sstevel@tonic-gate 		if (pp->cp_formal != NULL)
8810Sstevel@tonic-gate 			pp->cp_formal->cf_ops->co_free(pp);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	fmd_free(cfp->cf_params, sizeof (fmd_conf_param_t) * nparams);
8850Sstevel@tonic-gate 	fmd_free(cfp, sizeof (fmd_conf_t));
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate static fmd_conf_param_t *
fmd_conf_getparam(fmd_conf_t * cfp,const char * name)8890Sstevel@tonic-gate fmd_conf_getparam(fmd_conf_t *cfp, const char *name)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate 	ulong_t h = fmd_strhash(name) % cfp->cf_parhashlen;
8920Sstevel@tonic-gate 	fmd_conf_param_t *pp = cfp->cf_parhash[h];
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&cfp->cf_lock));
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	for (; pp != NULL; pp = pp->cp_next) {
8970Sstevel@tonic-gate 		if (strcmp(name, pp->cp_formal->cf_name) == 0)
8980Sstevel@tonic-gate 			return (pp);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	return (NULL);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9041414Scindi /*
9051414Scindi  * String-friendly version of fmd_conf_getprop(): return the string as our
9061414Scindi  * return value, and return NULL if the string is the empty string.
9071414Scindi  */
9081414Scindi const char *
fmd_conf_getnzstr(fmd_conf_t * cfp,const char * name)9091414Scindi fmd_conf_getnzstr(fmd_conf_t *cfp, const char *name)
9101414Scindi {
9111414Scindi 	const fmd_conf_param_t *pp;
9121414Scindi 	char *s = NULL;
9131414Scindi 
9141414Scindi 	(void) pthread_rwlock_rdlock(&cfp->cf_lock);
9151414Scindi 
9161414Scindi 	if ((pp = fmd_conf_getparam(cfp, name)) != NULL) {
9171414Scindi 		ASSERT(pp->cp_formal->cf_ops == &fmd_conf_string);
9181414Scindi 		pp->cp_formal->cf_ops->co_get(pp, &s);
9191414Scindi 	} else
9201414Scindi 		(void) fmd_set_errno(EFMD_CONF_NOPROP);
9211414Scindi 
9221414Scindi 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
9231414Scindi 
9241414Scindi 	if (s != NULL && s[0] == '\0') {
9251414Scindi 		(void) fmd_set_errno(EFMD_CONF_UNDEF);
9261414Scindi 		s = NULL;
9271414Scindi 	}
9281414Scindi 
9291414Scindi 	return (s);
9301414Scindi }
9311414Scindi 
9320Sstevel@tonic-gate const fmd_conf_ops_t *
fmd_conf_gettype(fmd_conf_t * cfp,const char * name)9330Sstevel@tonic-gate fmd_conf_gettype(fmd_conf_t *cfp, const char *name)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	const fmd_conf_param_t *pp;
9360Sstevel@tonic-gate 	const fmd_conf_ops_t *ops = NULL;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&cfp->cf_lock);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	if ((pp = fmd_conf_getparam(cfp, name)) != NULL) {
9410Sstevel@tonic-gate 		if ((ops = pp->cp_formal->cf_ops) == &fmd_conf_parent) {
9420Sstevel@tonic-gate 			ops = fmd_conf_gettype(fmd.d_conf,
9430Sstevel@tonic-gate 			    pp->cp_formal->cf_default);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 	} else
9460Sstevel@tonic-gate 		(void) fmd_set_errno(EFMD_CONF_NOPROP);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
9490Sstevel@tonic-gate 	return (ops);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate int
fmd_conf_getprop(fmd_conf_t * cfp,const char * name,void * data)9530Sstevel@tonic-gate fmd_conf_getprop(fmd_conf_t *cfp, const char *name, void *data)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	const fmd_conf_param_t *pp;
9560Sstevel@tonic-gate 	int err = 0;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&cfp->cf_lock);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if ((pp = fmd_conf_getparam(cfp, name)) != NULL)
9610Sstevel@tonic-gate 		pp->cp_formal->cf_ops->co_get(pp, data);
9620Sstevel@tonic-gate 	else
9630Sstevel@tonic-gate 		err = fmd_set_errno(EFMD_CONF_NOPROP);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
9660Sstevel@tonic-gate 	return (err);
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9691193Smws static int
fmd_conf_setdefer(fmd_conf_t * cfp,const char * name,const char * value)9701193Smws fmd_conf_setdefer(fmd_conf_t *cfp, const char *name, const char *value)
9711193Smws {
9721193Smws 	fmd_conf_defer_t *cdp;
9731193Smws 
9741193Smws 	if (!(cfp->cf_flag & FMD_CONF_DEFER))
9751193Smws 		return (fmd_set_errno(EFMD_CONF_NODEFER));
9761193Smws 
9771193Smws 	(void) pthread_rwlock_wrlock(&cfp->cf_lock);
9781193Smws 
9791193Smws 	for (cdp = cfp->cf_defer; cdp != NULL; cdp = cdp->cd_next) {
9801193Smws 		if (strcmp(name, cdp->cd_name) == 0) {
9811193Smws 			fmd_strfree(cdp->cd_value);
9821193Smws 			cdp->cd_value = fmd_strdup(value, FMD_SLEEP);
9831193Smws 			goto out;
9841193Smws 		}
9851193Smws 	}
9861193Smws 
9871193Smws 	cdp = fmd_alloc(sizeof (fmd_conf_defer_t), FMD_SLEEP);
9881193Smws 
9891193Smws 	cdp->cd_name = fmd_strdup(name, FMD_SLEEP);
9901193Smws 	cdp->cd_value = fmd_strdup(value, FMD_SLEEP);
9911193Smws 	cdp->cd_next = cfp->cf_defer;
9921193Smws 
9931193Smws 	cfp->cf_defer = cdp;
9941193Smws out:
9951193Smws 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
9961193Smws 	return (0);
9971193Smws }
9981193Smws 
9990Sstevel@tonic-gate int
fmd_conf_setprop(fmd_conf_t * cfp,const char * name,const char * value)10000Sstevel@tonic-gate fmd_conf_setprop(fmd_conf_t *cfp, const char *name, const char *value)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 	fmd_conf_param_t *pp;
10030Sstevel@tonic-gate 	int err;
10040Sstevel@tonic-gate 
10051193Smws 	if (strchr(name, ':') != NULL)
10061193Smws 		return (fmd_conf_setdefer(cfp, name, value));
10071193Smws 
10080Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&cfp->cf_lock);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	if ((pp = fmd_conf_getparam(cfp, name)) != NULL)
10110Sstevel@tonic-gate 		err = pp->cp_formal->cf_ops->co_set(pp, value);
10120Sstevel@tonic-gate 	else
10130Sstevel@tonic-gate 		err = fmd_set_errno(EFMD_CONF_NOPROP);
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
10160Sstevel@tonic-gate 	return (err);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate int
fmd_conf_delprop(fmd_conf_t * cfp,const char * name,const char * value)10200Sstevel@tonic-gate fmd_conf_delprop(fmd_conf_t *cfp, const char *name, const char *value)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	fmd_conf_param_t *pp;
10230Sstevel@tonic-gate 	int err;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&cfp->cf_lock);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	if ((pp = fmd_conf_getparam(cfp, name)) != NULL)
10280Sstevel@tonic-gate 		err = pp->cp_formal->cf_ops->co_del(pp, value);
10290Sstevel@tonic-gate 	else
10300Sstevel@tonic-gate 		err = fmd_set_errno(EFMD_CONF_NOPROP);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&cfp->cf_lock);
10330Sstevel@tonic-gate 	return (err);
10340Sstevel@tonic-gate }
1035