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