]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
8a6f877bba6a246e9273c18b38db2d8b351be2ae
[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 } 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
93 int config_parse_many_nulstr(
94 const char *conf_file, /* possibly NULL */
95 const char *conf_file_dirs, /* nulstr */
96 const char *sections, /* nulstr */
97 ConfigItemLookup lookup,
98 const void *table,
99 ConfigParseFlags flags,
100 void *userdata);
101
102 int config_parse_many(
103 const char *conf_file, /* possibly NULL */
104 const char* const* conf_file_dirs,
105 const char *dropin_dirname,
106 const char *sections, /* nulstr */
107 ConfigItemLookup lookup,
108 const void *table,
109 ConfigParseFlags flags,
110 void *userdata);
111
112 CONFIG_PARSER_PROTOTYPE(config_parse_int);
113 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
114 CONFIG_PARSER_PROTOTYPE(config_parse_long);
115 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
116 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
117 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
118 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
119 CONFIG_PARSER_PROTOTYPE(config_parse_double);
120 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
121 CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
122 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
123 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
124 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
125 CONFIG_PARSER_PROTOTYPE(config_parse_string);
126 CONFIG_PARSER_PROTOTYPE(config_parse_path);
127 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
128 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
129 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
130 CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_unset);
131 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
132 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
133 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
134 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
135 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
136 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
137 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
138 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
139 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
140 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
141 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
142 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
143
144 typedef enum Disabled {
145 DISABLED_CONFIGURATION,
146 DISABLED_LEGACY,
147 DISABLED_EXPERIMENTAL,
148 } Disabled;
149
150 #define DEFINE_CONFIG_PARSE(function, parser, msg) \
151 CONFIG_PARSER_PROTOTYPE(function) { \
152 int *i = data, r; \
153 \
154 assert(filename); \
155 assert(lvalue); \
156 assert(rvalue); \
157 assert(data); \
158 \
159 r = parser(rvalue); \
160 if (r < 0) { \
161 log_syntax(unit, LOG_ERR, filename, line, r, \
162 msg ", ignoring: %s", rvalue); \
163 return 0; \
164 } \
165 \
166 *i = r; \
167 return 0; \
168 }
169
170 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg) \
171 CONFIG_PARSER_PROTOTYPE(function) { \
172 type *i = data; \
173 int r; \
174 \
175 assert(filename); \
176 assert(lvalue); \
177 assert(rvalue); \
178 assert(data); \
179 \
180 r = parser(rvalue, i); \
181 if (r < 0) \
182 log_syntax(unit, LOG_ERR, filename, line, r, \
183 msg ", ignoring: %s", rvalue); \
184 \
185 return 0; \
186 }
187
188 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg) \
189 CONFIG_PARSER_PROTOTYPE(function) { \
190 type *i = data, x; \
191 \
192 assert(filename); \
193 assert(lvalue); \
194 assert(rvalue); \
195 assert(data); \
196 \
197 x = name##_from_string(rvalue); \
198 if (x < 0) { \
199 log_syntax(unit, LOG_ERR, filename, line, 0, \
200 msg ", ignoring: %s", rvalue); \
201 return 0; \
202 } \
203 \
204 *i = x; \
205 return 0; \
206 }
207
208 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
209 CONFIG_PARSER_PROTOTYPE(function) { \
210 type *i = data, x; \
211 \
212 assert(filename); \
213 assert(lvalue); \
214 assert(rvalue); \
215 assert(data); \
216 \
217 if (isempty(rvalue)) { \
218 *i = default_value; \
219 return 0; \
220 } \
221 \
222 x = name##_from_string(rvalue); \
223 if (x < 0) { \
224 log_syntax(unit, LOG_ERR, filename, line, 0, \
225 msg ", ignoring: %s", rvalue); \
226 return 0; \
227 } \
228 \
229 *i = x; \
230 return 0; \
231 }
232
233 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg) \
234 CONFIG_PARSER_PROTOTYPE(function) { \
235 type **enums = data, x, *ys; \
236 _cleanup_free_ type *xs = NULL; \
237 const char *word, *state; \
238 size_t l, i = 0; \
239 \
240 assert(filename); \
241 assert(lvalue); \
242 assert(rvalue); \
243 assert(data); \
244 \
245 xs = new0(type, 1); \
246 if (!xs) \
247 return -ENOMEM; \
248 \
249 *xs = invalid; \
250 \
251 FOREACH_WORD(word, l, rvalue, state) { \
252 _cleanup_free_ char *en = NULL; \
253 type *new_xs; \
254 \
255 en = strndup(word, l); \
256 if (!en) \
257 return -ENOMEM; \
258 \
259 if ((x = name##_from_string(en)) < 0) { \
260 log_syntax(unit, LOG_ERR, filename, line, 0, \
261 msg ", ignoring: %s", en); \
262 continue; \
263 } \
264 \
265 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
266 if (*ys == x) { \
267 log_syntax(unit, LOG_NOTICE, filename, \
268 line, 0, \
269 "Duplicate entry, ignoring: %s", \
270 en); \
271 x = invalid; \
272 } \
273 } \
274 \
275 if (x == invalid) \
276 continue; \
277 \
278 *(xs + i) = x; \
279 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
280 if (new_xs) \
281 xs = new_xs; \
282 else \
283 return -ENOMEM; \
284 \
285 *(xs + i) = invalid; \
286 } \
287 \
288 free_and_replace(*enums, xs); \
289 return 0; \
290 }