4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 #include <sys/types.h>
30 #include <sys/inttypes.h>
31 #include "libnvpair.h"
34 * libnvpair - A tools library for manipulating <name, value> pairs.
36 * This library provides routines packing an unpacking nv pairs
37 * for transporting data across process boundaries, transporting
38 * between kernel and userland, and possibly saving onto disk files.
42 indent(FILE *fp, int depth)
45 (void) fprintf(fp, "\t");
49 * nvlist_print - Prints elements in an event buffer
53 nvlist_print_with_indent(FILE *fp, nvlist_t *nvl, int depth)
64 (void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
66 nvp = nvlist_next_nvpair(nvl, NULL);
69 data_type_t type = nvpair_type(nvp);
72 name = nvpair_name(nvp);
73 (void) fprintf(fp, "\t%s =", name);
76 case DATA_TYPE_BOOLEAN: {
77 (void) fprintf(fp, " 1");
80 case DATA_TYPE_BOOLEAN_VALUE: {
82 (void) nvpair_value_boolean_value(nvp, &val);
83 (void) fprintf(fp, " %d", val);
86 case DATA_TYPE_BYTE: {
88 (void) nvpair_value_byte(nvp, &val);
89 (void) fprintf(fp, " 0x%2.2x", val);
92 case DATA_TYPE_INT8: {
94 (void) nvpair_value_int8(nvp, &val);
95 (void) fprintf(fp, " %d", val);
98 case DATA_TYPE_UINT8: {
100 (void) nvpair_value_uint8(nvp, &val);
101 (void) fprintf(fp, " 0x%x", val);
104 case DATA_TYPE_INT16: {
106 (void) nvpair_value_int16(nvp, &val);
107 (void) fprintf(fp, " %d", val);
110 case DATA_TYPE_UINT16: {
112 (void) nvpair_value_uint16(nvp, &val);
113 (void) fprintf(fp, " 0x%x", val);
116 case DATA_TYPE_INT32: {
118 (void) nvpair_value_int32(nvp, &val);
119 (void) fprintf(fp, " %d", val);
122 case DATA_TYPE_UINT32: {
124 (void) nvpair_value_uint32(nvp, &val);
125 (void) fprintf(fp, " 0x%x", val);
128 case DATA_TYPE_INT64: {
130 (void) nvpair_value_int64(nvp, &val);
131 (void) fprintf(fp, " %lld", (longlong_t)val);
134 case DATA_TYPE_UINT64: {
136 (void) nvpair_value_uint64(nvp, &val);
137 (void) fprintf(fp, " 0x%llx", (u_longlong_t)val);
140 case DATA_TYPE_DOUBLE: {
142 (void) nvpair_value_double(nvp, &val);
143 (void) fprintf(fp, " 0x%llf", val);
146 case DATA_TYPE_STRING: {
148 (void) nvpair_value_string(nvp, &val);
149 (void) fprintf(fp, " %s", val);
152 case DATA_TYPE_BOOLEAN_ARRAY: {
154 (void) nvpair_value_boolean_array(nvp, &val, &nelem);
155 for (i = 0; i < nelem; i++)
156 (void) fprintf(fp, " %d", val[i]);
159 case DATA_TYPE_BYTE_ARRAY: {
161 (void) nvpair_value_byte_array(nvp, &val, &nelem);
162 for (i = 0; i < nelem; i++)
163 (void) fprintf(fp, " 0x%2.2x", val[i]);
166 case DATA_TYPE_INT8_ARRAY: {
168 (void) nvpair_value_int8_array(nvp, &val, &nelem);
169 for (i = 0; i < nelem; i++)
170 (void) fprintf(fp, " %d", val[i]);
173 case DATA_TYPE_UINT8_ARRAY: {
175 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
176 for (i = 0; i < nelem; i++)
177 (void) fprintf(fp, " 0x%x", val[i]);
180 case DATA_TYPE_INT16_ARRAY: {
182 (void) nvpair_value_int16_array(nvp, &val, &nelem);
183 for (i = 0; i < nelem; i++)
184 (void) fprintf(fp, " %d", val[i]);
187 case DATA_TYPE_UINT16_ARRAY: {
189 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
190 for (i = 0; i < nelem; i++)
191 (void) fprintf(fp, " 0x%x", val[i]);
194 case DATA_TYPE_INT32_ARRAY: {
196 (void) nvpair_value_int32_array(nvp, &val, &nelem);
197 for (i = 0; i < nelem; i++)
198 (void) fprintf(fp, " %d", val[i]);
201 case DATA_TYPE_UINT32_ARRAY: {
203 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
204 for (i = 0; i < nelem; i++)
205 (void) fprintf(fp, " 0x%x", val[i]);
208 case DATA_TYPE_INT64_ARRAY: {
210 (void) nvpair_value_int64_array(nvp, &val, &nelem);
211 for (i = 0; i < nelem; i++)
212 (void) fprintf(fp, " %lld", (longlong_t)val[i]);
215 case DATA_TYPE_UINT64_ARRAY: {
217 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
218 for (i = 0; i < nelem; i++)
219 (void) fprintf(fp, " 0x%llx",
220 (u_longlong_t)val[i]);
223 case DATA_TYPE_STRING_ARRAY: {
225 (void) nvpair_value_string_array(nvp, &val, &nelem);
226 for (i = 0; i < nelem; i++)
227 (void) fprintf(fp, " %s", val[i]);
230 case DATA_TYPE_HRTIME: {
232 (void) nvpair_value_hrtime(nvp, &val);
233 (void) fprintf(fp, " 0x%llx", val);
236 case DATA_TYPE_NVLIST: {
238 (void) nvpair_value_nvlist(nvp, &val);
239 (void) fprintf(fp, " (embedded nvlist)\n");
240 nvlist_print_with_indent(fp, val, depth + 1);
241 indent(fp, depth + 1);
242 (void) fprintf(fp, "(end %s)\n", name);
245 case DATA_TYPE_NVLIST_ARRAY: {
247 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
248 (void) fprintf(fp, " (array of embedded nvlists)\n");
249 for (i = 0; i < nelem; i++) {
250 indent(fp, depth + 1);
252 "(start %s[%d])\n", name, i);
253 nvlist_print_with_indent(fp, val[i], depth + 1);
254 indent(fp, depth + 1);
255 (void) fprintf(fp, "(end %s[%d])\n", name, i);
260 (void) fprintf(fp, " unknown data type (%d)", type);
263 (void) fprintf(fp, "\n");
264 nvp = nvlist_next_nvpair(nvl, nvp);
269 nvlist_print(FILE *fp, nvlist_t *nvl)
271 nvlist_print_with_indent(fp, nvl, 0);
275 #define NVP(elem, type, vtype, ptype, format) { \
278 (void) nvpair_value_##type(elem, &value); \
279 (void) printf("%*s%s: " format "\n", indent, "", \
280 nvpair_name(elem), (ptype)value); \
283 #define NVPA(elem, type, vtype, ptype, format) { \
287 (void) nvpair_value_##type(elem, &value, &count); \
288 for (i = 0; i < count; i++) { \
289 (void) printf("%*s%s[%d]: " format "\n", indent, "", \
290 nvpair_name(elem), i, (ptype)value[i]); \
295 * Similar to nvlist_print() but handles arrays slightly differently.
298 dump_nvlist(nvlist_t *list, int indent)
300 nvpair_t *elem = NULL;
301 boolean_t bool_value;
302 nvlist_t *nvlist_value;
303 nvlist_t **nvlist_array_value;
310 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
311 switch (nvpair_type(elem)) {
312 case DATA_TYPE_BOOLEAN_VALUE:
313 (void) nvpair_value_boolean_value(elem, &bool_value);
314 (void) printf("%*s%s: %s\n", indent, "",
315 nvpair_name(elem), bool_value ? "true" : "false");
319 NVP(elem, byte, uchar_t, int, "%u");
323 NVP(elem, int8, int8_t, int, "%d");
326 case DATA_TYPE_UINT8:
327 NVP(elem, uint8, uint8_t, int, "%u");
330 case DATA_TYPE_INT16:
331 NVP(elem, int16, int16_t, int, "%d");
334 case DATA_TYPE_UINT16:
335 NVP(elem, uint16, uint16_t, int, "%u");
338 case DATA_TYPE_INT32:
339 NVP(elem, int32, int32_t, long, "%ld");
342 case DATA_TYPE_UINT32:
343 NVP(elem, uint32, uint32_t, ulong_t, "%lu");
346 case DATA_TYPE_INT64:
347 NVP(elem, int64, int64_t, longlong_t, "%lld");
350 case DATA_TYPE_UINT64:
351 NVP(elem, uint64, uint64_t, u_longlong_t, "%llu");
354 case DATA_TYPE_STRING:
355 NVP(elem, string, char *, char *, "'%s'");
358 case DATA_TYPE_BYTE_ARRAY:
359 NVPA(elem, byte_array, uchar_t, int, "%u");
362 case DATA_TYPE_INT8_ARRAY:
363 NVPA(elem, int8_array, int8_t, int, "%d");
366 case DATA_TYPE_UINT8_ARRAY:
367 NVPA(elem, uint8_array, uint8_t, int, "%u");
370 case DATA_TYPE_INT16_ARRAY:
371 NVPA(elem, int16_array, int16_t, int, "%d");
374 case DATA_TYPE_UINT16_ARRAY:
375 NVPA(elem, uint16_array, uint16_t, int, "%u");
378 case DATA_TYPE_INT32_ARRAY:
379 NVPA(elem, int32_array, int32_t, long, "%ld");
382 case DATA_TYPE_UINT32_ARRAY:
383 NVPA(elem, uint32_array, uint32_t, ulong_t, "%lu");
386 case DATA_TYPE_INT64_ARRAY:
387 NVPA(elem, int64_array, int64_t, longlong_t, "%lld");
390 case DATA_TYPE_UINT64_ARRAY:
391 NVPA(elem, uint64_array, uint64_t, u_longlong_t,
395 case DATA_TYPE_STRING_ARRAY:
396 NVPA(elem, string_array, char *, char *, "'%s'");
399 case DATA_TYPE_NVLIST:
400 (void) nvpair_value_nvlist(elem, &nvlist_value);
401 (void) printf("%*s%s:\n", indent, "",
403 dump_nvlist(nvlist_value, indent + 4);
406 case DATA_TYPE_NVLIST_ARRAY:
407 (void) nvpair_value_nvlist_array(elem,
408 &nvlist_array_value, &count);
409 for (i = 0; i < count; i++) {
410 (void) printf("%*s%s[%u]:\n", indent, "",
411 nvpair_name(elem), i);
412 dump_nvlist(nvlist_array_value[i], indent + 4);
417 (void) printf(dgettext(TEXT_DOMAIN, "bad config type "
418 "%d for %s\n"), nvpair_type(elem),
425 * Determine if string 'value' matches 'nvp' value. The 'value' string is
426 * converted, depending on the type of 'nvp', prior to match. For numeric
427 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
428 * is an array type, 'ai' is the index into the array against which we are
429 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
430 * in a regex_t compilation of value in 'value_regex' to trigger regular
431 * expression string match instead of simple strcmp().
433 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
434 * related to value syntax error and 'ep' is non-NULL, *ep will point into
435 * the 'value' string at the location where the error exists.
437 * NOTE: It may be possible to move the non-regex_t version of this into
438 * common code used by library/kernel/boot.
441 nvpair_value_match_regex(nvpair_t *nvp, int ai,
442 char *value, regex_t *value_regex, char **ep)
451 if ((nvp == NULL) || (value == NULL))
452 return (-1); /* error fail match - invalid args */
454 /* make sure array and index combination make sense */
455 if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
456 (!nvpair_type_is_array(nvp) && (ai >= 0)))
457 return (-1); /* error fail match - bad index */
459 /* non-string values should be single 'chunk' */
460 if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
461 (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
462 value += strspn(value, " \t");
463 evalue = value + strcspn(value, " \t");
467 return (-1); /* error fail match - syntax */
472 switch (nvpair_type(nvp)) {
473 case DATA_TYPE_STRING: {
476 /* check string value for match */
477 if (nvpair_value_string(nvp, &val) == 0) {
479 if (regexec(value_regex, val,
480 (size_t)0, NULL, 0) == 0)
481 return (1); /* match */
483 if (strcmp(value, val) == 0)
484 return (1); /* match */
489 case DATA_TYPE_STRING_ARRAY: {
492 /* check indexed string value of array for match */
493 if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
496 if (regexec(value_regex, val_array[ai],
497 (size_t)0, NULL, 0) == 0)
500 if (strcmp(value, val_array[ai]) == 0)
506 case DATA_TYPE_BYTE: {
507 uchar_t val, val_arg;
509 /* scanf uchar_t from value and check for match */
510 sr = sscanf(value, "%c", &val_arg);
511 if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
516 case DATA_TYPE_BYTE_ARRAY: {
517 uchar_t *val_array, val_arg;
520 /* check indexed value of array for match */
521 sr = sscanf(value, "%c", &val_arg);
523 (nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
525 (val_array[ai] == val_arg))
529 case DATA_TYPE_INT8: {
532 /* scanf int8_t from value and check for match */
533 sr = sscanf(value, "%"SCNi8, &val_arg);
535 (nvpair_value_int8(nvp, &val) == 0) &&
540 case DATA_TYPE_INT8_ARRAY: {
541 int8_t *val_array, val_arg;
543 /* check indexed value of array for match */
544 sr = sscanf(value, "%"SCNi8, &val_arg);
546 (nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
548 (val_array[ai] == val_arg))
552 case DATA_TYPE_UINT8: {
553 uint8_t val, val_arg;
555 /* scanf uint8_t from value and check for match */
556 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
558 (nvpair_value_uint8(nvp, &val) == 0) &&
563 case DATA_TYPE_UINT8_ARRAY: {
564 uint8_t *val_array, val_arg;
566 /* check indexed value of array for match */
567 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
569 (nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
571 (val_array[ai] == val_arg))
575 case DATA_TYPE_INT16: {
576 int16_t val, val_arg;
578 /* scanf int16_t from value and check for match */
579 sr = sscanf(value, "%"SCNi16, &val_arg);
581 (nvpair_value_int16(nvp, &val) == 0) &&
586 case DATA_TYPE_INT16_ARRAY: {
587 int16_t *val_array, val_arg;
589 /* check indexed value of array for match */
590 sr = sscanf(value, "%"SCNi16, &val_arg);
592 (nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
594 (val_array[ai] == val_arg))
598 case DATA_TYPE_UINT16: {
599 uint16_t val, val_arg;
601 /* scanf uint16_t from value and check for match */
602 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
604 (nvpair_value_uint16(nvp, &val) == 0) &&
609 case DATA_TYPE_UINT16_ARRAY: {
610 uint16_t *val_array, val_arg;
612 /* check indexed value of array for match */
613 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
615 (nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
617 (val_array[ai] == val_arg))
621 case DATA_TYPE_INT32: {
622 int32_t val, val_arg;
624 /* scanf int32_t from value and check for match */
625 sr = sscanf(value, "%"SCNi32, &val_arg);
627 (nvpair_value_int32(nvp, &val) == 0) &&
632 case DATA_TYPE_INT32_ARRAY: {
633 int32_t *val_array, val_arg;
635 /* check indexed value of array for match */
636 sr = sscanf(value, "%"SCNi32, &val_arg);
638 (nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
640 (val_array[ai] == val_arg))
644 case DATA_TYPE_UINT32: {
645 uint32_t val, val_arg;
647 /* scanf uint32_t from value and check for match */
648 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
650 (nvpair_value_uint32(nvp, &val) == 0) &&
655 case DATA_TYPE_UINT32_ARRAY: {
656 uint32_t *val_array, val_arg;
658 /* check indexed value of array for match */
659 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
661 (nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
663 (val_array[ai] == val_arg))
667 case DATA_TYPE_INT64: {
668 int64_t val, val_arg;
670 /* scanf int64_t from value and check for match */
671 sr = sscanf(value, "%"SCNi64, &val_arg);
673 (nvpair_value_int64(nvp, &val) == 0) &&
678 case DATA_TYPE_INT64_ARRAY: {
679 int64_t *val_array, val_arg;
681 /* check indexed value of array for match */
682 sr = sscanf(value, "%"SCNi64, &val_arg);
684 (nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
686 (val_array[ai] == val_arg))
690 case DATA_TYPE_UINT64: {
691 uint64_t val_arg, val;
693 /* scanf uint64_t from value and check for match */
694 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
696 (nvpair_value_uint64(nvp, &val) == 0) &&
701 case DATA_TYPE_UINT64_ARRAY: {
702 uint64_t *val_array, val_arg;
704 /* check indexed value of array for match */
705 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
707 (nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
709 (val_array[ai] == val_arg))
713 case DATA_TYPE_BOOLEAN_VALUE: {
714 boolean_t val, val_arg;
716 /* scanf boolean_t from value and check for match */
717 sr = sscanf(value, "%"SCNi32, &val_arg);
719 (nvpair_value_boolean_value(nvp, &val) == 0) &&
724 case DATA_TYPE_BOOLEAN_ARRAY: {
725 boolean_t *val_array, val_arg;
727 /* check indexed value of array for match */
728 sr = sscanf(value, "%"SCNi32, &val_arg);
730 (nvpair_value_boolean_array(nvp,
731 &val_array, &a_len) == 0) &&
733 (val_array[ai] == val_arg))
737 case DATA_TYPE_HRTIME:
738 case DATA_TYPE_NVLIST:
739 case DATA_TYPE_NVLIST_ARRAY:
740 case DATA_TYPE_BOOLEAN:
741 case DATA_TYPE_DOUBLE:
742 case DATA_TYPE_UNKNOWN:
745 * unknown/unsupported data type
747 return (-1); /* error fail match */
751 * check to see if sscanf failed conversion, return approximate
757 return (-1); /* error fail match - syntax */
760 return (0); /* fail match */
764 nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep)
766 return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));