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