1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
14 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
16 typedef enum ConfigParseFlags
{
17 CONFIG_PARSE_RELAXED
= 1 << 0, /* Do not warn about unknown non-extension fields */
18 CONFIG_PARSE_ALLOW_INCLUDE
= 1 << 1, /* Allow the deprecated .include stanza */
19 CONFIG_PARSE_WARN
= 1 << 2, /* Emit non-debug messages */
22 /* Argument list for parsers of specific configuration settings. */
23 #define CONFIG_PARSER_ARGUMENTS \
25 const char *filename, \
27 const char *section, \
28 unsigned section_line, \
35 /* Prototype for a parser for a specific configuration setting */
36 typedef int (*ConfigParserCallback
)(CONFIG_PARSER_ARGUMENTS
);
38 /* A macro declaring the a function prototype, following the typedef above, simply because it's so cumbersomely long
39 * otherwise. (And current emacs gets irritatingly slow when editing files that contain lots of very long function
40 * prototypes on the same screen…) */
41 #define CONFIG_PARSER_PROTOTYPE(name) int name(CONFIG_PARSER_ARGUMENTS)
43 /* Wraps information for parsing a specific configuration variable, to
44 * be stored in a simple array */
45 typedef struct ConfigTableItem
{
46 const char *section
; /* Section */
47 const char *lvalue
; /* Name of the variable */
48 ConfigParserCallback parse
; /* Function that is called to parse the variable's value */
49 int ltype
; /* Distinguish different variables passed to the same callback */
50 void *data
; /* Where to store the variable's data */
53 /* Wraps information for parsing a specific configuration variable, to
54 * be stored in a gperf perfect hashtable */
55 typedef struct ConfigPerfItem
{
56 const char *section_and_lvalue
; /* Section + "." + name of the variable */
57 ConfigParserCallback parse
; /* Function that is called to parse the variable's value */
58 int ltype
; /* Distinguish different variables passed to the same callback */
59 size_t offset
; /* Offset where to store data, from the beginning of userdata */
62 /* Prototype for a low-level gperf lookup function */
63 typedef const ConfigPerfItem
* (*ConfigPerfItemLookup
)(const char *section_and_lvalue
, unsigned length
);
65 /* Prototype for a generic high-level lookup function */
66 typedef int (*ConfigItemLookup
)(
70 ConfigParserCallback
*func
,
75 /* Linear table search implementation of ConfigItemLookup, based on
76 * ConfigTableItem arrays */
77 int config_item_table_lookup(const void *table
, const char *section
, const char *lvalue
, ConfigParserCallback
*func
, int *ltype
, void **data
, void *userdata
);
79 /* gperf implementation of ConfigItemLookup, based on gperf
80 * ConfigPerfItem tables */
81 int config_item_perf_lookup(const void *table
, const char *section
, const char *lvalue
, ConfigParserCallback
*func
, int *ltype
, void **data
, void *userdata
);
87 const char *sections
, /* nulstr */
88 ConfigItemLookup lookup
,
90 ConfigParseFlags flags
,
93 int config_parse_many_nulstr(
94 const char *conf_file
, /* possibly NULL */
95 const char *conf_file_dirs
, /* nulstr */
96 const char *sections
, /* nulstr */
97 ConfigItemLookup lookup
,
99 ConfigParseFlags flags
,
102 int config_parse_many(
103 const char *conf_file
, /* possibly NULL */
104 const char* const* conf_file_dirs
,
105 const char *dropin_dirname
,
106 const char *sections
, /* nulstr */
107 ConfigItemLookup lookup
,
109 ConfigParseFlags flags
,
111 char ***ret_dropins
); /* possibly NULL */
113 CONFIG_PARSER_PROTOTYPE(config_parse_int
);
114 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned
);
115 CONFIG_PARSER_PROTOTYPE(config_parse_long
);
116 CONFIG_PARSER_PROTOTYPE(config_parse_uint8
);
117 CONFIG_PARSER_PROTOTYPE(config_parse_uint16
);
118 CONFIG_PARSER_PROTOTYPE(config_parse_uint32
);
119 CONFIG_PARSER_PROTOTYPE(config_parse_int32
);
120 CONFIG_PARSER_PROTOTYPE(config_parse_uint64
);
121 CONFIG_PARSER_PROTOTYPE(config_parse_double
);
122 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size
);
123 CONFIG_PARSER_PROTOTYPE(config_parse_si_uint64
);
124 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64
);
125 CONFIG_PARSER_PROTOTYPE(config_parse_bool
);
126 CONFIG_PARSER_PROTOTYPE(config_parse_id128
);
127 CONFIG_PARSER_PROTOTYPE(config_parse_tristate
);
128 CONFIG_PARSER_PROTOTYPE(config_parse_string
);
129 CONFIG_PARSER_PROTOTYPE(config_parse_path
);
130 CONFIG_PARSER_PROTOTYPE(config_parse_strv
);
131 CONFIG_PARSER_PROTOTYPE(config_parse_sec
);
132 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity
);
133 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset
);
134 CONFIG_PARSER_PROTOTYPE(config_parse_nsec
);
135 CONFIG_PARSER_PROTOTYPE(config_parse_mode
);
136 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat
);
137 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility
);
138 CONFIG_PARSER_PROTOTYPE(config_parse_log_level
);
139 CONFIG_PARSER_PROTOTYPE(config_parse_signal
);
140 CONFIG_PARSER_PROTOTYPE(config_parse_personality
);
141 CONFIG_PARSER_PROTOTYPE(config_parse_permille
);
142 CONFIG_PARSER_PROTOTYPE(config_parse_ifname
);
143 CONFIG_PARSER_PROTOTYPE(config_parse_ifnames
);
144 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port
);
145 CONFIG_PARSER_PROTOTYPE(config_parse_mtu
);
146 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit
);
147 CONFIG_PARSER_PROTOTYPE(config_parse_vlanprotocol
);
149 typedef enum Disabled
{
150 DISABLED_CONFIGURATION
,
152 DISABLED_EXPERIMENTAL
,
155 #define DEFINE_CONFIG_PARSE(function, parser, msg) \
156 CONFIG_PARSER_PROTOTYPE(function) { \
164 r = parser(rvalue); \
166 log_syntax(unit, LOG_ERR, filename, line, r, \
167 msg ", ignoring: %s", rvalue); \
175 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
176 CONFIG_PARSER_PROTOTYPE(function) { \
185 r = parser(rvalue, i); \
187 log_syntax(unit, LOG_ERR, filename, line, r, \
188 msg ", ignoring: %s", rvalue); \
193 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
194 CONFIG_PARSER_PROTOTYPE(function) { \
202 x = name##_from_string(rvalue); \
204 log_syntax(unit, LOG_ERR, filename, line, 0, \
205 msg ", ignoring: %s", rvalue); \
213 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
214 CONFIG_PARSER_PROTOTYPE(function) { \
222 if (isempty(rvalue)) { \
223 *i = default_value; \
227 x = name##_from_string(rvalue); \
229 log_syntax(unit, LOG_ERR, filename, line, 0, \
230 msg ", ignoring: %s", rvalue); \
238 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
239 CONFIG_PARSER_PROTOTYPE(function) { \
240 type **enums = data, x, *ys; \
241 _cleanup_free_ type *xs = NULL; \
242 const char *word, *state; \
250 xs = new0(type, 1); \
256 FOREACH_WORD(word, l, rvalue, state) { \
257 _cleanup_free_ char *en = NULL; \
260 en = strndup(word, l); \
264 if ((x = name##_from_string(en)) < 0) { \
265 log_syntax(unit, LOG_ERR, filename, line, 0, \
266 msg ", ignoring: %s", en); \
270 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
272 log_syntax(unit, LOG_NOTICE, filename, \
274 "Duplicate entry, ignoring: %s", \
284 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
290 *(xs + i) = invalid; \
293 free_and_replace(*enums, xs); \