1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * evnv.c -- eversholt specific nvpair manipulation functions
27*7c478bd9Sstevel@tonic-gate  *
28*7c478bd9Sstevel@tonic-gate  * this module provides the simulated fault management exercise.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <libnvpair.h>
34*7c478bd9Sstevel@tonic-gate #include "evnv.h"
35*7c478bd9Sstevel@tonic-gate #include "out.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	min(a, b)	(((a) <= (b)) ? (a) : (b))
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate extern nv_alloc_t Eft_nv_hdl;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static void
outindent(int depth)42*7c478bd9Sstevel@tonic-gate outindent(int depth)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	while (depth-- > 0)
45*7c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB3|O_NONL, "   ");
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * evnv_cmpnvl -- compare two asrus in their nvlist form
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate int
evnv_cmpnvl(nvlist_t * nvl1,nvlist_t * nvl2,int depth)52*7c478bd9Sstevel@tonic-gate evnv_cmpnvl(nvlist_t *nvl1, nvlist_t *nvl2, int depth)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	/*
55*7c478bd9Sstevel@tonic-gate 	 * an assumption here is that each list was constructed in the
56*7c478bd9Sstevel@tonic-gate 	 * same order, which is a safe assumption since we built the
57*7c478bd9Sstevel@tonic-gate 	 * list of ourself (well, libtopo did at any rate)
58*7c478bd9Sstevel@tonic-gate 	 */
59*7c478bd9Sstevel@tonic-gate 	data_type_t t1, t2;
60*7c478bd9Sstevel@tonic-gate 	nvlist_t **la1 = NULL;
61*7c478bd9Sstevel@tonic-gate 	nvlist_t **la2 = NULL;
62*7c478bd9Sstevel@tonic-gate 	nvlist_t *l1 = NULL;
63*7c478bd9Sstevel@tonic-gate 	nvlist_t *l2 = NULL;
64*7c478bd9Sstevel@tonic-gate 	nvpair_t *p1 = NULL;
65*7c478bd9Sstevel@tonic-gate 	nvpair_t *p2 = NULL;
66*7c478bd9Sstevel@tonic-gate 	uint64_t lv1, lv2;
67*7c478bd9Sstevel@tonic-gate 	uint_t m, na1, na2;
68*7c478bd9Sstevel@tonic-gate 	char *s1, *s2;
69*7c478bd9Sstevel@tonic-gate 	int ret, i;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	for (;;) {
72*7c478bd9Sstevel@tonic-gate 		p1 = nvlist_next_nvpair(nvl1, p1);
73*7c478bd9Sstevel@tonic-gate 		p2 = nvlist_next_nvpair(nvl2, p2);
74*7c478bd9Sstevel@tonic-gate 		if (p1 == NULL && p2 == NULL) {
75*7c478bd9Sstevel@tonic-gate 			outindent(depth);
76*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "equal nvls\n");
77*7c478bd9Sstevel@tonic-gate 			return (0);
78*7c478bd9Sstevel@tonic-gate 		}
79*7c478bd9Sstevel@tonic-gate 		if (p1 == NULL)
80*7c478bd9Sstevel@tonic-gate 			return (-1);
81*7c478bd9Sstevel@tonic-gate 		if (p2 == NULL)
82*7c478bd9Sstevel@tonic-gate 			return (1);
83*7c478bd9Sstevel@tonic-gate 		s1 = nvpair_name(p1);
84*7c478bd9Sstevel@tonic-gate 		s2 = nvpair_name(p2);
85*7c478bd9Sstevel@tonic-gate 		outindent(depth);
86*7c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB3, "cmpnvl: pair %s vs %s", s1, s2);
87*7c478bd9Sstevel@tonic-gate 		if ((ret = strcmp(s1, s2)) != 0)
88*7c478bd9Sstevel@tonic-gate 			return (ret);
89*7c478bd9Sstevel@tonic-gate 		t1 = nvpair_type(p1);
90*7c478bd9Sstevel@tonic-gate 		t2 = nvpair_type(p2);
91*7c478bd9Sstevel@tonic-gate 		if (t1 != t2)
92*7c478bd9Sstevel@tonic-gate 			return (t1 - t2);
93*7c478bd9Sstevel@tonic-gate 		/*
94*7c478bd9Sstevel@tonic-gate 		 * We don't compare all possible types, just the
95*7c478bd9Sstevel@tonic-gate 		 * ones we know are likely to actually be present
96*7c478bd9Sstevel@tonic-gate 		 * in nvlists we've generated.
97*7c478bd9Sstevel@tonic-gate 		 */
98*7c478bd9Sstevel@tonic-gate 		switch (t1) {
99*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
100*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist(p1, &l1);
101*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist(p2, &l2);
102*7c478bd9Sstevel@tonic-gate 			if ((ret = evnv_cmpnvl(l1, l2, depth + 1)) != 0)
103*7c478bd9Sstevel@tonic-gate 				return (ret);
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
106*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist_array(p1, &la1, &na1);
107*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist_array(p2, &la2, &na2);
108*7c478bd9Sstevel@tonic-gate 			m = min(na1, na2);
109*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < m; i++) {
110*7c478bd9Sstevel@tonic-gate 				if ((ret =
111*7c478bd9Sstevel@tonic-gate 				    evnv_cmpnvl(*la1, *la2, depth + 1)) != 0)
112*7c478bd9Sstevel@tonic-gate 					return (ret);
113*7c478bd9Sstevel@tonic-gate 				la1++;
114*7c478bd9Sstevel@tonic-gate 				la2++;
115*7c478bd9Sstevel@tonic-gate 			}
116*7c478bd9Sstevel@tonic-gate 			if (na1 < na2)
117*7c478bd9Sstevel@tonic-gate 				return (-1);
118*7c478bd9Sstevel@tonic-gate 			else if (na2 < na1)
119*7c478bd9Sstevel@tonic-gate 				return (1);
120*7c478bd9Sstevel@tonic-gate 			break;
121*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
122*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string(p1, &s1);
123*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string(p2, &s2);
124*7c478bd9Sstevel@tonic-gate 			if ((ret = strcmp(s1, s2)) != 0) {
125*7c478bd9Sstevel@tonic-gate 				outindent(depth);
126*7c478bd9Sstevel@tonic-gate 				if (ret < 0)
127*7c478bd9Sstevel@tonic-gate 					out(O_ALTFP|O_VERB3,
128*7c478bd9Sstevel@tonic-gate 					    "cmpnvl: %s < %s", s1, s2);
129*7c478bd9Sstevel@tonic-gate 				else
130*7c478bd9Sstevel@tonic-gate 					out(O_ALTFP|O_VERB3,
131*7c478bd9Sstevel@tonic-gate 					    "cmpnvl: %s > %s", s1, s2);
132*7c478bd9Sstevel@tonic-gate 				return (ret);
133*7c478bd9Sstevel@tonic-gate 			}
134*7c478bd9Sstevel@tonic-gate 			break;
135*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
136*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
137*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint64(p1, &lv1);
138*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint64(p2, &lv2);
139*7c478bd9Sstevel@tonic-gate 			outindent(depth);
140*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %llu vs %llu", lv1, lv2);
141*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
142*7c478bd9Sstevel@tonic-gate 				return (1);
143*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
144*7c478bd9Sstevel@tonic-gate 				return (-1);
145*7c478bd9Sstevel@tonic-gate 			break;
146*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
147*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
148*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64(p1, (int64_t *)&lv1);
149*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64(p2, (int64_t *)&lv2);
150*7c478bd9Sstevel@tonic-gate 			outindent(depth);
151*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %lld vs %lld", lv1, lv2);
152*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
153*7c478bd9Sstevel@tonic-gate 				return (1);
154*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
155*7c478bd9Sstevel@tonic-gate 				return (-1);
156*7c478bd9Sstevel@tonic-gate 			break;
157*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
158*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
159*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint32(p1, (uint32_t *)&lv1);
160*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint32(p2, (uint32_t *)&lv2);
161*7c478bd9Sstevel@tonic-gate 			outindent(depth);
162*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
163*7c478bd9Sstevel@tonic-gate 			    *(uint32_t *)&lv1, *(uint32_t *)&lv2);
164*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
165*7c478bd9Sstevel@tonic-gate 				return (1);
166*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
167*7c478bd9Sstevel@tonic-gate 				return (-1);
168*7c478bd9Sstevel@tonic-gate 			break;
169*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
170*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
171*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(p1, (int32_t *)&lv1);
172*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(p2, (int32_t *)&lv2);
173*7c478bd9Sstevel@tonic-gate 			outindent(depth);
174*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
175*7c478bd9Sstevel@tonic-gate 			    *(int32_t *)&lv1, *(int32_t *)&lv2);
176*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
177*7c478bd9Sstevel@tonic-gate 				return (1);
178*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
179*7c478bd9Sstevel@tonic-gate 				return (-1);
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
182*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
183*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint16(p1, (uint16_t *)&lv1);
184*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint16(p2, (uint16_t *)&lv2);
185*7c478bd9Sstevel@tonic-gate 			outindent(depth);
186*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
187*7c478bd9Sstevel@tonic-gate 			    *(uint16_t *)&lv1, *(uint16_t *)&lv2);
188*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
189*7c478bd9Sstevel@tonic-gate 				return (1);
190*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
191*7c478bd9Sstevel@tonic-gate 				return (-1);
192*7c478bd9Sstevel@tonic-gate 			break;
193*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
194*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
195*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int16(p1, (int16_t *)&lv1);
196*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int16(p2, (int16_t *)&lv2);
197*7c478bd9Sstevel@tonic-gate 			outindent(depth);
198*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
199*7c478bd9Sstevel@tonic-gate 			    *(int16_t *)&lv1, *(int16_t *)&lv2);
200*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
201*7c478bd9Sstevel@tonic-gate 				return (1);
202*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
203*7c478bd9Sstevel@tonic-gate 				return (-1);
204*7c478bd9Sstevel@tonic-gate 			break;
205*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
206*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
207*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint8(p1, (uint8_t *)&lv1);
208*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint8(p2, (uint8_t *)&lv2);
209*7c478bd9Sstevel@tonic-gate 			outindent(depth);
210*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
211*7c478bd9Sstevel@tonic-gate 			    *(uint8_t *)&lv1, *(uint8_t *)&lv2);
212*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
213*7c478bd9Sstevel@tonic-gate 				return (1);
214*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
215*7c478bd9Sstevel@tonic-gate 				return (-1);
216*7c478bd9Sstevel@tonic-gate 			break;
217*7c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8:
218*7c478bd9Sstevel@tonic-gate 			lv1 = lv2 = 0;
219*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int8(p1, (int8_t *)&lv1);
220*7c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int8(p2, (int8_t *)&lv2);
221*7c478bd9Sstevel@tonic-gate 			outindent(depth);
222*7c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
223*7c478bd9Sstevel@tonic-gate 			    *(int8_t *)&lv1, *(int8_t *)&lv2);
224*7c478bd9Sstevel@tonic-gate 			if (lv1 > lv2)
225*7c478bd9Sstevel@tonic-gate 				return (1);
226*7c478bd9Sstevel@tonic-gate 			else if (lv2 > lv1)
227*7c478bd9Sstevel@tonic-gate 				return (-1);
228*7c478bd9Sstevel@tonic-gate 			break;
229*7c478bd9Sstevel@tonic-gate 		}
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate  * evnv_dupnvl -- duplicate a payload nvlist, keeping only the interesting stuff
235*7c478bd9Sstevel@tonic-gate  */
236*7c478bd9Sstevel@tonic-gate nvlist_t *
evnv_dupnvl(nvlist_t * nvp)237*7c478bd9Sstevel@tonic-gate evnv_dupnvl(nvlist_t *nvp)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	nvlist_t *retval = NULL;
240*7c478bd9Sstevel@tonic-gate 	int nvret;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	if (nvp == NULL)
243*7c478bd9Sstevel@tonic-gate 		return (NULL);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	if ((nvret = nvlist_xdup(nvp, &retval, &Eft_nv_hdl)) != 0)
246*7c478bd9Sstevel@tonic-gate 		out(O_DIE, "dupnvl: dup failed: %d", nvret);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	return (retval);
249*7c478bd9Sstevel@tonic-gate }
250