]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/conf-parser.h
Merge pull request #14592 from keszybz/simplifications
[thirdparty/systemd.git] / src / shared / conf-parser.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
ed5bcfbe 3
a8fbdf54 4#include <errno.h>
10e87ee7 5#include <stdbool.h>
a8fbdf54 6#include <stddef.h>
71d35b6b 7#include <stdio.h>
a8fbdf54 8#include <syslog.h>
ed5bcfbe 9
a8fbdf54
TA
10#include "alloc-util.h"
11#include "log.h"
e8e581bf
ZJS
12#include "macro.h"
13
bcde742e
LP
14/* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
15
16typedef enum ConfigParseFlags {
94a404cb
ZJS
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 */
bcde742e 20} ConfigParseFlags;
ed5bcfbe 21
1f12b48a
LP
22/* Argument list for parsers of specific configuration settings. */
23#define CONFIG_PARSER_ARGUMENTS \
24 const char *unit, \
25 const char *filename, \
26 unsigned line, \
27 const char *section, \
28 unsigned section_line, \
29 const char *lvalue, \
30 int ltype, \
31 const char *rvalue, \
32 void *data, \
33 void *userdata
34
f975e971 35/* Prototype for a parser for a specific configuration setting */
1f12b48a
LP
36typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
37
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)
f975e971
LP
42
43/* Wraps information for parsing a specific configuration variable, to
44 * be stored in a simple array */
45typedef 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 */
51} ConfigTableItem;
52
53/* Wraps information for parsing a specific configuration variable, to
e5a7f173 54 * be stored in a gperf perfect hashtable */
f975e971
LP
55typedef 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 */
60} ConfigPerfItem;
61
62/* Prototype for a low-level gperf lookup function */
63typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
64
65/* Prototype for a generic high-level lookup function */
66typedef int (*ConfigItemLookup)(
e9f3d2d5 67 const void *table,
f975e971
LP
68 const char *section,
69 const char *lvalue,
70 ConfigParserCallback *func,
71 int *ltype,
72 void **data,
73 void *userdata);
74
75/* Linear table search implementation of ConfigItemLookup, based on
76 * ConfigTableItem arrays */
e9f3d2d5 77int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
f975e971
LP
78
79/* gperf implementation of ConfigItemLookup, based on gperf
80 * ConfigPerfItem tables */
e9f3d2d5 81int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
f975e971 82
43688c49
ZJS
83int config_parse(
84 const char *unit,
85 const char *filename,
86 FILE *f,
87 const char *sections, /* nulstr */
88 ConfigItemLookup lookup,
89 const void *table,
bcde742e 90 ConfigParseFlags flags,
43688c49
ZJS
91 void *userdata);
92
93int 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,
98 const void *table,
bcde742e 99 ConfigParseFlags flags,
43688c49 100 void *userdata);
e8461023 101
23bb31aa
ZJS
102int 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,
108 const void *table,
bcde742e 109 ConfigParseFlags flags,
23bb31aa
ZJS
110 void *userdata);
111
1f12b48a
LP
112CONFIG_PARSER_PROTOTYPE(config_parse_int);
113CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
114CONFIG_PARSER_PROTOTYPE(config_parse_long);
115CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
116CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
117CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
118CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
119CONFIG_PARSER_PROTOTYPE(config_parse_double);
120CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
121CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
122CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
123CONFIG_PARSER_PROTOTYPE(config_parse_bool);
124CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
125CONFIG_PARSER_PROTOTYPE(config_parse_string);
126CONFIG_PARSER_PROTOTYPE(config_parse_path);
127CONFIG_PARSER_PROTOTYPE(config_parse_strv);
128CONFIG_PARSER_PROTOTYPE(config_parse_sec);
7b61ce3c 129CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
dc653bf4 130CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset);
1f12b48a
LP
131CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
132CONFIG_PARSER_PROTOTYPE(config_parse_mode);
133CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
134CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
135CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
136CONFIG_PARSER_PROTOTYPE(config_parse_signal);
137CONFIG_PARSER_PROTOTYPE(config_parse_personality);
c07b23ca 138CONFIG_PARSER_PROTOTYPE(config_parse_permille);
1f12b48a 139CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
a5053a15 140CONFIG_PARSER_PROTOTYPE(config_parse_ifnames);
1f12b48a 141CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
1f12b48a
LP
142CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
143CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
e8e581bf 144
1e35c5ab
RP
145typedef enum Disabled {
146 DISABLED_CONFIGURATION,
147 DISABLED_LEGACY,
148 DISABLED_EXPERIMENTAL,
149} Disabled;
150
2d1729ca
YW
151#define DEFINE_CONFIG_PARSE(function, parser, msg) \
152 CONFIG_PARSER_PROTOTYPE(function) { \
153 int *i = data, r; \
154 \
155 assert(filename); \
156 assert(lvalue); \
157 assert(rvalue); \
158 assert(data); \
159 \
160 r = parser(rvalue); \
161 if (r < 0) { \
162 log_syntax(unit, LOG_ERR, filename, line, r, \
163 msg ", ignoring: %s", rvalue); \
164 return 0; \
165 } \
166 \
167 *i = r; \
168 return 0; \
169 }
170
171#define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
172 CONFIG_PARSER_PROTOTYPE(function) { \
173 type *i = data; \
174 int r; \
175 \
176 assert(filename); \
177 assert(lvalue); \
178 assert(rvalue); \
179 assert(data); \
180 \
181 r = parser(rvalue, i); \
182 if (r < 0) \
183 log_syntax(unit, LOG_ERR, filename, line, r, \
184 msg ", ignoring: %s", rvalue); \
185 \
186 return 0; \
187 }
188
189#define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
190 CONFIG_PARSER_PROTOTYPE(function) { \
191 type *i = data, x; \
192 \
193 assert(filename); \
194 assert(lvalue); \
195 assert(rvalue); \
196 assert(data); \
197 \
198 x = name##_from_string(rvalue); \
199 if (x < 0) { \
200 log_syntax(unit, LOG_ERR, filename, line, 0, \
201 msg ", ignoring: %s", rvalue); \
202 return 0; \
203 } \
204 \
205 *i = x; \
206 return 0; \
207 }
208
209#define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
1f12b48a 210 CONFIG_PARSER_PROTOTYPE(function) { \
487393e9
LP
211 type *i = data, x; \
212 \
213 assert(filename); \
214 assert(lvalue); \
215 assert(rvalue); \
216 assert(data); \
217 \
2d1729ca
YW
218 if (isempty(rvalue)) { \
219 *i = default_value; \
220 return 0; \
221 } \
222 \
223 x = name##_from_string(rvalue); \
224 if (x < 0) { \
225 log_syntax(unit, LOG_ERR, filename, line, 0, \
e8e581bf 226 msg ", ignoring: %s", rvalue); \
c0b34696 227 return 0; \
487393e9
LP
228 } \
229 \
230 *i = x; \
487393e9
LP
231 return 0; \
232 }
916484f5 233
2d1729ca 234#define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
1f12b48a 235 CONFIG_PARSER_PROTOTYPE(function) { \
77c10205
TG
236 type **enums = data, x, *ys; \
237 _cleanup_free_ type *xs = NULL; \
a2a5291b 238 const char *word, *state; \
916484f5
TG
239 size_t l, i = 0; \
240 \
241 assert(filename); \
242 assert(lvalue); \
243 assert(rvalue); \
244 assert(data); \
245 \
246 xs = new0(type, 1); \
9ed794a3 247 if (!xs) \
83e341a6
TG
248 return -ENOMEM; \
249 \
916484f5
TG
250 *xs = invalid; \
251 \
a2a5291b 252 FOREACH_WORD(word, l, rvalue, state) { \
916484f5 253 _cleanup_free_ char *en = NULL; \
77c10205 254 type *new_xs; \
916484f5 255 \
a2a5291b 256 en = strndup(word, l); \
916484f5
TG
257 if (!en) \
258 return -ENOMEM; \
259 \
260 if ((x = name##_from_string(en)) < 0) { \
2d1729ca
YW
261 log_syntax(unit, LOG_ERR, filename, line, 0, \
262 msg ", ignoring: %s", en); \
916484f5
TG
263 continue; \
264 } \
265 \
266 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
267 if (*ys == x) { \
2d1729ca
YW
268 log_syntax(unit, LOG_NOTICE, filename, \
269 line, 0, \
270 "Duplicate entry, ignoring: %s", \
271 en); \
916484f5
TG
272 x = invalid; \
273 } \
274 } \
275 \
276 if (x == invalid) \
277 continue; \
278 \
279 *(xs + i) = x; \
77c10205
TG
280 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
281 if (new_xs) \
282 xs = new_xs; \
283 else \
916484f5 284 return -ENOMEM; \
83e341a6 285 \
916484f5
TG
286 *(xs + i) = invalid; \
287 } \
288 \
5f92e517 289 free_and_replace(*enums, xs); \
916484f5
TG
290 return 0; \
291 }