1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
12 #include <sys/types.h>
14 #include "alloc-util.h"
15 #include "conf-files.h"
16 #include "conf-parser.h"
18 #include "extract-word.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "signal-util.h"
28 #include "socket-util.h"
29 #include "string-util.h"
31 #include "syslog-util.h"
32 #include "time-util.h"
34 #include "rlimit-util.h"
36 int config_item_table_lookup(
40 ConfigParserCallback
*func
,
45 const ConfigTableItem
*t
;
53 for (t
= table
; t
->lvalue
; t
++) {
55 if (!streq(lvalue
, t
->lvalue
))
58 if (!streq_ptr(section
, t
->section
))
70 int config_item_perf_lookup(
74 ConfigParserCallback
*func
,
79 ConfigPerfItemLookup lookup
= (ConfigPerfItemLookup
) table
;
80 const ConfigPerfItem
*p
;
89 p
= lookup(lvalue
, strlen(lvalue
));
93 key
= strjoin(section
, ".", lvalue
);
97 p
= lookup(key
, strlen(key
));
106 *data
= (uint8_t*) userdata
+ p
->offset
;
110 /* Run the user supplied parser for an assignment */
111 static int next_assignment(
113 const char *filename
,
115 ConfigItemLookup lookup
,
118 unsigned section_line
,
121 ConfigParseFlags flags
,
124 ConfigParserCallback func
= NULL
;
135 r
= lookup(table
, section
, lvalue
, &func
, <ype
, &data
, userdata
);
141 return func(unit
, filename
, line
, section
, section_line
,
142 lvalue
, ltype
, rvalue
, data
, userdata
);
147 /* Warn about unknown non-extension fields. */
148 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(lvalue
, "X-"))
149 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown lvalue '%s' in section '%s'", lvalue
, section
);
154 /* Parse a single logical line */
155 static int parse_line(
157 const char *filename
,
159 const char *sections
,
160 ConfigItemLookup lookup
,
162 ConfigParseFlags flags
,
164 unsigned *section_line
,
165 bool *section_ignored
,
180 if (strchr(COMMENTS
"\n", *l
))
183 include
= first_word(l
, ".include");
185 _cleanup_free_
char *fn
= NULL
;
187 /* .includes are a bad idea, we only support them here
188 * for historical reasons. They create cyclic include
189 * problems and make it difficult to detect
190 * configuration file changes with an easy
191 * stat(). Better approaches, such as .d/ drop-in
194 * Support for them should be eventually removed. */
196 if (!(flags
& CONFIG_PARSE_ALLOW_INCLUDE
)) {
197 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, ".include not allowed here. Ignoring.");
201 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
202 ".include directives are deprecated, and support for them will be removed in a future version of systemd. "
203 "Please use drop-in files instead.");
205 fn
= file_in_same_dir(filename
, strstrip(include
));
209 return config_parse(unit
, fn
, NULL
, sections
, lookup
, table
, flags
, userdata
);
212 if (!utf8_is_valid(l
))
213 return log_syntax_invalid_utf8(unit
, LOG_WARNING
, filename
, line
, l
);
223 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid section header '%s'", l
);
227 n
= strndup(l
+1, k
-2);
231 if (sections
&& !nulstr_contains(sections
, n
)) {
233 if (!(flags
& CONFIG_PARSE_RELAXED
) && !startswith(n
, "X-"))
234 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown section '%s'. Ignoring.", n
);
237 *section
= mfree(*section
);
239 *section_ignored
= true;
241 free_and_replace(*section
, n
);
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(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
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
);
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
) {
539 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
547 int config_parse_si_size(
549 const char *filename
,
552 unsigned section_line
,
568 r
= parse_size(rvalue
, 1000, &v
);
569 if (r
< 0 || (uint64_t) (size_t) v
!= v
) {
570 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
578 int config_parse_iec_uint64(
580 const char *filename
,
583 unsigned section_line
,
590 uint64_t *bytes
= data
;
598 r
= parse_size(rvalue
, 1024, bytes
);
600 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse size value, ignoring: %s", rvalue
);
605 int config_parse_bool(const char* unit
,
606 const char *filename
,
609 unsigned section_line
,
625 k
= parse_boolean(rvalue
);
627 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
628 "Failed to parse boolean value%s: %s",
629 fatal
? "" : ", ignoring", rvalue
);
630 return fatal
? -ENOEXEC
: 0;
637 int config_parse_tristate(
639 const char *filename
,
642 unsigned section_line
,
656 /* A tristate is pretty much a boolean, except that it can
657 * also take the special value -1, indicating "uninitialized",
658 * much like NULL is for a pointer type. */
660 k
= parse_boolean(rvalue
);
662 log_syntax(unit
, LOG_ERR
, filename
, line
, k
, "Failed to parse boolean value, ignoring: %s", rvalue
);
670 int config_parse_string(
672 const char *filename
,
675 unsigned section_line
,
689 if (free_and_strdup(s
, empty_to_null(rvalue
)) < 0)
695 int config_parse_path(
697 const char *filename
,
700 unsigned section_line
,
707 _cleanup_free_
char *n
= NULL
;
724 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
726 return fatal
? -ENOEXEC
: 0;
729 return free_and_replace(*s
, n
);
732 int config_parse_strv(
734 const char *filename
,
737 unsigned section_line
,
752 if (isempty(rvalue
)) {
753 *sv
= strv_free(*sv
);
760 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RETAIN_ESCAPE
);
766 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
770 r
= strv_consume(sv
, word
);
778 int config_parse_warn_compat(
780 const char *filename
,
783 unsigned section_line
,
790 Disabled reason
= ltype
;
794 case DISABLED_CONFIGURATION
:
795 log_syntax(unit
, LOG_DEBUG
, filename
, line
, 0,
796 "Support for option %s= has been disabled at compile time and it is ignored", lvalue
);
799 case DISABLED_LEGACY
:
800 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
801 "Support for option %s= has been removed and it is ignored", lvalue
);
804 case DISABLED_EXPERIMENTAL
:
805 log_syntax(unit
, LOG_INFO
, filename
, line
, 0,
806 "Support for option %s= has not yet been enabled and it is ignored", lvalue
);
813 int config_parse_log_facility(
815 const char *filename
,
818 unsigned section_line
,
832 x
= log_facility_unshifted_from_string(rvalue
);
834 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log facility, ignoring: %s", rvalue
);
838 *o
= (x
<< 3) | LOG_PRI(*o
);
843 int config_parse_log_level(
845 const char *filename
,
848 unsigned section_line
,
862 x
= log_level_from_string(rvalue
);
864 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse log level, ignoring: %s", rvalue
);
868 if (*o
< 0) /* if it wasn't initialized so far, assume zero facility */
871 *o
= (*o
& LOG_FACMASK
) | x
;
876 int config_parse_signal(
878 const char *filename
,
881 unsigned section_line
,
895 r
= signal_from_string(rvalue
);
897 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse signal name, ignoring: %s", rvalue
);
905 int config_parse_personality(
907 const char *filename
,
910 unsigned section_line
,
917 unsigned long *personality
= data
, p
;
925 p
= PERSONALITY_INVALID
;
927 p
= personality_from_string(rvalue
);
928 if (p
== PERSONALITY_INVALID
) {
929 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse personality, ignoring: %s", rvalue
);
938 int config_parse_ifname(
940 const char *filename
,
943 unsigned section_line
,
958 if (isempty(rvalue
)) {
963 if (!ifname_valid(rvalue
)) {
964 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
968 r
= free_and_strdup(s
, rvalue
);
975 int config_parse_ip_port(
977 const char *filename
,
980 unsigned section_line
,
996 if (isempty(rvalue
)) {
1001 r
= parse_ip_port(rvalue
, &port
);
1003 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse port '%s'.", rvalue
);
1012 int config_parse_join_controllers(
1014 const char *filename
,
1016 const char *section
,
1017 unsigned section_line
,
1024 char ****ret
= data
;
1025 const char *whole_rvalue
= rvalue
;
1027 _cleanup_(strv_free_freep
) char ***controllers
= NULL
;
1035 _cleanup_free_
char *word
= NULL
;
1039 r
= extract_first_word(&rvalue
, &word
, NULL
, EXTRACT_QUOTES
);
1041 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
1047 l
= strv_split(word
, ",");
1052 if (strv_length(l
) <= 1) {
1058 controllers
= new(char**, 2);
1065 controllers
[1] = NULL
;
1072 t
= new0(char**, n
+2);
1080 for (a
= controllers
; *a
; a
++)
1081 if (strv_overlap(*a
, l
)) {
1082 if (strv_extend_strv(&l
, *a
, false) < 0) {
1101 t
[n
++] = strv_uniq(l
);
1103 strv_free_free(controllers
);
1107 if (!isempty(rvalue
))
1108 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Trailing garbage, ignoring.");
1110 /* As a special case, return a single empty strv, to override the default */
1112 controllers
= new(char**, 2);
1115 controllers
[0] = strv_new(NULL
, NULL
);
1116 if (!controllers
[0])
1118 controllers
[1] = NULL
;
1121 strv_free_free(*ret
);
1122 *ret
= TAKE_PTR(controllers
);
1127 int config_parse_mtu(
1129 const char *filename
,
1131 const char *section
,
1132 unsigned section_line
,
1139 uint32_t *mtu
= data
;
1145 r
= parse_mtu(ltype
, rvalue
, mtu
);
1147 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1148 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32
"…%" PRIu32
", ignoring: %s",
1149 (uint32_t) (ltype
== AF_INET6
? IPV6_MIN_MTU
: IPV4_MIN_MTU
), (uint32_t) UINT32_MAX
,
1154 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1155 "Failed to parse MTU value '%s', ignoring: %m", rvalue
);
1162 int config_parse_rlimit(
1164 const char *filename
,
1166 const char *section
,
1167 unsigned section_line
,
1174 struct rlimit
**rl
= data
, d
= {};
1180 r
= rlimit_parse(ltype
, rvalue
, &d
);
1182 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue
);
1186 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse resource value, ignoring: %s", rvalue
);
1193 rl
[ltype
] = newdup(struct rlimit
, &d
, 1);
1201 int config_parse_permille(const char* unit
,
1202 const char *filename
,
1204 const char *section
,
1205 unsigned section_line
,
1212 unsigned *permille
= data
;
1220 r
= parse_permille(rvalue
);
1222 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1223 "Failed to parse permille value, ignoring: %s", rvalue
);
1227 *permille
= (unsigned) r
;