]>
Commit | Line | Data |
---|---|---|
c2f1db8f | 1 | #pragma once |
ed5bcfbe | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
a8fbdf54 | 22 | #include <errno.h> |
10e87ee7 | 23 | #include <stdbool.h> |
a8fbdf54 | 24 | #include <stddef.h> |
71d35b6b | 25 | #include <stdio.h> |
a8fbdf54 | 26 | #include <syslog.h> |
ed5bcfbe | 27 | |
a8fbdf54 TA |
28 | #include "alloc-util.h" |
29 | #include "log.h" | |
e8e581bf ZJS |
30 | #include "macro.h" |
31 | ||
ed5bcfbe LP |
32 | /* An abstract parser for simple, line based, shallow configuration |
33 | * files consisting of variable assignments only. */ | |
34 | ||
f975e971 | 35 | /* Prototype for a parser for a specific configuration setting */ |
e8e581bf ZJS |
36 | typedef int (*ConfigParserCallback)(const char *unit, |
37 | const char *filename, | |
38 | unsigned line, | |
39 | const char *section, | |
71a61510 | 40 | unsigned section_line, |
e8e581bf ZJS |
41 | const char *lvalue, |
42 | int ltype, | |
43 | const char *rvalue, | |
44 | void *data, | |
45 | void *userdata); | |
f975e971 LP |
46 | |
47 | /* Wraps information for parsing a specific configuration variable, to | |
48 | * be stored in a simple array */ | |
49 | typedef struct ConfigTableItem { | |
50 | const char *section; /* Section */ | |
51 | const char *lvalue; /* Name of the variable */ | |
52 | ConfigParserCallback parse; /* Function that is called to parse the variable's value */ | |
53 | int ltype; /* Distinguish different variables passed to the same callback */ | |
54 | void *data; /* Where to store the variable's data */ | |
55 | } ConfigTableItem; | |
56 | ||
57 | /* Wraps information for parsing a specific configuration variable, to | |
e5a7f173 | 58 | * be stored in a gperf perfect hashtable */ |
f975e971 LP |
59 | typedef struct ConfigPerfItem { |
60 | const char *section_and_lvalue; /* Section + "." + name of the variable */ | |
61 | ConfigParserCallback parse; /* Function that is called to parse the variable's value */ | |
62 | int ltype; /* Distinguish different variables passed to the same callback */ | |
63 | size_t offset; /* Offset where to store data, from the beginning of userdata */ | |
64 | } ConfigPerfItem; | |
65 | ||
66 | /* Prototype for a low-level gperf lookup function */ | |
67 | typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length); | |
68 | ||
69 | /* Prototype for a generic high-level lookup function */ | |
70 | typedef int (*ConfigItemLookup)( | |
e9f3d2d5 | 71 | const void *table, |
f975e971 LP |
72 | const char *section, |
73 | const char *lvalue, | |
74 | ConfigParserCallback *func, | |
75 | int *ltype, | |
76 | void **data, | |
77 | void *userdata); | |
78 | ||
79 | /* Linear table search implementation of ConfigItemLookup, based on | |
80 | * ConfigTableItem arrays */ | |
e9f3d2d5 | 81 | int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata); |
f975e971 LP |
82 | |
83 | /* gperf implementation of ConfigItemLookup, based on gperf | |
84 | * ConfigPerfItem tables */ | |
e9f3d2d5 | 85 | int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata); |
f975e971 | 86 | |
43688c49 ZJS |
87 | int config_parse( |
88 | const char *unit, | |
89 | const char *filename, | |
90 | FILE *f, | |
91 | const char *sections, /* nulstr */ | |
92 | ConfigItemLookup lookup, | |
93 | const void *table, | |
94 | bool relaxed, | |
95 | bool allow_include, | |
96 | bool warn, | |
97 | void *userdata); | |
98 | ||
99 | int config_parse_many_nulstr( | |
100 | 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); | |
e8461023 | 107 | |
23bb31aa ZJS |
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 | bool relaxed, | |
116 | void *userdata); | |
117 | ||
ed5bcfbe | 118 | /* Generic parsers */ |
71a61510 TG |
119 | 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); |
120 | 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); | |
121 | 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); | |
134e24e1 | 122 | int config_parse_uint8(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); |
c7440e74 | 123 | int config_parse_uint16(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); |
9b3a67c5 | 124 | 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); |
71a61510 TG |
125 | 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); |
126 | 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); | |
5556b5fe LP |
127 | 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); |
128 | 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); | |
59f448cf | 129 | 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); |
71a61510 | 130 | 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); |
f757855e | 131 | 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); |
71a61510 TG |
132 | 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); |
133 | 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); | |
134 | 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); | |
71a61510 TG |
135 | 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); |
136 | 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); | |
137 | 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); | |
ca37242e LP |
138 | 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); |
139 | 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); | |
f757855e LP |
140 | 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); |
141 | 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); | |
d31645ad | 142 | int config_parse_ifname(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); |
177d0b20 | 143 | int config_parse_ip_port(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); |
e8e581bf | 144 | |
487393e9 | 145 | #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \ |
e8e581bf ZJS |
146 | int function(const char *unit, \ |
147 | const char *filename, \ | |
148 | unsigned line, \ | |
149 | const char *section, \ | |
71a61510 | 150 | unsigned section_line, \ |
e8e581bf ZJS |
151 | const char *lvalue, \ |
152 | int ltype, \ | |
153 | const char *rvalue, \ | |
154 | void *data, \ | |
155 | void *userdata) { \ | |
487393e9 LP |
156 | \ |
157 | type *i = data, x; \ | |
158 | \ | |
159 | assert(filename); \ | |
160 | assert(lvalue); \ | |
161 | assert(rvalue); \ | |
162 | assert(data); \ | |
163 | \ | |
164 | if ((x = name##_from_string(rvalue)) < 0) { \ | |
e8e581bf ZJS |
165 | log_syntax(unit, LOG_ERR, filename, line, -x, \ |
166 | msg ", ignoring: %s", rvalue); \ | |
c0b34696 | 167 | return 0; \ |
487393e9 LP |
168 | } \ |
169 | \ | |
170 | *i = x; \ | |
487393e9 LP |
171 | return 0; \ |
172 | } | |
916484f5 TG |
173 | |
174 | #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \ | |
175 | int function(const char *unit, \ | |
176 | const char *filename, \ | |
177 | unsigned line, \ | |
178 | const char *section, \ | |
71a61510 | 179 | unsigned section_line, \ |
916484f5 TG |
180 | const char *lvalue, \ |
181 | int ltype, \ | |
182 | const char *rvalue, \ | |
183 | void *data, \ | |
184 | void *userdata) { \ | |
185 | \ | |
77c10205 TG |
186 | type **enums = data, x, *ys; \ |
187 | _cleanup_free_ type *xs = NULL; \ | |
a2a5291b | 188 | const char *word, *state; \ |
916484f5 TG |
189 | size_t l, i = 0; \ |
190 | \ | |
191 | assert(filename); \ | |
192 | assert(lvalue); \ | |
193 | assert(rvalue); \ | |
194 | assert(data); \ | |
195 | \ | |
196 | xs = new0(type, 1); \ | |
9ed794a3 | 197 | if (!xs) \ |
83e341a6 TG |
198 | return -ENOMEM; \ |
199 | \ | |
916484f5 TG |
200 | *xs = invalid; \ |
201 | \ | |
a2a5291b | 202 | FOREACH_WORD(word, l, rvalue, state) { \ |
916484f5 | 203 | _cleanup_free_ char *en = NULL; \ |
77c10205 | 204 | type *new_xs; \ |
916484f5 | 205 | \ |
a2a5291b | 206 | en = strndup(word, l); \ |
916484f5 TG |
207 | if (!en) \ |
208 | return -ENOMEM; \ | |
209 | \ | |
210 | if ((x = name##_from_string(en)) < 0) { \ | |
211 | log_syntax(unit, LOG_ERR, filename, line, \ | |
212 | -x, msg ", ignoring: %s", en); \ | |
213 | continue; \ | |
214 | } \ | |
215 | \ | |
216 | for (ys = xs; x != invalid && *ys != invalid; ys++) { \ | |
217 | if (*ys == x) { \ | |
218 | log_syntax(unit, LOG_ERR, filename, \ | |
219 | line, -x, \ | |
220 | "Duplicate entry, ignoring: %s", \ | |
221 | en); \ | |
222 | x = invalid; \ | |
223 | } \ | |
224 | } \ | |
225 | \ | |
226 | if (x == invalid) \ | |
227 | continue; \ | |
228 | \ | |
229 | *(xs + i) = x; \ | |
77c10205 TG |
230 | new_xs = realloc(xs, (++i + 1) * sizeof(type)); \ |
231 | if (new_xs) \ | |
232 | xs = new_xs; \ | |
233 | else \ | |
916484f5 | 234 | return -ENOMEM; \ |
83e341a6 | 235 | \ |
916484f5 TG |
236 | *(xs + i) = invalid; \ |
237 | } \ | |
238 | \ | |
239 | free(*enums); \ | |
240 | *enums = xs; \ | |
77c10205 TG |
241 | xs = NULL; \ |
242 | \ | |
916484f5 TG |
243 | return 0; \ |
244 | } |