Deleted Added
full compact
nvpair.c (285139) nvpair.c (286796)
1/*-
2 * Copyright (c) 2009-2013 The FreeBSD Foundation
1/*-
2 * Copyright (c) 2009-2013 The FreeBSD Foundation
3 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:

--- 12 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:

--- 12 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/contrib/libnv/nvpair.c 285139 2015-07-04 16:33:37Z oshogbo $");
32__FBSDID("$FreeBSD: head/sys/contrib/libnv/nvpair.c 286796 2015-08-15 06:34:49Z oshogbo $");
32
33#include <sys/param.h>
34#include <sys/endian.h>
35#include <sys/queue.h>
36
37#ifdef _KERNEL
38
39#include <sys/errno.h>

--- 41 unchanged lines hidden (view full) ---

81
82#define NVPAIR_MAGIC 0x6e7670 /* "nvp" */
83struct nvpair {
84 int nvp_magic;
85 char *nvp_name;
86 int nvp_type;
87 uint64_t nvp_data;
88 size_t nvp_datasize;
33
34#include <sys/param.h>
35#include <sys/endian.h>
36#include <sys/queue.h>
37
38#ifdef _KERNEL
39
40#include <sys/errno.h>

--- 41 unchanged lines hidden (view full) ---

82
83#define NVPAIR_MAGIC 0x6e7670 /* "nvp" */
84struct nvpair {
85 int nvp_magic;
86 char *nvp_name;
87 int nvp_type;
88 uint64_t nvp_data;
89 size_t nvp_datasize;
90 size_t nvp_nitems; /* Used only for array types. */
89 nvlist_t *nvp_list;
90 TAILQ_ENTRY(nvpair) nvp_next;
91};
92
93#define NVPAIR_ASSERT(nvp) do { \
94 PJDLOG_ASSERT((nvp) != NULL); \
95 PJDLOG_ASSERT((nvp)->nvp_magic == NVPAIR_MAGIC); \
96} while (0)
97
98struct nvpair_header {
99 uint8_t nvph_type;
100 uint16_t nvph_namesize;
101 uint64_t nvph_datasize;
91 nvlist_t *nvp_list;
92 TAILQ_ENTRY(nvpair) nvp_next;
93};
94
95#define NVPAIR_ASSERT(nvp) do { \
96 PJDLOG_ASSERT((nvp) != NULL); \
97 PJDLOG_ASSERT((nvp)->nvp_magic == NVPAIR_MAGIC); \
98} while (0)
99
100struct nvpair_header {
101 uint8_t nvph_type;
102 uint16_t nvph_namesize;
103 uint64_t nvph_datasize;
104 uint64_t nvph_nitems;
102} __packed;
103
104
105void
106nvpair_assert(const nvpair_t *nvp)
107{
108
109 NVPAIR_ASSERT(nvp);
110}
111
105} __packed;
106
107
108void
109nvpair_assert(const nvpair_t *nvp)
110{
111
112 NVPAIR_ASSERT(nvp);
113}
114
115static nvpair_t *
116nvpair_allocv(const char *name, int type, uint64_t data, size_t datasize,
117 size_t nitems)
118{
119 nvpair_t *nvp;
120 size_t namelen;
121
122 PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
123
124 namelen = strlen(name);
125 if (namelen >= NV_NAME_MAX) {
126 ERRNO_SET(ENAMETOOLONG);
127 return (NULL);
128 }
129
130 nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
131 if (nvp != NULL) {
132 nvp->nvp_name = (char *)(nvp + 1);
133 memcpy(nvp->nvp_name, name, namelen);
134 nvp->nvp_name[namelen] = '\0';
135 nvp->nvp_type = type;
136 nvp->nvp_data = data;
137 nvp->nvp_datasize = datasize;
138 nvp->nvp_nitems = nitems;
139 nvp->nvp_magic = NVPAIR_MAGIC;
140 }
141
142 return (nvp);
143}
144
112nvlist_t *
113nvpair_nvlist(const nvpair_t *nvp)
114{
115
116 NVPAIR_ASSERT(nvp);
117
118 return (nvp->nvp_list);
119}

--- 37 unchanged lines hidden (view full) ---

157 nvlist_t *nvl;
158
159 /* XXX: DECONST is bad, mkay? */
160 nvl = __DECONST(nvlist_t *, nvpair_get_nvlist(nvp));
161 PJDLOG_ASSERT(nvl != NULL);
162 nvlist_set_parent(nvl, NULL);
163}
164
145nvlist_t *
146nvpair_nvlist(const nvpair_t *nvp)
147{
148
149 NVPAIR_ASSERT(nvp);
150
151 return (nvp->nvp_list);
152}

--- 37 unchanged lines hidden (view full) ---

190 nvlist_t *nvl;
191
192 /* XXX: DECONST is bad, mkay? */
193 nvl = __DECONST(nvlist_t *, nvpair_get_nvlist(nvp));
194 PJDLOG_ASSERT(nvl != NULL);
195 nvlist_set_parent(nvl, NULL);
196}
197
198static void
199nvpair_remove_nvlist_array(nvpair_t *nvp)
200{
201 nvlist_t **nvlarray;
202 size_t count, i;
203
204 /* XXX: DECONST is bad, mkay? */
205 nvlarray = __DECONST(nvlist_t **,
206 nvpair_get_nvlist_array(nvp, &count));
207 for (i = 0; i < count; i++)
208 nvlist_set_array_next(nvlarray[i], NULL);
209}
210
165void
166nvpair_remove(struct nvl_head *head, nvpair_t *nvp, const nvlist_t *nvl)
167{
168
169 NVPAIR_ASSERT(nvp);
170 PJDLOG_ASSERT(nvp->nvp_list == nvl);
171
172 if (nvpair_type(nvp) == NV_TYPE_NVLIST)
173 nvpair_remove_nvlist(nvp);
211void
212nvpair_remove(struct nvl_head *head, nvpair_t *nvp, const nvlist_t *nvl)
213{
214
215 NVPAIR_ASSERT(nvp);
216 PJDLOG_ASSERT(nvp->nvp_list == nvl);
217
218 if (nvpair_type(nvp) == NV_TYPE_NVLIST)
219 nvpair_remove_nvlist(nvp);
220 else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY)
221 nvpair_remove_nvlist_array(nvp);
174
175 TAILQ_REMOVE(head, nvp, nvp_next);
176 nvp->nvp_list = NULL;
177}
178
179nvpair_t *
180nvpair_clone(const nvpair_t *nvp)
181{

--- 17 unchanged lines hidden (view full) ---

199 newnvp = nvpair_create_number(name, nvpair_get_number(nvp));
200 break;
201 case NV_TYPE_STRING:
202 newnvp = nvpair_create_string(name, nvpair_get_string(nvp));
203 break;
204 case NV_TYPE_NVLIST:
205 newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
206 break;
222
223 TAILQ_REMOVE(head, nvp, nvp_next);
224 nvp->nvp_list = NULL;
225}
226
227nvpair_t *
228nvpair_clone(const nvpair_t *nvp)
229{

--- 17 unchanged lines hidden (view full) ---

247 newnvp = nvpair_create_number(name, nvpair_get_number(nvp));
248 break;
249 case NV_TYPE_STRING:
250 newnvp = nvpair_create_string(name, nvpair_get_string(nvp));
251 break;
252 case NV_TYPE_NVLIST:
253 newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
254 break;
255 case NV_TYPE_BINARY:
256 data = nvpair_get_binary(nvp, &datasize);
257 newnvp = nvpair_create_binary(name, data, datasize);
258 break;
259 case NV_TYPE_BOOL_ARRAY:
260 data = nvpair_get_bool_array(nvp, &datasize);
261 newnvp = nvpair_create_bool_array(name, data, datasize);
262 break;
263 case NV_TYPE_NUMBER_ARRAY:
264 data = nvpair_get_number_array(nvp, &datasize);
265 newnvp = nvpair_create_number_array(name, data, datasize);
266 break;
267 case NV_TYPE_STRING_ARRAY:
268 data = nvpair_get_string_array(nvp, &datasize);
269 newnvp = nvpair_create_string_array(name, data, datasize);
270 break;
271 case NV_TYPE_NVLIST_ARRAY:
272 data = nvpair_get_nvlist_array(nvp, &datasize);
273 newnvp = nvpair_create_nvlist_array(name, data, datasize);
274 break;
207#ifndef _KERNEL
208 case NV_TYPE_DESCRIPTOR:
209 newnvp = nvpair_create_descriptor(name,
210 nvpair_get_descriptor(nvp));
211 break;
275#ifndef _KERNEL
276 case NV_TYPE_DESCRIPTOR:
277 newnvp = nvpair_create_descriptor(name,
278 nvpair_get_descriptor(nvp));
279 break;
212#endif
213 case NV_TYPE_BINARY:
214 data = nvpair_get_binary(nvp, &datasize);
215 newnvp = nvpair_create_binary(name, data, datasize);
280 case NV_TYPE_DESCRIPTOR_ARRAY:
281 data = nvpair_get_descriptor_array(nvp, &datasize);
282 newnvp = nvpair_create_descriptor_array(name, data, datasize);
216 break;
283 break;
284#endif
217 default:
218 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
219 }
220
221 return (newnvp);
222}
223
224size_t

--- 20 unchanged lines hidden (view full) ---

245
246 NVPAIR_ASSERT(nvp);
247
248 nvphdr.nvph_type = nvp->nvp_type;
249 namesize = strlen(nvp->nvp_name) + 1;
250 PJDLOG_ASSERT(namesize > 0 && namesize <= UINT16_MAX);
251 nvphdr.nvph_namesize = namesize;
252 nvphdr.nvph_datasize = nvp->nvp_datasize;
285 default:
286 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
287 }
288
289 return (newnvp);
290}
291
292size_t

--- 20 unchanged lines hidden (view full) ---

313
314 NVPAIR_ASSERT(nvp);
315
316 nvphdr.nvph_type = nvp->nvp_type;
317 namesize = strlen(nvp->nvp_name) + 1;
318 PJDLOG_ASSERT(namesize > 0 && namesize <= UINT16_MAX);
319 nvphdr.nvph_namesize = namesize;
320 nvphdr.nvph_datasize = nvp->nvp_datasize;
321 nvphdr.nvph_nitems = nvp->nvp_nitems;
253 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
254 memcpy(ptr, &nvphdr, sizeof(nvphdr));
255 ptr += sizeof(nvphdr);
256 *leftp -= sizeof(nvphdr);
257
258 PJDLOG_ASSERT(*leftp >= namesize);
259 memcpy(ptr, nvp->nvp_name, namesize);
260 ptr += namesize;

--- 70 unchanged lines hidden (view full) ---

331 struct nvpair_header nvphdr;
332 size_t namesize;
333 const char *name = "";
334
335 namesize = 1;
336 nvphdr.nvph_type = NV_TYPE_NVLIST_UP;
337 nvphdr.nvph_namesize = namesize;
338 nvphdr.nvph_datasize = 0;
322 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
323 memcpy(ptr, &nvphdr, sizeof(nvphdr));
324 ptr += sizeof(nvphdr);
325 *leftp -= sizeof(nvphdr);
326
327 PJDLOG_ASSERT(*leftp >= namesize);
328 memcpy(ptr, nvp->nvp_name, namesize);
329 ptr += namesize;

--- 70 unchanged lines hidden (view full) ---

400 struct nvpair_header nvphdr;
401 size_t namesize;
402 const char *name = "";
403
404 namesize = 1;
405 nvphdr.nvph_type = NV_TYPE_NVLIST_UP;
406 nvphdr.nvph_namesize = namesize;
407 nvphdr.nvph_datasize = 0;
408 nvphdr.nvph_nitems = 0;
339 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
340 memcpy(ptr, &nvphdr, sizeof(nvphdr));
341 ptr += sizeof(nvphdr);
342 *leftp -= sizeof(nvphdr);
343
344 PJDLOG_ASSERT(*leftp >= namesize);
345 memcpy(ptr, name, namesize);
346 ptr += namesize;
347 *leftp -= namesize;
348
349 return (ptr);
350}
351
409 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
410 memcpy(ptr, &nvphdr, sizeof(nvphdr));
411 ptr += sizeof(nvphdr);
412 *leftp -= sizeof(nvphdr);
413
414 PJDLOG_ASSERT(*leftp >= namesize);
415 memcpy(ptr, name, namesize);
416 ptr += namesize;
417 *leftp -= namesize;
418
419 return (ptr);
420}
421
422unsigned char *
423nvpair_pack_nvlist_array_next(unsigned char *ptr, size_t *leftp)
424{
425 struct nvpair_header nvphdr;
426 size_t namesize;
427 const char *name = "";
428
429 namesize = 1;
430 nvphdr.nvph_type = NV_TYPE_NVLIST_ARRAY_NEXT;
431 nvphdr.nvph_namesize = namesize;
432 nvphdr.nvph_datasize = 0;
433 nvphdr.nvph_nitems = 0;
434 PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
435 memcpy(ptr, &nvphdr, sizeof(nvphdr));
436 ptr += sizeof(nvphdr);
437 *leftp -= sizeof(nvphdr);
438
439 PJDLOG_ASSERT(*leftp >= namesize);
440 memcpy(ptr, name, namesize);
441 ptr += namesize;
442 *leftp -= namesize;
443
444 return (ptr);
445}
446
352#ifndef _KERNEL
353unsigned char *
354nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
355 size_t *leftp)
356{
357 int64_t value;
358
359 NVPAIR_ASSERT(nvp);

--- 31 unchanged lines hidden (view full) ---

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
447#ifndef _KERNEL
448unsigned char *
449nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
450 size_t *leftp)
451{
452 int64_t value;
453
454 NVPAIR_ASSERT(nvp);

--- 31 unchanged lines hidden (view full) ---

486 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
487 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
488 ptr += nvp->nvp_datasize;
489 *leftp -= nvp->nvp_datasize;
490
491 return (ptr);
492}
493
494unsigned char *
495nvpair_pack_bool_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
496{
497
498 NVPAIR_ASSERT(nvp);
499 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
500 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
501
502 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
503 ptr += nvp->nvp_datasize;
504 *leftp -= nvp->nvp_datasize;
505
506 return (ptr);
507}
508
509unsigned char *
510nvpair_pack_number_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
511{
512
513 NVPAIR_ASSERT(nvp);
514 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
515 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
516
517 memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
518 ptr += nvp->nvp_datasize;
519 *leftp -= nvp->nvp_datasize;
520
521 return (ptr);
522}
523
524unsigned char *
525nvpair_pack_string_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
526{
527 unsigned int ii;
528 size_t size, len;
529 const char * const *array;
530
531 NVPAIR_ASSERT(nvp);
532 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
533 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
534
535 size = 0;
536 array = nvpair_get_string_array(nvp, NULL);
537 PJDLOG_ASSERT(array != NULL);
538
539 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
540 len = strlen(array[ii]) + 1;
541 PJDLOG_ASSERT(*leftp >= len);
542
543 memcpy(ptr, (const void *)array[ii], len);
544 size += len;
545 ptr += len;
546 *leftp -= len;
547 }
548
549 PJDLOG_ASSERT(size == nvp->nvp_datasize);
550
551 return (ptr);
552}
553
554#ifndef _KERNEL
555unsigned char *
556nvpair_pack_descriptor_array(const nvpair_t *nvp, unsigned char *ptr,
557 int64_t *fdidxp, size_t *leftp)
558{
559 int64_t value;
560 const int *array;
561 unsigned int ii;
562
563 NVPAIR_ASSERT(nvp);
564 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
565 PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
566
567 array = nvpair_get_descriptor_array(nvp, NULL);
568 PJDLOG_ASSERT(array != NULL);
569
570 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
571 PJDLOG_ASSERT(*leftp >= sizeof(value));
572
573 value = array[ii];
574 if (value != -1) {
575 /*
576 * If there is a real descriptor here, we change its
577 * number to position in the array of descriptors send
578 * via control message.
579 */
580 PJDLOG_ASSERT(fdidxp != NULL);
581
582 value = *fdidxp;
583 (*fdidxp)++;
584 }
585 memcpy(ptr, &value, sizeof(value));
586 ptr += sizeof(value);
587 *leftp -= sizeof(value);
588 }
589
590 return (ptr);
591}
592#endif
593
399void
400nvpair_init_datasize(nvpair_t *nvp)
401{
402
403 NVPAIR_ASSERT(nvp);
404
405 if (nvp->nvp_type == NV_TYPE_NVLIST) {
406 if (nvp->nvp_data == 0) {

--- 18 unchanged lines hidden (view full) ---

425 ptr += sizeof(nvphdr);
426 *leftp -= sizeof(nvphdr);
427
428#if NV_TYPE_FIRST > 0
429 if (nvphdr.nvph_type < NV_TYPE_FIRST)
430 goto failed;
431#endif
432 if (nvphdr.nvph_type > NV_TYPE_LAST &&
594void
595nvpair_init_datasize(nvpair_t *nvp)
596{
597
598 NVPAIR_ASSERT(nvp);
599
600 if (nvp->nvp_type == NV_TYPE_NVLIST) {
601 if (nvp->nvp_data == 0) {

--- 18 unchanged lines hidden (view full) ---

620 ptr += sizeof(nvphdr);
621 *leftp -= sizeof(nvphdr);
622
623#if NV_TYPE_FIRST > 0
624 if (nvphdr.nvph_type < NV_TYPE_FIRST)
625 goto failed;
626#endif
627 if (nvphdr.nvph_type > NV_TYPE_LAST &&
433 nvphdr.nvph_type != NV_TYPE_NVLIST_UP) {
628 nvphdr.nvph_type != NV_TYPE_NVLIST_UP &&
629 nvphdr.nvph_type != NV_TYPE_NVLIST_ARRAY_NEXT) {
434 goto failed;
435 }
436
437#if BYTE_ORDER == BIG_ENDIAN
438 if (!isbe) {
439 nvphdr.nvph_namesize = le16toh(nvphdr.nvph_namesize);
440 nvphdr.nvph_datasize = le64toh(nvphdr.nvph_datasize);
441 }

--- 20 unchanged lines hidden (view full) ---

462 *leftp -= nvphdr.nvph_namesize;
463
464 if (*leftp < nvphdr.nvph_datasize)
465 goto failed;
466
467 nvp->nvp_type = nvphdr.nvph_type;
468 nvp->nvp_data = 0;
469 nvp->nvp_datasize = nvphdr.nvph_datasize;
630 goto failed;
631 }
632
633#if BYTE_ORDER == BIG_ENDIAN
634 if (!isbe) {
635 nvphdr.nvph_namesize = le16toh(nvphdr.nvph_namesize);
636 nvphdr.nvph_datasize = le64toh(nvphdr.nvph_datasize);
637 }

--- 20 unchanged lines hidden (view full) ---

658 *leftp -= nvphdr.nvph_namesize;
659
660 if (*leftp < nvphdr.nvph_datasize)
661 goto failed;
662
663 nvp->nvp_type = nvphdr.nvph_type;
664 nvp->nvp_data = 0;
665 nvp->nvp_datasize = nvphdr.nvph_datasize;
666 nvp->nvp_nitems = nvphdr.nvph_nitems;
470
471 return (ptr);
472failed:
473 ERRNO_SET(EINVAL);
474 return (NULL);
475}
476
477const unsigned char *

--- 57 unchanged lines hidden (view full) ---

535 ERRNO_SET(EINVAL);
536 return (NULL);
537 }
538
539 if (isbe)
540 nvp->nvp_data = be64dec(ptr);
541 else
542 nvp->nvp_data = le64dec(ptr);
667
668 return (ptr);
669failed:
670 ERRNO_SET(EINVAL);
671 return (NULL);
672}
673
674const unsigned char *

--- 57 unchanged lines hidden (view full) ---

732 ERRNO_SET(EINVAL);
733 return (NULL);
734 }
735
736 if (isbe)
737 nvp->nvp_data = be64dec(ptr);
738 else
739 nvp->nvp_data = le64dec(ptr);
740
543 ptr += sizeof(uint64_t);
544 *leftp -= sizeof(uint64_t);
545
546 return (ptr);
547}
548
549const unsigned char *
550nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,

--- 114 unchanged lines hidden (view full) ---

665 *leftp -= nvp->nvp_datasize;
666
667 nvp->nvp_data = (uint64_t)(uintptr_t)value;
668
669 return (ptr);
670}
671
672const unsigned char *
741 ptr += sizeof(uint64_t);
742 *leftp -= sizeof(uint64_t);
743
744 return (ptr);
745}
746
747const unsigned char *
748nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,

--- 114 unchanged lines hidden (view full) ---

863 *leftp -= nvp->nvp_datasize;
864
865 nvp->nvp_data = (uint64_t)(uintptr_t)value;
866
867 return (ptr);
868}
869
870const unsigned char *
871nvpair_unpack_bool_array(bool isbe __unused, nvpair_t *nvp,
872 const unsigned char *ptr, size_t *leftp)
873{
874 uint8_t *value;
875 size_t size;
876 unsigned int i;
877
878 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
879
880 size = sizeof(*value) * nvp->nvp_nitems;
881 if (nvp->nvp_datasize != size || *leftp < size ||
882 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
883 ERRNO_SET(EINVAL);
884 return (NULL);
885 }
886
887 value = nv_malloc(size);
888 if (value == NULL)
889 return (NULL);
890
891 for (i = 0; i < nvp->nvp_nitems; i++) {
892 value[i] = *(const uint8_t *)ptr;
893
894 ptr += sizeof(*value);
895 *leftp -= sizeof(*value);
896 }
897
898 nvp->nvp_data = (uint64_t)(uintptr_t)value;
899
900 return (ptr);
901}
902
903const unsigned char *
904nvpair_unpack_number_array(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
905 size_t *leftp)
906{
907 uint64_t *value;
908 size_t size;
909 unsigned int i;
910
911 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
912
913 size = sizeof(*value) * nvp->nvp_nitems;
914 if (nvp->nvp_datasize != size || *leftp < size ||
915 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
916 ERRNO_SET(EINVAL);
917 return (NULL);
918 }
919
920 value = nv_malloc(size);
921 if (value == NULL)
922 return (NULL);
923
924 for (i = 0; i < nvp->nvp_nitems; i++) {
925 if (isbe)
926 value[i] = be64dec(ptr);
927 else
928 value[i] = le64dec(ptr);
929
930 ptr += sizeof(*value);
931 *leftp -= sizeof(*value);
932 }
933
934 nvp->nvp_data = (uint64_t)(uintptr_t)value;
935
936 return (ptr);
937}
938
939const unsigned char *
940nvpair_unpack_string_array(bool isbe __unused, nvpair_t *nvp,
941 const unsigned char *ptr, size_t *leftp)
942{
943 ssize_t size;
944 size_t len;
945 const char *tmp;
946 char **value;
947 unsigned int ii, j;
948
949 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
950
951 if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0 ||
952 nvp->nvp_nitems == 0) {
953 ERRNO_SET(EINVAL);
954 return (NULL);
955 }
956
957 size = nvp->nvp_datasize;
958 tmp = (const char *)ptr;
959 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
960 len = strnlen(tmp, size - 1) + 1;
961 size -= len;
962 if (size < 0) {
963 ERRNO_SET(EINVAL);
964 return (NULL);
965 }
966 tmp += len;
967 }
968 if (size != 0) {
969 ERRNO_SET(EINVAL);
970 return (NULL);
971 }
972
973 value = nv_malloc(sizeof(*value) * nvp->nvp_nitems);
974 if (value == NULL)
975 return (NULL);
976
977 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
978 value[ii] = nv_strdup((const char *)ptr);
979 if (value[ii] == NULL)
980 goto out;
981 len = strlen(value[ii]) + 1;
982 ptr += len;
983 *leftp -= len;
984 }
985 nvp->nvp_data = (uint64_t)(uintptr_t)value;
986
987 return (ptr);
988out:
989 for (j = 0; j < ii; j++)
990 nv_free(value[j]);
991 nv_free(value);
992 return (NULL);
993}
994
995#ifndef _KERNEL
996const unsigned char *
997nvpair_unpack_descriptor_array(bool isbe, nvpair_t *nvp,
998 const unsigned char *ptr, size_t *leftp, const int *fds, size_t nfds)
999{
1000 int64_t idx;
1001 size_t size;
1002 unsigned int ii;
1003 int *array;
1004
1005 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1006
1007 size = sizeof(idx) * nvp->nvp_nitems;
1008 if (nvp->nvp_datasize != size || *leftp < size ||
1009 nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
1010 ERRNO_SET(EINVAL);
1011 return (NULL);
1012 }
1013
1014 array = (int *)nv_malloc(size);
1015 if (array == NULL)
1016 return (NULL);
1017
1018 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1019 if (isbe)
1020 idx = be64dec(ptr);
1021 else
1022 idx = le64dec(ptr);
1023
1024 if (idx < 0) {
1025 ERRNO_SET(EINVAL);
1026 nv_free(array);
1027 return (NULL);
1028 }
1029
1030 if ((size_t)idx >= nfds) {
1031 ERRNO_SET(EINVAL);
1032 nv_free(array);
1033 return (NULL);
1034 }
1035
1036 array[ii] = (uint64_t)fds[idx];
1037
1038 ptr += sizeof(idx);
1039 *leftp -= sizeof(idx);
1040 }
1041
1042 nvp->nvp_data = (uint64_t)(uintptr_t)array;
1043
1044 return (ptr);
1045}
1046#endif
1047
1048const unsigned char *
1049nvpair_unpack_nvlist_array(bool isbe __unused, nvpair_t *nvp,
1050 const unsigned char *ptr, size_t *leftp, nvlist_t **firstel)
1051{
1052 nvlist_t **value;
1053 nvpair_t *tmpnvp;
1054 unsigned int ii, j;
1055 size_t sizeup;
1056
1057 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1058
1059 sizeup = sizeof(struct nvpair_header) * nvp->nvp_nitems;
1060 if (nvp->nvp_nitems == 0 || sizeup < nvp->nvp_nitems ||
1061 sizeup > *leftp) {
1062 ERRNO_SET(EINVAL);
1063 return (NULL);
1064 }
1065
1066 value = nv_malloc(nvp->nvp_nitems * sizeof(*value));
1067 if (value == NULL)
1068 return (NULL);
1069
1070 for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1071 value[ii] = nvlist_create(0);
1072 if (value[ii] == NULL)
1073 goto fail;
1074 if (ii > 0) {
1075 tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1076 (uint64_t)(uintptr_t)value[ii], 0, 0);
1077 if (tmpnvp == NULL)
1078 goto fail;
1079 nvlist_set_array_next(value[ii - 1], tmpnvp);
1080 }
1081 }
1082 nvlist_set_flags(value[nvp->nvp_nitems - 1], NV_FLAG_IN_ARRAY);
1083
1084 nvp->nvp_data = (uint64_t)(uintptr_t)value;
1085 *firstel = value[0];
1086
1087 return (ptr);
1088fail:
1089 ERRNO_SAVE();
1090 for (j = 0; j < ii; j++)
1091 nvlist_destroy(value[j]);
1092 nv_free(value);
1093 ERRNO_RESTORE();
1094
1095 return (NULL);
1096}
1097
1098const unsigned char *
673nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
674 nvpair_t **nvpp)
675{
676 nvpair_t *nvp, *tmp;
677
678 nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
679 if (nvp == NULL)
680 return (NULL);

--- 31 unchanged lines hidden (view full) ---

712nvpair_name(const nvpair_t *nvp)
713{
714
715 NVPAIR_ASSERT(nvp);
716
717 return (nvp->nvp_name);
718}
719
1099nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
1100 nvpair_t **nvpp)
1101{
1102 nvpair_t *nvp, *tmp;
1103
1104 nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
1105 if (nvp == NULL)
1106 return (NULL);

--- 31 unchanged lines hidden (view full) ---

1138nvpair_name(const nvpair_t *nvp)
1139{
1140
1141 NVPAIR_ASSERT(nvp);
1142
1143 return (nvp->nvp_name);
1144}
1145
720static nvpair_t *
721nvpair_allocv(const char *name, int type, uint64_t data, size_t datasize)
722{
723 nvpair_t *nvp;
724 size_t namelen;
725
726 PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
727
728 namelen = strlen(name);
729 if (namelen >= NV_NAME_MAX) {
730 ERRNO_SET(ENAMETOOLONG);
731 return (NULL);
732 }
733
734 nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
735 if (nvp != NULL) {
736 nvp->nvp_name = (char *)(nvp + 1);
737 memcpy(nvp->nvp_name, name, namelen);
738 nvp->nvp_name[namelen] = '\0';
739 nvp->nvp_type = type;
740 nvp->nvp_data = data;
741 nvp->nvp_datasize = datasize;
742 nvp->nvp_magic = NVPAIR_MAGIC;
743 }
744
745 return (nvp);
746}
747
748nvpair_t *
749nvpair_create_stringf(const char *name, const char *valuefmt, ...)
750{
751 va_list valueap;
752 nvpair_t *nvp;
753
754 va_start(valueap, valuefmt);
755 nvp = nvpair_create_stringv(name, valuefmt, valueap);

--- 17 unchanged lines hidden (view full) ---

773 nv_free(str);
774 return (nvp);
775}
776
777nvpair_t *
778nvpair_create_null(const char *name)
779{
780
1146nvpair_t *
1147nvpair_create_stringf(const char *name, const char *valuefmt, ...)
1148{
1149 va_list valueap;
1150 nvpair_t *nvp;
1151
1152 va_start(valueap, valuefmt);
1153 nvp = nvpair_create_stringv(name, valuefmt, valueap);

--- 17 unchanged lines hidden (view full) ---

1171 nv_free(str);
1172 return (nvp);
1173}
1174
1175nvpair_t *
1176nvpair_create_null(const char *name)
1177{
1178
781 return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0));
1179 return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0, 0));
782}
783
784nvpair_t *
785nvpair_create_bool(const char *name, bool value)
786{
787
788 return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
1180}
1181
1182nvpair_t *
1183nvpair_create_bool(const char *name, bool value)
1184{
1185
1186 return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
789 sizeof(uint8_t)));
1187 sizeof(uint8_t), 0));
790}
791
792nvpair_t *
793nvpair_create_number(const char *name, uint64_t value)
794{
795
1188}
1189
1190nvpair_t *
1191nvpair_create_number(const char *name, uint64_t value)
1192{
1193
796 return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value)));
1194 return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value), 0));
797}
798
799nvpair_t *
800nvpair_create_string(const char *name, const char *value)
801{
802 nvpair_t *nvp;
803 size_t size;
804 char *data;

--- 4 unchanged lines hidden (view full) ---

809 }
810
811 data = nv_strdup(value);
812 if (data == NULL)
813 return (NULL);
814 size = strlen(value) + 1;
815
816 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
1195}
1196
1197nvpair_t *
1198nvpair_create_string(const char *name, const char *value)
1199{
1200 nvpair_t *nvp;
1201 size_t size;
1202 char *data;

--- 4 unchanged lines hidden (view full) ---

1207 }
1208
1209 data = nv_strdup(value);
1210 if (data == NULL)
1211 return (NULL);
1212 size = strlen(value) + 1;
1213
1214 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
817 size);
1215 size, 0);
818 if (nvp == NULL)
819 nv_free(data);
820
821 return (nvp);
822}
823
824nvpair_t *
825nvpair_create_nvlist(const char *name, const nvlist_t *value)

--- 5 unchanged lines hidden (view full) ---

831 ERRNO_SET(EINVAL);
832 return (NULL);
833 }
834
835 nvl = nvlist_clone(value);
836 if (nvl == NULL)
837 return (NULL);
838
1216 if (nvp == NULL)
1217 nv_free(data);
1218
1219 return (nvp);
1220}
1221
1222nvpair_t *
1223nvpair_create_nvlist(const char *name, const nvlist_t *value)

--- 5 unchanged lines hidden (view full) ---

1229 ERRNO_SET(EINVAL);
1230 return (NULL);
1231 }
1232
1233 nvl = nvlist_clone(value);
1234 if (nvl == NULL)
1235 return (NULL);
1236
839 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0);
1237 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0,
1238 0);
840 if (nvp == NULL)
841 nvlist_destroy(nvl);
842 else
843 nvlist_set_parent(nvl, nvp);
844
845 return (nvp);
846}
847

--- 8 unchanged lines hidden (view full) ---

856 return (NULL);
857 }
858
859 value = fcntl(value, F_DUPFD_CLOEXEC, 0);
860 if (value < 0)
861 return (NULL);
862
863 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1239 if (nvp == NULL)
1240 nvlist_destroy(nvl);
1241 else
1242 nvlist_set_parent(nvl, nvp);
1243
1244 return (nvp);
1245}
1246

--- 8 unchanged lines hidden (view full) ---

1255 return (NULL);
1256 }
1257
1258 value = fcntl(value, F_DUPFD_CLOEXEC, 0);
1259 if (value < 0)
1260 return (NULL);
1261
1262 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
864 sizeof(int64_t));
1263 sizeof(int64_t), 0);
865 if (nvp == NULL) {
866 ERRNO_SAVE();
867 close(value);
868 ERRNO_RESTORE();
869 }
870
871 return (nvp);
872}

--- 11 unchanged lines hidden (view full) ---

884 }
885
886 data = nv_malloc(size);
887 if (data == NULL)
888 return (NULL);
889 memcpy(data, value, size);
890
891 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
1264 if (nvp == NULL) {
1265 ERRNO_SAVE();
1266 close(value);
1267 ERRNO_RESTORE();
1268 }
1269
1270 return (nvp);
1271}

--- 11 unchanged lines hidden (view full) ---

1283 }
1284
1285 data = nv_malloc(size);
1286 if (data == NULL)
1287 return (NULL);
1288 memcpy(data, value, size);
1289
1290 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
892 size);
1291 size, 0);
893 if (nvp == NULL)
894 nv_free(data);
895
896 return (nvp);
897}
898
899nvpair_t *
1292 if (nvp == NULL)
1293 nv_free(data);
1294
1295 return (nvp);
1296}
1297
1298nvpair_t *
1299nvpair_create_bool_array(const char *name, const bool *value, size_t nitems)
1300{
1301 nvpair_t *nvp;
1302 size_t size;
1303 void *data;
1304
1305 if (value == NULL || nitems == 0) {
1306 ERRNO_SET(EINVAL);
1307 return (NULL);
1308 }
1309
1310 size = sizeof(value[0]) * nitems;
1311 data = nv_malloc(size);
1312 if (data == NULL)
1313 return (NULL);
1314
1315 memcpy(data, value, size);
1316 nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data,
1317 size, nitems);
1318 if (nvp == NULL) {
1319 ERRNO_SAVE();
1320 nv_free(data);
1321 ERRNO_RESTORE();
1322 }
1323
1324 return (nvp);
1325}
1326
1327nvpair_t *
1328nvpair_create_number_array(const char *name, const uint64_t *value,
1329 size_t nitems)
1330{
1331 nvpair_t *nvp;
1332 size_t size;
1333 void *data;
1334
1335 if (value == NULL || nitems == 0) {
1336 ERRNO_SET(EINVAL);
1337 return (NULL);
1338 }
1339
1340 size = sizeof(value[0]) * nitems;
1341 data = nv_malloc(size);
1342 if (data == NULL)
1343 return (NULL);
1344
1345 memcpy(data, value, size);
1346 nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1347 (uint64_t)(uintptr_t)data, size, nitems);
1348 if (nvp == NULL) {
1349 ERRNO_SAVE();
1350 nv_free(data);
1351 ERRNO_RESTORE();
1352 }
1353
1354 return (nvp);
1355}
1356
1357nvpair_t *
1358nvpair_create_string_array(const char *name, const char * const *value,
1359 size_t nitems)
1360{
1361 nvpair_t *nvp;
1362 unsigned int ii;
1363 size_t datasize, size;
1364 char **data;
1365
1366 if (value == NULL || nitems == 0) {
1367 ERRNO_SET(EINVAL);
1368 return (NULL);
1369 }
1370
1371 nvp = NULL;
1372 datasize = 0;
1373 data = nv_malloc(sizeof(value[0]) * nitems);
1374 if (data == NULL)
1375 return (NULL);
1376
1377 for (ii = 0; ii < nitems; ii++) {
1378 if (value[ii] == NULL) {
1379 ERRNO_SET(EINVAL);
1380 goto fail;
1381 }
1382
1383 size = strlen(value[ii]) + 1;
1384 datasize += size;
1385 data[ii] = nv_strdup(value[ii]);
1386 if (data[ii] == NULL)
1387 goto fail;
1388 }
1389 nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1390 (uint64_t)(uintptr_t)data, datasize, nitems);
1391
1392fail:
1393 if (nvp == NULL) {
1394 ERRNO_SAVE();
1395 for (; ii > 0; ii--)
1396 nv_free(data[ii - 1]);
1397 nv_free(data);
1398 ERRNO_RESTORE();
1399 }
1400
1401 return (nvp);
1402}
1403
1404nvpair_t *
1405nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value,
1406 size_t nitems)
1407{
1408 unsigned int ii;
1409 nvlist_t **nvls;
1410 nvpair_t *nvp;
1411 int flags;
1412
1413 nvp = NULL;
1414 nvls = NULL;
1415 ii = 0;
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 nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1438 (uint64_t)(uintptr_t)nvls[ii], 0, 0);
1439 if (nvp == NULL)
1440 goto fail;
1441 nvlist_set_array_next(nvls[ii - 1], nvp);
1442 }
1443 }
1444 flags = nvlist_flags(nvls[nitems - 1]) | NV_FLAG_IN_ARRAY;
1445 nvlist_set_flags(nvls[nitems - 1], flags);
1446
1447 nvp = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1448 (uint64_t)(uintptr_t)nvls, 0, nitems);
1449
1450fail:
1451 if (nvp == NULL) {
1452 ERRNO_SAVE();
1453 for (; ii > 0; ii--)
1454 nvlist_destroy(nvls[ii - 1]);
1455
1456 nv_free(nvls);
1457 ERRNO_RESTORE();
1458 } else {
1459 for (ii = 0; ii < nitems; ii++)
1460 nvlist_set_parent(nvls[ii], nvp);
1461 }
1462
1463 return (nvp);
1464}
1465
1466#ifndef _KERNEL
1467nvpair_t *
1468nvpair_create_descriptor_array(const char *name, const int *value,
1469 size_t nitems)
1470{
1471 unsigned int ii;
1472 nvpair_t *nvp;
1473 int *fds;
1474
1475 if (value == NULL) {
1476 ERRNO_SET(EINVAL);
1477 return (NULL);
1478 }
1479
1480 nvp = NULL;
1481
1482 fds = nv_malloc(sizeof(value[0]) * nitems);
1483 if (fds == NULL)
1484 return (NULL);
1485 for (ii = 0; ii < nitems; ii++) {
1486 if (value[ii] == -1) {
1487 fds[ii] = -1;
1488 } else {
1489 if (!fd_is_valid(value[ii])) {
1490 ERRNO_SET(EBADF);
1491 goto fail;
1492 }
1493
1494 fds[ii] = fcntl(value[ii], F_DUPFD_CLOEXEC, 0);
1495 if (fds[ii] == -1)
1496 goto fail;
1497 }
1498 }
1499
1500 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1501 (uint64_t)(uintptr_t)fds, sizeof(int64_t) * nitems, nitems);
1502
1503fail:
1504 if (nvp == NULL) {
1505 ERRNO_SAVE();
1506 for (; ii > 0; ii--) {
1507 if (fds[ii - 1] != -1)
1508 close(fds[ii - 1]);
1509 }
1510 nv_free(fds);
1511 ERRNO_RESTORE();
1512 }
1513
1514 return (nvp);
1515}
1516#endif
1517
1518nvpair_t *
900nvpair_move_string(const char *name, char *value)
901{
902 nvpair_t *nvp;
903
904 if (value == NULL) {
905 ERRNO_SET(EINVAL);
906 return (NULL);
907 }
908
909 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
1519nvpair_move_string(const char *name, char *value)
1520{
1521 nvpair_t *nvp;
1522
1523 if (value == NULL) {
1524 ERRNO_SET(EINVAL);
1525 return (NULL);
1526 }
1527
1528 nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
910 strlen(value) + 1);
1529 strlen(value) + 1, 0);
911 if (nvp == NULL) {
912 ERRNO_SAVE();
913 nv_free(value);
914 ERRNO_RESTORE();
915 }
916
917 return (nvp);
918}

--- 10 unchanged lines hidden (view full) ---

929
930 if (nvlist_error(value) != 0) {
931 ERRNO_SET(nvlist_error(value));
932 nvlist_destroy(value);
933 return (NULL);
934 }
935
936 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
1530 if (nvp == NULL) {
1531 ERRNO_SAVE();
1532 nv_free(value);
1533 ERRNO_RESTORE();
1534 }
1535
1536 return (nvp);
1537}

--- 10 unchanged lines hidden (view full) ---

1548
1549 if (nvlist_error(value) != 0) {
1550 ERRNO_SET(nvlist_error(value));
1551 nvlist_destroy(value);
1552 return (NULL);
1553 }
1554
1555 nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
937 0);
1556 0, 0);
938 if (nvp == NULL)
939 nvlist_destroy(value);
940 else
941 nvlist_set_parent(value, nvp);
942
943 return (nvp);
944}
945

--- 4 unchanged lines hidden (view full) ---

950 nvpair_t *nvp;
951
952 if (value < 0 || !fd_is_valid(value)) {
953 ERRNO_SET(EBADF);
954 return (NULL);
955 }
956
957 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1557 if (nvp == NULL)
1558 nvlist_destroy(value);
1559 else
1560 nvlist_set_parent(value, nvp);
1561
1562 return (nvp);
1563}
1564

--- 4 unchanged lines hidden (view full) ---

1569 nvpair_t *nvp;
1570
1571 if (value < 0 || !fd_is_valid(value)) {
1572 ERRNO_SET(EBADF);
1573 return (NULL);
1574 }
1575
1576 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
958 sizeof(int64_t));
1577 sizeof(int64_t), 0);
959 if (nvp == NULL) {
960 ERRNO_SAVE();
961 close(value);
962 ERRNO_RESTORE();
963 }
964
965 return (nvp);
966}

--- 5 unchanged lines hidden (view full) ---

972 nvpair_t *nvp;
973
974 if (value == NULL || size == 0) {
975 ERRNO_SET(EINVAL);
976 return (NULL);
977 }
978
979 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
1578 if (nvp == NULL) {
1579 ERRNO_SAVE();
1580 close(value);
1581 ERRNO_RESTORE();
1582 }
1583
1584 return (nvp);
1585}

--- 5 unchanged lines hidden (view full) ---

1591 nvpair_t *nvp;
1592
1593 if (value == NULL || size == 0) {
1594 ERRNO_SET(EINVAL);
1595 return (NULL);
1596 }
1597
1598 nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
980 size);
1599 size, 0);
981 if (nvp == NULL) {
982 ERRNO_SAVE();
983 nv_free(value);
984 ERRNO_RESTORE();
985 }
986
987 return (nvp);
988}
989
1600 if (nvp == NULL) {
1601 ERRNO_SAVE();
1602 nv_free(value);
1603 ERRNO_RESTORE();
1604 }
1605
1606 return (nvp);
1607}
1608
1609nvpair_t *
1610nvpair_move_bool_array(const char *name, bool *value, size_t nitems)
1611{
1612 nvpair_t *nvp;
1613
1614 if (value == NULL || nitems == 0) {
1615 ERRNO_SET(EINVAL);
1616 return (NULL);
1617 }
1618
1619 nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY,
1620 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1621 if (nvp == NULL) {
1622 ERRNO_SAVE();
1623 nv_free(value);
1624 ERRNO_RESTORE();
1625 }
1626
1627 return (nvp);
1628}
1629
1630nvpair_t *
1631nvpair_move_string_array(const char *name, char **value, size_t nitems)
1632{
1633 nvpair_t *nvp;
1634 size_t i, size;
1635
1636 if (value == NULL || nitems == 0) {
1637 ERRNO_SET(EINVAL);
1638 return (NULL);
1639 }
1640
1641 size = 0;
1642 for (i = 0; i < nitems; i++) {
1643 if (value[i] == NULL) {
1644 ERRNO_SET(EINVAL);
1645 return (NULL);
1646 }
1647
1648 size += strlen(value[i]) + 1;
1649 }
1650
1651 nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1652 (uint64_t)(uintptr_t)value, size, nitems);
1653 if (nvp == NULL) {
1654 ERRNO_SAVE();
1655 for (i = 0; i < nitems; i++)
1656 nv_free(value[i]);
1657 nv_free(value);
1658 ERRNO_RESTORE();
1659 }
1660
1661 return (nvp);
1662}
1663
1664nvpair_t *
1665nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems)
1666{
1667 nvpair_t *nvp;
1668
1669 if (value == NULL || nitems == 0) {
1670 ERRNO_SET(EINVAL);
1671 return (NULL);
1672 }
1673
1674 nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1675 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1676 if (nvp == NULL) {
1677 ERRNO_SAVE();
1678 nv_free(value);
1679 ERRNO_RESTORE();
1680 }
1681
1682 return (nvp);
1683}
1684
1685nvpair_t *
1686nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems)
1687{
1688 unsigned int ii;
1689 nvpair_t *nvp;
1690 int flags;
1691
1692 nvp = NULL;
1693 if (value == NULL || nitems == 0) {
1694 ERRNO_SET(EINVAL);
1695 return (NULL);
1696 }
1697
1698 for (ii = 0; ii < nitems; ii++) {
1699 if (value == NULL || nvlist_error(value[ii]) != 0 ||
1700 nvlist_get_pararr(value[ii], NULL) != NULL) {
1701 ERRNO_SET(EINVAL);
1702 goto fail;
1703 }
1704 if (ii > 0) {
1705 nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1706 (uint64_t)(uintptr_t)value[ii], 0, 0);
1707 if (nvp == NULL)
1708 goto fail;
1709 nvlist_set_array_next(value[ii - 1], nvp);
1710 }
1711 }
1712 flags = nvlist_flags(value[nitems - 1]) | NV_FLAG_IN_ARRAY;
1713 nvlist_set_flags(value[nitems - 1], flags);
1714
1715 nvp = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1716 (uint64_t)(uintptr_t)value, 0, nitems);
1717fail:
1718 if (nvp == NULL) {
1719 ERRNO_SAVE();
1720 for (ii = 0; ii < nitems; ii++) {
1721 if (value[ii] != NULL &&
1722 nvlist_get_pararr(value[ii], NULL) != NULL) {
1723 nvlist_destroy(value[ii]);
1724 }
1725 nv_free(value);
1726 }
1727 ERRNO_RESTORE();
1728 } else {
1729 for (ii = 0; ii < nitems; ii++)
1730 nvlist_set_parent(value[ii], nvp);
1731 }
1732
1733 return (nvp);
1734}
1735
1736#ifndef _KERNEL
1737nvpair_t *
1738nvpair_move_descriptor_array(const char *name, int *value, size_t nitems)
1739{
1740 nvpair_t *nvp;
1741 size_t i;
1742
1743 nvp = NULL;
1744 if (value == NULL || nitems == 0) {
1745 ERRNO_SET(EINVAL);
1746 return (NULL);
1747 }
1748
1749 for (i = 0; i < nitems; i++) {
1750 if (value[i] != -1 && !fd_is_valid(value[i])) {
1751 ERRNO_SET(EBADF);
1752 goto fail;
1753 }
1754 }
1755
1756 nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1757 (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1758
1759fail:
1760 if (nvp == NULL) {
1761 ERRNO_SAVE();
1762 for (i = 0; i < nitems; i++) {
1763 if (fd_is_valid(value[i]))
1764 close(value[i]);
1765 }
1766 nv_free(value);
1767 ERRNO_RESTORE();
1768 }
1769
1770 return (nvp);
1771}
1772#endif
1773
990bool
991nvpair_get_bool(const nvpair_t *nvp)
992{
993
994 NVPAIR_ASSERT(nvp);
995
996 return (nvp->nvp_data == 1);
997}

--- 43 unchanged lines hidden (view full) ---

1041nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
1042{
1043
1044 NVPAIR_ASSERT(nvp);
1045 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
1046
1047 if (sizep != NULL)
1048 *sizep = nvp->nvp_datasize;
1774bool
1775nvpair_get_bool(const nvpair_t *nvp)
1776{
1777
1778 NVPAIR_ASSERT(nvp);
1779
1780 return (nvp->nvp_data == 1);
1781}

--- 43 unchanged lines hidden (view full) ---

1825nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
1826{
1827
1828 NVPAIR_ASSERT(nvp);
1829 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
1830
1831 if (sizep != NULL)
1832 *sizep = nvp->nvp_datasize;
1833
1049 return ((const void *)(intptr_t)nvp->nvp_data);
1050}
1051
1834 return ((const void *)(intptr_t)nvp->nvp_data);
1835}
1836
1837const bool *
1838nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitems)
1839{
1840
1841 NVPAIR_ASSERT(nvp);
1842 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1843
1844 if (nitems != NULL)
1845 *nitems = nvp->nvp_nitems;
1846
1847 return ((const bool *)(intptr_t)nvp->nvp_data);
1848}
1849
1850const uint64_t *
1851nvpair_get_number_array(const nvpair_t *nvp, size_t *nitems)
1852{
1853
1854 NVPAIR_ASSERT(nvp);
1855 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1856
1857 if (nitems != NULL)
1858 *nitems = nvp->nvp_nitems;
1859
1860 return ((const uint64_t *)(intptr_t)nvp->nvp_data);
1861}
1862
1863const char * const *
1864nvpair_get_string_array(const nvpair_t *nvp, size_t *nitems)
1865{
1866
1867 NVPAIR_ASSERT(nvp);
1868 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1869
1870 if (nitems != NULL)
1871 *nitems = nvp->nvp_nitems;
1872
1873 return ((const char * const *)(intptr_t)nvp->nvp_data);
1874}
1875
1876const nvlist_t * const *
1877nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitems)
1878{
1879
1880 NVPAIR_ASSERT(nvp);
1881 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1882
1883 if (nitems != NULL)
1884 *nitems = nvp->nvp_nitems;
1885
1886 return ((const nvlist_t * const *)((intptr_t)nvp->nvp_data));
1887}
1888
1889#ifndef _KERNEL
1890const int *
1891nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitems)
1892{
1893
1894 NVPAIR_ASSERT(nvp);
1895 PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1896
1897 if (nitems != NULL)
1898 *nitems = nvp->nvp_nitems;
1899
1900 return ((const int *)(intptr_t)nvp->nvp_data);
1901}
1902#endif
1903
1052void
1053nvpair_free(nvpair_t *nvp)
1054{
1904void
1905nvpair_free(nvpair_t *nvp)
1906{
1907 size_t i;
1055
1056 NVPAIR_ASSERT(nvp);
1057 PJDLOG_ASSERT(nvp->nvp_list == NULL);
1058
1059 nvp->nvp_magic = 0;
1060 switch (nvp->nvp_type) {
1061#ifndef _KERNEL
1062 case NV_TYPE_DESCRIPTOR:
1063 close((int)nvp->nvp_data);
1064 break;
1908
1909 NVPAIR_ASSERT(nvp);
1910 PJDLOG_ASSERT(nvp->nvp_list == NULL);
1911
1912 nvp->nvp_magic = 0;
1913 switch (nvp->nvp_type) {
1914#ifndef _KERNEL
1915 case NV_TYPE_DESCRIPTOR:
1916 close((int)nvp->nvp_data);
1917 break;
1918 case NV_TYPE_DESCRIPTOR_ARRAY:
1919 for (i = 0; i < nvp->nvp_nitems; i++)
1920 close(((int *)(intptr_t)nvp->nvp_data)[i]);
1921 break;
1065#endif
1066 case NV_TYPE_NVLIST:
1067 nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
1068 break;
1069 case NV_TYPE_STRING:
1070 nv_free((char *)(intptr_t)nvp->nvp_data);
1071 break;
1072 case NV_TYPE_BINARY:
1073 nv_free((void *)(intptr_t)nvp->nvp_data);
1074 break;
1922#endif
1923 case NV_TYPE_NVLIST:
1924 nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
1925 break;
1926 case NV_TYPE_STRING:
1927 nv_free((char *)(intptr_t)nvp->nvp_data);
1928 break;
1929 case NV_TYPE_BINARY:
1930 nv_free((void *)(intptr_t)nvp->nvp_data);
1931 break;
1932 case NV_TYPE_NVLIST_ARRAY:
1933 for (i = 0; i < nvp->nvp_nitems; i++) {
1934 nvlist_destroy(
1935 ((nvlist_t **)(intptr_t)nvp->nvp_data)[i]);
1936 }
1937 nv_free(((nvlist_t **)(intptr_t)nvp->nvp_data));
1938 break;
1939 case NV_TYPE_NUMBER_ARRAY:
1940 nv_free((uint64_t *)(intptr_t)nvp->nvp_data);
1941 break;
1942 case NV_TYPE_BOOL_ARRAY:
1943 nv_free((bool *)(intptr_t)nvp->nvp_data);
1944 break;
1945 case NV_TYPE_STRING_ARRAY:
1946 for (i = 0; i < nvp->nvp_nitems; i++)
1947 nv_free(((char **)(intptr_t)nvp->nvp_data)[i]);
1948 break;
1075 }
1076 nv_free(nvp);
1077}
1078
1079void
1080nvpair_free_structure(nvpair_t *nvp)
1081{
1082

--- 18 unchanged lines hidden (view full) ---

1101 case NV_TYPE_STRING:
1102 return ("STRING");
1103 case NV_TYPE_NVLIST:
1104 return ("NVLIST");
1105 case NV_TYPE_DESCRIPTOR:
1106 return ("DESCRIPTOR");
1107 case NV_TYPE_BINARY:
1108 return ("BINARY");
1949 }
1950 nv_free(nvp);
1951}
1952
1953void
1954nvpair_free_structure(nvpair_t *nvp)
1955{
1956

--- 18 unchanged lines hidden (view full) ---

1975 case NV_TYPE_STRING:
1976 return ("STRING");
1977 case NV_TYPE_NVLIST:
1978 return ("NVLIST");
1979 case NV_TYPE_DESCRIPTOR:
1980 return ("DESCRIPTOR");
1981 case NV_TYPE_BINARY:
1982 return ("BINARY");
1983 case NV_TYPE_BOOL_ARRAY:
1984 return ("BOOL ARRAY");
1985 case NV_TYPE_NUMBER_ARRAY:
1986 return ("NUMBER ARRAY");
1987 case NV_TYPE_STRING_ARRAY:
1988 return ("STRING ARRAY");
1989 case NV_TYPE_NVLIST_ARRAY:
1990 return ("NVLIST ARRAY");
1991 case NV_TYPE_DESCRIPTOR_ARRAY:
1992 return ("DESCRIPTOR ARRAY");
1109 default:
1110 return ("<UNKNOWN>");
1111 }
1112}
1113
1993 default:
1994 return ("<UNKNOWN>");
1995 }
1996}
1997