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