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