1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 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_IMPL_H_ 36 #define _NV_IMPL_H_ 37 38 #ifndef _NVPAIR_T_DECLARED 39 #define _NVPAIR_T_DECLARED 40 struct nvpair; 41 42 typedef struct nvpair nvpair_t; 43 #endif 44 45 struct nvlist_header { 46 uint8_t nvlh_magic; 47 uint8_t nvlh_version; 48 uint8_t nvlh_flags; 49 uint64_t nvlh_descriptors; 50 uint64_t nvlh_size; 51 } __packed; 52 53 #define NV_TYPE_NVLIST_ARRAY_NEXT 254 54 #define NV_TYPE_NVLIST_UP 255 55 56 #define NV_TYPE_FIRST NV_TYPE_NULL 57 #define NV_TYPE_LAST NV_TYPE_DESCRIPTOR_ARRAY 58 59 #define NV_FLAG_BIG_ENDIAN 0x080 60 #define NV_FLAG_IN_ARRAY 0x100 61 62 #ifdef _KERNEL 63 #define nv_malloc(size) malloc((size), M_NVLIST, M_NOWAIT) 64 #define nv_calloc(n, size) mallocarray((n), (size), M_NVLIST, \ 65 M_NOWAIT | M_ZERO) 66 #define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \ 67 M_NOWAIT) 68 #define nv_free(buf) free((buf), M_NVLIST) 69 #define nv_strdup(buf) strdup_flags((buf), M_NVLIST, M_NOWAIT) 70 #define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__) 71 72 #define ERRNO_SET(var) do { } while (0) 73 #define ERRNO_SAVE() do { do { } while(0) 74 #define ERRNO_RESTORE() } while (0) 75 76 #define ERRNO_OR_DEFAULT(default) (default) 77 78 #else 79 80 #define nv_malloc(size) malloc((size)) 81 #define nv_calloc(n, size) calloc((n), (size)) 82 #define nv_realloc(buf, size) realloc((buf), (size)) 83 #define nv_free(buf) free((buf)) 84 #define nv_strdup(buf) strdup((buf)) 85 #define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__) 86 87 #define ERRNO_SET(var) do { errno = (var); } while (0) 88 #define ERRNO_SAVE() do { \ 89 int _serrno; \ 90 \ 91 _serrno = errno 92 93 #define ERRNO_RESTORE() errno = _serrno; \ 94 } while (0) 95 96 #define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno) 97 98 #endif 99 100 int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp); 101 size_t nvlist_ndescriptors(const nvlist_t *nvl); 102 void nvlist_set_flags(nvlist_t *nvl, int flags); 103 104 nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl); 105 nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 106 nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 107 108 void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp); 109 110 bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp); 111 112 void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent); 113 void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele); 114 nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl); 115 116 const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name); 117 118 nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name); 119 120 /* Function removes the given nvpair from the nvlist. */ 121 void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp); 122 123 void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp); 124 125 int nvpair_type(const nvpair_t *nvp); 126 const char *nvpair_name(const nvpair_t *nvp); 127 128 nvpair_t *nvpair_clone(const nvpair_t *nvp); 129 130 nvpair_t *nvpair_create_null(const char *name); 131 nvpair_t *nvpair_create_bool(const char *name, bool value); 132 nvpair_t *nvpair_create_number(const char *name, uint64_t value); 133 nvpair_t *nvpair_create_string(const char *name, const char *value); 134 nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3); 135 nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0); 136 nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value); 137 nvpair_t *nvpair_create_descriptor(const char *name, int value); 138 nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size); 139 nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems); 140 nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems); 141 nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems); 142 nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems); 143 nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems); 144 145 nvpair_t *nvpair_move_string(const char *name, char *value); 146 nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value); 147 nvpair_t *nvpair_move_descriptor(const char *name, int value); 148 nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size); 149 nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems); 150 nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems); 151 nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems); 152 nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems); 153 nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems); 154 155 int nvpair_append_bool_array(nvpair_t *nvp, const bool value); 156 int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value); 157 int nvpair_append_string_array(nvpair_t *nvp, const char *value); 158 int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value); 159 int nvpair_append_descriptor_array(nvpair_t *nvp, const int value); 160 161 bool nvpair_get_bool(const nvpair_t *nvp); 162 uint64_t nvpair_get_number(const nvpair_t *nvp); 163 const char *nvpair_get_string(const nvpair_t *nvp); 164 const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp); 165 int nvpair_get_descriptor(const nvpair_t *nvp); 166 const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep); 167 const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp); 168 const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp); 169 const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp); 170 const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp); 171 const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp); 172 173 void nvpair_free(nvpair_t *nvp); 174 175 #endif /* !_NV_IMPL_H_ */ 176