Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/usr.sbin/bhyve/config.h
105216 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2021 John H. Baldwin <[email protected]>
5
* Copyright 2026 Hans Rosenfeld
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
#ifndef __CONFIG_H__
30
#define __CONFIG_H__
31
32
#include <sys/nv.h>
33
34
/*-
35
* Manages a configuration database backed by an nv(9) list.
36
*
37
* The database only stores string values. Callers should parse
38
* values into other types if needed. String values can reference
39
* other configuration variables using a '%(name)' syntax. In this
40
* case, the name must be the full path of the configuration
41
* variable. The % character can be escaped with a preceding \ to
42
* avoid expansion. Any \ characters must be escaped.
43
*
44
* Configuration variables are stored in a tree. The full path of a
45
* variable is specified as a dot-separated name similar to sysctl(8)
46
* OIDs.
47
*/
48
49
/*
50
* Walk the nodes under a parent nvlist. For each node found, call the given
51
* callback function passing the current prefix, nvlist, node name and type,
52
* and the given argument.
53
*/
54
int walk_config_nodes(const char *, const nvlist_t *, void *,
55
int (*cb)(const char *, const nvlist_t *, const char *, int, void *));
56
57
/*
58
* Fetches the value of a configuration variable. If the "raw" value
59
* contains references to other configuration variables, this function
60
* expands those references and returns a pointer to the parsed
61
* string. The string's storage is only stable until the next call to
62
* this function.
63
*
64
* If no node is found, returns NULL.
65
*
66
* If 'parent' is NULL, 'name' is assumed to be a top-level variable.
67
*/
68
const char *get_config_value_node(const nvlist_t *parent, const char *name);
69
70
/*
71
* Similar to get_config_value_node but expects a full path to the
72
* leaf node.
73
*/
74
const char *get_config_value(const char *path);
75
76
/* Initializes the tree to an empty state. */
77
void init_config(void);
78
79
/*
80
* Creates an existing configuration node via a dot-separated OID
81
* path. Will fail if the path names an existing leaf configuration
82
* variable. If the node already exists, this returns a pointer to
83
* the existing node.
84
*/
85
nvlist_t *create_config_node(const char *path);
86
87
/*
88
* Looks for an existing configuration node via a dot-separated OID
89
* path. Will fail if the path names an existing leaf configuration
90
* variable.
91
*/
92
nvlist_t *find_config_node(const char *path);
93
94
/*
95
* Similar to the above, but treats the path relative to an existing
96
* 'parent' node rather than as an absolute path.
97
*/
98
nvlist_t *create_relative_config_node(nvlist_t *parent, const char *path);
99
nvlist_t *find_relative_config_node(nvlist_t *parent, const char *path);
100
101
/*
102
* Adds or replaces the value of the specified variable.
103
*
104
* If 'parent' is NULL, 'name' is assumed to be a top-level variable.
105
*/
106
void set_config_value_node(nvlist_t *parent, const char *name,
107
const char *value);
108
109
/*
110
* Similar to set_config_value_node but only sets value if it's unset yet.
111
*/
112
void set_config_value_node_if_unset(nvlist_t *const parent,
113
const char *const name, const char *const value);
114
115
/*
116
* Similar to set_config_value_node but expects a full path to the
117
* leaf node.
118
*/
119
void set_config_value(const char *path, const char *value);
120
121
/*
122
* Similar to set_config_value but only sets the value if it's unset yet.
123
*/
124
void set_config_value_if_unset(const char *const path,
125
const char *const value);
126
127
/* Convenience wrappers for boolean variables. */
128
bool get_config_bool(const char *path);
129
bool get_config_bool_node(const nvlist_t *parent, const char *name);
130
bool get_config_bool_default(const char *path, bool def);
131
bool get_config_bool_node_default(const nvlist_t *parent, const char *name,
132
bool def);
133
void set_config_bool(const char *path, bool value);
134
void set_config_bool_node(nvlist_t *parent, const char *name, bool value);
135
136
void dump_config(void);
137
138
#endif /* !__CONFIG_H__ */
139
140