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"
36 #include "rlimit-util.h"
38 int config_item_table_lookup(
42 ConfigParserCallback
*func
,
47 const ConfigTableItem
*t
;
55 for (t
= table
; t
->lvalue
; t
++) {
57 if (!streq(lvalue
, t
->lvalue
))
60 if (!streq_ptr(section
, t
->section
))
72 int config_item_perf_lookup(
76 ConfigParserCallback
*func
,
81 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
82 const ConfigPerfItem
*p
;
91 p
= lookup(lvalue
, strlen(lvalue
));
95 key
= strjoin(section
, ".", lvalue
);
99 p
= lookup(key
, strlen(key
));
108 *data
= (uint8_t*) userdata
+ p
->offset
;
112 /* Run the user supplied parser for an assignment */
113 static int next_assignment(
115 const char *filename
,
117 ConfigItemLookup lookup
,
120 unsigned section_line
,
123 ConfigParseFlags flags
,
126 ConfigParserCallback func
= NULL
;
137 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
143 return func(unit
, filename
, line
, section
, section_line
,
144 lvalue
, ltype
, rvalue
, data
, userdata
);
149 /* Warn about unknown non-extension fields. */
150 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
151 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown lvalue '%s' in section '%s'", lvalue
, section
);
156 /* Parse a single logical line */
157 static int parse_line(
159 const char *filename
,
161 const char *sections
,
162 ConfigItemLookup lookup
,
164 ConfigParseFlags flags
,
166 unsigned *section_line
,
167 bool *section_ignored
,
182 if (strchr(COMMENTS
"\n", *l
))
185 include
= first_word(l
, ".include");
187 _cleanup_free_
char *fn
= NULL
;
189 /* .includes are a bad idea, we only support them here
190 * for historical reasons. They create cyclic include
191 * problems and make it difficult to detect
192 * configuration file changes with an easy
193 * stat(). Better approaches, such as .d/ drop-in
196 * Support for them should be eventually removed. */
198 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
199 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
203 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
204 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
205 "Please use drop-in files instead.");
207 fn
= file_in_same_dir(filename
, strstrip(include
));
211 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
222 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
226 n
= strndup(l
+1, k
-2);
230 if (sections
&& !nulstr_contains(sections
, n
)) {
232 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
233 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
236 *section
= mfree(*section
);
238 *section_ignored
= true;
242 *section_line
= line
;
243 *section_ignored
= false;
249 if (sections
&& !*section
) {
251 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
252 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
259 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
266 return next_assignment(unit
,
279 /* Go through the file and parse each line */
280 int config_parse(const char *unit
,
281 const char *filename
,
283 const char *sections
,
284 ConfigItemLookup lookup
,
286 ConfigParseFlags flags
,
289 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
290 _cleanup_fclose_
FILE *ours
= NULL
;
291 unsigned line
= 0, section_line
= 0;
292 bool section_ignored
= false;
299 f
= ours
= fopen(filename
, "re");
301 /* Only log on request, except for ENOENT,
302 * since we return 0 to the caller. */
303 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
304 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
,
305 "Failed to open configuration file '%s': %m", filename
);
306 return errno
== ENOENT
? 0 : -errno
;
310 fd_warn_permissions(filename
, fileno(f
));
313 _cleanup_free_
char *buf
= NULL
;
314 bool escaped
= false;
317 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
321 if (flags
& CONFIG_PARSE_WARN
)
322 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
327 if (CONFIG_PARSE_WARN
)
328 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
334 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
337 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
340 flags
|= CONFIG_PARSE_REFUSE_BOM
;
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
);
351 if (!strextend(&continuation
, l
, NULL
)) {
352 if (flags
& CONFIG_PARSE_WARN
)
361 for (e
= p
; *e
; e
++) {
372 continuation
= strdup(l
);
374 if (flags
& CONFIG_PARSE_WARN
)
396 if (flags
& CONFIG_PARSE_WARN
)
397 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
402 continuation
= mfree(continuation
);
419 if (flags
& CONFIG_PARSE_WARN
)
420 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
429 static int config_parse_many_files(
430 const char *conf_file
,
432 const char *sections
,
433 ConfigItemLookup lookup
,
435 ConfigParseFlags flags
,
442 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
447 STRV_FOREACH(fn
, files
) {
448 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
456 /* Parse each config file in the directories specified as nulstr. */
457 int config_parse_many_nulstr(
458 const char *conf_file
,
459 const char *conf_file_dirs
,
460 const char *sections
,
461 ConfigItemLookup lookup
,
463 ConfigParseFlags flags
,
466 _cleanup_strv_free_
char **files
= NULL
;
469 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
473 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
476 /* Parse each config file in the directories specified as strv. */
477 int config_parse_many(
478 const char *conf_file
,
479 const char* const* conf_file_dirs
,
480 const char *dropin_dirname
,
481 const char *sections
,
482 ConfigItemLookup lookup
,
484 ConfigParseFlags flags
,
487 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
488 _cleanup_strv_free_
char **files
= NULL
;
492 suffix
= strjoina("/", dropin_dirname
);
493 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
497 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
501 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
504 #define DEFINE_PARSER(type, vartype, conv_func) \
505 int config_parse_##type( \
507 const char *filename, \
509 const char *section, \
510 unsigned section_line, \
511 const char *lvalue, \
513 const char *rvalue, \
525 r = conv_func(rvalue, i); \
527 log_syntax(unit, LOG_ERR, filename, line, r, \
528 "Failed to parse %s value, ignoring: %s", \
534 DEFINE_PARSER(int, int, safe_atoi
);
535 DEFINE_PARSER(long, long, safe_atoli
);
536 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
537 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
538 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
539 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
540 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
541 DEFINE_PARSER(double, double, safe_atod
);
542 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
543 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
544 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
546 int config_parse_iec_size(const char* unit
,
547 const char *filename
,
550 unsigned section_line
,
566 r
= parse_size(rvalue
, 1024, &v
);
567 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
568 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
576 int config_parse_si_size(
578 const char *filename
,
581 unsigned section_line
,
597 r
= parse_size(rvalue
, 1000, &v
);
598 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
599 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
607 int config_parse_iec_uint64(
609 const char *filename
,
612 unsigned section_line
,
619 uint64_t *bytes
= data
;
627 r
= parse_size(rvalue
, 1024, bytes
);
629 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
634 int config_parse_bool(const char* unit
,
635 const char *filename
,
638 unsigned section_line
,
654 k
= parse_boolean(rvalue
);
656 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
657 "Failed to parse boolean value%s: %s",
658 fatal
? "" : ", ignoring", rvalue
);
659 return fatal
? -ENOEXEC
: 0;
666 int config_parse_tristate(
668 const char *filename
,
671 unsigned section_line
,
685 /* A tristate is pretty much a boolean, except that it can
686 * also take the special value -1, indicating "uninitialized",
687 * much like NULL is for a pointer type. */
689 k
= parse_boolean(rvalue
);
691 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
699 int config_parse_string(
701 const char *filename
,
704 unsigned section_line
,
718 if (!utf8_is_valid(rvalue
)) {
719 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
723 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
729 int config_parse_path(
731 const char *filename
,
734 unsigned section_line
,
749 if (isempty(rvalue
)) {
754 if (!utf8_is_valid(rvalue
)) {
755 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
756 return fatal
? -ENOEXEC
: 0;
759 if (!path_is_absolute(rvalue
)) {
760 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
761 "Not an absolute path%s: %s",
762 fatal
? "" : ", ignoring", rvalue
);
763 return fatal
? -ENOEXEC
: 0;
770 path_kill_slashes(n
);
779 int config_parse_strv(
781 const char *filename
,
784 unsigned section_line
,
799 if (isempty(rvalue
)) {
800 *sv
= strv_free(*sv
);
807 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
813 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
817 if (!utf8_is_valid(word
)) {
818 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, word
);
823 r
= strv_consume(sv
, word
);
831 int config_parse_warn_compat(
833 const char *filename
,
836 unsigned section_line
,
842 Disabled reason
= ltype
;
845 case DISABLED_CONFIGURATION
:
846 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
847 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
849 case DISABLED_LEGACY
:
850 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
851 "Support for option %s= has been removed and it is ignored", lvalue
);
853 case DISABLED_EXPERIMENTAL
:
854 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
855 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
862 int config_parse_log_facility(
864 const char *filename
,
867 unsigned section_line
,
881 x
= log_facility_unshifted_from_string(rvalue
);
883 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
887 *o
= (x
<< 3) | LOG_PRI(*o
);
892 int config_parse_log_level(
894 const char *filename
,
897 unsigned section_line
,
911 x
= log_level_from_string(rvalue
);
913 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
917 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
920 *o
= (*o
& LOG_FACMASK
) | x
;
925 int config_parse_signal(
927 const char *filename
,
930 unsigned section_line
,
944 r
= signal_from_string(rvalue
);
946 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
954 int config_parse_personality(
956 const char *filename
,
959 unsigned section_line
,
966 unsigned long *personality
= data
, p
;
974 p
= PERSONALITY_INVALID
;
976 p
= personality_from_string(rvalue
);
977 if (p
== PERSONALITY_INVALID
) {
978 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
987 int config_parse_ifname(
989 const char *filename
,
992 unsigned section_line
,
1007 if (isempty(rvalue
)) {
1012 if (!ifname_valid(rvalue
)) {
1013 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
1017 r
= free_and_strdup(s
, rvalue
);
1024 int config_parse_ip_port(
1026 const char *filename
,
1028 const char *section
,
1029 unsigned section_line
,
1045 if (isempty(rvalue
)) {
1050 r
= parse_ip_port(rvalue
, &port
);
1052 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1061 int config_parse_join_controllers(
1063 const char *filename
,
1065 const char *section
,
1066 unsigned section_line
,
1073 char ****ret
= data
;
1074 const char *whole_rvalue
= rvalue
;
1076 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1084 _cleanup_free_
char *word
= NULL
;
1088 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1090 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1096 l
= strv_split(word
, ",");
1101 if (strv_length(l
) <= 1) {
1107 controllers
= new(char**, 2);
1114 controllers
[1] = NULL
;
1121 t
= new0(char**, n
+2);
1129 for (a
= controllers
; *a
; a
++)
1130 if (strv_overlap(*a
, l
)) {
1131 if (strv_extend_strv(&l
, *a
, false) < 0) {
1150 t
[n
++] = strv_uniq(l
);
1152 strv_free_free(controllers
);
1156 if (!isempty(rvalue
))
1157 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1159 /* As a special case, return a single empty strv, to override the default */
1161 controllers
= new(char**, 2);
1164 controllers
[0] = strv_new(NULL
, NULL
);
1165 if (!controllers
[0])
1167 controllers
[1] = NULL
;
1170 strv_free_free(*ret
);
1171 *ret
= TAKE_PTR(controllers
);
1176 int config_parse_mtu(
1178 const char *filename
,
1180 const char *section
,
1181 unsigned section_line
,
1188 uint32_t *mtu
= data
;
1194 r
= parse_mtu(ltype
, rvalue
, mtu
);
1196 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1197 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1198 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1203 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1204 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1211 int config_parse_rlimit(
1213 const char *filename
,
1215 const char *section
,
1216 unsigned section_line
,
1223 struct rlimit
**rl
= data
, d
= {};
1229 r
= rlimit_parse(ltype
, rvalue
, &d
);
1231 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1235 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1242 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);