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