2 * Copyright (c) 2009-2013 The FreeBSD Foundation
3 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/queue.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/systm.h>
46 #include <machine/stdarg.h>
49 #include <sys/socket.h>
70 #include "nvlist_impl.h"
71 #include "nvpair_impl.h"
75 #define PJDLOG_ASSERT(...) MPASS(__VA_ARGS__)
76 #define PJDLOG_RASSERT(expr, ...) KASSERT(expr, (__VA_ARGS__))
77 #define PJDLOG_ABORT(...) panic(__VA_ARGS__)
80 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
81 #define PJDLOG_RASSERT(expr, ...) assert(expr)
82 #define PJDLOG_ABORT(...) do { \
83 fprintf(stderr, "%s:%u: ", __FILE__, __LINE__); \
84 fprintf(stderr, __VA_ARGS__); \
85 fprintf(stderr, "\n"); \
91 #define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN | NV_FLAG_IN_ARRAY)
92 #define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE | NV_FLAG_NO_UNIQUE)
93 #define NV_FLAG_ALL_MASK (NV_FLAG_PRIVATE_MASK | NV_FLAG_PUBLIC_MASK)
95 #define NVLIST_MAGIC 0x6e766c /* "nvl" */
100 nvpair_t *nvl_parent;
101 nvpair_t *nvl_array_next;
102 struct nvl_head nvl_head;
105 #define NVLIST_ASSERT(nvl) do { \
106 PJDLOG_ASSERT((nvl) != NULL); \
107 PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \
111 MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
114 #define NVPAIR_ASSERT(nvp) nvpair_assert(nvp)
116 #define NVLIST_HEADER_MAGIC 0x6c
117 #define NVLIST_HEADER_VERSION 0x00
118 struct nvlist_header {
120 uint8_t nvlh_version;
122 uint64_t nvlh_descriptors;
127 nvlist_create(int flags)
131 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
133 nvl = nv_malloc(sizeof(*nvl));
137 nvl->nvl_flags = flags;
138 nvl->nvl_parent = NULL;
139 nvl->nvl_array_next = NULL;
140 TAILQ_INIT(&nvl->nvl_head);
141 nvl->nvl_magic = NVLIST_MAGIC;
147 nvlist_destroy(nvlist_t *nvl)
158 while ((nvp = nvlist_first_nvpair(nvl)) != NULL) {
159 nvlist_remove_nvpair(nvl, nvp);
162 if (nvl->nvl_array_next != NULL)
163 nvpair_free_structure(nvl->nvl_array_next);
164 nvl->nvl_array_next = NULL;
165 nvl->nvl_parent = NULL;
173 nvlist_set_error(nvlist_t *nvl, int error)
176 PJDLOG_ASSERT(error != 0);
179 * Check for error != 0 so that we don't do the wrong thing if somebody
180 * tries to abuse this API when asserts are disabled.
182 if (nvl != NULL && error != 0 && nvl->nvl_error == 0)
183 nvl->nvl_error = error;
187 nvlist_error(const nvlist_t *nvl)
195 return (nvl->nvl_error);
199 nvlist_get_nvpair_parent(const nvlist_t *nvl)
204 return (nvl->nvl_parent);
208 nvlist_get_parent(const nvlist_t *nvl, void **cookiep)
214 nvp = nvl->nvl_parent;
220 return (nvpair_nvlist(nvp));
224 nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent)
229 nvl->nvl_parent = parent;
233 nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele)
239 nvl->nvl_flags |= NV_FLAG_IN_ARRAY;
241 nvl->nvl_flags &= ~NV_FLAG_IN_ARRAY;
242 nv_free(nvl->nvl_array_next);
245 nvl->nvl_array_next = ele;
249 nvlist_in_array(const nvlist_t *nvl)
254 return ((nvl->nvl_flags & NV_FLAG_IN_ARRAY) != 0);
258 nvlist_get_array_next(const nvlist_t *nvl)
264 nvp = nvl->nvl_array_next;
268 return (nvpair_get_nvlist(nvp));
272 nvlist_get_pararr(const nvlist_t *nvl, void **cookiep)
276 ret = nvlist_get_array_next(nvl);
283 return (nvlist_get_parent(nvl, cookiep));
287 nvlist_empty(const nvlist_t *nvl)
291 PJDLOG_ASSERT(nvl->nvl_error == 0);
293 return (nvlist_first_nvpair(nvl) == NULL);
297 nvlist_flags(const nvlist_t *nvl)
301 PJDLOG_ASSERT(nvl->nvl_error == 0);
303 return (nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
307 nvlist_set_flags(nvlist_t *nvl, int flags)
311 PJDLOG_ASSERT(nvl->nvl_error == 0);
313 nvl->nvl_flags = flags;
317 nvlist_report_missing(int type, const char *name)
320 PJDLOG_ABORT("Element '%s' of type %s doesn't exist.",
321 name, nvpair_type_string(type));
325 nvlist_find(const nvlist_t *nvl, int type, const char *name)
330 PJDLOG_ASSERT(nvl->nvl_error == 0);
331 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
332 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
334 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
335 nvp = nvlist_next_nvpair(nvl, nvp)) {
336 if (type != NV_TYPE_NONE && nvpair_type(nvp) != type)
338 if ((nvl->nvl_flags & NV_FLAG_IGNORE_CASE) != 0) {
339 if (strcasecmp(nvpair_name(nvp), name) != 0)
342 if (strcmp(nvpair_name(nvp), name) != 0)
355 nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
359 PJDLOG_ASSERT(nvl->nvl_error == 0);
360 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
361 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
363 return (nvlist_find(nvl, type, name) != NULL);
367 nvlist_free_type(nvlist_t *nvl, const char *name, int type)
372 PJDLOG_ASSERT(nvl->nvl_error == 0);
373 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
374 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
376 nvp = nvlist_find(nvl, type, name);
378 nvlist_free_nvpair(nvl, nvp);
380 nvlist_report_missing(type, name);
384 nvlist_clone(const nvlist_t *nvl)
387 nvpair_t *nvp, *newnvp;
391 if (nvl->nvl_error != 0) {
392 ERRNO_SET(nvl->nvl_error);
396 newnvl = nvlist_create(nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
397 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
398 nvp = nvlist_next_nvpair(nvl, nvp)) {
399 newnvp = nvpair_clone(nvp);
402 (void)nvlist_move_nvpair(newnvl, newnvp);
405 nvlist_destroy(newnvl);
413 nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
416 if (nvlist_error(nvl) != 0) {
417 dprintf(fd, "%*serror: %d\n", level * 4, "",
426 * Dump content of nvlist.
429 nvlist_dump(const nvlist_t *nvl, int fd)
431 const nvlist_t *tmpnvl;
432 nvpair_t *nvp, *tmpnvp;
437 if (nvlist_dump_error_check(nvl, fd, level))
440 nvp = nvlist_first_nvpair(nvl);
441 while (nvp != NULL) {
442 dprintf(fd, "%*s%s (%s):", level * 4, "", nvpair_name(nvp),
443 nvpair_type_string(nvpair_type(nvp)));
444 switch (nvpair_type(nvp)) {
446 dprintf(fd, " null\n");
449 dprintf(fd, " %s\n", nvpair_get_bool(nvp) ?
453 dprintf(fd, " %ju (%jd) (0x%jx)\n",
454 (uintmax_t)nvpair_get_number(nvp),
455 (intmax_t)nvpair_get_number(nvp),
456 (uintmax_t)nvpair_get_number(nvp));
459 dprintf(fd, " [%s]\n", nvpair_get_string(nvp));
463 tmpnvl = nvpair_get_nvlist(nvp);
464 if (nvlist_dump_error_check(tmpnvl, fd, level + 1))
466 tmpnvp = nvlist_first_nvpair(tmpnvl);
467 if (tmpnvp != NULL) {
474 case NV_TYPE_DESCRIPTOR:
475 dprintf(fd, " %d\n", nvpair_get_descriptor(nvp));
479 const unsigned char *binary;
483 binary = nvpair_get_binary(nvp, &size);
484 dprintf(fd, " %zu ", size);
485 for (ii = 0; ii < size; ii++)
486 dprintf(fd, "%02hhx", binary[ii]);
490 case NV_TYPE_BOOL_ARRAY:
496 value = nvpair_get_bool_array(nvp, &nitems);
498 for (ii = 0; ii < nitems; ii++) {
499 dprintf(fd, "%s", value[ii] ? "TRUE" : "FALSE");
500 if (ii != nitems - 1)
506 case NV_TYPE_STRING_ARRAY:
508 const char * const *value;
512 value = nvpair_get_string_array(nvp, &nitems);
514 for (ii = 0; ii < nitems; ii++) {
515 if (value[ii] == NULL)
518 dprintf(fd, "\"%s\"", value[ii]);
519 if (ii != nitems - 1)
525 case NV_TYPE_NUMBER_ARRAY:
527 const uint64_t *value;
531 value = nvpair_get_number_array(nvp, &nitems);
533 for (ii = 0; ii < nitems; ii++) {
534 dprintf(fd, "%ju (%jd) (0x%jx)",
535 value[ii], value[ii], value[ii]);
536 if (ii != nitems - 1)
542 case NV_TYPE_DESCRIPTOR_ARRAY:
548 value = nvpair_get_descriptor_array(nvp, &nitems);
550 for (ii = 0; ii < nitems; ii++) {
551 dprintf(fd, "%d", value[ii]);
552 if (ii != nitems - 1)
558 case NV_TYPE_NVLIST_ARRAY:
560 const nvlist_t * const *value;
564 value = nvpair_get_nvlist_array(nvp, &nitems);
565 dprintf(fd, " %zu\n", nitems);
568 for (ii = 0; ii < nitems; ii++) {
569 if (nvlist_dump_error_check(value[ii], fd,
574 if (tmpnvl == NULL) {
575 tmpnvp = nvlist_first_nvpair(value[ii]);
576 if (tmpnvp != NULL) {
579 dprintf(fd, "%*s,\n",
580 (level + 1) * 4, "");
584 if (tmpnvp != NULL) {
593 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
596 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
599 if (nvlist_in_array(nvl))
600 dprintf(fd, "%*s,\n", level * 4, "");
601 nvl = nvlist_get_pararr(nvl, &cookie);
604 if (nvlist_in_array(nvl) && cookie == NULL) {
605 nvp = nvlist_first_nvpair(nvl);
610 } while (nvp == NULL);
611 if (nvlist_in_array(nvl) && cookie == NULL)
618 nvlist_fdump(const nvlist_t *nvl, FILE *fp)
622 nvlist_dump(nvl, fileno(fp));
627 * The function obtains size of the nvlist after nvlist_pack().
630 nvlist_size(const nvlist_t *nvl)
632 const nvlist_t *tmpnvl;
633 const nvlist_t * const *nvlarray;
634 const nvpair_t *nvp, *tmpnvp;
640 PJDLOG_ASSERT(nvl->nvl_error == 0);
642 size = sizeof(struct nvlist_header);
643 nvp = nvlist_first_nvpair(nvl);
644 while (nvp != NULL) {
645 size += nvpair_header_size();
646 size += strlen(nvpair_name(nvp)) + 1;
647 if (nvpair_type(nvp) == NV_TYPE_NVLIST) {
648 size += sizeof(struct nvlist_header);
649 size += nvpair_header_size() + 1;
650 tmpnvl = nvpair_get_nvlist(nvp);
651 PJDLOG_ASSERT(tmpnvl->nvl_error == 0);
652 tmpnvp = nvlist_first_nvpair(tmpnvl);
653 if (tmpnvp != NULL) {
658 } else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY) {
659 nvlarray = nvpair_get_nvlist_array(nvp, &nitems);
660 PJDLOG_ASSERT(nitems > 0);
662 size += (nvpair_header_size() + 1) * nitems;
663 size += sizeof(struct nvlist_header) * nitems;
667 for (ii = 0; ii < nitems; ii++) {
668 PJDLOG_ASSERT(nvlarray[ii]->nvl_error == 0);
669 tmpnvp = nvlist_first_nvpair(nvlarray[ii]);
670 if (tmpnvp != NULL) {
671 tmpnvl = nvlarray[ii];
675 if (tmpnvp != NULL) {
682 size += nvpair_size(nvp);
685 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
688 nvl = nvlist_get_pararr(nvl, &cookie);
691 if (nvlist_in_array(nvl) && cookie == NULL) {
692 nvp = nvlist_first_nvpair(nvl);
696 } while (nvp == NULL);
697 if (nvlist_in_array(nvl) && cookie == NULL)
708 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
714 PJDLOG_ASSERT(nvl->nvl_error == 0);
718 while (nvlist_next(nvl, &type, (void *)&nvp) != NULL) {
720 case NV_TYPE_DESCRIPTOR:
721 *descs = nvpair_get_descriptor(nvp);
724 case NV_TYPE_DESCRIPTOR_ARRAY:
730 value = nvpair_get_descriptor_array(nvp,
732 for (ii = 0; ii < nitems; ii++) {
739 nvl = nvpair_get_nvlist(nvp);
742 case NV_TYPE_NVLIST_ARRAY:
744 const nvlist_t * const *value;
747 value = nvpair_get_nvlist_array(nvp, &nitems);
748 PJDLOG_ASSERT(value != NULL);
749 PJDLOG_ASSERT(nitems > 0);
757 } while ((nvl = nvlist_get_pararr(nvl, (void *)&nvp)) != NULL);
765 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
770 nitems = nvlist_ndescriptors(nvl);
771 fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
775 nvlist_xdescriptors(nvl, fds);
784 nvlist_ndescriptors(const nvlist_t *nvl)
792 PJDLOG_ASSERT(nvl->nvl_error == 0);
797 while (nvlist_next(nvl, &type, (void *)&nvp) != NULL) {
799 case NV_TYPE_DESCRIPTOR:
803 nvl = nvpair_get_nvlist(nvp);
806 case NV_TYPE_NVLIST_ARRAY:
808 const nvlist_t * const *value;
811 value = nvpair_get_nvlist_array(nvp, &nitems);
812 PJDLOG_ASSERT(value != NULL);
813 PJDLOG_ASSERT(nitems > 0);
819 case NV_TYPE_DESCRIPTOR_ARRAY:
823 (void)nvpair_get_descriptor_array(nvp,
830 } while ((nvl = nvlist_get_pararr(nvl, (void *)&nvp)) != NULL);
838 static unsigned char *
839 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
841 struct nvlist_header nvlhdr;
845 nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
846 nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
847 nvlhdr.nvlh_flags = nvl->nvl_flags;
848 #if BYTE_ORDER == BIG_ENDIAN
849 nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
851 nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
852 nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
853 PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
854 memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
855 ptr += sizeof(nvlhdr);
856 *leftp -= sizeof(nvlhdr);
862 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
864 unsigned char *buf, *ptr;
866 const nvlist_t *tmpnvl;
867 nvpair_t *nvp, *tmpnvp;
872 if (nvl->nvl_error != 0) {
873 ERRNO_SET(nvl->nvl_error);
877 size = nvlist_size(nvl);
878 buf = nv_malloc(size);
885 ptr = nvlist_pack_header(nvl, ptr, &left);
887 nvp = nvlist_first_nvpair(nvl);
888 while (nvp != NULL) {
891 nvpair_init_datasize(nvp);
892 ptr = nvpair_pack_header(nvp, ptr, &left);
895 switch (nvpair_type(nvp)) {
897 ptr = nvpair_pack_null(nvp, ptr, &left);
900 ptr = nvpair_pack_bool(nvp, ptr, &left);
903 ptr = nvpair_pack_number(nvp, ptr, &left);
906 ptr = nvpair_pack_string(nvp, ptr, &left);
909 tmpnvl = nvpair_get_nvlist(nvp);
910 ptr = nvlist_pack_header(tmpnvl, ptr, &left);
913 tmpnvp = nvlist_first_nvpair(tmpnvl);
914 if (tmpnvp != NULL) {
919 ptr = nvpair_pack_nvlist_up(ptr, &left);
922 case NV_TYPE_DESCRIPTOR:
923 ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
925 case NV_TYPE_DESCRIPTOR_ARRAY:
926 ptr = nvpair_pack_descriptor_array(nvp, ptr, fdidxp,
931 ptr = nvpair_pack_binary(nvp, ptr, &left);
933 case NV_TYPE_BOOL_ARRAY:
934 ptr = nvpair_pack_bool_array(nvp, ptr, &left);
936 case NV_TYPE_NUMBER_ARRAY:
937 ptr = nvpair_pack_number_array(nvp, ptr, &left);
939 case NV_TYPE_STRING_ARRAY:
940 ptr = nvpair_pack_string_array(nvp, ptr, &left);
942 case NV_TYPE_NVLIST_ARRAY:
944 const nvlist_t * const * value;
949 value = nvpair_get_nvlist_array(nvp, &nitems);
950 for (ii = 0; ii < nitems; ii++) {
951 ptr = nvlist_pack_header(value[ii], ptr, &left);
954 tmpnvp = nvlist_first_nvpair(value[ii]);
955 if (tmpnvp != NULL) {
959 ptr = nvpair_pack_nvlist_array_next(ptr, &left);
963 if (tmpnvl != NULL) {
971 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
975 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
978 if (nvlist_in_array(nvl)) {
979 ptr = nvpair_pack_nvlist_array_next(ptr,
984 nvl = nvlist_get_pararr(nvl, &cookie);
987 if (nvlist_in_array(nvl) && cookie == NULL) {
988 nvp = nvlist_first_nvpair(nvl);
989 ptr = nvlist_pack_header(nvl, ptr,
993 } else if (nvpair_type((nvpair_t *)cookie) !=
994 NV_TYPE_NVLIST_ARRAY) {
995 ptr = nvpair_pack_nvlist_up(ptr, &left);
1002 } while (nvp == NULL);
1003 if (nvlist_in_array(nvl) && cookie == NULL)
1018 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
1023 if (nvl->nvl_error != 0) {
1024 ERRNO_SET(nvl->nvl_error);
1028 if (nvlist_ndescriptors(nvl) > 0) {
1029 ERRNO_SET(EOPNOTSUPP);
1033 return (nvlist_xpack(nvl, NULL, sizep));
1037 nvlist_check_header(struct nvlist_header *nvlhdrp)
1040 if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
1044 if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
1048 #if BYTE_ORDER == BIG_ENDIAN
1049 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
1050 nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
1051 nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
1054 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
1055 nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
1056 nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
1062 const unsigned char *
1063 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
1064 bool *isbep, size_t *leftp)
1066 struct nvlist_header nvlhdr;
1069 if (*leftp < sizeof(nvlhdr))
1072 memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
1074 if (!nvlist_check_header(&nvlhdr))
1077 if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
1081 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
1083 if (nvlhdr.nvlh_descriptors > nfds)
1086 if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
1089 inarrayf = (nvl->nvl_flags & NV_FLAG_IN_ARRAY);
1090 nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK) | inarrayf;
1092 ptr += sizeof(nvlhdr);
1094 *isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
1095 *leftp -= sizeof(nvlhdr);
1104 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds,
1107 const unsigned char *ptr;
1108 nvlist_t *nvl, *retnvl, *tmpnvl, *array;
1113 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
1118 tmpnvl = array = NULL;
1119 nvl = retnvl = nvlist_create(0);
1123 ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
1126 if (nvl->nvl_flags != flags) {
1132 ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
1135 switch (nvpair_type(nvp)) {
1137 ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
1140 ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
1142 case NV_TYPE_NUMBER:
1143 ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
1145 case NV_TYPE_STRING:
1146 ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
1148 case NV_TYPE_NVLIST:
1149 ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
1151 if (tmpnvl == NULL || ptr == NULL)
1153 nvlist_set_parent(tmpnvl, nvp);
1156 case NV_TYPE_DESCRIPTOR:
1157 ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
1160 case NV_TYPE_DESCRIPTOR_ARRAY:
1161 ptr = nvpair_unpack_descriptor_array(isbe, nvp, ptr,
1165 case NV_TYPE_BINARY:
1166 ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
1168 case NV_TYPE_NVLIST_UP:
1169 if (nvl->nvl_parent == NULL)
1171 nvl = nvpair_nvlist(nvl->nvl_parent);
1172 nvpair_free_structure(nvp);
1174 case NV_TYPE_NVLIST_ARRAY_NEXT:
1175 if (nvl->nvl_array_next == NULL) {
1176 if (nvl->nvl_parent == NULL)
1178 nvl = nvpair_nvlist(nvl->nvl_parent);
1180 nvl = __DECONST(nvlist_t *,
1181 nvlist_get_array_next(nvl));
1182 ptr = nvlist_unpack_header(nvl, ptr, nfds,
1187 nvpair_free_structure(nvp);
1189 case NV_TYPE_BOOL_ARRAY:
1190 ptr = nvpair_unpack_bool_array(isbe, nvp, ptr, &left);
1192 case NV_TYPE_NUMBER_ARRAY:
1193 ptr = nvpair_unpack_number_array(isbe, nvp, ptr, &left);
1195 case NV_TYPE_STRING_ARRAY:
1196 ptr = nvpair_unpack_string_array(isbe, nvp, ptr, &left);
1198 case NV_TYPE_NVLIST_ARRAY:
1199 ptr = nvpair_unpack_nvlist_array(isbe, nvp, ptr, &left,
1203 PJDLOG_ASSERT(array != NULL);
1206 nvlist_set_parent(array, nvp);
1207 array = __DECONST(nvlist_t *,
1208 nvlist_get_array_next(array));
1209 } while (array != NULL);
1210 ptr = nvlist_unpack_header(tmpnvl, ptr, nfds, &isbe,
1214 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
1218 if (!nvlist_move_nvpair(nvl, nvp))
1220 if (tmpnvl != NULL) {
1228 nvlist_destroy(retnvl);
1233 nvlist_unpack(const void *buf, size_t size, int flags)
1236 return (nvlist_xunpack(buf, size, NULL, 0, flags));
1241 nvlist_send(int sock, const nvlist_t *nvl)
1243 size_t datasize, nfds;
1249 if (nvlist_error(nvl) != 0) {
1250 ERRNO_SET(nvlist_error(nvl));
1254 fds = nvlist_descriptors(nvl, &nfds);
1261 data = nvlist_xpack(nvl, &fdidx, &datasize);
1265 if (buf_send(sock, data, datasize) == -1)
1269 if (fd_send(sock, fds, nfds) == -1)
1283 nvlist_recv(int sock, int flags)
1285 struct nvlist_header nvlhdr;
1286 nvlist_t *nvl, *ret;
1288 size_t nfds, size, i;
1291 if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr)) == -1)
1294 if (!nvlist_check_header(&nvlhdr))
1297 nfds = (size_t)nvlhdr.nvlh_descriptors;
1298 size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
1300 buf = nv_malloc(size);
1304 memcpy(buf, &nvlhdr, sizeof(nvlhdr));
1309 if (buf_recv(sock, buf + sizeof(nvlhdr), size - sizeof(nvlhdr)) == -1)
1313 fds = nv_malloc(nfds * sizeof(fds[0]));
1316 if (fd_recv(sock, fds, nfds) == -1)
1320 nvl = nvlist_xunpack(buf, size, fds, nfds, flags);
1323 for (i = 0; i < nfds; i++)
1340 nvlist_xfer(int sock, nvlist_t *nvl, int flags)
1343 if (nvlist_send(sock, nvl) < 0) {
1344 nvlist_destroy(nvl);
1347 nvlist_destroy(nvl);
1348 return (nvlist_recv(sock, flags));
1353 nvlist_first_nvpair(const nvlist_t *nvl)
1358 return (TAILQ_FIRST(&nvl->nvl_head));
1362 nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1368 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1370 retnvp = nvpair_next(nvp);
1371 PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
1378 nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1384 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1386 retnvp = nvpair_prev(nvp);
1387 PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1393 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1399 if (cookiep == NULL || *cookiep == NULL)
1400 nvp = nvlist_first_nvpair(nvl);
1402 nvp = nvlist_next_nvpair(nvl, *cookiep);
1406 *typep = nvpair_type(nvp);
1407 if (cookiep != NULL)
1409 return (nvpair_name(nvp));
1413 nvlist_exists(const nvlist_t *nvl, const char *name)
1416 return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1419 #define NVLIST_EXISTS(type, TYPE) \
1421 nvlist_exists_##type(const nvlist_t *nvl, const char *name) \
1424 return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL); \
1427 NVLIST_EXISTS(null, NULL)
1428 NVLIST_EXISTS(bool, BOOL)
1429 NVLIST_EXISTS(number, NUMBER)
1430 NVLIST_EXISTS(string, STRING)
1431 NVLIST_EXISTS(nvlist, NVLIST)
1432 NVLIST_EXISTS(binary, BINARY)
1433 NVLIST_EXISTS(bool_array, BOOL_ARRAY)
1434 NVLIST_EXISTS(number_array, NUMBER_ARRAY)
1435 NVLIST_EXISTS(string_array, STRING_ARRAY)
1436 NVLIST_EXISTS(nvlist_array, NVLIST_ARRAY)
1438 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1439 NVLIST_EXISTS(descriptor_array, DESCRIPTOR_ARRAY)
1442 #undef NVLIST_EXISTS
1445 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1451 if (nvlist_error(nvl) != 0) {
1452 ERRNO_SET(nvlist_error(nvl));
1455 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1456 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1457 nvl->nvl_error = EEXIST;
1458 ERRNO_SET(nvlist_error(nvl));
1463 newnvp = nvpair_clone(nvp);
1464 if (newnvp == NULL) {
1465 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1466 ERRNO_SET(nvlist_error(nvl));
1470 nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1474 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1478 va_start(valueap, valuefmt);
1479 nvlist_add_stringv(nvl, name, valuefmt, valueap);
1484 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1489 if (nvlist_error(nvl) != 0) {
1490 ERRNO_SET(nvlist_error(nvl));
1494 nvp = nvpair_create_stringv(name, valuefmt, valueap);
1496 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1497 ERRNO_SET(nvl->nvl_error);
1499 (void)nvlist_move_nvpair(nvl, nvp);
1504 nvlist_add_null(nvlist_t *nvl, const char *name)
1508 if (nvlist_error(nvl) != 0) {
1509 ERRNO_SET(nvlist_error(nvl));
1513 nvp = nvpair_create_null(name);
1515 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1516 ERRNO_SET(nvl->nvl_error);
1518 (void)nvlist_move_nvpair(nvl, nvp);
1523 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1528 if (nvlist_error(nvl) != 0) {
1529 ERRNO_SET(nvlist_error(nvl));
1533 nvp = nvpair_create_binary(name, value, size);
1535 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1536 ERRNO_SET(nvl->nvl_error);
1538 (void)nvlist_move_nvpair(nvl, nvp);
1543 #define NVLIST_ADD(vtype, type) \
1545 nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value) \
1549 if (nvlist_error(nvl) != 0) { \
1550 ERRNO_SET(nvlist_error(nvl)); \
1554 nvp = nvpair_create_##type(name, value); \
1555 if (nvp == NULL) { \
1556 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1557 ERRNO_SET(nvl->nvl_error); \
1559 (void)nvlist_move_nvpair(nvl, nvp); \
1563 NVLIST_ADD(bool, bool)
1564 NVLIST_ADD(uint64_t, number)
1565 NVLIST_ADD(const char *, string)
1566 NVLIST_ADD(const nvlist_t *, nvlist)
1568 NVLIST_ADD(int, descriptor);
1573 #define NVLIST_ADD_ARRAY(vtype, type) \
1575 nvlist_add_##type##_array(nvlist_t *nvl, const char *name, vtype value, \
1580 if (nvlist_error(nvl) != 0) { \
1581 ERRNO_SET(nvlist_error(nvl)); \
1585 nvp = nvpair_create_##type##_array(name, value, nitems); \
1586 if (nvp == NULL) { \
1587 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1588 ERRNO_SET(nvl->nvl_error); \
1590 (void)nvlist_move_nvpair(nvl, nvp); \
1594 NVLIST_ADD_ARRAY(const bool *, bool)
1595 NVLIST_ADD_ARRAY(const uint64_t *, number)
1596 NVLIST_ADD_ARRAY(const char * const *, string)
1597 NVLIST_ADD_ARRAY(const nvlist_t * const *, nvlist)
1599 NVLIST_ADD_ARRAY(const int *, descriptor)
1602 #undef NVLIST_ADD_ARRAY
1605 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1609 PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1611 if (nvlist_error(nvl) != 0) {
1613 ERRNO_SET(nvlist_error(nvl));
1616 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1617 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1619 nvl->nvl_error = EEXIST;
1620 ERRNO_SET(nvl->nvl_error);
1625 nvpair_insert(&nvl->nvl_head, nvp, nvl);
1630 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1634 if (nvlist_error(nvl) != 0) {
1636 ERRNO_SET(nvlist_error(nvl));
1640 nvp = nvpair_move_string(name, value);
1642 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1643 ERRNO_SET(nvl->nvl_error);
1645 (void)nvlist_move_nvpair(nvl, nvp);
1650 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1654 if (nvlist_error(nvl) != 0) {
1655 if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1656 nvlist_destroy(value);
1657 ERRNO_SET(nvlist_error(nvl));
1661 nvp = nvpair_move_nvlist(name, value);
1663 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1664 ERRNO_SET(nvl->nvl_error);
1666 (void)nvlist_move_nvpair(nvl, nvp);
1672 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1676 if (nvlist_error(nvl) != 0) {
1678 ERRNO_SET(nvlist_error(nvl));
1682 nvp = nvpair_move_descriptor(name, value);
1684 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1685 ERRNO_SET(nvl->nvl_error);
1687 (void)nvlist_move_nvpair(nvl, nvp);
1693 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1697 if (nvlist_error(nvl) != 0) {
1699 ERRNO_SET(nvlist_error(nvl));
1703 nvp = nvpair_move_binary(name, value, size);
1705 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1706 ERRNO_SET(nvl->nvl_error);
1708 (void)nvlist_move_nvpair(nvl, nvp);
1713 nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value,
1718 if (nvlist_error(nvl) != 0) {
1720 ERRNO_SET(nvlist_error(nvl));
1724 nvp = nvpair_move_bool_array(name, value, nitems);
1726 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1727 ERRNO_SET(nvl->nvl_error);
1729 (void)nvlist_move_nvpair(nvl, nvp);
1734 nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value,
1740 if (nvlist_error(nvl) != 0) {
1741 if (value != NULL) {
1742 for (i = 0; i < nitems; i++)
1746 ERRNO_SET(nvlist_error(nvl));
1750 nvp = nvpair_move_string_array(name, value, nitems);
1752 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1753 ERRNO_SET(nvl->nvl_error);
1755 (void)nvlist_move_nvpair(nvl, nvp);
1760 nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value,
1766 if (nvlist_error(nvl) != 0) {
1767 if (value != NULL) {
1768 for (i = 0; i < nitems; i++) {
1769 if (nvlist_get_pararr(value[i], NULL) == NULL)
1770 nvlist_destroy(value[i]);
1774 ERRNO_SET(nvlist_error(nvl));
1778 nvp = nvpair_move_nvlist_array(name, value, nitems);
1780 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1781 ERRNO_SET(nvl->nvl_error);
1783 (void)nvlist_move_nvpair(nvl, nvp);
1788 nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value,
1793 if (nvlist_error(nvl) != 0) {
1795 ERRNO_SET(nvlist_error(nvl));
1799 nvp = nvpair_move_number_array(name, value, nitems);
1801 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1802 ERRNO_SET(nvl->nvl_error);
1804 (void)nvlist_move_nvpair(nvl, nvp);
1810 nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value,
1816 if (nvlist_error(nvl) != 0) {
1818 for (i = 0; i < nitems; i++)
1823 ERRNO_SET(nvlist_error(nvl));
1827 nvp = nvpair_move_descriptor_array(name, value, nitems);
1829 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1830 ERRNO_SET(nvl->nvl_error);
1832 (void)nvlist_move_nvpair(nvl, nvp);
1838 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1841 return (nvlist_find(nvl, NV_TYPE_NONE, name));
1844 #define NVLIST_GET(ftype, type, TYPE) \
1846 nvlist_get_##type(const nvlist_t *nvl, const char *name) \
1848 const nvpair_t *nvp; \
1850 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1852 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1853 return (nvpair_get_##type(nvp)); \
1856 NVLIST_GET(bool, bool, BOOL)
1857 NVLIST_GET(uint64_t, number, NUMBER)
1858 NVLIST_GET(const char *, string, STRING)
1859 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1861 NVLIST_GET(int, descriptor, DESCRIPTOR)
1867 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1871 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1873 nvlist_report_missing(NV_TYPE_BINARY, name);
1875 return (nvpair_get_binary(nvp, sizep));
1878 #define NVLIST_GET_ARRAY(ftype, type, TYPE) \
1880 nvlist_get_##type##_array(const nvlist_t *nvl, const char *name, \
1883 const nvpair_t *nvp; \
1885 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1887 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1888 return (nvpair_get_##type##_array(nvp, nitems)); \
1891 NVLIST_GET_ARRAY(const bool *, bool, BOOL)
1892 NVLIST_GET_ARRAY(const uint64_t *, number, NUMBER)
1893 NVLIST_GET_ARRAY(const char * const *, string, STRING)
1894 NVLIST_GET_ARRAY(const nvlist_t * const *, nvlist, NVLIST)
1896 NVLIST_GET_ARRAY(const int *, descriptor, DESCRIPTOR)
1899 #undef NVLIST_GET_ARRAY
1901 #define NVLIST_TAKE(ftype, type, TYPE) \
1903 nvlist_take_##type(nvlist_t *nvl, const char *name) \
1908 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1910 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1911 value = (ftype)(intptr_t)nvpair_get_##type(nvp); \
1912 nvlist_remove_nvpair(nvl, nvp); \
1913 nvpair_free_structure(nvp); \
1917 NVLIST_TAKE(bool, bool, BOOL)
1918 NVLIST_TAKE(uint64_t, number, NUMBER)
1919 NVLIST_TAKE(char *, string, STRING)
1920 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1922 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1928 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1933 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1935 nvlist_report_missing(NV_TYPE_BINARY, name);
1937 value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1938 nvlist_remove_nvpair(nvl, nvp);
1939 nvpair_free_structure(nvp);
1943 #define NVLIST_TAKE_ARRAY(ftype, type, TYPE) \
1945 nvlist_take_##type##_array(nvlist_t *nvl, const char *name, \
1951 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1953 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1954 value = (ftype)(intptr_t)nvpair_get_##type##_array(nvp, nitems);\
1955 nvlist_remove_nvpair(nvl, nvp); \
1956 nvpair_free_structure(nvp); \
1960 NVLIST_TAKE_ARRAY(bool *, bool, BOOL)
1961 NVLIST_TAKE_ARRAY(uint64_t *, number, NUMBER)
1962 NVLIST_TAKE_ARRAY(char **, string, STRING)
1963 NVLIST_TAKE_ARRAY(nvlist_t **, nvlist, NVLIST)
1965 NVLIST_TAKE_ARRAY(int *, descriptor, DESCRIPTOR)
1969 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1974 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1976 nvpair_remove(&nvl->nvl_head, nvp, nvl);
1980 nvlist_free(nvlist_t *nvl, const char *name)
1983 nvlist_free_type(nvl, name, NV_TYPE_NONE);
1986 #define NVLIST_FREE(type, TYPE) \
1988 nvlist_free_##type(nvlist_t *nvl, const char *name) \
1991 nvlist_free_type(nvl, name, NV_TYPE_##TYPE); \
1994 NVLIST_FREE(null, NULL)
1995 NVLIST_FREE(bool, BOOL)
1996 NVLIST_FREE(number, NUMBER)
1997 NVLIST_FREE(string, STRING)
1998 NVLIST_FREE(nvlist, NVLIST)
1999 NVLIST_FREE(binary, BINARY)
2000 NVLIST_FREE(bool_array, BOOL_ARRAY)
2001 NVLIST_FREE(number_array, NUMBER_ARRAY)
2002 NVLIST_FREE(string_array, STRING_ARRAY)
2003 NVLIST_FREE(nvlist_array, NVLIST_ARRAY)
2005 NVLIST_FREE(descriptor, DESCRIPTOR)
2006 NVLIST_FREE(descriptor_array, DESCRIPTOR_ARRAY)
2012 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2017 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2019 nvlist_remove_nvpair(nvl, nvp);