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