]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
ed5bcfbe | 3 | #include <errno.h> |
a8fbdf54 TA |
4 | #include <limits.h> |
5 | #include <stdint.h> | |
07630cea | 6 | #include <stdio.h> |
ed5bcfbe | 7 | #include <stdlib.h> |
a8fbdf54 | 8 | #include <sys/types.h> |
07630cea | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
e8461023 | 11 | #include "conf-files.h" |
6bedfcbb | 12 | #include "conf-parser.h" |
28db6fbf | 13 | #include "constants.h" |
fa787a13 | 14 | #include "dns-domain.h" |
3f87eaa5 | 15 | #include "escape.h" |
0ec2a7a1 | 16 | #include "ether-addr-util.h" |
a8fbdf54 | 17 | #include "extract-word.h" |
6bedfcbb | 18 | #include "fd-util.h" |
e6dde451 | 19 | #include "fileio.h" |
f4f15635 | 20 | #include "fs-util.h" |
9ac2f3c4 | 21 | #include "hash-funcs.h" |
fa787a13 | 22 | #include "hostname-util.h" |
aea3f594 | 23 | #include "id128-util.h" |
cf074772 | 24 | #include "in-addr-util.h" |
16354eff | 25 | #include "log.h" |
07630cea | 26 | #include "macro.h" |
f5947a5e | 27 | #include "missing_network.h" |
d8b4d14d | 28 | #include "nulstr-util.h" |
c3eaba2d | 29 | #include "parse-helpers.h" |
6bedfcbb | 30 | #include "parse-util.h" |
9eb977db | 31 | #include "path-util.h" |
ed5033fd | 32 | #include "percent-util.h" |
7b3e062c | 33 | #include "process-util.h" |
ef118d00 | 34 | #include "rlimit-util.h" |
12963533 | 35 | #include "sd-id128.h" |
0ec2a7a1 | 36 | #include "set.h" |
f757855e | 37 | #include "signal-util.h" |
d31645ad | 38 | #include "socket-util.h" |
bdb2d3c6 | 39 | #include "stat-util.h" |
07630cea LP |
40 | #include "string-util.h" |
41 | #include "strv.h" | |
7b3e062c | 42 | #include "syslog-util.h" |
a8fbdf54 | 43 | #include "time-util.h" |
07630cea | 44 | #include "utf8.h" |
e8e581bf | 45 | |
9ac2f3c4 MY |
46 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(config_file_hash_ops_fclose, |
47 | char, path_hash_func, path_compare, | |
48 | FILE, safe_fclose); | |
49 | ||
f975e971 | 50 | int config_item_table_lookup( |
e9f3d2d5 | 51 | const void *table, |
ed5bcfbe | 52 | const char *section, |
ed5bcfbe | 53 | const char *lvalue, |
0b954099 LP |
54 | ConfigParserCallback *ret_func, |
55 | int *ret_ltype, | |
56 | void **ret_data, | |
ed5bcfbe LP |
57 | void *userdata) { |
58 | ||
f975e971 | 59 | assert(table); |
ed5bcfbe | 60 | assert(lvalue); |
0b954099 LP |
61 | assert(ret_func); |
62 | assert(ret_ltype); | |
63 | assert(ret_data); | |
ed5bcfbe | 64 | |
62c9beaa | 65 | for (const ConfigTableItem *t = table; t->lvalue; t++) { |
ed5bcfbe | 66 | |
f975e971 | 67 | if (!streq(lvalue, t->lvalue)) |
ed5bcfbe LP |
68 | continue; |
69 | ||
f975e971 | 70 | if (!streq_ptr(section, t->section)) |
ed5bcfbe LP |
71 | continue; |
72 | ||
0b954099 LP |
73 | *ret_func = t->parse; |
74 | *ret_ltype = t->ltype; | |
75 | *ret_data = t->data; | |
f975e971 LP |
76 | return 1; |
77 | } | |
ed5bcfbe | 78 | |
0b954099 LP |
79 | *ret_func = NULL; |
80 | *ret_ltype = 0; | |
81 | *ret_data = NULL; | |
f975e971 LP |
82 | return 0; |
83 | } | |
10e87ee7 | 84 | |
f975e971 | 85 | int config_item_perf_lookup( |
e9f3d2d5 | 86 | const void *table, |
f975e971 LP |
87 | const char *section, |
88 | const char *lvalue, | |
0b954099 LP |
89 | ConfigParserCallback *ret_func, |
90 | int *ret_ltype, | |
91 | void **ret_data, | |
f975e971 LP |
92 | void *userdata) { |
93 | ||
94 | ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table; | |
95 | const ConfigPerfItem *p; | |
96 | ||
97 | assert(table); | |
98 | assert(lvalue); | |
0b954099 LP |
99 | assert(ret_func); |
100 | assert(ret_ltype); | |
101 | assert(ret_data); | |
f975e971 | 102 | |
4472fa6d LP |
103 | if (section) { |
104 | const char *key; | |
f975e971 | 105 | |
4472fa6d | 106 | key = strjoina(section, ".", lvalue); |
f975e971 | 107 | p = lookup(key, strlen(key)); |
4472fa6d LP |
108 | } else |
109 | p = lookup(lvalue, strlen(lvalue)); | |
0b954099 LP |
110 | if (!p) { |
111 | *ret_func = NULL; | |
112 | *ret_ltype = 0; | |
113 | *ret_data = NULL; | |
f975e971 | 114 | return 0; |
0b954099 | 115 | } |
f975e971 | 116 | |
0b954099 LP |
117 | *ret_func = p->parse; |
118 | *ret_ltype = p->ltype; | |
119 | *ret_data = (uint8_t*) userdata + p->offset; | |
f975e971 LP |
120 | return 1; |
121 | } | |
122 | ||
123 | /* Run the user supplied parser for an assignment */ | |
bcde742e LP |
124 | static int next_assignment( |
125 | const char *unit, | |
126 | const char *filename, | |
127 | unsigned line, | |
128 | ConfigItemLookup lookup, | |
129 | const void *table, | |
130 | const char *section, | |
131 | unsigned section_line, | |
132 | const char *lvalue, | |
133 | const char *rvalue, | |
134 | ConfigParseFlags flags, | |
135 | void *userdata) { | |
f975e971 LP |
136 | |
137 | ConfigParserCallback func = NULL; | |
138 | int ltype = 0; | |
139 | void *data = NULL; | |
140 | int r; | |
141 | ||
142 | assert(filename); | |
143 | assert(line > 0); | |
144 | assert(lookup); | |
145 | assert(lvalue); | |
146 | assert(rvalue); | |
147 | ||
148 | r = lookup(table, section, lvalue, &func, <ype, &data, userdata); | |
149 | if (r < 0) | |
150 | return r; | |
d937fbbd | 151 | if (r > 0) { |
0b954099 LP |
152 | if (!func) |
153 | return 0; | |
d937fbbd | 154 | |
0b954099 LP |
155 | return func(unit, filename, line, section, section_line, |
156 | lvalue, ltype, rvalue, data, userdata); | |
d937fbbd | 157 | } |
f975e971 | 158 | |
46205bb6 | 159 | /* Warn about unknown non-extension fields. */ |
bcde742e | 160 | if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-")) |
28f30f40 ZJS |
161 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
162 | "Unknown key name '%s' in section '%s', ignoring.", lvalue, section); | |
46205bb6 | 163 | |
f1857be0 | 164 | return 0; |
ed5bcfbe LP |
165 | } |
166 | ||
8a37ce65 | 167 | /* Parse a single logical line */ |
bcde742e LP |
168 | static int parse_line( |
169 | const char* unit, | |
170 | const char *filename, | |
171 | unsigned line, | |
172 | const char *sections, | |
173 | ConfigItemLookup lookup, | |
174 | const void *table, | |
175 | ConfigParseFlags flags, | |
176 | char **section, | |
177 | unsigned *section_line, | |
178 | bool *section_ignored, | |
73a4ac8a | 179 | char *l, /* is modified */ |
bcde742e | 180 | void *userdata) { |
f975e971 | 181 | |
7ade8982 | 182 | char *e; |
ed5bcfbe | 183 | |
f975e971 LP |
184 | assert(filename); |
185 | assert(line > 0); | |
186 | assert(lookup); | |
187 | assert(l); | |
188 | ||
b2aa81ef | 189 | l = strstrip(l); |
73a4ac8a | 190 | if (isempty(l)) |
ed5bcfbe | 191 | return 0; |
1ea86b18 | 192 | |
73a4ac8a | 193 | if (l[0] == '\n') |
1ea86b18 | 194 | return 0; |
ed5bcfbe | 195 | |
78d17fa0 YW |
196 | if (!utf8_is_valid(l)) |
197 | return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l); | |
198 | ||
deec0b6d LP |
199 | if (l[0] == '[') { |
200 | _cleanup_free_ char *n = NULL; | |
ed5bcfbe | 201 | size_t k; |
ed5bcfbe | 202 | |
b2aa81ef | 203 | k = strlen(l); |
ed5bcfbe LP |
204 | assert(k > 0); |
205 | ||
3de39a1a YW |
206 | if (l[k-1] != ']') |
207 | return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Invalid section header '%s'", l); | |
ed5bcfbe | 208 | |
f975e971 LP |
209 | n = strndup(l+1, k-2); |
210 | if (!n) | |
7a602af0 | 211 | return log_oom(); |
ed5bcfbe | 212 | |
cec7f09d LP |
213 | if (!string_is_safe(n)) |
214 | return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Bad characters in section header '%s'", l); | |
215 | ||
f975e971 | 216 | if (sections && !nulstr_contains(sections, n)) { |
73a4ac8a | 217 | bool ignore; |
42f4e3c4 | 218 | |
73a4ac8a | 219 | ignore = (flags & CONFIG_PARSE_RELAXED) || startswith(n, "X-"); |
ddeb3f5d ZJS |
220 | |
221 | if (!ignore) | |
222 | NULSTR_FOREACH(t, sections) | |
73a4ac8a | 223 | if (streq_ptr(n, startswith(t, "-"))) { /* Ignore sections prefixed with "-" in valid section list */ |
ddeb3f5d ZJS |
224 | ignore = true; |
225 | break; | |
226 | } | |
227 | ||
228 | if (!ignore) | |
12ca818f | 229 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n); |
f975e971 | 230 | |
a1e58e8e | 231 | *section = mfree(*section); |
71a61510 | 232 | *section_line = 0; |
342aea19 | 233 | *section_ignored = true; |
f975e971 | 234 | } else { |
97b9c506 | 235 | free_and_replace(*section, n); |
71a61510 | 236 | *section_line = line; |
342aea19 | 237 | *section_ignored = false; |
f975e971 | 238 | } |
ed5bcfbe LP |
239 | |
240 | return 0; | |
241 | } | |
242 | ||
62f168a0 | 243 | if (sections && !*section) { |
bcde742e | 244 | if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored) |
12ca818f | 245 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring."); |
62f168a0 | 246 | |
10e87ee7 | 247 | return 0; |
62f168a0 | 248 | } |
10e87ee7 | 249 | |
f975e971 | 250 | e = strchr(l, '='); |
2d4fffb0 ZJS |
251 | if (!e) |
252 | return log_syntax(unit, LOG_WARNING, filename, line, 0, | |
253 | "Missing '=', ignoring line."); | |
8be8ed8c ZJS |
254 | if (e == l) |
255 | return log_syntax(unit, LOG_WARNING, filename, line, 0, | |
256 | "Missing key name before '=', ignoring line."); | |
ed5bcfbe LP |
257 | |
258 | *e = 0; | |
259 | e++; | |
260 | ||
e8e581bf ZJS |
261 | return next_assignment(unit, |
262 | filename, | |
263 | line, | |
264 | lookup, | |
265 | table, | |
266 | *section, | |
71a61510 | 267 | *section_line, |
e8e581bf ZJS |
268 | strstrip(l), |
269 | strstrip(e), | |
bcde742e | 270 | flags, |
e8e581bf | 271 | userdata); |
ed5bcfbe LP |
272 | } |
273 | ||
274 | /* Go through the file and parse each line */ | |
c2f781bc YW |
275 | int config_parse( |
276 | const char *unit, | |
277 | const char *filename, | |
278 | FILE *f, | |
279 | const char *sections, | |
280 | ConfigItemLookup lookup, | |
281 | const void *table, | |
282 | ConfigParseFlags flags, | |
283 | void *userdata, | |
8524db50 | 284 | struct stat *ret_stat) { |
f975e971 | 285 | |
7fd1b19b HH |
286 | _cleanup_free_ char *section = NULL, *continuation = NULL; |
287 | _cleanup_fclose_ FILE *ours = NULL; | |
71a61510 | 288 | unsigned line = 0, section_line = 0; |
f9761a89 | 289 | bool section_ignored = false, bom_seen = false; |
8524db50 | 290 | struct stat st; |
14f594b9 | 291 | int r, fd; |
ed5bcfbe LP |
292 | |
293 | assert(filename); | |
f975e971 | 294 | assert(lookup); |
ed5bcfbe | 295 | |
87f0e418 | 296 | if (!f) { |
245802dd | 297 | f = ours = fopen(filename, "re"); |
f975e971 | 298 | if (!f) { |
36f822c4 ZJS |
299 | /* Only log on request, except for ENOENT, |
300 | * since we return 0 to the caller. */ | |
bcde742e | 301 | if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT) |
b8b846d7 LP |
302 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, |
303 | "Failed to open configuration file '%s': %m", filename); | |
8524db50 YW |
304 | |
305 | if (errno == ENOENT) { | |
306 | if (ret_stat) | |
307 | *ret_stat = (struct stat) {}; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | return -errno; | |
87f0e418 | 313 | } |
ed5bcfbe LP |
314 | } |
315 | ||
14f594b9 | 316 | fd = fileno(f); |
4f9ff96a | 317 | if (fd >= 0) { /* stream might not have an fd, let's be careful hence */ |
4f9ff96a LP |
318 | |
319 | if (fstat(fd, &st) < 0) | |
320 | return log_full_errno(FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_ERR : LOG_DEBUG, errno, | |
321 | "Failed to fstat(%s): %m", filename); | |
322 | ||
323 | (void) stat_warn_permissions(filename, &st); | |
9fd8d678 | 324 | } else |
8524db50 | 325 | st = (struct stat) {}; |
fdb9161c | 326 | |
9dd7ea9a | 327 | for (;;) { |
e6dde451 | 328 | _cleanup_free_ char *buf = NULL; |
3dab2943 | 329 | bool escaped = false; |
92b5e605 | 330 | char *l, *p, *e; |
ed5bcfbe | 331 | |
e6dde451 LP |
332 | r = read_line(f, LONG_LINE_MAX, &buf); |
333 | if (r == 0) | |
334 | break; | |
335 | if (r == -ENOBUFS) { | |
bcde742e | 336 | if (flags & CONFIG_PARSE_WARN) |
e6dde451 LP |
337 | log_error_errno(r, "%s:%u: Line too long", filename, line); |
338 | ||
339 | return r; | |
340 | } | |
341 | if (r < 0) { | |
5aca2e67 | 342 | if (FLAGS_SET(flags, CONFIG_PARSE_WARN)) |
e6dde451 | 343 | log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line); |
ed5bcfbe | 344 | |
e6dde451 | 345 | return r; |
ed5bcfbe LP |
346 | } |
347 | ||
68c1ac15 YW |
348 | line++; |
349 | ||
0ef69585 YW |
350 | l = skip_leading_chars(buf, WHITESPACE); |
351 | if (*l != '\0' && strchr(COMMENTS, *l)) | |
9adbfeb3 YW |
352 | continue; |
353 | ||
9dd7ea9a | 354 | l = buf; |
f9761a89 | 355 | if (!bom_seen) { |
e6dde451 | 356 | char *q; |
9dd7ea9a | 357 | |
e6dde451 LP |
358 | q = startswith(buf, UTF8_BYTE_ORDER_MARK); |
359 | if (q) { | |
360 | l = q; | |
f9761a89 | 361 | bom_seen = true; |
e6dde451 LP |
362 | } |
363 | } | |
3dab2943 LP |
364 | |
365 | if (continuation) { | |
e6dde451 | 366 | if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) { |
bcde742e | 367 | if (flags & CONFIG_PARSE_WARN) |
e6dde451 LP |
368 | log_error("%s:%u: Continuation line too long", filename, line); |
369 | return -ENOBUFS; | |
370 | } | |
371 | ||
c2bc710b | 372 | if (!strextend(&continuation, l)) { |
bcde742e | 373 | if (flags & CONFIG_PARSE_WARN) |
36f822c4 | 374 | log_oom(); |
245802dd | 375 | return -ENOMEM; |
36f822c4 | 376 | } |
3dab2943 | 377 | |
92b5e605 | 378 | p = continuation; |
3dab2943 LP |
379 | } else |
380 | p = l; | |
381 | ||
382 | for (e = p; *e; e++) { | |
383 | if (escaped) | |
384 | escaped = false; | |
385 | else if (*e == '\\') | |
386 | escaped = true; | |
387 | } | |
388 | ||
389 | if (escaped) { | |
390 | *(e-1) = ' '; | |
391 | ||
92b5e605 | 392 | if (!continuation) { |
f975e971 | 393 | continuation = strdup(l); |
36f822c4 | 394 | if (!continuation) { |
bcde742e | 395 | if (flags & CONFIG_PARSE_WARN) |
36f822c4 | 396 | log_oom(); |
245802dd | 397 | return -ENOMEM; |
36f822c4 | 398 | } |
3dab2943 LP |
399 | } |
400 | ||
401 | continue; | |
402 | } | |
403 | ||
e8e581bf ZJS |
404 | r = parse_line(unit, |
405 | filename, | |
68c1ac15 | 406 | line, |
e8e581bf ZJS |
407 | sections, |
408 | lookup, | |
409 | table, | |
bcde742e | 410 | flags, |
e8e581bf | 411 | §ion, |
71a61510 | 412 | §ion_line, |
342aea19 | 413 | §ion_ignored, |
e8e581bf ZJS |
414 | p, |
415 | userdata); | |
36f822c4 | 416 | if (r < 0) { |
bcde742e | 417 | if (flags & CONFIG_PARSE_WARN) |
e6dde451 | 418 | log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line); |
245802dd | 419 | return r; |
36f822c4 | 420 | } |
92b5e605 LP |
421 | |
422 | continuation = mfree(continuation); | |
ed5bcfbe LP |
423 | } |
424 | ||
4f29e0db FB |
425 | if (continuation) { |
426 | r = parse_line(unit, | |
427 | filename, | |
428 | ++line, | |
429 | sections, | |
430 | lookup, | |
431 | table, | |
432 | flags, | |
433 | §ion, | |
434 | §ion_line, | |
435 | §ion_ignored, | |
436 | continuation, | |
437 | userdata); | |
438 | if (r < 0) { | |
439 | if (flags & CONFIG_PARSE_WARN) | |
440 | log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line); | |
441 | return r; | |
4f29e0db FB |
442 | } |
443 | } | |
444 | ||
8524db50 YW |
445 | if (ret_stat) |
446 | *ret_stat = st; | |
4f9ff96a | 447 | |
8b8024f1 | 448 | return 1; |
ed5bcfbe LP |
449 | } |
450 | ||
acfbd71c | 451 | int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st) { |
8524db50 YW |
452 | _cleanup_free_ struct stat *st_copy = NULL; |
453 | _cleanup_free_ char *path_copy = NULL; | |
454 | int r; | |
455 | ||
456 | assert(stats_by_path); | |
457 | assert(path); | |
458 | assert(st); | |
459 | ||
460 | r = hashmap_ensure_allocated(stats_by_path, &path_hash_ops_free_free); | |
461 | if (r < 0) | |
462 | return r; | |
463 | ||
464 | st_copy = newdup(struct stat, st, 1); | |
465 | if (!st_copy) | |
466 | return -ENOMEM; | |
467 | ||
468 | path_copy = strdup(path); | |
469 | if (!path) | |
470 | return -ENOMEM; | |
471 | ||
472 | r = hashmap_put(*stats_by_path, path_copy, st_copy); | |
473 | if (r < 0) | |
474 | return r; | |
475 | ||
476 | assert(r > 0); | |
477 | TAKE_PTR(path_copy); | |
478 | TAKE_PTR(st_copy); | |
479 | return 0; | |
480 | } | |
481 | ||
23bb31aa | 482 | static int config_parse_many_files( |
8b8024f1 | 483 | const char* const* conf_files, |
23bb31aa | 484 | char **files, |
43688c49 ZJS |
485 | const char *sections, |
486 | ConfigItemLookup lookup, | |
487 | const void *table, | |
bcde742e | 488 | ConfigParseFlags flags, |
4f9ff96a | 489 | void *userdata, |
8524db50 | 490 | Hashmap **ret_stats_by_path) { |
43688c49 | 491 | |
8524db50 | 492 | _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
9ac2f3c4 | 493 | _cleanup_ordered_hashmap_free_ OrderedHashmap *dropins = NULL; |
93f1da45 | 494 | _cleanup_set_free_ Set *inodes = NULL; |
8524db50 | 495 | struct stat st; |
e8461023 JT |
496 | int r; |
497 | ||
8524db50 YW |
498 | if (ret_stats_by_path) { |
499 | stats_by_path = hashmap_new(&path_hash_ops_free_free); | |
500 | if (!stats_by_path) | |
501 | return -ENOMEM; | |
502 | } | |
503 | ||
93f1da45 MY |
504 | STRV_FOREACH(fn, files) { |
505 | _cleanup_free_ struct stat *st_dropin = NULL; | |
9ac2f3c4 MY |
506 | _cleanup_fclose_ FILE *f = NULL; |
507 | int fd; | |
508 | ||
509 | f = fopen(*fn, "re"); | |
510 | if (!f) { | |
511 | if (errno == ENOENT) | |
512 | continue; | |
513 | ||
514 | return -errno; | |
515 | } | |
516 | ||
517 | fd = fileno(f); | |
518 | ||
519 | r = ordered_hashmap_ensure_put(&dropins, &config_file_hash_ops_fclose, *fn, f); | |
520 | if (r < 0) { | |
521 | assert(r != -EEXIST); | |
522 | return r; | |
523 | } | |
524 | assert(r > 0); | |
525 | TAKE_PTR(f); | |
526 | ||
527 | /* Get inodes for all drop-ins. Later we'll verify if main config is a symlink to or is | |
528 | * symlinked as one of them. If so, we skip reading main config file directly. */ | |
93f1da45 MY |
529 | |
530 | st_dropin = new(struct stat, 1); | |
531 | if (!st_dropin) | |
532 | return -ENOMEM; | |
533 | ||
9ac2f3c4 MY |
534 | if (fstat(fd, st_dropin) < 0) |
535 | return -errno; | |
93f1da45 MY |
536 | |
537 | r = set_ensure_consume(&inodes, &inode_hash_ops, TAKE_PTR(st_dropin)); | |
538 | if (r < 0) | |
539 | return r; | |
540 | } | |
541 | ||
8b8024f1 | 542 | /* First read the first found main config file. */ |
2034c8b8 | 543 | STRV_FOREACH(fn, conf_files) { |
9ac2f3c4 MY |
544 | _cleanup_fclose_ FILE *f = NULL; |
545 | ||
546 | f = fopen(*fn, "re"); | |
547 | if (!f) { | |
548 | if (errno == ENOENT) | |
549 | continue; | |
550 | ||
551 | return -errno; | |
552 | } | |
553 | ||
93f1da45 | 554 | if (inodes) { |
9ac2f3c4 MY |
555 | if (fstat(fileno(f), &st) < 0) |
556 | return -errno; | |
93f1da45 MY |
557 | |
558 | if (set_contains(inodes, &st)) { | |
9ac2f3c4 | 559 | log_debug("%s: symlink to/symlinked as drop-in, will be read later.", *fn); |
93f1da45 MY |
560 | continue; |
561 | } | |
562 | } | |
563 | ||
9ac2f3c4 | 564 | r = config_parse(NULL, *fn, f, sections, lookup, table, flags, userdata, &st); |
e8461023 JT |
565 | if (r < 0) |
566 | return r; | |
9ac2f3c4 MY |
567 | if (r > 0) { |
568 | if (ret_stats_by_path) { | |
569 | r = hashmap_put_stats_by_path(&stats_by_path, *fn, &st); | |
570 | if (r < 0) | |
571 | return r; | |
572 | } | |
8524db50 | 573 | |
9ac2f3c4 | 574 | break; |
8524db50 | 575 | } |
e8461023 JT |
576 | } |
577 | ||
8b8024f1 | 578 | /* Then read all the drop-ins. */ |
9ac2f3c4 MY |
579 | |
580 | const char *path_dropin; | |
581 | FILE *f_dropin; | |
582 | ORDERED_HASHMAP_FOREACH_KEY(f_dropin, path_dropin, dropins) { | |
583 | r = config_parse(NULL, path_dropin, f_dropin, sections, lookup, table, flags, userdata, &st); | |
e8461023 JT |
584 | if (r < 0) |
585 | return r; | |
8524db50 YW |
586 | if (r == 0) |
587 | continue; | |
588 | ||
589 | if (ret_stats_by_path) { | |
9ac2f3c4 | 590 | r = hashmap_put_stats_by_path(&stats_by_path, path_dropin, &st); |
8524db50 YW |
591 | if (r < 0) |
592 | return r; | |
593 | } | |
e8461023 JT |
594 | } |
595 | ||
8524db50 YW |
596 | if (ret_stats_by_path) |
597 | *ret_stats_by_path = TAKE_PTR(stats_by_path); | |
4f9ff96a | 598 | |
e8461023 JT |
599 | return 0; |
600 | } | |
601 | ||
07e0ffc8 FB |
602 | /* Parse one main config file located in /etc/systemd and its drop-ins, which is what all systemd daemons |
603 | * do. */ | |
604 | int config_parse_config_file( | |
23bb31aa | 605 | const char *conf_file, |
23bb31aa ZJS |
606 | const char *sections, |
607 | ConfigItemLookup lookup, | |
608 | const void *table, | |
bcde742e | 609 | ConfigParseFlags flags, |
07e0ffc8 | 610 | void *userdata) { |
23bb31aa | 611 | |
07e0ffc8 FB |
612 | _cleanup_strv_free_ char **dropins = NULL, **dropin_dirs = NULL; |
613 | char **conf_paths = CONF_PATHS_STRV(""); | |
23bb31aa ZJS |
614 | int r; |
615 | ||
07e0ffc8 FB |
616 | assert(conf_file); |
617 | ||
618 | /* build the dropin dir list */ | |
619 | dropin_dirs = new0(char*, strv_length(conf_paths) + 1); | |
620 | if (!dropin_dirs) { | |
621 | if (flags & CONFIG_PARSE_WARN) | |
622 | return log_oom(); | |
623 | return -ENOMEM; | |
624 | } | |
625 | ||
626 | size_t i = 0; | |
627 | STRV_FOREACH(p, conf_paths) { | |
628 | char *d; | |
629 | ||
630 | d = strjoin(*p, "systemd/", conf_file, ".d"); | |
631 | if (!d) { | |
632 | if (flags & CONFIG_PARSE_WARN) | |
633 | return log_oom(); | |
634 | return -ENOMEM; | |
635 | } | |
636 | ||
637 | dropin_dirs[i++] = d; | |
638 | } | |
639 | ||
640 | r = conf_files_list_strv(&dropins, ".conf", NULL, 0, (const char**) dropin_dirs); | |
23bb31aa ZJS |
641 | if (r < 0) |
642 | return r; | |
643 | ||
07e0ffc8 FB |
644 | const char *sysconf_file = strjoina(PKGSYSCONFDIR, "/", conf_file); |
645 | ||
646 | return config_parse_many_files(STRV_MAKE_CONST(sysconf_file), dropins, | |
647 | sections, lookup, table, flags, userdata, NULL); | |
23bb31aa ZJS |
648 | } |
649 | ||
650 | /* Parse each config file in the directories specified as strv. */ | |
651 | int config_parse_many( | |
8b8024f1 | 652 | const char* const* conf_files, |
23bb31aa ZJS |
653 | const char* const* conf_file_dirs, |
654 | const char *dropin_dirname, | |
947f59ba | 655 | const char *root, |
23bb31aa ZJS |
656 | const char *sections, |
657 | ConfigItemLookup lookup, | |
658 | const void *table, | |
bcde742e | 659 | ConfigParseFlags flags, |
9f83091e | 660 | void *userdata, |
ead3a3fc RP |
661 | Hashmap **ret_stats_by_path, |
662 | char ***ret_dropin_files) { | |
23bb31aa | 663 | |
23bb31aa | 664 | _cleanup_strv_free_ char **files = NULL; |
23bb31aa ZJS |
665 | int r; |
666 | ||
bdb2d3c6 YW |
667 | assert(conf_file_dirs); |
668 | assert(dropin_dirname); | |
669 | assert(sections); | |
670 | assert(table); | |
671 | ||
35c0e344 | 672 | r = conf_files_list_dropins(&files, dropin_dirname, root, conf_file_dirs); |
bdb2d3c6 YW |
673 | if (r < 0) |
674 | return r; | |
675 | ||
ead3a3fc RP |
676 | r = config_parse_many_files(conf_files, files, sections, lookup, table, flags, userdata, ret_stats_by_path); |
677 | if (r < 0) | |
678 | return r; | |
679 | ||
680 | if (ret_dropin_files) | |
681 | *ret_dropin_files = TAKE_PTR(files); | |
682 | ||
683 | return 0; | |
bdb2d3c6 YW |
684 | } |
685 | ||
3f4dfd9d | 686 | static int dropins_get_stats_by_path( |
bdb2d3c6 YW |
687 | const char* conf_file, |
688 | const char* const* conf_file_dirs, | |
3f4dfd9d | 689 | Hashmap **stats_by_path) { |
bdb2d3c6 YW |
690 | |
691 | _cleanup_strv_free_ char **files = NULL; | |
692 | _cleanup_free_ char *dropin_dirname = NULL; | |
bdb2d3c6 YW |
693 | int r; |
694 | ||
695 | assert(conf_file); | |
696 | assert(conf_file_dirs); | |
697 | assert(stats_by_path); | |
698 | ||
bdb2d3c6 | 699 | r = path_extract_filename(conf_file, &dropin_dirname); |
23bb31aa ZJS |
700 | if (r < 0) |
701 | return r; | |
bdb2d3c6 YW |
702 | if (r == O_DIRECTORY) |
703 | return -EINVAL; | |
704 | ||
705 | if (!strextend(&dropin_dirname, ".d")) | |
706 | return -ENOMEM; | |
23bb31aa | 707 | |
35c0e344 | 708 | r = conf_files_list_dropins(&files, dropin_dirname, /* root = */ NULL, conf_file_dirs); |
23bb31aa ZJS |
709 | if (r < 0) |
710 | return r; | |
711 | ||
bdb2d3c6 | 712 | STRV_FOREACH(fn, files) { |
3f4dfd9d YW |
713 | struct stat st; |
714 | ||
bdb2d3c6 YW |
715 | if (stat(*fn, &st) < 0) { |
716 | if (errno == ENOENT) | |
717 | continue; | |
718 | ||
719 | return -errno; | |
720 | } | |
721 | ||
3f4dfd9d | 722 | r = hashmap_put_stats_by_path(stats_by_path, *fn, &st); |
bdb2d3c6 YW |
723 | if (r < 0) |
724 | return r; | |
725 | } | |
726 | ||
727 | return 0; | |
728 | } | |
729 | ||
730 | int config_get_stats_by_path( | |
731 | const char *suffix, | |
732 | const char *root, | |
733 | unsigned flags, | |
734 | const char* const* dirs, | |
3f4dfd9d | 735 | bool check_dropins, |
bdb2d3c6 YW |
736 | Hashmap **ret) { |
737 | ||
738 | _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; | |
739 | _cleanup_strv_free_ char **files = NULL; | |
740 | int r; | |
741 | ||
742 | assert(suffix); | |
743 | assert(dirs); | |
744 | assert(ret); | |
745 | ||
3f4dfd9d YW |
746 | /* Unlike config_parse(), this does not support stream. */ |
747 | ||
bdb2d3c6 YW |
748 | r = conf_files_list_strv(&files, suffix, root, flags, dirs); |
749 | if (r < 0) | |
750 | return r; | |
751 | ||
bdb2d3c6 | 752 | STRV_FOREACH(f, files) { |
3f4dfd9d YW |
753 | struct stat st; |
754 | ||
755 | /* First read the main config file. */ | |
756 | if (stat(*f, &st) < 0) { | |
757 | if (errno == ENOENT) | |
758 | continue; | |
759 | ||
760 | return -errno; | |
761 | } | |
762 | ||
763 | r = hashmap_put_stats_by_path(&stats_by_path, *f, &st); | |
764 | if (r < 0) | |
765 | return r; | |
766 | ||
767 | if (!check_dropins) | |
768 | continue; | |
769 | ||
770 | /* Then read all the drop-ins if requested. */ | |
771 | r = dropins_get_stats_by_path(*f, dirs, &stats_by_path); | |
bdb2d3c6 YW |
772 | if (r < 0) |
773 | return r; | |
774 | } | |
775 | ||
776 | *ret = TAKE_PTR(stats_by_path); | |
777 | return 0; | |
778 | } | |
779 | ||
780 | bool stats_by_path_equal(Hashmap *a, Hashmap *b) { | |
781 | struct stat *st_a, *st_b; | |
782 | const char *path; | |
783 | ||
784 | if (hashmap_size(a) != hashmap_size(b)) | |
785 | return false; | |
786 | ||
787 | HASHMAP_FOREACH_KEY(st_a, path, a) { | |
788 | st_b = hashmap_get(b, path); | |
789 | if (!st_b) | |
790 | return false; | |
791 | ||
792 | if (!stat_inode_unmodified(st_a, st_b)) | |
793 | return false; | |
794 | } | |
795 | ||
796 | return true; | |
23bb31aa ZJS |
797 | } |
798 | ||
307fe3cd YW |
799 | static void config_section_hash_func(const ConfigSection *c, struct siphash *state) { |
800 | siphash24_compress_string(c->filename, state); | |
801 | siphash24_compress(&c->line, sizeof(c->line), state); | |
802 | } | |
803 | ||
804 | static int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) { | |
805 | int r; | |
806 | ||
807 | r = strcmp(x->filename, y->filename); | |
808 | if (r != 0) | |
809 | return r; | |
810 | ||
811 | return CMP(x->line, y->line); | |
812 | } | |
813 | ||
814 | DEFINE_HASH_OPS(config_section_hash_ops, ConfigSection, config_section_hash_func, config_section_compare_func); | |
815 | ||
08ca764d | 816 | int config_section_new(const char *filename, unsigned line, ConfigSection **ret) { |
307fe3cd YW |
817 | ConfigSection *cs; |
818 | ||
08ca764d YW |
819 | assert(filename); |
820 | assert(line > 0); | |
821 | assert(ret); | |
822 | ||
307fe3cd YW |
823 | cs = malloc0(offsetof(ConfigSection, filename) + strlen(filename) + 1); |
824 | if (!cs) | |
825 | return -ENOMEM; | |
826 | ||
827 | strcpy(cs->filename, filename); | |
828 | cs->line = line; | |
829 | ||
08ca764d | 830 | *ret = TAKE_PTR(cs); |
307fe3cd YW |
831 | return 0; |
832 | } | |
833 | ||
e63c6e9f YW |
834 | int _hashmap_by_section_find_unused_line( |
835 | HashmapBase *entries_by_section, | |
d9171a23 YW |
836 | const char *filename, |
837 | unsigned *ret) { | |
838 | ||
307fe3cd YW |
839 | ConfigSection *cs; |
840 | unsigned n = 0; | |
841 | void *entry; | |
842 | ||
e63c6e9f | 843 | HASHMAP_BASE_FOREACH_KEY(entry, cs, entries_by_section) { |
d9171a23 YW |
844 | if (filename && !streq(cs->filename, filename)) |
845 | continue; | |
846 | n = MAX(n, cs->line); | |
847 | } | |
848 | ||
849 | /* overflow? */ | |
850 | if (n >= UINT_MAX) | |
851 | return -EFBIG; | |
307fe3cd | 852 | |
d9171a23 YW |
853 | *ret = n + 1; |
854 | return 0; | |
307fe3cd YW |
855 | } |
856 | ||
eb3491d9 | 857 | #define DEFINE_PARSER(type, vartype, conv_func) \ |
2d1729ca | 858 | DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value") |
94d75d64 LP |
859 | |
860 | DEFINE_PARSER(int, int, safe_atoi); | |
861 | DEFINE_PARSER(long, long, safe_atoli); | |
134e24e1 | 862 | DEFINE_PARSER(uint8, uint8_t, safe_atou8); |
c7440e74 | 863 | DEFINE_PARSER(uint16, uint16_t, safe_atou16); |
94d75d64 | 864 | DEFINE_PARSER(uint32, uint32_t, safe_atou32); |
b57ebc60 | 865 | DEFINE_PARSER(int32, int32_t, safe_atoi32); |
94d75d64 LP |
866 | DEFINE_PARSER(uint64, uint64_t, safe_atou64); |
867 | DEFINE_PARSER(unsigned, unsigned, safe_atou); | |
868 | DEFINE_PARSER(double, double, safe_atod); | |
869 | DEFINE_PARSER(nsec, nsec_t, parse_nsec); | |
870 | DEFINE_PARSER(sec, usec_t, parse_sec); | |
7b61ce3c | 871 | DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity); |
94d75d64 | 872 | DEFINE_PARSER(mode, mode_t, parse_mode); |
65a0ede2 | 873 | DEFINE_PARSER(pid, pid_t, parse_pid); |
ed5bcfbe | 874 | |
c2f781bc YW |
875 | int config_parse_iec_size( |
876 | const char* unit, | |
877 | const char *filename, | |
878 | unsigned line, | |
879 | const char *section, | |
880 | unsigned section_line, | |
881 | const char *lvalue, | |
882 | int ltype, | |
883 | const char *rvalue, | |
884 | void *data, | |
885 | void *userdata) { | |
ed5bcfbe | 886 | |
99534007 | 887 | size_t *sz = ASSERT_PTR(data); |
59f448cf | 888 | uint64_t v; |
e8e581bf | 889 | int r; |
ed5bcfbe LP |
890 | |
891 | assert(filename); | |
892 | assert(lvalue); | |
893 | assert(rvalue); | |
ed5bcfbe | 894 | |
59f448cf | 895 | r = parse_size(rvalue, 1024, &v); |
1e5f4e8b YW |
896 | if (r >= 0 && (uint64_t) (size_t) v != v) |
897 | r = -ERANGE; | |
898 | if (r < 0) { | |
d96edb2c | 899 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue); |
9ba1a159 LP |
900 | return 0; |
901 | } | |
902 | ||
59f448cf | 903 | *sz = (size_t) v; |
9ba1a159 LP |
904 | return 0; |
905 | } | |
906 | ||
50299121 | 907 | int config_parse_si_uint64( |
78f3c4bc LP |
908 | const char* unit, |
909 | const char *filename, | |
910 | unsigned line, | |
911 | const char *section, | |
912 | unsigned section_line, | |
913 | const char *lvalue, | |
914 | int ltype, | |
915 | const char *rvalue, | |
916 | void *data, | |
917 | void *userdata) { | |
5556b5fe | 918 | |
99534007 | 919 | uint64_t *sz = ASSERT_PTR(data); |
5556b5fe LP |
920 | int r; |
921 | ||
922 | assert(filename); | |
923 | assert(lvalue); | |
924 | assert(rvalue); | |
5556b5fe | 925 | |
50299121 | 926 | r = parse_size(rvalue, 1000, sz); |
d96edb2c YW |
927 | if (r < 0) |
928 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue); | |
5556b5fe | 929 | |
5556b5fe LP |
930 | return 0; |
931 | } | |
9ba1a159 | 932 | |
78f3c4bc LP |
933 | int config_parse_iec_uint64( |
934 | const char* unit, | |
935 | const char *filename, | |
936 | unsigned line, | |
937 | const char *section, | |
938 | unsigned section_line, | |
939 | const char *lvalue, | |
940 | int ltype, | |
941 | const char *rvalue, | |
942 | void *data, | |
943 | void *userdata) { | |
9ba1a159 | 944 | |
99534007 | 945 | uint64_t *bytes = ASSERT_PTR(data); |
e8e581bf | 946 | int r; |
9ba1a159 LP |
947 | |
948 | assert(filename); | |
949 | assert(lvalue); | |
950 | assert(rvalue); | |
9ba1a159 | 951 | |
5556b5fe | 952 | r = parse_size(rvalue, 1024, bytes); |
e8e581bf | 953 | if (r < 0) |
d96edb2c | 954 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue); |
ed5bcfbe | 955 | |
ed5bcfbe LP |
956 | return 0; |
957 | } | |
958 | ||
6e8791a0 TB |
959 | int config_parse_iec_uint64_infinity( |
960 | const char* unit, | |
961 | const char *filename, | |
962 | unsigned line, | |
963 | const char *section, | |
964 | unsigned section_line, | |
965 | const char *lvalue, | |
966 | int ltype, | |
967 | const char *rvalue, | |
968 | void *data, | |
969 | void *userdata) { | |
970 | ||
99534007 | 971 | uint64_t *bytes = ASSERT_PTR(data); |
6e8791a0 TB |
972 | |
973 | assert(rvalue); | |
6e8791a0 TB |
974 | |
975 | if (streq(rvalue, "infinity")) { | |
976 | *bytes = UINT64_MAX; | |
977 | return 0; | |
978 | } | |
979 | ||
980 | return config_parse_iec_uint64(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata); | |
981 | } | |
982 | ||
c2f781bc YW |
983 | int config_parse_bool( |
984 | const char* unit, | |
985 | const char *filename, | |
986 | unsigned line, | |
987 | const char *section, | |
988 | unsigned section_line, | |
989 | const char *lvalue, | |
990 | int ltype, | |
991 | const char *rvalue, | |
992 | void *data, | |
993 | void *userdata) { | |
ed5bcfbe LP |
994 | |
995 | int k; | |
99534007 | 996 | bool *b = ASSERT_PTR(data); |
2c75fb73 | 997 | bool fatal = ltype; |
ed5bcfbe LP |
998 | |
999 | assert(filename); | |
1000 | assert(lvalue); | |
1001 | assert(rvalue); | |
ed5bcfbe | 1002 | |
e8e581bf ZJS |
1003 | k = parse_boolean(rvalue); |
1004 | if (k < 0) { | |
d96edb2c | 1005 | log_syntax(unit, fatal ? LOG_ERR : LOG_WARNING, filename, line, k, |
2c75fb73 ZJS |
1006 | "Failed to parse boolean value%s: %s", |
1007 | fatal ? "" : ", ignoring", rvalue); | |
1008 | return fatal ? -ENOEXEC : 0; | |
ed5bcfbe LP |
1009 | } |
1010 | ||
5d904a6a | 1011 | *b = k; |
ed5bcfbe LP |
1012 | return 0; |
1013 | } | |
1014 | ||
12963533 TH |
1015 | int config_parse_id128( |
1016 | const char *unit, | |
1017 | const char *filename, | |
1018 | unsigned line, | |
1019 | const char *section, | |
1020 | unsigned section_line, | |
1021 | const char *lvalue, | |
1022 | int ltype, | |
1023 | const char *rvalue, | |
1024 | void *data, | |
1025 | void *userdata) { | |
1026 | ||
aea3f594 | 1027 | sd_id128_t *result = data; |
12963533 TH |
1028 | int r; |
1029 | ||
1030 | assert(filename); | |
1031 | assert(lvalue); | |
1032 | assert(rvalue); | |
1033 | ||
aea3f594 ZJS |
1034 | r = id128_from_string_nonzero(rvalue, result); |
1035 | if (r == -ENXIO) | |
1036 | log_syntax(unit, LOG_WARNING, filename, line, r, "128-bit ID/UUID is all 0, ignoring: %s", rvalue); | |
1037 | else if (r < 0) | |
da890466 | 1038 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128-bit ID/UUID, ignoring: %s", rvalue); |
12963533 | 1039 | |
12963533 TH |
1040 | return 0; |
1041 | } | |
1042 | ||
f757855e LP |
1043 | int config_parse_tristate( |
1044 | const char* unit, | |
1045 | const char *filename, | |
1046 | unsigned line, | |
1047 | const char *section, | |
1048 | unsigned section_line, | |
1049 | const char *lvalue, | |
1050 | int ltype, | |
1051 | const char *rvalue, | |
1052 | void *data, | |
1053 | void *userdata) { | |
1054 | ||
b71a721f | 1055 | int r, *t = ASSERT_PTR(data); |
f757855e LP |
1056 | |
1057 | assert(filename); | |
1058 | assert(lvalue); | |
1059 | assert(rvalue); | |
f757855e | 1060 | |
33f2de7b YW |
1061 | /* A tristate is pretty much a boolean, except that it can also take an empty string, |
1062 | * indicating "uninitialized", much like NULL is for a pointer type. */ | |
1063 | ||
1064 | if (isempty(rvalue)) { | |
1065 | *t = -1; | |
1066 | return 0; | |
1067 | } | |
f757855e | 1068 | |
b71a721f LP |
1069 | r = parse_tristate(rvalue, t); |
1070 | if (r < 0) { | |
1071 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
33f2de7b | 1072 | "Failed to parse boolean value for %s=, ignoring: %s", lvalue, rvalue); |
f757855e LP |
1073 | return 0; |
1074 | } | |
1075 | ||
f757855e LP |
1076 | return 0; |
1077 | } | |
1078 | ||
8f2665a4 LP |
1079 | int config_parse_string( |
1080 | const char *unit, | |
1081 | const char *filename, | |
1082 | unsigned line, | |
1083 | const char *section, | |
1084 | unsigned section_line, | |
1085 | const char *lvalue, | |
1086 | int ltype, | |
1087 | const char *rvalue, | |
1088 | void *data, | |
1089 | void *userdata) { | |
1090 | ||
3f87eaa5 | 1091 | char **s = ASSERT_PTR(data); |
ed5bcfbe LP |
1092 | |
1093 | assert(filename); | |
1094 | assert(lvalue); | |
1095 | assert(rvalue); | |
ed5bcfbe | 1096 | |
3f87eaa5 YW |
1097 | if (isempty(rvalue)) { |
1098 | *s = mfree(*s); | |
1099 | return 0; | |
1100 | } | |
2d17d699 | 1101 | |
3f87eaa5 YW |
1102 | if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_SAFE) && !string_is_safe(rvalue)) { |
1103 | _cleanup_free_ char *escaped = NULL; | |
2d17d699 | 1104 | |
3f87eaa5 YW |
1105 | escaped = cescape(rvalue); |
1106 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1107 | "Specified string contains unsafe characters, ignoring: %s", strna(escaped)); | |
2d17d699 LP |
1108 | return 0; |
1109 | } | |
1110 | ||
e289ce7f YW |
1111 | if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_ASCII) && !ascii_is_valid(rvalue)) { |
1112 | _cleanup_free_ char *escaped = NULL; | |
1113 | ||
1114 | escaped = cescape(rvalue); | |
1115 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1116 | "Specified string contains invalid ASCII characters, ignoring: %s", strna(escaped)); | |
1117 | return 0; | |
1118 | } | |
1119 | ||
2d17d699 LP |
1120 | return free_and_strdup_warn(s, empty_to_null(rvalue)); |
1121 | } | |
1122 | ||
fa787a13 YW |
1123 | int config_parse_dns_name( |
1124 | const char *unit, | |
1125 | const char *filename, | |
1126 | unsigned line, | |
1127 | const char *section, | |
1128 | unsigned section_line, | |
1129 | const char *lvalue, | |
1130 | int ltype, | |
1131 | const char *rvalue, | |
1132 | void *data, | |
1133 | void *userdata) { | |
1134 | ||
1135 | char **hostname = ASSERT_PTR(data); | |
1136 | int r; | |
1137 | ||
1138 | assert(filename); | |
1139 | assert(lvalue); | |
1140 | assert(rvalue); | |
1141 | ||
1142 | if (isempty(rvalue)) { | |
1143 | *hostname = mfree(*hostname); | |
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | r = dns_name_is_valid(rvalue); | |
1148 | if (r < 0) { | |
1149 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1150 | "Failed to check validity of DNS domain name '%s', ignoring assignment: %m", rvalue); | |
1151 | return 0; | |
1152 | } | |
1153 | if (r == 0) { | |
1154 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1155 | "Specified invalid DNS domain name, ignoring assignment: %s", rvalue); | |
1156 | return 0; | |
1157 | } | |
1158 | ||
1159 | return free_and_strdup_warn(hostname, rvalue); | |
1160 | } | |
1161 | ||
1162 | int config_parse_hostname( | |
1163 | const char *unit, | |
1164 | const char *filename, | |
1165 | unsigned line, | |
1166 | const char *section, | |
1167 | unsigned section_line, | |
1168 | const char *lvalue, | |
1169 | int ltype, | |
1170 | const char *rvalue, | |
1171 | void *data, | |
1172 | void *userdata) { | |
1173 | ||
1174 | char **hostname = ASSERT_PTR(data); | |
1175 | ||
1176 | assert(filename); | |
1177 | assert(lvalue); | |
1178 | assert(rvalue); | |
1179 | ||
1180 | if (isempty(rvalue)) { | |
1181 | *hostname = mfree(*hostname); | |
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | if (!hostname_is_valid(rvalue, 0)) { | |
1186 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1187 | "Specified invalid hostname, ignoring assignment: %s", rvalue); | |
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | return config_parse_dns_name(unit, filename, line, section, section_line, | |
1192 | lvalue, ltype, rvalue, data, userdata); | |
1193 | } | |
1194 | ||
8f2665a4 LP |
1195 | int config_parse_path( |
1196 | const char *unit, | |
1197 | const char *filename, | |
1198 | unsigned line, | |
1199 | const char *section, | |
1200 | unsigned section_line, | |
1201 | const char *lvalue, | |
1202 | int ltype, | |
1203 | const char *rvalue, | |
1204 | void *data, | |
1205 | void *userdata) { | |
034c6ed7 | 1206 | |
c0d72c43 | 1207 | _cleanup_free_ char *n = NULL; |
2c75fb73 | 1208 | bool fatal = ltype; |
99534007 | 1209 | char **s = ASSERT_PTR(data); |
cd4f53c5 | 1210 | int r; |
034c6ed7 LP |
1211 | |
1212 | assert(filename); | |
1213 | assert(lvalue); | |
1214 | assert(rvalue); | |
034c6ed7 | 1215 | |
8e7b5bd0 | 1216 | if (isempty(rvalue)) |
0fe50629 | 1217 | goto finalize; |
0fe50629 | 1218 | |
7f110ff9 LP |
1219 | n = strdup(rvalue); |
1220 | if (!n) | |
74051b9b | 1221 | return log_oom(); |
034c6ed7 | 1222 | |
cd4f53c5 YW |
1223 | r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue); |
1224 | if (r < 0) | |
1225 | return fatal ? -ENOEXEC : 0; | |
01f78473 | 1226 | |
0fe50629 | 1227 | finalize: |
8e7b5bd0 | 1228 | return free_and_replace(*s, n); |
034c6ed7 | 1229 | } |
57d42a5f | 1230 | |
8249bb72 LP |
1231 | int config_parse_strv( |
1232 | const char *unit, | |
1233 | const char *filename, | |
1234 | unsigned line, | |
1235 | const char *section, | |
1236 | unsigned section_line, | |
1237 | const char *lvalue, | |
1238 | int ltype, | |
1239 | const char *rvalue, | |
1240 | void *data, | |
1241 | void *userdata) { | |
57d42a5f | 1242 | |
99534007 | 1243 | char ***sv = ASSERT_PTR(data); |
00d0fd06 | 1244 | int r; |
57d42a5f LP |
1245 | |
1246 | assert(filename); | |
1247 | assert(lvalue); | |
1248 | assert(rvalue); | |
57d42a5f | 1249 | |
74051b9b | 1250 | if (isempty(rvalue)) { |
8249bb72 | 1251 | *sv = strv_free(*sv); |
853b8397 | 1252 | return 0; |
74051b9b LP |
1253 | } |
1254 | ||
d96edb2c | 1255 | for (const char *p = rvalue;;) { |
34f253f0 | 1256 | char *word = NULL; |
00d0fd06 | 1257 | |
d96edb2c | 1258 | r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE); |
34f253f0 | 1259 | if (r == 0) |
d96edb2c | 1260 | return 0; |
34f253f0 | 1261 | if (r == -ENOMEM) |
853b8397 | 1262 | return log_oom(); |
34f253f0 | 1263 | if (r < 0) { |
d96edb2c YW |
1264 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
1265 | return 0; | |
34f253f0 | 1266 | } |
853b8397 | 1267 | |
34f253f0 | 1268 | r = strv_consume(sv, word); |
853b8397 LP |
1269 | if (r < 0) |
1270 | return log_oom(); | |
7f110ff9 | 1271 | } |
57d42a5f | 1272 | } |
15ae422b | 1273 | |
1e35c5ab RP |
1274 | int config_parse_warn_compat( |
1275 | const char *unit, | |
1276 | const char *filename, | |
1277 | unsigned line, | |
1278 | const char *section, | |
1279 | unsigned section_line, | |
1280 | const char *lvalue, | |
1281 | int ltype, | |
1282 | const char *rvalue, | |
1283 | void *data, | |
1284 | void *userdata) { | |
7ef7e15b | 1285 | |
1e35c5ab RP |
1286 | Disabled reason = ltype; |
1287 | ||
79893116 | 1288 | switch (reason) { |
7ef7e15b | 1289 | |
1e35c5ab RP |
1290 | case DISABLED_CONFIGURATION: |
1291 | log_syntax(unit, LOG_DEBUG, filename, line, 0, | |
1292 | "Support for option %s= has been disabled at compile time and it is ignored", lvalue); | |
1293 | break; | |
7ef7e15b | 1294 | |
1e35c5ab RP |
1295 | case DISABLED_LEGACY: |
1296 | log_syntax(unit, LOG_INFO, filename, line, 0, | |
1297 | "Support for option %s= has been removed and it is ignored", lvalue); | |
1298 | break; | |
7ef7e15b | 1299 | |
1e35c5ab RP |
1300 | case DISABLED_EXPERIMENTAL: |
1301 | log_syntax(unit, LOG_INFO, filename, line, 0, | |
1302 | "Support for option %s= has not yet been enabled and it is ignored", lvalue); | |
1303 | break; | |
7ef7e15b | 1304 | } |
1e35c5ab RP |
1305 | |
1306 | return 0; | |
1307 | } | |
1308 | ||
ca37242e LP |
1309 | int config_parse_log_facility( |
1310 | const char *unit, | |
1311 | const char *filename, | |
1312 | unsigned line, | |
1313 | const char *section, | |
1314 | unsigned section_line, | |
1315 | const char *lvalue, | |
1316 | int ltype, | |
1317 | const char *rvalue, | |
1318 | void *data, | |
1319 | void *userdata) { | |
213ba152 | 1320 | |
213ba152 LP |
1321 | int *o = data, x; |
1322 | ||
1323 | assert(filename); | |
1324 | assert(lvalue); | |
1325 | assert(rvalue); | |
1326 | assert(data); | |
1327 | ||
1328 | x = log_facility_unshifted_from_string(rvalue); | |
1329 | if (x < 0) { | |
b98680b2 | 1330 | log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse log facility, ignoring: %s", rvalue); |
213ba152 LP |
1331 | return 0; |
1332 | } | |
1333 | ||
1334 | *o = (x << 3) | LOG_PRI(*o); | |
1335 | ||
1336 | return 0; | |
1337 | } | |
1338 | ||
ca37242e LP |
1339 | int config_parse_log_level( |
1340 | const char *unit, | |
1341 | const char *filename, | |
1342 | unsigned line, | |
1343 | const char *section, | |
1344 | unsigned section_line, | |
1345 | const char *lvalue, | |
1346 | int ltype, | |
1347 | const char *rvalue, | |
1348 | void *data, | |
1349 | void *userdata) { | |
213ba152 | 1350 | |
213ba152 LP |
1351 | int *o = data, x; |
1352 | ||
1353 | assert(filename); | |
1354 | assert(lvalue); | |
1355 | assert(rvalue); | |
1356 | assert(data); | |
1357 | ||
1358 | x = log_level_from_string(rvalue); | |
1359 | if (x < 0) { | |
b98680b2 | 1360 | log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse log level, ignoring: %s", rvalue); |
213ba152 LP |
1361 | return 0; |
1362 | } | |
1363 | ||
d3070fbd LP |
1364 | if (*o < 0) /* if it wasn't initialized so far, assume zero facility */ |
1365 | *o = x; | |
1366 | else | |
1367 | *o = (*o & LOG_FACMASK) | x; | |
1368 | ||
213ba152 LP |
1369 | return 0; |
1370 | } | |
f757855e LP |
1371 | |
1372 | int config_parse_signal( | |
1373 | const char *unit, | |
1374 | const char *filename, | |
1375 | unsigned line, | |
1376 | const char *section, | |
1377 | unsigned section_line, | |
1378 | const char *lvalue, | |
1379 | int ltype, | |
1380 | const char *rvalue, | |
1381 | void *data, | |
1382 | void *userdata) { | |
1383 | ||
1384 | int *sig = data, r; | |
1385 | ||
1386 | assert(filename); | |
1387 | assert(lvalue); | |
1388 | assert(rvalue); | |
1389 | assert(sig); | |
1390 | ||
29a3db75 | 1391 | r = signal_from_string(rvalue); |
f757855e | 1392 | if (r <= 0) { |
b98680b2 | 1393 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse signal name, ignoring: %s", rvalue); |
f757855e LP |
1394 | return 0; |
1395 | } | |
1396 | ||
1397 | *sig = r; | |
1398 | return 0; | |
1399 | } | |
1400 | ||
1401 | int config_parse_personality( | |
1402 | const char *unit, | |
1403 | const char *filename, | |
1404 | unsigned line, | |
1405 | const char *section, | |
1406 | unsigned section_line, | |
1407 | const char *lvalue, | |
1408 | int ltype, | |
1409 | const char *rvalue, | |
1410 | void *data, | |
1411 | void *userdata) { | |
1412 | ||
1413 | unsigned long *personality = data, p; | |
1414 | ||
1415 | assert(filename); | |
1416 | assert(lvalue); | |
1417 | assert(rvalue); | |
1418 | assert(personality); | |
1419 | ||
40fdd636 LP |
1420 | if (isempty(rvalue)) |
1421 | p = PERSONALITY_INVALID; | |
1422 | else { | |
1423 | p = personality_from_string(rvalue); | |
1424 | if (p == PERSONALITY_INVALID) { | |
d96edb2c | 1425 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue); |
40fdd636 LP |
1426 | return 0; |
1427 | } | |
f757855e LP |
1428 | } |
1429 | ||
1430 | *personality = p; | |
1431 | return 0; | |
1432 | } | |
d31645ad LP |
1433 | |
1434 | int config_parse_ifname( | |
1435 | const char *unit, | |
1436 | const char *filename, | |
1437 | unsigned line, | |
1438 | const char *section, | |
1439 | unsigned section_line, | |
1440 | const char *lvalue, | |
1441 | int ltype, | |
1442 | const char *rvalue, | |
1443 | void *data, | |
1444 | void *userdata) { | |
1445 | ||
99534007 | 1446 | char **s = ASSERT_PTR(data); |
d31645ad LP |
1447 | int r; |
1448 | ||
1449 | assert(filename); | |
1450 | assert(lvalue); | |
1451 | assert(rvalue); | |
d31645ad LP |
1452 | |
1453 | if (isempty(rvalue)) { | |
1454 | *s = mfree(*s); | |
1455 | return 0; | |
1456 | } | |
1457 | ||
1458 | if (!ifname_valid(rvalue)) { | |
d96edb2c | 1459 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue); |
d31645ad LP |
1460 | return 0; |
1461 | } | |
1462 | ||
1463 | r = free_and_strdup(s, rvalue); | |
1464 | if (r < 0) | |
1465 | return log_oom(); | |
1466 | ||
1467 | return 0; | |
1468 | } | |
177d0b20 | 1469 | |
a5053a15 YW |
1470 | int config_parse_ifnames( |
1471 | const char *unit, | |
1472 | const char *filename, | |
1473 | unsigned line, | |
1474 | const char *section, | |
1475 | unsigned section_line, | |
1476 | const char *lvalue, | |
1477 | int ltype, | |
1478 | const char *rvalue, | |
1479 | void *data, | |
1480 | void *userdata) { | |
1481 | ||
1482 | _cleanup_strv_free_ char **names = NULL; | |
99534007 | 1483 | char ***s = ASSERT_PTR(data); |
a5053a15 YW |
1484 | int r; |
1485 | ||
1486 | assert(filename); | |
1487 | assert(lvalue); | |
1488 | assert(rvalue); | |
a5053a15 YW |
1489 | |
1490 | if (isempty(rvalue)) { | |
1491 | *s = strv_free(*s); | |
1492 | return 0; | |
1493 | } | |
1494 | ||
d96edb2c | 1495 | for (const char *p = rvalue;;) { |
a5053a15 YW |
1496 | _cleanup_free_ char *word = NULL; |
1497 | ||
1498 | r = extract_first_word(&p, &word, NULL, 0); | |
d96edb2c YW |
1499 | if (r == -ENOMEM) |
1500 | return log_oom(); | |
a5053a15 | 1501 | if (r < 0) { |
d96edb2c | 1502 | log_syntax(unit, LOG_WARNING, filename, line, r, |
a5053a15 YW |
1503 | "Failed to extract interface name, ignoring assignment: %s", |
1504 | rvalue); | |
1505 | return 0; | |
1506 | } | |
1507 | if (r == 0) | |
1508 | break; | |
1509 | ||
1510 | if (!ifname_valid_full(word, ltype)) { | |
d96edb2c | 1511 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
a5053a15 YW |
1512 | "Interface name is not valid or too long, ignoring assignment: %s", |
1513 | word); | |
1514 | continue; | |
1515 | } | |
1516 | ||
1517 | r = strv_consume(&names, TAKE_PTR(word)); | |
1518 | if (r < 0) | |
1519 | return log_oom(); | |
1520 | } | |
1521 | ||
1522 | r = strv_extend_strv(s, names, true); | |
1523 | if (r < 0) | |
1524 | return log_oom(); | |
1525 | ||
1526 | return 0; | |
1527 | } | |
1528 | ||
177d0b20 SS |
1529 | int config_parse_ip_port( |
1530 | const char *unit, | |
1531 | const char *filename, | |
1532 | unsigned line, | |
1533 | const char *section, | |
1534 | unsigned section_line, | |
1535 | const char *lvalue, | |
1536 | int ltype, | |
1537 | const char *rvalue, | |
1538 | void *data, | |
1539 | void *userdata) { | |
1540 | ||
99534007 | 1541 | uint16_t *s = ASSERT_PTR(data); |
177d0b20 SS |
1542 | uint16_t port; |
1543 | int r; | |
1544 | ||
1545 | assert(filename); | |
1546 | assert(lvalue); | |
1547 | assert(rvalue); | |
177d0b20 SS |
1548 | |
1549 | if (isempty(rvalue)) { | |
1550 | *s = 0; | |
1551 | return 0; | |
1552 | } | |
1553 | ||
1554 | r = parse_ip_port(rvalue, &port); | |
1555 | if (r < 0) { | |
d96edb2c | 1556 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse port '%s'.", rvalue); |
177d0b20 SS |
1557 | return 0; |
1558 | } | |
1559 | ||
1560 | *s = port; | |
1561 | ||
1562 | return 0; | |
1563 | } | |
9ecdba8c | 1564 | |
79138a38 LP |
1565 | int config_parse_mtu( |
1566 | const char *unit, | |
1567 | const char *filename, | |
1568 | unsigned line, | |
1569 | const char *section, | |
1570 | unsigned section_line, | |
1571 | const char *lvalue, | |
1572 | int ltype, | |
1573 | const char *rvalue, | |
1574 | void *data, | |
1575 | void *userdata) { | |
1576 | ||
99534007 | 1577 | uint32_t *mtu = ASSERT_PTR(data); |
79138a38 LP |
1578 | int r; |
1579 | ||
1580 | assert(rvalue); | |
79138a38 LP |
1581 | |
1582 | r = parse_mtu(ltype, rvalue, mtu); | |
1583 | if (r == -ERANGE) { | |
d96edb2c | 1584 | log_syntax(unit, LOG_WARNING, filename, line, r, |
79138a38 LP |
1585 | "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s", |
1586 | (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX, | |
1587 | rvalue); | |
1588 | return 0; | |
1589 | } | |
1590 | if (r < 0) { | |
d96edb2c | 1591 | log_syntax(unit, LOG_WARNING, filename, line, r, |
79138a38 LP |
1592 | "Failed to parse MTU value '%s', ignoring: %m", rvalue); |
1593 | return 0; | |
1594 | } | |
1595 | ||
1596 | return 0; | |
1597 | } | |
4f424df7 LP |
1598 | |
1599 | int config_parse_rlimit( | |
1600 | const char *unit, | |
1601 | const char *filename, | |
1602 | unsigned line, | |
1603 | const char *section, | |
1604 | unsigned section_line, | |
1605 | const char *lvalue, | |
1606 | int ltype, | |
1607 | const char *rvalue, | |
1608 | void *data, | |
1609 | void *userdata) { | |
1610 | ||
1611 | struct rlimit **rl = data, d = {}; | |
1612 | int r; | |
1613 | ||
1614 | assert(rvalue); | |
1615 | assert(rl); | |
1616 | ||
1617 | r = rlimit_parse(ltype, rvalue, &d); | |
1618 | if (r == -EILSEQ) { | |
1619 | log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue); | |
1620 | return 0; | |
1621 | } | |
1622 | if (r < 0) { | |
d96edb2c | 1623 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue); |
4f424df7 LP |
1624 | return 0; |
1625 | } | |
1626 | ||
1627 | if (rl[ltype]) | |
1628 | *rl[ltype] = d; | |
1629 | else { | |
1630 | rl[ltype] = newdup(struct rlimit, &d, 1); | |
1631 | if (!rl[ltype]) | |
1632 | return log_oom(); | |
1633 | } | |
1634 | ||
1635 | return 0; | |
1636 | } | |
c07b23ca | 1637 | |
c2f781bc YW |
1638 | int config_parse_permille( |
1639 | const char* unit, | |
1640 | const char *filename, | |
1641 | unsigned line, | |
1642 | const char *section, | |
1643 | unsigned section_line, | |
1644 | const char *lvalue, | |
1645 | int ltype, | |
1646 | const char *rvalue, | |
1647 | void *data, | |
1648 | void *userdata) { | |
c07b23ca | 1649 | |
99534007 | 1650 | unsigned *permille = ASSERT_PTR(data); |
c07b23ca MKB |
1651 | int r; |
1652 | ||
1653 | assert(filename); | |
1654 | assert(lvalue); | |
1655 | assert(rvalue); | |
c07b23ca MKB |
1656 | |
1657 | r = parse_permille(rvalue); | |
1658 | if (r < 0) { | |
d96edb2c | 1659 | log_syntax(unit, LOG_WARNING, filename, line, r, |
c07b23ca MKB |
1660 | "Failed to parse permille value, ignoring: %s", rvalue); |
1661 | return 0; | |
1662 | } | |
1663 | ||
1664 | *permille = (unsigned) r; | |
1665 | ||
1666 | return 0; | |
1667 | } | |
4df4df5b | 1668 | |
c2f781bc YW |
1669 | int config_parse_vlanprotocol( |
1670 | const char* unit, | |
1671 | const char *filename, | |
1672 | unsigned line, | |
1673 | const char *section, | |
1674 | unsigned section_line, | |
1675 | const char *lvalue, | |
1676 | int ltype, | |
1677 | const char *rvalue, | |
1678 | void *data, | |
1679 | void *userdata) { | |
1680 | ||
4df4df5b | 1681 | int *vlan_protocol = data; |
c2f781bc | 1682 | |
4df4df5b RF |
1683 | assert(filename); |
1684 | assert(lvalue); | |
1685 | ||
1686 | if (isempty(rvalue)) { | |
1687 | *vlan_protocol = -1; | |
1688 | return 0; | |
1689 | } | |
1690 | ||
1691 | if (STR_IN_SET(rvalue, "802.1ad", "802.1AD")) | |
1692 | *vlan_protocol = ETH_P_8021AD; | |
1693 | else if (STR_IN_SET(rvalue, "802.1q", "802.1Q")) | |
1694 | *vlan_protocol = ETH_P_8021Q; | |
1695 | else { | |
d96edb2c | 1696 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
4df4df5b RF |
1697 | "Failed to parse VLAN protocol value, ignoring: %s", rvalue); |
1698 | return 0; | |
1699 | } | |
1700 | ||
1701 | return 0; | |
1702 | } | |
9de5e321 | 1703 | |
99628f36 YW |
1704 | int config_parse_hw_addr( |
1705 | const char *unit, | |
1706 | const char *filename, | |
1707 | unsigned line, | |
1708 | const char *section, | |
1709 | unsigned section_line, | |
1710 | const char *lvalue, | |
1711 | int ltype, | |
1712 | const char *rvalue, | |
1713 | void *data, | |
1714 | void *userdata) { | |
1715 | ||
99534007 | 1716 | struct hw_addr_data a, *hwaddr = ASSERT_PTR(data); |
99628f36 YW |
1717 | int r; |
1718 | ||
1719 | assert(filename); | |
1720 | assert(lvalue); | |
1721 | assert(rvalue); | |
99628f36 YW |
1722 | |
1723 | if (isempty(rvalue)) { | |
1724 | *hwaddr = HW_ADDR_NULL; | |
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | r = parse_hw_addr_full(rvalue, ltype, &a); | |
1729 | if (r < 0) { | |
1730 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1731 | "Not a valid hardware address, ignoring assignment: %s", rvalue); | |
1732 | return 0; | |
1733 | } | |
1734 | ||
1735 | *hwaddr = a; | |
1736 | return 0; | |
1737 | } | |
1738 | ||
1739 | int config_parse_hw_addrs( | |
1740 | const char *unit, | |
1741 | const char *filename, | |
1742 | unsigned line, | |
1743 | const char *section, | |
1744 | unsigned section_line, | |
1745 | const char *lvalue, | |
1746 | int ltype, | |
1747 | const char *rvalue, | |
1748 | void *data, | |
1749 | void *userdata) { | |
1750 | ||
99534007 | 1751 | Set **hwaddrs = ASSERT_PTR(data); |
99628f36 YW |
1752 | int r; |
1753 | ||
1754 | assert(filename); | |
1755 | assert(lvalue); | |
1756 | assert(rvalue); | |
99628f36 YW |
1757 | |
1758 | if (isempty(rvalue)) { | |
1759 | /* Empty assignment resets the list */ | |
1760 | *hwaddrs = set_free(*hwaddrs); | |
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | for (const char *p = rvalue;;) { | |
1765 | _cleanup_free_ char *word = NULL; | |
1766 | _cleanup_free_ struct hw_addr_data *n = NULL; | |
1767 | ||
1768 | r = extract_first_word(&p, &word, NULL, 0); | |
1769 | if (r == 0) | |
1770 | return 0; | |
1771 | if (r == -ENOMEM) | |
1772 | return log_oom(); | |
1773 | if (r < 0) { | |
1774 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1775 | "Invalid syntax, ignoring: %s", rvalue); | |
1776 | return 0; | |
1777 | } | |
1778 | ||
1779 | n = new(struct hw_addr_data, 1); | |
1780 | if (!n) | |
1781 | return log_oom(); | |
1782 | ||
1783 | r = parse_hw_addr_full(word, ltype, n); | |
1784 | if (r < 0) { | |
1785 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1786 | "Not a valid hardware address, ignoring: %s", word); | |
1787 | continue; | |
1788 | } | |
1789 | ||
1790 | r = set_ensure_consume(hwaddrs, &hw_addr_hash_ops_free, TAKE_PTR(n)); | |
1791 | if (r < 0) | |
1792 | return log_oom(); | |
1793 | } | |
1794 | } | |
1795 | ||
aa4f7653 | 1796 | int config_parse_ether_addr( |
0ec2a7a1 YW |
1797 | const char *unit, |
1798 | const char *filename, | |
1799 | unsigned line, | |
1800 | const char *section, | |
1801 | unsigned section_line, | |
1802 | const char *lvalue, | |
1803 | int ltype, | |
1804 | const char *rvalue, | |
1805 | void *data, | |
1806 | void *userdata) { | |
1807 | ||
1808 | _cleanup_free_ struct ether_addr *n = NULL; | |
99534007 | 1809 | struct ether_addr **hwaddr = ASSERT_PTR(data); |
0ec2a7a1 YW |
1810 | int r; |
1811 | ||
1812 | assert(filename); | |
1813 | assert(lvalue); | |
1814 | assert(rvalue); | |
0ec2a7a1 YW |
1815 | |
1816 | if (isempty(rvalue)) { | |
1817 | *hwaddr = mfree(*hwaddr); | |
1818 | return 0; | |
1819 | } | |
1820 | ||
1821 | n = new0(struct ether_addr, 1); | |
1822 | if (!n) | |
1823 | return log_oom(); | |
1824 | ||
227e9ce2 | 1825 | r = parse_ether_addr(rvalue, n); |
0ec2a7a1 YW |
1826 | if (r < 0) { |
1827 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1828 | "Not a valid MAC address, ignoring assignment: %s", rvalue); | |
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | free_and_replace(*hwaddr, n); | |
1833 | ||
1834 | return 0; | |
1835 | } | |
1836 | ||
aa4f7653 | 1837 | int config_parse_ether_addrs( |
0ec2a7a1 YW |
1838 | const char *unit, |
1839 | const char *filename, | |
1840 | unsigned line, | |
1841 | const char *section, | |
1842 | unsigned section_line, | |
1843 | const char *lvalue, | |
1844 | int ltype, | |
1845 | const char *rvalue, | |
1846 | void *data, | |
1847 | void *userdata) { | |
1848 | ||
99534007 | 1849 | Set **hwaddrs = ASSERT_PTR(data); |
0ec2a7a1 YW |
1850 | int r; |
1851 | ||
1852 | assert(filename); | |
1853 | assert(lvalue); | |
1854 | assert(rvalue); | |
0ec2a7a1 YW |
1855 | |
1856 | if (isempty(rvalue)) { | |
1857 | /* Empty assignment resets the list */ | |
c6df73ca | 1858 | *hwaddrs = set_free(*hwaddrs); |
0ec2a7a1 YW |
1859 | return 0; |
1860 | } | |
1861 | ||
1862 | for (const char *p = rvalue;;) { | |
1863 | _cleanup_free_ char *word = NULL; | |
1864 | _cleanup_free_ struct ether_addr *n = NULL; | |
1865 | ||
1866 | r = extract_first_word(&p, &word, NULL, 0); | |
1867 | if (r == 0) | |
1868 | return 0; | |
1869 | if (r == -ENOMEM) | |
1870 | return log_oom(); | |
1871 | if (r < 0) { | |
1872 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1873 | "Invalid syntax, ignoring: %s", rvalue); | |
1874 | return 0; | |
1875 | } | |
1876 | ||
1877 | n = new(struct ether_addr, 1); | |
1878 | if (!n) | |
1879 | return log_oom(); | |
1880 | ||
227e9ce2 | 1881 | r = parse_ether_addr(word, n); |
0ec2a7a1 YW |
1882 | if (r < 0) { |
1883 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1884 | "Not a valid MAC address, ignoring: %s", word); | |
1885 | continue; | |
1886 | } | |
1887 | ||
c6df73ca | 1888 | r = set_ensure_consume(hwaddrs, ðer_addr_hash_ops_free, TAKE_PTR(n)); |
0ec2a7a1 YW |
1889 | if (r < 0) |
1890 | return log_oom(); | |
0ec2a7a1 YW |
1891 | } |
1892 | } | |
1893 | ||
cf074772 YW |
1894 | int config_parse_in_addr_non_null( |
1895 | const char *unit, | |
1896 | const char *filename, | |
1897 | unsigned line, | |
1898 | const char *section, | |
1899 | unsigned section_line, | |
1900 | const char *lvalue, | |
1901 | int ltype, | |
1902 | const char *rvalue, | |
1903 | void *data, | |
1904 | void *userdata) { | |
1905 | ||
1906 | /* data must be a pointer to struct in_addr or in6_addr, and the type is determined by ltype. */ | |
99534007 DT |
1907 | struct in_addr *ipv4 = ASSERT_PTR(data); |
1908 | struct in6_addr *ipv6 = ASSERT_PTR(data); | |
cf074772 YW |
1909 | union in_addr_union a; |
1910 | int r; | |
1911 | ||
1912 | assert(filename); | |
1913 | assert(lvalue); | |
1914 | assert(rvalue); | |
cf074772 YW |
1915 | assert(IN_SET(ltype, AF_INET, AF_INET6)); |
1916 | ||
1917 | if (isempty(rvalue)) { | |
1918 | if (ltype == AF_INET) | |
1919 | *ipv4 = (struct in_addr) {}; | |
1920 | else | |
1921 | *ipv6 = (struct in6_addr) {}; | |
1922 | return 0; | |
1923 | } | |
1924 | ||
1925 | r = in_addr_from_string(ltype, rvalue, &a); | |
1926 | if (r < 0) { | |
1927 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1928 | "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); | |
1929 | return 0; | |
1930 | } | |
1931 | ||
1932 | if (!in_addr_is_set(ltype, &a)) { | |
1933 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1934 | "%s= cannot be the ANY address, ignoring: %s", lvalue, rvalue); | |
1935 | return 0; | |
1936 | } | |
1937 | ||
1938 | if (ltype == AF_INET) | |
1939 | *ipv4 = a.in; | |
1940 | else | |
1941 | *ipv6 = a.in6; | |
1942 | return 0; | |
1943 | } | |
1944 | ||
851cdffd ZJS |
1945 | int config_parse_unsigned_bounded( |
1946 | const char *unit, | |
1947 | const char *filename, | |
1948 | unsigned line, | |
1949 | const char *section, | |
1950 | unsigned section_line, | |
1951 | const char *name, | |
1952 | const char *value, | |
1953 | unsigned min, | |
1954 | unsigned max, | |
1955 | bool ignoring, | |
1956 | unsigned *ret) { | |
1957 | ||
1958 | int r; | |
1959 | ||
1960 | assert(filename); | |
1961 | assert(name); | |
1962 | assert(value); | |
1963 | assert(ret); | |
1964 | ||
1965 | r = safe_atou_bounded(value, min, max, ret); | |
1966 | if (r == -ERANGE) | |
1967 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1968 | "Invalid '%s=%s', allowed range is %u..%u%s.", | |
1969 | name, value, min, max, ignoring ? ", ignoring" : ""); | |
1970 | else if (r < 0) | |
1971 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1972 | "Failed to parse '%s=%s'%s: %m", | |
1973 | name, value, ignoring ? ", ignoring" : ""); | |
1974 | ||
1975 | if (r >= 0) | |
1976 | return 1; /* Return 1 if something was set */ | |
1977 | else if (ignoring) | |
1978 | return 0; | |
1979 | else | |
1980 | return r; | |
1981 | } | |
1982 | ||
9de5e321 | 1983 | DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent, "Failed to parse percent value"); |
0a9f9344 | 1984 | DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad, "Failed to parse permyriad value"); |
4ee8176f | 1985 | DEFINE_CONFIG_PARSE_PTR(config_parse_sec_fix_0, parse_sec_fix_0, usec_t, "Failed to parse time value"); |