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 ret = nvlist_get_parent(nvl, cookiep);
288 nvlist_empty(const nvlist_t *nvl)
292 PJDLOG_ASSERT(nvl->nvl_error == 0);
294 return (nvlist_first_nvpair(nvl) == NULL);
298 nvlist_flags(const nvlist_t *nvl)
302 PJDLOG_ASSERT(nvl->nvl_error == 0);
304 return (nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
308 nvlist_set_flags(nvlist_t *nvl, int flags)
312 PJDLOG_ASSERT(nvl->nvl_error == 0);
314 nvl->nvl_flags = flags;
318 nvlist_report_missing(int type, const char *name)
321 PJDLOG_ABORT("Element '%s' of type %s doesn't exist.",
322 name, nvpair_type_string(type));
326 nvlist_find(const nvlist_t *nvl, int type, const char *name)
331 PJDLOG_ASSERT(nvl->nvl_error == 0);
332 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
333 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
335 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
336 nvp = nvlist_next_nvpair(nvl, nvp)) {
337 if (type != NV_TYPE_NONE && nvpair_type(nvp) != type)
339 if ((nvl->nvl_flags & NV_FLAG_IGNORE_CASE) != 0) {
340 if (strcasecmp(nvpair_name(nvp), name) != 0)
343 if (strcmp(nvpair_name(nvp), name) != 0)
356 nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
360 PJDLOG_ASSERT(nvl->nvl_error == 0);
361 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
362 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
364 return (nvlist_find(nvl, type, name) != NULL);
368 nvlist_free_type(nvlist_t *nvl, const char *name, int type)
373 PJDLOG_ASSERT(nvl->nvl_error == 0);
374 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
375 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
377 nvp = nvlist_find(nvl, type, name);
379 nvlist_free_nvpair(nvl, nvp);
381 nvlist_report_missing(type, name);
385 nvlist_clone(const nvlist_t *nvl)
388 nvpair_t *nvp, *newnvp;
392 if (nvl->nvl_error != 0) {
393 ERRNO_SET(nvl->nvl_error);
397 newnvl = nvlist_create(nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
398 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
399 nvp = nvlist_next_nvpair(nvl, nvp)) {
400 newnvp = nvpair_clone(nvp);
403 (void)nvlist_move_nvpair(newnvl, newnvp);
406 nvlist_destroy(newnvl);
414 nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
417 if (nvlist_error(nvl) != 0) {
418 dprintf(fd, "%*serror: %d\n", level * 4, "",
427 * Dump content of nvlist.
430 nvlist_dump(const nvlist_t *nvl, int fd)
432 const nvlist_t *tmpnvl;
433 nvpair_t *nvp, *tmpnvp;
438 if (nvlist_dump_error_check(nvl, fd, level))
441 nvp = nvlist_first_nvpair(nvl);
442 while (nvp != NULL) {
443 dprintf(fd, "%*s%s (%s):", level * 4, "", nvpair_name(nvp),
444 nvpair_type_string(nvpair_type(nvp)));
445 switch (nvpair_type(nvp)) {
447 dprintf(fd, " null\n");
450 dprintf(fd, " %s\n", nvpair_get_bool(nvp) ?
454 dprintf(fd, " %ju (%jd) (0x%jx)\n",
455 (uintmax_t)nvpair_get_number(nvp),
456 (intmax_t)nvpair_get_number(nvp),
457 (uintmax_t)nvpair_get_number(nvp));
460 dprintf(fd, " [%s]\n", nvpair_get_string(nvp));
464 tmpnvl = nvpair_get_nvlist(nvp);
465 if (nvlist_dump_error_check(tmpnvl, fd, level + 1))
467 tmpnvp = nvlist_first_nvpair(tmpnvl);
468 if (tmpnvp != NULL) {
475 case NV_TYPE_DESCRIPTOR:
476 dprintf(fd, " %d\n", nvpair_get_descriptor(nvp));
480 const unsigned char *binary;
484 binary = nvpair_get_binary(nvp, &size);
485 dprintf(fd, " %zu ", size);
486 for (ii = 0; ii < size; ii++)
487 dprintf(fd, "%02hhx", binary[ii]);
491 case NV_TYPE_BOOL_ARRAY:
497 value = nvpair_get_bool_array(nvp, &nitems);
499 for (ii = 0; ii < nitems; ii++) {
500 dprintf(fd, "%s", value[ii] ? "TRUE" : "FALSE");
501 if (ii != nitems - 1)
507 case NV_TYPE_STRING_ARRAY:
509 const char * const *value;
513 value = nvpair_get_string_array(nvp, &nitems);
515 for (ii = 0; ii < nitems; ii++) {
516 if (value[ii] == NULL)
519 dprintf(fd, "\"%s\"", value[ii]);
520 if (ii != nitems - 1)
526 case NV_TYPE_NUMBER_ARRAY:
528 const uint64_t *value;
532 value = nvpair_get_number_array(nvp, &nitems);
534 for (ii = 0; ii < nitems; ii++) {
535 dprintf(fd, "%ju (%jd) (0x%jx)",
536 value[ii], value[ii], value[ii]);
537 if (ii != nitems - 1)
543 case NV_TYPE_DESCRIPTOR_ARRAY:
549 value = nvpair_get_descriptor_array(nvp, &nitems);
551 for (ii = 0; ii < nitems; ii++) {
552 dprintf(fd, "%d", value[ii]);
553 if (ii != nitems - 1)
559 case NV_TYPE_NVLIST_ARRAY:
561 const nvlist_t * const *value;
565 value = nvpair_get_nvlist_array(nvp, &nitems);
566 dprintf(fd, " %zu\n", nitems);
569 for (ii = 0; ii < nitems; ii++) {
570 if (nvlist_dump_error_check(value[ii], fd,
575 if (tmpnvl == NULL) {
576 tmpnvp = nvlist_first_nvpair(value[ii]);
577 if (tmpnvp != NULL) {
580 dprintf(fd, "%*s,\n",
581 (level + 1) * 4, "");
585 if (tmpnvp != NULL) {
594 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
597 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
600 if (nvlist_in_array(nvl))
601 dprintf(fd, "%*s,\n", level * 4, "");
602 nvl = nvlist_get_pararr(nvl, &cookie);
605 if (nvlist_in_array(nvl) && cookie == NULL) {
606 nvp = nvlist_first_nvpair(nvl);
611 } while (nvp == NULL);
612 if (nvlist_in_array(nvl) && cookie == NULL)
619 nvlist_fdump(const nvlist_t *nvl, FILE *fp)
623 nvlist_dump(nvl, fileno(fp));
628 * The function obtains size of the nvlist after nvlist_pack().
631 nvlist_size(const nvlist_t *nvl)
633 const nvlist_t *tmpnvl;
634 const nvlist_t * const *nvlarray;
635 const nvpair_t *nvp, *tmpnvp;
641 PJDLOG_ASSERT(nvl->nvl_error == 0);
643 size = sizeof(struct nvlist_header);
644 nvp = nvlist_first_nvpair(nvl);
645 while (nvp != NULL) {
646 size += nvpair_header_size();
647 size += strlen(nvpair_name(nvp)) + 1;
648 if (nvpair_type(nvp) == NV_TYPE_NVLIST) {
649 size += sizeof(struct nvlist_header);
650 size += nvpair_header_size() + 1;
651 tmpnvl = nvpair_get_nvlist(nvp);
652 PJDLOG_ASSERT(tmpnvl->nvl_error == 0);
653 tmpnvp = nvlist_first_nvpair(tmpnvl);
654 if (tmpnvp != NULL) {
659 } else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY) {
660 nvlarray = nvpair_get_nvlist_array(nvp, &nitems);
661 PJDLOG_ASSERT(nitems > 0);
663 size += (nvpair_header_size() + 1) * nitems;
664 size += sizeof(struct nvlist_header) * nitems;
668 for (ii = 0; ii < nitems; ii++) {
669 PJDLOG_ASSERT(nvlarray[ii]->nvl_error == 0);
670 tmpnvp = nvlist_first_nvpair(nvlarray[ii]);
671 if (tmpnvp != NULL) {
672 tmpnvl = nvlarray[ii];
676 if (tmpnvp != NULL) {
683 size += nvpair_size(nvp);
686 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
689 nvl = nvlist_get_pararr(nvl, &cookie);
692 if (nvlist_in_array(nvl) && cookie == NULL) {
693 nvp = nvlist_first_nvpair(nvl);
697 } while (nvp == NULL);
698 if (nvlist_in_array(nvl) && cookie == NULL)
709 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
716 PJDLOG_ASSERT(nvl->nvl_error == 0);
720 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
722 case NV_TYPE_DESCRIPTOR:
723 *descs = nvpair_get_descriptor(nvp);
726 case NV_TYPE_DESCRIPTOR_ARRAY:
732 value = nvpair_get_descriptor_array(nvp,
734 for (ii = 0; ii < nitems; ii++) {
741 nvl = nvpair_get_nvlist(nvp);
744 case NV_TYPE_NVLIST_ARRAY:
746 const nvlist_t * const *value;
749 value = nvpair_get_nvlist_array(nvp, &nitems);
750 PJDLOG_ASSERT(value != NULL);
751 PJDLOG_ASSERT(nitems > 0);
759 } while ((nvl = nvlist_get_pararr(nvl, (void**)&nvp)) != NULL);
767 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
772 nitems = nvlist_ndescriptors(nvl);
773 fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
777 nvlist_xdescriptors(nvl, fds);
786 nvlist_ndescriptors(const nvlist_t *nvl)
795 PJDLOG_ASSERT(nvl->nvl_error == 0);
800 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
802 case NV_TYPE_DESCRIPTOR:
806 nvl = nvpair_get_nvlist(nvp);
809 case NV_TYPE_NVLIST_ARRAY:
811 const nvlist_t * const *value;
814 value = nvpair_get_nvlist_array(nvp, &nitems);
815 PJDLOG_ASSERT(value != NULL);
816 PJDLOG_ASSERT(nitems > 0);
822 case NV_TYPE_DESCRIPTOR_ARRAY:
826 (void)nvpair_get_descriptor_array(nvp,
833 } while ((nvl = nvlist_get_pararr(nvl, (void**)&nvp)) != NULL);
841 static unsigned char *
842 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
844 struct nvlist_header nvlhdr;
848 nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
849 nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
850 nvlhdr.nvlh_flags = nvl->nvl_flags;
851 #if BYTE_ORDER == BIG_ENDIAN
852 nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
854 nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
855 nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
856 PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
857 memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
858 ptr += sizeof(nvlhdr);
859 *leftp -= sizeof(nvlhdr);
865 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
867 unsigned char *buf, *ptr;
869 const nvlist_t *tmpnvl;
870 nvpair_t *nvp, *tmpnvp;
875 if (nvl->nvl_error != 0) {
876 ERRNO_SET(nvl->nvl_error);
880 size = nvlist_size(nvl);
881 buf = nv_malloc(size);
888 ptr = nvlist_pack_header(nvl, ptr, &left);
890 nvp = nvlist_first_nvpair(nvl);
891 while (nvp != NULL) {
894 nvpair_init_datasize(nvp);
895 ptr = nvpair_pack_header(nvp, ptr, &left);
898 switch (nvpair_type(nvp)) {
900 ptr = nvpair_pack_null(nvp, ptr, &left);
903 ptr = nvpair_pack_bool(nvp, ptr, &left);
906 ptr = nvpair_pack_number(nvp, ptr, &left);
909 ptr = nvpair_pack_string(nvp, ptr, &left);
912 tmpnvl = nvpair_get_nvlist(nvp);
913 ptr = nvlist_pack_header(tmpnvl, ptr, &left);
916 tmpnvp = nvlist_first_nvpair(tmpnvl);
917 if (tmpnvp != NULL) {
922 ptr = nvpair_pack_nvlist_up(ptr, &left);
925 case NV_TYPE_DESCRIPTOR:
926 ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
928 case NV_TYPE_DESCRIPTOR_ARRAY:
929 ptr = nvpair_pack_descriptor_array(nvp, ptr, fdidxp,
934 ptr = nvpair_pack_binary(nvp, ptr, &left);
936 case NV_TYPE_BOOL_ARRAY:
937 ptr = nvpair_pack_bool_array(nvp, ptr, &left);
939 case NV_TYPE_NUMBER_ARRAY:
940 ptr = nvpair_pack_number_array(nvp, ptr, &left);
942 case NV_TYPE_STRING_ARRAY:
943 ptr = nvpair_pack_string_array(nvp, ptr, &left);
945 case NV_TYPE_NVLIST_ARRAY:
947 const nvlist_t * const * value;
952 value = nvpair_get_nvlist_array(nvp, &nitems);
953 for (ii = 0; ii < nitems; ii++) {
954 ptr = nvlist_pack_header(value[ii], ptr, &left);
957 tmpnvp = nvlist_first_nvpair(value[ii]);
958 if (tmpnvp != NULL) {
962 ptr = nvpair_pack_nvlist_array_next(ptr, &left);
966 if (tmpnvl != NULL) {
974 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
978 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
981 if (nvlist_in_array(nvl)) {
982 ptr = nvpair_pack_nvlist_array_next(ptr,
987 nvl = nvlist_get_pararr(nvl, &cookie);
990 if (nvlist_in_array(nvl) && cookie == NULL) {
991 nvp = nvlist_first_nvpair(nvl);
992 ptr = nvlist_pack_header(nvl, ptr,
996 } else if (nvpair_type((nvpair_t *)cookie) !=
997 NV_TYPE_NVLIST_ARRAY) {
998 ptr = nvpair_pack_nvlist_up(ptr, &left);
1005 } while (nvp == NULL);
1006 if (nvlist_in_array(nvl) && cookie == NULL)
1021 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
1026 if (nvl->nvl_error != 0) {
1027 ERRNO_SET(nvl->nvl_error);
1031 if (nvlist_ndescriptors(nvl) > 0) {
1032 ERRNO_SET(EOPNOTSUPP);
1036 return (nvlist_xpack(nvl, NULL, sizep));
1040 nvlist_check_header(struct nvlist_header *nvlhdrp)
1043 if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
1047 if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
1051 #if BYTE_ORDER == BIG_ENDIAN
1052 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
1053 nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
1054 nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
1057 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
1058 nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
1059 nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
1065 const unsigned char *
1066 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
1067 bool *isbep, size_t *leftp)
1069 struct nvlist_header nvlhdr;
1072 if (*leftp < sizeof(nvlhdr))
1075 memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
1077 if (!nvlist_check_header(&nvlhdr))
1080 if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
1084 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
1086 if (nvlhdr.nvlh_descriptors > nfds)
1089 if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
1092 inarrayf = (nvl->nvl_flags & NV_FLAG_IN_ARRAY);
1093 nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK) | inarrayf;
1095 ptr += sizeof(nvlhdr);
1097 *isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
1098 *leftp -= sizeof(nvlhdr);
1107 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds,
1110 const unsigned char *ptr;
1111 nvlist_t *nvl, *retnvl, *tmpnvl, *array;
1116 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
1121 tmpnvl = array = NULL;
1122 nvl = retnvl = nvlist_create(0);
1126 ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
1129 if (nvl->nvl_flags != flags) {
1135 ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
1138 switch (nvpair_type(nvp)) {
1140 ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
1143 ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
1145 case NV_TYPE_NUMBER:
1146 ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
1148 case NV_TYPE_STRING:
1149 ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
1151 case NV_TYPE_NVLIST:
1152 ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
1154 if (tmpnvl == NULL || ptr == NULL)
1156 nvlist_set_parent(tmpnvl, nvp);
1159 case NV_TYPE_DESCRIPTOR:
1160 ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
1163 case NV_TYPE_DESCRIPTOR_ARRAY:
1164 ptr = nvpair_unpack_descriptor_array(isbe, nvp, ptr,
1168 case NV_TYPE_BINARY:
1169 ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
1171 case NV_TYPE_NVLIST_UP:
1172 if (nvl->nvl_parent == NULL)
1174 nvl = nvpair_nvlist(nvl->nvl_parent);
1175 nvpair_free_structure(nvp);
1177 case NV_TYPE_NVLIST_ARRAY_NEXT:
1178 if (nvl->nvl_array_next == NULL) {
1179 if (nvl->nvl_parent == NULL)
1181 nvl = nvpair_nvlist(nvl->nvl_parent);
1183 nvl = __DECONST(nvlist_t *,
1184 nvlist_get_array_next(nvl));
1185 ptr = nvlist_unpack_header(nvl, ptr, nfds,
1190 nvpair_free_structure(nvp);
1192 case NV_TYPE_BOOL_ARRAY:
1193 ptr = nvpair_unpack_bool_array(isbe, nvp, ptr, &left);
1195 case NV_TYPE_NUMBER_ARRAY:
1196 ptr = nvpair_unpack_number_array(isbe, nvp, ptr, &left);
1198 case NV_TYPE_STRING_ARRAY:
1199 ptr = nvpair_unpack_string_array(isbe, nvp, ptr, &left);
1201 case NV_TYPE_NVLIST_ARRAY:
1202 ptr = nvpair_unpack_nvlist_array(isbe, nvp, ptr, &left,
1207 while (array != NULL) {
1208 nvlist_set_parent(array, nvp);
1209 array = __DECONST(nvlist_t *,
1210 nvlist_get_array_next(array));
1212 ptr = nvlist_unpack_header(tmpnvl, ptr, nfds, &isbe,
1216 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
1220 if (!nvlist_move_nvpair(nvl, nvp))
1222 if (tmpnvl != NULL) {
1230 nvlist_destroy(retnvl);
1235 nvlist_unpack(const void *buf, size_t size, int flags)
1238 return (nvlist_xunpack(buf, size, NULL, 0, flags));
1243 nvlist_send(int sock, const nvlist_t *nvl)
1245 size_t datasize, nfds;
1251 if (nvlist_error(nvl) != 0) {
1252 ERRNO_SET(nvlist_error(nvl));
1256 fds = nvlist_descriptors(nvl, &nfds);
1264 data = nvlist_xpack(nvl, &fdidx, &datasize);
1268 if (buf_send(sock, data, datasize) == -1)
1272 if (fd_send(sock, fds, nfds) == -1)
1286 nvlist_recv(int sock, int flags)
1288 struct nvlist_header nvlhdr;
1289 nvlist_t *nvl, *ret;
1291 size_t nfds, size, i;
1294 if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr)) == -1)
1297 if (!nvlist_check_header(&nvlhdr))
1300 nfds = (size_t)nvlhdr.nvlh_descriptors;
1301 size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
1303 buf = nv_malloc(size);
1307 memcpy(buf, &nvlhdr, sizeof(nvlhdr));
1312 if (buf_recv(sock, buf + sizeof(nvlhdr), size - sizeof(nvlhdr)) == -1)
1316 fds = nv_malloc(nfds * sizeof(fds[0]));
1319 if (fd_recv(sock, fds, nfds) == -1)
1323 nvl = nvlist_xunpack(buf, size, fds, nfds, flags);
1326 for (i = 0; i < nfds; i++)
1343 nvlist_xfer(int sock, nvlist_t *nvl, int flags)
1346 if (nvlist_send(sock, nvl) < 0) {
1347 nvlist_destroy(nvl);
1350 nvlist_destroy(nvl);
1351 return (nvlist_recv(sock, flags));
1356 nvlist_first_nvpair(const nvlist_t *nvl)
1361 return (TAILQ_FIRST(&nvl->nvl_head));
1365 nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1371 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1373 retnvp = nvpair_next(nvp);
1374 PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
1381 nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
1387 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1389 retnvp = nvpair_prev(nvp);
1390 PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1396 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1402 if (cookiep == NULL || *cookiep == NULL)
1403 nvp = nvlist_first_nvpair(nvl);
1405 nvp = nvlist_next_nvpair(nvl, *cookiep);
1409 *typep = nvpair_type(nvp);
1410 if (cookiep != NULL)
1412 return (nvpair_name(nvp));
1416 nvlist_exists(const nvlist_t *nvl, const char *name)
1419 return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1422 #define NVLIST_EXISTS(type, TYPE) \
1424 nvlist_exists_##type(const nvlist_t *nvl, const char *name) \
1427 return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL); \
1430 NVLIST_EXISTS(null, NULL)
1431 NVLIST_EXISTS(bool, BOOL)
1432 NVLIST_EXISTS(number, NUMBER)
1433 NVLIST_EXISTS(string, STRING)
1434 NVLIST_EXISTS(nvlist, NVLIST)
1435 NVLIST_EXISTS(binary, BINARY)
1436 NVLIST_EXISTS(bool_array, BOOL_ARRAY)
1437 NVLIST_EXISTS(number_array, NUMBER_ARRAY)
1438 NVLIST_EXISTS(string_array, STRING_ARRAY)
1439 NVLIST_EXISTS(nvlist_array, NVLIST_ARRAY)
1441 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1442 NVLIST_EXISTS(descriptor_array, DESCRIPTOR_ARRAY)
1445 #undef NVLIST_EXISTS
1448 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1454 if (nvlist_error(nvl) != 0) {
1455 ERRNO_SET(nvlist_error(nvl));
1458 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1459 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1460 nvl->nvl_error = EEXIST;
1461 ERRNO_SET(nvlist_error(nvl));
1466 newnvp = nvpair_clone(nvp);
1467 if (newnvp == NULL) {
1468 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1469 ERRNO_SET(nvlist_error(nvl));
1473 nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1477 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1481 va_start(valueap, valuefmt);
1482 nvlist_add_stringv(nvl, name, valuefmt, valueap);
1487 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1492 if (nvlist_error(nvl) != 0) {
1493 ERRNO_SET(nvlist_error(nvl));
1497 nvp = nvpair_create_stringv(name, valuefmt, valueap);
1499 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1500 ERRNO_SET(nvl->nvl_error);
1502 (void)nvlist_move_nvpair(nvl, nvp);
1507 nvlist_add_null(nvlist_t *nvl, const char *name)
1511 if (nvlist_error(nvl) != 0) {
1512 ERRNO_SET(nvlist_error(nvl));
1516 nvp = nvpair_create_null(name);
1518 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1519 ERRNO_SET(nvl->nvl_error);
1521 (void)nvlist_move_nvpair(nvl, nvp);
1526 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1531 if (nvlist_error(nvl) != 0) {
1532 ERRNO_SET(nvlist_error(nvl));
1536 nvp = nvpair_create_binary(name, value, size);
1538 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1539 ERRNO_SET(nvl->nvl_error);
1541 (void)nvlist_move_nvpair(nvl, nvp);
1546 #define NVLIST_ADD(vtype, type) \
1548 nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value) \
1552 if (nvlist_error(nvl) != 0) { \
1553 ERRNO_SET(nvlist_error(nvl)); \
1557 nvp = nvpair_create_##type(name, value); \
1558 if (nvp == NULL) { \
1559 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1560 ERRNO_SET(nvl->nvl_error); \
1562 (void)nvlist_move_nvpair(nvl, nvp); \
1566 NVLIST_ADD(bool, bool)
1567 NVLIST_ADD(uint64_t, number)
1568 NVLIST_ADD(const char *, string)
1569 NVLIST_ADD(const nvlist_t *, nvlist)
1571 NVLIST_ADD(int, descriptor);
1576 #define NVLIST_ADD_ARRAY(vtype, type) \
1578 nvlist_add_##type##_array(nvlist_t *nvl, const char *name, vtype value, \
1583 if (nvlist_error(nvl) != 0) { \
1584 ERRNO_SET(nvlist_error(nvl)); \
1588 nvp = nvpair_create_##type##_array(name, value, nitems); \
1589 if (nvp == NULL) { \
1590 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
1591 ERRNO_SET(nvl->nvl_error); \
1593 (void)nvlist_move_nvpair(nvl, nvp); \
1597 NVLIST_ADD_ARRAY(const bool *, bool)
1598 NVLIST_ADD_ARRAY(const uint64_t *, number)
1599 NVLIST_ADD_ARRAY(const char * const *, string)
1600 NVLIST_ADD_ARRAY(const nvlist_t * const *, nvlist)
1602 NVLIST_ADD_ARRAY(const int *, descriptor)
1605 #undef NVLIST_ADD_ARRAY
1608 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1612 PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1614 if (nvlist_error(nvl) != 0) {
1616 ERRNO_SET(nvlist_error(nvl));
1619 if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1620 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1622 nvl->nvl_error = EEXIST;
1623 ERRNO_SET(nvl->nvl_error);
1628 nvpair_insert(&nvl->nvl_head, nvp, nvl);
1633 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1637 if (nvlist_error(nvl) != 0) {
1639 ERRNO_SET(nvlist_error(nvl));
1643 nvp = nvpair_move_string(name, value);
1645 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1646 ERRNO_SET(nvl->nvl_error);
1648 (void)nvlist_move_nvpair(nvl, nvp);
1653 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1657 if (nvlist_error(nvl) != 0) {
1658 if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1659 nvlist_destroy(value);
1660 ERRNO_SET(nvlist_error(nvl));
1664 nvp = nvpair_move_nvlist(name, value);
1666 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1667 ERRNO_SET(nvl->nvl_error);
1669 (void)nvlist_move_nvpair(nvl, nvp);
1675 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1679 if (nvlist_error(nvl) != 0) {
1681 ERRNO_SET(nvlist_error(nvl));
1685 nvp = nvpair_move_descriptor(name, value);
1687 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1688 ERRNO_SET(nvl->nvl_error);
1690 (void)nvlist_move_nvpair(nvl, nvp);
1696 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1700 if (nvlist_error(nvl) != 0) {
1702 ERRNO_SET(nvlist_error(nvl));
1706 nvp = nvpair_move_binary(name, value, size);
1708 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1709 ERRNO_SET(nvl->nvl_error);
1711 (void)nvlist_move_nvpair(nvl, nvp);
1716 nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value,
1721 if (nvlist_error(nvl) != 0) {
1723 ERRNO_SET(nvlist_error(nvl));
1727 nvp = nvpair_move_bool_array(name, value, nitems);
1729 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1730 ERRNO_SET(nvl->nvl_error);
1732 (void)nvlist_move_nvpair(nvl, nvp);
1737 nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value,
1743 if (nvlist_error(nvl) != 0) {
1744 if (value != NULL) {
1745 for (i = 0; i < nitems; i++)
1749 ERRNO_SET(nvlist_error(nvl));
1753 nvp = nvpair_move_string_array(name, value, nitems);
1755 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1756 ERRNO_SET(nvl->nvl_error);
1758 (void)nvlist_move_nvpair(nvl, nvp);
1763 nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value,
1769 if (nvlist_error(nvl) != 0) {
1770 if (value != NULL) {
1771 for (i = 0; i < nitems; i++) {
1772 if (nvlist_get_pararr(value[i], NULL) == NULL)
1773 nvlist_destroy(value[i]);
1777 ERRNO_SET(nvlist_error(nvl));
1781 nvp = nvpair_move_nvlist_array(name, value, nitems);
1783 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1784 ERRNO_SET(nvl->nvl_error);
1786 (void)nvlist_move_nvpair(nvl, nvp);
1791 nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value,
1796 if (nvlist_error(nvl) != 0) {
1798 ERRNO_SET(nvlist_error(nvl));
1802 nvp = nvpair_move_number_array(name, value, nitems);
1804 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1805 ERRNO_SET(nvl->nvl_error);
1807 (void)nvlist_move_nvpair(nvl, nvp);
1813 nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value,
1819 if (nvlist_error(nvl) != 0) {
1821 for (i = 0; i < nitems; i++)
1826 ERRNO_SET(nvlist_error(nvl));
1830 nvp = nvpair_move_descriptor_array(name, value, nitems);
1832 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1833 ERRNO_SET(nvl->nvl_error);
1835 (void)nvlist_move_nvpair(nvl, nvp);
1841 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1844 return (nvlist_find(nvl, NV_TYPE_NONE, name));
1847 #define NVLIST_GET(ftype, type, TYPE) \
1849 nvlist_get_##type(const nvlist_t *nvl, const char *name) \
1851 const nvpair_t *nvp; \
1853 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1855 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1856 return (nvpair_get_##type(nvp)); \
1859 NVLIST_GET(bool, bool, BOOL)
1860 NVLIST_GET(uint64_t, number, NUMBER)
1861 NVLIST_GET(const char *, string, STRING)
1862 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1864 NVLIST_GET(int, descriptor, DESCRIPTOR)
1870 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1874 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1876 nvlist_report_missing(NV_TYPE_BINARY, name);
1878 return (nvpair_get_binary(nvp, sizep));
1881 #define NVLIST_GET_ARRAY(ftype, type, TYPE) \
1883 nvlist_get_##type##_array(const nvlist_t *nvl, const char *name, \
1886 const nvpair_t *nvp; \
1888 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1890 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1891 return (nvpair_get_##type##_array(nvp, nitems)); \
1894 NVLIST_GET_ARRAY(const bool *, bool, BOOL)
1895 NVLIST_GET_ARRAY(const uint64_t *, number, NUMBER)
1896 NVLIST_GET_ARRAY(const char * const *, string, STRING)
1897 NVLIST_GET_ARRAY(const nvlist_t * const *, nvlist, NVLIST)
1899 NVLIST_GET_ARRAY(const int *, descriptor, DESCRIPTOR)
1902 #undef NVLIST_GET_ARRAY
1904 #define NVLIST_TAKE(ftype, type, TYPE) \
1906 nvlist_take_##type(nvlist_t *nvl, const char *name) \
1911 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1913 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1914 value = (ftype)(intptr_t)nvpair_get_##type(nvp); \
1915 nvlist_remove_nvpair(nvl, nvp); \
1916 nvpair_free_structure(nvp); \
1920 NVLIST_TAKE(bool, bool, BOOL)
1921 NVLIST_TAKE(uint64_t, number, NUMBER)
1922 NVLIST_TAKE(char *, string, STRING)
1923 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1925 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1931 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1936 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1938 nvlist_report_missing(NV_TYPE_BINARY, name);
1940 value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1941 nvlist_remove_nvpair(nvl, nvp);
1942 nvpair_free_structure(nvp);
1946 #define NVLIST_TAKE_ARRAY(ftype, type, TYPE) \
1948 nvlist_take_##type##_array(nvlist_t *nvl, const char *name, \
1954 nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name); \
1956 nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name); \
1957 value = (ftype)(intptr_t)nvpair_get_##type##_array(nvp, nitems);\
1958 nvlist_remove_nvpair(nvl, nvp); \
1959 nvpair_free_structure(nvp); \
1963 NVLIST_TAKE_ARRAY(bool *, bool, BOOL)
1964 NVLIST_TAKE_ARRAY(uint64_t *, number, NUMBER)
1965 NVLIST_TAKE_ARRAY(char **, string, STRING)
1966 NVLIST_TAKE_ARRAY(nvlist_t **, nvlist, NVLIST)
1968 NVLIST_TAKE_ARRAY(int *, descriptor, DESCRIPTOR)
1972 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1977 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1979 nvpair_remove(&nvl->nvl_head, nvp, nvl);
1983 nvlist_free(nvlist_t *nvl, const char *name)
1986 nvlist_free_type(nvl, name, NV_TYPE_NONE);
1989 #define NVLIST_FREE(type, TYPE) \
1991 nvlist_free_##type(nvlist_t *nvl, const char *name) \
1994 nvlist_free_type(nvl, name, NV_TYPE_##TYPE); \
1997 NVLIST_FREE(null, NULL)
1998 NVLIST_FREE(bool, BOOL)
1999 NVLIST_FREE(number, NUMBER)
2000 NVLIST_FREE(string, STRING)
2001 NVLIST_FREE(nvlist, NVLIST)
2002 NVLIST_FREE(binary, BINARY)
2003 NVLIST_FREE(bool_array, BOOL_ARRAY)
2004 NVLIST_FREE(number_array, NUMBER_ARRAY)
2005 NVLIST_FREE(string_array, STRING_ARRAY)
2006 NVLIST_FREE(nvlist_array, NVLIST_ARRAY)
2008 NVLIST_FREE(descriptor, DESCRIPTOR)
2009 NVLIST_FREE(descriptor_array, DESCRIPTOR_ARRAY)
2015 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2020 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2022 nvlist_remove_nvpair(nvl, nvp);