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 #pragma ident "%Z%%M% %I% %E% SMI"
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <libnvpair.h>
36*7c478bd9Sstevel@tonic-gate #include "evnv.h"
37*7c478bd9Sstevel@tonic-gate #include "out.h"
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #define min(a, b) (((a) <= (b)) ? (a) : (b))
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate extern nv_alloc_t Eft_nv_hdl;
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate static void
outindent(int depth)44*7c478bd9Sstevel@tonic-gate outindent(int depth)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate while (depth-- > 0)
47*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3|O_NONL, " ");
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate * evnv_cmpnvl -- compare two asrus in their nvlist form
52*7c478bd9Sstevel@tonic-gate */
53*7c478bd9Sstevel@tonic-gate int
evnv_cmpnvl(nvlist_t * nvl1,nvlist_t * nvl2,int depth)54*7c478bd9Sstevel@tonic-gate evnv_cmpnvl(nvlist_t *nvl1, nvlist_t *nvl2, int depth)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate * an assumption here is that each list was constructed in the
58*7c478bd9Sstevel@tonic-gate * same order, which is a safe assumption since we built the
59*7c478bd9Sstevel@tonic-gate * list of ourself (well, libtopo did at any rate)
60*7c478bd9Sstevel@tonic-gate */
61*7c478bd9Sstevel@tonic-gate data_type_t t1, t2;
62*7c478bd9Sstevel@tonic-gate nvlist_t **la1 = NULL;
63*7c478bd9Sstevel@tonic-gate nvlist_t **la2 = NULL;
64*7c478bd9Sstevel@tonic-gate nvlist_t *l1 = NULL;
65*7c478bd9Sstevel@tonic-gate nvlist_t *l2 = NULL;
66*7c478bd9Sstevel@tonic-gate nvpair_t *p1 = NULL;
67*7c478bd9Sstevel@tonic-gate nvpair_t *p2 = NULL;
68*7c478bd9Sstevel@tonic-gate uint64_t lv1, lv2;
69*7c478bd9Sstevel@tonic-gate uint_t m, na1, na2;
70*7c478bd9Sstevel@tonic-gate char *s1, *s2;
71*7c478bd9Sstevel@tonic-gate int ret, i;
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate for (;;) {
74*7c478bd9Sstevel@tonic-gate p1 = nvlist_next_nvpair(nvl1, p1);
75*7c478bd9Sstevel@tonic-gate p2 = nvlist_next_nvpair(nvl2, p2);
76*7c478bd9Sstevel@tonic-gate if (p1 == NULL && p2 == NULL) {
77*7c478bd9Sstevel@tonic-gate outindent(depth);
78*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "equal nvls\n");
79*7c478bd9Sstevel@tonic-gate return (0);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate if (p1 == NULL)
82*7c478bd9Sstevel@tonic-gate return (-1);
83*7c478bd9Sstevel@tonic-gate if (p2 == NULL)
84*7c478bd9Sstevel@tonic-gate return (1);
85*7c478bd9Sstevel@tonic-gate s1 = nvpair_name(p1);
86*7c478bd9Sstevel@tonic-gate s2 = nvpair_name(p2);
87*7c478bd9Sstevel@tonic-gate outindent(depth);
88*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: pair %s vs %s", s1, s2);
89*7c478bd9Sstevel@tonic-gate if ((ret = strcmp(s1, s2)) != 0)
90*7c478bd9Sstevel@tonic-gate return (ret);
91*7c478bd9Sstevel@tonic-gate t1 = nvpair_type(p1);
92*7c478bd9Sstevel@tonic-gate t2 = nvpair_type(p2);
93*7c478bd9Sstevel@tonic-gate if (t1 != t2)
94*7c478bd9Sstevel@tonic-gate return (t1 - t2);
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate * We don't compare all possible types, just the
97*7c478bd9Sstevel@tonic-gate * ones we know are likely to actually be present
98*7c478bd9Sstevel@tonic-gate * in nvlists we've generated.
99*7c478bd9Sstevel@tonic-gate */
100*7c478bd9Sstevel@tonic-gate switch (t1) {
101*7c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
102*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist(p1, &l1);
103*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist(p2, &l2);
104*7c478bd9Sstevel@tonic-gate if ((ret = evnv_cmpnvl(l1, l2, depth + 1)) != 0)
105*7c478bd9Sstevel@tonic-gate return (ret);
106*7c478bd9Sstevel@tonic-gate break;
107*7c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
108*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist_array(p1, &la1, &na1);
109*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist_array(p2, &la2, &na2);
110*7c478bd9Sstevel@tonic-gate m = min(na1, na2);
111*7c478bd9Sstevel@tonic-gate for (i = 0; i < m; i++) {
112*7c478bd9Sstevel@tonic-gate if ((ret =
113*7c478bd9Sstevel@tonic-gate evnv_cmpnvl(*la1, *la2, depth + 1)) != 0)
114*7c478bd9Sstevel@tonic-gate return (ret);
115*7c478bd9Sstevel@tonic-gate la1++;
116*7c478bd9Sstevel@tonic-gate la2++;
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate if (na1 < na2)
119*7c478bd9Sstevel@tonic-gate return (-1);
120*7c478bd9Sstevel@tonic-gate else if (na2 < na1)
121*7c478bd9Sstevel@tonic-gate return (1);
122*7c478bd9Sstevel@tonic-gate break;
123*7c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
124*7c478bd9Sstevel@tonic-gate (void) nvpair_value_string(p1, &s1);
125*7c478bd9Sstevel@tonic-gate (void) nvpair_value_string(p2, &s2);
126*7c478bd9Sstevel@tonic-gate if ((ret = strcmp(s1, s2)) != 0) {
127*7c478bd9Sstevel@tonic-gate outindent(depth);
128*7c478bd9Sstevel@tonic-gate if (ret < 0)
129*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3,
130*7c478bd9Sstevel@tonic-gate "cmpnvl: %s < %s", s1, s2);
131*7c478bd9Sstevel@tonic-gate else
132*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3,
133*7c478bd9Sstevel@tonic-gate "cmpnvl: %s > %s", s1, s2);
134*7c478bd9Sstevel@tonic-gate return (ret);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate break;
137*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
138*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
139*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(p1, &lv1);
140*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(p2, &lv2);
141*7c478bd9Sstevel@tonic-gate outindent(depth);
142*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %llu vs %llu", lv1, lv2);
143*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
144*7c478bd9Sstevel@tonic-gate return (1);
145*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
146*7c478bd9Sstevel@tonic-gate return (-1);
147*7c478bd9Sstevel@tonic-gate break;
148*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
149*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
150*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int64(p1, (int64_t *)&lv1);
151*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int64(p2, (int64_t *)&lv2);
152*7c478bd9Sstevel@tonic-gate outindent(depth);
153*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %lld vs %lld", lv1, lv2);
154*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
155*7c478bd9Sstevel@tonic-gate return (1);
156*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
157*7c478bd9Sstevel@tonic-gate return (-1);
158*7c478bd9Sstevel@tonic-gate break;
159*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
160*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
161*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint32(p1, (uint32_t *)&lv1);
162*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint32(p2, (uint32_t *)&lv2);
163*7c478bd9Sstevel@tonic-gate outindent(depth);
164*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
165*7c478bd9Sstevel@tonic-gate *(uint32_t *)&lv1, *(uint32_t *)&lv2);
166*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
167*7c478bd9Sstevel@tonic-gate return (1);
168*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
169*7c478bd9Sstevel@tonic-gate return (-1);
170*7c478bd9Sstevel@tonic-gate break;
171*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
172*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
173*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int32(p1, (int32_t *)&lv1);
174*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int32(p2, (int32_t *)&lv2);
175*7c478bd9Sstevel@tonic-gate outindent(depth);
176*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
177*7c478bd9Sstevel@tonic-gate *(int32_t *)&lv1, *(int32_t *)&lv2);
178*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
179*7c478bd9Sstevel@tonic-gate return (1);
180*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
181*7c478bd9Sstevel@tonic-gate return (-1);
182*7c478bd9Sstevel@tonic-gate break;
183*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
184*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
185*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint16(p1, (uint16_t *)&lv1);
186*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint16(p2, (uint16_t *)&lv2);
187*7c478bd9Sstevel@tonic-gate outindent(depth);
188*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
189*7c478bd9Sstevel@tonic-gate *(uint16_t *)&lv1, *(uint16_t *)&lv2);
190*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
191*7c478bd9Sstevel@tonic-gate return (1);
192*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
193*7c478bd9Sstevel@tonic-gate return (-1);
194*7c478bd9Sstevel@tonic-gate break;
195*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
196*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
197*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int16(p1, (int16_t *)&lv1);
198*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int16(p2, (int16_t *)&lv2);
199*7c478bd9Sstevel@tonic-gate outindent(depth);
200*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
201*7c478bd9Sstevel@tonic-gate *(int16_t *)&lv1, *(int16_t *)&lv2);
202*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
203*7c478bd9Sstevel@tonic-gate return (1);
204*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
205*7c478bd9Sstevel@tonic-gate return (-1);
206*7c478bd9Sstevel@tonic-gate break;
207*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
208*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
209*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint8(p1, (uint8_t *)&lv1);
210*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint8(p2, (uint8_t *)&lv2);
211*7c478bd9Sstevel@tonic-gate outindent(depth);
212*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %u vs %u",
213*7c478bd9Sstevel@tonic-gate *(uint8_t *)&lv1, *(uint8_t *)&lv2);
214*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
215*7c478bd9Sstevel@tonic-gate return (1);
216*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
217*7c478bd9Sstevel@tonic-gate return (-1);
218*7c478bd9Sstevel@tonic-gate break;
219*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
220*7c478bd9Sstevel@tonic-gate lv1 = lv2 = 0;
221*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int8(p1, (int8_t *)&lv1);
222*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int8(p2, (int8_t *)&lv2);
223*7c478bd9Sstevel@tonic-gate outindent(depth);
224*7c478bd9Sstevel@tonic-gate out(O_ALTFP|O_VERB3, "cmpnvl: %d vs %d",
225*7c478bd9Sstevel@tonic-gate *(int8_t *)&lv1, *(int8_t *)&lv2);
226*7c478bd9Sstevel@tonic-gate if (lv1 > lv2)
227*7c478bd9Sstevel@tonic-gate return (1);
228*7c478bd9Sstevel@tonic-gate else if (lv2 > lv1)
229*7c478bd9Sstevel@tonic-gate return (-1);
230*7c478bd9Sstevel@tonic-gate break;
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate /*
236*7c478bd9Sstevel@tonic-gate * evnv_dupnvl -- duplicate a payload nvlist, keeping only the interesting stuff
237*7c478bd9Sstevel@tonic-gate */
238*7c478bd9Sstevel@tonic-gate nvlist_t *
evnv_dupnvl(nvlist_t * nvp)239*7c478bd9Sstevel@tonic-gate evnv_dupnvl(nvlist_t *nvp)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate nvlist_t *retval = NULL;
242*7c478bd9Sstevel@tonic-gate int nvret;
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate if (nvp == NULL)
245*7c478bd9Sstevel@tonic-gate return (NULL);
246*7c478bd9Sstevel@tonic-gate
247*7c478bd9Sstevel@tonic-gate if ((nvret = nvlist_xdup(nvp, &retval, &Eft_nv_hdl)) != 0)
248*7c478bd9Sstevel@tonic-gate out(O_DIE, "dupnvl: dup failed: %d", nvret);
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate return (retval);
251*7c478bd9Sstevel@tonic-gate }
252