chiark / gitweb /
3c78c404458810aec537855cba6f9710428d7813
[elogind.git] / src / shared / conf-parser.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <syslog.h>
13
14 #include "alloc-util.h"
15 #include "log.h"
16 #include "macro.h"
17
18 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
19
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,
25 } ConfigParseFlags;
26
27 /* Argument list for parsers of specific configuration settings. */
28 #define CONFIG_PARSER_ARGUMENTS                 \
29         const char *unit,                       \
30         const char *filename,                   \
31         unsigned line,                          \
32         const char *section,                    \
33         unsigned section_line,                  \
34         const char *lvalue,                     \
35         int ltype,                              \
36         const char *rvalue,                     \
37         void *data,                             \
38         void *userdata
39
40 /* Prototype for a parser for a specific configuration setting */
41 typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
42
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)
47
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 */
56 } ConfigTableItem;
57
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 */
65 } ConfigPerfItem;
66
67 /* Prototype for a low-level gperf lookup function */
68 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
69
70 /* Prototype for a generic high-level lookup function */
71 typedef int (*ConfigItemLookup)(
72                 const void *table,
73                 const char *section,
74                 const char *lvalue,
75                 ConfigParserCallback *func,
76                 int *ltype,
77                 void **data,
78                 void *userdata);
79
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);
83
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);
87
88 int config_parse(
89                 const char *unit,
90                 const char *filename,
91                 FILE *f,
92                 const char *sections,  /* nulstr */
93                 ConfigItemLookup lookup,
94                 const void *table,
95                 ConfigParseFlags flags,
96                 void *userdata);
97
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,
103                 const void *table,
104                 ConfigParseFlags flags,
105                 void *userdata);
106
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,
114                 const void *table,
115                 ConfigParseFlags flags,
116                 void *userdata);
117 #endif // 0
118
119 #if 0 /// UNNEEDED by elogind
120 #endif // 0
121 #if 0 /// UNNEEDED by elogind
122 #endif // 0
123 #if 0 /// UNNEEDED by elogind
124 #endif // 0
125 #if 0 /// UNNEEDED by elogind
126 #endif // 0
127 #if 0 /// UNNEEDED by elogind
128 #endif // 0
129 #if 0 /// UNNEEDED by elogind
130 #endif // 0
131 #if 0 /// UNNEEDED by elogind
132 #endif // 0
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);
163
164 typedef enum Disabled {
165         DISABLED_CONFIGURATION,
166         DISABLED_LEGACY,
167         DISABLED_EXPERIMENTAL,
168 } Disabled;
169
170 #define DEFINE_CONFIG_PARSE(function, parser, msg)                      \
171         CONFIG_PARSER_PROTOTYPE(function) {                             \
172                 int *i = data, r;                                       \
173                                                                         \
174                 assert(filename);                                       \
175                 assert(lvalue);                                         \
176                 assert(rvalue);                                         \
177                 assert(data);                                           \
178                                                                         \
179                 r = parser(rvalue);                                     \
180                 if (r < 0) {                                            \
181                         log_syntax(unit, LOG_ERR, filename, line, r,    \
182                                    msg ", ignoring: %s", rvalue);       \
183                         return 0;                                       \
184                 }                                                       \
185                                                                         \
186                 *i = r;                                                 \
187                 return 0;                                               \
188         }
189
190 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg)            \
191         CONFIG_PARSER_PROTOTYPE(function) {                             \
192                 type *i = data;                                         \
193                 int r;                                                  \
194                                                                         \
195                 assert(filename);                                       \
196                 assert(lvalue);                                         \
197                 assert(rvalue);                                         \
198                 assert(data);                                           \
199                                                                         \
200                 r = parser(rvalue, i);                                  \
201                 if (r < 0)                                              \
202                         log_syntax(unit, LOG_ERR, filename, line, r,    \
203                                    msg ", ignoring: %s", rvalue);       \
204                                                                         \
205                 return 0;                                               \
206         }
207
208 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, 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                 x = name##_from_string(rvalue);                         \
218                 if (x < 0) {                                            \
219                         log_syntax(unit, LOG_ERR, filename, line, 0,    \
220                                    msg ", ignoring: %s", rvalue);       \
221                         return 0;                                       \
222                 }                                                       \
223                                                                         \
224                 *i = x;                                                 \
225                 return 0;                                               \
226         }
227
228 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
229         CONFIG_PARSER_PROTOTYPE(function) {                             \
230                 type *i = data, x;                                      \
231                                                                         \
232                 assert(filename);                                       \
233                 assert(lvalue);                                         \
234                 assert(rvalue);                                         \
235                 assert(data);                                           \
236                                                                         \
237                 if (isempty(rvalue)) {                                  \
238                         *i = default_value;                             \
239                         return 0;                                       \
240                 }                                                       \
241                                                                         \
242                 x = name##_from_string(rvalue);                         \
243                 if (x < 0) {                                            \
244                         log_syntax(unit, LOG_ERR, filename, line, 0,    \
245                                    msg ", ignoring: %s", rvalue);       \
246                         return 0;                                       \
247                 }                                                       \
248                                                                         \
249                 *i = x;                                                 \
250                 return 0;                                               \
251         }
252
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;                                      \
258                 size_t l, i = 0;                                               \
259                                                                                \
260                 assert(filename);                                              \
261                 assert(lvalue);                                                \
262                 assert(rvalue);                                                \
263                 assert(data);                                                  \
264                                                                                \
265                 xs = new0(type, 1);                                            \
266                 if (!xs)                                                       \
267                         return -ENOMEM;                                        \
268                                                                                \
269                 *xs = invalid;                                                 \
270                                                                                \
271                 FOREACH_WORD(word, l, rvalue, state) {                         \
272                         _cleanup_free_ char *en = NULL;                        \
273                         type *new_xs;                                          \
274                                                                                \
275                         en = strndup(word, l);                                 \
276                         if (!en)                                               \
277                                 return -ENOMEM;                                \
278                                                                                \
279                         if ((x = name##_from_string(en)) < 0) {                \
280                                 log_syntax(unit, LOG_ERR, filename, line, 0,   \
281                                            msg ", ignoring: %s", en);          \
282                                 continue;                                      \
283                         }                                                      \
284                                                                                \
285                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
286                                 if (*ys == x) {                                \
287                                         log_syntax(unit, LOG_NOTICE, filename, \
288                                                    line, 0,                    \
289                                                    "Duplicate entry, ignoring: %s", \
290                                                    en);                        \
291                                         x = invalid;                           \
292                                 }                                              \
293                         }                                                      \
294                                                                                \
295                         if (x == invalid)                                      \
296                                 continue;                                      \
297                                                                                \
298                         *(xs + i) = x;                                         \
299                         new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
300                         if (new_xs)                                            \
301                                 xs = new_xs;                                   \
302                         else                                                   \
303                                 return -ENOMEM;                                \
304                                                                                \
305                         *(xs + i) = invalid;                                   \
306                 }                                                              \
307                                                                                \
308                 free_and_replace(*enums, xs);                                  \
309                 return 0;                                                      \
310         }