2 * Copyright (c) 2009-2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
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 AUTHORS 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 AUTHORS 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
35 #include <sys/cdefs.h>
44 #ifndef _NVLIST_T_DECLARED
45 #define _NVLIST_T_DECLARED
48 typedef struct nvlist nvlist_t;
51 #define NV_NAME_MAX 2048
53 #define NV_TYPE_NONE 0
55 #define NV_TYPE_NULL 1
56 #define NV_TYPE_BOOL 2
57 #define NV_TYPE_NUMBER 3
58 #define NV_TYPE_STRING 4
59 #define NV_TYPE_NVLIST 5
60 #define NV_TYPE_DESCRIPTOR 6
61 #define NV_TYPE_BINARY 7
64 * Perform case-insensitive lookups of provided names.
66 #define NV_FLAG_IGNORE_CASE 0x01
68 #if defined(_KERNEL) && defined(MALLOC_DECLARE)
69 MALLOC_DECLARE(M_NVLIST);
74 nvlist_t *nvlist_create(int flags);
75 void nvlist_destroy(nvlist_t *nvl);
76 int nvlist_error(const nvlist_t *nvl);
77 bool nvlist_empty(const nvlist_t *nvl);
78 void nvlist_set_error(nvlist_t *nvl, int error);
80 nvlist_t *nvlist_clone(const nvlist_t *nvl);
83 void nvlist_dump(const nvlist_t *nvl, int fd);
84 void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
87 size_t nvlist_size(const nvlist_t *nvl);
88 void *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
89 nvlist_t *nvlist_unpack(const void *buf, size_t size);
91 int nvlist_send(int sock, const nvlist_t *nvl);
92 nvlist_t *nvlist_recv(int sock);
93 nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl);
95 const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep);
97 const nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep);
100 * The nvlist_exists functions check if the given name (optionally of the given
101 * type) exists on nvlist.
104 bool nvlist_exists(const nvlist_t *nvl, const char *name);
105 bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type);
107 bool nvlist_exists_null(const nvlist_t *nvl, const char *name);
108 bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
109 bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
110 bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
111 bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
113 bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
115 bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
118 * The nvlist_add functions add the given name/value pair.
119 * If a pointer is provided, nvlist_add will internally allocate memory for the
120 * given data (in other words it won't consume provided buffer).
123 void nvlist_add_null(nvlist_t *nvl, const char *name);
124 void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
125 void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
126 void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
127 void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
128 #ifdef _VA_LIST_DECLARED
129 void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
131 void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
133 void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
135 void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
138 * The nvlist_move functions add the given name/value pair.
139 * The functions consumes provided buffer.
142 void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
143 void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
145 void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
147 void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
150 * The nvlist_get functions returns value associated with the given name.
151 * If it returns a pointer, the pointer represents internal buffer and should
152 * not be freed by the caller.
155 bool nvlist_get_bool(const nvlist_t *nvl, const char *name);
156 uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name);
157 const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
158 const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
160 int nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
162 const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
165 * The nvlist_take functions returns value associated with the given name and
166 * remove the given entry from the nvlist.
167 * The caller is responsible for freeing received data.
170 bool nvlist_take_bool(nvlist_t *nvl, const char *name);
171 uint64_t nvlist_take_number(nvlist_t *nvl, const char *name);
172 char *nvlist_take_string(nvlist_t *nvl, const char *name);
173 nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
175 int nvlist_take_descriptor(nvlist_t *nvl, const char *name);
177 void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
180 * The nvlist_free functions removes the given name/value pair from the nvlist
181 * and frees memory associated with it.
184 void nvlist_free(nvlist_t *nvl, const char *name);
185 void nvlist_free_type(nvlist_t *nvl, const char *name, int type);
187 void nvlist_free_null(nvlist_t *nvl, const char *name);
188 void nvlist_free_bool(nvlist_t *nvl, const char *name);
189 void nvlist_free_number(nvlist_t *nvl, const char *name);
190 void nvlist_free_string(nvlist_t *nvl, const char *name);
191 void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
193 void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
195 void nvlist_free_binary(nvlist_t *nvl, const char *name);
198 * Below are the same functions, but which operate on format strings and
199 * variable argument lists.
201 * Functions that are not inserting a new pair into the nvlist cannot handle
202 * a failure to allocate the memory to hold the new name. Therefore these
203 * functions are not provided in the kernel.
207 bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
208 bool nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4);
210 bool nvlist_existsf_null(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
211 bool nvlist_existsf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
212 bool nvlist_existsf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
213 bool nvlist_existsf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
214 bool nvlist_existsf_nvlist(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
215 bool nvlist_existsf_descriptor(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
216 bool nvlist_existsf_binary(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
218 bool nvlist_existsv(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
219 bool nvlist_existsv_type(const nvlist_t *nvl, int type, const char *namefmt, va_list nameap) __printflike(3, 0);
221 bool nvlist_existsv_null(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
222 bool nvlist_existsv_bool(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
223 bool nvlist_existsv_number(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
224 bool nvlist_existsv_string(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
225 bool nvlist_existsv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
226 bool nvlist_existsv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
227 bool nvlist_existsv_binary(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
230 void nvlist_addf_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
231 void nvlist_addf_bool(nvlist_t *nvl, bool value, const char *namefmt, ...) __printflike(3, 4);
232 void nvlist_addf_number(nvlist_t *nvl, uint64_t value, const char *namefmt, ...) __printflike(3, 4);
233 void nvlist_addf_string(nvlist_t *nvl, const char *value, const char *namefmt, ...) __printflike(3, 4);
234 void nvlist_addf_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
236 void nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
238 void nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
240 #if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
241 void nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
242 void nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap) __printflike(3, 0);
243 void nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt, va_list nameap) __printflike(3, 0);
244 void nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
245 void nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
247 void nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
249 void nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
252 void nvlist_movef_string(nvlist_t *nvl, char *value, const char *namefmt, ...) __printflike(3, 4);
253 void nvlist_movef_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
255 void nvlist_movef_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
257 void nvlist_movef_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
259 #if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
260 void nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
261 void nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
263 void nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
265 void nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
269 bool nvlist_getf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
270 uint64_t nvlist_getf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
271 const char *nvlist_getf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
272 const nvlist_t *nvlist_getf_nvlist(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
273 int nvlist_getf_descriptor(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
274 const void *nvlist_getf_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt, ...) __printflike(3, 4);
276 bool nvlist_getv_bool(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
277 uint64_t nvlist_getv_number(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
278 const char *nvlist_getv_string(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
279 const nvlist_t *nvlist_getv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
280 int nvlist_getv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
281 const void *nvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt, va_list nameap) __printflike(3, 0);
283 bool nvlist_takef_bool(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
284 uint64_t nvlist_takef_number(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
285 char *nvlist_takef_string(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
286 nvlist_t *nvlist_takef_nvlist(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
287 int nvlist_takef_descriptor(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
288 void *nvlist_takef_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt, ...) __printflike(3, 4);
290 bool nvlist_takev_bool(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
291 uint64_t nvlist_takev_number(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
292 char *nvlist_takev_string(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
293 nvlist_t *nvlist_takev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
294 int nvlist_takev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
295 void *nvlist_takev_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt, va_list nameap) __printflike(3, 0);
297 void nvlist_freef(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
298 void nvlist_freef_type(nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4);
300 void nvlist_freef_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
301 void nvlist_freef_bool(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
302 void nvlist_freef_number(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
303 void nvlist_freef_string(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
304 void nvlist_freef_nvlist(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
305 void nvlist_freef_descriptor(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
306 void nvlist_freef_binary(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
308 void nvlist_freev(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
309 void nvlist_freev_type(nvlist_t *nvl, int type, const char *namefmt, va_list nameap) __printflike(3, 0);
311 void nvlist_freev_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
312 void nvlist_freev_bool(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
313 void nvlist_freev_number(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
314 void nvlist_freev_string(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
315 void nvlist_freev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
316 void nvlist_freev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
317 void nvlist_freev_binary(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);