]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / shared / conf-parser.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <errno.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <syslog.h>
9
10 #include "alloc-util.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "time-util.h"
14
15 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
16
17 typedef enum ConfigParseFlags {
18 CONFIG_PARSE_RELAXED = 1 << 0, /* Do not warn about unknown non-extension fields */
19 CONFIG_PARSE_WARN = 1 << 1, /* Emit non-debug messages */
20 } ConfigParseFlags;
21
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
35 /* Prototype for a parser for a specific configuration setting */
36 typedef 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)
42
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 */
51 } ConfigTableItem;
52
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 */
60 } ConfigPerfItem;
61
62 /* Prototype for a low-level gperf lookup function */
63 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
64
65 /* Prototype for a generic high-level lookup function */
66 typedef int (*ConfigItemLookup)(
67 const void *table,
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 */
77 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
78
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);
82
83 int 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,
90 ConfigParseFlags flags,
91 void *userdata,
92 usec_t *ret_mtime); /* possibly NULL */
93
94 int config_parse_many_nulstr(
95 const char *conf_file, /* possibly NULL */
96 const char *conf_file_dirs, /* nulstr */
97 const char *sections, /* nulstr */
98 ConfigItemLookup lookup,
99 const void *table,
100 ConfigParseFlags flags,
101 void *userdata,
102 usec_t *ret_mtime); /* possibly NULL */
103
104 int config_parse_many(
105 const char *conf_file, /* possibly NULL */
106 const char* const* conf_file_dirs,
107 const char *dropin_dirname,
108 const char *sections, /* nulstr */
109 ConfigItemLookup lookup,
110 const void *table,
111 ConfigParseFlags flags,
112 void *userdata,
113 usec_t *ret_mtime); /* possibly NULL */
114
115 CONFIG_PARSER_PROTOTYPE(config_parse_int);
116 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
117 CONFIG_PARSER_PROTOTYPE(config_parse_long);
118 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
119 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
120 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
121 CONFIG_PARSER_PROTOTYPE(config_parse_int32);
122 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
123 CONFIG_PARSER_PROTOTYPE(config_parse_double);
124 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
125 CONFIG_PARSER_PROTOTYPE(config_parse_si_uint64);
126 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
127 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
128 CONFIG_PARSER_PROTOTYPE(config_parse_id128);
129 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
130 CONFIG_PARSER_PROTOTYPE(config_parse_string);
131 CONFIG_PARSER_PROTOTYPE(config_parse_path);
132 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
133 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
134 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
135 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset);
136 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
137 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
138 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
139 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
140 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
141 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
142 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
143 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
144 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
145 CONFIG_PARSER_PROTOTYPE(config_parse_ifnames);
146 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
147 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
148 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
149 CONFIG_PARSER_PROTOTYPE(config_parse_vlanprotocol);
150 CONFIG_PARSER_PROTOTYPE(config_parse_percent);
151
152 typedef enum Disabled {
153 DISABLED_CONFIGURATION,
154 DISABLED_LEGACY,
155 DISABLED_EXPERIMENTAL,
156 } Disabled;
157
158 #define DEFINE_CONFIG_PARSE(function, parser, msg) \
159 CONFIG_PARSER_PROTOTYPE(function) { \
160 int *i = data, r; \
161 \
162 assert(filename); \
163 assert(lvalue); \
164 assert(rvalue); \
165 assert(data); \
166 \
167 r = parser(rvalue); \
168 if (r < 0) { \
169 log_syntax(unit, LOG_WARNING, filename, line, r, \
170 msg ", ignoring: %s", rvalue); \
171 return 0; \
172 } \
173 \
174 *i = r; \
175 return 0; \
176 }
177
178 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
179 CONFIG_PARSER_PROTOTYPE(function) { \
180 type *i = data; \
181 int r; \
182 \
183 assert(filename); \
184 assert(lvalue); \
185 assert(rvalue); \
186 assert(data); \
187 \
188 r = parser(rvalue, i); \
189 if (r < 0) \
190 log_syntax(unit, LOG_WARNING, filename, line, r, \
191 msg ", ignoring: %s", rvalue); \
192 \
193 return 0; \
194 }
195
196 #define DEFINE_CONFIG_PARSE_ENUM_FULL(function, from_string, type, msg) \
197 CONFIG_PARSER_PROTOTYPE(function) { \
198 type *i = data, x; \
199 \
200 assert(filename); \
201 assert(lvalue); \
202 assert(rvalue); \
203 assert(data); \
204 \
205 x = from_string(rvalue); \
206 if (x < 0) { \
207 log_syntax(unit, LOG_WARNING, filename, line, 0, \
208 msg ", ignoring: %s", rvalue); \
209 return 0; \
210 } \
211 \
212 *i = x; \
213 return 0; \
214 }
215
216 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
217 DEFINE_CONFIG_PARSE_ENUM_FULL(function, name##_from_string, type, msg)
218
219 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
220 CONFIG_PARSER_PROTOTYPE(function) { \
221 type *i = data, x; \
222 \
223 assert(filename); \
224 assert(lvalue); \
225 assert(rvalue); \
226 assert(data); \
227 \
228 if (isempty(rvalue)) { \
229 *i = default_value; \
230 return 0; \
231 } \
232 \
233 x = name##_from_string(rvalue); \
234 if (x < 0) { \
235 log_syntax(unit, LOG_WARNING, filename, line, 0, \
236 msg ", ignoring: %s", rvalue); \
237 return 0; \
238 } \
239 \
240 *i = x; \
241 return 0; \
242 }
243
244 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
245 CONFIG_PARSER_PROTOTYPE(function) { \
246 type **enums = data; \
247 _cleanup_free_ type *xs = NULL; \
248 size_t i = 0; \
249 int r; \
250 \
251 assert(filename); \
252 assert(lvalue); \
253 assert(rvalue); \
254 assert(data); \
255 \
256 xs = new0(type, 1); \
257 if (!xs) \
258 return -ENOMEM; \
259 \
260 *xs = invalid; \
261 \
262 for (const char *p = rvalue;;) { \
263 _cleanup_free_ char *en = NULL; \
264 type x, *new_xs; \
265 \
266 r = extract_first_word(&p, &en, NULL, 0); \
267 if (r == -ENOMEM) \
268 return log_oom(); \
269 if (r < 0) \
270 return log_syntax(unit, LOG_ERR, filename, line, 0, \
271 msg ": %s", en); \
272 if (r == 0) \
273 break; \
274 \
275 if ((x = name##_from_string(en)) < 0) { \
276 log_syntax(unit, LOG_WARNING, filename, line, 0, \
277 msg ", ignoring: %s", en); \
278 continue; \
279 } \
280 \
281 for (type *ys = xs; x != invalid && *ys != invalid; ys++) \
282 if (*ys == x) { \
283 log_syntax(unit, LOG_NOTICE, filename, line, 0, \
284 "Duplicate entry, ignoring: %s", \
285 en); \
286 x = invalid; \
287 } \
288 \
289 if (x == invalid) \
290 continue; \
291 \
292 *(xs + i) = x; \
293 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
294 if (new_xs) \
295 xs = new_xs; \
296 else \
297 return log_oom(); \
298 \
299 *(xs + i) = invalid; \
300 } \
301 \
302 return free_and_replace(*enums, xs); \
303 }