/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 John H. Baldwin <[email protected]>4* Copyright 2026 Hans Rosenfeld5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef __CONFIG_H__29#define __CONFIG_H__3031#include <sys/nv.h>3233/*-34* Manages a configuration database backed by an nv(9) list.35*36* The database only stores string values. Callers should parse37* values into other types if needed. String values can reference38* other configuration variables using a '%(name)' syntax. In this39* case, the name must be the full path of the configuration40* variable. The % character can be escaped with a preceding \ to41* avoid expansion. Any \ characters must be escaped.42*43* Configuration variables are stored in a tree. The full path of a44* variable is specified as a dot-separated name similar to sysctl(8)45* OIDs.46*/4748/*49* Walk the nodes under a parent nvlist. For each node found, call the given50* callback function passing the current prefix, nvlist, node name and type,51* and the given argument.52*/53int walk_config_nodes(const char *, const nvlist_t *, void *,54int (*cb)(const char *, const nvlist_t *, const char *, int, void *));5556/*57* Fetches the value of a configuration variable. If the "raw" value58* contains references to other configuration variables, this function59* expands those references and returns a pointer to the parsed60* string. The string's storage is only stable until the next call to61* this function.62*63* If no node is found, returns NULL.64*65* If 'parent' is NULL, 'name' is assumed to be a top-level variable.66*/67const char *get_config_value_node(const nvlist_t *parent, const char *name);6869/*70* Similar to get_config_value_node but expects a full path to the71* leaf node.72*/73const char *get_config_value(const char *path);7475/* Initializes the tree to an empty state. */76void init_config(void);7778/*79* Creates an existing configuration node via a dot-separated OID80* path. Will fail if the path names an existing leaf configuration81* variable. If the node already exists, this returns a pointer to82* the existing node.83*/84nvlist_t *create_config_node(const char *path);8586/*87* Looks for an existing configuration node via a dot-separated OID88* path. Will fail if the path names an existing leaf configuration89* variable.90*/91nvlist_t *find_config_node(const char *path);9293/*94* Similar to the above, but treats the path relative to an existing95* 'parent' node rather than as an absolute path.96*/97nvlist_t *create_relative_config_node(nvlist_t *parent, const char *path);98nvlist_t *find_relative_config_node(nvlist_t *parent, const char *path);99100/*101* Adds or replaces the value of the specified variable.102*103* If 'parent' is NULL, 'name' is assumed to be a top-level variable.104*/105void set_config_value_node(nvlist_t *parent, const char *name,106const char *value);107108/*109* Similar to set_config_value_node but only sets value if it's unset yet.110*/111void set_config_value_node_if_unset(nvlist_t *const parent,112const char *const name, const char *const value);113114/*115* Similar to set_config_value_node but expects a full path to the116* leaf node.117*/118void set_config_value(const char *path, const char *value);119120/*121* Similar to set_config_value but only sets the value if it's unset yet.122*/123void set_config_value_if_unset(const char *const path,124const char *const value);125126/* Convenience wrappers for boolean variables. */127bool get_config_bool(const char *path);128bool get_config_bool_node(const nvlist_t *parent, const char *name);129bool get_config_bool_default(const char *path, bool def);130bool get_config_bool_node_default(const nvlist_t *parent, const char *name,131bool def);132void set_config_bool(const char *path, bool value);133void set_config_bool_node(nvlist_t *parent, const char *name, bool value);134135void dump_config(void);136137#endif /* !__CONFIG_H__ */138139140