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