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