]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / shared / conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10
11 #include "alloc-util.h"
12 #include "conf-files.h"
13 #include "conf-parser.h"
14 #include "def.h"
15 #include "extract-word.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "fs-util.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "missing.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "rlimit-util.h"
26 #include "signal-util.h"
27 #include "socket-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "syslog-util.h"
31 #include "time-util.h"
32 #include "utf8.h"
33
34 int config_item_table_lookup(
35 const void *table,
36 const char *section,
37 const char *lvalue,
38 ConfigParserCallback *func,
39 int *ltype,
40 void **data,
41 void *userdata) {
42
43 const ConfigTableItem *t;
44
45 assert(table);
46 assert(lvalue);
47 assert(func);
48 assert(ltype);
49 assert(data);
50
51 for (t = table; t->lvalue; t++) {
52
53 if (!streq(lvalue, t->lvalue))
54 continue;
55
56 if (!streq_ptr(section, t->section))
57 continue;
58
59 *func = t->parse;
60 *ltype = t->ltype;
61 *data = t->data;
62 return 1;
63 }
64
65 return 0;
66 }
67
68 int config_item_perf_lookup(
69 const void *table,
70 const char *section,
71 const char *lvalue,
72 ConfigParserCallback *func,
73 int *ltype,
74 void **data,
75 void *userdata) {
76
77 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
78 const ConfigPerfItem *p;
79
80 assert(table);
81 assert(lvalue);
82 assert(func);
83 assert(ltype);
84 assert(data);
85
86 if (section) {
87 const char *key;
88
89 key = strjoina(section, ".", lvalue);
90 p = lookup(key, strlen(key));
91 } else
92 p = lookup(lvalue, strlen(lvalue));
93 if (!p)
94 return 0;
95
96 *func = p->parse;
97 *ltype = p->ltype;
98 *data = (uint8_t*) userdata + p->offset;
99 return 1;
100 }
101
102 /* Run the user supplied parser for an assignment */
103 static int next_assignment(
104 const char *unit,
105 const char *filename,
106 unsigned line,
107 ConfigItemLookup lookup,
108 const void *table,
109 const char *section,
110 unsigned section_line,
111 const char *lvalue,
112 const char *rvalue,
113 ConfigParseFlags flags,
114 void *userdata) {
115
116 ConfigParserCallback func = NULL;
117 int ltype = 0;
118 void *data = NULL;
119 int r;
120
121 assert(filename);
122 assert(line > 0);
123 assert(lookup);
124 assert(lvalue);
125 assert(rvalue);
126
127 r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
128 if (r < 0)
129 return r;
130 if (r > 0) {
131 if (func)
132 return func(unit, filename, line, section, section_line,
133 lvalue, ltype, rvalue, data, userdata);
134
135 return 0;
136 }
137
138 /* Warn about unknown non-extension fields. */
139 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
140 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s', ignoring", lvalue, section);
141
142 return 0;
143 }
144
145 /* Parse a single logical line */
146 static int parse_line(
147 const char* unit,
148 const char *filename,
149 unsigned line,
150 const char *sections,
151 ConfigItemLookup lookup,
152 const void *table,
153 ConfigParseFlags flags,
154 char **section,
155 unsigned *section_line,
156 bool *section_ignored,
157 char *l,
158 void *userdata) {
159
160 char *e, *include;
161
162 assert(filename);
163 assert(line > 0);
164 assert(lookup);
165 assert(l);
166
167 l = strstrip(l);
168 if (!*l)
169 return 0;
170
171 if (*l == '\n')
172 return 0;
173
174 include = first_word(l, ".include");
175 if (include) {
176 _cleanup_free_ char *fn = NULL;
177
178 /* .includes are a bad idea, we only support them here
179 * for historical reasons. They create cyclic include
180 * problems and make it difficult to detect
181 * configuration file changes with an easy
182 * stat(). Better approaches, such as .d/ drop-in
183 * snippets exist.
184 *
185 * Support for them should be eventually removed. */
186
187 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
188 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
189 return 0;
190 }
191
192 log_syntax(unit, LOG_WARNING, filename, line, 0,
193 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
194 "Please use drop-in files instead.");
195
196 fn = file_in_same_dir(filename, strstrip(include));
197 if (!fn)
198 return -ENOMEM;
199
200 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
201 }
202
203 if (!utf8_is_valid(l))
204 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
205
206 if (*l == '[') {
207 size_t k;
208 char *n;
209
210 k = strlen(l);
211 assert(k > 0);
212
213 if (l[k-1] != ']') {
214 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
215 return -EBADMSG;
216 }
217
218 n = strndup(l+1, k-2);
219 if (!n)
220 return -ENOMEM;
221
222 if (sections && !nulstr_contains(sections, n)) {
223
224 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
225 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
226
227 free(n);
228 *section = mfree(*section);
229 *section_line = 0;
230 *section_ignored = true;
231 } else {
232 free_and_replace(*section, n);
233 *section_line = line;
234 *section_ignored = false;
235 }
236
237 return 0;
238 }
239
240 if (sections && !*section) {
241
242 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
243 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
244
245 return 0;
246 }
247
248 e = strchr(l, '=');
249 if (!e) {
250 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
251 return -EINVAL;
252 }
253
254 *e = 0;
255 e++;
256
257 return next_assignment(unit,
258 filename,
259 line,
260 lookup,
261 table,
262 *section,
263 *section_line,
264 strstrip(l),
265 strstrip(e),
266 flags,
267 userdata);
268 }
269
270 /* Go through the file and parse each line */
271 int config_parse(const char *unit,
272 const char *filename,
273 FILE *f,
274 const char *sections,
275 ConfigItemLookup lookup,
276 const void *table,
277 ConfigParseFlags flags,
278 void *userdata) {
279
280 _cleanup_free_ char *section = NULL, *continuation = NULL;
281 _cleanup_fclose_ FILE *ours = NULL;
282 unsigned line = 0, section_line = 0;
283 bool section_ignored = false;
284 int r;
285
286 assert(filename);
287 assert(lookup);
288
289 if (!f) {
290 f = ours = fopen(filename, "re");
291 if (!f) {
292 /* Only log on request, except for ENOENT,
293 * since we return 0 to the caller. */
294 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
295 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
296 "Failed to open configuration file '%s': %m", filename);
297 return errno == ENOENT ? 0 : -errno;
298 }
299 }
300
301 fd_warn_permissions(filename, fileno(f));
302
303 for (;;) {
304 _cleanup_free_ char *buf = NULL;
305 bool escaped = false;
306 char *l, *p, *e;
307
308 r = read_line(f, LONG_LINE_MAX, &buf);
309 if (r == 0)
310 break;
311 if (r == -ENOBUFS) {
312 if (flags & CONFIG_PARSE_WARN)
313 log_error_errno(r, "%s:%u: Line too long", filename, line);
314
315 return r;
316 }
317 if (r < 0) {
318 if (CONFIG_PARSE_WARN)
319 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
320
321 return r;
322 }
323
324 if (strchr(COMMENTS, *skip_leading_chars(buf, WHITESPACE)))
325 continue;
326
327 l = buf;
328 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
329 char *q;
330
331 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
332 if (q) {
333 l = q;
334 flags |= CONFIG_PARSE_REFUSE_BOM;
335 }
336 }
337
338 if (continuation) {
339 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
340 if (flags & CONFIG_PARSE_WARN)
341 log_error("%s:%u: Continuation line too long", filename, line);
342 return -ENOBUFS;
343 }
344
345 if (!strextend(&continuation, l, NULL)) {
346 if (flags & CONFIG_PARSE_WARN)
347 log_oom();
348 return -ENOMEM;
349 }
350
351 p = continuation;
352 } else
353 p = l;
354
355 for (e = p; *e; e++) {
356 if (escaped)
357 escaped = false;
358 else if (*e == '\\')
359 escaped = true;
360 }
361
362 if (escaped) {
363 *(e-1) = ' ';
364
365 if (!continuation) {
366 continuation = strdup(l);
367 if (!continuation) {
368 if (flags & CONFIG_PARSE_WARN)
369 log_oom();
370 return -ENOMEM;
371 }
372 }
373
374 continue;
375 }
376
377 r = parse_line(unit,
378 filename,
379 ++line,
380 sections,
381 lookup,
382 table,
383 flags,
384 &section,
385 &section_line,
386 &section_ignored,
387 p,
388 userdata);
389 if (r < 0) {
390 if (flags & CONFIG_PARSE_WARN)
391 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
392 return r;
393 }
394
395 continuation = mfree(continuation);
396 }
397
398 if (continuation) {
399 r = parse_line(unit,
400 filename,
401 ++line,
402 sections,
403 lookup,
404 table,
405 flags,
406 &section,
407 &section_line,
408 &section_ignored,
409 continuation,
410 userdata);
411 if (r < 0) {
412 if (flags & CONFIG_PARSE_WARN)
413 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
414 return r;
415 }
416 }
417
418 return 0;
419 }
420
421 static int config_parse_many_files(
422 const char *conf_file,
423 char **files,
424 const char *sections,
425 ConfigItemLookup lookup,
426 const void *table,
427 ConfigParseFlags flags,
428 void *userdata) {
429
430 char **fn;
431 int r;
432
433 if (conf_file) {
434 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
435 if (r < 0)
436 return r;
437 }
438
439 STRV_FOREACH(fn, files) {
440 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
441 if (r < 0)
442 return r;
443 }
444
445 return 0;
446 }
447
448 /* Parse each config file in the directories specified as nulstr. */
449 int config_parse_many_nulstr(
450 const char *conf_file,
451 const char *conf_file_dirs,
452 const char *sections,
453 ConfigItemLookup lookup,
454 const void *table,
455 ConfigParseFlags flags,
456 void *userdata) {
457
458 _cleanup_strv_free_ char **files = NULL;
459 int r;
460
461 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
462 if (r < 0)
463 return r;
464
465 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
466 }
467
468 /* Parse each config file in the directories specified as strv. */
469 int config_parse_many(
470 const char *conf_file,
471 const char* const* conf_file_dirs,
472 const char *dropin_dirname,
473 const char *sections,
474 ConfigItemLookup lookup,
475 const void *table,
476 ConfigParseFlags flags,
477 void *userdata) {
478
479 _cleanup_strv_free_ char **dropin_dirs = NULL;
480 _cleanup_strv_free_ char **files = NULL;
481 const char *suffix;
482 int r;
483
484 suffix = strjoina("/", dropin_dirname);
485 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
486 if (r < 0)
487 return r;
488
489 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
490 if (r < 0)
491 return r;
492
493 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
494 }
495
496 #define DEFINE_PARSER(type, vartype, conv_func) \
497 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
498
499 DEFINE_PARSER(int, int, safe_atoi);
500 DEFINE_PARSER(long, long, safe_atoli);
501 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
502 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
503 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
504 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
505 DEFINE_PARSER(unsigned, unsigned, safe_atou);
506 DEFINE_PARSER(double, double, safe_atod);
507 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
508 DEFINE_PARSER(sec, usec_t, parse_sec);
509 DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity);
510 DEFINE_PARSER(mode, mode_t, parse_mode);
511
512 int config_parse_iec_size(const char* unit,
513 const char *filename,
514 unsigned line,
515 const char *section,
516 unsigned section_line,
517 const char *lvalue,
518 int ltype,
519 const char *rvalue,
520 void *data,
521 void *userdata) {
522
523 size_t *sz = data;
524 uint64_t v;
525 int r;
526
527 assert(filename);
528 assert(lvalue);
529 assert(rvalue);
530 assert(data);
531
532 r = parse_size(rvalue, 1024, &v);
533 if (r >= 0 && (uint64_t) (size_t) v != v)
534 r = -ERANGE;
535 if (r < 0) {
536 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
537 return 0;
538 }
539
540 *sz = (size_t) v;
541 return 0;
542 }
543
544 int config_parse_si_size(
545 const char* unit,
546 const char *filename,
547 unsigned line,
548 const char *section,
549 unsigned section_line,
550 const char *lvalue,
551 int ltype,
552 const char *rvalue,
553 void *data,
554 void *userdata) {
555
556 size_t *sz = data;
557 uint64_t v;
558 int r;
559
560 assert(filename);
561 assert(lvalue);
562 assert(rvalue);
563 assert(data);
564
565 r = parse_size(rvalue, 1000, &v);
566 if (r >= 0 && (uint64_t) (size_t) v != v)
567 r = -ERANGE;
568 if (r < 0) {
569 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
570 return 0;
571 }
572
573 *sz = (size_t) v;
574 return 0;
575 }
576
577 int config_parse_iec_uint64(
578 const char* unit,
579 const char *filename,
580 unsigned line,
581 const char *section,
582 unsigned section_line,
583 const char *lvalue,
584 int ltype,
585 const char *rvalue,
586 void *data,
587 void *userdata) {
588
589 uint64_t *bytes = data;
590 int r;
591
592 assert(filename);
593 assert(lvalue);
594 assert(rvalue);
595 assert(data);
596
597 r = parse_size(rvalue, 1024, bytes);
598 if (r < 0)
599 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
600
601 return 0;
602 }
603
604 int config_parse_bool(const char* unit,
605 const char *filename,
606 unsigned line,
607 const char *section,
608 unsigned section_line,
609 const char *lvalue,
610 int ltype,
611 const char *rvalue,
612 void *data,
613 void *userdata) {
614
615 int k;
616 bool *b = data;
617 bool fatal = ltype;
618
619 assert(filename);
620 assert(lvalue);
621 assert(rvalue);
622 assert(data);
623
624 k = parse_boolean(rvalue);
625 if (k < 0) {
626 log_syntax(unit, LOG_ERR, filename, line, k,
627 "Failed to parse boolean value%s: %s",
628 fatal ? "" : ", ignoring", rvalue);
629 return fatal ? -ENOEXEC : 0;
630 }
631
632 *b = k;
633 return 0;
634 }
635
636 int config_parse_tristate(
637 const char* unit,
638 const char *filename,
639 unsigned line,
640 const char *section,
641 unsigned section_line,
642 const char *lvalue,
643 int ltype,
644 const char *rvalue,
645 void *data,
646 void *userdata) {
647
648 int k, *t = data;
649
650 assert(filename);
651 assert(lvalue);
652 assert(rvalue);
653 assert(data);
654
655 /* A tristate is pretty much a boolean, except that it can
656 * also take the special value -1, indicating "uninitialized",
657 * much like NULL is for a pointer type. */
658
659 k = parse_boolean(rvalue);
660 if (k < 0) {
661 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
662 return 0;
663 }
664
665 *t = !!k;
666 return 0;
667 }
668
669 int config_parse_string(
670 const char *unit,
671 const char *filename,
672 unsigned line,
673 const char *section,
674 unsigned section_line,
675 const char *lvalue,
676 int ltype,
677 const char *rvalue,
678 void *data,
679 void *userdata) {
680
681 char **s = data;
682
683 assert(filename);
684 assert(lvalue);
685 assert(rvalue);
686 assert(data);
687
688 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
689 return log_oom();
690
691 return 0;
692 }
693
694 int config_parse_path(
695 const char *unit,
696 const char *filename,
697 unsigned line,
698 const char *section,
699 unsigned section_line,
700 const char *lvalue,
701 int ltype,
702 const char *rvalue,
703 void *data,
704 void *userdata) {
705
706 _cleanup_free_ char *n = NULL;
707 bool fatal = ltype;
708 char **s = data;
709 int r;
710
711 assert(filename);
712 assert(lvalue);
713 assert(rvalue);
714 assert(data);
715
716 if (isempty(rvalue))
717 goto finalize;
718
719 n = strdup(rvalue);
720 if (!n)
721 return log_oom();
722
723 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
724 if (r < 0)
725 return fatal ? -ENOEXEC : 0;
726
727 finalize:
728 return free_and_replace(*s, n);
729 }
730
731 int config_parse_strv(
732 const char *unit,
733 const char *filename,
734 unsigned line,
735 const char *section,
736 unsigned section_line,
737 const char *lvalue,
738 int ltype,
739 const char *rvalue,
740 void *data,
741 void *userdata) {
742
743 char ***sv = data;
744 int r;
745
746 assert(filename);
747 assert(lvalue);
748 assert(rvalue);
749 assert(data);
750
751 if (isempty(rvalue)) {
752 *sv = strv_free(*sv);
753 return 0;
754 }
755
756 for (;;) {
757 char *word = NULL;
758
759 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
760 if (r == 0)
761 break;
762 if (r == -ENOMEM)
763 return log_oom();
764 if (r < 0) {
765 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
766 break;
767 }
768
769 r = strv_consume(sv, word);
770 if (r < 0)
771 return log_oom();
772 }
773
774 return 0;
775 }
776
777 int config_parse_warn_compat(
778 const char *unit,
779 const char *filename,
780 unsigned line,
781 const char *section,
782 unsigned section_line,
783 const char *lvalue,
784 int ltype,
785 const char *rvalue,
786 void *data,
787 void *userdata) {
788
789 Disabled reason = ltype;
790
791 switch(reason) {
792
793 case DISABLED_CONFIGURATION:
794 log_syntax(unit, LOG_DEBUG, filename, line, 0,
795 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
796 break;
797
798 case DISABLED_LEGACY:
799 log_syntax(unit, LOG_INFO, filename, line, 0,
800 "Support for option %s= has been removed and it is ignored", lvalue);
801 break;
802
803 case DISABLED_EXPERIMENTAL:
804 log_syntax(unit, LOG_INFO, filename, line, 0,
805 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
806 break;
807 }
808
809 return 0;
810 }
811
812 int config_parse_log_facility(
813 const char *unit,
814 const char *filename,
815 unsigned line,
816 const char *section,
817 unsigned section_line,
818 const char *lvalue,
819 int ltype,
820 const char *rvalue,
821 void *data,
822 void *userdata) {
823
824 int *o = data, x;
825
826 assert(filename);
827 assert(lvalue);
828 assert(rvalue);
829 assert(data);
830
831 x = log_facility_unshifted_from_string(rvalue);
832 if (x < 0) {
833 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
834 return 0;
835 }
836
837 *o = (x << 3) | LOG_PRI(*o);
838
839 return 0;
840 }
841
842 int config_parse_log_level(
843 const char *unit,
844 const char *filename,
845 unsigned line,
846 const char *section,
847 unsigned section_line,
848 const char *lvalue,
849 int ltype,
850 const char *rvalue,
851 void *data,
852 void *userdata) {
853
854 int *o = data, x;
855
856 assert(filename);
857 assert(lvalue);
858 assert(rvalue);
859 assert(data);
860
861 x = log_level_from_string(rvalue);
862 if (x < 0) {
863 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
864 return 0;
865 }
866
867 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
868 *o = x;
869 else
870 *o = (*o & LOG_FACMASK) | x;
871
872 return 0;
873 }
874
875 int config_parse_signal(
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) {
886
887 int *sig = data, r;
888
889 assert(filename);
890 assert(lvalue);
891 assert(rvalue);
892 assert(sig);
893
894 r = signal_from_string(rvalue);
895 if (r <= 0) {
896 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
897 return 0;
898 }
899
900 *sig = r;
901 return 0;
902 }
903
904 int config_parse_personality(
905 const char *unit,
906 const char *filename,
907 unsigned line,
908 const char *section,
909 unsigned section_line,
910 const char *lvalue,
911 int ltype,
912 const char *rvalue,
913 void *data,
914 void *userdata) {
915
916 unsigned long *personality = data, p;
917
918 assert(filename);
919 assert(lvalue);
920 assert(rvalue);
921 assert(personality);
922
923 if (isempty(rvalue))
924 p = PERSONALITY_INVALID;
925 else {
926 p = personality_from_string(rvalue);
927 if (p == PERSONALITY_INVALID) {
928 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
929 return 0;
930 }
931 }
932
933 *personality = p;
934 return 0;
935 }
936
937 int config_parse_ifname(
938 const char *unit,
939 const char *filename,
940 unsigned line,
941 const char *section,
942 unsigned section_line,
943 const char *lvalue,
944 int ltype,
945 const char *rvalue,
946 void *data,
947 void *userdata) {
948
949 char **s = data;
950 int r;
951
952 assert(filename);
953 assert(lvalue);
954 assert(rvalue);
955 assert(data);
956
957 if (isempty(rvalue)) {
958 *s = mfree(*s);
959 return 0;
960 }
961
962 if (!ifname_valid(rvalue)) {
963 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
964 return 0;
965 }
966
967 r = free_and_strdup(s, rvalue);
968 if (r < 0)
969 return log_oom();
970
971 return 0;
972 }
973
974 int config_parse_ip_port(
975 const char *unit,
976 const char *filename,
977 unsigned line,
978 const char *section,
979 unsigned section_line,
980 const char *lvalue,
981 int ltype,
982 const char *rvalue,
983 void *data,
984 void *userdata) {
985
986 uint16_t *s = data;
987 uint16_t port;
988 int r;
989
990 assert(filename);
991 assert(lvalue);
992 assert(rvalue);
993 assert(data);
994
995 if (isempty(rvalue)) {
996 *s = 0;
997 return 0;
998 }
999
1000 r = parse_ip_port(rvalue, &port);
1001 if (r < 0) {
1002 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1003 return 0;
1004 }
1005
1006 *s = port;
1007
1008 return 0;
1009 }
1010
1011 int config_parse_mtu(
1012 const char *unit,
1013 const char *filename,
1014 unsigned line,
1015 const char *section,
1016 unsigned section_line,
1017 const char *lvalue,
1018 int ltype,
1019 const char *rvalue,
1020 void *data,
1021 void *userdata) {
1022
1023 uint32_t *mtu = data;
1024 int r;
1025
1026 assert(rvalue);
1027 assert(mtu);
1028
1029 r = parse_mtu(ltype, rvalue, mtu);
1030 if (r == -ERANGE) {
1031 log_syntax(unit, LOG_ERR, filename, line, r,
1032 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1033 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1034 rvalue);
1035 return 0;
1036 }
1037 if (r < 0) {
1038 log_syntax(unit, LOG_ERR, filename, line, r,
1039 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1040 return 0;
1041 }
1042
1043 return 0;
1044 }
1045
1046 int config_parse_rlimit(
1047 const char *unit,
1048 const char *filename,
1049 unsigned line,
1050 const char *section,
1051 unsigned section_line,
1052 const char *lvalue,
1053 int ltype,
1054 const char *rvalue,
1055 void *data,
1056 void *userdata) {
1057
1058 struct rlimit **rl = data, d = {};
1059 int r;
1060
1061 assert(rvalue);
1062 assert(rl);
1063
1064 r = rlimit_parse(ltype, rvalue, &d);
1065 if (r == -EILSEQ) {
1066 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1067 return 0;
1068 }
1069 if (r < 0) {
1070 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1071 return 0;
1072 }
1073
1074 if (rl[ltype])
1075 *rl[ltype] = d;
1076 else {
1077 rl[ltype] = newdup(struct rlimit, &d, 1);
1078 if (!rl[ltype])
1079 return log_oom();
1080 }
1081
1082 return 0;
1083 }
1084
1085 int config_parse_permille(const char* unit,
1086 const char *filename,
1087 unsigned line,
1088 const char *section,
1089 unsigned section_line,
1090 const char *lvalue,
1091 int ltype,
1092 const char *rvalue,
1093 void *data,
1094 void *userdata) {
1095
1096 unsigned *permille = data;
1097 int r;
1098
1099 assert(filename);
1100 assert(lvalue);
1101 assert(rvalue);
1102 assert(permille);
1103
1104 r = parse_permille(rvalue);
1105 if (r < 0) {
1106 log_syntax(unit, LOG_ERR, filename, line, r,
1107 "Failed to parse permille value, ignoring: %s", rvalue);
1108 return 0;
1109 }
1110
1111 *permille = (unsigned) r;
1112
1113 return 0;
1114 }