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 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
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
);
519 int config_parse_iec_size(const char* unit
,
520 const char *filename
,
523 unsigned section_line
,
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
);
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
) {
572 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
580 int config_parse_iec_uint64(
582 const char *filename
,
585 unsigned section_line
,
592 uint64_t *bytes
= data
;
600 r
= parse_size(rvalue
, 1024, bytes
);
602 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
607 int config_parse_bool(const char* unit
,
608 const char *filename
,
611 unsigned section_line
,
627 k
= parse_boolean(rvalue
);
629 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
630 "Failed to parse boolean value%s: %s",
631 fatal
? "" : ", ignoring", rvalue
);
632 return fatal
? -ENOEXEC
: 0;
639 int config_parse_tristate(
641 const char *filename
,
644 unsigned section_line
,
658 /* A tristate is pretty much a boolean, except that it can
659 * also take the special value -1, indicating "uninitialized",
660 * much like NULL is for a pointer type. */
662 k
= parse_boolean(rvalue
);
664 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
672 int config_parse_string(
674 const char *filename
,
677 unsigned section_line
,
691 if (!utf8_is_valid(rvalue
)) {
692 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, rvalue
);
696 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
702 int config_parse_path(
704 const char *filename
,
707 unsigned section_line
,
723 if (isempty(rvalue
)) {
732 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
734 return fatal
? -ENOEXEC
: 0;
737 free_and_replace(*s
, n
);
742 int config_parse_strv(
744 const char *filename
,
747 unsigned section_line
,
762 if (isempty(rvalue
)) {
763 *sv
= strv_free(*sv
);
770 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
776 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
780 if (!utf8_is_valid(word
)) {
781 log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, word
);
786 r
= strv_consume(sv
, word
);
794 int config_parse_warn_compat(
796 const char *filename
,
799 unsigned section_line
,
806 Disabled reason
= ltype
;
810 case DISABLED_CONFIGURATION
:
811 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
812 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
815 case DISABLED_LEGACY
:
816 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
817 "Support for option %s= has been removed and it is ignored", lvalue
);
820 case DISABLED_EXPERIMENTAL
:
821 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
822 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
829 int config_parse_log_facility(
831 const char *filename
,
834 unsigned section_line
,
848 x
= log_facility_unshifted_from_string(rvalue
);
850 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
854 *o
= (x
<< 3) | LOG_PRI(*o
);
859 int config_parse_log_level(
861 const char *filename
,
864 unsigned section_line
,
878 x
= log_level_from_string(rvalue
);
880 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
884 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
887 *o
= (*o
& LOG_FACMASK
) | x
;
892 int config_parse_signal(
894 const char *filename
,
897 unsigned section_line
,
911 r
= signal_from_string(rvalue
);
913 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
921 int config_parse_personality(
923 const char *filename
,
926 unsigned section_line
,
933 unsigned long *personality
= data
, p
;
941 p
= PERSONALITY_INVALID
;
943 p
= personality_from_string(rvalue
);
944 if (p
== PERSONALITY_INVALID
) {
945 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
954 int config_parse_ifname(
956 const char *filename
,
959 unsigned section_line
,
974 if (isempty(rvalue
)) {
979 if (!ifname_valid(rvalue
)) {
980 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
984 r
= free_and_strdup(s
, rvalue
);
991 int config_parse_ip_port(
993 const char *filename
,
996 unsigned section_line
,
1012 if (isempty(rvalue
)) {
1017 r
= parse_ip_port(rvalue
, &port
);
1019 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1028 int config_parse_join_controllers(
1030 const char *filename
,
1032 const char *section
,
1033 unsigned section_line
,
1040 char ****ret
= data
;
1041 const char *whole_rvalue
= rvalue
;
1043 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1051 _cleanup_free_
char *word
= NULL
;
1055 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1057 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1063 l
= strv_split(word
, ",");
1068 if (strv_length(l
) <= 1) {
1074 controllers
= new(char**, 2);
1081 controllers
[1] = NULL
;
1088 t
= new0(char**, n
+2);
1096 for (a
= controllers
; *a
; a
++)
1097 if (strv_overlap(*a
, l
)) {
1098 if (strv_extend_strv(&l
, *a
, false) < 0) {
1117 t
[n
++] = strv_uniq(l
);
1119 strv_free_free(controllers
);
1123 if (!isempty(rvalue
))
1124 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1126 /* As a special case, return a single empty strv, to override the default */
1128 controllers
= new(char**, 2);
1131 controllers
[0] = strv_new(NULL
, NULL
);
1132 if (!controllers
[0])
1134 controllers
[1] = NULL
;
1137 strv_free_free(*ret
);
1138 *ret
= TAKE_PTR(controllers
);
1143 int config_parse_mtu(
1145 const char *filename
,
1147 const char *section
,
1148 unsigned section_line
,
1155 uint32_t *mtu
= data
;
1161 r
= parse_mtu(ltype
, rvalue
, mtu
);
1163 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1164 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1165 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1170 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1171 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1178 int config_parse_rlimit(
1180 const char *filename
,
1182 const char *section
,
1183 unsigned section_line
,
1190 struct rlimit
**rl
= data
, d
= {};
1196 r
= rlimit_parse(ltype
, rvalue
, &d
);
1198 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1202 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1209 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);