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"
37 /// Additional includes needed by elogind
40 //#include "rlimit-util.h"
41 //#include "rlimit-util.h"
42 //#include "rlimit-util.h"
43 //#include "rlimit-util.h"
44 //#include "rlimit-util.h"
45 //#include "rlimit-util.h"
46 //#include "rlimit-util.h"
47 //#include "rlimit-util.h"
49 int config_item_table_lookup(
53 ConfigParserCallback *func,
58 const ConfigTableItem *t;
66 for (t = table; t->lvalue; t++) {
68 if (!streq(lvalue, t->lvalue))
71 if (!streq_ptr(section, t->section))
83 int config_item_perf_lookup(
87 ConfigParserCallback *func,
92 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
93 const ConfigPerfItem *p;
102 p = lookup(lvalue, strlen(lvalue));
106 key = strjoin(section, ".", lvalue);
110 p = lookup(key, strlen(key));
119 *data = (uint8_t*) userdata + p->offset;
123 /* Run the user supplied parser for an assignment */
124 static int next_assignment(
126 const char *filename,
128 ConfigItemLookup lookup,
131 unsigned section_line,
134 ConfigParseFlags flags,
137 ConfigParserCallback func = NULL;
148 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
154 return func(unit, filename, line, section, section_line,
155 lvalue, ltype, rvalue, data, userdata);
160 /* Warn about unknown non-extension fields. */
161 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
162 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
167 /* Parse a single logical line */
168 static int parse_line(
170 const char *filename,
172 const char *sections,
173 ConfigItemLookup lookup,
175 ConfigParseFlags flags,
177 unsigned *section_line,
178 bool *section_ignored,
193 if (strchr(COMMENTS "\n", *l))
196 include = first_word(l, ".include");
198 _cleanup_free_ char *fn = NULL;
200 /* .includes are a bad idea, we only support them here
201 * for historical reasons. They create cyclic include
202 * problems and make it difficult to detect
203 * configuration file changes with an easy
204 * stat(). Better approaches, such as .d/ drop-in
207 * Support for them should be eventually removed. */
209 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
210 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
214 log_syntax(unit, LOG_WARNING, filename, line, 0,
215 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
216 "Please use drop-in files instead.");
218 fn = file_in_same_dir(filename, strstrip(include));
222 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
225 if (!utf8_is_valid(l))
226 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
236 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
240 n = strndup(l+1, k-2);
244 if (sections && !nulstr_contains(sections, n)) {
246 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
247 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
250 *section = mfree(*section);
252 *section_ignored = true;
254 free_and_replace(*section, n);
255 *section_line = line;
256 *section_ignored = false;
262 if (sections && !*section) {
264 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
265 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
272 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
279 return next_assignment(unit,
292 /* Go through the file and parse each line */
293 int config_parse(const char *unit,
294 const char *filename,
296 const char *sections,
297 ConfigItemLookup lookup,
299 ConfigParseFlags flags,
302 _cleanup_free_ char *section = NULL, *continuation = NULL;
303 _cleanup_fclose_ FILE *ours = NULL;
304 unsigned line = 0, section_line = 0;
305 bool section_ignored = false;
312 f = ours = fopen(filename, "re");
314 /* Only log on request, except for ENOENT,
315 * since we return 0 to the caller. */
316 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
317 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
318 "Failed to open configuration file '%s': %m", filename);
319 return errno == ENOENT ? 0 : -errno;
323 fd_warn_permissions(filename, fileno(f));
326 _cleanup_free_ char *buf = NULL;
327 bool escaped = false;
330 r = read_line(f, LONG_LINE_MAX, &buf);
334 if (flags & CONFIG_PARSE_WARN)
335 log_error_errno(r, "%s:%u: Line too long", filename, line);
340 if (CONFIG_PARSE_WARN)
341 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
347 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
350 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
353 flags |= CONFIG_PARSE_REFUSE_BOM;
358 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
359 if (flags & CONFIG_PARSE_WARN)
360 log_error("%s:%u: Continuation line too long", filename, line);
364 if (!strextend(&continuation, l, NULL)) {
365 if (flags & CONFIG_PARSE_WARN)
374 for (e = p; *e; e++) {
385 continuation = strdup(l);
387 if (flags & CONFIG_PARSE_WARN)
409 if (flags & CONFIG_PARSE_WARN)
410 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
414 continuation = mfree(continuation);
431 if (flags & CONFIG_PARSE_WARN)
432 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
440 static int config_parse_many_files(
441 const char *conf_file,
443 const char *sections,
444 ConfigItemLookup lookup,
446 ConfigParseFlags flags,
453 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
458 STRV_FOREACH(fn, files) {
459 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
467 /* Parse each config file in the directories specified as nulstr. */
468 int config_parse_many_nulstr(
469 const char *conf_file,
470 const char *conf_file_dirs,
471 const char *sections,
472 ConfigItemLookup lookup,
474 ConfigParseFlags flags,
477 _cleanup_strv_free_ char **files = NULL;
480 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
484 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
487 #if 0 /// UNNEEDED by elogind
488 /* Parse each config file in the directories specified as strv. */
489 int config_parse_many(
490 const char *conf_file,
491 const char* const* conf_file_dirs,
492 const char *dropin_dirname,
493 const char *sections,
494 ConfigItemLookup lookup,
496 ConfigParseFlags flags,
499 _cleanup_strv_free_ char **dropin_dirs = NULL;
500 _cleanup_strv_free_ char **files = NULL;
504 suffix = strjoina("/", dropin_dirname);
505 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
509 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
513 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
517 #define DEFINE_PARSER(type, vartype, conv_func) \
518 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
520 DEFINE_PARSER(int, int, safe_atoi);
521 DEFINE_PARSER(long, long, safe_atoli);
522 #if 0 /// UNNEEDED by elogind
523 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
524 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
525 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
527 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
528 DEFINE_PARSER(unsigned, unsigned, safe_atou);
529 DEFINE_PARSER(double, double, safe_atod);
530 #if 0 /// UNNEEDED by elogind
531 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
533 DEFINE_PARSER(sec, usec_t, parse_sec);
534 DEFINE_PARSER(mode, mode_t, parse_mode);
536 int config_parse_iec_size(const char* unit,
537 const char *filename,
540 unsigned section_line,
556 r = parse_size(rvalue, 1024, &v);
557 if (r < 0 || (uint64_t) (size_t) v != v) {
558 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
566 #if 0 /// UNNEEDED by elogind
567 int config_parse_si_size(
569 const char *filename,
572 unsigned section_line,
588 r = parse_size(rvalue, 1000, &v);
589 if (r < 0 || (uint64_t) (size_t) v != v) {
590 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
598 int config_parse_iec_uint64(
600 const char *filename,
603 unsigned section_line,
610 uint64_t *bytes = data;
618 r = parse_size(rvalue, 1024, bytes);
620 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
626 int config_parse_bool(const char* unit,
627 const char *filename,
630 unsigned section_line,
646 k = parse_boolean(rvalue);
648 log_syntax(unit, LOG_ERR, filename, line, k,
649 "Failed to parse boolean value%s: %s",
650 fatal ? "" : ", ignoring", rvalue);
651 return fatal ? -ENOEXEC : 0;
658 #if 0 /// UNNEEDED by elogind
659 int config_parse_tristate(
661 const char *filename,
664 unsigned section_line,
678 /* A tristate is pretty much a boolean, except that it can
679 * also take the special value -1, indicating "uninitialized",
680 * much like NULL is for a pointer type. */
682 k = parse_boolean(rvalue);
684 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
693 int config_parse_string(
695 const char *filename,
698 unsigned section_line,
712 if (!utf8_is_valid(rvalue)) {
713 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
717 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
723 int config_parse_path(
725 const char *filename,
728 unsigned section_line,
744 if (isempty(rvalue)) {
753 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
755 return fatal ? -ENOEXEC : 0;
758 free_and_replace(*s, n);
763 int config_parse_strv(
765 const char *filename,
768 unsigned section_line,
783 if (isempty(rvalue)) {
784 *sv = strv_free(*sv);
791 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
797 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
801 if (!utf8_is_valid(word)) {
802 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
807 r = strv_consume(sv, word);
815 int config_parse_warn_compat(
817 const char *filename,
820 unsigned section_line,
827 Disabled reason = ltype;
831 case DISABLED_CONFIGURATION:
832 log_syntax(unit, LOG_DEBUG, filename, line, 0,
833 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
836 case DISABLED_LEGACY:
837 log_syntax(unit, LOG_INFO, filename, line, 0,
838 "Support for option %s= has been removed and it is ignored", lvalue);
841 case DISABLED_EXPERIMENTAL:
842 log_syntax(unit, LOG_INFO, filename, line, 0,
843 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
850 #if 0 /// UNNEEDED by elogind
851 int config_parse_log_facility(
853 const char *filename,
856 unsigned section_line,
870 x = log_facility_unshifted_from_string(rvalue);
872 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
876 *o = (x << 3) | LOG_PRI(*o);
882 int config_parse_log_level(
884 const char *filename,
887 unsigned section_line,
901 x = log_level_from_string(rvalue);
903 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
907 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
910 *o = (*o & LOG_FACMASK) | x;
915 int config_parse_signal(
917 const char *filename,
920 unsigned section_line,
934 r = signal_from_string(rvalue);
936 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
944 #if 0 /// UNNEEDED by elogind
945 int config_parse_personality(
947 const char *filename,
950 unsigned section_line,
957 unsigned long *personality = data, p;
965 p = PERSONALITY_INVALID;
967 p = personality_from_string(rvalue);
968 if (p == PERSONALITY_INVALID) {
969 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
978 int config_parse_ifname(
980 const char *filename,
983 unsigned section_line,
998 if (isempty(rvalue)) {
1003 if (!ifname_valid(rvalue)) {
1004 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1008 r = free_and_strdup(s, rvalue);
1015 int config_parse_ip_port(
1017 const char *filename,
1019 const char *section,
1020 unsigned section_line,
1036 if (isempty(rvalue)) {
1041 r = parse_ip_port(rvalue, &port);
1043 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1052 int config_parse_join_controllers(
1054 const char *filename,
1056 const char *section,
1057 unsigned section_line,
1064 char ****ret = data;
1065 const char *whole_rvalue = rvalue;
1067 _cleanup_(strv_free_freep) char ***controllers = NULL;
1075 _cleanup_free_ char *word = NULL;
1079 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1081 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1087 l = strv_split(word, ",");
1092 if (strv_length(l) <= 1) {
1098 controllers = new(char**, 2);
1105 controllers[1] = NULL;
1112 t = new0(char**, n+2);
1120 for (a = controllers; *a; a++)
1121 if (strv_overlap(*a, l)) {
1122 if (strv_extend_strv(&l, *a, false) < 0) {
1141 t[n++] = strv_uniq(l);
1143 strv_free_free(controllers);
1147 if (!isempty(rvalue))
1148 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1150 /* As a special case, return a single empty strv, to override the default */
1152 controllers = new(char**, 2);
1155 controllers[0] = strv_new(NULL, NULL);
1156 if (!controllers[0])
1158 controllers[1] = NULL;
1161 strv_free_free(*ret);
1162 *ret = TAKE_PTR(controllers);
1168 int config_parse_mtu(
1170 const char *filename,
1172 const char *section,
1173 unsigned section_line,
1180 uint32_t *mtu = data;
1186 r = parse_mtu(ltype, rvalue, mtu);
1188 log_syntax(unit, LOG_ERR, filename, line, r,
1189 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1190 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1195 log_syntax(unit, LOG_ERR, filename, line, r,
1196 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1203 int config_parse_rlimit(
1205 const char *filename,
1207 const char *section,
1208 unsigned section_line,
1215 struct rlimit **rl = data, d = {};
1221 r = rlimit_parse(ltype, rvalue, &d);
1223 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1227 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1234 rl[ltype] = newdup(struct rlimit, &d, 1);