nv.h revision 292973
1/*- 2 * Copyright (c) 2009-2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Pawel Jakub Dawidek under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: stable/10/sys/sys/nv.h 292973 2015-12-31 03:28:14Z ngie $ 30 */ 31 32#ifndef _NV_H_ 33#define _NV_H_ 34 35#include <sys/cdefs.h> 36 37#ifndef _KERNEL 38#include <stdarg.h> 39#include <stdbool.h> 40#include <stdint.h> 41#include <stdio.h> 42#endif 43 44#ifndef _NVLIST_T_DECLARED 45#define _NVLIST_T_DECLARED 46struct nvlist; 47 48typedef struct nvlist nvlist_t; 49#endif 50 51#define NV_NAME_MAX 2048 52 53#define NV_TYPE_NONE 0 54 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 62 63/* 64 * Perform case-insensitive lookups of provided names. 65 */ 66#define NV_FLAG_IGNORE_CASE 0x01 67 68#if defined(_KERNEL) && defined(MALLOC_DECLARE) 69MALLOC_DECLARE(M_NVLIST); 70#endif 71 72__BEGIN_DECLS 73 74nvlist_t *nvlist_create(int flags); 75void nvlist_destroy(nvlist_t *nvl); 76int nvlist_error(const nvlist_t *nvl); 77bool nvlist_empty(const nvlist_t *nvl); 78int nvlist_flags(const nvlist_t *nvl); 79void nvlist_set_error(nvlist_t *nvl, int error); 80 81nvlist_t *nvlist_clone(const nvlist_t *nvl); 82 83#ifndef _KERNEL 84void nvlist_dump(const nvlist_t *nvl, int fd); 85void nvlist_fdump(const nvlist_t *nvl, FILE *fp); 86#endif 87 88size_t nvlist_size(const nvlist_t *nvl); 89void *nvlist_pack(const nvlist_t *nvl, size_t *sizep); 90nvlist_t *nvlist_unpack(const void *buf, size_t size); 91 92int nvlist_send(int sock, const nvlist_t *nvl); 93nvlist_t *nvlist_recv(int sock); 94nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl); 95 96const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep); 97 98const nvlist_t *nvlist_get_parent(const nvlist_t *nvl, void **cookiep); 99 100/* 101 * The nvlist_exists functions check if the given name (optionally of the given 102 * type) exists on nvlist. 103 */ 104 105bool nvlist_exists(const nvlist_t *nvl, const char *name); 106bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type); 107 108bool nvlist_exists_null(const nvlist_t *nvl, const char *name); 109bool nvlist_exists_bool(const nvlist_t *nvl, const char *name); 110bool nvlist_exists_number(const nvlist_t *nvl, const char *name); 111bool nvlist_exists_string(const nvlist_t *nvl, const char *name); 112bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name); 113#ifndef _KERNEL 114bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name); 115#endif 116bool nvlist_exists_binary(const nvlist_t *nvl, const char *name); 117 118/* 119 * The nvlist_add functions add the given name/value pair. 120 * If a pointer is provided, nvlist_add will internally allocate memory for the 121 * given data (in other words it won't consume provided buffer). 122 */ 123 124void nvlist_add_null(nvlist_t *nvl, const char *name); 125void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value); 126void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value); 127void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value); 128void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4); 129#ifdef _VA_LIST_DECLARED 130void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0); 131#endif 132void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value); 133#ifndef _KERNEL 134void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value); 135#endif 136void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size); 137 138/* 139 * The nvlist_move functions add the given name/value pair. 140 * The functions consumes provided buffer. 141 */ 142 143void nvlist_move_string(nvlist_t *nvl, const char *name, char *value); 144void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value); 145#ifndef _KERNEL 146void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value); 147#endif 148void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size); 149 150/* 151 * The nvlist_get functions returns value associated with the given name. 152 * If it returns a pointer, the pointer represents internal buffer and should 153 * not be freed by the caller. 154 */ 155 156bool nvlist_get_bool(const nvlist_t *nvl, const char *name); 157uint64_t nvlist_get_number(const nvlist_t *nvl, const char *name); 158const char *nvlist_get_string(const nvlist_t *nvl, const char *name); 159const nvlist_t *nvlist_get_nvlist(const nvlist_t *nvl, const char *name); 160#ifndef _KERNEL 161int nvlist_get_descriptor(const nvlist_t *nvl, const char *name); 162#endif 163const void *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep); 164 165/* 166 * The nvlist_take functions returns value associated with the given name and 167 * remove the given entry from the nvlist. 168 * The caller is responsible for freeing received data. 169 */ 170 171bool nvlist_take_bool(nvlist_t *nvl, const char *name); 172uint64_t nvlist_take_number(nvlist_t *nvl, const char *name); 173char *nvlist_take_string(nvlist_t *nvl, const char *name); 174nvlist_t *nvlist_take_nvlist(nvlist_t *nvl, const char *name); 175#ifndef _KERNEL 176int nvlist_take_descriptor(nvlist_t *nvl, const char *name); 177#endif 178void *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep); 179 180/* 181 * The nvlist_free functions removes the given name/value pair from the nvlist 182 * and frees memory associated with it. 183 */ 184 185void nvlist_free(nvlist_t *nvl, const char *name); 186void nvlist_free_type(nvlist_t *nvl, const char *name, int type); 187 188void nvlist_free_null(nvlist_t *nvl, const char *name); 189void nvlist_free_bool(nvlist_t *nvl, const char *name); 190void nvlist_free_number(nvlist_t *nvl, const char *name); 191void nvlist_free_string(nvlist_t *nvl, const char *name); 192void nvlist_free_nvlist(nvlist_t *nvl, const char *name); 193#ifndef _KERNEL 194void nvlist_free_descriptor(nvlist_t *nvl, const char *name); 195#endif 196void nvlist_free_binary(nvlist_t *nvl, const char *name); 197 198__END_DECLS 199 200#endif /* !_NV_H_ */ 201