2 * Copyright (c) 2009-2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/queue.h>
39 #include <sys/errno.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
45 #include <machine/stdarg.h>
48 #include <sys/socket.h>
68 #include <sys/nv_impl.h>
69 #include <sys/nvlist_impl.h>
70 #include <sys/nvpair_impl.h>
74 #define PJDLOG_ASSERT(...) MPASS(__VA_ARGS__)
75 #define PJDLOG_RASSERT(expr, ...) KASSERT(expr, (__VA_ARGS__))
76 #define PJDLOG_ABORT(...) panic(__VA_ARGS__)
79 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
80 #define PJDLOG_RASSERT(expr, ...) assert(expr)
81 #define PJDLOG_ABORT(...) do { \
82 fprintf(stderr, "%s:%u: ", __FILE__, __LINE__); \
83 fprintf(stderr, __VA_ARGS__); \
84 fprintf(stderr, "\n"); \
90 #define NV_FLAG_PRIVATE_MASK (NV_FLAG_BIG_ENDIAN)
91 #define NV_FLAG_PUBLIC_MASK (NV_FLAG_IGNORE_CASE)
92 #define NV_FLAG_ALL_MASK (NV_FLAG_PRIVATE_MASK | NV_FLAG_PUBLIC_MASK)
94 #define NVLIST_MAGIC 0x6e766c /* "nvl" */
100 struct nvl_head nvl_head;
103 #define NVLIST_ASSERT(nvl) do { \
104 PJDLOG_ASSERT((nvl) != NULL); \
105 PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC); \
109 MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
112 #define NVPAIR_ASSERT(nvp) nvpair_assert(nvp)
114 #define NVLIST_HEADER_MAGIC 0x6c
115 #define NVLIST_HEADER_VERSION 0x00
116 struct nvlist_header {
118 uint8_t nvlh_version;
120 uint64_t nvlh_descriptors;
125 nvlist_create(int flags)
129 PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
131 nvl = nv_malloc(sizeof(*nvl));
133 nvl->nvl_flags = flags;
134 nvl->nvl_parent = NULL;
135 TAILQ_INIT(&nvl->nvl_head);
136 nvl->nvl_magic = NVLIST_MAGIC;
142 nvlist_destroy(nvlist_t *nvl)
154 while ((nvp = nvlist_first_nvpair(nvl)) != NULL) {
155 nvlist_remove_nvpair(nvl, nvp);
161 RESTORE_ERRNO(serrno);
165 nvlist_set_error(nvlist_t *nvl, int error)
168 PJDLOG_ASSERT(error != 0);
171 * Check for error != 0 so that we don't do the wrong thing if somebody
172 * tries to abuse this API when asserts are disabled.
174 if (nvl != NULL && error != 0 && nvl->nvl_error == 0)
175 nvl->nvl_error = error;
179 nvlist_error(const nvlist_t *nvl)
187 return (nvl->nvl_error);
191 nvlist_get_nvpair_parent(const nvlist_t *nvl)
196 return (nvl->nvl_parent);
200 nvlist_get_parent(const nvlist_t *nvl, void **cookiep)
206 nvp = nvl->nvl_parent;
212 return (nvpair_nvlist(nvp));
216 nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent)
221 nvl->nvl_parent = parent;
225 nvlist_empty(const nvlist_t *nvl)
229 PJDLOG_ASSERT(nvl->nvl_error == 0);
231 return (nvlist_first_nvpair(nvl) == NULL);
235 nvlist_report_missing(int type, const char *name)
238 PJDLOG_ABORT("Element '%s' of type %s doesn't exist.",
239 name, nvpair_type_string(type));
243 nvlist_find(const nvlist_t *nvl, int type, const char *name)
248 PJDLOG_ASSERT(nvl->nvl_error == 0);
249 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
250 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
252 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
253 nvp = nvlist_next_nvpair(nvl, nvp)) {
254 if (type != NV_TYPE_NONE && nvpair_type(nvp) != type)
256 if ((nvl->nvl_flags & NV_FLAG_IGNORE_CASE) != 0) {
257 if (strcasecmp(nvpair_name(nvp), name) != 0)
260 if (strcmp(nvpair_name(nvp), name) != 0)
267 RESTORE_ERRNO(ENOENT);
273 nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
277 PJDLOG_ASSERT(nvl->nvl_error == 0);
278 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
279 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
281 return (nvlist_find(nvl, type, name) != NULL);
285 nvlist_free_type(nvlist_t *nvl, const char *name, int type)
290 PJDLOG_ASSERT(nvl->nvl_error == 0);
291 PJDLOG_ASSERT(type == NV_TYPE_NONE ||
292 (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
294 nvp = nvlist_find(nvl, type, name);
296 nvlist_free_nvpair(nvl, nvp);
298 nvlist_report_missing(type, name);
302 nvlist_clone(const nvlist_t *nvl)
305 nvpair_t *nvp, *newnvp;
309 if (nvl->nvl_error != 0) {
310 RESTORE_ERRNO(nvl->nvl_error);
314 newnvl = nvlist_create(nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
315 for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
316 nvp = nvlist_next_nvpair(nvl, nvp)) {
317 newnvp = nvpair_clone(nvp);
320 nvlist_move_nvpair(newnvl, newnvp);
323 nvlist_destroy(newnvl);
331 nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
334 if (nvlist_error(nvl) != 0) {
335 dprintf(fd, "%*serror: %d\n", level * 4, "",
344 * Dump content of nvlist.
347 nvlist_dump(const nvlist_t *nvl, int fd)
349 const nvlist_t *tmpnvl;
350 nvpair_t *nvp, *tmpnvp;
355 if (nvlist_dump_error_check(nvl, fd, level))
358 nvp = nvlist_first_nvpair(nvl);
359 while (nvp != NULL) {
360 dprintf(fd, "%*s%s (%s):", level * 4, "", nvpair_name(nvp),
361 nvpair_type_string(nvpair_type(nvp)));
362 switch (nvpair_type(nvp)) {
364 dprintf(fd, " null\n");
367 dprintf(fd, " %s\n", nvpair_get_bool(nvp) ?
371 dprintf(fd, " %ju (%jd) (0x%jx)\n",
372 (uintmax_t)nvpair_get_number(nvp),
373 (intmax_t)nvpair_get_number(nvp),
374 (uintmax_t)nvpair_get_number(nvp));
377 dprintf(fd, " [%s]\n", nvpair_get_string(nvp));
381 tmpnvl = nvpair_get_nvlist(nvp);
382 if (nvlist_dump_error_check(tmpnvl, fd, level + 1))
384 tmpnvp = nvlist_first_nvpair(tmpnvl);
385 if (tmpnvp != NULL) {
392 case NV_TYPE_DESCRIPTOR:
393 dprintf(fd, " %d\n", nvpair_get_descriptor(nvp));
397 const unsigned char *binary;
401 binary = nvpair_get_binary(nvp, &size);
402 dprintf(fd, " %zu ", size);
403 for (ii = 0; ii < size; ii++)
404 dprintf(fd, "%02hhx", binary[ii]);
409 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
412 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
414 nvl = nvlist_get_parent(nvl, &cookie);
424 nvlist_fdump(const nvlist_t *nvl, FILE *fp)
428 nvlist_dump(nvl, fileno(fp));
433 * The function obtains size of the nvlist after nvlist_pack().
436 nvlist_size(const nvlist_t *nvl)
438 const nvlist_t *tmpnvl;
439 const nvpair_t *nvp, *tmpnvp;
444 PJDLOG_ASSERT(nvl->nvl_error == 0);
446 size = sizeof(struct nvlist_header);
447 nvp = nvlist_first_nvpair(nvl);
448 while (nvp != NULL) {
449 size += nvpair_header_size();
450 size += strlen(nvpair_name(nvp)) + 1;
451 if (nvpair_type(nvp) == NV_TYPE_NVLIST) {
452 size += sizeof(struct nvlist_header);
453 size += nvpair_header_size() + 1;
454 tmpnvl = nvpair_get_nvlist(nvp);
455 PJDLOG_ASSERT(tmpnvl->nvl_error == 0);
456 tmpnvp = nvlist_first_nvpair(tmpnvl);
457 if (tmpnvp != NULL) {
463 size += nvpair_size(nvp);
466 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
468 nvl = nvlist_get_parent(nvl, &cookie);
481 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
488 PJDLOG_ASSERT(nvl->nvl_error == 0);
492 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
494 case NV_TYPE_DESCRIPTOR:
495 *descs = nvpair_get_descriptor(nvp);
499 nvl = nvpair_get_nvlist(nvp);
504 } while ((nvl = nvlist_get_parent(nvl, (void**)&nvp)) != NULL);
512 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
517 nitems = nvlist_ndescriptors(nvl);
518 fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
522 nvlist_xdescriptors(nvl, fds);
531 nvlist_ndescriptors(const nvlist_t *nvl)
540 PJDLOG_ASSERT(nvl->nvl_error == 0);
545 while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) {
547 case NV_TYPE_DESCRIPTOR:
551 nvl = nvpair_get_nvlist(nvp);
556 } while ((nvl = nvlist_get_parent(nvl, (void**)&nvp)) != NULL);
564 static unsigned char *
565 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
567 struct nvlist_header nvlhdr;
571 nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
572 nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
573 nvlhdr.nvlh_flags = nvl->nvl_flags;
574 #if BYTE_ORDER == BIG_ENDIAN
575 nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
577 nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
578 nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
579 PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
580 memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
581 ptr += sizeof(nvlhdr);
582 *leftp -= sizeof(nvlhdr);
588 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
590 unsigned char *buf, *ptr;
592 const nvlist_t *tmpnvl;
593 nvpair_t *nvp, *tmpnvp;
598 if (nvl->nvl_error != 0) {
599 RESTORE_ERRNO(nvl->nvl_error);
603 size = nvlist_size(nvl);
604 buf = nv_malloc(size);
611 ptr = nvlist_pack_header(nvl, ptr, &left);
613 nvp = nvlist_first_nvpair(nvl);
614 while (nvp != NULL) {
617 nvpair_init_datasize(nvp);
618 ptr = nvpair_pack_header(nvp, ptr, &left);
623 switch (nvpair_type(nvp)) {
625 ptr = nvpair_pack_null(nvp, ptr, &left);
628 ptr = nvpair_pack_bool(nvp, ptr, &left);
631 ptr = nvpair_pack_number(nvp, ptr, &left);
634 ptr = nvpair_pack_string(nvp, ptr, &left);
637 tmpnvl = nvpair_get_nvlist(nvp);
638 ptr = nvlist_pack_header(tmpnvl, ptr, &left);
641 tmpnvp = nvlist_first_nvpair(tmpnvl);
642 if (tmpnvp != NULL) {
647 ptr = nvpair_pack_nvlist_up(ptr, &left);
650 case NV_TYPE_DESCRIPTOR:
651 ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
655 ptr = nvpair_pack_binary(nvp, ptr, &left);
658 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
664 while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
666 nvl = nvlist_get_parent(nvl, &cookie);
670 ptr = nvpair_pack_nvlist_up(ptr, &left);
683 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
688 if (nvl->nvl_error != 0) {
689 RESTORE_ERRNO(nvl->nvl_error);
693 if (nvlist_ndescriptors(nvl) > 0) {
694 RESTORE_ERRNO(EOPNOTSUPP);
698 return (nvlist_xpack(nvl, NULL, sizep));
702 nvlist_check_header(struct nvlist_header *nvlhdrp)
705 if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
706 RESTORE_ERRNO(EINVAL);
709 if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
710 RESTORE_ERRNO(EINVAL);
713 #if BYTE_ORDER == BIG_ENDIAN
714 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
715 nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
716 nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
719 if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
720 nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
721 nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
727 const unsigned char *
728 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
729 bool *isbep, size_t *leftp)
731 struct nvlist_header nvlhdr;
733 if (*leftp < sizeof(nvlhdr))
736 memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
738 if (!nvlist_check_header(&nvlhdr))
741 if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
745 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
747 if (nvlhdr.nvlh_descriptors > nfds)
750 if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
753 nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK);
755 ptr += sizeof(nvlhdr);
757 *isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
758 *leftp -= sizeof(nvlhdr);
762 RESTORE_ERRNO(EINVAL);
767 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds)
769 const unsigned char *ptr;
770 nvlist_t *nvl, *retnvl, *tmpnvl;
779 nvl = retnvl = nvlist_create(0);
783 ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
788 ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
791 switch (nvpair_type(nvp)) {
793 ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
796 ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
799 ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
802 ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
805 ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
807 nvlist_set_parent(tmpnvl, nvp);
810 case NV_TYPE_DESCRIPTOR:
811 ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
816 ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
818 case NV_TYPE_NVLIST_UP:
819 if (nvl->nvl_parent == NULL)
821 nvl = nvpair_nvlist(nvl->nvl_parent);
824 PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
828 nvlist_move_nvpair(nvl, nvp);
829 if (tmpnvl != NULL) {
837 nvlist_destroy(retnvl);
842 nvlist_unpack(const void *buf, size_t size)
845 return (nvlist_xunpack(buf, size, NULL, 0));
850 nvlist_send(int sock, const nvlist_t *nvl)
852 size_t datasize, nfds;
858 if (nvlist_error(nvl) != 0) {
859 errno = nvlist_error(nvl);
863 fds = nvlist_descriptors(nvl, &nfds);
871 data = nvlist_xpack(nvl, &fdidx, &datasize);
875 if (buf_send(sock, data, datasize) == -1)
879 if (fd_send(sock, fds, nfds) == -1)
893 nvlist_recv(int sock)
895 struct nvlist_header nvlhdr;
898 size_t nfds, size, i;
901 if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr)) == -1)
904 if (!nvlist_check_header(&nvlhdr))
907 nfds = (size_t)nvlhdr.nvlh_descriptors;
908 size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
910 buf = nv_malloc(size);
914 memcpy(buf, &nvlhdr, sizeof(nvlhdr));
919 if (buf_recv(sock, buf + sizeof(nvlhdr), size - sizeof(nvlhdr)) == -1)
923 fds = nv_malloc(nfds * sizeof(fds[0]));
926 if (fd_recv(sock, fds, nfds) == -1)
930 nvl = nvlist_xunpack(buf, size, fds, nfds);
932 for (i = 0; i < nfds; i++)
948 nvlist_xfer(int sock, nvlist_t *nvl)
951 if (nvlist_send(sock, nvl) < 0) {
956 return (nvlist_recv(sock));
961 nvlist_first_nvpair(const nvlist_t *nvl)
966 return (TAILQ_FIRST(&nvl->nvl_head));
970 nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
976 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
978 retnvp = nvpair_next(nvp);
979 PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
986 nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp)
992 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
994 retnvp = nvpair_prev(nvp);
995 PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1001 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1006 PJDLOG_ASSERT(cookiep != NULL);
1008 if (*cookiep == NULL)
1009 nvp = nvlist_first_nvpair(nvl);
1011 nvp = nvlist_next_nvpair(nvl, *cookiep);
1015 *typep = nvpair_type(nvp);
1017 return (nvpair_name(nvp));
1021 nvlist_exists(const nvlist_t *nvl, const char *name)
1024 return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1027 #define NVLIST_EXISTS(type, TYPE) \
1029 nvlist_exists_##type(const nvlist_t *nvl, const char *name) \
1032 return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL); \
1035 NVLIST_EXISTS(null, NULL)
1036 NVLIST_EXISTS(bool, BOOL)
1037 NVLIST_EXISTS(number, NUMBER)
1038 NVLIST_EXISTS(string, STRING)
1039 NVLIST_EXISTS(nvlist, NVLIST)
1041 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1043 NVLIST_EXISTS(binary, BINARY)
1045 #undef NVLIST_EXISTS
1048 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1054 if (nvlist_error(nvl) != 0) {
1055 RESTORE_ERRNO(nvlist_error(nvl));
1058 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1059 nvl->nvl_error = EEXIST;
1060 RESTORE_ERRNO(nvlist_error(nvl));
1064 newnvp = nvpair_clone(nvp);
1065 if (newnvp == NULL) {
1066 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1067 RESTORE_ERRNO(nvlist_error(nvl));
1071 nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1075 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1079 va_start(valueap, valuefmt);
1080 nvlist_add_stringv(nvl, name, valuefmt, valueap);
1085 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1090 if (nvlist_error(nvl) != 0) {
1091 RESTORE_ERRNO(nvlist_error(nvl));
1095 nvp = nvpair_create_stringv(name, valuefmt, valueap);
1097 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1098 RESTORE_ERRNO(nvl->nvl_error);
1100 nvlist_move_nvpair(nvl, nvp);
1105 nvlist_add_null(nvlist_t *nvl, const char *name)
1109 if (nvlist_error(nvl) != 0) {
1110 RESTORE_ERRNO(nvlist_error(nvl));
1114 nvp = nvpair_create_null(name);
1116 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1117 RESTORE_ERRNO(nvl->nvl_error);
1119 nvlist_move_nvpair(nvl, nvp);
1124 nvlist_add_bool(nvlist_t *nvl, const char *name, bool value)
1128 if (nvlist_error(nvl) != 0) {
1129 RESTORE_ERRNO(nvlist_error(nvl));
1133 nvp = nvpair_create_bool(name, value);
1135 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1136 RESTORE_ERRNO(nvl->nvl_error);
1138 nvlist_move_nvpair(nvl, nvp);
1143 nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value)
1147 if (nvlist_error(nvl) != 0) {
1148 RESTORE_ERRNO(nvlist_error(nvl));
1152 nvp = nvpair_create_number(name, value);
1154 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1155 RESTORE_ERRNO(nvl->nvl_error);
1157 nvlist_move_nvpair(nvl, nvp);
1162 nvlist_add_string(nvlist_t *nvl, const char *name, const char *value)
1166 if (nvlist_error(nvl) != 0) {
1167 RESTORE_ERRNO(nvlist_error(nvl));
1171 nvp = nvpair_create_string(name, value);
1173 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1174 RESTORE_ERRNO(nvl->nvl_error);
1176 nvlist_move_nvpair(nvl, nvp);
1181 nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value)
1185 if (nvlist_error(nvl) != 0) {
1186 RESTORE_ERRNO(nvlist_error(nvl));
1190 nvp = nvpair_create_nvlist(name, value);
1192 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1193 RESTORE_ERRNO(nvl->nvl_error);
1195 nvlist_move_nvpair(nvl, nvp);
1201 nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value)
1205 if (nvlist_error(nvl) != 0) {
1206 errno = nvlist_error(nvl);
1210 nvp = nvpair_create_descriptor(name, value);
1212 nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM);
1214 nvlist_move_nvpair(nvl, nvp);
1219 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1224 if (nvlist_error(nvl) != 0) {
1225 RESTORE_ERRNO(nvlist_error(nvl));
1229 nvp = nvpair_create_binary(name, value, size);
1231 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1232 RESTORE_ERRNO(nvl->nvl_error);
1234 nvlist_move_nvpair(nvl, nvp);
1239 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1243 PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1245 if (nvlist_error(nvl) != 0) {
1247 RESTORE_ERRNO(nvlist_error(nvl));
1250 if (nvlist_exists(nvl, nvpair_name(nvp))) {
1252 nvl->nvl_error = EEXIST;
1253 RESTORE_ERRNO(nvl->nvl_error);
1257 nvpair_insert(&nvl->nvl_head, nvp, nvl);
1261 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1265 if (nvlist_error(nvl) != 0) {
1267 RESTORE_ERRNO(nvlist_error(nvl));
1271 nvp = nvpair_move_string(name, value);
1273 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1274 RESTORE_ERRNO(nvl->nvl_error);
1276 nvlist_move_nvpair(nvl, nvp);
1281 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1285 if (nvlist_error(nvl) != 0) {
1286 if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1287 nvlist_destroy(value);
1288 RESTORE_ERRNO(nvlist_error(nvl));
1292 nvp = nvpair_move_nvlist(name, value);
1294 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1295 RESTORE_ERRNO(nvl->nvl_error);
1297 nvlist_move_nvpair(nvl, nvp);
1303 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1307 if (nvlist_error(nvl) != 0) {
1309 errno = nvlist_error(nvl);
1313 nvp = nvpair_move_descriptor(name, value);
1315 nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM);
1317 nvlist_move_nvpair(nvl, nvp);
1322 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1326 if (nvlist_error(nvl) != 0) {
1328 RESTORE_ERRNO(nvlist_error(nvl));
1332 nvp = nvpair_move_binary(name, value, size);
1334 nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1335 RESTORE_ERRNO(nvl->nvl_error);
1337 nvlist_move_nvpair(nvl, nvp);
1342 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1345 return (nvlist_find(nvl, NV_TYPE_NONE, name));
1348 #define NVLIST_GET(ftype, type, TYPE) \
1350 nvlist_get_##type(const nvlist_t *nvl, const char *name) \
1352 const nvpair_t *nvp; \
1354 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1356 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1357 return (nvpair_get_##type(nvp)); \
1360 NVLIST_GET(bool, bool, BOOL)
1361 NVLIST_GET(uint64_t, number, NUMBER)
1362 NVLIST_GET(const char *, string, STRING)
1363 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1365 NVLIST_GET(int, descriptor, DESCRIPTOR)
1371 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1375 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1377 nvlist_report_missing(NV_TYPE_BINARY, name);
1379 return (nvpair_get_binary(nvp, sizep));
1382 #define NVLIST_TAKE(ftype, type, TYPE) \
1384 nvlist_take_##type(nvlist_t *nvl, const char *name) \
1389 nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name); \
1391 nvlist_report_missing(NV_TYPE_##TYPE, name); \
1392 value = (ftype)(intptr_t)nvpair_get_##type(nvp); \
1393 nvlist_remove_nvpair(nvl, nvp); \
1394 nvpair_free_structure(nvp); \
1398 NVLIST_TAKE(bool, bool, BOOL)
1399 NVLIST_TAKE(uint64_t, number, NUMBER)
1400 NVLIST_TAKE(char *, string, STRING)
1401 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1403 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1409 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1414 nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1416 nvlist_report_missing(NV_TYPE_BINARY, name);
1418 value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1419 nvlist_remove_nvpair(nvl, nvp);
1420 nvpair_free_structure(nvp);
1425 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1430 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1432 nvpair_remove(&nvl->nvl_head, nvp, nvl);
1436 nvlist_free(nvlist_t *nvl, const char *name)
1439 nvlist_free_type(nvl, name, NV_TYPE_NONE);
1442 #define NVLIST_FREE(type, TYPE) \
1444 nvlist_free_##type(nvlist_t *nvl, const char *name) \
1447 nvlist_free_type(nvl, name, NV_TYPE_##TYPE); \
1450 NVLIST_FREE(null, NULL)
1451 NVLIST_FREE(bool, BOOL)
1452 NVLIST_FREE(number, NUMBER)
1453 NVLIST_FREE(string, STRING)
1454 NVLIST_FREE(nvlist, NVLIST)
1456 NVLIST_FREE(descriptor, DESCRIPTOR)
1458 NVLIST_FREE(binary, BINARY)
1463 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1468 PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1470 nvlist_remove_nvpair(nvl, nvp);