Debian Codegraph @

src/shared/conf-parser.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <syslog.h>
5
6 #include "alloc-util.h"
7 #include "conf-parser-forward.h"
8 #include "fd-util.h"
9 #include "log.h"
10
11 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
12
13 typedef enum ConfigParseFlags {
14 CONFIG_PARSE_RELAXED = 1 << 0, /* Do not warn about unknown non-extension fields */
15 CONFIG_PARSE_WARN = 1 << 1, /* Emit non-debug messages */
16 } ConfigParseFlags;
17
18 /* Wraps information for parsing a specific configuration variable, to
19 * be stored in a simple array */
20 typedef struct ConfigTableItem {
21 const char *section; /* Section */
22 const char *lvalue; /* Name of the variable */
23 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
24 int ltype; /* Distinguish different variables passed to the same callback */
25 void *data; /* Where to store the variable's data */
26 } ConfigTableItem;
27
28 /* Wraps information for parsing a specific configuration variable, to
29 * be stored in a gperf perfect hashtable */
30 typedef struct ConfigPerfItem {
31 const char *section_and_lvalue; /* Section + "." + name of the variable */
32 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
33 int ltype; /* Distinguish different variables passed to the same callback */
34 size_t offset; /* Offset where to store data, from the beginning of userdata */
35 } ConfigPerfItem;
36
37 /* Prototype for a low-level gperf lookup function */
38 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, GPERF_LEN_TYPE length);
39
40 /* Prototype for a generic high-level lookup function */
41 typedef int (*ConfigItemLookup)(
42 const void *table,
43 const char *section,
44 const char *lvalue,
45 ConfigParserCallback *ret_func,
46 int *ret_ltype,
47 void **ret_data,
48 void *userdata);
49
50 /* Linear table search implementation of ConfigItemLookup, based on
51 * ConfigTableItem arrays */
52 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *ret_func, int *ret_ltype, void **ret_data, void *userdata);
53
54 /* gperf implementation of ConfigItemLookup, based on gperf
55 * ConfigPerfItem tables */
56 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *ret_func, int *ret_ltype, void **ret_data, void *userdata);
57
58 int config_parse(
59 const char *unit,
60 const char *filename,
61 FILE *f,
62 const char *sections, /* nulstr */
63 ConfigItemLookup lookup,
64 const void *table,
65 ConfigParseFlags flags,
66 void *userdata,
67 struct stat *ret_stat); /* possibly NULL */
68
69 int config_parse_many_full(
70 const char* const* conf_files, /* possibly empty */
71 const char* const* conf_file_dirs,
72 const char *dropin_dirname,
73 const char *root,
74 int root_fd,
75 const char *sections, /* nulstr */
76 ConfigItemLookup lookup,
77 const void *table,
78 ConfigParseFlags flags,
79 void *userdata,
80 Hashmap **ret_stats_by_path, /* possibly NULL */
81 char ***ret_dropin_files); /* possibly NULL */
82
83 static inline int config_parse_many(
84 const char* const* conf_files, /* possibly empty */
85 const char* const* conf_file_dirs,
86 const char *dropin_dirname,
87 const char *sections, /* nulstr */
88 ConfigItemLookup lookup,
89 const void *table,
90 ConfigParseFlags flags,
91 void *userdata) {
92
93 return config_parse_many_full(
94 conf_files,
95 conf_file_dirs,
96 dropin_dirname,
97 /* root= */ NULL,
98 /* root_fd= */ XAT_FDROOT,
99 sections,
100 lookup,
101 table,
102 flags,
103 userdata,
104 /* ret_stats_by_path= */ NULL,
105 /* ret_drop_in_files= */ NULL);
106 }
107
108 int config_parse_standard_file_with_dropins_full(
109 const char *root,
110 int root_fd,
111 const char *main_file, /* A path like "systemd/frobnicator.conf" */
112 const char *sections,
113 ConfigItemLookup lookup,
114 const void *table,
115 ConfigParseFlags flags,
116 void *userdata,
117 Hashmap **ret_stats_by_path, /* possibly NULL */
118 char ***ret_dropin_files); /* possibly NULL */
119
120 static inline int config_parse_standard_file_with_dropins(
121 const char *main_file, /* A path like "systemd/frobnicator.conf" */
122 const char *sections, /* nulstr */
123 ConfigItemLookup lookup,
124 const void *table,
125 ConfigParseFlags flags,
126 void *userdata) {
127 return config_parse_standard_file_with_dropins_full(
128 /* root= */ NULL,
129 /* root_fd= */ XAT_FDROOT,
130 main_file,
131 sections,
132 lookup,
133 table,
134 flags,
135 userdata,
136 /* ret_stats_by_path= */ NULL,
137 /* ret_dropin_files= */ NULL);
138 }
139
140 int config_get_stats_by_path(
141 const char *suffix,
142 const char *root,
143 ConfFilesFlags flags,
144 const char* const* dirs,
145 bool check_dropins,
146 Hashmap **ret);
147
148 int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st);
149 bool stats_by_path_equal(Hashmap *a, Hashmap *b);
150
151 typedef struct ConfigSectionParser {
152 ConfigParserCallback parser;
153 int ltype;
154 size_t offset;
155 } ConfigSectionParser;
156
157 int config_section_parse(
158 const ConfigSectionParser *parsers,
159 size_t n_parsers,
160 const char *unit,
161 const char *filename,
162 unsigned line,
163 const char *section,
164 unsigned section_line,
165 const char *lvalue,
166 int ltype,
167 const char *rvalue,
168 void *userdata);
169
170 static inline ConfigSection* config_section_free(ConfigSection *cs) {
171 return mfree(cs);
172 }
173 DEFINE_TRIVIAL_CLEANUP_FUNC(ConfigSection*, config_section_free);
174
175 int config_section_new(const char *filename, unsigned line, ConfigSection **ret);
176
177 void config_section_hash_func(const ConfigSection *c, struct siphash *state);
178 int config_section_compare_func(const ConfigSection *x, const ConfigSection *y);
179 extern const struct hash_ops config_section_hash_ops;
180
181 int hashmap_by_section_find_unused_line(Hashmap *entries_by_section, const char *filename, unsigned *ret);
182 int ordered_hashmap_by_section_find_unused_line(OrderedHashmap *entries_by_section, const char *filename, unsigned *ret);
183
184 static inline bool section_is_invalid(ConfigSection *section) {
185 /* If this returns false, then it does _not_ mean the section is valid. */
186
187 if (!section)
188 return false;
189
190 return section->invalid;
191 }
192
193 #define log_section_full_errno_zerook(section, level, error, ...) \
194 ({ \
195 const ConfigSection *_s = (section); \
196 log_syntax(/* unit= */ NULL, \
197 level, \
198 _s ? _s->filename : NULL, \
199 _s ? _s->line : 0, \
200 error, \
201 __VA_ARGS__); \
202 })
203
204 #define log_section_full_errno(section, level, error, ...) \
205 ({ \
206 int _error = (error); \
207 ASSERT_NON_ZERO(_error); \
208 log_section_full_errno_zerook(section, level, _error, __VA_ARGS__); \
209 })
210
211 #define log_section_full(section, level, fmt, ...) \
212 ({ \
213 if (BUILD_MODE_DEVELOPER) \
214 assert(!strstr(fmt, "%m")); \
215 (void) log_section_full_errno_zerook(section, level, 0, fmt, ##__VA_ARGS__); \
216 })
217
218 #define log_section_debug(section, ...) log_section_full(section, LOG_DEBUG, __VA_ARGS__)
219 #define log_section_info(section, ...) log_section_full(section, LOG_INFO, __VA_ARGS__)
220 #define log_section_notice(section, ...) log_section_full(section, LOG_NOTICE, __VA_ARGS__)
221 #define log_section_warning(section, ...) log_section_full(section, LOG_WARNING, __VA_ARGS__)
222 #define log_section_error(section, ...) log_section_full(section, LOG_ERR, __VA_ARGS__)
223
224 #define log_section_debug_errno(section, error, ...) log_section_full_errno(section, LOG_DEBUG, error, __VA_ARGS__)
225 #define log_section_info_errno(section, error, ...) log_section_full_errno(section, LOG_INFO, error, __VA_ARGS__)
226 #define log_section_notice_errno(section, error, ...) log_section_full_errno(section, LOG_NOTICE, error, __VA_ARGS__)
227 #define log_section_warning_errno(section, error, ...) log_section_full_errno(section, LOG_WARNING, error, __VA_ARGS__)
228 #define log_section_error_errno(section, error, ...) log_section_full_errno(section, LOG_ERR, error, __VA_ARGS__)
229
230 CONFIG_PARSER_PROTOTYPE(config_parse_int);
231 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
232 CONFIG_PARSER_PROTOTYPE(config_parse_long);
233 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
234 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
235 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
236 CONFIG_PARSER_PROTOTYPE(config_parse_int32);
237 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
238 CONFIG_PARSER_PROTOTYPE(config_parse_double);
239 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
240 CONFIG_PARSER_PROTOTYPE(config_parse_si_uint64);
241 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
242 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64_infinity);
243 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
244 CONFIG_PARSER_PROTOTYPE(config_parse_uint32_flag);
245 CONFIG_PARSER_PROTOTYPE(config_parse_uint32_invert_flag);
246 CONFIG_PARSER_PROTOTYPE(config_parse_id128);
247 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
248 CONFIG_PARSER_PROTOTYPE(config_parse_string);
249 CONFIG_PARSER_PROTOTYPE(config_parse_dns_name);
250 CONFIG_PARSER_PROTOTYPE(config_parse_hostname);
251 CONFIG_PARSER_PROTOTYPE(config_parse_path);
252 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
253 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
254 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
255 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset);
256 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
257 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
258 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
259 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
260 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
261 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
262 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
263 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
264 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
265 CONFIG_PARSER_PROTOTYPE(config_parse_ifnames);
266 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
267 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
268 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
269 CONFIG_PARSER_PROTOTYPE(config_parse_vlanprotocol);
270 CONFIG_PARSER_PROTOTYPE(config_parse_hw_addr);
271 CONFIG_PARSER_PROTOTYPE(config_parse_hw_addrs);
272 CONFIG_PARSER_PROTOTYPE(config_parse_ether_addr);
273 CONFIG_PARSER_PROTOTYPE(config_parse_ether_addrs);
274 CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_non_null);
275 CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_data);
276 CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_prefix);
277 CONFIG_PARSER_PROTOTYPE(config_parse_percent);
278 CONFIG_PARSER_PROTOTYPE(config_parse_permyriad);
279 CONFIG_PARSER_PROTOTYPE(config_parse_pid);
280 CONFIG_PARSER_PROTOTYPE(config_parse_sec_fix_0);
281 CONFIG_PARSER_PROTOTYPE(config_parse_timezone);
282 CONFIG_PARSER_PROTOTYPE(config_parse_calendar);
283 CONFIG_PARSER_PROTOTYPE(config_parse_ip_protocol);
284 CONFIG_PARSER_PROTOTYPE(config_parse_loadavg);
285
286 typedef enum Disabled {
287 DISABLED_CONFIGURATION,
288 DISABLED_LEGACY,
289 DISABLED_EXPERIMENTAL,
290 } Disabled;
291
292 typedef enum ConfigParseStringFlags {
293 CONFIG_PARSE_STRING_SAFE = 1 << 0,
294 CONFIG_PARSE_STRING_ASCII = 1 << 1,
295
296 CONFIG_PARSE_STRING_SAFE_AND_ASCII = CONFIG_PARSE_STRING_SAFE | CONFIG_PARSE_STRING_ASCII,
297 } ConfigParseStringFlags;
298
299 #define DEFINE_CONFIG_PARSE(function, parser) \
300 CONFIG_PARSER_PROTOTYPE(function) { \
301 int *i = data, r; \
302 \
303 assert(filename); \
304 assert(lvalue); \
305 assert(rvalue); \
306 assert(data); \
307 \
308 r = parser(rvalue); \
309 if (r < 0) \
310 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); \
311 \
312 *i = r; \
313 return 1; \
314 }
315
316 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type) \
317 CONFIG_PARSER_PROTOTYPE(function) { \
318 type *i = ASSERT_PTR(data); \
319 int r; \
320 \
321 assert(filename); \
322 assert(lvalue); \
323 assert(rvalue); \
324 \
325 r = parser(rvalue, i); \
326 if (r < 0) \
327 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); \
328 \
329 return 1; \
330 }
331
332 #define DEFINE_CONFIG_PARSE_ENUM_FULL(function, from_string, type) \
333 CONFIG_PARSER_PROTOTYPE(function) { \
334 type *i = data, x; \
335 \
336 assert(filename); \
337 assert(lvalue); \
338 assert(rvalue); \
339 assert(data); \
340 \
341 x = from_string(rvalue); \
342 if (x < 0) \
343 return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue); \
344 \
345 *i = x; \
346 return 1; \
347 }
348
349 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type) \
350 DEFINE_CONFIG_PARSE_ENUM_FULL(function, name##_from_string, type)
351
352 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value) \
353 CONFIG_PARSER_PROTOTYPE(function) { \
354 type *i = data, x; \
355 \
356 assert(filename); \
357 assert(lvalue); \
358 assert(rvalue); \
359 assert(data); \
360 \
361 if (isempty(rvalue)) { \
362 *i = default_value; \
363 return 1; \
364 } \
365 \
366 x = name##_from_string(rvalue); \
367 if (x < 0) \
368 return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue); \
369 \
370 *i = x; \
371 return 1; \
372 }
373
374 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid) \
375 CONFIG_PARSER_PROTOTYPE(function) { \
376 type **enums = ASSERT_PTR(data); \
377 _cleanup_free_ type *xs = NULL; \
378 size_t n = 0; \
379 int r; \
380 \
381 assert(lvalue); \
382 \
383 for (const char *p = rvalue;;) { \
384 _cleanup_free_ char *en = NULL; \
385 type x; \
386 \
387 r = extract_first_word(&p, &en, NULL, 0); \
388 if (r < 0) \
389 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); \
390 if (r == 0) \
391 break; \
392 \
393 x = name##_from_string(en); \
394 if (x < 0) { \
395 log_syntax(unit, LOG_WARNING, filename, line, x, \
396 "Failed to parse %s in %s=, ignoring.", \
397 en, lvalue); \
398 continue; \
399 } \
400 \
401 FOREACH_ARRAY(i, xs, n) \
402 if (*i == x) { \
403 log_syntax(unit, LOG_NOTICE, filename, line, 0, \
404 "Duplicate entry %s in %s=, ignoring.", \
405 en, lvalue); \
406 x = invalid; \
407 break; \
408 } \
409 \
410 if (x == invalid) \
411 continue; \
412 \
413 /* Allocate one more for the trailing 'invalid'. */ \
414 if (!GREEDY_REALLOC(xs, n + 2)) \
415 return log_oom(); \
416 \
417 xs[n++] = x; \
418 } \
419 \
420 if (n <= 0) { \
421 /* An empty string, or invalid values only. */ \
422 *enums = mfree(*enums); \
423 return 1; \
424 } \
425 \
426 /* Terminate with 'invalid' */ \
427 xs[n] = invalid; \
428 free_and_replace(*enums, xs); \
429 return 1; \
430 }
431
432 int config_parse_unsigned_bounded(
433 const char *unit,
434 const char *filename,
435 unsigned line,
436 const char *section,
437 unsigned section_line,
438 const char *lvalue,
439 const char *rvalue,
440 unsigned min,
441 unsigned max,
442 bool ignoring,
443 unsigned *ret);
444
445 static inline int config_parse_uint32_bounded(
446 const char *unit,
447 const char *filename,
448 unsigned line,
449 const char *section,
450 unsigned section_line,
451 const char *lvalue,
452 const char *rvalue,
453 uint32_t min,
454 uint32_t max,
455 bool ignoring,
456 uint32_t *ret) {
457
458 unsigned t;
459 int r;
460
461 r = config_parse_unsigned_bounded(
462 unit, filename, line, section, section_line, lvalue, rvalue,
463 min, max, ignoring,
464 &t);
465 if (r <= 0)
466 return r;
467 assert(t <= UINT32_MAX);
468 *ret = t;
469 return 1;
470 }
471
472 static inline int config_parse_uint16_bounded(
473 const char *unit,
474 const char *filename,
475 unsigned line,
476 const char *section,
477 unsigned section_line,
478 const char *lvalue,
479 const char *rvalue,
480 uint16_t min,
481 uint16_t max,
482 bool ignoring,
483 uint16_t *ret) {
484
485 unsigned t;
486 int r;
487
488 r = config_parse_unsigned_bounded(
489 unit, filename, line, section, section_line, lvalue, rvalue,
490 min, max, ignoring,
491 &t);
492 if (r <= 0)
493 return r;
494 assert(t <= UINT16_MAX);
495 *ret = t;
496 return 1;
497 }
498
499 static inline int config_parse_uint8_bounded(
500 const char *unit,
501 const char *filename,
502 unsigned line,
503 const char *section,
504 unsigned section_line,
505 const char *lvalue,
506 const char *rvalue,
507 uint8_t min,
508 uint8_t max,
509 bool ignoring,
510 uint8_t *ret) {
511
512 unsigned t;
513 int r;
514
515 r = config_parse_unsigned_bounded(
516 unit, filename, line, section, section_line, lvalue, rvalue,
517 min, max, ignoring,
518 &t);
519 if (r <= 0)
520 return r;
521 assert(t <= UINT8_MAX);
522 *ret = t;
523 return 1;
524 }