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
);
214 if (!utf8_is_valid(l
))
215 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
225 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
229 n
= strndup(l
+1, k
-2);
233 if (sections
&& !nulstr_contains(sections
, n
)) {
235 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
236 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
239 *section
= mfree(*section
);
241 *section_ignored
= true;
245 *section_line
= line
;
246 *section_ignored
= false;
252 if (sections
&& !*section
) {
254 if (!(flags
& CONFIG_PARSE_RELAXED
) && !*section_ignored
)
255 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Assignment outside of section. Ignoring.");
262 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Missing '='.");
269 return next_assignment(unit
,
282 /* Go through the file and parse each line */
283 int config_parse(const char *unit
,
284 const char *filename
,
286 const char *sections
,
287 ConfigItemLookup lookup
,
289 ConfigParseFlags flags
,
292 _cleanup_free_
char *section
= NULL
, *continuation
= NULL
;
293 _cleanup_fclose_
FILE *ours
= NULL
;
294 unsigned line
= 0, section_line
= 0;
295 bool section_ignored
= false;
302 f
= ours
= fopen(filename
, "re");
304 /* Only log on request, except for ENOENT,
305 * since we return 0 to the caller. */
306 if ((flags
& CONFIG_PARSE_WARN
) || errno
== ENOENT
)
307 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
,
308 "Failed to open configuration file '%s': %m", filename
);
309 return errno
== ENOENT
? 0 : -errno
;
313 fd_warn_permissions(filename
, fileno(f
));
316 _cleanup_free_
char *buf
= NULL
;
317 bool escaped
= false;
320 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
324 if (flags
& CONFIG_PARSE_WARN
)
325 log_error_errno(r
, "%s:%u: Line too long", filename
, line
);
330 if (CONFIG_PARSE_WARN
)
331 log_error_errno(r
, "%s:%u: Error while reading configuration file: %m", filename
, line
);
337 if (!(flags
& CONFIG_PARSE_REFUSE_BOM
)) {
340 q
= startswith(buf
, UTF8_BYTE_ORDER_MARK
);
343 flags
|= CONFIG_PARSE_REFUSE_BOM
;
348 if (strlen(continuation
) + strlen(l
) > LONG_LINE_MAX
) {
349 if (flags
& CONFIG_PARSE_WARN
)
350 log_error("%s:%u: Continuation line too long", filename
, line
);
354 if (!strextend(&continuation
, l
, NULL
)) {
355 if (flags
& CONFIG_PARSE_WARN
)
364 for (e
= p
; *e
; e
++) {
375 continuation
= strdup(l
);
377 if (flags
& CONFIG_PARSE_WARN
)
399 if (flags
& CONFIG_PARSE_WARN
)
400 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
405 continuation
= mfree(continuation
);
422 if (flags
& CONFIG_PARSE_WARN
)
423 log_warning_errno(r
, "%s:%u: Failed to parse file: %m", filename
, line
);
432 static int config_parse_many_files(
433 const char *conf_file
,
435 const char *sections
,
436 ConfigItemLookup lookup
,
438 ConfigParseFlags flags
,
445 r
= config_parse(NULL
, conf_file
, NULL
, sections
, lookup
, table
, flags
, userdata
);
450 STRV_FOREACH(fn
, files
) {
451 r
= config_parse(NULL
, *fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
459 /* Parse each config file in the directories specified as nulstr. */
460 int config_parse_many_nulstr(
461 const char *conf_file
,
462 const char *conf_file_dirs
,
463 const char *sections
,
464 ConfigItemLookup lookup
,
466 ConfigParseFlags flags
,
469 _cleanup_strv_free_
char **files
= NULL
;
472 r
= conf_files_list_nulstr(&files
, ".conf", NULL
, 0, conf_file_dirs
);
476 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
479 /* Parse each config file in the directories specified as strv. */
480 int config_parse_many(
481 const char *conf_file
,
482 const char* const* conf_file_dirs
,
483 const char *dropin_dirname
,
484 const char *sections
,
485 ConfigItemLookup lookup
,
487 ConfigParseFlags flags
,
490 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
491 _cleanup_strv_free_
char **files
= NULL
;
495 suffix
= strjoina("/", dropin_dirname
);
496 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) conf_file_dirs
, suffix
);
500 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char* const*) dropin_dirs
);
504 return config_parse_many_files(conf_file
, files
, sections
, lookup
, table
, flags
, userdata
);
507 #define DEFINE_PARSER(type, vartype, conv_func) \
508 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
510 DEFINE_PARSER(int, int, safe_atoi
);
511 DEFINE_PARSER(long, long, safe_atoli
);
512 DEFINE_PARSER(uint8
, uint8_t, safe_atou8
);
513 DEFINE_PARSER(uint16
, uint16_t, safe_atou16
);
514 DEFINE_PARSER(uint32
, uint32_t, safe_atou32
);
515 DEFINE_PARSER(uint64
, uint64_t, safe_atou64
);
516 DEFINE_PARSER(unsigned, unsigned, safe_atou
);
517 DEFINE_PARSER(double, double, safe_atod
);
518 DEFINE_PARSER(nsec
, nsec_t
, parse_nsec
);
519 DEFINE_PARSER(sec
, usec_t
, parse_sec
);
520 DEFINE_PARSER(mode
, mode_t
, parse_mode
);
522 int config_parse_iec_size(const char* unit
,
523 const char *filename
,
526 unsigned section_line
,
542 r
= parse_size(rvalue
, 1024, &v
);
543 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
544 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
552 int config_parse_si_size(
554 const char *filename
,
557 unsigned section_line
,
573 r
= parse_size(rvalue
, 1000, &v
);
574 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
575 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
583 int config_parse_iec_uint64(
585 const char *filename
,
588 unsigned section_line
,
595 uint64_t *bytes
= data
;
603 r
= parse_size(rvalue
, 1024, bytes
);
605 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
610 int config_parse_bool(const char* unit
,
611 const char *filename
,
614 unsigned section_line
,
630 k
= parse_boolean(rvalue
);
632 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
633 "Failed to parse boolean value%s: %s",
634 fatal
? "" : ", ignoring", rvalue
);
635 return fatal
? -ENOEXEC
: 0;
642 int config_parse_tristate(
644 const char *filename
,
647 unsigned section_line
,
661 /* A tristate is pretty much a boolean, except that it can
662 * also take the special value -1, indicating "uninitialized",
663 * much like NULL is for a pointer type. */
665 k
= parse_boolean(rvalue
);
667 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
675 int config_parse_string(
677 const char *filename
,
680 unsigned section_line
,
694 if (!utf8_is_valid(rvalue
)) {
695 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
699 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
705 int config_parse_path(
707 const char *filename
,
710 unsigned section_line
,
726 if (isempty(rvalue
)) {
735 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
737 return fatal
? -ENOEXEC
: 0;
740 free_and_replace(*s
, n
);
745 int config_parse_strv(
747 const char *filename
,
750 unsigned section_line
,
765 if (isempty(rvalue
)) {
766 *sv
= strv_free(*sv
);
773 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
779 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
783 if (!utf8_is_valid(word
)) {
784 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, word
);
789 r
= strv_consume(sv
, word
);
797 int config_parse_warn_compat(
799 const char *filename
,
802 unsigned section_line
,
809 Disabled reason
= ltype
;
813 case DISABLED_CONFIGURATION
:
814 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
815 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
818 case DISABLED_LEGACY
:
819 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
820 "Support for option %s= has been removed and it is ignored", lvalue
);
823 case DISABLED_EXPERIMENTAL
:
824 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
825 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
832 int config_parse_log_facility(
834 const char *filename
,
837 unsigned section_line
,
851 x
= log_facility_unshifted_from_string(rvalue
);
853 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
857 *o
= (x
<< 3) | LOG_PRI(*o
);
862 int config_parse_log_level(
864 const char *filename
,
867 unsigned section_line
,
881 x
= log_level_from_string(rvalue
);
883 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
887 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
890 *o
= (*o
& LOG_FACMASK
) | x
;
895 int config_parse_signal(
897 const char *filename
,
900 unsigned section_line
,
914 r
= signal_from_string(rvalue
);
916 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
924 int config_parse_personality(
926 const char *filename
,
929 unsigned section_line
,
936 unsigned long *personality
= data
, p
;
944 p
= PERSONALITY_INVALID
;
946 p
= personality_from_string(rvalue
);
947 if (p
== PERSONALITY_INVALID
) {
948 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
957 int config_parse_ifname(
959 const char *filename
,
962 unsigned section_line
,
977 if (isempty(rvalue
)) {
982 if (!ifname_valid(rvalue
)) {
983 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
987 r
= free_and_strdup(s
, rvalue
);
994 int config_parse_ip_port(
996 const char *filename
,
999 unsigned section_line
,
1015 if (isempty(rvalue
)) {
1020 r
= parse_ip_port(rvalue
, &port
);
1022 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1031 int config_parse_join_controllers(
1033 const char *filename
,
1035 const char *section
,
1036 unsigned section_line
,
1043 char ****ret
= data
;
1044 const char *whole_rvalue
= rvalue
;
1046 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1054 _cleanup_free_
char *word
= NULL
;
1058 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1060 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1066 l
= strv_split(word
, ",");
1071 if (strv_length(l
) <= 1) {
1077 controllers
= new(char**, 2);
1084 controllers
[1] = NULL
;
1091 t
= new0(char**, n
+2);
1099 for (a
= controllers
; *a
; a
++)
1100 if (strv_overlap(*a
, l
)) {
1101 if (strv_extend_strv(&l
, *a
, false) < 0) {
1120 t
[n
++] = strv_uniq(l
);
1122 strv_free_free(controllers
);
1126 if (!isempty(rvalue
))
1127 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1129 /* As a special case, return a single empty strv, to override the default */
1131 controllers
= new(char**, 2);
1134 controllers
[0] = strv_new(NULL
, NULL
);
1135 if (!controllers
[0])
1137 controllers
[1] = NULL
;
1140 strv_free_free(*ret
);
1141 *ret
= TAKE_PTR(controllers
);
1146 int config_parse_mtu(
1148 const char *filename
,
1150 const char *section
,
1151 unsigned section_line
,
1158 uint32_t *mtu
= data
;
1164 r
= parse_mtu(ltype
, rvalue
, mtu
);
1166 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1167 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1168 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1173 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1174 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1181 int config_parse_rlimit(
1183 const char *filename
,
1185 const char *section
,
1186 unsigned section_line
,
1193 struct rlimit
**rl
= data
, d
= {};
1199 r
= rlimit_parse(ltype
, rvalue
, &d
);
1201 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1205 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1212 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);