chiark / gitweb /
7b5a3c2b3c3a3affba671226b668c8e612b6c376
[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       = 1U << 0,
24         CONFIG_PARSE_ALLOW_INCLUDE = 1U << 1,
25         CONFIG_PARSE_WARN          = 1U << 2,
26         CONFIG_PARSE_REFUSE_BOM    = 1U << 3,
27 } ConfigParseFlags;
28
29 /* Prototype for a parser for a specific configuration setting */
30 typedef int (*ConfigParserCallback)(const char *unit,
31                                     const char *filename,
32                                     unsigned line,
33                                     const char *section,
34                                     unsigned section_line,
35                                     const char *lvalue,
36                                     int ltype,
37                                     const char *rvalue,
38                                     void *data,
39                                     void *userdata);
40
41 /* Wraps information for parsing a specific configuration variable, to
42  * be stored in a simple array */
43 typedef struct ConfigTableItem {
44         const char *section;            /* Section */
45         const char *lvalue;             /* Name of the variable */
46         ConfigParserCallback parse;     /* Function that is called to parse the variable's value */
47         int ltype;                      /* Distinguish different variables passed to the same callback */
48         void *data;                     /* Where to store the variable's data */
49 } ConfigTableItem;
50
51 /* Wraps information for parsing a specific configuration variable, to
52  * be stored in a gperf perfect hashtable */
53 typedef struct ConfigPerfItem {
54         const char *section_and_lvalue; /* Section + "." + 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         size_t offset;                  /* Offset where to store data, from the beginning of userdata */
58 } ConfigPerfItem;
59
60 /* Prototype for a low-level gperf lookup function */
61 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
62
63 /* Prototype for a generic high-level lookup function */
64 typedef int (*ConfigItemLookup)(
65                 const void *table,
66                 const char *section,
67                 const char *lvalue,
68                 ConfigParserCallback *func,
69                 int *ltype,
70                 void **data,
71                 void *userdata);
72
73 /* Linear table search implementation of ConfigItemLookup, based on
74  * ConfigTableItem arrays */
75 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
76
77 /* gperf implementation of ConfigItemLookup, based on gperf
78  * ConfigPerfItem tables */
79 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
80
81 int config_parse(
82                 const char *unit,
83                 const char *filename,
84                 FILE *f,
85                 const char *sections,  /* nulstr */
86                 ConfigItemLookup lookup,
87                 const void *table,
88                 ConfigParseFlags flags,
89                 void *userdata);
90
91 int config_parse_many_nulstr(
92                 const char *conf_file,      /* possibly NULL */
93                 const char *conf_file_dirs, /* nulstr */
94                 const char *sections,       /* nulstr */
95                 ConfigItemLookup lookup,
96                 const void *table,
97                 ConfigParseFlags flags,
98                 void *userdata);
99
100 #if 0 /// UNNEEDED by elogind
101 int config_parse_many(
102                 const char *conf_file,      /* possibly NULL */
103                 const char* const* conf_file_dirs,
104                 const char *dropin_dirname,
105                 const char *sections,       /* nulstr */
106                 ConfigItemLookup lookup,
107                 const void *table,
108                 ConfigParseFlags flags,
109                 void *userdata);
110 #endif // 0
111
112 /* Generic parsers */
113 #if 0 /// UNNEEDED by elogind
114 #endif // 0
115 #if 0 /// UNNEEDED by elogind
116 #endif // 0
117 #if 0 /// UNNEEDED by elogind
118 #endif // 0
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 #define GENERIC_PARSER_ARGS \
126                 const char *unit,                                       \
127                 const char *filename,                                   \
128                 unsigned line,                                          \
129                 const char *section,                                    \
130                 unsigned section_line,                                  \
131                 const char *lvalue,                                     \
132                 int ltype,                                              \
133                 const char *rvalue,                                     \
134                 void *data,                                             \
135                 void *userdata
136 int config_parse_int(GENERIC_PARSER_ARGS);
137 int config_parse_unsigned(GENERIC_PARSER_ARGS);
138 int config_parse_long(GENERIC_PARSER_ARGS);
139 int config_parse_uint8(GENERIC_PARSER_ARGS);
140 int config_parse_uint16(GENERIC_PARSER_ARGS);
141 int config_parse_uint32(GENERIC_PARSER_ARGS);
142 int config_parse_uint64(GENERIC_PARSER_ARGS);
143 int config_parse_double(GENERIC_PARSER_ARGS);
144 int config_parse_iec_size(GENERIC_PARSER_ARGS);
145 int config_parse_si_size(GENERIC_PARSER_ARGS);
146 int config_parse_iec_uint64(GENERIC_PARSER_ARGS);
147 int config_parse_bool(GENERIC_PARSER_ARGS);
148 int config_parse_tristate(GENERIC_PARSER_ARGS);
149 int config_parse_string(GENERIC_PARSER_ARGS);
150 int config_parse_path(GENERIC_PARSER_ARGS);
151 int config_parse_strv(GENERIC_PARSER_ARGS);
152 int config_parse_sec(GENERIC_PARSER_ARGS);
153 int config_parse_nsec(GENERIC_PARSER_ARGS);
154 int config_parse_mode(GENERIC_PARSER_ARGS);
155 int config_parse_warn_compat(GENERIC_PARSER_ARGS);
156 int config_parse_log_facility(GENERIC_PARSER_ARGS);
157 int config_parse_log_level(GENERIC_PARSER_ARGS);
158 int config_parse_signal(GENERIC_PARSER_ARGS);
159 int config_parse_personality(GENERIC_PARSER_ARGS);
160 int config_parse_ifname(GENERIC_PARSER_ARGS);
161 int config_parse_ip_port(GENERIC_PARSER_ARGS);
162 #if 0 /// UNNEEDED by elogind
163 int config_parse_join_controllers(GENERIC_PARSER_ARGS);
164 #endif // 0
165 int config_parse_mtu(GENERIC_PARSER_ARGS);
166 int config_parse_rlimit(GENERIC_PARSER_ARGS);
167
168 typedef enum Disabled {
169         DISABLED_CONFIGURATION,
170         DISABLED_LEGACY,
171         DISABLED_EXPERIMENTAL,
172 } Disabled;
173
174 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
175         int function(GENERIC_PARSER_ARGS) {                             \
176                 type *i = data, x;                                      \
177                                                                         \
178                 assert(filename);                                       \
179                 assert(lvalue);                                         \
180                 assert(rvalue);                                         \
181                 assert(data);                                           \
182                                                                         \
183                 if ((x = name##_from_string(rvalue)) < 0) {             \
184                         log_syntax(unit, LOG_ERR, filename, line, -x,   \
185                                    msg ", ignoring: %s", rvalue);       \
186                         return 0;                                       \
187                 }                                                       \
188                                                                         \
189                 *i = x;                                                 \
190                 return 0;                                               \
191         }
192
193 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg)              \
194         int function(GENERIC_PARSER_ARGS) {                                    \
195                 type **enums = data, x, *ys;                                   \
196                 _cleanup_free_ type *xs = NULL;                                \
197                 const char *word, *state;                                      \
198                 size_t l, i = 0;                                               \
199                                                                                \
200                 assert(filename);                                              \
201                 assert(lvalue);                                                \
202                 assert(rvalue);                                                \
203                 assert(data);                                                  \
204                                                                                \
205                 xs = new0(type, 1);                                            \
206                 if (!xs)                                                       \
207                         return -ENOMEM;                                        \
208                                                                                \
209                 *xs = invalid;                                                 \
210                                                                                \
211                 FOREACH_WORD(word, l, rvalue, state) {                         \
212                         _cleanup_free_ char *en = NULL;                        \
213                         type *new_xs;                                          \
214                                                                                \
215                         en = strndup(word, l);                                 \
216                         if (!en)                                               \
217                                 return -ENOMEM;                                \
218                                                                                \
219                         if ((x = name##_from_string(en)) < 0) {                \
220                                 log_syntax(unit, LOG_ERR, filename, line,      \
221                                        -x, msg ", ignoring: %s", en);          \
222                                 continue;                                      \
223                         }                                                      \
224                                                                                \
225                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
226                                 if (*ys == x) {                                \
227                                         log_syntax(unit, LOG_ERR, filename,    \
228                                               line, -x,                        \
229                                               "Duplicate entry, ignoring: %s", \
230                                               en);                             \
231                                         x = invalid;                           \
232                                 }                                              \
233                         }                                                      \
234                                                                                \
235                         if (x == invalid)                                      \
236                                 continue;                                      \
237                                                                                \
238                         *(xs + i) = x;                                         \
239                         new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
240                         if (new_xs)                                            \
241                                 xs = new_xs;                                   \
242                         else                                                   \
243                                 return -ENOMEM;                                \
244                                                                                \
245                         *(xs + i) = invalid;                                   \
246                 }                                                              \
247                                                                                \
248                 free(*enums);                                                  \
249                 *enums = xs;                                                   \
250                 xs = NULL;                                                     \
251                                                                                \
252                 return 0;                                                      \
253         }