]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
Merge pull request #13365 from keszybz/fix-commits-from-pr-13246
[thirdparty/systemd.git] / src / shared / conf-parser.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
14 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
15
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,
21 } ConfigParseFlags;
22
23 /* Argument list for parsers of specific configuration settings. */
24 #define CONFIG_PARSER_ARGUMENTS \
25 const char *unit, \
26 const char *filename, \
27 unsigned line, \
28 const char *section, \
29 unsigned section_line, \
30 const char *lvalue, \
31 int ltype, \
32 const char *rvalue, \
33 void *data, \
34 void *userdata
35
36 /* Prototype for a parser for a specific configuration setting */
37 typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
38
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)
43
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 */
52 } ConfigTableItem;
53
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 */
61 } ConfigPerfItem;
62
63 /* Prototype for a low-level gperf lookup function */
64 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
65
66 /* Prototype for a generic high-level lookup function */
67 typedef int (*ConfigItemLookup)(
68 const void *table,
69 const char *section,
70 const char *lvalue,
71 ConfigParserCallback *func,
72 int *ltype,
73 void **data,
74 void *userdata);
75
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);
79
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);
83
84 int config_parse(
85 const char *unit,
86 const char *filename,
87 FILE *f,
88 const char *sections, /* nulstr */
89 ConfigItemLookup lookup,
90 const void *table,
91 ConfigParseFlags flags,
92 void *userdata);
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
103 int config_parse_many(
104 const char *conf_file, /* possibly NULL */
105 const char* const* conf_file_dirs,
106 const char *dropin_dirname,
107 const char *sections, /* nulstr */
108 ConfigItemLookup lookup,
109 const void *table,
110 ConfigParseFlags flags,
111 void *userdata);
112
113 CONFIG_PARSER_PROTOTYPE(config_parse_int);
114 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
115 CONFIG_PARSER_PROTOTYPE(config_parse_long);
116 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
117 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
118 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
119 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
120 CONFIG_PARSER_PROTOTYPE(config_parse_double);
121 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
122 CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
123 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
124 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
125 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
126 CONFIG_PARSER_PROTOTYPE(config_parse_string);
127 CONFIG_PARSER_PROTOTYPE(config_parse_path);
128 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
129 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
130 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
131 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset);
132 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
133 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
134 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
135 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
136 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
137 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
138 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
139 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
140 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
141 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
142 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
143 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
144
145 typedef enum Disabled {
146 DISABLED_CONFIGURATION,
147 DISABLED_LEGACY,
148 DISABLED_EXPERIMENTAL,
149 } Disabled;
150
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) \
210 CONFIG_PARSER_PROTOTYPE(function) { \
211 type *i = data, x; \
212 \
213 assert(filename); \
214 assert(lvalue); \
215 assert(rvalue); \
216 assert(data); \
217 \
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, \
226 msg ", ignoring: %s", rvalue); \
227 return 0; \
228 } \
229 \
230 *i = x; \
231 return 0; \
232 }
233
234 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
235 CONFIG_PARSER_PROTOTYPE(function) { \
236 type **enums = data, x, *ys; \
237 _cleanup_free_ type *xs = NULL; \
238 const char *word, *state; \
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); \
247 if (!xs) \
248 return -ENOMEM; \
249 \
250 *xs = invalid; \
251 \
252 FOREACH_WORD(word, l, rvalue, state) { \
253 _cleanup_free_ char *en = NULL; \
254 type *new_xs; \
255 \
256 en = strndup(word, l); \
257 if (!en) \
258 return -ENOMEM; \
259 \
260 if ((x = name##_from_string(en)) < 0) { \
261 log_syntax(unit, LOG_ERR, filename, line, 0, \
262 msg ", ignoring: %s", en); \
263 continue; \
264 } \
265 \
266 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
267 if (*ys == x) { \
268 log_syntax(unit, LOG_NOTICE, filename, \
269 line, 0, \
270 "Duplicate entry, ignoring: %s", \
271 en); \
272 x = invalid; \
273 } \
274 } \
275 \
276 if (x == invalid) \
277 continue; \
278 \
279 *(xs + i) = x; \
280 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
281 if (new_xs) \
282 xs = new_xs; \
283 else \
284 return -ENOMEM; \
285 \
286 *(xs + i) = invalid; \
287 } \
288 \
289 free_and_replace(*enums, xs); \
290 return 0; \
291 }