17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
580ab886dSwesolows  * Common Development and Distribution License (the "License").
680ab886dSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2180ab886dSwesolows 
227c478bd9Sstevel@tonic-gate /*
237aec1d6eScindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * After having been declared, events, FMRIs and authorities must be defined
297c478bd9Sstevel@tonic-gate  * (instantiated) before they can be used as the subjects of commands.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <libnvpair.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <assert.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <inj_event.h>
387c478bd9Sstevel@tonic-gate #include <inj_err.h>
397c478bd9Sstevel@tonic-gate #include <inj_lex.h>
407c478bd9Sstevel@tonic-gate #include <inj_string.h>
417c478bd9Sstevel@tonic-gate #include <inj.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static inj_hash_t inj_defns[3];
447c478bd9Sstevel@tonic-gate static int inj_defns_initialized;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* Intrinsics (signed and unsigned integer integer constants) */
477c478bd9Sstevel@tonic-gate typedef struct intr {
487c478bd9Sstevel@tonic-gate 	uchar_t ei_signed;
497c478bd9Sstevel@tonic-gate 	uchar_t ei_width;
507c478bd9Sstevel@tonic-gate } intr_t;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static inj_hash_t *
item2hash(inj_itemtype_t item)537c478bd9Sstevel@tonic-gate item2hash(inj_itemtype_t item)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	int i;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	assert(item >= 0 && item < sizeof (inj_defns) / sizeof (inj_hash_t));
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (!inj_defns_initialized) {
607c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (inj_defns) / sizeof (inj_hash_t); i++)
617c478bd9Sstevel@tonic-gate 			inj_strhash_create(&inj_defns[i]);
627c478bd9Sstevel@tonic-gate 		inj_defns_initialized = 1;
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return (&inj_defns[item]);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate inj_defn_t *
inj_defn_lookup(const char * name,inj_memtype_t type)697c478bd9Sstevel@tonic-gate inj_defn_lookup(const char *name, inj_memtype_t type)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	inj_hash_t *hash = item2hash(inj_mem2item(type));
727c478bd9Sstevel@tonic-gate 	inj_var_t *v;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((v = inj_strhash_lookup(hash, name)) == NULL)
757c478bd9Sstevel@tonic-gate 		return (NULL);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	return (inj_hash_get_cookie(v));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static void
inj_defn_destroy_memlist(inj_defnmem_t * m)817c478bd9Sstevel@tonic-gate inj_defn_destroy_memlist(inj_defnmem_t *m)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	inj_defnmem_t *n;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	for (/* */; m != NULL; m = n) {
867c478bd9Sstevel@tonic-gate 		n = inj_list_next(m);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		switch (m->dfm_type) {
897c478bd9Sstevel@tonic-gate 		case DEFNMEM_ARRAY:
907aec1d6eScindi 		case DEFNMEM_LIST:
917c478bd9Sstevel@tonic-gate 			inj_defn_destroy_memlist(inj_list_next(&m->dfm_list));
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		default:
947c478bd9Sstevel@tonic-gate 			inj_strfree(m->dfm_str);
957c478bd9Sstevel@tonic-gate 		}
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate void
inj_defn_destroy(inj_defn_t * defn)1007c478bd9Sstevel@tonic-gate inj_defn_destroy(inj_defn_t *defn)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	if (defn->defn_name != NULL)
1037c478bd9Sstevel@tonic-gate 		inj_strfree(defn->defn_name);
1047c478bd9Sstevel@tonic-gate 
105*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(defn->defn_nvl);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	inj_defn_destroy_memlist(inj_list_next(&defn->defn_members));
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static inj_defnmem_t *
inj_defn_mem_create_common(inj_defnmemtype_t type)1117c478bd9Sstevel@tonic-gate inj_defn_mem_create_common(inj_defnmemtype_t type)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	inj_defnmem_t *dfm = inj_zalloc(sizeof (inj_defnmem_t));
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	dfm->dfm_type = type;
1167c478bd9Sstevel@tonic-gate 	dfm->dfm_lineno = yylineno;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	return (dfm);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate inj_defnmem_t *
inj_defn_mem_create(const char * str,inj_defnmemtype_t type)1227c478bd9Sstevel@tonic-gate inj_defn_mem_create(const char *str, inj_defnmemtype_t type)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	inj_defnmem_t *dfm = inj_defn_mem_create_common(type);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	dfm->dfm_str = str;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (dfm);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate inj_defnmem_t *
inj_defn_mem_create_list(inj_defn_t * list,inj_defnmemtype_t type)1327c478bd9Sstevel@tonic-gate inj_defn_mem_create_list(inj_defn_t *list, inj_defnmemtype_t type)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	inj_defnmem_t *dfm = inj_defn_mem_create_common(type);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	dfm->dfm_list = list->defn_members;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	inj_free(list, sizeof (inj_defn_t));
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	return (dfm);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate inj_defn_t *
inj_defn_create(inj_defnmem_t * dfm)1447c478bd9Sstevel@tonic-gate inj_defn_create(inj_defnmem_t *dfm)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	inj_defn_t *defn = inj_zalloc(sizeof (inj_defn_t));
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	defn->defn_lineno = yylineno;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	inj_list_append(&defn->defn_members, dfm);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	return (defn);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void
inj_defn_addmem(inj_defn_t * defn,inj_defnmem_t * dfm)1567c478bd9Sstevel@tonic-gate inj_defn_addmem(inj_defn_t *defn, inj_defnmem_t *dfm)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	inj_list_append(&defn->defn_members, dfm);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Validate the dimensions of an array.  If the declared array size was zero,
1637c478bd9Sstevel@tonic-gate  * accept (and return) whatever the definition used.  If fewer cells were
1647c478bd9Sstevel@tonic-gate  * defined than were declared, return the declared size - the calling code will
1657c478bd9Sstevel@tonic-gate  * fill the remaining cells with zeros.  The definition of more than the
1667c478bd9Sstevel@tonic-gate  * declared number of cells triggers an error.  We print and error message in
1677c478bd9Sstevel@tonic-gate  * this case and return the declared number.  This will allow processing to
1687c478bd9Sstevel@tonic-gate  * continue.  The act of emitting the error will guarantee that we never
1697c478bd9Sstevel@tonic-gate  * pass from parsing to program execution.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static size_t
array_dim_check(inj_declmem_t * dlm,inj_defnmem_t * dfm)1727c478bd9Sstevel@tonic-gate array_dim_check(inj_declmem_t *dlm, inj_defnmem_t *dfm)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	inj_list_t *l;
1757c478bd9Sstevel@tonic-gate 	size_t dfnelems;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (dfnelems = 0, l = inj_list_next(&dfm->dfm_list); l != NULL;
1787c478bd9Sstevel@tonic-gate 	    l = inj_list_next(l), dfnelems++);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (dlm->dlm_arrdim != 0 && dlm->dlm_arrdim != dfnelems) {
1817c478bd9Sstevel@tonic-gate 		yyerror(" %d: defined array has %d elements, expected %d\n",
1827c478bd9Sstevel@tonic-gate 		    dfm->dfm_lineno, dfnelems, dlm->dlm_arrdim);
1837c478bd9Sstevel@tonic-gate 		dfnelems = dlm->dlm_arrdim;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (MAX(dfnelems, dlm->dlm_arrdim));
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * The inj_defn_memcmp_* routines serve two purposes.  First, they compare a
1917c478bd9Sstevel@tonic-gate  * given defined member with the corresponding declared member, signalling an
1927c478bd9Sstevel@tonic-gate  * error if the two are incompatible.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * Assuming that validation succeeds, an entry is added to the passed nvlist
1957c478bd9Sstevel@tonic-gate  * for the defined member.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* Used to ease signed and unsigned integer validation */
1997c478bd9Sstevel@tonic-gate static const intr_t inj_intrinsics[] = {
2007c478bd9Sstevel@tonic-gate 	{ 0, 0 }, /* MEMTYPE_UNKNOWN */
2017c478bd9Sstevel@tonic-gate 	{ 1, 8 }, { 1, 16 }, { 1, 32 }, { 1, 64 },
2027c478bd9Sstevel@tonic-gate 	{ 0, 8 }, { 0, 16 }, { 0, 32 }, { 0, 64 }
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_signed(const intr_t * intr,inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)2067c478bd9Sstevel@tonic-gate inj_defn_memcmp_signed(const intr_t *intr, inj_declmem_t *dlm,
2077c478bd9Sstevel@tonic-gate     inj_defnmem_t *dfm, nvlist_t *nvl)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	longlong_t val;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_IMM && dfm->dfm_type != DEFNMEM_IDENT)
2127c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (inj_strtoll(dfm->dfm_str, intr->ei_width, &val) < 0)
2157c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	switch (dlm->dlm_type) {
2187c478bd9Sstevel@tonic-gate 	case MEMTYPE_INT8:
2197c478bd9Sstevel@tonic-gate 		errno = nvlist_add_int8(nvl, (char *)dlm->dlm_name,
2207c478bd9Sstevel@tonic-gate 		    (int8_t)val);
2217c478bd9Sstevel@tonic-gate 		break;
2227c478bd9Sstevel@tonic-gate 	case MEMTYPE_INT16:
2237c478bd9Sstevel@tonic-gate 		errno = nvlist_add_int16(nvl, (char *)dlm->dlm_name,
2247c478bd9Sstevel@tonic-gate 		    (int16_t)val);
2257c478bd9Sstevel@tonic-gate 		break;
2267c478bd9Sstevel@tonic-gate 	case MEMTYPE_INT32:
2277c478bd9Sstevel@tonic-gate 		errno = nvlist_add_int32(nvl, (char *)dlm->dlm_name,
2287c478bd9Sstevel@tonic-gate 		    (int32_t)val);
2297c478bd9Sstevel@tonic-gate 		break;
2307c478bd9Sstevel@tonic-gate 	case MEMTYPE_INT64:
2317c478bd9Sstevel@tonic-gate 		errno = nvlist_add_int64(nvl, (char *)dlm->dlm_name,
2327c478bd9Sstevel@tonic-gate 		    (int64_t)val);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (errno != 0)
2367c478bd9Sstevel@tonic-gate 		die("failed to add member %s\n", dlm->dlm_name);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_unsigned(const intr_t * intr,inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)2427c478bd9Sstevel@tonic-gate inj_defn_memcmp_unsigned(const intr_t *intr, inj_declmem_t *dlm,
2437c478bd9Sstevel@tonic-gate     inj_defnmem_t *dfm, nvlist_t *nvl)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	u_longlong_t val;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_IMM && dfm->dfm_type != DEFNMEM_IDENT)
2487c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (inj_strtoull(dfm->dfm_str, intr->ei_width, &val) < 0)
2517c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	switch (dlm->dlm_type) {
2547c478bd9Sstevel@tonic-gate 	case MEMTYPE_UINT8:
2557c478bd9Sstevel@tonic-gate 		errno = nvlist_add_uint8(nvl, (char *)dlm->dlm_name,
2567c478bd9Sstevel@tonic-gate 		    (uint8_t)val);
2577c478bd9Sstevel@tonic-gate 		break;
2587c478bd9Sstevel@tonic-gate 	case MEMTYPE_UINT16:
2597c478bd9Sstevel@tonic-gate 		errno = nvlist_add_uint16(nvl, (char *)dlm->dlm_name,
2607c478bd9Sstevel@tonic-gate 		    (uint16_t)val);
2617c478bd9Sstevel@tonic-gate 		break;
2627c478bd9Sstevel@tonic-gate 	case MEMTYPE_UINT32:
2637c478bd9Sstevel@tonic-gate 		errno = nvlist_add_uint32(nvl, (char *)dlm->dlm_name,
2647c478bd9Sstevel@tonic-gate 		    (uint32_t)val);
2657c478bd9Sstevel@tonic-gate 		break;
2667c478bd9Sstevel@tonic-gate 	case MEMTYPE_UINT64:
2677c478bd9Sstevel@tonic-gate 		errno = nvlist_add_uint64(nvl, (char *)dlm->dlm_name,
2687c478bd9Sstevel@tonic-gate 		    (uint64_t)val);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (errno != 0)
2727c478bd9Sstevel@tonic-gate 		die("failed to add member %s\n", dlm->dlm_name);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /* Validate an array of (un)signed integers. */
2787c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_intr_array(const intr_t * cont,inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)2797c478bd9Sstevel@tonic-gate inj_defn_memcmp_intr_array(const intr_t *cont, inj_declmem_t *dlm,
2807c478bd9Sstevel@tonic-gate     inj_defnmem_t *dfm, nvlist_t *nvl)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	typedef int (*adder_t)();
2837c478bd9Sstevel@tonic-gate 	static const adder_t signed_adders[] = {
2847c478bd9Sstevel@tonic-gate 		NULL, nvlist_add_int8_array, nvlist_add_int16_array,
2857c478bd9Sstevel@tonic-gate 		NULL, nvlist_add_int32_array, NULL, NULL, NULL,
2867c478bd9Sstevel@tonic-gate 		nvlist_add_int64_array
2877c478bd9Sstevel@tonic-gate 	};
2887c478bd9Sstevel@tonic-gate 	static const adder_t unsigned_adders[] = {
2897c478bd9Sstevel@tonic-gate 		NULL,
2907c478bd9Sstevel@tonic-gate 		nvlist_add_uint8_array, nvlist_add_uint16_array,
2917c478bd9Sstevel@tonic-gate 		NULL, nvlist_add_uint32_array, NULL, NULL, NULL,
2927c478bd9Sstevel@tonic-gate 		nvlist_add_uint64_array
2937c478bd9Sstevel@tonic-gate 	};
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	union {
2967c478bd9Sstevel@tonic-gate 		char *a;
2977c478bd9Sstevel@tonic-gate 		int8_t *a8; uint8_t *au8;
2987c478bd9Sstevel@tonic-gate 		int16_t *a16; uint16_t *au16;
2997c478bd9Sstevel@tonic-gate 		int32_t *a32; uint32_t *au32;
3007c478bd9Sstevel@tonic-gate 		int64_t *a64; uint64_t *au64;
3017c478bd9Sstevel@tonic-gate 	} a;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	int (*adder)(nvlist_t *, const char *, char *, uint_t);
3047c478bd9Sstevel@tonic-gate 	size_t nelems;
3057c478bd9Sstevel@tonic-gate 	inj_defnmem_t *elem;
3067c478bd9Sstevel@tonic-gate 	char *arrbase, *arr;
3077c478bd9Sstevel@tonic-gate 	size_t arrsz;
3087c478bd9Sstevel@tonic-gate 	int err = 0;
3097c478bd9Sstevel@tonic-gate 	int i;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_ARRAY)
3127c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Each nvlist array adder wants an array of its own type as input,
3167c478bd9Sstevel@tonic-gate 	 * which is reasonable, but it complicates our general implementation.
3177c478bd9Sstevel@tonic-gate 	 * We fight back with casting magic.
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	nelems = array_dim_check(dlm, dfm);
3217c478bd9Sstevel@tonic-gate 	arrsz = (nelems + 1) * (cont->ei_width / NBBY);
3227c478bd9Sstevel@tonic-gate 	arrbase = inj_zalloc(arrsz);
3237c478bd9Sstevel@tonic-gate 	a.a = arr = (char *)P2ROUNDUP((uintptr_t)arrbase,
3247c478bd9Sstevel@tonic-gate 	    cont->ei_width / NBBY);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	adder = (cont->ei_signed ? signed_adders :
3277c478bd9Sstevel@tonic-gate 	    unsigned_adders)[cont->ei_width / NBBY];
3287c478bd9Sstevel@tonic-gate 	assert(adder != NULL);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	for (i = 1, elem = inj_list_next(&dfm->dfm_list); elem != NULL;
3317c478bd9Sstevel@tonic-gate 	    elem = inj_list_next(elem), i++) {
3327c478bd9Sstevel@tonic-gate 		if (elem->dfm_type != DEFNMEM_IMM &&
3337c478bd9Sstevel@tonic-gate 		    elem->dfm_type != DEFNMEM_IDENT) {
3347c478bd9Sstevel@tonic-gate 			yyerror(" %d: array cell %d is invalid\n",
3357c478bd9Sstevel@tonic-gate 			    dfm->dfm_lineno, i);
3367c478bd9Sstevel@tonic-gate 			err++;
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		if (cont->ei_signed) {
3417c478bd9Sstevel@tonic-gate 			longlong_t val;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			if (inj_strtoll(elem->dfm_str, cont->ei_width,
3447c478bd9Sstevel@tonic-gate 			    &val) < 0) {
3457c478bd9Sstevel@tonic-gate 				yyerror(" %d: array cell %d %s\n",
3467c478bd9Sstevel@tonic-gate 				    dfm->dfm_lineno, i, (errno == ERANGE ?
3477c478bd9Sstevel@tonic-gate 				    "out of range for type" : "invalid"));
3487c478bd9Sstevel@tonic-gate 				err++;
3497c478bd9Sstevel@tonic-gate 				continue;
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 			switch (cont->ei_width) {
3537c478bd9Sstevel@tonic-gate 			case 8:
3547c478bd9Sstevel@tonic-gate 				*a.a8++ = (int8_t)val;
3557c478bd9Sstevel@tonic-gate 				break;
3567c478bd9Sstevel@tonic-gate 			case 16:
3577c478bd9Sstevel@tonic-gate 				*a.a16++ = (int16_t)val;
3587c478bd9Sstevel@tonic-gate 				break;
3597c478bd9Sstevel@tonic-gate 			case 32:
3607c478bd9Sstevel@tonic-gate 				*a.a32++ = (int32_t)val;
3617c478bd9Sstevel@tonic-gate 				break;
3627c478bd9Sstevel@tonic-gate 			default:
3637c478bd9Sstevel@tonic-gate 				*a.a64++ = (int64_t)val;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		} else {
3677c478bd9Sstevel@tonic-gate 			u_longlong_t val;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 			if (inj_strtoull(elem->dfm_str, cont->ei_width,
3707c478bd9Sstevel@tonic-gate 			    &val) < 0) {
3717c478bd9Sstevel@tonic-gate 				yyerror(" %d: array cell %d %s\n",
3727c478bd9Sstevel@tonic-gate 				    dfm->dfm_lineno, i, (errno == ERANGE ?
3737c478bd9Sstevel@tonic-gate 				    "out of range for type" : "invalid"));
3747c478bd9Sstevel@tonic-gate 				err++;
3757c478bd9Sstevel@tonic-gate 				continue;
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			switch (cont->ei_width) {
3797c478bd9Sstevel@tonic-gate 			case 8:
3807c478bd9Sstevel@tonic-gate 				*a.au8++ = (uint8_t)val;
3817c478bd9Sstevel@tonic-gate 				break;
3827c478bd9Sstevel@tonic-gate 			case 16:
3837c478bd9Sstevel@tonic-gate 				*a.au16++ = (uint16_t)val;
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 			case 32:
3867c478bd9Sstevel@tonic-gate 				*a.au32++ = (uint32_t)val;
3877c478bd9Sstevel@tonic-gate 				break;
3887c478bd9Sstevel@tonic-gate 			default:
3897c478bd9Sstevel@tonic-gate 				*a.au64++ = (uint64_t)val;
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (err == 0 && (errno = adder(nvl, dlm->dlm_name, arr, nelems)) != 0)
3957c478bd9Sstevel@tonic-gate 		die("failed to add array member %s", dlm->dlm_name);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	inj_free(arrbase, arrsz);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (err != 0)
4007c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	return (0);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate static int
bool2val(const char * str,boolean_t * valp)4067c478bd9Sstevel@tonic-gate bool2val(const char *str, boolean_t *valp)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	if (strcasecmp(str, "true") == 0)
4097c478bd9Sstevel@tonic-gate 		*valp = 1;
4107c478bd9Sstevel@tonic-gate 	else if (strcasecmp(str, "false") == 0)
4117c478bd9Sstevel@tonic-gate 		*valp = 0;
4127c478bd9Sstevel@tonic-gate 	else
4137c478bd9Sstevel@tonic-gate 		return (-1);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_bool(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)4197c478bd9Sstevel@tonic-gate inj_defn_memcmp_bool(inj_declmem_t *dlm, inj_defnmem_t *dfm, nvlist_t *nvl)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	boolean_t val;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_IDENT)
4247c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (bool2val(dfm->dfm_str, &val) < 0)
4277c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_add_boolean_value(nvl, (char *)dlm->dlm_name,
4307c478bd9Sstevel@tonic-gate 	    val)) != 0)
4317c478bd9Sstevel@tonic-gate 		die("failed to add boolean member %s", dlm->dlm_name);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	return (0);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_bool_array(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)4377c478bd9Sstevel@tonic-gate inj_defn_memcmp_bool_array(inj_declmem_t *dlm, inj_defnmem_t *dfm,
4387c478bd9Sstevel@tonic-gate     nvlist_t *nvl)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	inj_defnmem_t *elem;
4417c478bd9Sstevel@tonic-gate 	boolean_t *arr;
4427c478bd9Sstevel@tonic-gate 	size_t nelems, arrsz;
4437c478bd9Sstevel@tonic-gate 	int err = 0;
4447c478bd9Sstevel@tonic-gate 	int i;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_ARRAY)
4477c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	nelems = array_dim_check(dlm, dfm);
4507c478bd9Sstevel@tonic-gate 	arrsz = nelems * sizeof (boolean_t);
4517c478bd9Sstevel@tonic-gate 	arr = inj_zalloc(arrsz);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	for (i = 0, elem = inj_list_next(&dfm->dfm_list); elem != NULL;
4547c478bd9Sstevel@tonic-gate 	    elem = inj_list_next(elem), i++) {
4557c478bd9Sstevel@tonic-gate 		if (elem->dfm_type != DEFNMEM_IDENT) {
4567c478bd9Sstevel@tonic-gate 			yyerror(" %d: array cell %d is invalid\n",
4577c478bd9Sstevel@tonic-gate 			    dfm->dfm_lineno, i + 1);
4587c478bd9Sstevel@tonic-gate 			err++;
4597c478bd9Sstevel@tonic-gate 			continue;
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		if (bool2val(elem->dfm_str, &arr[i]) < 0)
4637c478bd9Sstevel@tonic-gate 			return (inj_set_errno(EINVAL));
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (err == 0 && (errno = nvlist_add_boolean_array(nvl,
4677c478bd9Sstevel@tonic-gate 	    (char *)dlm->dlm_name, arr, nelems)) != 0)
4687c478bd9Sstevel@tonic-gate 		die("failed to add boolean array member %s", dlm->dlm_name);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	inj_free(arr, arrsz);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /* Used for both strings and enums */
4767c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_strenum(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)4777c478bd9Sstevel@tonic-gate inj_defn_memcmp_strenum(inj_declmem_t *dlm, inj_defnmem_t *dfm, nvlist_t *nvl)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	inj_defnmemtype_t defnmemtype = (dlm->dlm_type == MEMTYPE_ENUM ?
4807c478bd9Sstevel@tonic-gate 	    DEFNMEM_IDENT : DEFNMEM_QSTRING);
4817c478bd9Sstevel@tonic-gate 	const char *strenum = (dlm->dlm_type == MEMTYPE_ENUM ? "enum" :
4827c478bd9Sstevel@tonic-gate 	    "string");
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != defnmemtype)
4857c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_add_string(nvl, (char *)dlm->dlm_name,
4887c478bd9Sstevel@tonic-gate 	    (char *)dfm->dfm_str)) != 0)
4897c478bd9Sstevel@tonic-gate 		die("failed to add %s member %s", strenum, dlm->dlm_name);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (0);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_strenum_array(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)4957c478bd9Sstevel@tonic-gate inj_defn_memcmp_strenum_array(inj_declmem_t *dlm, inj_defnmem_t *dfm,
4967c478bd9Sstevel@tonic-gate     nvlist_t *nvl)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	inj_defnmemtype_t defnmemtype = (dlm->dlm_type == MEMTYPE_ENUM ?
4997c478bd9Sstevel@tonic-gate 	    DEFNMEM_IDENT : DEFNMEM_QSTRING);
5007c478bd9Sstevel@tonic-gate 	const char *strenum = (dlm->dlm_type == MEMTYPE_ENUM ? "enum" :
5017c478bd9Sstevel@tonic-gate 	    "string");
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	inj_defnmem_t *elem;
5047c478bd9Sstevel@tonic-gate 	size_t nelems, arrsz;
5057c478bd9Sstevel@tonic-gate 	const char **arr;
5067c478bd9Sstevel@tonic-gate 	int err = 0;
5077c478bd9Sstevel@tonic-gate 	int i;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_ARRAY)
5107c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	nelems = array_dim_check(dlm, dfm);
5137c478bd9Sstevel@tonic-gate 	arrsz = nelems * sizeof (char *);
5147c478bd9Sstevel@tonic-gate 	arr = inj_zalloc(arrsz);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	for (i = 0, elem = inj_list_next(&dfm->dfm_list); elem != NULL;
5177c478bd9Sstevel@tonic-gate 	    elem = inj_list_next(elem), i++) {
5187c478bd9Sstevel@tonic-gate 		if (elem->dfm_type != defnmemtype) {
5197c478bd9Sstevel@tonic-gate 			yyerror(" %d: array cell %d is invalid\n",
5207c478bd9Sstevel@tonic-gate 			    dfm->dfm_lineno, i + 1);
5217c478bd9Sstevel@tonic-gate 			err++;
5227c478bd9Sstevel@tonic-gate 			continue;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if (dlm->dlm_type == MEMTYPE_ENUM &&
5267c478bd9Sstevel@tonic-gate 		    inj_strhash_lookup(dlm->dlm_enumvals, elem->dfm_str) ==
5277c478bd9Sstevel@tonic-gate 		    NULL) {
5287c478bd9Sstevel@tonic-gate 			yyerror(" %d: invalid enum value %s\n",
5297c478bd9Sstevel@tonic-gate 			    dfm->dfm_lineno, elem->dfm_str);
5307c478bd9Sstevel@tonic-gate 			err++;
5317c478bd9Sstevel@tonic-gate 			continue;
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		arr[i] = elem->dfm_str;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (err == 0 && (errno = nvlist_add_string_array(nvl,
5387c478bd9Sstevel@tonic-gate 	    dlm->dlm_name, (char **)arr, nelems)) != 0)
5397c478bd9Sstevel@tonic-gate 		die("failed to add %s array member %s", strenum, dlm->dlm_name);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	inj_free(arr, arrsz);
5427c478bd9Sstevel@tonic-gate 	return (0);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*
5467aec1d6eScindi  * Validator for embedded lists (events, fmris, authorities, lists, etc.).
5477aec1d6eScindi  * There are two cases to deal with here.  The user could either have provided
5487aec1d6eScindi  * the name of a previously-defined list, in which case we just make a copy of
5497aec1d6eScindi  * said list for insertion into ours.  Alternatively, the user could simply
5507aec1d6eScindi  * define a new list here.  In that case, we recursively invoke the member
5517c478bd9Sstevel@tonic-gate  * comparator, but against the list type for the member being defined.
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate static nvlist_t *inj_defn_validate_memlist(inj_declmem_t *, inj_defnmem_t *);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /* Embedded definition */
5567c478bd9Sstevel@tonic-gate static nvlist_t *
inj_defn_memcmp_sub_list(inj_declmem_t * dlm,inj_defnmem_t * dfm)5577c478bd9Sstevel@tonic-gate inj_defn_memcmp_sub_list(inj_declmem_t *dlm, inj_defnmem_t *dfm)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	inj_declmem_t *subdlm = inj_list_next(&dlm->dlm_decl->decl_members);
5607c478bd9Sstevel@tonic-gate 	inj_defnmem_t *subdfm = inj_list_next(&dfm->dfm_list);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	return (inj_defn_validate_memlist(subdlm, subdfm));
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /* Reference to previously-defined thing */
5667c478bd9Sstevel@tonic-gate static nvlist_t *
inj_defn_memcmp_sub_defined(inj_declmem_t * dlm,inj_defnmem_t * dfm)5677c478bd9Sstevel@tonic-gate inj_defn_memcmp_sub_defined(inj_declmem_t *dlm, inj_defnmem_t *dfm)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	inj_defn_t *subdefn;
5707c478bd9Sstevel@tonic-gate 	nvlist_t *new;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if ((subdefn = inj_defn_lookup(dfm->dfm_str, dlm->dlm_type)) == NULL) {
5737c478bd9Sstevel@tonic-gate 		yyerror(" %d: reference to undefined %s %s\n", dfm->dfm_lineno,
5747c478bd9Sstevel@tonic-gate 		    inj_mem2str(dlm->dlm_type), dfm->dfm_str);
5757c478bd9Sstevel@tonic-gate 		(void) inj_set_errno(EINVAL);
5767c478bd9Sstevel@tonic-gate 		return (NULL);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (subdefn->defn_decl != dlm->dlm_decl) {
5807c478bd9Sstevel@tonic-gate 		yyerror(" %d: %s %s is not a(n) %s\n", dfm->dfm_lineno,
5817c478bd9Sstevel@tonic-gate 		    inj_mem2str(dlm->dlm_type), dfm->dfm_str,
5827c478bd9Sstevel@tonic-gate 		    subdefn->defn_decl->decl_name);
5837c478bd9Sstevel@tonic-gate 		(void) inj_set_errno(EINVAL);
5847c478bd9Sstevel@tonic-gate 		return (NULL);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	assert(subdefn->defn_nvl != NULL);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_dup(subdefn->defn_nvl, &new, 0)) != 0) {
5907c478bd9Sstevel@tonic-gate 		die("failed to duplicate %s list %s",
5917c478bd9Sstevel@tonic-gate 		    inj_item2str(subdefn->defn_decl->decl_type), dfm->dfm_str);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	return (new);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static nvlist_t *
inj_defn_memcmp_sub_makenvl(inj_declmem_t * dlm,inj_defnmem_t * dfm)5987c478bd9Sstevel@tonic-gate inj_defn_memcmp_sub_makenvl(inj_declmem_t *dlm, inj_defnmem_t *dfm)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	inj_defnmemtype_t dftype = dfm->dfm_type;
6017c478bd9Sstevel@tonic-gate 	inj_memtype_t dltype = dlm->dlm_type;
6027c478bd9Sstevel@tonic-gate 	nvlist_t *new = NULL;
6037c478bd9Sstevel@tonic-gate 
6047aec1d6eScindi 	if (dftype == DEFNMEM_LIST)
6057c478bd9Sstevel@tonic-gate 		new = inj_defn_memcmp_sub_list(dlm, dfm);
6067c478bd9Sstevel@tonic-gate 	else if (dftype == DEFNMEM_IDENT && (dltype == MEMTYPE_EVENT ||
6077c478bd9Sstevel@tonic-gate 	    dltype == MEMTYPE_FMRI || dltype == MEMTYPE_AUTH))
6087c478bd9Sstevel@tonic-gate 		new = inj_defn_memcmp_sub_defined(dlm, dfm);
6097c478bd9Sstevel@tonic-gate 	else
6107c478bd9Sstevel@tonic-gate 		(void) inj_set_errno(EINVAL);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	return (new);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /* A single sub-list */
6167c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_sub(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)6177c478bd9Sstevel@tonic-gate inj_defn_memcmp_sub(inj_declmem_t *dlm, inj_defnmem_t *dfm, nvlist_t *nvl)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	nvlist_t *new;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if ((new = inj_defn_memcmp_sub_makenvl(dlm, dfm)) == NULL)
6227c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_add_nvlist(nvl, (char *)dlm->dlm_name,
6257c478bd9Sstevel@tonic-gate 	    new)) != 0)
6267c478bd9Sstevel@tonic-gate 		die("failed to add list member %s", dlm->dlm_name);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	return (0);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /* An array of sub-lists (for example, an array of events of a given type) */
6327c478bd9Sstevel@tonic-gate static int
inj_defn_memcmp_sub_array(inj_declmem_t * dlm,inj_defnmem_t * dfm,nvlist_t * nvl)6337c478bd9Sstevel@tonic-gate inj_defn_memcmp_sub_array(inj_declmem_t *dlm, inj_defnmem_t *dfm, nvlist_t *nvl)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	size_t nelems, arrsz;
6367c478bd9Sstevel@tonic-gate 	inj_defnmem_t *elem;
6377c478bd9Sstevel@tonic-gate 	nvlist_t **arr;
6387c478bd9Sstevel@tonic-gate 	int err = 0;
6397c478bd9Sstevel@tonic-gate 	int i;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (dfm->dfm_type != DEFNMEM_ARRAY)
6427c478bd9Sstevel@tonic-gate 		return (inj_set_errno(EINVAL));
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	nelems = array_dim_check(dlm, dfm);
6457c478bd9Sstevel@tonic-gate 	arrsz = nelems * sizeof (char *);
6467c478bd9Sstevel@tonic-gate 	arr = inj_zalloc(arrsz);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	for (i = 0, elem = inj_list_next(&dfm->dfm_list); elem != NULL;
6497c478bd9Sstevel@tonic-gate 	    elem = inj_list_next(elem), i++) {
6507c478bd9Sstevel@tonic-gate 		if ((arr[i] = inj_defn_memcmp_sub_makenvl(dlm, elem)) == NULL) {
6517c478bd9Sstevel@tonic-gate 			yyerror(" %d: array cell %d is invalid\n",
6527c478bd9Sstevel@tonic-gate 			    elem->dfm_lineno, i + 1);
6537c478bd9Sstevel@tonic-gate 			err++;
6547c478bd9Sstevel@tonic-gate 			continue;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (err == 0 && (errno = nvlist_add_nvlist_array(nvl,
6597c478bd9Sstevel@tonic-gate 	    (char *)dlm->dlm_name, arr, nelems)) != 0)
6607c478bd9Sstevel@tonic-gate 		die("failed to add nvlist list member %s", dlm->dlm_name);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	inj_free(arr, arrsz);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	return (0);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * The declaration-definition member comparator.  Designed to recursive
6697aec1d6eScindi  * invocation to allow for the validation of embedded/referenced lists.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate nvlist_t *
inj_defn_validate_memlist(inj_declmem_t * dlm,inj_defnmem_t * dfm)6727c478bd9Sstevel@tonic-gate inj_defn_validate_memlist(inj_declmem_t *dlm, inj_defnmem_t *dfm)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	const intr_t *intr;
6757c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
6767c478bd9Sstevel@tonic-gate 	int rc, nmem, dlnmem, dfnmem;
6777c478bd9Sstevel@tonic-gate 	int err = 0;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0)
6807c478bd9Sstevel@tonic-gate 		die("failed to allocate nvl for event");
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	for (nmem = 1; dlm != NULL && dfm != NULL;
6837c478bd9Sstevel@tonic-gate 	    dlm = inj_list_next(dlm), dfm = inj_list_next(dfm), nmem++) {
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		switch (dlm->dlm_type) {
6867c478bd9Sstevel@tonic-gate 		case MEMTYPE_INT8:
6877c478bd9Sstevel@tonic-gate 		case MEMTYPE_INT16:
6887c478bd9Sstevel@tonic-gate 		case MEMTYPE_INT32:
6897c478bd9Sstevel@tonic-gate 		case MEMTYPE_INT64:
6907c478bd9Sstevel@tonic-gate 			intr = &inj_intrinsics[dlm->dlm_type];
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY) {
6937c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_intr_array(intr, dlm, dfm,
6947c478bd9Sstevel@tonic-gate 				    nvl);
6957c478bd9Sstevel@tonic-gate 			} else {
6967c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_signed(intr, dlm, dfm,
6977c478bd9Sstevel@tonic-gate 				    nvl);
6987c478bd9Sstevel@tonic-gate 			}
6997c478bd9Sstevel@tonic-gate 			break;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		case MEMTYPE_UINT8:
7027c478bd9Sstevel@tonic-gate 		case MEMTYPE_UINT16:
7037c478bd9Sstevel@tonic-gate 		case MEMTYPE_UINT32:
7047c478bd9Sstevel@tonic-gate 		case MEMTYPE_UINT64:
7057c478bd9Sstevel@tonic-gate 			intr = &inj_intrinsics[dlm->dlm_type];
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY) {
7087c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_intr_array(intr, dlm, dfm,
7097c478bd9Sstevel@tonic-gate 				    nvl);
7107c478bd9Sstevel@tonic-gate 			} else {
7117c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_unsigned(intr, dlm, dfm,
7127c478bd9Sstevel@tonic-gate 				    nvl);
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		case MEMTYPE_BOOL:
7177c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY)
7187c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_bool_array(dlm, dfm, nvl);
7197c478bd9Sstevel@tonic-gate 			else
7207c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_bool(dlm, dfm, nvl);
7217c478bd9Sstevel@tonic-gate 			break;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		case MEMTYPE_STRING:
7247c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY) {
7257c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_strenum_array(dlm, dfm,
7267c478bd9Sstevel@tonic-gate 				    nvl);
7277c478bd9Sstevel@tonic-gate 			} else
7287c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_strenum(dlm, dfm, nvl);
7297c478bd9Sstevel@tonic-gate 			break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		case MEMTYPE_ENUM:
7327c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY) {
7337c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_strenum_array(dlm, dfm,
7347c478bd9Sstevel@tonic-gate 				    nvl);
7357c478bd9Sstevel@tonic-gate 			} else
7367c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_strenum(dlm, dfm, nvl);
7377c478bd9Sstevel@tonic-gate 			break;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		case MEMTYPE_EVENT:
7407c478bd9Sstevel@tonic-gate 		case MEMTYPE_FMRI:
7417c478bd9Sstevel@tonic-gate 		case MEMTYPE_AUTH:
7427aec1d6eScindi 		case MEMTYPE_LIST:
7437c478bd9Sstevel@tonic-gate 			if (dlm->dlm_flags & DECLMEM_F_ARRAY)
7447c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_sub_array(dlm, dfm, nvl);
7457c478bd9Sstevel@tonic-gate 			else
7467c478bd9Sstevel@tonic-gate 				rc = inj_defn_memcmp_sub(dlm, dfm, nvl);
7477c478bd9Sstevel@tonic-gate 			break;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		default:
7507c478bd9Sstevel@tonic-gate 			die("unknown decl member type %d on member %s\n",
7517c478bd9Sstevel@tonic-gate 			    dlm->dlm_type, dlm->dlm_name);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		if (rc < 0) {
7557c478bd9Sstevel@tonic-gate 			yyerror(" %d: %s for member %s\n", dfm->dfm_lineno,
7567c478bd9Sstevel@tonic-gate 			    (errno == ERANGE ? "value out of range" :
7577c478bd9Sstevel@tonic-gate 			    "invalid value"), dlm->dlm_name);
7587c478bd9Sstevel@tonic-gate 			err++;
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	dlnmem = dfnmem = nmem;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	while (dlm != NULL) {
7657c478bd9Sstevel@tonic-gate 		dlm = inj_list_next(dlm);
7667c478bd9Sstevel@tonic-gate 		dlnmem++;
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	while (dfm != NULL) {
7707c478bd9Sstevel@tonic-gate 		dfm = inj_list_next(dfm);
7717c478bd9Sstevel@tonic-gate 		dfnmem++;
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (dlnmem != dfnmem) {
7757c478bd9Sstevel@tonic-gate 		yyerror("%d members found, expected %d", dfnmem, dlnmem);
7767c478bd9Sstevel@tonic-gate 		err++;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (err > 0) {
7807c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
7817c478bd9Sstevel@tonic-gate 		return (NULL);
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	return (nvl);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * The members have all been defined.  Validate the members against the
7897c478bd9Sstevel@tonic-gate  * declaration, and add it to the appropriate "defined" list.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate void
inj_defn_finish(inj_defn_t * defn,const char * declnm,const char * name,inj_itemtype_t type)7927c478bd9Sstevel@tonic-gate inj_defn_finish(inj_defn_t *defn, const char *declnm, const char *name,
7937c478bd9Sstevel@tonic-gate     inj_itemtype_t type)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	inj_decl_t *decl = inj_decl_lookup(declnm, type);
7967c478bd9Sstevel@tonic-gate 	inj_hash_t *hash = item2hash(type);
7977c478bd9Sstevel@tonic-gate 	inj_declmem_t *dlm;
7987c478bd9Sstevel@tonic-gate 	inj_defnmem_t *dfm;
7997c478bd9Sstevel@tonic-gate 	inj_var_t *v;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	defn->defn_name = name;
8027c478bd9Sstevel@tonic-gate 	defn->defn_decl = decl;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (decl == NULL) {
8057c478bd9Sstevel@tonic-gate 		yyerror("unknown %s type %s\n", inj_item2str(type), declnm);
8067c478bd9Sstevel@tonic-gate 		inj_defn_destroy(defn);
8077c478bd9Sstevel@tonic-gate 		return;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	dlm = inj_list_next(&decl->decl_members);
8117c478bd9Sstevel@tonic-gate 	dfm = inj_list_next(&defn->defn_members);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if ((defn->defn_nvl = inj_defn_validate_memlist(dlm, dfm)) == NULL) {
8147c478bd9Sstevel@tonic-gate 		inj_defn_destroy(defn);
8157c478bd9Sstevel@tonic-gate 		return;
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (type == ITEMTYPE_EVENT) {
8197c478bd9Sstevel@tonic-gate 		if ((errno = nvlist_add_string(defn->defn_nvl, "class",
8207c478bd9Sstevel@tonic-gate 		    (char *)defn->defn_decl->decl_name)) != 0)
8217c478bd9Sstevel@tonic-gate 			die("failed to add class to %s", name);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if ((v = inj_strhash_lookup(hash, name)) != NULL) {
8257c478bd9Sstevel@tonic-gate 		inj_defn_t *other = inj_hash_get_cookie(v);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		yyerror("duplicate %s name %s (other on line %d)\n",
8287c478bd9Sstevel@tonic-gate 		    inj_item2str(type), name, other->defn_lineno);
8297c478bd9Sstevel@tonic-gate 		inj_defn_destroy(defn);
8307c478bd9Sstevel@tonic-gate 		return;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
83380ab886dSwesolows 	(void) inj_strhash_insert(hash, name, (uintptr_t)defn);
8347c478bd9Sstevel@tonic-gate }
835