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