1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include <sys/types.h>
16 #include "alloc-util.h"
17 #include "conf-files.h"
18 #include "conf-parser.h"
20 #include "extract-word.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "signal-util.h"
30 #include "socket-util.h"
31 #include "string-util.h"
33 #include "syslog-util.h"
34 #include "time-util.h"
37 int config_item_table_lookup(
41 ConfigParserCallback
*func
,
46 const ConfigTableItem
*t
;
54 for (t
= table
; t
->lvalue
; t
++) {
56 if (!streq(lvalue
, t
->lvalue
))
59 if (!streq_ptr(section
, t
->section
))
71 int config_item_perf_lookup(
75 ConfigParserCallback
*func
,
80 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
81 const ConfigPerfItem
*p
;
90 p
= lookup(lvalue
, strlen(lvalue
));
94 key
= strjoin(section
, ".", lvalue
);
98 p
= lookup(key
, strlen(key
));
107 *data
= (uint8_t*) userdata
+ p
->offset
;
111 /* Run the user supplied parser for an assignment */
112 static int next_assignment(
114 const char *filename
,
116 ConfigItemLookup lookup
,
119 unsigned section_line
,
122 ConfigParseFlags flags
,
125 ConfigParserCallback func
= NULL
;
136 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
142 return func(unit
, filename
, line
, section
, section_line
,
143 lvalue
, ltype
, rvalue
, data
, userdata
);
148 /* Warn about unknown non-extension fields. */
149 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
150 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown lvalue '%s' in section '%s'", lvalue
, section
);
155 /* Parse a single logical line */
156 static int parse_line(
158 const char *filename
,
160 const char *sections
,
161 ConfigItemLookup lookup
,
163 ConfigParseFlags flags
,
165 unsigned *section_line
,
166 bool *section_ignored
,
181 if (strchr(COMMENTS
"\n", *l
))
184 include
= first_word(l
, ".include");
186 _cleanup_free_
char *fn
= NULL
;
188 /* .includes are a bad idea, we only support them here
189 * for historical reasons. They create cyclic include
190 * problems and make it difficult to detect
191 * configuration file changes with an easy
192 * stat(). Better approaches, such as .d/ drop-in
195 * Support for them should be eventually removed. */
197 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
198 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
202 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
203 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
204 "Please use drop-in files instead.");
206 fn
= file_in_same_dir(filename
, strstrip(include
));
210 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
221 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
225 n
= strndup(l
+1, k
-2);
229 if (sections
&& !nulstr_contains(sections
, n
)) {
231 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
232 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
235 *section
= mfree(*section
);
237 *section_ignored
= true;
241 *section_line
= line
;
242 *section_ignored
= false;
248 if (sections
&& !*section
) {
250 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
251 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
258 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
265 return next_assignment(unit
,
278 /* Go through the file and parse each line */
279 int config_parse(const char *unit
,
280 const char *filename
,
282 const char *sections
,
283 ConfigItemLookup lookup
,
285 ConfigParseFlags flags
,
288 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
289 _cleanup_fclose_
FILE *ours
= NULL
;
290 unsigned line
= 0, section_line
= 0;
291 bool section_ignored
= false;
298 f
= ours
= fopen(filename
, "re");
300 /* Only log on request, except for ENOENT,
301 * since we return 0 to the caller. */
302 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
303 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
,
304 "Failed to open configuration file '%s': %m", filename
);
305 return errno
== ENOENT
? 0 : -errno
;
309 fd_warn_permissions(filename
, fileno(f
));
312 _cleanup_free_
char *buf
= NULL
;
313 bool escaped
= false;
316 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
320 if (flags
& CONFIG_PARSE_WARN
)
321 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
326 if (CONFIG_PARSE_WARN
)
327 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
333 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
336 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
339 flags
|= CONFIG_PARSE_REFUSE_BOM
;
344 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
345 if (flags
& CONFIG_PARSE_WARN
)
346 log_error("%s:%u: Continuation line too long", filename
, line
);
350 if (!strextend(&continuation
, l
, NULL
)) {
351 if (flags
& CONFIG_PARSE_WARN
)
360 for (e
= p
; *e
; e
++) {
371 continuation
= strdup(l
);
373 if (flags
& CONFIG_PARSE_WARN
)
395 if (flags
& CONFIG_PARSE_WARN
)
396 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
401 continuation
= mfree(continuation
);
407 static int config_parse_many_files(
408 const char *conf_file
,
410 const char *sections
,
411 ConfigItemLookup lookup
,
413 ConfigParseFlags flags
,
420 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
425 STRV_FOREACH(fn
, files
) {
426 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
434 /* Parse each config file in the directories specified as nulstr. */
435 int config_parse_many_nulstr(
436 const char *conf_file
,
437 const char *conf_file_dirs
,
438 const char *sections
,
439 ConfigItemLookup lookup
,
441 ConfigParseFlags flags
,
444 _cleanup_strv_free_
char **files
= NULL
;
447 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
451 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
454 /* Parse each config file in the directories specified as strv. */
455 int config_parse_many(
456 const char *conf_file
,
457 const char* const* conf_file_dirs
,
458 const char *dropin_dirname
,
459 const char *sections
,
460 ConfigItemLookup lookup
,
462 ConfigParseFlags flags
,
465 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
466 _cleanup_strv_free_
char **files
= NULL
;
470 suffix
= strjoina("/", dropin_dirname
);
471 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
475 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
479 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
482 #define DEFINE_PARSER(type, vartype, conv_func) \
483 int config_parse_##type( \
485 const char *filename, \
487 const char *section, \
488 unsigned section_line, \
489 const char *lvalue, \
491 const char *rvalue, \
503 r = conv_func(rvalue, i); \
505 log_syntax(unit, LOG_ERR, filename, line, r, \
506 "Failed to parse %s value, ignoring: %s", \
512 DEFINE_PARSER(int, int, safe_atoi
);
513 DEFINE_PARSER(long, long, safe_atoli
);
514 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
515 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
516 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
517 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
518 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
519 DEFINE_PARSER(double, double, safe_atod
);
520 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
521 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
522 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
524 int config_parse_iec_size(const char* unit
,
525 const char *filename
,
528 unsigned section_line
,
544 r
= parse_size(rvalue
, 1024, &v
);
545 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
546 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
554 int config_parse_si_size(
556 const char *filename
,
559 unsigned section_line
,
575 r
= parse_size(rvalue
, 1000, &v
);
576 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
577 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
585 int config_parse_iec_uint64(
587 const char *filename
,
590 unsigned section_line
,
597 uint64_t *bytes
= data
;
605 r
= parse_size(rvalue
, 1024, bytes
);
607 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
612 int config_parse_bool(const char* unit
,
613 const char *filename
,
616 unsigned section_line
,
632 k
= parse_boolean(rvalue
);
634 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
635 "Failed to parse boolean value%s: %s",
636 fatal
? "" : ", ignoring", rvalue
);
637 return fatal
? -ENOEXEC
: 0;
644 int config_parse_tristate(
646 const char *filename
,
649 unsigned section_line
,
663 /* A tristate is pretty much a boolean, except that it can
664 * also take the special value -1, indicating "uninitialized",
665 * much like NULL is for a pointer type. */
667 k
= parse_boolean(rvalue
);
669 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
677 int config_parse_string(
679 const char *filename
,
682 unsigned section_line
,
696 if (!utf8_is_valid(rvalue
)) {
697 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
715 int config_parse_path(
717 const char *filename
,
720 unsigned section_line
,
735 if (isempty(rvalue
)) {
740 if (!utf8_is_valid(rvalue
)) {
741 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
742 return fatal
? -ENOEXEC
: 0;
745 if (!path_is_absolute(rvalue
)) {
746 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
747 "Not an absolute path%s: %s",
748 fatal
? "" : ", ignoring", rvalue
);
749 return fatal
? -ENOEXEC
: 0;
756 path_kill_slashes(n
);
765 int config_parse_strv(
767 const char *filename
,
770 unsigned section_line
,
785 if (isempty(rvalue
)) {
786 *sv
= strv_free(*sv
);
793 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
799 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
803 if (!utf8_is_valid(word
)) {
804 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, word
);
809 r
= strv_consume(sv
, word
);
817 int config_parse_warn_compat(
819 const char *filename
,
822 unsigned section_line
,
828 Disabled reason
= ltype
;
831 case DISABLED_CONFIGURATION
:
832 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
833 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
835 case DISABLED_LEGACY
:
836 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
837 "Support for option %s= has been removed and it is ignored", lvalue
);
839 case DISABLED_EXPERIMENTAL
:
840 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
841 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
848 int config_parse_log_facility(
850 const char *filename
,
853 unsigned section_line
,
867 x
= log_facility_unshifted_from_string(rvalue
);
869 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
873 *o
= (x
<< 3) | LOG_PRI(*o
);
878 int config_parse_log_level(
880 const char *filename
,
883 unsigned section_line
,
897 x
= log_level_from_string(rvalue
);
899 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
903 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
906 *o
= (*o
& LOG_FACMASK
) | x
;
911 int config_parse_signal(
913 const char *filename
,
916 unsigned section_line
,
930 r
= signal_from_string(rvalue
);
932 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
940 int config_parse_personality(
942 const char *filename
,
945 unsigned section_line
,
952 unsigned long *personality
= data
, p
;
960 p
= PERSONALITY_INVALID
;
962 p
= personality_from_string(rvalue
);
963 if (p
== PERSONALITY_INVALID
) {
964 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
973 int config_parse_ifname(
975 const char *filename
,
978 unsigned section_line
,
993 if (isempty(rvalue
)) {
998 if (!ifname_valid(rvalue
)) {
999 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
1003 r
= free_and_strdup(s
, rvalue
);
1010 int config_parse_ip_port(
1012 const char *filename
,
1014 const char *section
,
1015 unsigned section_line
,
1031 if (isempty(rvalue
)) {
1036 r
= parse_ip_port(rvalue
, &port
);
1038 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1047 int config_parse_join_controllers(
1049 const char *filename
,
1051 const char *section
,
1052 unsigned section_line
,
1059 char ****ret
= data
;
1060 const char *whole_rvalue
= rvalue
;
1062 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1070 _cleanup_free_
char *word
= NULL
;
1074 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1076 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1082 l
= strv_split(word
, ",");
1087 if (strv_length(l
) <= 1) {
1093 controllers
= new(char**, 2);
1100 controllers
[1] = NULL
;
1107 t
= new0(char**, n
+2);
1115 for (a
= controllers
; *a
; a
++)
1116 if (strv_overlap(*a
, l
)) {
1117 if (strv_extend_strv(&l
, *a
, false) < 0) {
1136 t
[n
++] = strv_uniq(l
);
1138 strv_free_free(controllers
);
1142 if (!isempty(rvalue
))
1143 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1145 /* As a special case, return a single empty strv, to override the default */
1147 controllers
= new(char**, 2);
1150 controllers
[0] = strv_new(NULL
, NULL
);
1151 if (!controllers
[0])
1153 controllers
[1] = NULL
;
1156 strv_free_free(*ret
);
1157 *ret
= TAKE_PTR(controllers
);
1162 int config_parse_mtu(
1164 const char *filename
,
1166 const char *section
,
1167 unsigned section_line
,
1174 uint32_t *mtu
= data
;
1180 r
= parse_mtu(ltype
, rvalue
, mtu
);
1182 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1183 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1184 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1189 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1190 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);