chiark / gitweb /
64e7f8aab28c02388397cce90d1713ad877e10fc
[elogind.git] / src / shared / conf-parser.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8 ***/
9
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <syslog.h>
15
16 #include "alloc-util.h"
17 #include "log.h"
18 #include "macro.h"
19
20 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
21
22 typedef enum ConfigParseFlags {
23         CONFIG_PARSE_RELAXED       = 1 << 0,
24         CONFIG_PARSE_ALLOW_INCLUDE = 1 << 1,
25         CONFIG_PARSE_WARN          = 1 << 2,
26         CONFIG_PARSE_REFUSE_BOM    = 1 << 3,
27 } ConfigParseFlags;
28
29 /* Argument list for parsers of specific configuration settings. */
30 #define CONFIG_PARSER_ARGUMENTS                 \
31         const char *unit,                       \
32         const char *filename,                   \
33         unsigned line,                          \
34         const char *section,                    \
35         unsigned section_line,                  \
36         const char *lvalue,                     \
37         int ltype,                              \
38         const char *rvalue,                     \
39         void *data,                             \
40         void *userdata
41
42 /* Prototype for a parser for a specific configuration setting */
43 typedef int (*ConfigParserCallback)(CONFIG_PARSER_ARGUMENTS);
44
45 /* A macro declaring the a function prototype, following the typedef above, simply because it's so cumbersomely long
46  * otherwise. (And current emacs gets irritatingly slow when editing files that contain lots of very long function
47  * prototypes on the same screen…) */
48 #define CONFIG_PARSER_PROTOTYPE(name) int name(CONFIG_PARSER_ARGUMENTS)
49
50 /* Wraps information for parsing a specific configuration variable, to
51  * be stored in a simple array */
52 typedef struct ConfigTableItem {
53         const char *section;            /* Section */
54         const char *lvalue;             /* Name of the variable */
55         ConfigParserCallback parse;     /* Function that is called to parse the variable's value */
56         int ltype;                      /* Distinguish different variables passed to the same callback */
57         void *data;                     /* Where to store the variable's data */
58 } ConfigTableItem;
59
60 /* Wraps information for parsing a specific configuration variable, to
61  * be stored in a gperf perfect hashtable */
62 typedef struct ConfigPerfItem {
63         const char *section_and_lvalue; /* Section + "." + name of the variable */
64         ConfigParserCallback parse;     /* Function that is called to parse the variable's value */
65         int ltype;                      /* Distinguish different variables passed to the same callback */
66         size_t offset;                  /* Offset where to store data, from the beginning of userdata */
67 } ConfigPerfItem;
68
69 /* Prototype for a low-level gperf lookup function */
70 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
71
72 /* Prototype for a generic high-level lookup function */
73 typedef int (*ConfigItemLookup)(
74                 const void *table,
75                 const char *section,
76                 const char *lvalue,
77                 ConfigParserCallback *func,
78                 int *ltype,
79                 void **data,
80                 void *userdata);
81
82 /* Linear table search implementation of ConfigItemLookup, based on
83  * ConfigTableItem arrays */
84 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
85
86 /* gperf implementation of ConfigItemLookup, based on gperf
87  * ConfigPerfItem tables */
88 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
89
90 int config_parse(
91                 const char *unit,
92                 const char *filename,
93                 FILE *f,
94                 const char *sections,  /* nulstr */
95                 ConfigItemLookup lookup,
96                 const void *table,
97                 ConfigParseFlags flags,
98                 void *userdata);
99
100 int config_parse_many_nulstr(
101                 const char *conf_file,      /* possibly NULL */
102                 const char *conf_file_dirs, /* nulstr */
103                 const char *sections,       /* nulstr */
104                 ConfigItemLookup lookup,
105                 const void *table,
106                 ConfigParseFlags flags,
107                 void *userdata);
108
109 #if 0 /// UNNEEDED by elogind
110 int config_parse_many(
111                 const char *conf_file,      /* possibly NULL */
112                 const char* const* conf_file_dirs,
113                 const char *dropin_dirname,
114                 const char *sections,       /* nulstr */
115                 ConfigItemLookup lookup,
116                 const void *table,
117                 ConfigParseFlags flags,
118                 void *userdata);
119 #endif // 0
120
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 #if 0 /// UNNEEDED by elogind
134 #endif // 0
135 CONFIG_PARSER_PROTOTYPE(config_parse_int);
136 CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
137 CONFIG_PARSER_PROTOTYPE(config_parse_long);
138 CONFIG_PARSER_PROTOTYPE(config_parse_uint8);
139 CONFIG_PARSER_PROTOTYPE(config_parse_uint16);
140 CONFIG_PARSER_PROTOTYPE(config_parse_uint32);
141 CONFIG_PARSER_PROTOTYPE(config_parse_uint64);
142 CONFIG_PARSER_PROTOTYPE(config_parse_double);
143 CONFIG_PARSER_PROTOTYPE(config_parse_iec_size);
144 CONFIG_PARSER_PROTOTYPE(config_parse_si_size);
145 CONFIG_PARSER_PROTOTYPE(config_parse_iec_uint64);
146 CONFIG_PARSER_PROTOTYPE(config_parse_bool);
147 CONFIG_PARSER_PROTOTYPE(config_parse_tristate);
148 CONFIG_PARSER_PROTOTYPE(config_parse_string);
149 CONFIG_PARSER_PROTOTYPE(config_parse_path);
150 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
151 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
152 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
153 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
154 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
155 CONFIG_PARSER_PROTOTYPE(config_parse_log_facility);
156 CONFIG_PARSER_PROTOTYPE(config_parse_log_level);
157 CONFIG_PARSER_PROTOTYPE(config_parse_signal);
158 CONFIG_PARSER_PROTOTYPE(config_parse_personality);
159 CONFIG_PARSER_PROTOTYPE(config_parse_permille);
160 CONFIG_PARSER_PROTOTYPE(config_parse_ifname);
161 CONFIG_PARSER_PROTOTYPE(config_parse_ip_port);
162 CONFIG_PARSER_PROTOTYPE(config_parse_join_controllers);
163 CONFIG_PARSER_PROTOTYPE(config_parse_mtu);
164 CONFIG_PARSER_PROTOTYPE(config_parse_rlimit);
165
166 typedef enum Disabled {
167         DISABLED_CONFIGURATION,
168         DISABLED_LEGACY,
169         DISABLED_EXPERIMENTAL,
170 } Disabled;
171
172 #define DEFINE_CONFIG_PARSE(function, parser, msg)                      \
173         CONFIG_PARSER_PROTOTYPE(function) {                             \
174                 int *i = data, r;                                       \
175                                                                         \
176                 assert(filename);                                       \
177                 assert(lvalue);                                         \
178                 assert(rvalue);                                         \
179                 assert(data);                                           \
180                                                                         \
181                 r = parser(rvalue);                                     \
182                 if (r < 0) {                                            \
183                         log_syntax(unit, LOG_ERR, filename, line, r,    \
184                                    msg ", ignoring: %s", rvalue);       \
185                         return 0;                                       \
186                 }                                                       \
187                                                                         \
188                 *i = r;                                                 \
189                 return 0;                                               \
190         }
191
192 #define DEFINE_CONFIG_PARSE_PTR(function, parser, type, msg)            \
193         CONFIG_PARSER_PROTOTYPE(function) {                             \
194                 type *i = data;                                         \
195                 int r;                                                  \
196                                                                         \
197                 assert(filename);                                       \
198                 assert(lvalue);                                         \
199                 assert(rvalue);                                         \
200                 assert(data);                                           \
201                                                                         \
202                 r = parser(rvalue, i);                                  \
203                 if (r < 0)                                              \
204                         log_syntax(unit, LOG_ERR, filename, line, r,    \
205                                    msg ", ignoring: %s", rvalue);       \
206                                                                         \
207                 return 0;                                               \
208         }
209
210 #define DEFINE_CONFIG_PARSE_ENUM(function, name, type, msg)             \
211         CONFIG_PARSER_PROTOTYPE(function) {                             \
212                 type *i = data, x;                                      \
213                                                                         \
214                 assert(filename);                                       \
215                 assert(lvalue);                                         \
216                 assert(rvalue);                                         \
217                 assert(data);                                           \
218                                                                         \
219                 x = name##_from_string(rvalue);                         \
220                 if (x < 0) {                                            \
221                         log_syntax(unit, LOG_ERR, filename, line, 0,    \
222                                    msg ", ignoring: %s", rvalue);       \
223                         return 0;                                       \
224                 }                                                       \
225                                                                         \
226                 *i = x;                                                 \
227                 return 0;                                               \
228         }
229
230 #define DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(function, name, type, default_value, msg) \
231         CONFIG_PARSER_PROTOTYPE(function) {                             \
232                 type *i = data, x;                                      \
233                                                                         \
234                 assert(filename);                                       \
235                 assert(lvalue);                                         \
236                 assert(rvalue);                                         \
237                 assert(data);                                           \
238                                                                         \
239                 if (isempty(rvalue)) {                                  \
240                         *i = default_value;                             \
241                         return 0;                                       \
242                 }                                                       \
243                                                                         \
244                 x = name##_from_string(rvalue);                         \
245                 if (x < 0) {                                            \
246                         log_syntax(unit, LOG_ERR, filename, line, 0,    \
247                                    msg ", ignoring: %s", rvalue);       \
248                         return 0;                                       \
249                 }                                                       \
250                                                                         \
251                 *i = x;                                                 \
252                 return 0;                                               \
253         }
254
255 #define DEFINE_CONFIG_PARSE_ENUMV(function, name, type, invalid, msg)          \
256         CONFIG_PARSER_PROTOTYPE(function) {                                    \
257                 type **enums = data, x, *ys;                                   \
258                 _cleanup_free_ type *xs = NULL;                                \
259                 const char *word, *state;                                      \
260                 size_t l, i = 0;                                               \
261                                                                                \
262                 assert(filename);                                              \
263                 assert(lvalue);                                                \
264                 assert(rvalue);                                                \
265                 assert(data);                                                  \
266                                                                                \
267                 xs = new0(type, 1);                                            \
268                 if (!xs)                                                       \
269                         return -ENOMEM;                                        \
270                                                                                \
271                 *xs = invalid;                                                 \
272                                                                                \
273                 FOREACH_WORD(word, l, rvalue, state) {                         \
274                         _cleanup_free_ char *en = NULL;                        \
275                         type *new_xs;                                          \
276                                                                                \
277                         en = strndup(word, l);                                 \
278                         if (!en)                                               \
279                                 return -ENOMEM;                                \
280                                                                                \
281                         if ((x = name##_from_string(en)) < 0) {                \
282                                 log_syntax(unit, LOG_ERR, filename, line, 0,   \
283                                            msg ", ignoring: %s", en);          \
284                                 continue;                                      \
285                         }                                                      \
286                                                                                \
287                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
288                                 if (*ys == x) {                                \
289                                         log_syntax(unit, LOG_NOTICE, filename, \
290                                                    line, 0,                    \
291                                                    "Duplicate entry, ignoring: %s", \
292                                                    en);                        \
293                                         x = invalid;                           \
294                                 }                                              \
295                         }                                                      \
296                                                                                \
297                         if (x == invalid)                                      \
298                                 continue;                                      \
299                                                                                \
300                         *(xs + i) = x;                                         \
301                         new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
302                         if (new_xs)                                            \
303                                 xs = new_xs;                                   \
304                         else                                                   \
305                                 return -ENOMEM;                                \
306                                                                                \
307                         *(xs + i) = invalid;                                   \
308                 }                                                              \
309                                                                                \
310                 free_and_replace(*enums, xs);                                  \
311                 return 0;                                                      \
312         }