1621b5090SJohn Baldwin /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3621b5090SJohn Baldwin * 4621b5090SJohn Baldwin * Copyright (c) 2021 John H. Baldwin <jhb@FreeBSD.org> 5621b5090SJohn Baldwin * 6621b5090SJohn Baldwin * Redistribution and use in source and binary forms, with or without 7621b5090SJohn Baldwin * modification, are permitted provided that the following conditions 8621b5090SJohn Baldwin * are met: 9621b5090SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 10621b5090SJohn Baldwin * notice, this list of conditions and the following disclaimer. 11621b5090SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 12621b5090SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 13621b5090SJohn Baldwin * documentation and/or other materials provided with the distribution. 14621b5090SJohn Baldwin * 15621b5090SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16621b5090SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17621b5090SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18621b5090SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19621b5090SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20621b5090SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21621b5090SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22621b5090SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23621b5090SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24621b5090SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25621b5090SJohn Baldwin * SUCH DAMAGE. 26621b5090SJohn Baldwin */ 27621b5090SJohn Baldwin 28621b5090SJohn Baldwin #ifndef __CONFIG_H__ 29621b5090SJohn Baldwin #define __CONFIG_H__ 30621b5090SJohn Baldwin 31621b5090SJohn Baldwin #include <sys/nv.h> 32621b5090SJohn Baldwin 33621b5090SJohn Baldwin /*- 34621b5090SJohn Baldwin * Manages a configuration database backed by an nv(9) list. 35621b5090SJohn Baldwin * 36621b5090SJohn Baldwin * The database only stores string values. Callers should parse 37621b5090SJohn Baldwin * values into other types if needed. String values can reference 38621b5090SJohn Baldwin * other configuration variables using a '%(name)' syntax. In this 39886ce99dSGordon Bergling * case, the name must be the full path of the configuration 40621b5090SJohn Baldwin * variable. The % character can be escaped with a preceding \ to 41621b5090SJohn Baldwin * avoid expansion. Any \ characters must be escaped. 42621b5090SJohn Baldwin * 43621b5090SJohn Baldwin * Configuration variables are stored in a tree. The full path of a 44621b5090SJohn Baldwin * variable is specified as a dot-separated name similar to sysctl(8) 45621b5090SJohn Baldwin * OIDs. 46621b5090SJohn Baldwin */ 47621b5090SJohn Baldwin 48621b5090SJohn Baldwin /* 49621b5090SJohn Baldwin * Fetches the value of a configuration variable. If the "raw" value 50621b5090SJohn Baldwin * contains references to other configuration variables, this function 51621b5090SJohn Baldwin * expands those references and returns a pointer to the parsed 52621b5090SJohn Baldwin * string. The string's storage is only stable until the next call to 53621b5090SJohn Baldwin * this function. 54621b5090SJohn Baldwin * 55621b5090SJohn Baldwin * If no node is found, returns NULL. 56621b5090SJohn Baldwin * 57621b5090SJohn Baldwin * If 'parent' is NULL, 'name' is assumed to be a top-level variable. 58621b5090SJohn Baldwin */ 59621b5090SJohn Baldwin const char *get_config_value_node(const nvlist_t *parent, const char *name); 60621b5090SJohn Baldwin 61621b5090SJohn Baldwin /* 62621b5090SJohn Baldwin * Similar to get_config_value_node but expects a full path to the 63621b5090SJohn Baldwin * leaf node. 64621b5090SJohn Baldwin */ 65621b5090SJohn Baldwin const char *get_config_value(const char *path); 66621b5090SJohn Baldwin 67621b5090SJohn Baldwin /* Initializes the tree to an empty state. */ 68621b5090SJohn Baldwin void init_config(void); 69621b5090SJohn Baldwin 70621b5090SJohn Baldwin /* 71621b5090SJohn Baldwin * Creates an existing configuration node via a dot-separated OID 72621b5090SJohn Baldwin * path. Will fail if the path names an existing leaf configuration 73621b5090SJohn Baldwin * variable. If the node already exists, this returns a pointer to 74621b5090SJohn Baldwin * the existing node. 75621b5090SJohn Baldwin */ 76621b5090SJohn Baldwin nvlist_t *create_config_node(const char *path); 77621b5090SJohn Baldwin 78621b5090SJohn Baldwin /* 79621b5090SJohn Baldwin * Looks for an existing configuration node via a dot-separated OID 80621b5090SJohn Baldwin * path. Will fail if the path names an existing leaf configuration 81621b5090SJohn Baldwin * variable. 82621b5090SJohn Baldwin */ 83621b5090SJohn Baldwin nvlist_t *find_config_node(const char *path); 84621b5090SJohn Baldwin 85621b5090SJohn Baldwin /* 86621b5090SJohn Baldwin * Similar to the above, but treats the path relative to an existing 87621b5090SJohn Baldwin * 'parent' node rather than as an absolute path. 88621b5090SJohn Baldwin */ 89621b5090SJohn Baldwin nvlist_t *create_relative_config_node(nvlist_t *parent, const char *path); 90621b5090SJohn Baldwin nvlist_t *find_relative_config_node(nvlist_t *parent, const char *path); 91621b5090SJohn Baldwin 92621b5090SJohn Baldwin /* 93621b5090SJohn Baldwin * Adds or replaces the value of the specified variable. 94621b5090SJohn Baldwin * 95621b5090SJohn Baldwin * If 'parent' is NULL, 'name' is assumed to be a top-level variable. 96621b5090SJohn Baldwin */ 97621b5090SJohn Baldwin void set_config_value_node(nvlist_t *parent, const char *name, 98621b5090SJohn Baldwin const char *value); 99621b5090SJohn Baldwin 100621b5090SJohn Baldwin /* 101fe453891SCorvin Köhne * Similar to set_config_value_node but only sets value if it's unset yet. 102fe453891SCorvin Köhne */ 103fe453891SCorvin Köhne void set_config_value_node_if_unset(nvlist_t *const parent, 104fe453891SCorvin Köhne const char *const name, const char *const value); 105fe453891SCorvin Köhne 106fe453891SCorvin Köhne /* 107621b5090SJohn Baldwin * Similar to set_config_value_node but expects a full path to the 108621b5090SJohn Baldwin * leaf node. 109621b5090SJohn Baldwin */ 110621b5090SJohn Baldwin void set_config_value(const char *path, const char *value); 111621b5090SJohn Baldwin 112fe453891SCorvin Köhne /* 113fe453891SCorvin Köhne * Similar to set_config_value but only sets the value if it's unset yet. 114fe453891SCorvin Köhne */ 115fe453891SCorvin Köhne void set_config_value_if_unset(const char *const path, 116fe453891SCorvin Köhne const char *const value); 117fe453891SCorvin Köhne 118621b5090SJohn Baldwin /* Convenience wrappers for boolean variables. */ 119621b5090SJohn Baldwin bool get_config_bool(const char *path); 120621b5090SJohn Baldwin bool get_config_bool_node(const nvlist_t *parent, const char *name); 121621b5090SJohn Baldwin bool get_config_bool_default(const char *path, bool def); 122621b5090SJohn Baldwin bool get_config_bool_node_default(const nvlist_t *parent, const char *name, 123621b5090SJohn Baldwin bool def); 124621b5090SJohn Baldwin void set_config_bool(const char *path, bool value); 125621b5090SJohn Baldwin void set_config_bool_node(nvlist_t *parent, const char *name, bool value); 126621b5090SJohn Baldwin 127621b5090SJohn Baldwin void dump_config(void); 128621b5090SJohn Baldwin 129621b5090SJohn Baldwin #endif /* !__CONFIG_H__ */ 130