]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
Merge pull request #2095 from evverx/fix-distcheck-for-disable-timesync
[thirdparty/systemd.git] / src / shared / conf-parser.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <errno.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <syslog.h>
29
30 #include "alloc-util.h"
31 #include "log.h"
32 #include "macro.h"
33
34 /* An abstract parser for simple, line based, shallow configuration
35 * files consisting of variable assignments only. */
36
37 /* Prototype for a parser for a specific configuration setting */
38 typedef int (*ConfigParserCallback)(const char *unit,
39 const char *filename,
40 unsigned line,
41 const char *section,
42 unsigned section_line,
43 const char *lvalue,
44 int ltype,
45 const char *rvalue,
46 void *data,
47 void *userdata);
48
49 /* Wraps information for parsing a specific configuration variable, to
50 * be stored in a simple array */
51 typedef struct ConfigTableItem {
52 const char *section; /* Section */
53 const char *lvalue; /* Name of the variable */
54 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
55 int ltype; /* Distinguish different variables passed to the same callback */
56 void *data; /* Where to store the variable's data */
57 } ConfigTableItem;
58
59 /* Wraps information for parsing a specific configuration variable, to
60 * be stored in a gperf perfect hashtable */
61 typedef struct ConfigPerfItem {
62 const char *section_and_lvalue; /* Section + "." + name of the variable */
63 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
64 int ltype; /* Distinguish different variables passed to the same callback */
65 size_t offset; /* Offset where to store data, from the beginning of userdata */
66 } ConfigPerfItem;
67
68 /* Prototype for a low-level gperf lookup function */
69 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
70
71 /* Prototype for a generic high-level lookup function */
72 typedef int (*ConfigItemLookup)(
73 const void *table,
74 const char *section,
75 const char *lvalue,
76 ConfigParserCallback *func,
77 int *ltype,
78 void **data,
79 void *userdata);
80
81 /* Linear table search implementation of ConfigItemLookup, based on
82 * ConfigTableItem arrays */
83 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
84
85 /* gperf implementation of ConfigItemLookup, based on gperf
86 * ConfigPerfItem tables */
87 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
88
89 int config_parse(const char *unit,
90 const char *filename,
91 FILE *f,
92 const char *sections, /* nulstr */
93 ConfigItemLookup lookup,
94 const void *table,
95 bool relaxed,
96 bool allow_include,
97 bool warn,
98 void *userdata);
99
100 int config_parse_many(const char *conf_file, /* possibly NULL */
101 const char *conf_file_dirs, /* nulstr */
102 const char *sections, /* nulstr */
103 ConfigItemLookup lookup,
104 const void *table,
105 bool relaxed,
106 void *userdata);
107
108 /* Generic parsers */
109 int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
110 int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
111 int config_parse_long(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
112 int config_parse_uint32(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
113 int config_parse_uint64(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
114 int config_parse_double(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
115 int config_parse_iec_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
116 int config_parse_si_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
117 int config_parse_iec_uint64(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
118 int config_parse_bool(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
119 int config_parse_tristate(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
120 int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
121 int config_parse_path(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
122 int config_parse_strv(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
123 int config_parse_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
124 int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
125 int config_parse_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
126 int config_parse_log_facility(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
127 int config_parse_log_level(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
128 int config_parse_signal(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
129 int config_parse_personality(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
130
131 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
132 int function(const char *unit, \
133 const char *filename, \
134 unsigned line, \
135 const char *section, \
136 unsigned section_line, \
137 const char *lvalue, \
138 int ltype, \
139 const char *rvalue, \
140 void *data, \
141 void *userdata) { \
142 \
143 type *i = data, x; \
144 \
145 assert(filename); \
146 assert(lvalue); \
147 assert(rvalue); \
148 assert(data); \
149 \
150 if ((x = name##_from_string(rvalue)) < 0) { \
151 log_syntax(unit, LOG_ERR, filename, line, -x, \
152 msg ", ignoring: %s", rvalue); \
153 return 0; \
154 } \
155 \
156 *i = x; \
157 return 0; \
158 }
159
160 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
161 int function(const char *unit, \
162 const char *filename, \
163 unsigned line, \
164 const char *section, \
165 unsigned section_line, \
166 const char *lvalue, \
167 int ltype, \
168 const char *rvalue, \
169 void *data, \
170 void *userdata) { \
171 \
172 type **enums = data, x, *ys; \
173 _cleanup_free_ type *xs = NULL; \
174 const char *word, *state; \
175 size_t l, i = 0; \
176 \
177 assert(filename); \
178 assert(lvalue); \
179 assert(rvalue); \
180 assert(data); \
181 \
182 xs = new0(type, 1); \
183 if(!xs) \
184 return -ENOMEM; \
185 \
186 *xs = invalid; \
187 \
188 FOREACH_WORD(word, l, rvalue, state) { \
189 _cleanup_free_ char *en = NULL; \
190 type *new_xs; \
191 \
192 en = strndup(word, l); \
193 if (!en) \
194 return -ENOMEM; \
195 \
196 if ((x = name##_from_string(en)) < 0) { \
197 log_syntax(unit, LOG_ERR, filename, line, \
198 -x, msg ", ignoring: %s", en); \
199 continue; \
200 } \
201 \
202 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
203 if (*ys == x) { \
204 log_syntax(unit, LOG_ERR, filename, \
205 line, -x, \
206 "Duplicate entry, ignoring: %s", \
207 en); \
208 x = invalid; \
209 } \
210 } \
211 \
212 if (x == invalid) \
213 continue; \
214 \
215 *(xs + i) = x; \
216 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
217 if (new_xs) \
218 xs = new_xs; \
219 else \
220 return -ENOMEM; \
221 \
222 *(xs + i) = invalid; \
223 } \
224 \
225 free(*enums); \
226 *enums = xs; \
227 xs = NULL; \
228 \
229 return 0; \
230 }