xref: /freebsd-14.2/sys/contrib/libnv/nvlist.c (revision 056c50c4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2013 The FreeBSD Foundation
5  * Copyright (c) 2013-2015 Mariusz Zaborski <[email protected]>
6  * All rights reserved.
7  *
8  * This software was developed by Pawel Jakub Dawidek under sponsorship from
9  * the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/queue.h>
39 
40 #ifdef _KERNEL
41 
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 
48 #include <machine/stdarg.h>
49 
50 #else
51 #include <sys/socket.h>
52 
53 #include <errno.h>
54 #include <stdarg.h>
55 #include <stdbool.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include "msgio.h"
63 #endif
64 
65 #ifdef HAVE_PJDLOG
66 #include <pjdlog.h>
67 #endif
68 
69 #include <sys/nv.h>
70 
71 #include "nv_impl.h"
72 #include "nvlist_impl.h"
73 #include "nvpair_impl.h"
74 
75 #ifndef	HAVE_PJDLOG
76 #ifdef _KERNEL
77 #define	PJDLOG_ASSERT(...)		MPASS(__VA_ARGS__)
78 #define	PJDLOG_RASSERT(expr, ...)	KASSERT(expr, (__VA_ARGS__))
79 #define	PJDLOG_ABORT(...)		panic(__VA_ARGS__)
80 #else
81 #include <assert.h>
82 #define	PJDLOG_ASSERT(...)		assert(__VA_ARGS__)
83 #define	PJDLOG_RASSERT(expr, ...)	assert(expr)
84 #define	PJDLOG_ABORT(...)		do {				\
85 	fprintf(stderr, "%s:%u: ", __FILE__, __LINE__);			\
86 	fprintf(stderr, __VA_ARGS__);					\
87 	fprintf(stderr, "\n");						\
88 	abort();							\
89 } while (0)
90 #endif
91 #endif
92 
93 #define	NV_FLAG_PRIVATE_MASK	(NV_FLAG_BIG_ENDIAN | NV_FLAG_IN_ARRAY)
94 #define	NV_FLAG_PUBLIC_MASK	(NV_FLAG_IGNORE_CASE | NV_FLAG_NO_UNIQUE)
95 #define	NV_FLAG_ALL_MASK	(NV_FLAG_PRIVATE_MASK | NV_FLAG_PUBLIC_MASK)
96 
97 #define	NVLIST_MAGIC	0x6e766c	/* "nvl" */
98 struct nvlist {
99 	int		 nvl_magic;
100 	int		 nvl_error;
101 	int		 nvl_flags;
102 	size_t		 nvl_datasize;
103 	nvpair_t	*nvl_parent;
104 	nvpair_t	*nvl_array_next;
105 	struct nvl_head	 nvl_head;
106 };
107 
108 #define	NVLIST_ASSERT(nvl)	do {					\
109 	PJDLOG_ASSERT((nvl) != NULL);					\
110 	PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC);		\
111 } while (0)
112 
113 #ifdef _KERNEL
114 MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
115 #endif
116 
117 #define	NVPAIR_ASSERT(nvp)	nvpair_assert(nvp)
118 
119 #define	NVLIST_HEADER_MAGIC	0x6c
120 #define	NVLIST_HEADER_VERSION	0x00
121 struct nvlist_header {
122 	uint8_t		nvlh_magic;
123 	uint8_t		nvlh_version;
124 	uint8_t		nvlh_flags;
125 	uint64_t	nvlh_descriptors;
126 	uint64_t	nvlh_size;
127 } __packed;
128 
129 nvlist_t *
nvlist_create(int flags)130 nvlist_create(int flags)
131 {
132 	nvlist_t *nvl;
133 
134 	PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
135 
136 	nvl = nv_malloc(sizeof(*nvl));
137 	if (nvl == NULL)
138 		return (NULL);
139 	nvl->nvl_error = 0;
140 	nvl->nvl_flags = flags;
141 	nvl->nvl_parent = NULL;
142 	nvl->nvl_array_next = NULL;
143 	nvl->nvl_datasize = sizeof(struct nvlist_header);
144 	TAILQ_INIT(&nvl->nvl_head);
145 	nvl->nvl_magic = NVLIST_MAGIC;
146 
147 	return (nvl);
148 }
149 
150 void
nvlist_destroy(nvlist_t * nvl)151 nvlist_destroy(nvlist_t *nvl)
152 {
153 	nvpair_t *nvp;
154 
155 	if (nvl == NULL)
156 		return;
157 
158 	ERRNO_SAVE();
159 
160 	NVLIST_ASSERT(nvl);
161 
162 	while ((nvp = nvlist_first_nvpair(nvl)) != NULL) {
163 		nvlist_remove_nvpair(nvl, nvp);
164 		nvpair_free(nvp);
165 	}
166 	if (nvl->nvl_array_next != NULL)
167 		nvpair_free_structure(nvl->nvl_array_next);
168 	nvl->nvl_array_next = NULL;
169 	nvl->nvl_parent = NULL;
170 	nvl->nvl_magic = 0;
171 	nv_free(nvl);
172 
173 	ERRNO_RESTORE();
174 }
175 
176 void
nvlist_set_error(nvlist_t * nvl,int error)177 nvlist_set_error(nvlist_t *nvl, int error)
178 {
179 
180 	PJDLOG_ASSERT(error != 0);
181 
182 	/*
183 	 * Check for error != 0 so that we don't do the wrong thing if somebody
184 	 * tries to abuse this API when asserts are disabled.
185 	 */
186 	if (nvl != NULL && error != 0 && nvl->nvl_error == 0)
187 		nvl->nvl_error = error;
188 }
189 
190 int
nvlist_error(const nvlist_t * nvl)191 nvlist_error(const nvlist_t *nvl)
192 {
193 
194 	if (nvl == NULL)
195 		return (ENOMEM);
196 
197 	NVLIST_ASSERT(nvl);
198 
199 	return (nvl->nvl_error);
200 }
201 
202 nvpair_t *
nvlist_get_nvpair_parent(const nvlist_t * nvl)203 nvlist_get_nvpair_parent(const nvlist_t *nvl)
204 {
205 
206 	NVLIST_ASSERT(nvl);
207 
208 	return (nvl->nvl_parent);
209 }
210 
211 const nvlist_t *
nvlist_get_parent(const nvlist_t * nvl,void ** cookiep)212 nvlist_get_parent(const nvlist_t *nvl, void **cookiep)
213 {
214 	nvpair_t *nvp;
215 
216 	NVLIST_ASSERT(nvl);
217 
218 	nvp = nvl->nvl_parent;
219 	if (cookiep != NULL)
220 		*cookiep = nvp;
221 	if (nvp == NULL)
222 		return (NULL);
223 
224 	return (nvpair_nvlist(nvp));
225 }
226 
227 void
nvlist_set_parent(nvlist_t * nvl,nvpair_t * parent)228 nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent)
229 {
230 
231 	NVLIST_ASSERT(nvl);
232 
233 	nvl->nvl_parent = parent;
234 }
235 
236 void
nvlist_set_array_next(nvlist_t * nvl,nvpair_t * ele)237 nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele)
238 {
239 
240 	NVLIST_ASSERT(nvl);
241 
242 	if (ele != NULL) {
243 		nvl->nvl_flags |= NV_FLAG_IN_ARRAY;
244 	} else {
245 		nvl->nvl_flags &= ~NV_FLAG_IN_ARRAY;
246 		nv_free(nvl->nvl_array_next);
247 	}
248 
249 	nvl->nvl_array_next = ele;
250 }
251 
252 static void
nvlist_update_size(nvlist_t * nvl,nvpair_t * new,ssize_t mul)253 nvlist_update_size(nvlist_t *nvl, nvpair_t *new, ssize_t mul)
254 {
255 	ssize_t size;
256 	size_t nitems;
257 	const nvlist_t *nvlistnew;
258 	const nvlist_t * const *nvlarray;
259 	nvlist_t *parent;
260 	unsigned int ii;
261 
262 	NVLIST_ASSERT(nvl);
263 	NVPAIR_ASSERT(new);
264 	PJDLOG_ASSERT(mul == 1 || mul == -1);
265 
266 	size = nvpair_header_size();
267 	size += strlen(nvpair_name(new)) + 1;
268 
269 	if (nvpair_type(new) == NV_TYPE_NVLIST) {
270 		nvlistnew = nvpair_get_nvlist(new);
271 		size += nvlistnew->nvl_datasize;
272 		size += nvpair_header_size() + 1;
273 	} else if (nvpair_type(new) == NV_TYPE_NVLIST_ARRAY) {
274 		nvlarray = nvpair_get_nvlist_array(new, &nitems);
275 		PJDLOG_ASSERT(nitems > 0);
276 
277 		size += (nvpair_header_size() + 1) * nitems;
278 		for (ii = 0; ii < nitems; ii++) {
279 			PJDLOG_ASSERT(nvlarray[ii]->nvl_error == 0);
280 			size += nvlarray[ii]->nvl_datasize;
281 		}
282 	} else {
283 		size += nvpair_size(new);
284 	}
285 
286 	size *= mul;
287 
288 	nvl->nvl_datasize += size;
289 
290 	parent = nvl;
291 	while ((parent = __DECONST(nvlist_t *,
292 	    nvlist_get_parent(parent, NULL))) != NULL) {
293 		parent->nvl_datasize += size;
294 	}
295 }
296 
297 nvpair_t *
nvlist_get_array_next_nvpair(nvlist_t * nvl)298 nvlist_get_array_next_nvpair(nvlist_t *nvl)
299 {
300 
301 	NVLIST_ASSERT(nvl);
302 
303 	return (nvl->nvl_array_next);
304 }
305 
306 bool
nvlist_in_array(const nvlist_t * nvl)307 nvlist_in_array(const nvlist_t *nvl)
308 {
309 
310 	NVLIST_ASSERT(nvl);
311 
312 	return ((nvl->nvl_flags & NV_FLAG_IN_ARRAY) != 0);
313 }
314 
315 const nvlist_t *
nvlist_get_array_next(const nvlist_t * nvl)316 nvlist_get_array_next(const nvlist_t *nvl)
317 {
318 	nvpair_t *nvp;
319 
320 	NVLIST_ASSERT(nvl);
321 
322 	nvp = nvl->nvl_array_next;
323 	if (nvp == NULL)
324 		return (NULL);
325 
326 	return (nvpair_get_nvlist(nvp));
327 }
328 
329 const nvlist_t *
nvlist_get_pararr(const nvlist_t * nvl,void ** cookiep)330 nvlist_get_pararr(const nvlist_t *nvl, void **cookiep)
331 {
332 	const nvlist_t *ret;
333 
334 	ret = nvlist_get_array_next(nvl);
335 	if (ret != NULL) {
336 		if (cookiep != NULL)
337 			*cookiep = NULL;
338 		return (ret);
339 	}
340 
341 	return (nvlist_get_parent(nvl, cookiep));
342 }
343 
344 bool
nvlist_empty(const nvlist_t * nvl)345 nvlist_empty(const nvlist_t *nvl)
346 {
347 
348 	NVLIST_ASSERT(nvl);
349 	PJDLOG_ASSERT(nvl->nvl_error == 0);
350 
351 	return (nvlist_first_nvpair(nvl) == NULL);
352 }
353 
354 int
nvlist_flags(const nvlist_t * nvl)355 nvlist_flags(const nvlist_t *nvl)
356 {
357 
358 	NVLIST_ASSERT(nvl);
359 	PJDLOG_ASSERT(nvl->nvl_error == 0);
360 
361 	return (nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
362 }
363 
364 void
nvlist_set_flags(nvlist_t * nvl,int flags)365 nvlist_set_flags(nvlist_t *nvl, int flags)
366 {
367 
368 	NVLIST_ASSERT(nvl);
369 	PJDLOG_ASSERT(nvl->nvl_error == 0);
370 
371 	nvl->nvl_flags = flags;
372 }
373 
374 void
nvlist_report_missing(int type,const char * name)375 nvlist_report_missing(int type, const char *name)
376 {
377 
378 	PJDLOG_ABORT("Element '%s' of type %s doesn't exist.",
379 	    name, nvpair_type_string(type));
380 }
381 
382 static nvpair_t *
nvlist_find(const nvlist_t * nvl,int type,const char * name)383 nvlist_find(const nvlist_t *nvl, int type, const char *name)
384 {
385 	nvpair_t *nvp;
386 
387 	NVLIST_ASSERT(nvl);
388 	PJDLOG_ASSERT(nvl->nvl_error == 0);
389 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
390 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
391 
392 	for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
393 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
394 		if (type != NV_TYPE_NONE && nvpair_type(nvp) != type)
395 			continue;
396 		if ((nvl->nvl_flags & NV_FLAG_IGNORE_CASE) != 0) {
397 			if (strcasecmp(nvpair_name(nvp), name) != 0)
398 				continue;
399 		} else {
400 			if (strcmp(nvpair_name(nvp), name) != 0)
401 				continue;
402 		}
403 		break;
404 	}
405 
406 	if (nvp == NULL)
407 		ERRNO_SET(ENOENT);
408 
409 	return (nvp);
410 }
411 
412 bool
nvlist_exists_type(const nvlist_t * nvl,const char * name,int type)413 nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
414 {
415 
416 	NVLIST_ASSERT(nvl);
417 	PJDLOG_ASSERT(nvl->nvl_error == 0);
418 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
419 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
420 
421 	return (nvlist_find(nvl, type, name) != NULL);
422 }
423 
424 void
nvlist_free_type(nvlist_t * nvl,const char * name,int type)425 nvlist_free_type(nvlist_t *nvl, const char *name, int type)
426 {
427 	nvpair_t *nvp;
428 
429 	NVLIST_ASSERT(nvl);
430 	PJDLOG_ASSERT(nvl->nvl_error == 0);
431 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
432 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
433 
434 	nvp = nvlist_find(nvl, type, name);
435 	if (nvp != NULL)
436 		nvlist_free_nvpair(nvl, nvp);
437 	else
438 		nvlist_report_missing(type, name);
439 }
440 
441 nvlist_t *
nvlist_clone(const nvlist_t * nvl)442 nvlist_clone(const nvlist_t *nvl)
443 {
444 	nvlist_t *newnvl;
445 	nvpair_t *nvp, *newnvp;
446 
447 	NVLIST_ASSERT(nvl);
448 
449 	if (nvl->nvl_error != 0) {
450 		ERRNO_SET(nvl->nvl_error);
451 		return (NULL);
452 	}
453 
454 	newnvl = nvlist_create(nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
455 	for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
456 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
457 		newnvp = nvpair_clone(nvp);
458 		if (newnvp == NULL)
459 			break;
460 		(void)nvlist_move_nvpair(newnvl, newnvp);
461 	}
462 	if (nvp != NULL) {
463 		nvlist_destroy(newnvl);
464 		return (NULL);
465 	}
466 	return (newnvl);
467 }
468 
469 #ifndef _KERNEL
470 static bool
nvlist_dump_error_check(const nvlist_t * nvl,int fd,int level)471 nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
472 {
473 
474 	if (nvlist_error(nvl) != 0) {
475 		dprintf(fd, "%*serror: %d\n", level * 4, "",
476 		    nvlist_error(nvl));
477 		return (true);
478 	}
479 
480 	return (false);
481 }
482 
483 /*
484  * Dump content of nvlist.
485  */
486 void
nvlist_dump(const nvlist_t * nvl,int fd)487 nvlist_dump(const nvlist_t *nvl, int fd)
488 {
489 	const nvlist_t *tmpnvl;
490 	nvpair_t *nvp, *tmpnvp;
491 	void *cookie;
492 	int level;
493 
494 	level = 0;
495 	if (nvlist_dump_error_check(nvl, fd, level))
496 		return;
497 
498 	nvp = nvlist_first_nvpair(nvl);
499 	while (nvp != NULL) {
500 		dprintf(fd, "%*s%s (%s):", level * 4, "", nvpair_name(nvp),
501 		    nvpair_type_string(nvpair_type(nvp)));
502 		switch (nvpair_type(nvp)) {
503 		case NV_TYPE_NULL:
504 			dprintf(fd, " null\n");
505 			break;
506 		case NV_TYPE_BOOL:
507 			dprintf(fd, " %s\n", nvpair_get_bool(nvp) ?
508 			    "TRUE" : "FALSE");
509 			break;
510 		case NV_TYPE_NUMBER:
511 			dprintf(fd, " %ju (%jd) (0x%jx)\n",
512 			    (uintmax_t)nvpair_get_number(nvp),
513 			    (intmax_t)nvpair_get_number(nvp),
514 			    (uintmax_t)nvpair_get_number(nvp));
515 			break;
516 		case NV_TYPE_STRING:
517 			dprintf(fd, " [%s]\n", nvpair_get_string(nvp));
518 			break;
519 		case NV_TYPE_NVLIST:
520 			dprintf(fd, "\n");
521 			tmpnvl = nvpair_get_nvlist(nvp);
522 			if (nvlist_dump_error_check(tmpnvl, fd, level + 1))
523 				break;
524 			tmpnvp = nvlist_first_nvpair(tmpnvl);
525 			if (tmpnvp != NULL) {
526 				nvl = tmpnvl;
527 				nvp = tmpnvp;
528 				level++;
529 				continue;
530 			}
531 			break;
532 		case NV_TYPE_DESCRIPTOR:
533 			dprintf(fd, " %d\n", nvpair_get_descriptor(nvp));
534 			break;
535 		case NV_TYPE_BINARY:
536 		    {
537 			const unsigned char *binary;
538 			unsigned int ii;
539 			size_t size;
540 
541 			binary = nvpair_get_binary(nvp, &size);
542 			dprintf(fd, " %zu ", size);
543 			for (ii = 0; ii < size; ii++)
544 				dprintf(fd, "%02hhx", binary[ii]);
545 			dprintf(fd, "\n");
546 			break;
547 		    }
548 		case NV_TYPE_BOOL_ARRAY:
549 		    {
550 			const bool *value;
551 			unsigned int ii;
552 			size_t nitems;
553 
554 			value = nvpair_get_bool_array(nvp, &nitems);
555 			dprintf(fd, " [ ");
556 			for (ii = 0; ii < nitems; ii++) {
557 				dprintf(fd, "%s", value[ii] ? "TRUE" : "FALSE");
558 				if (ii != nitems - 1)
559 					dprintf(fd, ", ");
560 			}
561 			dprintf(fd, " ]\n");
562 			break;
563 		    }
564 		case NV_TYPE_STRING_ARRAY:
565 		    {
566 			const char * const *value;
567 			unsigned int ii;
568 			size_t nitems;
569 
570 			value = nvpair_get_string_array(nvp, &nitems);
571 			dprintf(fd, " [ ");
572 			for (ii = 0; ii < nitems; ii++) {
573 				if (value[ii] == NULL)
574 					dprintf(fd, "NULL");
575 				else
576 					dprintf(fd, "\"%s\"", value[ii]);
577 				if (ii != nitems - 1)
578 					dprintf(fd, ", ");
579 			}
580 			dprintf(fd, " ]\n");
581 			break;
582 		    }
583 		case NV_TYPE_NUMBER_ARRAY:
584 		    {
585 			const uint64_t *value;
586 			unsigned int ii;
587 			size_t nitems;
588 
589 			value = nvpair_get_number_array(nvp, &nitems);
590 			dprintf(fd, " [ ");
591 			for (ii = 0; ii < nitems; ii++) {
592 				dprintf(fd, "%ju (%jd) (0x%jx)",
593 				    value[ii], value[ii], value[ii]);
594 				if (ii != nitems - 1)
595 					dprintf(fd, ", ");
596 			}
597 			dprintf(fd, " ]\n");
598 			break;
599 		    }
600 		case NV_TYPE_DESCRIPTOR_ARRAY:
601 		    {
602 			const int *value;
603 			unsigned int ii;
604 			size_t nitems;
605 
606 			value = nvpair_get_descriptor_array(nvp, &nitems);
607 			dprintf(fd, " [ ");
608 			for (ii = 0; ii < nitems; ii++) {
609 				dprintf(fd, "%d", value[ii]);
610 				if (ii != nitems - 1)
611 					dprintf(fd, ", ");
612 			}
613 			dprintf(fd, " ]\n");
614 			break;
615 		    }
616 		case NV_TYPE_NVLIST_ARRAY:
617 		    {
618 			const nvlist_t * const *value;
619 			unsigned int ii;
620 			size_t nitems;
621 
622 			value = nvpair_get_nvlist_array(nvp, &nitems);
623 			dprintf(fd, " %zu\n", nitems);
624 			tmpnvl = NULL;
625 			tmpnvp = NULL;
626 			for (ii = 0; ii < nitems; ii++) {
627 				if (nvlist_dump_error_check(value[ii], fd,
628 				    level + 1)) {
629 					break;
630 				}
631 
632 				if (tmpnvl == NULL) {
633 					tmpnvp = nvlist_first_nvpair(value[ii]);
634 					if (tmpnvp != NULL) {
635 						tmpnvl = value[ii];
636 					} else {
637 						dprintf(fd, "%*s,\n",
638 						    (level + 1) * 4, "");
639 					}
640 				}
641 			}
642 			if (tmpnvp != NULL) {
643 				nvl = tmpnvl;
644 				nvp = tmpnvp;
645 				level++;
646 				continue;
647 			}
648 			break;
649 		    }
650 		default:
651 			PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
652 		}
653 
654 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
655 			do {
656 				cookie = NULL;
657 				if (nvlist_in_array(nvl))
658 					dprintf(fd, "%*s,\n", level * 4, "");
659 				nvl = nvlist_get_pararr(nvl, &cookie);
660 				if (nvl == NULL)
661 					return;
662 				if (nvlist_in_array(nvl) && cookie == NULL) {
663 					nvp = nvlist_first_nvpair(nvl);
664 				} else {
665 					nvp = cookie;
666 					level--;
667 				}
668 			} while (nvp == NULL);
669 			if (nvlist_in_array(nvl) && cookie == NULL)
670 				break;
671 		}
672 	}
673 }
674 
675 void
nvlist_fdump(const nvlist_t * nvl,FILE * fp)676 nvlist_fdump(const nvlist_t *nvl, FILE *fp)
677 {
678 
679 	fflush(fp);
680 	nvlist_dump(nvl, fileno(fp));
681 }
682 #endif
683 
684 /*
685  * The function obtains size of the nvlist after nvlist_pack().
686  */
687 size_t
nvlist_size(const nvlist_t * nvl)688 nvlist_size(const nvlist_t *nvl)
689 {
690 
691 	return (nvl->nvl_datasize);
692 }
693 
694 #ifndef _KERNEL
695 static int *
nvlist_xdescriptors(const nvlist_t * nvl,int * descs)696 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
697 {
698 	void *cookie;
699 	nvpair_t *nvp;
700 	int type;
701 
702 	NVLIST_ASSERT(nvl);
703 	PJDLOG_ASSERT(nvl->nvl_error == 0);
704 
705 	cookie = NULL;
706 	do {
707 		while (nvlist_next(nvl, &type, &cookie) != NULL) {
708 			nvp = cookie;
709 			switch (type) {
710 			case NV_TYPE_DESCRIPTOR:
711 				*descs = nvpair_get_descriptor(nvp);
712 				descs++;
713 				break;
714 			case NV_TYPE_DESCRIPTOR_ARRAY:
715 			    {
716 				const int *value;
717 				size_t nitems;
718 				unsigned int ii;
719 
720 				value = nvpair_get_descriptor_array(nvp,
721 				    &nitems);
722 				for (ii = 0; ii < nitems; ii++) {
723 					*descs = value[ii];
724 					descs++;
725 				}
726 				break;
727 			    }
728 			case NV_TYPE_NVLIST:
729 				nvl = nvpair_get_nvlist(nvp);
730 				cookie = NULL;
731 				break;
732 			case NV_TYPE_NVLIST_ARRAY:
733 			    {
734 				const nvlist_t * const *value;
735 				size_t nitems;
736 
737 				value = nvpair_get_nvlist_array(nvp, &nitems);
738 				PJDLOG_ASSERT(value != NULL);
739 				PJDLOG_ASSERT(nitems > 0);
740 
741 				nvl = value[0];
742 				cookie = NULL;
743 				break;
744 			    }
745 			}
746 		}
747 	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
748 
749 	return (descs);
750 }
751 #endif
752 
753 #ifndef _KERNEL
754 int *
nvlist_descriptors(const nvlist_t * nvl,size_t * nitemsp)755 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
756 {
757 	size_t nitems;
758 	int *fds;
759 
760 	nitems = nvlist_ndescriptors(nvl);
761 	fds = nv_calloc(nitems + 1, sizeof(fds[0]));
762 	if (fds == NULL)
763 		return (NULL);
764 	if (nitems > 0)
765 		nvlist_xdescriptors(nvl, fds);
766 	fds[nitems] = -1;
767 	if (nitemsp != NULL)
768 		*nitemsp = nitems;
769 	return (fds);
770 }
771 #endif
772 
773 size_t
nvlist_ndescriptors(const nvlist_t * nvl)774 nvlist_ndescriptors(const nvlist_t *nvl)
775 {
776 #ifndef _KERNEL
777 	void *cookie;
778 	nvpair_t *nvp;
779 	size_t ndescs;
780 	int type;
781 
782 	NVLIST_ASSERT(nvl);
783 	PJDLOG_ASSERT(nvl->nvl_error == 0);
784 
785 	ndescs = 0;
786 	cookie = NULL;
787 	do {
788 		while (nvlist_next(nvl, &type, &cookie) != NULL) {
789 			nvp = cookie;
790 			switch (type) {
791 			case NV_TYPE_DESCRIPTOR:
792 				ndescs++;
793 				break;
794 			case NV_TYPE_NVLIST:
795 				nvl = nvpair_get_nvlist(nvp);
796 				cookie = NULL;
797 				break;
798 			case NV_TYPE_NVLIST_ARRAY:
799 			    {
800 				const nvlist_t * const *value;
801 				size_t nitems;
802 
803 				value = nvpair_get_nvlist_array(nvp, &nitems);
804 				PJDLOG_ASSERT(value != NULL);
805 				PJDLOG_ASSERT(nitems > 0);
806 
807 				nvl = value[0];
808 				cookie = NULL;
809 				break;
810 			    }
811 			case NV_TYPE_DESCRIPTOR_ARRAY:
812 			    {
813 				size_t nitems;
814 
815 				(void)nvpair_get_descriptor_array(nvp,
816 				    &nitems);
817 				ndescs += nitems;
818 				break;
819 			    }
820 			}
821 		}
822 	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
823 
824 	return (ndescs);
825 #else
826 	return (0);
827 #endif
828 }
829 
830 static unsigned char *
nvlist_pack_header(const nvlist_t * nvl,unsigned char * ptr,size_t * leftp)831 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
832 {
833 	struct nvlist_header nvlhdr;
834 
835 	NVLIST_ASSERT(nvl);
836 
837 	nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
838 	nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
839 	nvlhdr.nvlh_flags = nvl->nvl_flags;
840 #if BYTE_ORDER == BIG_ENDIAN
841 	nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
842 #endif
843 	nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
844 	nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
845 	PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
846 	memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
847 	ptr += sizeof(nvlhdr);
848 	*leftp -= sizeof(nvlhdr);
849 
850 	return (ptr);
851 }
852 
853 static void *
nvlist_xpack(const nvlist_t * nvl,int64_t * fdidxp,size_t * sizep)854 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
855 {
856 	unsigned char *buf, *ptr;
857 	size_t left, size;
858 	const nvlist_t *tmpnvl;
859 	nvpair_t *nvp, *tmpnvp;
860 	void *cookie;
861 
862 	NVLIST_ASSERT(nvl);
863 
864 	if (nvl->nvl_error != 0) {
865 		ERRNO_SET(nvl->nvl_error);
866 		return (NULL);
867 	}
868 
869 	size = nvlist_size(nvl);
870 	buf = nv_malloc(size);
871 	if (buf == NULL)
872 		return (NULL);
873 
874 	ptr = buf;
875 	left = size;
876 
877 	ptr = nvlist_pack_header(nvl, ptr, &left);
878 
879 	nvp = nvlist_first_nvpair(nvl);
880 	while (nvp != NULL) {
881 		NVPAIR_ASSERT(nvp);
882 
883 		nvpair_init_datasize(nvp);
884 		ptr = nvpair_pack_header(nvp, ptr, &left);
885 		if (ptr == NULL)
886 			goto fail;
887 		switch (nvpair_type(nvp)) {
888 		case NV_TYPE_NULL:
889 			ptr = nvpair_pack_null(nvp, ptr, &left);
890 			break;
891 		case NV_TYPE_BOOL:
892 			ptr = nvpair_pack_bool(nvp, ptr, &left);
893 			break;
894 		case NV_TYPE_NUMBER:
895 			ptr = nvpair_pack_number(nvp, ptr, &left);
896 			break;
897 		case NV_TYPE_STRING:
898 			ptr = nvpair_pack_string(nvp, ptr, &left);
899 			break;
900 		case NV_TYPE_NVLIST:
901 			tmpnvl = nvpair_get_nvlist(nvp);
902 			ptr = nvlist_pack_header(tmpnvl, ptr, &left);
903 			if (ptr == NULL)
904 				goto fail;
905 			tmpnvp = nvlist_first_nvpair(tmpnvl);
906 			if (tmpnvp != NULL) {
907 				nvl = tmpnvl;
908 				nvp = tmpnvp;
909 				continue;
910 			}
911 			ptr = nvpair_pack_nvlist_up(ptr, &left);
912 			break;
913 #ifndef _KERNEL
914 		case NV_TYPE_DESCRIPTOR:
915 			ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
916 			break;
917 		case NV_TYPE_DESCRIPTOR_ARRAY:
918 			ptr = nvpair_pack_descriptor_array(nvp, ptr, fdidxp,
919 			    &left);
920 			break;
921 #endif
922 		case NV_TYPE_BINARY:
923 			ptr = nvpair_pack_binary(nvp, ptr, &left);
924 			break;
925 		case NV_TYPE_BOOL_ARRAY:
926 			ptr = nvpair_pack_bool_array(nvp, ptr, &left);
927 			break;
928 		case NV_TYPE_NUMBER_ARRAY:
929 			ptr = nvpair_pack_number_array(nvp, ptr, &left);
930 			break;
931 		case NV_TYPE_STRING_ARRAY:
932 			ptr = nvpair_pack_string_array(nvp, ptr, &left);
933 			break;
934 		case NV_TYPE_NVLIST_ARRAY:
935 		    {
936 			const nvlist_t * const * value;
937 			size_t nitems;
938 			unsigned int ii;
939 
940 			tmpnvl = NULL;
941 			value = nvpair_get_nvlist_array(nvp, &nitems);
942 			for (ii = 0; ii < nitems; ii++) {
943 				ptr = nvlist_pack_header(value[ii], ptr, &left);
944 				if (ptr == NULL)
945 					goto out;
946 				tmpnvp = nvlist_first_nvpair(value[ii]);
947 				if (tmpnvp != NULL) {
948 					tmpnvl = value[ii];
949 					break;
950 				}
951 				ptr = nvpair_pack_nvlist_array_next(ptr, &left);
952 				if (ptr == NULL)
953 					goto out;
954 			}
955 			if (tmpnvl != NULL) {
956 				nvl = tmpnvl;
957 				nvp = tmpnvp;
958 				continue;
959 			}
960 			break;
961 		    }
962 		default:
963 			PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
964 		}
965 		if (ptr == NULL)
966 			goto fail;
967 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
968 			do {
969 				cookie = NULL;
970 				if (nvlist_in_array(nvl)) {
971 					ptr = nvpair_pack_nvlist_array_next(ptr,
972 					    &left);
973 					if (ptr == NULL)
974 						goto fail;
975 				}
976 				nvl = nvlist_get_pararr(nvl, &cookie);
977 				if (nvl == NULL)
978 					goto out;
979 				if (nvlist_in_array(nvl) && cookie == NULL) {
980 					nvp = nvlist_first_nvpair(nvl);
981 					ptr = nvlist_pack_header(nvl, ptr,
982 					    &left);
983 					if (ptr == NULL)
984 						goto fail;
985 				} else if (nvpair_type((nvpair_t *)cookie) !=
986 				    NV_TYPE_NVLIST_ARRAY) {
987 					ptr = nvpair_pack_nvlist_up(ptr, &left);
988 					if (ptr == NULL)
989 						goto fail;
990 					nvp = cookie;
991 				} else {
992 					nvp = cookie;
993 				}
994 			} while (nvp == NULL);
995 			if (nvlist_in_array(nvl) && cookie == NULL)
996 				break;
997 		}
998 	}
999 
1000 out:
1001 	if (sizep != NULL)
1002 		*sizep = size;
1003 	return (buf);
1004 fail:
1005 	nv_free(buf);
1006 	return (NULL);
1007 }
1008 
1009 void *
nvlist_pack(const nvlist_t * nvl,size_t * sizep)1010 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
1011 {
1012 
1013 	NVLIST_ASSERT(nvl);
1014 
1015 	if (nvl->nvl_error != 0) {
1016 		ERRNO_SET(nvl->nvl_error);
1017 		return (NULL);
1018 	}
1019 
1020 	if (nvlist_ndescriptors(nvl) > 0) {
1021 		ERRNO_SET(EOPNOTSUPP);
1022 		return (NULL);
1023 	}
1024 
1025 	return (nvlist_xpack(nvl, NULL, sizep));
1026 }
1027 
1028 static bool
nvlist_check_header(struct nvlist_header * nvlhdrp)1029 nvlist_check_header(struct nvlist_header *nvlhdrp)
1030 {
1031 
1032 	if (nvlhdrp->nvlh_size > SIZE_MAX - sizeof(*nvlhdrp)) {
1033 		ERRNO_SET(EINVAL);
1034 		return (false);
1035 	}
1036 	if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
1037 		ERRNO_SET(EINVAL);
1038 		return (false);
1039 	}
1040 	if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
1041 		ERRNO_SET(EINVAL);
1042 		return (false);
1043 	}
1044 #if BYTE_ORDER == BIG_ENDIAN
1045 	if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
1046 		nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
1047 		nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
1048 	}
1049 #else
1050 	if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
1051 		nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
1052 		nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
1053 	}
1054 #endif
1055 	return (true);
1056 }
1057 
1058 const unsigned char *
nvlist_unpack_header(nvlist_t * nvl,const unsigned char * ptr,size_t nfds,bool * isbep,size_t * leftp)1059 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
1060     bool *isbep, size_t *leftp)
1061 {
1062 	struct nvlist_header nvlhdr;
1063 	int inarrayf;
1064 
1065 	if (*leftp < sizeof(nvlhdr))
1066 		goto fail;
1067 
1068 	memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
1069 
1070 	if (!nvlist_check_header(&nvlhdr))
1071 		goto fail;
1072 
1073 	if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
1074 		goto fail;
1075 
1076 	/*
1077 	 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
1078 	 */
1079 	if (nvlhdr.nvlh_descriptors > nfds)
1080 		goto fail;
1081 
1082 	if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
1083 		goto fail;
1084 
1085 	inarrayf = (nvl->nvl_flags & NV_FLAG_IN_ARRAY);
1086 	nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK) | inarrayf;
1087 
1088 	ptr += sizeof(nvlhdr);
1089 	if (isbep != NULL)
1090 		*isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
1091 	*leftp -= sizeof(nvlhdr);
1092 
1093 	return (ptr);
1094 fail:
1095 	ERRNO_SET(EINVAL);
1096 	return (NULL);
1097 }
1098 
1099 static nvlist_t *
nvlist_xunpack(const void * buf,size_t size,const int * fds,size_t nfds,int flags)1100 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds,
1101     int flags)
1102 {
1103 	const unsigned char *ptr;
1104 	nvlist_t *nvl, *retnvl, *tmpnvl, *array;
1105 	nvpair_t *nvp;
1106 	size_t left;
1107 	bool isbe;
1108 
1109 	PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
1110 
1111 	left = size;
1112 	ptr = buf;
1113 
1114 	tmpnvl = array = NULL;
1115 	nvl = retnvl = nvlist_create(0);
1116 	if (nvl == NULL)
1117 		goto fail;
1118 
1119 	ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
1120 	if (ptr == NULL)
1121 		goto fail;
1122 	if (nvl->nvl_flags != flags) {
1123 		ERRNO_SET(EILSEQ);
1124 		goto fail;
1125 	}
1126 
1127 	while (left > 0) {
1128 		ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
1129 		if (ptr == NULL)
1130 			goto fail;
1131 		switch (nvpair_type(nvp)) {
1132 		case NV_TYPE_NULL:
1133 			ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
1134 			break;
1135 		case NV_TYPE_BOOL:
1136 			ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
1137 			break;
1138 		case NV_TYPE_NUMBER:
1139 			ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
1140 			break;
1141 		case NV_TYPE_STRING:
1142 			ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
1143 			break;
1144 		case NV_TYPE_NVLIST:
1145 			ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
1146 			    &tmpnvl);
1147 			if (tmpnvl == NULL || ptr == NULL)
1148 				goto fail;
1149 			nvlist_set_parent(tmpnvl, nvp);
1150 			break;
1151 #ifndef _KERNEL
1152 		case NV_TYPE_DESCRIPTOR:
1153 			ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
1154 			    fds, nfds);
1155 			break;
1156 		case NV_TYPE_DESCRIPTOR_ARRAY:
1157 			ptr = nvpair_unpack_descriptor_array(isbe, nvp, ptr,
1158 			    &left, fds, nfds);
1159 			break;
1160 #endif
1161 		case NV_TYPE_BINARY:
1162 			ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
1163 			break;
1164 		case NV_TYPE_NVLIST_UP:
1165 			if (nvl->nvl_parent == NULL)
1166 				goto fail;
1167 			nvl = nvpair_nvlist(nvl->nvl_parent);
1168 			nvpair_free_structure(nvp);
1169 			continue;
1170 		case NV_TYPE_NVLIST_ARRAY_NEXT:
1171 			if (nvl->nvl_array_next == NULL) {
1172 				if (nvl->nvl_parent == NULL)
1173 					goto fail;
1174 				nvl = nvpair_nvlist(nvl->nvl_parent);
1175 			} else {
1176 				nvl = __DECONST(nvlist_t *,
1177 				    nvlist_get_array_next(nvl));
1178 				ptr = nvlist_unpack_header(nvl, ptr, nfds,
1179 				    &isbe, &left);
1180 				if (ptr == NULL)
1181 					goto fail;
1182 			}
1183 			nvpair_free_structure(nvp);
1184 			continue;
1185 		case NV_TYPE_BOOL_ARRAY:
1186 			ptr = nvpair_unpack_bool_array(isbe, nvp, ptr, &left);
1187 			break;
1188 		case NV_TYPE_NUMBER_ARRAY:
1189 			ptr = nvpair_unpack_number_array(isbe, nvp, ptr, &left);
1190 			break;
1191 		case NV_TYPE_STRING_ARRAY:
1192 			ptr = nvpair_unpack_string_array(isbe, nvp, ptr, &left);
1193 			break;
1194 		case NV_TYPE_NVLIST_ARRAY:
1195 			ptr = nvpair_unpack_nvlist_array(isbe, nvp, ptr, &left,
1196 			    &array);
1197 			if (ptr == NULL)
1198 				goto fail;
1199 			PJDLOG_ASSERT(array != NULL);
1200 			tmpnvl = array;
1201 			do {
1202 				nvlist_set_parent(array, nvp);
1203 				array = __DECONST(nvlist_t *,
1204 				    nvlist_get_array_next(array));
1205 			} while (array != NULL);
1206 			ptr = nvlist_unpack_header(tmpnvl, ptr, nfds, &isbe,
1207 			    &left);
1208 			break;
1209 		default:
1210 			PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
1211 		}
1212 		if (ptr == NULL)
1213 			goto fail;
1214 		if (!nvlist_move_nvpair(nvl, nvp))
1215 			goto fail;
1216 		if (tmpnvl != NULL) {
1217 			nvl = tmpnvl;
1218 			tmpnvl = NULL;
1219 		}
1220 	}
1221 
1222 	return (retnvl);
1223 fail:
1224 	nvlist_destroy(retnvl);
1225 	return (NULL);
1226 }
1227 
1228 nvlist_t *
nvlist_unpack(const void * buf,size_t size,int flags)1229 nvlist_unpack(const void *buf, size_t size, int flags)
1230 {
1231 
1232 	return (nvlist_xunpack(buf, size, NULL, 0, flags));
1233 }
1234 
1235 #ifndef _KERNEL
1236 int
nvlist_send(int sock,const nvlist_t * nvl)1237 nvlist_send(int sock, const nvlist_t *nvl)
1238 {
1239 	size_t datasize, nfds;
1240 	int *fds;
1241 	void *data;
1242 	int64_t fdidx;
1243 	int ret;
1244 
1245 	if (nvlist_error(nvl) != 0) {
1246 		ERRNO_SET(nvlist_error(nvl));
1247 		return (-1);
1248 	}
1249 
1250 	fds = nvlist_descriptors(nvl, &nfds);
1251 	if (fds == NULL)
1252 		return (-1);
1253 
1254 	ret = -1;
1255 	fdidx = 0;
1256 
1257 	data = nvlist_xpack(nvl, &fdidx, &datasize);
1258 	if (data == NULL)
1259 		goto out;
1260 
1261 	if (buf_send(sock, data, datasize) == -1)
1262 		goto out;
1263 
1264 	if (nfds > 0) {
1265 		if (fd_send(sock, fds, nfds) == -1)
1266 			goto out;
1267 	}
1268 
1269 	ret = 0;
1270 out:
1271 	ERRNO_SAVE();
1272 	nv_free(fds);
1273 	nv_free(data);
1274 	ERRNO_RESTORE();
1275 	return (ret);
1276 }
1277 
1278 nvlist_t *
nvlist_recv(int sock,int flags)1279 nvlist_recv(int sock, int flags)
1280 {
1281 	struct nvlist_header nvlhdr;
1282 	nvlist_t *nvl, *ret;
1283 	unsigned char *buf;
1284 	size_t nfds, size, i, offset;
1285 	int *fds, soflags, sotype;
1286 	socklen_t solen;
1287 
1288 	solen = sizeof(sotype);
1289 	if (getsockopt(sock, SOL_SOCKET, SO_TYPE, &sotype, &solen) != 0)
1290 		return (NULL);
1291 
1292 	soflags = sotype == SOCK_DGRAM ? MSG_PEEK : 0;
1293 	if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr), soflags) == -1)
1294 		return (NULL);
1295 
1296 	if (!nvlist_check_header(&nvlhdr))
1297 		return (NULL);
1298 
1299 	nfds = (size_t)nvlhdr.nvlh_descriptors;
1300 	size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
1301 
1302 	buf = nv_malloc(size);
1303 	if (buf == NULL)
1304 		return (NULL);
1305 
1306 	ret = NULL;
1307 	fds = NULL;
1308 
1309 	if (sotype == SOCK_DGRAM)
1310 		offset = 0;
1311 	else {
1312 		memcpy(buf, &nvlhdr, sizeof(nvlhdr));
1313 		offset = sizeof(nvlhdr);
1314 	}
1315 
1316 	if (buf_recv(sock, buf + offset, size - offset, 0) == -1)
1317 		goto out;
1318 
1319 	if (nfds > 0) {
1320 		fds = nv_calloc(nfds, sizeof(fds[0]));
1321 		if (fds == NULL)
1322 			goto out;
1323 		if (fd_recv(sock, fds, nfds) == -1)
1324 			goto out;
1325 	}
1326 
1327 	nvl = nvlist_xunpack(buf, size, fds, nfds, flags);
1328 	if (nvl == NULL) {
1329 		ERRNO_SAVE();
1330 		for (i = 0; i < nfds; i++)
1331 			close(fds[i]);
1332 		ERRNO_RESTORE();
1333 		goto out;
1334 	}
1335 
1336 	ret = nvl;
1337 out:
1338 	ERRNO_SAVE();
1339 	nv_free(buf);
1340 	nv_free(fds);
1341 	ERRNO_RESTORE();
1342 
1343 	return (ret);
1344 }
1345 
1346 nvlist_t *
nvlist_xfer(int sock,nvlist_t * nvl,int flags)1347 nvlist_xfer(int sock, nvlist_t *nvl, int flags)
1348 {
1349 
1350 	if (nvlist_send(sock, nvl) < 0) {
1351 		nvlist_destroy(nvl);
1352 		return (NULL);
1353 	}
1354 	nvlist_destroy(nvl);
1355 	return (nvlist_recv(sock, flags));
1356 }
1357 #endif
1358 
1359 nvpair_t *
nvlist_first_nvpair(const nvlist_t * nvl)1360 nvlist_first_nvpair(const nvlist_t *nvl)
1361 {
1362 
1363 	NVLIST_ASSERT(nvl);
1364 
1365 	return (TAILQ_FIRST(&nvl->nvl_head));
1366 }
1367 
1368 nvpair_t *
nvlist_next_nvpair(const nvlist_t * nvl __unused,const nvpair_t * nvp)1369 nvlist_next_nvpair(const nvlist_t *nvl __unused, const nvpair_t *nvp)
1370 {
1371 	nvpair_t *retnvp;
1372 
1373 	NVLIST_ASSERT(nvl);
1374 	NVPAIR_ASSERT(nvp);
1375 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1376 
1377 	retnvp = nvpair_next(nvp);
1378 	PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
1379 
1380 	return (retnvp);
1381 
1382 }
1383 
1384 nvpair_t *
nvlist_prev_nvpair(const nvlist_t * nvl __unused,const nvpair_t * nvp)1385 nvlist_prev_nvpair(const nvlist_t *nvl __unused, const nvpair_t *nvp)
1386 {
1387 	nvpair_t *retnvp;
1388 
1389 	NVLIST_ASSERT(nvl);
1390 	NVPAIR_ASSERT(nvp);
1391 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1392 
1393 	retnvp = nvpair_prev(nvp);
1394 	PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1395 
1396 	return (retnvp);
1397 }
1398 
1399 const char *
nvlist_next(const nvlist_t * nvl,int * typep,void ** cookiep)1400 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1401 {
1402 	nvpair_t *nvp;
1403 
1404 	NVLIST_ASSERT(nvl);
1405 
1406 	if (cookiep == NULL || *cookiep == NULL)
1407 		nvp = nvlist_first_nvpair(nvl);
1408 	else
1409 		nvp = nvlist_next_nvpair(nvl, *cookiep);
1410 	if (nvp == NULL)
1411 		return (NULL);
1412 	if (typep != NULL)
1413 		*typep = nvpair_type(nvp);
1414 	if (cookiep != NULL)
1415 		*cookiep = nvp;
1416 	return (nvpair_name(nvp));
1417 }
1418 
1419 bool
nvlist_exists(const nvlist_t * nvl,const char * name)1420 nvlist_exists(const nvlist_t *nvl, const char *name)
1421 {
1422 
1423 	return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1424 }
1425 
1426 #define	NVLIST_EXISTS(type, TYPE)					\
1427 bool									\
1428 nvlist_exists_##type(const nvlist_t *nvl, const char *name)		\
1429 {									\
1430 									\
1431 	return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL);	\
1432 }
1433 
NVLIST_EXISTS(null,NULL)1434 NVLIST_EXISTS(null, NULL)
1435 NVLIST_EXISTS(bool, BOOL)
1436 NVLIST_EXISTS(number, NUMBER)
1437 NVLIST_EXISTS(string, STRING)
1438 NVLIST_EXISTS(nvlist, NVLIST)
1439 NVLIST_EXISTS(binary, BINARY)
1440 NVLIST_EXISTS(bool_array, BOOL_ARRAY)
1441 NVLIST_EXISTS(number_array, NUMBER_ARRAY)
1442 NVLIST_EXISTS(string_array, STRING_ARRAY)
1443 NVLIST_EXISTS(nvlist_array, NVLIST_ARRAY)
1444 #ifndef _KERNEL
1445 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1446 NVLIST_EXISTS(descriptor_array, DESCRIPTOR_ARRAY)
1447 #endif
1448 
1449 #undef	NVLIST_EXISTS
1450 
1451 void
1452 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1453 {
1454 	nvpair_t *newnvp;
1455 
1456 	NVPAIR_ASSERT(nvp);
1457 
1458 	if (nvlist_error(nvl) != 0) {
1459 		ERRNO_SET(nvlist_error(nvl));
1460 		return;
1461 	}
1462 	if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1463 		if (nvlist_exists(nvl, nvpair_name(nvp))) {
1464 			nvl->nvl_error = EEXIST;
1465 			ERRNO_SET(nvlist_error(nvl));
1466 			return;
1467 		}
1468 	}
1469 
1470 	newnvp = nvpair_clone(nvp);
1471 	if (newnvp == NULL) {
1472 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1473 		ERRNO_SET(nvlist_error(nvl));
1474 		return;
1475 	}
1476 
1477 	nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1478 	nvlist_update_size(nvl, newnvp, 1);
1479 }
1480 
1481 void
nvlist_add_stringf(nvlist_t * nvl,const char * name,const char * valuefmt,...)1482 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1483 {
1484 	va_list valueap;
1485 
1486 	va_start(valueap, valuefmt);
1487 	nvlist_add_stringv(nvl, name, valuefmt, valueap);
1488 	va_end(valueap);
1489 }
1490 
1491 void
nvlist_add_stringv(nvlist_t * nvl,const char * name,const char * valuefmt,va_list valueap)1492 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1493     va_list valueap)
1494 {
1495 	nvpair_t *nvp;
1496 
1497 	if (nvlist_error(nvl) != 0) {
1498 		ERRNO_SET(nvlist_error(nvl));
1499 		return;
1500 	}
1501 
1502 	nvp = nvpair_create_stringv(name, valuefmt, valueap);
1503 	if (nvp == NULL) {
1504 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1505 		ERRNO_SET(nvl->nvl_error);
1506 	} else {
1507 		(void)nvlist_move_nvpair(nvl, nvp);
1508 	}
1509 }
1510 
1511 void
nvlist_add_null(nvlist_t * nvl,const char * name)1512 nvlist_add_null(nvlist_t *nvl, const char *name)
1513 {
1514 	nvpair_t *nvp;
1515 
1516 	if (nvlist_error(nvl) != 0) {
1517 		ERRNO_SET(nvlist_error(nvl));
1518 		return;
1519 	}
1520 
1521 	nvp = nvpair_create_null(name);
1522 	if (nvp == NULL) {
1523 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1524 		ERRNO_SET(nvl->nvl_error);
1525 	} else {
1526 		(void)nvlist_move_nvpair(nvl, nvp);
1527 	}
1528 }
1529 
1530 void
nvlist_add_binary(nvlist_t * nvl,const char * name,const void * value,size_t size)1531 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1532     size_t size)
1533 {
1534 	nvpair_t *nvp;
1535 
1536 	if (nvlist_error(nvl) != 0) {
1537 		ERRNO_SET(nvlist_error(nvl));
1538 		return;
1539 	}
1540 
1541 	nvp = nvpair_create_binary(name, value, size);
1542 	if (nvp == NULL) {
1543 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1544 		ERRNO_SET(nvl->nvl_error);
1545 	} else {
1546 		(void)nvlist_move_nvpair(nvl, nvp);
1547 	}
1548 }
1549 
1550 
1551 #define	NVLIST_ADD(vtype, type)						\
1552 void									\
1553 nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value)		\
1554 {									\
1555 	nvpair_t *nvp;							\
1556 									\
1557 	if (nvlist_error(nvl) != 0) {					\
1558 		ERRNO_SET(nvlist_error(nvl));				\
1559 		return;							\
1560 	}								\
1561 									\
1562 	nvp = nvpair_create_##type(name, value);			\
1563 	if (nvp == NULL) {						\
1564 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1565 		ERRNO_SET(nvl->nvl_error);				\
1566 	} else {							\
1567 		(void)nvlist_move_nvpair(nvl, nvp);			\
1568 	}								\
1569 }
1570 
1571 NVLIST_ADD(bool, bool)
1572 NVLIST_ADD(uint64_t, number)
1573 NVLIST_ADD(const char *, string)
1574 NVLIST_ADD(const nvlist_t *, nvlist)
1575 #ifndef _KERNEL
1576 NVLIST_ADD(int, descriptor);
1577 #endif
1578 
1579 #undef	NVLIST_ADD
1580 
1581 #define	NVLIST_ADD_ARRAY(vtype, type)					\
1582 void									\
1583 nvlist_add_##type##_array(nvlist_t *nvl, const char *name, vtype value,	\
1584     size_t nitems)							\
1585 {									\
1586 	nvpair_t *nvp;							\
1587 									\
1588 	if (nvlist_error(nvl) != 0) {					\
1589 		ERRNO_SET(nvlist_error(nvl));				\
1590 		return;							\
1591 	}								\
1592 									\
1593 	nvp = nvpair_create_##type##_array(name, value, nitems);	\
1594 	if (nvp == NULL) {						\
1595 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1596 		ERRNO_SET(nvl->nvl_error);				\
1597 	} else {							\
1598 		(void)nvlist_move_nvpair(nvl, nvp);			\
1599 	}								\
1600 }
1601 
NVLIST_ADD_ARRAY(const bool *,bool)1602 NVLIST_ADD_ARRAY(const bool *, bool)
1603 NVLIST_ADD_ARRAY(const uint64_t *, number)
1604 NVLIST_ADD_ARRAY(const char * const *, string)
1605 NVLIST_ADD_ARRAY(const nvlist_t * const *, nvlist)
1606 #ifndef _KERNEL
1607 NVLIST_ADD_ARRAY(const int *, descriptor)
1608 #endif
1609 
1610 #undef	NVLIST_ADD_ARRAY
1611 
1612 #define	NVLIST_APPEND_ARRAY(vtype, type, TYPE)				\
1613 void									\
1614 nvlist_append_##type##_array(nvlist_t *nvl, const char *name, vtype value)\
1615 {									\
1616 	nvpair_t *nvp;							\
1617 									\
1618 	if (nvlist_error(nvl) != 0) {					\
1619 		ERRNO_SET(nvlist_error(nvl));				\
1620 		return;							\
1621 	}								\
1622 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1623 	if (nvp == NULL) {						\
1624 		nvlist_add_##type##_array(nvl, name, &value, 1);	\
1625 		return;							\
1626 	}								\
1627 	nvlist_update_size(nvl, nvp, -1);				\
1628 	if (nvpair_append_##type##_array(nvp, value) == -1) {		\
1629 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1630 		ERRNO_SET(nvl->nvl_error);				\
1631 	}								\
1632 	nvlist_update_size(nvl, nvp, 1);				\
1633 }
1634 
1635 NVLIST_APPEND_ARRAY(const bool, bool, BOOL)
1636 NVLIST_APPEND_ARRAY(const uint64_t, number, NUMBER)
1637 NVLIST_APPEND_ARRAY(const char *, string, STRING)
1638 NVLIST_APPEND_ARRAY(const nvlist_t *, nvlist, NVLIST)
1639 #ifndef _KERNEL
1640 NVLIST_APPEND_ARRAY(const int, descriptor, DESCRIPTOR)
1641 #endif
1642 
1643 #undef	NVLIST_APPEND_ARRAY
1644 
1645 bool
1646 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1647 {
1648 
1649 	NVPAIR_ASSERT(nvp);
1650 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1651 
1652 	if (nvlist_error(nvl) != 0) {
1653 		nvpair_free(nvp);
1654 		ERRNO_SET(nvlist_error(nvl));
1655 		return (false);
1656 	}
1657 	if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1658 		if (nvlist_exists(nvl, nvpair_name(nvp))) {
1659 			nvpair_free(nvp);
1660 			nvl->nvl_error = EEXIST;
1661 			ERRNO_SET(nvl->nvl_error);
1662 			return (false);
1663 		}
1664 	}
1665 
1666 	nvpair_insert(&nvl->nvl_head, nvp, nvl);
1667 	nvlist_update_size(nvl, nvp, 1);
1668 	return (true);
1669 }
1670 
1671 void
nvlist_move_string(nvlist_t * nvl,const char * name,char * value)1672 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1673 {
1674 	nvpair_t *nvp;
1675 
1676 	if (nvlist_error(nvl) != 0) {
1677 		nv_free(value);
1678 		ERRNO_SET(nvlist_error(nvl));
1679 		return;
1680 	}
1681 
1682 	nvp = nvpair_move_string(name, value);
1683 	if (nvp == NULL) {
1684 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1685 		ERRNO_SET(nvl->nvl_error);
1686 	} else {
1687 		(void)nvlist_move_nvpair(nvl, nvp);
1688 	}
1689 }
1690 
1691 void
nvlist_move_nvlist(nvlist_t * nvl,const char * name,nvlist_t * value)1692 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1693 {
1694 	nvpair_t *nvp;
1695 
1696 	if (nvlist_error(nvl) != 0) {
1697 		if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1698 			nvlist_destroy(value);
1699 		ERRNO_SET(nvlist_error(nvl));
1700 		return;
1701 	}
1702 
1703 	nvp = nvpair_move_nvlist(name, value);
1704 	if (nvp == NULL) {
1705 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1706 		ERRNO_SET(nvl->nvl_error);
1707 	} else {
1708 		(void)nvlist_move_nvpair(nvl, nvp);
1709 	}
1710 }
1711 
1712 #ifndef _KERNEL
1713 void
nvlist_move_descriptor(nvlist_t * nvl,const char * name,int value)1714 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1715 {
1716 	nvpair_t *nvp;
1717 
1718 	if (nvlist_error(nvl) != 0) {
1719 		close(value);
1720 		ERRNO_SET(nvlist_error(nvl));
1721 		return;
1722 	}
1723 
1724 	nvp = nvpair_move_descriptor(name, value);
1725 	if (nvp == NULL) {
1726 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1727 		ERRNO_SET(nvl->nvl_error);
1728 	} else {
1729 		(void)nvlist_move_nvpair(nvl, nvp);
1730 	}
1731 }
1732 #endif
1733 
1734 void
nvlist_move_binary(nvlist_t * nvl,const char * name,void * value,size_t size)1735 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1736 {
1737 	nvpair_t *nvp;
1738 
1739 	if (nvlist_error(nvl) != 0) {
1740 		nv_free(value);
1741 		ERRNO_SET(nvlist_error(nvl));
1742 		return;
1743 	}
1744 
1745 	nvp = nvpair_move_binary(name, value, size);
1746 	if (nvp == NULL) {
1747 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1748 		ERRNO_SET(nvl->nvl_error);
1749 	} else {
1750 		(void)nvlist_move_nvpair(nvl, nvp);
1751 	}
1752 }
1753 
1754 void
nvlist_move_bool_array(nvlist_t * nvl,const char * name,bool * value,size_t nitems)1755 nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value,
1756     size_t nitems)
1757 {
1758 	nvpair_t *nvp;
1759 
1760 	if (nvlist_error(nvl) != 0) {
1761 		nv_free(value);
1762 		ERRNO_SET(nvlist_error(nvl));
1763 		return;
1764 	}
1765 
1766 	nvp = nvpair_move_bool_array(name, value, nitems);
1767 	if (nvp == NULL) {
1768 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1769 		ERRNO_SET(nvl->nvl_error);
1770 	} else {
1771 		(void)nvlist_move_nvpair(nvl, nvp);
1772 	}
1773 }
1774 
1775 void
nvlist_move_string_array(nvlist_t * nvl,const char * name,char ** value,size_t nitems)1776 nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value,
1777     size_t nitems)
1778 {
1779 	nvpair_t *nvp;
1780 	size_t i;
1781 
1782 	if (nvlist_error(nvl) != 0) {
1783 		if (value != NULL) {
1784 			for (i = 0; i < nitems; i++)
1785 				nv_free(value[i]);
1786 			nv_free(value);
1787 		}
1788 		ERRNO_SET(nvlist_error(nvl));
1789 		return;
1790 	}
1791 
1792 	nvp = nvpair_move_string_array(name, value, nitems);
1793 	if (nvp == NULL) {
1794 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1795 		ERRNO_SET(nvl->nvl_error);
1796 	} else {
1797 		(void)nvlist_move_nvpair(nvl, nvp);
1798 	}
1799 }
1800 
1801 void
nvlist_move_nvlist_array(nvlist_t * nvl,const char * name,nvlist_t ** value,size_t nitems)1802 nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value,
1803     size_t nitems)
1804 {
1805 	nvpair_t *nvp;
1806 	size_t i;
1807 
1808 	if (nvlist_error(nvl) != 0) {
1809 		if (value != NULL) {
1810 			for (i = 0; i < nitems; i++) {
1811 				if (nvlist_get_pararr(value[i], NULL) == NULL)
1812 					nvlist_destroy(value[i]);
1813 			}
1814 		}
1815 		nv_free(value);
1816 		ERRNO_SET(nvlist_error(nvl));
1817 		return;
1818 	}
1819 
1820 	nvp = nvpair_move_nvlist_array(name, value, nitems);
1821 	if (nvp == NULL) {
1822 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1823 		ERRNO_SET(nvl->nvl_error);
1824 	} else {
1825 		(void)nvlist_move_nvpair(nvl, nvp);
1826 	}
1827 }
1828 
1829 void
nvlist_move_number_array(nvlist_t * nvl,const char * name,uint64_t * value,size_t nitems)1830 nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value,
1831     size_t nitems)
1832 {
1833 	nvpair_t *nvp;
1834 
1835 	if (nvlist_error(nvl) != 0) {
1836 		nv_free(value);
1837 		ERRNO_SET(nvlist_error(nvl));
1838 		return;
1839 	}
1840 
1841 	nvp = nvpair_move_number_array(name, value, nitems);
1842 	if (nvp == NULL) {
1843 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1844 		ERRNO_SET(nvl->nvl_error);
1845 	} else {
1846 		(void)nvlist_move_nvpair(nvl, nvp);
1847 	}
1848 }
1849 
1850 #ifndef _KERNEL
1851 void
nvlist_move_descriptor_array(nvlist_t * nvl,const char * name,int * value,size_t nitems)1852 nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value,
1853     size_t nitems)
1854 {
1855 	nvpair_t *nvp;
1856 	size_t i;
1857 
1858 	if (nvlist_error(nvl) != 0) {
1859 		if (value != 0) {
1860 			for (i = 0; i < nitems; i++)
1861 				close(value[i]);
1862 			nv_free(value);
1863 		}
1864 
1865 		ERRNO_SET(nvlist_error(nvl));
1866 		return;
1867 	}
1868 
1869 	nvp = nvpair_move_descriptor_array(name, value, nitems);
1870 	if (nvp == NULL) {
1871 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1872 		ERRNO_SET(nvl->nvl_error);
1873 	} else {
1874 		(void)nvlist_move_nvpair(nvl, nvp);
1875 	}
1876 }
1877 #endif
1878 
1879 const nvpair_t *
nvlist_get_nvpair(const nvlist_t * nvl,const char * name)1880 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1881 {
1882 
1883 	return (nvlist_find(nvl, NV_TYPE_NONE, name));
1884 }
1885 
1886 #define	NVLIST_GET(ftype, type, TYPE)					\
1887 ftype									\
1888 nvlist_get_##type(const nvlist_t *nvl, const char *name)		\
1889 {									\
1890 	const nvpair_t *nvp;						\
1891 									\
1892 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name);			\
1893 	if (nvp == NULL)						\
1894 		nvlist_report_missing(NV_TYPE_##TYPE, name);		\
1895 	return (nvpair_get_##type(nvp));				\
1896 }
1897 
NVLIST_GET(bool,bool,BOOL)1898 NVLIST_GET(bool, bool, BOOL)
1899 NVLIST_GET(uint64_t, number, NUMBER)
1900 NVLIST_GET(const char *, string, STRING)
1901 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1902 #ifndef _KERNEL
1903 NVLIST_GET(int, descriptor, DESCRIPTOR)
1904 #endif
1905 
1906 #undef	NVLIST_GET
1907 
1908 const void *
1909 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1910 {
1911 	nvpair_t *nvp;
1912 
1913 	nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1914 	if (nvp == NULL)
1915 		nvlist_report_missing(NV_TYPE_BINARY, name);
1916 
1917 	return (nvpair_get_binary(nvp, sizep));
1918 }
1919 
1920 #define	NVLIST_GET_ARRAY(ftype, type, TYPE)				\
1921 ftype									\
1922 nvlist_get_##type##_array(const nvlist_t *nvl, const char *name,	\
1923     size_t *nitems)							\
1924 {									\
1925 	const nvpair_t *nvp;						\
1926 									\
1927 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1928 	if (nvp == NULL)						\
1929 		nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name);	\
1930 	return (nvpair_get_##type##_array(nvp, nitems));		\
1931 }
1932 
NVLIST_GET_ARRAY(const bool *,bool,BOOL)1933 NVLIST_GET_ARRAY(const bool *, bool, BOOL)
1934 NVLIST_GET_ARRAY(const uint64_t *, number, NUMBER)
1935 NVLIST_GET_ARRAY(const char * const *, string, STRING)
1936 NVLIST_GET_ARRAY(const nvlist_t * const *, nvlist, NVLIST)
1937 #ifndef _KERNEL
1938 NVLIST_GET_ARRAY(const int *, descriptor, DESCRIPTOR)
1939 #endif
1940 
1941 #undef	NVLIST_GET_ARRAY
1942 
1943 #define	NVLIST_TAKE(ftype, type, TYPE)					\
1944 ftype									\
1945 nvlist_take_##type(nvlist_t *nvl, const char *name)			\
1946 {									\
1947 	nvpair_t *nvp;							\
1948 	ftype value;							\
1949 									\
1950 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name);			\
1951 	if (nvp == NULL)						\
1952 		nvlist_report_missing(NV_TYPE_##TYPE, name);		\
1953 	value = (ftype)(intptr_t)nvpair_get_##type(nvp);		\
1954 	nvlist_remove_nvpair(nvl, nvp);					\
1955 	nvpair_free_structure(nvp);					\
1956 	return (value);							\
1957 }
1958 
1959 NVLIST_TAKE(bool, bool, BOOL)
1960 NVLIST_TAKE(uint64_t, number, NUMBER)
1961 NVLIST_TAKE(char *, string, STRING)
1962 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1963 #ifndef _KERNEL
1964 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1965 #endif
1966 
1967 #undef	NVLIST_TAKE
1968 
1969 void *
1970 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1971 {
1972 	nvpair_t *nvp;
1973 	void *value;
1974 
1975 	nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1976 	if (nvp == NULL)
1977 		nvlist_report_missing(NV_TYPE_BINARY, name);
1978 
1979 	value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1980 	nvlist_remove_nvpair(nvl, nvp);
1981 	nvpair_free_structure(nvp);
1982 	return (value);
1983 }
1984 
1985 #define	NVLIST_TAKE_ARRAY(ftype, type, TYPE)				\
1986 ftype									\
1987 nvlist_take_##type##_array(nvlist_t *nvl, const char *name,		\
1988     size_t *nitems)							\
1989 {									\
1990 	nvpair_t *nvp;							\
1991 	ftype value;							\
1992 									\
1993 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1994 	if (nvp == NULL)						\
1995 		nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name);	\
1996 	value = (ftype)(intptr_t)nvpair_get_##type##_array(nvp, nitems);\
1997 	nvlist_remove_nvpair(nvl, nvp);					\
1998 	nvpair_free_structure(nvp);					\
1999 	return (value);							\
2000 }
2001 
NVLIST_TAKE_ARRAY(bool *,bool,BOOL)2002 NVLIST_TAKE_ARRAY(bool *, bool, BOOL)
2003 NVLIST_TAKE_ARRAY(uint64_t *, number, NUMBER)
2004 NVLIST_TAKE_ARRAY(char **, string, STRING)
2005 NVLIST_TAKE_ARRAY(nvlist_t **, nvlist, NVLIST)
2006 #ifndef _KERNEL
2007 NVLIST_TAKE_ARRAY(int *, descriptor, DESCRIPTOR)
2008 #endif
2009 
2010 void
2011 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2012 {
2013 
2014 	NVLIST_ASSERT(nvl);
2015 	NVPAIR_ASSERT(nvp);
2016 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2017 
2018 	nvpair_remove(&nvl->nvl_head, nvp, nvl);
2019 	nvlist_update_size(nvl, nvp, -1);
2020 }
2021 
2022 void
nvlist_free(nvlist_t * nvl,const char * name)2023 nvlist_free(nvlist_t *nvl, const char *name)
2024 {
2025 
2026 	nvlist_free_type(nvl, name, NV_TYPE_NONE);
2027 }
2028 
2029 #define	NVLIST_FREE(type, TYPE)						\
2030 void									\
2031 nvlist_free_##type(nvlist_t *nvl, const char *name)			\
2032 {									\
2033 									\
2034 	nvlist_free_type(nvl, name, NV_TYPE_##TYPE);			\
2035 }
2036 
NVLIST_FREE(null,NULL)2037 NVLIST_FREE(null, NULL)
2038 NVLIST_FREE(bool, BOOL)
2039 NVLIST_FREE(number, NUMBER)
2040 NVLIST_FREE(string, STRING)
2041 NVLIST_FREE(nvlist, NVLIST)
2042 NVLIST_FREE(binary, BINARY)
2043 NVLIST_FREE(bool_array, BOOL_ARRAY)
2044 NVLIST_FREE(number_array, NUMBER_ARRAY)
2045 NVLIST_FREE(string_array, STRING_ARRAY)
2046 NVLIST_FREE(nvlist_array, NVLIST_ARRAY)
2047 #ifndef _KERNEL
2048 NVLIST_FREE(descriptor, DESCRIPTOR)
2049 NVLIST_FREE(descriptor_array, DESCRIPTOR_ARRAY)
2050 #endif
2051 
2052 #undef	NVLIST_FREE
2053 
2054 void
2055 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2056 {
2057 
2058 	NVLIST_ASSERT(nvl);
2059 	NVPAIR_ASSERT(nvp);
2060 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2061 
2062 	nvlist_remove_nvpair(nvl, nvp);
2063 	nvpair_free(nvp);
2064 }
2065 
2066