2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2013 The FreeBSD Foundation
5 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/queue.h>
42 #include <sys/errno.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
47 #include <machine/stdarg.h>
59 #include "common_impl.h"
69 #include "nvlist_impl.h"
70 #include "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(...) abort()
85 #define NVPAIR_MAGIC 0x6e7670 /* "nvp" */
92 size_t nvp_nitems; /* Used only for array types. */
94 TAILQ_ENTRY(nvpair) nvp_next;
97 #define NVPAIR_ASSERT(nvp) do { \
98 PJDLOG_ASSERT((nvp) != NULL); \
99 PJDLOG_ASSERT((nvp)->nvp_magic == NVPAIR_MAGIC); \
102 struct nvpair_header {
104 uint16_t nvph_namesize;
105 uint64_t nvph_datasize;
106 uint64_t nvph_nitems;
111 nvpair_assert(const nvpair_t *nvp)
118 nvpair_allocv(const char *name, int type, uint64_t data, size_t datasize,
124 PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
126 namelen = strlen(name);
127 if (namelen >= NV_NAME_MAX) {
128 ERRNO_SET(ENAMETOOLONG);
132 nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
134 nvp->nvp_name = (char *)(nvp + 1);
135 memcpy(nvp->nvp_name, name, namelen);
136 nvp->nvp_name[namelen] = '\0';
137 nvp->nvp_type = type;
138 nvp->nvp_data = data;
139 nvp->nvp_datasize = datasize;
140 nvp->nvp_nitems = nitems;
141 nvp->nvp_magic = NVPAIR_MAGIC;
148 nvpair_nvlist(const nvpair_t *nvp)
153 return (nvp->nvp_list);
157 nvpair_next(const nvpair_t *nvp)
161 PJDLOG_ASSERT(nvp->nvp_list != NULL);
163 return (TAILQ_NEXT(nvp, nvp_next));
167 nvpair_prev(const nvpair_t *nvp)
171 PJDLOG_ASSERT(nvp->nvp_list != NULL);
173 return (TAILQ_PREV(nvp, nvl_head, nvp_next));
177 nvpair_insert(struct nvl_head *head, nvpair_t *nvp, nvlist_t *nvl)
181 PJDLOG_ASSERT(nvp->nvp_list == NULL);
182 PJDLOG_ASSERT((nvlist_flags(nvl) & NV_FLAG_NO_UNIQUE) != 0 ||
183 !nvlist_exists(nvl, nvpair_name(nvp)));
185 TAILQ_INSERT_TAIL(head, nvp, nvp_next);
190 nvpair_remove_nvlist(nvpair_t *nvp)
194 /* XXX: DECONST is bad, mkay? */
195 nvl = __DECONST(nvlist_t *, nvpair_get_nvlist(nvp));
196 PJDLOG_ASSERT(nvl != NULL);
197 nvlist_set_parent(nvl, NULL);
201 nvpair_remove_nvlist_array(nvpair_t *nvp)
206 /* XXX: DECONST is bad, mkay? */
207 nvlarray = __DECONST(nvlist_t **,
208 nvpair_get_nvlist_array(nvp, &count));
209 for (i = 0; i < count; i++)
210 nvlist_set_array_next(nvlarray[i], NULL);
214 nvpair_remove(struct nvl_head *head, nvpair_t *nvp, const nvlist_t *nvl)
218 PJDLOG_ASSERT(nvp->nvp_list == nvl);
220 if (nvpair_type(nvp) == NV_TYPE_NVLIST)
221 nvpair_remove_nvlist(nvp);
222 else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY)
223 nvpair_remove_nvlist_array(nvp);
225 TAILQ_REMOVE(head, nvp, nvp_next);
226 nvp->nvp_list = NULL;
230 nvpair_clone(const nvpair_t *nvp)
239 name = nvpair_name(nvp);
241 switch (nvpair_type(nvp)) {
243 newnvp = nvpair_create_null(name);
246 newnvp = nvpair_create_bool(name, nvpair_get_bool(nvp));
249 newnvp = nvpair_create_number(name, nvpair_get_number(nvp));
252 newnvp = nvpair_create_string(name, nvpair_get_string(nvp));
255 newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
258 data = nvpair_get_binary(nvp, &datasize);
259 newnvp = nvpair_create_binary(name, data, datasize);
261 case NV_TYPE_BOOL_ARRAY:
262 data = nvpair_get_bool_array(nvp, &datasize);
263 newnvp = nvpair_create_bool_array(name, data, datasize);
265 case NV_TYPE_NUMBER_ARRAY:
266 data = nvpair_get_number_array(nvp, &datasize);
267 newnvp = nvpair_create_number_array(name, data, datasize);
269 case NV_TYPE_STRING_ARRAY:
270 data = nvpair_get_string_array(nvp, &datasize);
271 newnvp = nvpair_create_string_array(name, data, datasize);
273 case NV_TYPE_NVLIST_ARRAY:
274 data = nvpair_get_nvlist_array(nvp, &datasize);
275 newnvp = nvpair_create_nvlist_array(name, data, datasize);
278 case NV_TYPE_DESCRIPTOR:
279 newnvp = nvpair_create_descriptor(name,
280 nvpair_get_descriptor(nvp));
282 case NV_TYPE_DESCRIPTOR_ARRAY:
283 data = nvpair_get_descriptor_array(nvp, &datasize);
284 newnvp = nvpair_create_descriptor_array(name, data, datasize);
288 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
295 nvpair_header_size(void)
298 return (sizeof(struct nvpair_header));
302 nvpair_size(const nvpair_t *nvp)
307 return (nvp->nvp_datasize);
311 nvpair_pack_header(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
313 struct nvpair_header nvphdr;
318 nvphdr.nvph_type = nvp->nvp_type;
319 namesize = strlen(nvp->nvp_name) + 1;
320 PJDLOG_ASSERT(namesize > 0 && namesize <= UINT16_MAX);
321 nvphdr.nvph_namesize = namesize;
322 nvphdr.nvph_datasize = nvp->nvp_datasize;
323 nvphdr.nvph_nitems = nvp->nvp_nitems;
324 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
325 memcpy(ptr, &nvphdr, sizeof(nvphdr));
326 ptr += sizeof(nvphdr);
327 *leftp -= sizeof(nvphdr);
329 PJDLOG_ASSERT(*leftp >= namesize);
330 memcpy(ptr, nvp->nvp_name, namesize);
338 nvpair_pack_null(const nvpair_t *nvp, unsigned char *ptr,
339 size_t *leftp __unused)
343 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
349 nvpair_pack_bool(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
354 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
356 value = (uint8_t)nvp->nvp_data;
358 PJDLOG_ASSERT(*leftp >= sizeof(value));
359 memcpy(ptr, &value, sizeof(value));
360 ptr += sizeof(value);
361 *leftp -= sizeof(value);
367 nvpair_pack_number(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
372 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
374 value = (uint64_t)nvp->nvp_data;
376 PJDLOG_ASSERT(*leftp >= sizeof(value));
377 memcpy(ptr, &value, sizeof(value));
378 ptr += sizeof(value);
379 *leftp -= sizeof(value);
385 nvpair_pack_string(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
389 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
391 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
392 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
393 ptr += nvp->nvp_datasize;
394 *leftp -= nvp->nvp_datasize;
400 nvpair_pack_nvlist_up(unsigned char *ptr, size_t *leftp)
402 struct nvpair_header nvphdr;
404 const char *name = "";
407 nvphdr.nvph_type = NV_TYPE_NVLIST_UP;
408 nvphdr.nvph_namesize = namesize;
409 nvphdr.nvph_datasize = 0;
410 nvphdr.nvph_nitems = 0;
411 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
412 memcpy(ptr, &nvphdr, sizeof(nvphdr));
413 ptr += sizeof(nvphdr);
414 *leftp -= sizeof(nvphdr);
416 PJDLOG_ASSERT(*leftp >= namesize);
417 memcpy(ptr, name, namesize);
425 nvpair_pack_nvlist_array_next(unsigned char *ptr, size_t *leftp)
427 struct nvpair_header nvphdr;
429 const char *name = "";
432 nvphdr.nvph_type = NV_TYPE_NVLIST_ARRAY_NEXT;
433 nvphdr.nvph_namesize = namesize;
434 nvphdr.nvph_datasize = 0;
435 nvphdr.nvph_nitems = 0;
436 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
437 memcpy(ptr, &nvphdr, sizeof(nvphdr));
438 ptr += sizeof(nvphdr);
439 *leftp -= sizeof(nvphdr);
441 PJDLOG_ASSERT(*leftp >= namesize);
442 memcpy(ptr, name, namesize);
451 nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
457 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
459 value = (int64_t)nvp->nvp_data;
462 * If there is a real descriptor here, we change its number
463 * to position in the array of descriptors send via control
466 PJDLOG_ASSERT(fdidxp != NULL);
472 PJDLOG_ASSERT(*leftp >= sizeof(value));
473 memcpy(ptr, &value, sizeof(value));
474 ptr += sizeof(value);
475 *leftp -= sizeof(value);
482 nvpair_pack_binary(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
486 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
488 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
489 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
490 ptr += nvp->nvp_datasize;
491 *leftp -= nvp->nvp_datasize;
497 nvpair_pack_bool_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
501 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
502 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
504 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
505 ptr += nvp->nvp_datasize;
506 *leftp -= nvp->nvp_datasize;
512 nvpair_pack_number_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
516 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
517 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
519 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
520 ptr += nvp->nvp_datasize;
521 *leftp -= nvp->nvp_datasize;
527 nvpair_pack_string_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
531 const char * const *array;
534 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
535 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
538 array = nvpair_get_string_array(nvp, NULL);
539 PJDLOG_ASSERT(array != NULL);
541 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
542 len = strlen(array[ii]) + 1;
543 PJDLOG_ASSERT(*leftp >= len);
545 memcpy(ptr, (const void *)array[ii], len);
551 PJDLOG_ASSERT(size == nvp->nvp_datasize);
558 nvpair_pack_descriptor_array(const nvpair_t *nvp, unsigned char *ptr,
559 int64_t *fdidxp, size_t *leftp)
566 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
567 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
569 array = nvpair_get_descriptor_array(nvp, NULL);
570 PJDLOG_ASSERT(array != NULL);
572 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
573 PJDLOG_ASSERT(*leftp >= sizeof(value));
578 * If there is a real descriptor here, we change its
579 * number to position in the array of descriptors send
580 * via control message.
582 PJDLOG_ASSERT(fdidxp != NULL);
587 memcpy(ptr, &value, sizeof(value));
588 ptr += sizeof(value);
589 *leftp -= sizeof(value);
597 nvpair_init_datasize(nvpair_t *nvp)
602 if (nvp->nvp_type == NV_TYPE_NVLIST) {
603 if (nvp->nvp_data == 0) {
604 nvp->nvp_datasize = 0;
607 nvlist_size((const nvlist_t *)(intptr_t)nvp->nvp_data);
612 const unsigned char *
613 nvpair_unpack_header(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
616 struct nvpair_header nvphdr;
618 if (*leftp < sizeof(nvphdr))
621 memcpy(&nvphdr, ptr, sizeof(nvphdr));
622 ptr += sizeof(nvphdr);
623 *leftp -= sizeof(nvphdr);
625 #if NV_TYPE_FIRST > 0
626 if (nvphdr.nvph_type < NV_TYPE_FIRST)
629 if (nvphdr.nvph_type > NV_TYPE_LAST &&
630 nvphdr.nvph_type != NV_TYPE_NVLIST_UP &&
631 nvphdr.nvph_type != NV_TYPE_NVLIST_ARRAY_NEXT) {
635 #if BYTE_ORDER == BIG_ENDIAN
637 nvphdr.nvph_namesize = le16toh(nvphdr.nvph_namesize);
638 nvphdr.nvph_datasize = le64toh(nvphdr.nvph_datasize);
642 nvphdr.nvph_namesize = be16toh(nvphdr.nvph_namesize);
643 nvphdr.nvph_datasize = be64toh(nvphdr.nvph_datasize);
647 if (nvphdr.nvph_namesize > NV_NAME_MAX)
649 if (*leftp < nvphdr.nvph_namesize)
651 if (nvphdr.nvph_namesize < 1)
653 if (strnlen((const char *)ptr, nvphdr.nvph_namesize) !=
654 (size_t)(nvphdr.nvph_namesize - 1)) {
658 memcpy(nvp->nvp_name, ptr, nvphdr.nvph_namesize);
659 ptr += nvphdr.nvph_namesize;
660 *leftp -= nvphdr.nvph_namesize;
662 if (*leftp < nvphdr.nvph_datasize)
665 nvp->nvp_type = nvphdr.nvph_type;
667 nvp->nvp_datasize = nvphdr.nvph_datasize;
668 nvp->nvp_nitems = nvphdr.nvph_nitems;
676 const unsigned char *
677 nvpair_unpack_null(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
678 size_t *leftp __unused)
681 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
683 if (nvp->nvp_datasize != 0) {
691 const unsigned char *
692 nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
697 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
699 if (nvp->nvp_datasize != sizeof(value)) {
703 if (*leftp < sizeof(value)) {
708 memcpy(&value, ptr, sizeof(value));
709 ptr += sizeof(value);
710 *leftp -= sizeof(value);
712 if (value != 0 && value != 1) {
717 nvp->nvp_data = (uint64_t)value;
722 const unsigned char *
723 nvpair_unpack_number(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
727 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
729 if (nvp->nvp_datasize != sizeof(uint64_t)) {
733 if (*leftp < sizeof(uint64_t)) {
739 nvp->nvp_data = be64dec(ptr);
741 nvp->nvp_data = le64dec(ptr);
743 ptr += sizeof(uint64_t);
744 *leftp -= sizeof(uint64_t);
749 const unsigned char *
750 nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,
751 const unsigned char *ptr, size_t *leftp)
754 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
756 if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
761 if (strnlen((const char *)ptr, nvp->nvp_datasize) !=
762 nvp->nvp_datasize - 1) {
767 nvp->nvp_data = (uint64_t)(uintptr_t)nv_strdup((const char *)ptr);
768 if (nvp->nvp_data == 0)
771 ptr += nvp->nvp_datasize;
772 *leftp -= nvp->nvp_datasize;
777 const unsigned char *
778 nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp,
779 const unsigned char *ptr, size_t *leftp, size_t nfds, nvlist_t **child)
783 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
785 if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
790 value = nvlist_create(0);
794 ptr = nvlist_unpack_header(value, ptr, nfds, NULL, leftp);
798 nvp->nvp_data = (uint64_t)(uintptr_t)value;
805 const unsigned char *
806 nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
807 size_t *leftp, const int *fds, size_t nfds)
811 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
813 if (nvp->nvp_datasize != sizeof(idx)) {
817 if (*leftp < sizeof(idx)) {
832 if ((size_t)idx >= nfds) {
837 nvp->nvp_data = (uint64_t)fds[idx];
840 *leftp -= sizeof(idx);
846 const unsigned char *
847 nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp,
848 const unsigned char *ptr, size_t *leftp)
852 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
854 if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
859 value = nv_malloc(nvp->nvp_datasize);
863 memcpy(value, ptr, nvp->nvp_datasize);
864 ptr += nvp->nvp_datasize;
865 *leftp -= nvp->nvp_datasize;
867 nvp->nvp_data = (uint64_t)(uintptr_t)value;
872 const unsigned char *
873 nvpair_unpack_bool_array(bool isbe __unused, nvpair_t *nvp,
874 const unsigned char *ptr, size_t *leftp)
880 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
882 size = sizeof(*value) * nvp->nvp_nitems;
883 if (nvp->nvp_datasize != size || *leftp < size ||
884 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
889 value = nv_malloc(size);
893 for (i = 0; i < nvp->nvp_nitems; i++) {
894 value[i] = *(const uint8_t *)ptr;
896 ptr += sizeof(*value);
897 *leftp -= sizeof(*value);
900 nvp->nvp_data = (uint64_t)(uintptr_t)value;
905 const unsigned char *
906 nvpair_unpack_number_array(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
913 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
915 size = sizeof(*value) * nvp->nvp_nitems;
916 if (nvp->nvp_datasize != size || *leftp < size ||
917 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
922 value = nv_malloc(size);
926 for (i = 0; i < nvp->nvp_nitems; i++) {
928 value[i] = be64dec(ptr);
930 value[i] = le64dec(ptr);
932 ptr += sizeof(*value);
933 *leftp -= sizeof(*value);
936 nvp->nvp_data = (uint64_t)(uintptr_t)value;
941 const unsigned char *
942 nvpair_unpack_string_array(bool isbe __unused, nvpair_t *nvp,
943 const unsigned char *ptr, size_t *leftp)
951 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
953 if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0 ||
954 nvp->nvp_nitems == 0) {
959 size = nvp->nvp_datasize;
960 tmp = (const char *)ptr;
961 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
962 len = strnlen(tmp, size - 1) + 1;
975 value = nv_malloc(sizeof(*value) * nvp->nvp_nitems);
979 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
980 value[ii] = nv_strdup((const char *)ptr);
981 if (value[ii] == NULL)
983 len = strlen(value[ii]) + 1;
987 nvp->nvp_data = (uint64_t)(uintptr_t)value;
991 for (j = 0; j < ii; j++)
998 const unsigned char *
999 nvpair_unpack_descriptor_array(bool isbe, nvpair_t *nvp,
1000 const unsigned char *ptr, size_t *leftp, const int *fds, size_t nfds)
1007 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1009 size = sizeof(idx) * nvp->nvp_nitems;
1010 if (nvp->nvp_datasize != size || *leftp < size ||
1011 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
1016 array = (int *)nv_malloc(size);
1020 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1032 if ((size_t)idx >= nfds) {
1038 array[ii] = (uint64_t)fds[idx];
1041 *leftp -= sizeof(idx);
1044 nvp->nvp_data = (uint64_t)(uintptr_t)array;
1050 const unsigned char *
1051 nvpair_unpack_nvlist_array(bool isbe __unused, nvpair_t *nvp,
1052 const unsigned char *ptr, size_t *leftp, nvlist_t **firstel)
1059 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1061 sizeup = sizeof(struct nvpair_header) * nvp->nvp_nitems;
1062 if (nvp->nvp_nitems == 0 || sizeup < nvp->nvp_nitems ||
1068 value = nv_malloc(nvp->nvp_nitems * sizeof(*value));
1072 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1073 value[ii] = nvlist_create(0);
1074 if (value[ii] == NULL)
1077 tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1078 (uint64_t)(uintptr_t)value[ii], 0, 0);
1081 nvlist_set_array_next(value[ii - 1], tmpnvp);
1084 nvlist_set_flags(value[nvp->nvp_nitems - 1], NV_FLAG_IN_ARRAY);
1086 nvp->nvp_data = (uint64_t)(uintptr_t)value;
1087 *firstel = value[0];
1092 for (j = 0; j <= ii; j++)
1093 nvlist_destroy(value[j]);
1100 const unsigned char *
1101 nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
1104 nvpair_t *nvp, *tmp;
1106 nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
1109 nvp->nvp_name = (char *)(nvp + 1);
1111 ptr = nvpair_unpack_header(isbe, nvp, ptr, leftp);
1114 tmp = nv_realloc(nvp, sizeof(*nvp) + strlen(nvp->nvp_name) + 1);
1119 /* Update nvp_name after realloc(). */
1120 nvp->nvp_name = (char *)(nvp + 1);
1121 nvp->nvp_data = 0x00;
1122 nvp->nvp_magic = NVPAIR_MAGIC;
1131 nvpair_type(const nvpair_t *nvp)
1136 return (nvp->nvp_type);
1140 nvpair_name(const nvpair_t *nvp)
1145 return (nvp->nvp_name);
1149 nvpair_create_stringf(const char *name, const char *valuefmt, ...)
1154 va_start(valueap, valuefmt);
1155 nvp = nvpair_create_stringv(name, valuefmt, valueap);
1162 nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap)
1168 len = nv_vasprintf(&str, valuefmt, valueap);
1171 nvp = nvpair_create_string(name, str);
1178 nvpair_create_null(const char *name)
1181 return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0, 0));
1185 nvpair_create_bool(const char *name, bool value)
1188 return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
1189 sizeof(uint8_t), 0));
1193 nvpair_create_number(const char *name, uint64_t value)
1196 return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value), 0));
1200 nvpair_create_string(const char *name, const char *value)
1206 if (value == NULL) {
1211 data = nv_strdup(value);
1214 size = strlen(value) + 1;
1216 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
1225 nvpair_create_nvlist(const char *name, const nvlist_t *value)
1230 if (value == NULL) {
1235 nvl = nvlist_clone(value);
1239 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0,
1242 nvlist_destroy(nvl);
1244 nvlist_set_parent(nvl, nvp);
1251 nvpair_create_descriptor(const char *name, int value)
1255 if (value < 0 || !fd_is_valid(value)) {
1260 value = fcntl(value, F_DUPFD_CLOEXEC, 0);
1264 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1265 sizeof(int64_t), 0);
1277 nvpair_create_binary(const char *name, const void *value, size_t size)
1282 if (value == NULL || size == 0) {
1287 data = nv_malloc(size);
1290 memcpy(data, value, size);
1292 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
1301 nvpair_create_bool_array(const char *name, const bool *value, size_t nitems)
1307 if (value == NULL || nitems == 0) {
1312 size = sizeof(value[0]) * nitems;
1313 data = nv_malloc(size);
1317 memcpy(data, value, size);
1318 nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data,
1330 nvpair_create_number_array(const char *name, const uint64_t *value,
1337 if (value == NULL || nitems == 0) {
1342 size = sizeof(value[0]) * nitems;
1343 data = nv_malloc(size);
1347 memcpy(data, value, size);
1348 nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1349 (uint64_t)(uintptr_t)data, size, nitems);
1360 nvpair_create_string_array(const char *name, const char * const *value,
1365 size_t datasize, size;
1368 if (value == NULL || nitems == 0) {
1375 data = nv_malloc(sizeof(value[0]) * nitems);
1379 for (ii = 0; ii < nitems; ii++) {
1380 if (value[ii] == NULL) {
1385 size = strlen(value[ii]) + 1;
1387 data[ii] = nv_strdup(value[ii]);
1388 if (data[ii] == NULL)
1391 nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1392 (uint64_t)(uintptr_t)data, datasize, nitems);
1397 for (; ii > 0; ii--)
1398 nv_free(data[ii - 1]);
1407 nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value,
1417 if (value == NULL || nitems == 0) {
1422 nvls = nv_malloc(sizeof(value[0]) * nitems);
1426 for (ii = 0; ii < nitems; ii++) {
1427 if (value[ii] == NULL) {
1432 nvls[ii] = nvlist_clone(value[ii]);
1433 if (nvls[ii] == NULL)
1439 nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1440 (uint64_t)(uintptr_t)nvls[ii], 0, 0);
1443 nvlist_destroy(nvls[ii]);
1447 nvlist_set_array_next(nvls[ii - 1], nvp);
1450 flags = nvlist_flags(nvls[nitems - 1]) | NV_FLAG_IN_ARRAY;
1451 nvlist_set_flags(nvls[nitems - 1], flags);
1453 parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1454 (uint64_t)(uintptr_t)nvls, 0, nitems);
1458 for (ii = 0; ii < nitems; ii++)
1459 nvlist_set_parent(nvls[ii], parent);
1465 for (; ii > 0; ii--)
1466 nvlist_destroy(nvls[ii - 1]);
1475 nvpair_create_descriptor_array(const char *name, const int *value,
1482 if (value == NULL) {
1489 fds = nv_malloc(sizeof(value[0]) * nitems);
1492 for (ii = 0; ii < nitems; ii++) {
1493 if (value[ii] == -1) {
1496 if (!fd_is_valid(value[ii])) {
1501 fds[ii] = fcntl(value[ii], F_DUPFD_CLOEXEC, 0);
1507 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1508 (uint64_t)(uintptr_t)fds, sizeof(int64_t) * nitems, nitems);
1513 for (; ii > 0; ii--) {
1514 if (fds[ii - 1] != -1)
1526 nvpair_move_string(const char *name, char *value)
1530 if (value == NULL) {
1535 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
1536 strlen(value) + 1, 0);
1547 nvpair_move_nvlist(const char *name, nvlist_t *value)
1551 if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) {
1556 if (nvlist_error(value) != 0) {
1557 ERRNO_SET(nvlist_error(value));
1558 nvlist_destroy(value);
1562 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
1565 nvlist_destroy(value);
1567 nvlist_set_parent(value, nvp);
1574 nvpair_move_descriptor(const char *name, int value)
1578 if (value < 0 || !fd_is_valid(value)) {
1583 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1584 sizeof(int64_t), 0);
1596 nvpair_move_binary(const char *name, void *value, size_t size)
1600 if (value == NULL || size == 0) {
1605 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
1617 nvpair_move_bool_array(const char *name, bool *value, size_t nitems)
1621 if (value == NULL || nitems == 0) {
1626 nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY,
1627 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1638 nvpair_move_string_array(const char *name, char **value, size_t nitems)
1643 if (value == NULL || nitems == 0) {
1649 for (i = 0; i < nitems; i++) {
1650 if (value[i] == NULL) {
1655 size += strlen(value[i]) + 1;
1658 nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1659 (uint64_t)(uintptr_t)value, size, nitems);
1662 for (i = 0; i < nitems; i++)
1672 nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems)
1676 if (value == NULL || nitems == 0) {
1681 nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1682 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1693 nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems)
1699 if (value == NULL || nitems == 0) {
1704 for (ii = 0; ii < nitems; ii++) {
1705 if (value == NULL || nvlist_error(value[ii]) != 0 ||
1706 nvlist_get_pararr(value[ii], NULL) != NULL) {
1713 nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1714 (uint64_t)(uintptr_t)value[ii], 0, 0);
1717 nvlist_set_array_next(value[ii - 1], nvp);
1720 flags = nvlist_flags(value[nitems - 1]) | NV_FLAG_IN_ARRAY;
1721 nvlist_set_flags(value[nitems - 1], flags);
1723 parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1724 (uint64_t)(uintptr_t)value, 0, nitems);
1728 for (ii = 0; ii < nitems; ii++)
1729 nvlist_set_parent(value[ii], parent);
1734 for (ii = 0; ii < nitems; ii++) {
1735 if (value[ii] != NULL &&
1736 nvlist_get_pararr(value[ii], NULL) != NULL) {
1737 nvlist_destroy(value[ii]);
1748 nvpair_move_descriptor_array(const char *name, int *value, size_t nitems)
1753 if (value == NULL || nitems == 0) {
1758 for (i = 0; i < nitems; i++) {
1759 if (value[i] != -1 && !fd_is_valid(value[i])) {
1765 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1766 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1773 for (i = 0; i < nitems; i++) {
1774 if (fd_is_valid(value[i]))
1785 nvpair_get_bool(const nvpair_t *nvp)
1790 return (nvp->nvp_data == 1);
1794 nvpair_get_number(const nvpair_t *nvp)
1799 return (nvp->nvp_data);
1803 nvpair_get_string(const nvpair_t *nvp)
1807 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
1809 return ((const char *)(intptr_t)nvp->nvp_data);
1813 nvpair_get_nvlist(const nvpair_t *nvp)
1817 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
1819 return ((const nvlist_t *)(intptr_t)nvp->nvp_data);
1824 nvpair_get_descriptor(const nvpair_t *nvp)
1828 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
1830 return ((int)nvp->nvp_data);
1835 nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
1839 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
1842 *sizep = nvp->nvp_datasize;
1844 return ((const void *)(intptr_t)nvp->nvp_data);
1848 nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitems)
1852 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1855 *nitems = nvp->nvp_nitems;
1857 return ((const bool *)(intptr_t)nvp->nvp_data);
1861 nvpair_get_number_array(const nvpair_t *nvp, size_t *nitems)
1865 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1868 *nitems = nvp->nvp_nitems;
1870 return ((const uint64_t *)(intptr_t)nvp->nvp_data);
1873 const char * const *
1874 nvpair_get_string_array(const nvpair_t *nvp, size_t *nitems)
1878 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1881 *nitems = nvp->nvp_nitems;
1883 return ((const char * const *)(intptr_t)nvp->nvp_data);
1886 const nvlist_t * const *
1887 nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitems)
1891 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1894 *nitems = nvp->nvp_nitems;
1896 return ((const nvlist_t * const *)((intptr_t)nvp->nvp_data));
1901 nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitems)
1905 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1908 *nitems = nvp->nvp_nitems;
1910 return ((const int *)(intptr_t)nvp->nvp_data);
1915 nvpair_free(nvpair_t *nvp)
1920 PJDLOG_ASSERT(nvp->nvp_list == NULL);
1923 switch (nvp->nvp_type) {
1925 case NV_TYPE_DESCRIPTOR:
1926 close((int)nvp->nvp_data);
1928 case NV_TYPE_DESCRIPTOR_ARRAY:
1929 for (i = 0; i < nvp->nvp_nitems; i++)
1930 close(((int *)(intptr_t)nvp->nvp_data)[i]);
1933 case NV_TYPE_NVLIST:
1934 nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
1936 case NV_TYPE_STRING:
1937 nv_free((char *)(intptr_t)nvp->nvp_data);
1939 case NV_TYPE_BINARY:
1940 nv_free((void *)(intptr_t)nvp->nvp_data);
1942 case NV_TYPE_NVLIST_ARRAY:
1943 for (i = 0; i < nvp->nvp_nitems; i++) {
1945 ((nvlist_t **)(intptr_t)nvp->nvp_data)[i]);
1947 nv_free(((nvlist_t **)(intptr_t)nvp->nvp_data));
1949 case NV_TYPE_NUMBER_ARRAY:
1950 nv_free((uint64_t *)(intptr_t)nvp->nvp_data);
1952 case NV_TYPE_BOOL_ARRAY:
1953 nv_free((bool *)(intptr_t)nvp->nvp_data);
1955 case NV_TYPE_STRING_ARRAY:
1956 for (i = 0; i < nvp->nvp_nitems; i++)
1957 nv_free(((char **)(intptr_t)nvp->nvp_data)[i]);
1958 nv_free((char **)(intptr_t)nvp->nvp_data);
1965 nvpair_free_structure(nvpair_t *nvp)
1969 PJDLOG_ASSERT(nvp->nvp_list == NULL);
1976 nvpair_type_string(int type)
1984 case NV_TYPE_NUMBER:
1986 case NV_TYPE_STRING:
1988 case NV_TYPE_NVLIST:
1990 case NV_TYPE_DESCRIPTOR:
1991 return ("DESCRIPTOR");
1992 case NV_TYPE_BINARY:
1994 case NV_TYPE_BOOL_ARRAY:
1995 return ("BOOL ARRAY");
1996 case NV_TYPE_NUMBER_ARRAY:
1997 return ("NUMBER ARRAY");
1998 case NV_TYPE_STRING_ARRAY:
1999 return ("STRING ARRAY");
2000 case NV_TYPE_NVLIST_ARRAY:
2001 return ("NVLIST ARRAY");
2002 case NV_TYPE_DESCRIPTOR_ARRAY:
2003 return ("DESCRIPTOR ARRAY");
2005 return ("<UNKNOWN>");