2 .\" Copyright (c) 2013 The FreeBSD Foundation
3 .\" All rights reserved.
5 .\" This documentation was written by Pawel Jakub Dawidek under sponsorship
6 .\" the FreeBSD Foundation.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
31 .Dd September 25, 2014
55 .Nd "library for name/value pairs"
61 .Fn nvlist_create "int flags"
63 .Fn nvlist_destroy "nvlist_t *nvl"
65 .Fn nvlist_error "const nvlist_t *nvl"
67 .Fn nvlist_empty "const nvlist_t *nvl"
70 .Fn nvlist_clone "const nvlist_t *nvl"
73 .Fn nvlist_dump "const nvlist_t *nvl, int fd"
75 .Fn nvlist_fdump "const nvlist_t *nvl, FILE *fp"
78 .Fn nvlist_size "const nvlist_t *nvl"
80 .Fn nvlist_pack "const nvlist_t *nvl" "size_t *sizep"
82 .Fn nvlist_unpack "const void *buf" "size_t size"
85 .Fn nvlist_send "int sock" "const nvlist_t *nvl"
87 .Fn nvlist_recv "int sock"
89 .Fn nvlist_xfer "int sock" "nvlist_t *nvl"
92 .Fn nvlist_next "const nvlist_t *nvl" "int *typep" "void **cookiep"
95 .Fn nvlist_exists "const nvlist_t *nvl" "const char *name"
97 .Fn nvlist_exists_type "const nvlist_t *nvl" "const char *name" "int type"
99 .Fn nvlist_exists_null "const nvlist_t *nvl" "const char *name"
101 .Fn nvlist_exists_bool "const nvlist_t *nvl" "const char *name"
103 .Fn nvlist_exists_number "const nvlist_t *nvl" "const char *name"
105 .Fn nvlist_exists_string "const nvlist_t *nvl" "const char *name"
107 .Fn nvlist_exists_nvlist "const nvlist_t *nvl" "const char *name"
109 .Fn nvlist_exists_descriptor "const nvlist_t *nvl" "const char *name"
111 .Fn nvlist_exists_binary "const nvlist_t *nvl" "const char *name"
114 .Fn nvlist_add_null "nvlist_t *nvl" "const char *name"
116 .Fn nvlist_add_bool "nvlist_t *nvl" "const char *name" "bool value"
118 .Fn nvlist_add_number "nvlist_t *nvl" "const char *name" "uint64_t value"
120 .Fn nvlist_add_string "nvlist_t *nvl" "const char *name" "const char *value"
122 .Fn nvlist_add_stringf "nvlist_t *nvl" "const char *name" "const char *valuefmt" "..."
124 .Fn nvlist_add_stringv "nvlist_t *nvl" "const char *name" "const char *valuefmt" "va_list valueap"
126 .Fn nvlist_add_nvlist "nvlist_t *nvl" "const char *name" "const nvlist_t *value"
128 .Fn nvlist_add_descriptor "nvlist_t *nvl" "const char *name" "int value"
130 .Fn nvlist_add_binary "nvlist_t *nvl" "const char *name" "const void *value" "size_t size"
133 .Fn nvlist_move_string "nvlist_t *nvl" "const char *name" "char *value"
135 .Fn nvlist_move_nvlist "nvlist_t *nvl" "const char *name" "nvlist_t *value"
137 .Fn nvlist_move_descriptor "nvlist_t *nvl" "const char *name" "int value"
139 .Fn nvlist_move_binary "nvlist_t *nvl" "const char *name" "void *value" "size_t size"
142 .Fn nvlist_get_bool "const nvlist_t *nvl" "const char *name"
144 .Fn nvlist_get_number "const nvlist_t *nvl" "const char *name"
146 .Fn nvlist_get_string "const nvlist_t *nvl" "const char *name"
147 .Ft "const nvlist_t *"
148 .Fn nvlist_get_nvlist "const nvlist_t *nvl" "const char *name"
150 .Fn nvlist_get_descriptor "const nvlist_t *nvl" "const char *name"
152 .Fn nvlist_get_binary "const nvlist_t *nvl" "const char *name" "size_t *sizep"
153 .Ft "const nvlist_t *"
154 .Fn nvlist_get_parent "const nvlist_t *nvl"
157 .Fn nvlist_take_bool "nvlist_t *nvl" "const char *name"
159 .Fn nvlist_take_number "nvlist_t *nvl" "const char *name"
161 .Fn nvlist_take_string "nvlist_t *nvl" "const char *name"
163 .Fn nvlist_take_nvlist "nvlist_t *nvl" "const char *name"
165 .Fn nvlist_take_descriptor "nvlist_t *nvl" "const char *name"
167 .Fn nvlist_take_binary "nvlist_t *nvl" "const char *name" "size_t *sizep"
170 .Fn nvlist_free "nvlist_t *nvl" "const char *name"
172 .Fn nvlist_free_type "nvlist_t *nvl" "const char *name" "int type"
175 .Fn nvlist_free_null "nvlist_t *nvl" "const char *name"
177 .Fn nvlist_free_bool "nvlist_t *nvl" "const char *name"
179 .Fn nvlist_free_number "nvlist_t *nvl" "const char *name"
181 .Fn nvlist_free_string "nvlist_t *nvl" "const char *name"
183 .Fn nvlist_free_nvlist "nvlist_t *nvl" "const char *name"
185 .Fn nvlist_free_descriptor "nvlist_t *nvl" "const char *name"
187 .Fn nvlist_free_binary "nvlist_t *nvl" "const char *name"
191 library allows to easily manage name value pairs as well as send and receive
193 A group (list) of name value pairs is called an
195 The API supports the following data types:
196 .Bl -ohang -offset indent
197 .It Sy null ( NV_TYPE_NULL )
198 There is no data associated with the name.
199 .It Sy bool ( NV_TYPE_BOOL )
200 The value can be either
204 .It Sy number ( NV_TYPE_NUMBER )
205 The value is a number stored as
207 .It Sy string ( NV_TYPE_STRING )
208 The value is a C string.
209 .It Sy nvlist ( NV_TYPE_NVLIST )
210 The value is a nested nvlist.
211 .It Sy descriptor ( NV_TYPE_DESCRIPTOR )
212 The value is a file descriptor.
213 Note that file descriptors can be sent only over
216 .It Sy binary ( NV_TYPE_BINARY )
217 The value is a binary buffer.
222 function allocates memory and initializes an nvlist.
224 The following flag can be provided:
226 .Bl -tag -width "NV_FLAG_IGNORE_CASE" -compact -offset indent
227 .It Dv NV_FLAG_IGNORE_CASE
228 Perform case-insensitive lookups of provided names.
233 function destroys the given nvlist.
234 Function does nothing if
237 Function never modifies the
243 function returns any error value that the nvlist accumulated.
244 If the given nvlist is
248 error will be returned.
254 if the given nvlist is empty and
257 The nvlist must not be in error state.
261 functions clones the given nvlist.
262 The clone shares no resources with its origin.
263 This also means that all file descriptors that are part of the nvlist will be
266 system call before placing them in the clone.
270 dumps nvlist content for debugging purposes to the given file descriptor
275 dumps nvlist content for debugging purposes to the given file stream
280 function returns the size of the given nvlist after converting it to binary
287 function converts the given nvlist to a binary buffer.
288 The function allocates memory for the buffer, which should be freed with the
295 the size of the buffer will be stored there.
298 in case of an error (allocation failure).
299 If the nvlist contains any file descriptors
302 The nvlist must not be in error state.
306 function converts the given buffer to the nvlist.
313 function sends the given nvlist over the socket given by the
316 Note that nvlist that contains file descriptors can only be send over
322 function receives nvlist over the socket given by the
328 function sends the given nvlist over the socket given by the
330 argument and receives nvlist over the same socket.
331 The given nvlist is always destroyed.
335 function iterates over the given nvlist returning names and types of subsequent
339 argument allows the function to figure out which element should be returned
345 for the first call and should not be changed later.
348 means there are no more elements on the nvlist.
351 argument can be NULL.
352 Elements may not be removed from the nvlist while traversing it.
353 The nvlist must not be in error state.
359 if element of the given name exists (besides of its type) or
362 The nvlist must not be in error state.
365 .Fn nvlist_exists_type
368 if element of the given name and the given type exists or
371 The nvlist must not be in error state.
374 .Fn nvlist_exists_null ,
375 .Fn nvlist_exists_bool ,
376 .Fn nvlist_exists_number ,
377 .Fn nvlist_exists_string ,
378 .Fn nvlist_exists_nvlist ,
379 .Fn nvlist_exists_descriptor ,
380 .Fn nvlist_exists_binary
383 if element of the given name and the given type determined by the function name
387 The nvlist must not be in error state.
390 .Fn nvlist_add_null ,
391 .Fn nvlist_add_bool ,
392 .Fn nvlist_add_number ,
393 .Fn nvlist_add_string ,
394 .Fn nvlist_add_stringf ,
395 .Fn nvlist_add_stringv ,
396 .Fn nvlist_add_nvlist ,
397 .Fn nvlist_add_descriptor ,
398 .Fn nvlist_add_binary
399 functions add element to the given nvlist.
400 When adding string or binary buffor the functions will allocate memory
401 and copy the data over.
402 When adding nvlist, the nvlist will be cloned and clone will be added.
403 When adding descriptor, the descriptor will be duplicated using the
405 system call and the new descriptor will be added.
406 If an error occurs while adding new element, internal error is set which can be
412 .Fn nvlist_move_string ,
413 .Fn nvlist_move_nvlist ,
414 .Fn nvlist_move_descriptor ,
415 .Fn nvlist_move_binary
416 functions add new element to the given nvlist, but unlike
417 .Fn nvlist_add_<type>
418 functions they will consume the given resource.
419 If an error occurs while adding new element, the resource is destroyed and
420 internal error is set which can be examined using the
425 .Fn nvlist_get_bool ,
426 .Fn nvlist_get_number ,
427 .Fn nvlist_get_string ,
428 .Fn nvlist_get_nvlist ,
429 .Fn nvlist_get_descriptor ,
430 .Fn nvlist_get_binary
431 functions allow to obtain value of the given name.
432 In case of string, nvlist, descriptor or binary, returned resource should
433 not be modified - it still belongs to the nvlist.
434 If element of the given name does not exist, the program will be aborted.
435 To avoid that the caller should check for existence before trying to obtain
438 extension, which allows to provide default value for a missing element.
439 The nvlist must not be in error state.
442 .Fn nvlist_get_parent
443 function allows to obtain the parent nvlist from the nested nvlist.
446 .Fn nvlist_take_bool ,
447 .Fn nvlist_take_number ,
448 .Fn nvlist_take_string ,
449 .Fn nvlist_take_nvlist ,
450 .Fn nvlist_take_descriptor ,
451 .Fn nvlist_take_binary
452 functions return value associated with the given name and remove the element
454 In case of string and binary values, the caller is responsible for free returned
458 In case of nvlist, the caller is responsible for destroying returned nvlist
462 In case of descriptor, the caller is responsible for closing returned descriptor
466 If element of the given name does not exist, the program will be aborted.
467 To avoid that the caller should check for existence before trying to obtain
470 extension, which allows to provide default value for a missing element.
471 The nvlist must not be in error state.
475 function removes element of the given name from the nvlist (besides of its type)
476 and frees all resources associated with it.
477 If element of the given name does not exist, the program will be aborted.
478 The nvlist must not be in error state.
482 function removes element of the given name and the given type from the nvlist
483 and frees all resources associated with it.
484 If element of the given name and the given type does not exist, the program
486 The nvlist must not be in error state.
489 .Fn nvlist_free_null ,
490 .Fn nvlist_free_bool ,
491 .Fn nvlist_free_number ,
492 .Fn nvlist_free_string ,
493 .Fn nvlist_free_nvlist ,
494 .Fn nvlist_free_descriptor ,
495 .Fn nvlist_free_binary
496 functions remove element of the given name and the given type determined by the
497 function name from the nvlist and free all resources associated with it.
498 If element of the given name and the given type does not exist, the program
500 The nvlist must not be in error state.
502 The following example demonstrates how to prepare an nvlist and send it over
509 fd = open("/tmp/foo", O_RDONLY);
511 err(1, "open(\\"/tmp/foo\\") failed");
513 nvl = nvlist_create(0);
515 * There is no need to check if nvlist_create() succeeded,
516 * as the nvlist_add_<type>() functions can cope.
517 * If it failed, nvlist_send() will fail.
519 nvlist_add_string(nvl, "filename", "/tmp/foo");
520 nvlist_add_number(nvl, "flags", O_RDONLY);
522 * We just want to send the descriptor, so we can give it
523 * for the nvlist to consume (that's why we use nvlist_move
526 nvlist_move_descriptor(nvl, "fd", fd);
527 if (nvlist_send(sock, nvl) < 0) {
529 err(1, "nvlist_send() failed");
534 Receiving nvlist and getting data:
541 nvl = nvlist_recv(sock);
543 err(1, "nvlist_recv() failed");
545 /* For command we take pointer to nvlist's buffer. */
546 command = nvlist_get_string(nvl, "command");
548 * For filename we remove it from the nvlist and take
549 * ownership of the buffer.
551 filename = nvlist_take_string(nvl, "filename");
552 /* The same for the descriptor. */
553 fd = nvlist_take_descriptor(nvl, "fd");
555 printf("command=%s filename=%s fd=%d\n", command, filename, fd);
560 /* command was freed by nvlist_destroy() */
563 Iterating over nvlist:
570 nvl = nvlist_recv(sock);
572 err(1, "nvlist_recv() failed");
575 while ((name = nvlist_next(nvl, &type, &cookie)) != NULL) {
579 printf("%ju", (uintmax_t)nvlist_get_number(nvl, name));
582 printf("%s", nvlist_get_string(nvl, name));
608 library was implemented by
609 .An Pawel Jakub Dawidek Aq Mt pawel@dawidek.net
610 under sponsorship from the FreeBSD Foundation.