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"
32 /// Additional includes needed by elogind
35 //#include "rlimit-util.h"
36 //#include "rlimit-util.h"
37 //#include "rlimit-util.h"
38 //#include "rlimit-util.h"
39 //#include "rlimit-util.h"
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"
48 //#include "rlimit-util.h"
49 //#include "rlimit-util.h"
50 //#include "rlimit-util.h"
52 int config_item_table_lookup(
56 ConfigParserCallback *func,
61 const ConfigTableItem *t;
69 for (t = table; t->lvalue; t++) {
71 if (!streq(lvalue, t->lvalue))
74 if (!streq_ptr(section, t->section))
86 int config_item_perf_lookup(
90 ConfigParserCallback *func,
95 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
96 const ConfigPerfItem *p;
105 p = lookup(lvalue, strlen(lvalue));
109 key = strjoin(section, ".", lvalue);
113 p = lookup(key, strlen(key));
122 *data = (uint8_t*) userdata + p->offset;
126 /* Run the user supplied parser for an assignment */
127 static int next_assignment(
129 const char *filename,
131 ConfigItemLookup lookup,
134 unsigned section_line,
137 ConfigParseFlags flags,
140 ConfigParserCallback func = NULL;
151 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
157 return func(unit, filename, line, section, section_line,
158 lvalue, ltype, rvalue, data, userdata);
163 /* Warn about unknown non-extension fields. */
164 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
165 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
170 /* Parse a single logical line */
171 static int parse_line(
173 const char *filename,
175 const char *sections,
176 ConfigItemLookup lookup,
178 ConfigParseFlags flags,
180 unsigned *section_line,
181 bool *section_ignored,
196 if (strchr(COMMENTS "\n", *l))
199 include = first_word(l, ".include");
201 _cleanup_free_ char *fn = NULL;
203 /* .includes are a bad idea, we only support them here
204 * for historical reasons. They create cyclic include
205 * problems and make it difficult to detect
206 * configuration file changes with an easy
207 * stat(). Better approaches, such as .d/ drop-in
210 * Support for them should be eventually removed. */
212 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
213 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
217 log_syntax(unit, LOG_WARNING, filename, line, 0,
218 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
219 "Please use drop-in files instead.");
221 fn = file_in_same_dir(filename, strstrip(include));
225 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
228 if (!utf8_is_valid(l))
229 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
239 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
243 n = strndup(l+1, k-2);
247 if (sections && !nulstr_contains(sections, n)) {
249 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
250 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
253 *section = mfree(*section);
255 *section_ignored = true;
257 free_and_replace(*section, n);
258 *section_line = line;
259 *section_ignored = false;
265 if (sections && !*section) {
267 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
268 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
275 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
282 return next_assignment(unit,
295 /* Go through the file and parse each line */
296 int config_parse(const char *unit,
297 const char *filename,
299 const char *sections,
300 ConfigItemLookup lookup,
302 ConfigParseFlags flags,
305 _cleanup_free_ char *section = NULL, *continuation = NULL;
306 _cleanup_fclose_ FILE *ours = NULL;
307 unsigned line = 0, section_line = 0;
308 bool section_ignored = false;
315 f = ours = fopen(filename, "re");
317 /* Only log on request, except for ENOENT,
318 * since we return 0 to the caller. */
319 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
320 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
321 "Failed to open configuration file '%s': %m", filename);
322 return errno == ENOENT ? 0 : -errno;
326 fd_warn_permissions(filename, fileno(f));
329 _cleanup_free_ char *buf = NULL;
330 bool escaped = false;
333 r = read_line(f, LONG_LINE_MAX, &buf);
337 if (flags & CONFIG_PARSE_WARN)
338 log_error_errno(r, "%s:%u: Line too long", filename, line);
343 if (CONFIG_PARSE_WARN)
344 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
350 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
353 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
356 flags |= CONFIG_PARSE_REFUSE_BOM;
361 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
362 if (flags & CONFIG_PARSE_WARN)
363 log_error("%s:%u: Continuation line too long", filename, line);
367 if (!strextend(&continuation, l, NULL)) {
368 if (flags & CONFIG_PARSE_WARN)
377 for (e = p; *e; e++) {
388 continuation = strdup(l);
390 if (flags & CONFIG_PARSE_WARN)
412 if (flags & CONFIG_PARSE_WARN)
413 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
417 continuation = mfree(continuation);
434 if (flags & CONFIG_PARSE_WARN)
435 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
443 static int config_parse_many_files(
444 const char *conf_file,
446 const char *sections,
447 ConfigItemLookup lookup,
449 ConfigParseFlags flags,
456 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
461 STRV_FOREACH(fn, files) {
462 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
470 /* Parse each config file in the directories specified as nulstr. */
471 int config_parse_many_nulstr(
472 const char *conf_file,
473 const char *conf_file_dirs,
474 const char *sections,
475 ConfigItemLookup lookup,
477 ConfigParseFlags flags,
480 _cleanup_strv_free_ char **files = NULL;
483 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
487 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
490 #if 0 /// UNNEEDED by elogind
491 /* Parse each config file in the directories specified as strv. */
492 int config_parse_many(
493 const char *conf_file,
494 const char* const* conf_file_dirs,
495 const char *dropin_dirname,
496 const char *sections,
497 ConfigItemLookup lookup,
499 ConfigParseFlags flags,
502 _cleanup_strv_free_ char **dropin_dirs = NULL;
503 _cleanup_strv_free_ char **files = NULL;
507 suffix = strjoina("/", dropin_dirname);
508 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
512 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
516 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
520 #define DEFINE_PARSER(type, vartype, conv_func) \
521 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
523 DEFINE_PARSER(int, int, safe_atoi);
524 DEFINE_PARSER(long, long, safe_atoli);
525 #if 0 /// UNNEEDED by elogind
526 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
527 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
528 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
530 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
531 DEFINE_PARSER(unsigned, unsigned, safe_atou);
532 DEFINE_PARSER(double, double, safe_atod);
533 #if 0 /// UNNEEDED by elogind
534 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
536 DEFINE_PARSER(sec, usec_t, parse_sec);
537 DEFINE_PARSER(mode, mode_t, parse_mode);
539 int config_parse_iec_size(const char* unit,
540 const char *filename,
543 unsigned section_line,
559 r = parse_size(rvalue, 1024, &v);
560 if (r < 0 || (uint64_t) (size_t) v != v) {
561 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
569 #if 0 /// UNNEEDED by elogind
570 int config_parse_si_size(
572 const char *filename,
575 unsigned section_line,
591 r = parse_size(rvalue, 1000, &v);
592 if (r < 0 || (uint64_t) (size_t) v != v) {
593 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
601 int config_parse_iec_uint64(
603 const char *filename,
606 unsigned section_line,
613 uint64_t *bytes = data;
621 r = parse_size(rvalue, 1024, bytes);
623 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
629 int config_parse_bool(const char* unit,
630 const char *filename,
633 unsigned section_line,
649 k = parse_boolean(rvalue);
651 log_syntax(unit, LOG_ERR, filename, line, k,
652 "Failed to parse boolean value%s: %s",
653 fatal ? "" : ", ignoring", rvalue);
654 return fatal ? -ENOEXEC : 0;
661 #if 0 /// UNNEEDED by elogind
662 int config_parse_tristate(
664 const char *filename,
667 unsigned section_line,
681 /* A tristate is pretty much a boolean, except that it can
682 * also take the special value -1, indicating "uninitialized",
683 * much like NULL is for a pointer type. */
685 k = parse_boolean(rvalue);
687 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
696 int config_parse_string(
698 const char *filename,
701 unsigned section_line,
715 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
721 int config_parse_path(
723 const char *filename,
726 unsigned section_line,
733 _cleanup_free_ char *n = NULL;
750 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
752 return fatal ? -ENOEXEC : 0;
755 return free_and_replace(*s, n);
758 int config_parse_strv(
760 const char *filename,
763 unsigned section_line,
778 if (isempty(rvalue)) {
779 *sv = strv_free(*sv);
786 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
792 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
796 r = strv_consume(sv, word);
804 int config_parse_warn_compat(
806 const char *filename,
809 unsigned section_line,
816 Disabled reason = ltype;
820 case DISABLED_CONFIGURATION:
821 log_syntax(unit, LOG_DEBUG, filename, line, 0,
822 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
825 case DISABLED_LEGACY:
826 log_syntax(unit, LOG_INFO, filename, line, 0,
827 "Support for option %s= has been removed and it is ignored", lvalue);
830 case DISABLED_EXPERIMENTAL:
831 log_syntax(unit, LOG_INFO, filename, line, 0,
832 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
839 #if 0 /// UNNEEDED by elogind
840 int config_parse_log_facility(
842 const char *filename,
845 unsigned section_line,
859 x = log_facility_unshifted_from_string(rvalue);
861 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
865 *o = (x << 3) | LOG_PRI(*o);
871 int config_parse_log_level(
873 const char *filename,
876 unsigned section_line,
890 x = log_level_from_string(rvalue);
892 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
896 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
899 *o = (*o & LOG_FACMASK) | x;
904 int config_parse_signal(
906 const char *filename,
909 unsigned section_line,
923 r = signal_from_string(rvalue);
925 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
933 #if 0 /// UNNEEDED by elogind
934 int config_parse_personality(
936 const char *filename,
939 unsigned section_line,
946 unsigned long *personality = data, p;
954 p = PERSONALITY_INVALID;
956 p = personality_from_string(rvalue);
957 if (p == PERSONALITY_INVALID) {
958 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
967 int config_parse_ifname(
969 const char *filename,
972 unsigned section_line,
987 if (isempty(rvalue)) {
992 if (!ifname_valid(rvalue)) {
993 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
997 r = free_and_strdup(s, rvalue);
1004 int config_parse_ip_port(
1006 const char *filename,
1008 const char *section,
1009 unsigned section_line,
1025 if (isempty(rvalue)) {
1030 r = parse_ip_port(rvalue, &port);
1032 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1041 int config_parse_join_controllers(
1043 const char *filename,
1045 const char *section,
1046 unsigned section_line,
1053 char ****ret = data;
1054 const char *whole_rvalue = rvalue;
1056 _cleanup_(strv_free_freep) char ***controllers = NULL;
1064 _cleanup_free_ char *word = NULL;
1068 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1070 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1076 l = strv_split(word, ",");
1081 if (strv_length(l) <= 1) {
1087 controllers = new(char**, 2);
1094 controllers[1] = NULL;
1101 t = new0(char**, n+2);
1109 for (a = controllers; *a; a++)
1110 if (strv_overlap(*a, l)) {
1111 if (strv_extend_strv(&l, *a, false) < 0) {
1130 t[n++] = strv_uniq(l);
1132 strv_free_free(controllers);
1136 if (!isempty(rvalue))
1137 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1139 /* As a special case, return a single empty strv, to override the default */
1141 controllers = new(char**, 2);
1144 controllers[0] = strv_new(NULL, NULL);
1145 if (!controllers[0])
1147 controllers[1] = NULL;
1150 strv_free_free(*ret);
1151 *ret = TAKE_PTR(controllers);
1157 int config_parse_mtu(
1159 const char *filename,
1161 const char *section,
1162 unsigned section_line,
1169 uint32_t *mtu = data;
1175 r = parse_mtu(ltype, rvalue, mtu);
1177 log_syntax(unit, LOG_ERR, filename, line, r,
1178 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1179 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1184 log_syntax(unit, LOG_ERR, filename, line, r,
1185 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1192 int config_parse_rlimit(
1194 const char *filename,
1196 const char *section,
1197 unsigned section_line,
1204 struct rlimit **rl = data, d = {};
1210 r = rlimit_parse(ltype, rvalue, &d);
1212 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1216 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1223 rl[ltype] = newdup(struct rlimit, &d, 1);
1231 int config_parse_permille(const char* unit,
1232 const char *filename,
1234 const char *section,
1235 unsigned section_line,
1242 unsigned *permille = data;
1250 r = parse_permille(rvalue);
1252 log_syntax(unit, LOG_ERR, filename, line, r,
1253 "Failed to parse permille value, ignoring: %s", rvalue);
1257 *permille = (unsigned) r;