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