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