1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright 2010 Lennart Poettering
14 #include "alloc-util.h"
18 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
20 typedef enum ConfigParseFlags {
21 CONFIG_PARSE_RELAXED = 1 << 0,
22 CONFIG_PARSE_ALLOW_INCLUDE = 1 << 1,
23 CONFIG_PARSE_WARN = 1 << 2,
24 CONFIG_PARSE_REFUSE_BOM = 1 << 3,
27 /* Argument list for parsers of specific configuration settings. */
28 #define CONFIG_PARSER_ARGUMENTS \
30 const char *filename, \
32 const char *section, \
33 unsigned section_line, \
40 /* Prototype for a parser for a specific configuration setting */
41 typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
43 /* A macro declaring the a function prototype, following the typedef above, simply because it's so cumbersomely long
44 * otherwise. (And current emacs gets irritatingly slow when editing files that contain lots of very long function
45 * prototypes on the same screen…) */
46 #define CONFIG_PARSER_PROTOTYPE(name) int name(CONFIG_PARSER_ARGUMENTS)
48 /* Wraps information for parsing a specific configuration variable, to
49 * be stored in a simple array */
50 typedef struct ConfigTableItem {
51 const char *section; /* Section */
52 const char *lvalue; /* Name of the variable */
53 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
54 int ltype; /* Distinguish different variables passed to the same callback */
55 void *data; /* Where to store the variable's data */
58 /* Wraps information for parsing a specific configuration variable, to
59 * be stored in a gperf perfect hashtable */
60 typedef struct ConfigPerfItem {
61 const char *section_and_lvalue; /* Section + "." + name of the variable */
62 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
63 int ltype; /* Distinguish different variables passed to the same callback */
64 size_t offset; /* Offset where to store data, from the beginning of userdata */
67 /* Prototype for a low-level gperf lookup function */
68 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
70 /* Prototype for a generic high-level lookup function */
71 typedef int (*ConfigItemLookup)(
75 ConfigParserCallback *func,
80 /* Linear table search implementation of ConfigItemLookup, based on
81 * ConfigTableItem arrays */
82 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
84 /* gperf implementation of ConfigItemLookup, based on gperf
85 * ConfigPerfItem tables */
86 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
92 const char *sections, /* nulstr */
93 ConfigItemLookup lookup,
95 ConfigParseFlags flags,
98 int config_parse_many_nulstr(
99 const char *conf_file, /* possibly NULL */
100 const char *conf_file_dirs, /* nulstr */
101 const char *sections, /* nulstr */
102 ConfigItemLookup lookup,
104 ConfigParseFlags flags,
107 #if 0 /// UNNEEDED by elogind
108 int config_parse_many(
109 const char *conf_file, /* possibly NULL */
110 const char* const* conf_file_dirs,
111 const char *dropin_dirname,
112 const char *sections, /* nulstr */
113 ConfigItemLookup lookup,
115 ConfigParseFlags flags,
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 #if 0 /// UNNEEDED by elogind
131 #if 0 /// UNNEEDED by elogind
133 CONFIG_PARSER_PROTOTYPE(config_parse_int);
134 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
135 CONFIG_PARSER_PROTOTYPE(config_parse_long);
136 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
137 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
138 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
139 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
140 CONFIG_PARSER_PROTOTYPE(config_parse_double);
141 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
142 CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
143 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
144 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
145 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
146 CONFIG_PARSER_PROTOTYPE(config_parse_string);
147 CONFIG_PARSER_PROTOTYPE(config_parse_path);
148 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
149 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
150 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
151 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
152 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
153 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
154 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
155 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
156 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
157 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
158 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
159 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
160 CONFIG_PARSER_PROTOTYPE(config_parse_join_controllers);
161 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
162 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
164 typedef enum Disabled {
165 DISABLED_CONFIGURATION,
167 DISABLED_EXPERIMENTAL,
170 #define DEFINE_CONFIG_PARSE(function, parser, msg) \
171 CONFIG_PARSER_PROTOTYPE(function) { \
179 r = parser(rvalue); \
181 log_syntax(unit, LOG_ERR, filename, line, r, \
182 msg ", ignoring: %s", rvalue); \
190 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
191 CONFIG_PARSER_PROTOTYPE(function) { \
200 r = parser(rvalue, i); \
202 log_syntax(unit, LOG_ERR, filename, line, r, \
203 msg ", ignoring: %s", rvalue); \
208 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
209 CONFIG_PARSER_PROTOTYPE(function) { \
217 x = name##_from_string(rvalue); \
219 log_syntax(unit, LOG_ERR, filename, line, 0, \
220 msg ", ignoring: %s", rvalue); \
228 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
229 CONFIG_PARSER_PROTOTYPE(function) { \
237 if (isempty(rvalue)) { \
238 *i = default_value; \
242 x = name##_from_string(rvalue); \
244 log_syntax(unit, LOG_ERR, filename, line, 0, \
245 msg ", ignoring: %s", rvalue); \
253 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
254 CONFIG_PARSER_PROTOTYPE(function) { \
255 type **enums = data, x, *ys; \
256 _cleanup_free_ type *xs = NULL; \
257 const char *word, *state; \
265 xs = new0(type, 1); \
271 FOREACH_WORD(word, l, rvalue, state) { \
272 _cleanup_free_ char *en = NULL; \
275 en = strndup(word, l); \
279 if ((x = name##_from_string(en)) < 0) { \
280 log_syntax(unit, LOG_ERR, filename, line, 0, \
281 msg ", ignoring: %s", en); \
285 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
287 log_syntax(unit, LOG_NOTICE, filename, \
289 "Duplicate entry, ignoring: %s", \
299 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
305 *(xs + i) = invalid; \
308 free_and_replace(*enums, xs); \