nv.h revision 1.2 1 /* $NetBSD: nv.h,v 1.2 2018/09/08 14:02:15 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2009-2013 The FreeBSD Foundation
7 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo (at) FreeBSD.org>
8 * All rights reserved.
9 *
10 * This software was developed by Pawel Jakub Dawidek under sponsorship from
11 * the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/sys/nv.h 335347 2018-06-18 22:57:32Z oshogbo $
35 */
36
37 #ifndef _NV_H_
38 #define _NV_H_
39
40 #include <sys/cdefs.h>
41
42 #if !defined(_KERNEL) && !defined(_STANDALONE)
43 #include <stdarg.h>
44 #include <stdbool.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #else
48 #include <sys/types.h>
49 #endif
50
51 #ifndef _NVLIST_T_DECLARED
52 #define _NVLIST_T_DECLARED
53 struct nvlist;
54
55 typedef struct nvlist nvlist_t;
56 #endif
57
58 #define NV_NAME_MAX 2048
59
60 #define NV_TYPE_NONE 0
61
62 #define NV_TYPE_NULL 1
63 #define NV_TYPE_BOOL 2
64 #define NV_TYPE_NUMBER 3
65 #define NV_TYPE_STRING 4
66 #define NV_TYPE_NVLIST 5
67 #define NV_TYPE_DESCRIPTOR 6
68 #define NV_TYPE_BINARY 7
69 #define NV_TYPE_BOOL_ARRAY 8
70 #define NV_TYPE_NUMBER_ARRAY 9
71 #define NV_TYPE_STRING_ARRAY 10
72 #define NV_TYPE_NVLIST_ARRAY 11
73 #define NV_TYPE_DESCRIPTOR_ARRAY 12
74
75 /*
76 * Perform case-insensitive lookups of provided names.
77 */
78 #define NV_FLAG_IGNORE_CASE 0x01
79 /*
80 * Names don't have to be unique.
81 */
82 #define NV_FLAG_NO_UNIQUE 0x02
83
84 #if defined(_KERNEL) && defined(MALLOC_DECLARE)
85 MALLOC_DECLARE(M_NVLIST);
86 #endif
87
88 __BEGIN_DECLS
89
90 nvlist_t *nvlist_create(int flags);
91 void nvlist_destroy(nvlist_t *nvl);
92 int nvlist_error(const nvlist_t *nvl);
93 bool nvlist_empty(const nvlist_t *nvl);
94 int nvlist_flags(const nvlist_t *nvl);
95 void nvlist_set_error(nvlist_t *nvl, int error);
96
97 nvlist_t *nvlist_clone(const nvlist_t *nvl);
98
99 #if !defined(_KERNEL) && !defined(_STANDALONE)
100 void nvlist_dump(const nvlist_t *nvl, int fd);
101 void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
102 #endif
103
104 size_t nvlist_size(const nvlist_t *nvl);
105 void *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
106 nvlist_t *nvlist_unpack(const void *buf, size_t size, int flags);
107
108 int nvlist_send(int sock, const nvlist_t *nvl);
109 nvlist_t *nvlist_recv(int sock, int flags);
110 nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl, int flags);
111
112 const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep);
113
114 const nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep);
115
116 const nvlist_t *nvlist_get_array_next(const nvlist_t *nvl);
117 bool nvlist_in_array(const nvlist_t *nvl);
118
119 const nvlist_t *nvlist_get_pararr(const nvlist_t *nvl, void **cookiep);
120
121 /*
122 * The nvlist_exists functions check if the given name (optionally of the given
123 * type) exists on nvlist.
124 */
125
126 bool nvlist_exists(const nvlist_t *nvl, const char *name);
127 bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type);
128
129 bool nvlist_exists_null(const nvlist_t *nvl, const char *name);
130 bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
131 bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
132 bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
133 bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
134 bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
135 bool nvlist_exists_bool_array(const nvlist_t *nvl, const char *name);
136 bool nvlist_exists_number_array(const nvlist_t *nvl, const char *name);
137 bool nvlist_exists_string_array(const nvlist_t *nvl, const char *name);
138 bool nvlist_exists_nvlist_array(const nvlist_t *nvl, const char *name);
139 #if !defined(_KERNEL) && !defined(_STANDALONE)
140 bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
141 bool nvlist_exists_descriptor_array(const nvlist_t *nvl, const char *name);
142 #endif
143
144 /*
145 * The nvlist_add functions add the given name/value pair.
146 * If a pointer is provided, nvlist_add will internally allocate memory for the
147 * given data (in other words it won't consume provided buffer).
148 */
149
150 void nvlist_add_null(nvlist_t *nvl, const char *name);
151 void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
152 void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
153 void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
154 void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
155 #if !defined(_KERNEL) || defined(_VA_LIST_DECLARED)
156 void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
157 #endif
158 void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
159 void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
160 void nvlist_add_bool_array(nvlist_t *nvl, const char *name, const bool *value, size_t nitems);
161 void nvlist_add_number_array(nvlist_t *nvl, const char *name, const uint64_t *value, size_t nitems);
162 void nvlist_add_string_array(nvlist_t *nvl, const char *name, const char * const *value, size_t nitems);
163 void nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, const nvlist_t * const *value, size_t nitems);
164 #if !defined(_KERNEL) && !defined(_STANDALONE)
165 void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
166 void nvlist_add_descriptor_array(nvlist_t *nvl, const char *name, const int *value, size_t nitems);
167 #endif
168
169 void nvlist_append_bool_array(nvlist_t *nvl, const char *name, const bool value);
170 void nvlist_append_number_array(nvlist_t *nvl, const char *name, const uint64_t value);
171 void nvlist_append_string_array(nvlist_t *nvl, const char *name, const char * const value);
172 void nvlist_append_nvlist_array(nvlist_t *nvl, const char *name, const nvlist_t * const value);
173 #if !defined(_KERNEL) && !defined(_STANDALONE)
174 void nvlist_append_descriptor_array(nvlist_t *nvl, const char *name, int value);
175 #endif
176
177 /*
178 * The nvlist_move functions add the given name/value pair.
179 * The functions consumes provided buffer.
180 */
181
182 void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
183 void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
184 void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
185 void nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value, size_t nitems);
186 void nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value, size_t nitems);
187 void nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value, size_t nitems);
188 void nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value, size_t nitems);
189 #if !defined(_KERNEL) && !defined(_STANDALONE)
190 void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
191 void nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value, size_t nitems);
192 #endif
193
194 /*
195 * The nvlist_get functions returns value associated with the given name.
196 * If it returns a pointer, the pointer represents internal buffer and should
197 * not be freed by the caller.
198 */
199
200 bool nvlist_get_bool(const nvlist_t *nvl, const char *name);
201 uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name);
202 const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
203 const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
204 const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
205 const bool *nvlist_get_bool_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
206 const uint64_t *nvlist_get_number_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
207 const char * const *nvlist_get_string_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
208 const nvlist_t * const *nvlist_get_nvlist_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
209 #if !defined(_KERNEL) && !defined(_STANDALONE)
210 int nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
211 const int *nvlist_get_descriptor_array(const nvlist_t *nvl, const char *name, size_t *nitemsp);
212 #endif
213
214 /*
215 * The nvlist_take functions returns value associated with the given name and
216 * remove the given entry from the nvlist.
217 * The caller is responsible for freeing received data.
218 */
219
220 bool nvlist_take_bool(nvlist_t *nvl, const char *name);
221 uint64_t nvlist_take_number(nvlist_t *nvl, const char *name);
222 char *nvlist_take_string(nvlist_t *nvl, const char *name);
223 nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
224 void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
225 bool *nvlist_take_bool_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
226 uint64_t *nvlist_take_number_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
227 char **nvlist_take_string_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
228 nvlist_t **nvlist_take_nvlist_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
229 #if !defined(_KERNEL) && !defined(_STANDALONE)
230 int nvlist_take_descriptor(nvlist_t *nvl, const char *name);
231 int *nvlist_take_descriptor_array(nvlist_t *nvl, const char *name, size_t *nitemsp);
232 #endif
233
234 /*
235 * The nvlist_free functions removes the given name/value pair from the nvlist
236 * and frees memory associated with it.
237 */
238
239 void nvlist_free(nvlist_t *nvl, const char *name);
240 void nvlist_free_type(nvlist_t *nvl, const char *name, int type);
241
242 void nvlist_free_null(nvlist_t *nvl, const char *name);
243 void nvlist_free_bool(nvlist_t *nvl, const char *name);
244 void nvlist_free_number(nvlist_t *nvl, const char *name);
245 void nvlist_free_string(nvlist_t *nvl, const char *name);
246 void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
247 void nvlist_free_binary(nvlist_t *nvl, const char *name);
248 void nvlist_free_bool_array(nvlist_t *nvl, const char *name);
249 void nvlist_free_number_array(nvlist_t *nvl, const char *name);
250 void nvlist_free_string_array(nvlist_t *nvl, const char *name);
251 void nvlist_free_nvlist_array(nvlist_t *nvl, const char *name);
252 void nvlist_free_binary_array(nvlist_t *nvl, const char *name);
253 #if !defined(_KERNEL) && !defined(_STANDALONE)
254 void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
255 void nvlist_free_descriptor_array(nvlist_t *nvl, const char *name);
256 #endif
257
258 #ifdef __NetBSD__
259 /*
260 * ioctl kernel-userspace interface.
261 */
262
263 typedef struct {
264 void * buf;
265 size_t len;
266 int flags;
267 } nvlist_ref_t;
268
269 #if defined(_KERNEL)
270 int nvlist_copyin(const nvlist_ref_t *nref, nvlist_t **nvlp, size_t lim);
271 int nvlist_copyout(nvlist_ref_t *nref, const nvlist_t *nvl);
272 #else
273 int nvlist_xfer_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl,
274 nvlist_t **nvlp);
275 int nvlist_send_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl);
276 int nvlist_recv_ioctl(int fd, unsigned long cmd, nvlist_t **nvlp);
277 #endif
278 #endif
279
280 __END_DECLS
281
282 #endif /* !_NV_H_ */
283