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,
18 CONFIG_PARSE_ALLOW_INCLUDE = 1 << 1,
19 CONFIG_PARSE_WARN = 1 << 2,
20 CONFIG_PARSE_REFUSE_BOM = 1 << 3,
23 /* Argument list for parsers of specific configuration settings. */
24 #define CONFIG_PARSER_ARGUMENTS \
26 const char *filename, \
28 const char *section, \
29 unsigned section_line, \
36 /* Prototype for a parser for a specific configuration setting */
37 typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
39 /* A macro declaring the a function prototype, following the typedef above, simply because it's so cumbersomely long
40 * otherwise. (And current emacs gets irritatingly slow when editing files that contain lots of very long function
41 * prototypes on the same screen…) */
42 #define CONFIG_PARSER_PROTOTYPE(name) int name(CONFIG_PARSER_ARGUMENTS)
44 /* Wraps information for parsing a specific configuration variable, to
45 * be stored in a simple array */
46 typedef struct ConfigTableItem {
47 const char *section; /* Section */
48 const char *lvalue; /* Name of the variable */
49 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
50 int ltype; /* Distinguish different variables passed to the same callback */
51 void *data; /* Where to store the variable's data */
54 /* Wraps information for parsing a specific configuration variable, to
55 * be stored in a gperf perfect hashtable */
56 typedef struct ConfigPerfItem {
57 const char *section_and_lvalue; /* Section + "." + name of the variable */
58 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
59 int ltype; /* Distinguish different variables passed to the same callback */
60 size_t offset; /* Offset where to store data, from the beginning of userdata */
63 /* Prototype for a low-level gperf lookup function */
64 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
66 /* Prototype for a generic high-level lookup function */
67 typedef int (*ConfigItemLookup)(
71 ConfigParserCallback *func,
76 /* Linear table search implementation of ConfigItemLookup, based on
77 * ConfigTableItem arrays */
78 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
80 /* gperf implementation of ConfigItemLookup, based on gperf
81 * ConfigPerfItem tables */
82 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
88 const char *sections, /* nulstr */
89 ConfigItemLookup lookup,
91 ConfigParseFlags flags,
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,
100 ConfigParseFlags flags,
103 #if 0 /// UNNEEDED by elogind
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,
111 ConfigParseFlags flags,
115 #if 0 /// UNNEEDED by elogind
117 #if 0 /// UNNEEDED by elogind
119 #if 0 /// UNNEEDED by elogind
121 #if 0 /// UNNEEDED by elogind
123 #if 0 /// UNNEEDED by elogind
125 #if 0 /// UNNEEDED by elogind
127 #if 0 /// UNNEEDED by elogind
129 CONFIG_PARSER_PROTOTYPE(config_parse_int);
130 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
131 CONFIG_PARSER_PROTOTYPE(config_parse_long);
132 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
133 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
134 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
135 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
136 CONFIG_PARSER_PROTOTYPE(config_parse_double);
137 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
138 CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
139 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
140 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
141 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
142 CONFIG_PARSER_PROTOTYPE(config_parse_string);
143 CONFIG_PARSER_PROTOTYPE(config_parse_path);
144 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
145 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
146 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
147 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
148 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
149 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
150 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
151 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
152 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
153 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
154 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
155 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
156 CONFIG_PARSER_PROTOTYPE(config_parse_join_controllers);
157 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
158 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
160 typedef enum Disabled {
161 DISABLED_CONFIGURATION,
163 DISABLED_EXPERIMENTAL,
166 #define DEFINE_CONFIG_PARSE(function, parser, msg) \
167 CONFIG_PARSER_PROTOTYPE(function) { \
175 r = parser(rvalue); \
177 log_syntax(unit, LOG_ERR, filename, line, r, \
178 msg ", ignoring: %s", rvalue); \
186 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
187 CONFIG_PARSER_PROTOTYPE(function) { \
196 r = parser(rvalue, i); \
198 log_syntax(unit, LOG_ERR, filename, line, r, \
199 msg ", ignoring: %s", rvalue); \
204 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
205 CONFIG_PARSER_PROTOTYPE(function) { \
213 x = name##_from_string(rvalue); \
215 log_syntax(unit, LOG_ERR, filename, line, 0, \
216 msg ", ignoring: %s", rvalue); \
224 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
225 CONFIG_PARSER_PROTOTYPE(function) { \
233 if (isempty(rvalue)) { \
234 *i = default_value; \
238 x = name##_from_string(rvalue); \
240 log_syntax(unit, LOG_ERR, filename, line, 0, \
241 msg ", ignoring: %s", rvalue); \
249 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
250 CONFIG_PARSER_PROTOTYPE(function) { \
251 type **enums = data, x, *ys; \
252 _cleanup_free_ type *xs = NULL; \
253 const char *word, *state; \
261 xs = new0(type, 1); \
267 FOREACH_WORD(word, l, rvalue, state) { \
268 _cleanup_free_ char *en = NULL; \
271 en = strndup(word, l); \
275 if ((x = name##_from_string(en)) < 0) { \
276 log_syntax(unit, LOG_ERR, filename, line, 0, \
277 msg ", ignoring: %s", en); \
281 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
283 log_syntax(unit, LOG_NOTICE, filename, \
285 "Duplicate entry, ignoring: %s", \
295 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
301 *(xs + i) = invalid; \
304 free_and_replace(*enums, xs); \