1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "conf-files.h"
13 #include "conf-parser.h"
15 #include "extract-word.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"
28 #include "syslog-util.h"
29 #include "time-util.h"
31 #include "rlimit-util.h"
33 int config_item_table_lookup(
37 ConfigParserCallback
*func
,
42 const ConfigTableItem
*t
;
50 for (t
= table
; t
->lvalue
; t
++) {
52 if (!streq(lvalue
, t
->lvalue
))
55 if (!streq_ptr(section
, t
->section
))
67 int config_item_perf_lookup(
71 ConfigParserCallback
*func
,
76 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
77 const ConfigPerfItem
*p
;
86 p
= lookup(lvalue
, strlen(lvalue
));
90 key
= strjoin(section
, ".", lvalue
);
94 p
= lookup(key
, strlen(key
));
103 *data
= (uint8_t*) userdata
+ p
->offset
;
107 /* Run the user supplied parser for an assignment */
108 static int next_assignment(
110 const char *filename
,
112 ConfigItemLookup lookup
,
115 unsigned section_line
,
118 ConfigParseFlags flags
,
121 ConfigParserCallback func
= NULL
;
132 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
138 return func(unit
, filename
, line
, section
, section_line
,
139 lvalue
, ltype
, rvalue
, data
, userdata
);
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
);
151 /* Parse a single logical line */
152 static int parse_line(
154 const char *filename
,
156 const char *sections
,
157 ConfigItemLookup lookup
,
159 ConfigParseFlags flags
,
161 unsigned *section_line
,
162 bool *section_ignored
,
180 include
= first_word(l
, ".include");
182 _cleanup_free_
char *fn
= NULL
;
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
191 * Support for them should be eventually removed. */
193 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
194 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
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.");
202 fn
= file_in_same_dir(filename
, strstrip(include
));
206 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
209 if (!utf8_is_valid(l
))
210 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
220 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
224 n
= strndup(l
+1, k
-2);
228 if (sections
&& !nulstr_contains(sections
, n
)) {
230 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
231 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
234 *section
= mfree(*section
);
236 *section_ignored
= true;
238 free_and_replace(*section
, n
);
239 *section_line
= line
;
240 *section_ignored
= false;
246 if (sections
&& !*section
) {
248 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
249 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
256 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
263 return next_assignment(unit
,
276 /* Go through the file and parse each line */
277 int config_parse(const char *unit
,
278 const char *filename
,
280 const char *sections
,
281 ConfigItemLookup lookup
,
283 ConfigParseFlags flags
,
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;
296 f
= ours
= fopen(filename
, "re");
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
;
307 fd_warn_permissions(filename
, fileno(f
));
310 _cleanup_free_
char *buf
= NULL
;
311 bool escaped
= false;
314 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
318 if (flags
& CONFIG_PARSE_WARN
)
319 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
324 if (CONFIG_PARSE_WARN
)
325 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
330 if (strchr(COMMENTS
, *buf
))
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
);
401 continuation
= mfree(continuation
);
418 if (flags
& CONFIG_PARSE_WARN
)
419 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
427 static int config_parse_many_files(
428 const char *conf_file
,
430 const char *sections
,
431 ConfigItemLookup lookup
,
433 ConfigParseFlags flags
,
440 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
445 STRV_FOREACH(fn
, files
) {
446 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
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
,
461 ConfigParseFlags flags
,
464 _cleanup_strv_free_
char **files
= NULL
;
467 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
471 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
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
,
482 ConfigParseFlags flags
,
485 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
486 _cleanup_strv_free_
char **files
= NULL
;
490 suffix
= strjoina("/", dropin_dirname
);
491 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
495 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
499 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
502 #define DEFINE_PARSER(type, vartype, conv_func) \
503 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
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
);
517 int config_parse_iec_size(const char* unit
,
518 const char *filename
,
521 unsigned section_line
,
537 r
= parse_size(rvalue
, 1024, &v
);
538 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
541 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
549 int config_parse_si_size(
551 const char *filename
,
554 unsigned section_line
,
570 r
= parse_size(rvalue
, 1000, &v
);
571 if (r
>= 0 && (uint64_t) (size_t) v
!= v
)
574 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value '%s', ignoring: %m", rvalue
);
582 int config_parse_iec_uint64(
584 const char *filename
,
587 unsigned section_line
,
594 uint64_t *bytes
= data
;
602 r
= parse_size(rvalue
, 1024, bytes
);
604 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
609 int config_parse_bool(const char* unit
,
610 const char *filename
,
613 unsigned section_line
,
629 k
= parse_boolean(rvalue
);
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;
641 int config_parse_tristate(
643 const char *filename
,
646 unsigned section_line
,
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. */
664 k
= parse_boolean(rvalue
);
666 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
674 int config_parse_string(
676 const char *filename
,
679 unsigned section_line
,
693 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
699 int config_parse_path(
701 const char *filename
,
704 unsigned section_line
,
711 _cleanup_free_
char *n
= NULL
;
728 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
730 return fatal
? -ENOEXEC
: 0;
733 return free_and_replace(*s
, n
);
736 int config_parse_strv(
738 const char *filename
,
741 unsigned section_line
,
756 if (isempty(rvalue
)) {
757 *sv
= strv_free(*sv
);
764 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
770 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
774 r
= strv_consume(sv
, word
);
782 int config_parse_warn_compat(
784 const char *filename
,
787 unsigned section_line
,
794 Disabled reason
= ltype
;
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
);
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
);
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
);
817 int config_parse_log_facility(
819 const char *filename
,
822 unsigned section_line
,
836 x
= log_facility_unshifted_from_string(rvalue
);
838 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
842 *o
= (x
<< 3) | LOG_PRI(*o
);
847 int config_parse_log_level(
849 const char *filename
,
852 unsigned section_line
,
866 x
= log_level_from_string(rvalue
);
868 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
872 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
875 *o
= (*o
& LOG_FACMASK
) | x
;
880 int config_parse_signal(
882 const char *filename
,
885 unsigned section_line
,
899 r
= signal_from_string(rvalue
);
901 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
909 int config_parse_personality(
911 const char *filename
,
914 unsigned section_line
,
921 unsigned long *personality
= data
, p
;
929 p
= PERSONALITY_INVALID
;
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
);
942 int config_parse_ifname(
944 const char *filename
,
947 unsigned section_line
,
962 if (isempty(rvalue
)) {
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
);
972 r
= free_and_strdup(s
, rvalue
);
979 int config_parse_ip_port(
981 const char *filename
,
984 unsigned section_line
,
1000 if (isempty(rvalue
)) {
1005 r
= parse_ip_port(rvalue
, &port
);
1007 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1016 int config_parse_join_controllers(
1018 const char *filename
,
1020 const char *section
,
1021 unsigned section_line
,
1028 char ****ret
= data
;
1029 const char *whole_rvalue
= rvalue
;
1031 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1039 _cleanup_free_
char *word
= NULL
;
1043 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1045 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1051 l
= strv_split(word
, ",");
1056 if (strv_length(l
) <= 1) {
1062 controllers
= new(char**, 2);
1069 controllers
[1] = NULL
;
1076 t
= new0(char**, n
+2);
1084 for (a
= controllers
; *a
; a
++)
1085 if (strv_overlap(*a
, l
)) {
1086 if (strv_extend_strv(&l
, *a
, false) < 0) {
1105 t
[n
++] = strv_uniq(l
);
1107 strv_free_free(controllers
);
1111 if (!isempty(rvalue
))
1112 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1114 /* As a special case, return a single empty strv, to override the default */
1116 controllers
= new(char**, 2);
1119 controllers
[0] = strv_new(NULL
);
1120 if (!controllers
[0])
1122 controllers
[1] = NULL
;
1125 strv_free_free(*ret
);
1126 *ret
= TAKE_PTR(controllers
);
1131 int config_parse_mtu(
1133 const char *filename
,
1135 const char *section
,
1136 unsigned section_line
,
1143 uint32_t *mtu
= data
;
1149 r
= parse_mtu(ltype
, rvalue
, mtu
);
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
,
1158 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1159 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1166 int config_parse_rlimit(
1168 const char *filename
,
1170 const char *section
,
1171 unsigned section_line
,
1178 struct rlimit
**rl
= data
, d
= {};
1184 r
= rlimit_parse(ltype
, rvalue
, &d
);
1186 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1190 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1197 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1205 int config_parse_permille(const char* unit
,
1206 const char *filename
,
1208 const char *section
,
1209 unsigned section_line
,
1216 unsigned *permille
= data
;
1224 r
= parse_permille(rvalue
);
1226 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1227 "Failed to parse permille value, ignoring: %s", rvalue
);
1231 *permille
= (unsigned) r
;