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>
71 #include "nvlist_impl.h"
72 #include "nvpair_impl.h"
76 #define PJDLOG_ASSERT(...) MPASS(__VA_ARGS__)
77 #define PJDLOG_RASSERT(expr, ...) KASSERT(expr, (__VA_ARGS__))
78 #define PJDLOG_ABORT(...) panic(__VA_ARGS__)
81 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
82 #define PJDLOG_RASSERT(expr, ...) assert(expr)
83 #define PJDLOG_ABORT(...) do { \
84 fprintf(stderr, "%s:%u: ", __FILE__, __LINE__); \
85 fprintf(stderr, __VA_ARGS__); \
86 fprintf(stderr, "\n"); \
92 #define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN | NV_FLAG_IN_ARRAY)
93 #define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE | NV_FLAG_NO_UNIQUE)
94 #define NV_FLAG_ALL_MASK (NV_FLAG_PRIVATE_MASK | NV_FLAG_PUBLIC_MASK)
96 #define NVLIST_MAGIC 0x6e766c /* "nvl" */
101 nvpair_t *nvl_parent;
102 nvpair_t *nvl_array_next;
103 struct nvl_head nvl_head;
106 #define NVLIST_ASSERT(nvl) do { \
107 PJDLOG_ASSERT((nvl) != NULL); \
108 PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \
112 MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
115 #define NVPAIR_ASSERT(nvp) nvpair_assert(nvp)
117 #define NVLIST_HEADER_MAGIC 0x6c
118 #define NVLIST_HEADER_VERSION 0x00
119 struct nvlist_header {
121 uint8_t nvlh_version;
123 uint64_t nvlh_descriptors;
128 nvlist_create(int flags)
132 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
134 nvl = nv_malloc(sizeof(*nvl));
138 nvl->nvl_flags = flags;
139 nvl->nvl_parent = NULL;
140 nvl->nvl_array_next = NULL;
141 TAILQ_INIT(&nvl->nvl_head);
142 nvl->nvl_magic = NVLIST_MAGIC;
148 nvlist_destroy(nvlist_t *nvl)
159 while ((nvp = nvlist_first_nvpair(nvl)) != NULL) {
160 nvlist_remove_nvpair(nvl, nvp);
163 if (nvl->nvl_array_next != NULL)
164 nvpair_free_structure(nvl->nvl_array_next);
165 nvl->nvl_array_next = NULL;
166 nvl->nvl_parent = NULL;
174 nvlist_set_error(nvlist_t *nvl, int error)
177 PJDLOG_ASSERT(error != 0);
180 * Check for error != 0 so that we don't do the wrong thing if somebody
181 * tries to abuse this API when asserts are disabled.
183 if (nvl != NULL && error != 0 && nvl->nvl_error == 0)
184 nvl->nvl_error = error;
188 nvlist_error(const nvlist_t *nvl)
196 return (nvl->nvl_error);
200 nvlist_get_nvpair_parent(const nvlist_t *nvl)
205 return (nvl->nvl_parent);
209 nvlist_get_parent(const nvlist_t *nvl, void **cookiep)
215 nvp = nvl->nvl_parent;
221 return (nvpair_nvlist(nvp));
225 nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent)
230 nvl->nvl_parent = parent;
234 nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele)
240 nvl->nvl_flags |= NV_FLAG_IN_ARRAY;
242 nvl->nvl_flags &= ~NV_FLAG_IN_ARRAY;
244 nvl->nvl_array_next = ele;
248 nvlist_in_array(const nvlist_t *nvl)
253 return ((nvl->nvl_flags & NV_FLAG_IN_ARRAY) != 0);
257 nvlist_get_array_next(const nvlist_t *nvl)
263 nvp = nvl->nvl_array_next;
267 return (nvpair_get_nvlist(nvp));
271 nvlist_get_pararr(const nvlist_t *nvl, void **cookiep)
275 ret = nvlist_get_array_next(nvl);
282 ret = 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)
715 PJDLOG_ASSERT(nvl->nvl_error == 0);
719 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
721 case NV_TYPE_DESCRIPTOR:
722 *descs = nvpair_get_descriptor(nvp);
725 case NV_TYPE_DESCRIPTOR_ARRAY:
731 value = nvpair_get_descriptor_array(nvp,
733 for (ii = 0; ii < nitems; ii++) {
740 nvl = nvpair_get_nvlist(nvp);
743 case NV_TYPE_NVLIST_ARRAY:
745 const nvlist_t * const *value;
748 value = nvpair_get_nvlist_array(nvp, &nitems);
749 PJDLOG_ASSERT(value != NULL);
750 PJDLOG_ASSERT(nitems > 0);
758 } while ((nvl = nvlist_get_pararr(nvl, (void**)&nvp)) != NULL);
766 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
771 nitems = nvlist_ndescriptors(nvl);
772 fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
776 nvlist_xdescriptors(nvl, fds);
785 nvlist_ndescriptors(const nvlist_t *nvl)
794 PJDLOG_ASSERT(nvl->nvl_error == 0);
799 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
801 case NV_TYPE_DESCRIPTOR:
805 nvl = nvpair_get_nvlist(nvp);
808 case NV_TYPE_NVLIST_ARRAY:
810 const nvlist_t * const *value;
813 value = nvpair_get_nvlist_array(nvp, &nitems);
814 PJDLOG_ASSERT(value != NULL);
815 PJDLOG_ASSERT(nitems > 0);
821 case NV_TYPE_DESCRIPTOR_ARRAY:
825 (void)nvpair_get_descriptor_array(nvp,
832 } while ((nvl = nvlist_get_pararr(nvl, (void**)&nvp)) != NULL);
840 static unsigned char *
841 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
843 struct nvlist_header nvlhdr;
847 nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
848 nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
849 nvlhdr.nvlh_flags = nvl->nvl_flags;
850 #if BYTE_ORDER == BIG_ENDIAN
851 nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
853 nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
854 nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
855 PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
856 memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
857 ptr += sizeof(nvlhdr);
858 *leftp -= sizeof(nvlhdr);
864 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
866 unsigned char *buf, *ptr;
868 const nvlist_t *tmpnvl;
869 nvpair_t *nvp, *tmpnvp;
874 if (nvl->nvl_error != 0) {
875 ERRNO_SET(nvl->nvl_error);
879 size = nvlist_size(nvl);
880 buf = nv_malloc(size);
887 ptr = nvlist_pack_header(nvl, ptr, &left);
889 nvp = nvlist_first_nvpair(nvl);
890 while (nvp != NULL) {
893 nvpair_init_datasize(nvp);
894 ptr = nvpair_pack_header(nvp, ptr, &left);
897 switch (nvpair_type(nvp)) {
899 ptr = nvpair_pack_null(nvp, ptr, &left);
902 ptr = nvpair_pack_bool(nvp, ptr, &left);
905 ptr = nvpair_pack_number(nvp, ptr, &left);
908 ptr = nvpair_pack_string(nvp, ptr, &left);
911 tmpnvl = nvpair_get_nvlist(nvp);
912 ptr = nvlist_pack_header(tmpnvl, ptr, &left);
915 tmpnvp = nvlist_first_nvpair(tmpnvl);
916 if (tmpnvp != NULL) {
921 ptr = nvpair_pack_nvlist_up(ptr, &left);
924 case NV_TYPE_DESCRIPTOR:
925 ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
927 case NV_TYPE_DESCRIPTOR_ARRAY:
928 ptr = nvpair_pack_descriptor_array(nvp, ptr, fdidxp,
933 ptr = nvpair_pack_binary(nvp, ptr, &left);
935 case NV_TYPE_BOOL_ARRAY:
936 ptr = nvpair_pack_bool_array(nvp, ptr, &left);
938 case NV_TYPE_NUMBER_ARRAY:
939 ptr = nvpair_pack_number_array(nvp, ptr, &left);
941 case NV_TYPE_STRING_ARRAY:
942 ptr = nvpair_pack_string_array(nvp, ptr, &left);
944 case NV_TYPE_NVLIST_ARRAY:
946 const nvlist_t * const * value;
951 value = nvpair_get_nvlist_array(nvp, &nitems);
952 for (ii = 0; ii < nitems; ii++) {
953 ptr = nvlist_pack_header(value[ii], ptr, &left);
956 tmpnvp = nvlist_first_nvpair(value[ii]);
957 if (tmpnvp != NULL) {
961 ptr = nvpair_pack_nvlist_array_next(ptr, &left);
965 if (tmpnvl != NULL) {
973 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
977 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
980 if (nvlist_in_array(nvl)) {
981 ptr = nvpair_pack_nvlist_array_next(ptr,
986 nvl = nvlist_get_pararr(nvl, &cookie);
989 if (nvlist_in_array(nvl) && cookie == NULL) {
990 nvp = nvlist_first_nvpair(nvl);
991 ptr = nvlist_pack_header(nvl, ptr,
995 } else if (nvpair_type((nvpair_t *)cookie) !=
996 NV_TYPE_NVLIST_ARRAY) {
997 ptr = nvpair_pack_nvlist_up(ptr, &left);
1004 } while (nvp == NULL);
1005 if (nvlist_in_array(nvl) && cookie == NULL)
1020 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
1025 if (nvl->nvl_error != 0) {
1026 ERRNO_SET(nvl->nvl_error);
1030 if (nvlist_ndescriptors(nvl) > 0) {
1031 ERRNO_SET(EOPNOTSUPP);
1035 return (nvlist_xpack(nvl, NULL, sizep));
1039 nvlist_check_header(struct nvlist_header *nvlhdrp)
1042 if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
1046 if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
1050 #if BYTE_ORDER == BIG_ENDIAN
1051 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
1052 nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
1053 nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
1056 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
1057 nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
1058 nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
1064 const unsigned char *
1065 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
1066 bool *isbep, size_t *leftp)
1068 struct nvlist_header nvlhdr;
1071 if (*leftp < sizeof(nvlhdr))
1074 memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
1076 if (!nvlist_check_header(&nvlhdr))
1079 if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
1083 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
1085 if (nvlhdr.nvlh_descriptors > nfds)
1088 if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
1091 inarrayf = (nvl->nvl_flags & NV_FLAG_IN_ARRAY);
1092 nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK) | inarrayf;
1094 ptr += sizeof(nvlhdr);
1096 *isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
1097 *leftp -= sizeof(nvlhdr);
1106 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds,
1109 const unsigned char *ptr;
1110 nvlist_t *nvl, *retnvl, *tmpnvl, *array;
1115 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
1120 tmpnvl = array = NULL;
1121 nvl = retnvl = nvlist_create(0);
1125 ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
1128 if (nvl->nvl_flags != flags) {
1134 ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
1137 switch (nvpair_type(nvp)) {
1139 ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
1142 ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
1144 case NV_TYPE_NUMBER:
1145 ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
1147 case NV_TYPE_STRING:
1148 ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
1150 case NV_TYPE_NVLIST:
1151 ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
1153 if (tmpnvl == NULL || ptr == NULL)
1155 nvlist_set_parent(tmpnvl, nvp);
1158 case NV_TYPE_DESCRIPTOR:
1159 ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
1162 case NV_TYPE_DESCRIPTOR_ARRAY:
1163 ptr = nvpair_unpack_descriptor_array(isbe, nvp, ptr,
1167 case NV_TYPE_BINARY:
1168 ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
1170 case NV_TYPE_NVLIST_UP:
1171 if (nvl->nvl_parent == NULL)
1173 nvl = nvpair_nvlist(nvl->nvl_parent);
1174 nvpair_free_structure(nvp);
1176 case NV_TYPE_NVLIST_ARRAY_NEXT:
1177 if (nvl->nvl_array_next == NULL) {
1178 if (nvl->nvl_parent == NULL)
1180 nvl = nvpair_nvlist(nvl->nvl_parent);
1182 nvl = __DECONST(nvlist_t *,
1183 nvlist_get_array_next(nvl));
1184 ptr = nvlist_unpack_header(nvl, ptr, nfds,
1189 nvpair_free_structure(nvp);
1191 case NV_TYPE_BOOL_ARRAY:
1192 ptr = nvpair_unpack_bool_array(isbe, nvp, ptr, &left);
1194 case NV_TYPE_NUMBER_ARRAY:
1195 ptr = nvpair_unpack_number_array(isbe, nvp, ptr, &left);
1197 case NV_TYPE_STRING_ARRAY:
1198 ptr = nvpair_unpack_string_array(isbe, nvp, ptr, &left);
1200 case NV_TYPE_NVLIST_ARRAY:
1201 ptr = nvpair_unpack_nvlist_array(isbe, nvp, ptr, &left,
1206 while (array != NULL) {
1207 nvlist_set_parent(array, nvp);
1208 array = __DECONST(nvlist_t *,
1209 nvlist_get_array_next(array));
1211 ptr = nvlist_unpack_header(tmpnvl, ptr, nfds, &isbe,
1215 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
1219 if (!nvlist_move_nvpair(nvl, nvp))
1221 if (tmpnvl != NULL) {
1229 nvlist_destroy(retnvl);
1234 nvlist_unpack(const void *buf, size_t size, int flags)
1237 return (nvlist_xunpack(buf, size, NULL, 0, flags));
1242 nvlist_send(int sock, const nvlist_t *nvl)
1244 size_t datasize, nfds;
1250 if (nvlist_error(nvl) != 0) {
1251 ERRNO_SET(nvlist_error(nvl));
1255 fds = nvlist_descriptors(nvl, &nfds);
1263 data = nvlist_xpack(nvl, &fdidx, &datasize);
1267 if (buf_send(sock, data, datasize) == -1)
1271 if (fd_send(sock, fds, nfds) == -1)
1285 nvlist_recv(int sock, int flags)
1287 struct nvlist_header nvlhdr;
1288 nvlist_t *nvl, *ret;
1290 size_t nfds, size, i;
1293 if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr)) == -1)
1296 if (!nvlist_check_header(&nvlhdr))
1299 nfds = (size_t)nvlhdr.nvlh_descriptors;
1300 size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
1302 buf = nv_malloc(size);
1306 memcpy(buf, &nvlhdr, sizeof(nvlhdr));
1311 if (buf_recv(sock, buf + sizeof(nvlhdr), size - sizeof(nvlhdr)) == -1)
1315 fds = nv_malloc(nfds * sizeof(fds[0]));
1318 if (fd_recv(sock, fds, nfds) == -1)
1322 nvl = nvlist_xunpack(buf, size, fds, nfds, flags);
1325 for (i = 0; i < nfds; i++)
1342 nvlist_xfer(int sock, nvlist_t *nvl, int flags)
1345 if (nvlist_send(sock, nvl) < 0) {
1346 nvlist_destroy(nvl);
1349 nvlist_destroy(nvl);
1350 return (nvlist_recv(sock, flags));
1355 nvlist_first_nvpair(const nvlist_t *nvl)
1360 return (TAILQ_FIRST(&nvl->nvl_head));
1364 nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1370 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1372 retnvp = nvpair_next(nvp);
1373 PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
1380 nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1386 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1388 retnvp = nvpair_prev(nvp);
1389 PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1395 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1401 if (cookiep == NULL || *cookiep == NULL)
1402 nvp = nvlist_first_nvpair(nvl);
1404 nvp = nvlist_next_nvpair(nvl, *cookiep);
1408 *typep = nvpair_type(nvp);
1409 if (cookiep != NULL)
1411 return (nvpair_name(nvp));
1415 nvlist_exists(const nvlist_t *nvl, const char *name)
1418 return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1421 #define NVLIST_EXISTS(type, TYPE) \
1423 nvlist_exists_##type(const nvlist_t *nvl, const char *name) \
1426 return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL); \
1429 NVLIST_EXISTS(null, NULL)
1430 NVLIST_EXISTS(bool, BOOL)
1431 NVLIST_EXISTS(number, NUMBER)
1432 NVLIST_EXISTS(string, STRING)
1433 NVLIST_EXISTS(nvlist, NVLIST)
1434 NVLIST_EXISTS(binary, BINARY)
1435 NVLIST_EXISTS(bool_array, BOOL_ARRAY)
1436 NVLIST_EXISTS(number_array, NUMBER_ARRAY)
1437 NVLIST_EXISTS(string_array, STRING_ARRAY)
1438 NVLIST_EXISTS(nvlist_array, NVLIST_ARRAY)
1440 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1441 NVLIST_EXISTS(descriptor_array, DESCRIPTOR_ARRAY)
1444 #undef NVLIST_EXISTS
1447 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1453 if (nvlist_error(nvl) != 0) {
1454 ERRNO_SET(nvlist_error(nvl));
1457 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1458 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1459 nvl->nvl_error = EEXIST;
1460 ERRNO_SET(nvlist_error(nvl));
1465 newnvp = nvpair_clone(nvp);
1466 if (newnvp == NULL) {
1467 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1468 ERRNO_SET(nvlist_error(nvl));
1472 nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1476 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1480 va_start(valueap, valuefmt);
1481 nvlist_add_stringv(nvl, name, valuefmt, valueap);
1486 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1491 if (nvlist_error(nvl) != 0) {
1492 ERRNO_SET(nvlist_error(nvl));
1496 nvp = nvpair_create_stringv(name, valuefmt, valueap);
1498 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1499 ERRNO_SET(nvl->nvl_error);
1501 (void)nvlist_move_nvpair(nvl, nvp);
1506 nvlist_add_null(nvlist_t *nvl, const char *name)
1510 if (nvlist_error(nvl) != 0) {
1511 ERRNO_SET(nvlist_error(nvl));
1515 nvp = nvpair_create_null(name);
1517 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1518 ERRNO_SET(nvl->nvl_error);
1520 (void)nvlist_move_nvpair(nvl, nvp);
1525 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1530 if (nvlist_error(nvl) != 0) {
1531 ERRNO_SET(nvlist_error(nvl));
1535 nvp = nvpair_create_binary(name, value, size);
1537 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1538 ERRNO_SET(nvl->nvl_error);
1540 (void)nvlist_move_nvpair(nvl, nvp);
1545 #define NVLIST_ADD(vtype, type) \
1547 nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value) \
1551 if (nvlist_error(nvl) != 0) { \
1552 ERRNO_SET(nvlist_error(nvl)); \
1556 nvp = nvpair_create_##type(name, value); \
1557 if (nvp == NULL) { \
1558 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1559 ERRNO_SET(nvl->nvl_error); \
1561 (void)nvlist_move_nvpair(nvl, nvp); \
1565 NVLIST_ADD(bool, bool)
1566 NVLIST_ADD(uint64_t, number)
1567 NVLIST_ADD(const char *, string)
1568 NVLIST_ADD(const nvlist_t *, nvlist)
1570 NVLIST_ADD(int, descriptor);
1575 #define NVLIST_ADD_ARRAY(vtype, type) \
1577 nvlist_add_##type##_array(nvlist_t *nvl, const char *name, vtype value, \
1582 if (nvlist_error(nvl) != 0) { \
1583 ERRNO_SET(nvlist_error(nvl)); \
1587 nvp = nvpair_create_##type##_array(name, value, nitems); \
1588 if (nvp == NULL) { \
1589 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1590 ERRNO_SET(nvl->nvl_error); \
1592 (void)nvlist_move_nvpair(nvl, nvp); \
1596 NVLIST_ADD_ARRAY(const bool *, bool)
1597 NVLIST_ADD_ARRAY(const uint64_t *, number)
1598 NVLIST_ADD_ARRAY(const char * const *, string)
1599 NVLIST_ADD_ARRAY(const nvlist_t * const *, nvlist)
1601 NVLIST_ADD_ARRAY(const int *, descriptor)
1604 #undef NVLIST_ADD_ARRAY
1607 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1611 PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1613 if (nvlist_error(nvl) != 0) {
1615 ERRNO_SET(nvlist_error(nvl));
1618 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1619 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1621 nvl->nvl_error = EEXIST;
1622 ERRNO_SET(nvl->nvl_error);
1627 nvpair_insert(&nvl->nvl_head, nvp, nvl);
1632 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1636 if (nvlist_error(nvl) != 0) {
1638 ERRNO_SET(nvlist_error(nvl));
1642 nvp = nvpair_move_string(name, value);
1644 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1645 ERRNO_SET(nvl->nvl_error);
1647 (void)nvlist_move_nvpair(nvl, nvp);
1652 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1656 if (nvlist_error(nvl) != 0) {
1657 if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1658 nvlist_destroy(value);
1659 ERRNO_SET(nvlist_error(nvl));
1663 nvp = nvpair_move_nvlist(name, value);
1665 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1666 ERRNO_SET(nvl->nvl_error);
1668 (void)nvlist_move_nvpair(nvl, nvp);
1674 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1678 if (nvlist_error(nvl) != 0) {
1680 ERRNO_SET(nvlist_error(nvl));
1684 nvp = nvpair_move_descriptor(name, value);
1686 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1687 ERRNO_SET(nvl->nvl_error);
1689 (void)nvlist_move_nvpair(nvl, nvp);
1695 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1699 if (nvlist_error(nvl) != 0) {
1701 ERRNO_SET(nvlist_error(nvl));
1705 nvp = nvpair_move_binary(name, value, size);
1707 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1708 ERRNO_SET(nvl->nvl_error);
1710 (void)nvlist_move_nvpair(nvl, nvp);
1715 nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value,
1720 if (nvlist_error(nvl) != 0) {
1722 ERRNO_SET(nvlist_error(nvl));
1726 nvp = nvpair_move_bool_array(name, value, nitems);
1728 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1729 ERRNO_SET(nvl->nvl_error);
1731 (void)nvlist_move_nvpair(nvl, nvp);
1736 nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value,
1742 if (nvlist_error(nvl) != 0) {
1743 if (value != NULL) {
1744 for (i = 0; i < nitems; i++)
1748 ERRNO_SET(nvlist_error(nvl));
1752 nvp = nvpair_move_string_array(name, value, nitems);
1754 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1755 ERRNO_SET(nvl->nvl_error);
1757 (void)nvlist_move_nvpair(nvl, nvp);
1762 nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value,
1768 if (nvlist_error(nvl) != 0) {
1769 if (value != NULL) {
1770 for (i = 0; i < nitems; i++) {
1771 if (nvlist_get_pararr(value[i], NULL) == NULL)
1772 nvlist_destroy(value[i]);
1776 ERRNO_SET(nvlist_error(nvl));
1780 nvp = nvpair_move_nvlist_array(name, value, nitems);
1782 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1783 ERRNO_SET(nvl->nvl_error);
1785 (void)nvlist_move_nvpair(nvl, nvp);
1790 nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value,
1795 if (nvlist_error(nvl) != 0) {
1797 ERRNO_SET(nvlist_error(nvl));
1801 nvp = nvpair_move_number_array(name, value, nitems);
1803 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1804 ERRNO_SET(nvl->nvl_error);
1806 (void)nvlist_move_nvpair(nvl, nvp);
1812 nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value,
1818 if (nvlist_error(nvl) != 0) {
1820 for (i = 0; i < nitems; i++)
1825 ERRNO_SET(nvlist_error(nvl));
1829 nvp = nvpair_move_descriptor_array(name, value, nitems);
1831 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1832 ERRNO_SET(nvl->nvl_error);
1834 (void)nvlist_move_nvpair(nvl, nvp);
1840 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1843 return (nvlist_find(nvl, NV_TYPE_NONE, name));
1846 #define NVLIST_GET(ftype, type, TYPE) \
1848 nvlist_get_##type(const nvlist_t *nvl, const char *name) \
1850 const nvpair_t *nvp; \
1852 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1854 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1855 return (nvpair_get_##type(nvp)); \
1858 NVLIST_GET(bool, bool, BOOL)
1859 NVLIST_GET(uint64_t, number, NUMBER)
1860 NVLIST_GET(const char *, string, STRING)
1861 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1863 NVLIST_GET(int, descriptor, DESCRIPTOR)
1869 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1873 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1875 nvlist_report_missing(NV_TYPE_BINARY, name);
1877 return (nvpair_get_binary(nvp, sizep));
1880 #define NVLIST_GET_ARRAY(ftype, type, TYPE) \
1882 nvlist_get_##type##_array(const nvlist_t *nvl, const char *name, \
1885 const nvpair_t *nvp; \
1887 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1889 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1890 return (nvpair_get_##type##_array(nvp, nitems)); \
1893 NVLIST_GET_ARRAY(const bool *, bool, BOOL)
1894 NVLIST_GET_ARRAY(const uint64_t *, number, NUMBER)
1895 NVLIST_GET_ARRAY(const char * const *, string, STRING)
1896 NVLIST_GET_ARRAY(const nvlist_t * const *, nvlist, NVLIST)
1898 NVLIST_GET_ARRAY(const int *, descriptor, DESCRIPTOR)
1901 #undef NVLIST_GET_ARRAY
1903 #define NVLIST_TAKE(ftype, type, TYPE) \
1905 nvlist_take_##type(nvlist_t *nvl, const char *name) \
1910 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1912 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1913 value = (ftype)(intptr_t)nvpair_get_##type(nvp); \
1914 nvlist_remove_nvpair(nvl, nvp); \
1915 nvpair_free_structure(nvp); \
1919 NVLIST_TAKE(bool, bool, BOOL)
1920 NVLIST_TAKE(uint64_t, number, NUMBER)
1921 NVLIST_TAKE(char *, string, STRING)
1922 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1924 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1930 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1935 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1937 nvlist_report_missing(NV_TYPE_BINARY, name);
1939 value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1940 nvlist_remove_nvpair(nvl, nvp);
1941 nvpair_free_structure(nvp);
1945 #define NVLIST_TAKE_ARRAY(ftype, type, TYPE) \
1947 nvlist_take_##type##_array(nvlist_t *nvl, const char *name, \
1953 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1955 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1956 value = (ftype)(intptr_t)nvpair_get_##type##_array(nvp, nitems);\
1957 nvlist_remove_nvpair(nvl, nvp); \
1958 nvpair_free_structure(nvp); \
1962 NVLIST_TAKE_ARRAY(bool *, bool, BOOL)
1963 NVLIST_TAKE_ARRAY(uint64_t *, number, NUMBER)
1964 NVLIST_TAKE_ARRAY(char **, string, STRING)
1965 NVLIST_TAKE_ARRAY(nvlist_t **, nvlist, NVLIST)
1967 NVLIST_TAKE_ARRAY(int *, descriptor, DESCRIPTOR)
1971 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1976 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1978 nvpair_remove(&nvl->nvl_head, nvp, nvl);
1982 nvlist_free(nvlist_t *nvl, const char *name)
1985 nvlist_free_type(nvl, name, NV_TYPE_NONE);
1988 #define NVLIST_FREE(type, TYPE) \
1990 nvlist_free_##type(nvlist_t *nvl, const char *name) \
1993 nvlist_free_type(nvl, name, NV_TYPE_##TYPE); \
1996 NVLIST_FREE(null, NULL)
1997 NVLIST_FREE(bool, BOOL)
1998 NVLIST_FREE(number, NUMBER)
1999 NVLIST_FREE(string, STRING)
2000 NVLIST_FREE(nvlist, NVLIST)
2001 NVLIST_FREE(binary, BINARY)
2002 NVLIST_FREE(bool_array, BOOL_ARRAY)
2003 NVLIST_FREE(number_array, NUMBER_ARRAY)
2004 NVLIST_FREE(string_array, STRING_ARRAY)
2005 NVLIST_FREE(nvlist_array, NVLIST_ARRAY)
2007 NVLIST_FREE(descriptor, DESCRIPTOR)
2008 NVLIST_FREE(descriptor_array, DESCRIPTOR_ARRAY)
2014 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2019 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2021 nvlist_remove_nvpair(nvl, nvp);