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