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"
48 //#include "rlimit-util.h"
49 //#include "rlimit-util.h"
50 //#include "rlimit-util.h"
51 //#include "rlimit-util.h"
52 //#include "rlimit-util.h"
53 //#include "rlimit-util.h"
55 int config_item_table_lookup(
59 ConfigParserCallback *func,
64 const ConfigTableItem *t;
72 for (t = table; t->lvalue; t++) {
74 if (!streq(lvalue, t->lvalue))
77 if (!streq_ptr(section, t->section))
89 int config_item_perf_lookup(
93 ConfigParserCallback *func,
98 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
99 const ConfigPerfItem *p;
108 p = lookup(lvalue, strlen(lvalue));
112 key = strjoin(section, ".", lvalue);
116 p = lookup(key, strlen(key));
125 *data = (uint8_t*) userdata + p->offset;
129 /* Run the user supplied parser for an assignment */
130 static int next_assignment(
132 const char *filename,
134 ConfigItemLookup lookup,
137 unsigned section_line,
140 ConfigParseFlags flags,
143 ConfigParserCallback func = NULL;
154 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
160 return func(unit, filename, line, section, section_line,
161 lvalue, ltype, rvalue, data, userdata);
166 /* Warn about unknown non-extension fields. */
167 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
168 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
173 /* Parse a single logical line */
174 static int parse_line(
176 const char *filename,
178 const char *sections,
179 ConfigItemLookup lookup,
181 ConfigParseFlags flags,
183 unsigned *section_line,
184 bool *section_ignored,
199 if (strchr(COMMENTS "\n", *l))
202 include = first_word(l, ".include");
204 _cleanup_free_ char *fn = NULL;
206 /* .includes are a bad idea, we only support them here
207 * for historical reasons. They create cyclic include
208 * problems and make it difficult to detect
209 * configuration file changes with an easy
210 * stat(). Better approaches, such as .d/ drop-in
213 * Support for them should be eventually removed. */
215 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
216 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
220 log_syntax(unit, LOG_WARNING, filename, line, 0,
221 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
222 "Please use drop-in files instead.");
224 fn = file_in_same_dir(filename, strstrip(include));
228 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
231 if (!utf8_is_valid(l))
232 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
242 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
246 n = strndup(l+1, k-2);
250 if (sections && !nulstr_contains(sections, n)) {
252 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
253 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
256 *section = mfree(*section);
258 *section_ignored = true;
260 free_and_replace(*section, n);
261 *section_line = line;
262 *section_ignored = false;
268 if (sections && !*section) {
270 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
271 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
278 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
285 return next_assignment(unit,
298 /* Go through the file and parse each line */
299 int config_parse(const char *unit,
300 const char *filename,
302 const char *sections,
303 ConfigItemLookup lookup,
305 ConfigParseFlags flags,
308 _cleanup_free_ char *section = NULL, *continuation = NULL;
309 _cleanup_fclose_ FILE *ours = NULL;
310 unsigned line = 0, section_line = 0;
311 bool section_ignored = false;
318 f = ours = fopen(filename, "re");
320 /* Only log on request, except for ENOENT,
321 * since we return 0 to the caller. */
322 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
323 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
324 "Failed to open configuration file '%s': %m", filename);
325 return errno == ENOENT ? 0 : -errno;
329 fd_warn_permissions(filename, fileno(f));
332 _cleanup_free_ char *buf = NULL;
333 bool escaped = false;
336 r = read_line(f, LONG_LINE_MAX, &buf);
340 if (flags & CONFIG_PARSE_WARN)
341 log_error_errno(r, "%s:%u: Line too long", filename, line);
346 if (CONFIG_PARSE_WARN)
347 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
353 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
356 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
359 flags |= CONFIG_PARSE_REFUSE_BOM;
364 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
365 if (flags & CONFIG_PARSE_WARN)
366 log_error("%s:%u: Continuation line too long", filename, line);
370 if (!strextend(&continuation, l, NULL)) {
371 if (flags & CONFIG_PARSE_WARN)
380 for (e = p; *e; e++) {
391 continuation = strdup(l);
393 if (flags & CONFIG_PARSE_WARN)
415 if (flags & CONFIG_PARSE_WARN)
416 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
420 continuation = mfree(continuation);
437 if (flags & CONFIG_PARSE_WARN)
438 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
446 static int config_parse_many_files(
447 const char *conf_file,
449 const char *sections,
450 ConfigItemLookup lookup,
452 ConfigParseFlags flags,
459 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
464 STRV_FOREACH(fn, files) {
465 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
473 /* Parse each config file in the directories specified as nulstr. */
474 int config_parse_many_nulstr(
475 const char *conf_file,
476 const char *conf_file_dirs,
477 const char *sections,
478 ConfigItemLookup lookup,
480 ConfigParseFlags flags,
483 _cleanup_strv_free_ char **files = NULL;
486 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
490 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
493 #if 0 /// UNNEEDED by elogind
494 /* Parse each config file in the directories specified as strv. */
495 int config_parse_many(
496 const char *conf_file,
497 const char* const* conf_file_dirs,
498 const char *dropin_dirname,
499 const char *sections,
500 ConfigItemLookup lookup,
502 ConfigParseFlags flags,
505 _cleanup_strv_free_ char **dropin_dirs = NULL;
506 _cleanup_strv_free_ char **files = NULL;
510 suffix = strjoina("/", dropin_dirname);
511 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
515 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
519 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
523 #define DEFINE_PARSER(type, vartype, conv_func) \
524 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
526 DEFINE_PARSER(int, int, safe_atoi);
527 DEFINE_PARSER(long, long, safe_atoli);
528 #if 0 /// UNNEEDED by elogind
529 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
530 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
531 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
533 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
534 DEFINE_PARSER(unsigned, unsigned, safe_atou);
535 DEFINE_PARSER(double, double, safe_atod);
536 #if 0 /// UNNEEDED by elogind
537 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
539 DEFINE_PARSER(sec, usec_t, parse_sec);
540 DEFINE_PARSER(mode, mode_t, parse_mode);
542 int config_parse_iec_size(const char* unit,
543 const char *filename,
546 unsigned section_line,
562 r = parse_size(rvalue, 1024, &v);
563 if (r < 0 || (uint64_t) (size_t) v != v) {
564 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
572 #if 0 /// UNNEEDED by elogind
573 int config_parse_si_size(
575 const char *filename,
578 unsigned section_line,
594 r = parse_size(rvalue, 1000, &v);
595 if (r < 0 || (uint64_t) (size_t) v != v) {
596 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
604 int config_parse_iec_uint64(
606 const char *filename,
609 unsigned section_line,
616 uint64_t *bytes = data;
624 r = parse_size(rvalue, 1024, bytes);
626 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
632 int config_parse_bool(const char* unit,
633 const char *filename,
636 unsigned section_line,
652 k = parse_boolean(rvalue);
654 log_syntax(unit, LOG_ERR, filename, line, k,
655 "Failed to parse boolean value%s: %s",
656 fatal ? "" : ", ignoring", rvalue);
657 return fatal ? -ENOEXEC : 0;
664 #if 0 /// UNNEEDED by elogind
665 int config_parse_tristate(
667 const char *filename,
670 unsigned section_line,
684 /* A tristate is pretty much a boolean, except that it can
685 * also take the special value -1, indicating "uninitialized",
686 * much like NULL is for a pointer type. */
688 k = parse_boolean(rvalue);
690 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
699 int config_parse_string(
701 const char *filename,
704 unsigned section_line,
718 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
724 int config_parse_path(
726 const char *filename,
729 unsigned section_line,
736 _cleanup_free_ char *n = NULL;
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 return free_and_replace(*s, n);
761 int config_parse_strv(
763 const char *filename,
766 unsigned section_line,
781 if (isempty(rvalue)) {
782 *sv = strv_free(*sv);
789 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
795 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
799 r = strv_consume(sv, word);
807 int config_parse_warn_compat(
809 const char *filename,
812 unsigned section_line,
819 Disabled reason = ltype;
823 case DISABLED_CONFIGURATION:
824 log_syntax(unit, LOG_DEBUG, filename, line, 0,
825 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
828 case DISABLED_LEGACY:
829 log_syntax(unit, LOG_INFO, filename, line, 0,
830 "Support for option %s= has been removed and it is ignored", lvalue);
833 case DISABLED_EXPERIMENTAL:
834 log_syntax(unit, LOG_INFO, filename, line, 0,
835 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
842 #if 0 /// UNNEEDED by elogind
843 int config_parse_log_facility(
845 const char *filename,
848 unsigned section_line,
862 x = log_facility_unshifted_from_string(rvalue);
864 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
868 *o = (x << 3) | LOG_PRI(*o);
874 int config_parse_log_level(
876 const char *filename,
879 unsigned section_line,
893 x = log_level_from_string(rvalue);
895 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
899 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
902 *o = (*o & LOG_FACMASK) | x;
907 int config_parse_signal(
909 const char *filename,
912 unsigned section_line,
926 r = signal_from_string(rvalue);
928 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
936 #if 0 /// UNNEEDED by elogind
937 int config_parse_personality(
939 const char *filename,
942 unsigned section_line,
949 unsigned long *personality = data, p;
957 p = PERSONALITY_INVALID;
959 p = personality_from_string(rvalue);
960 if (p == PERSONALITY_INVALID) {
961 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
970 int config_parse_ifname(
972 const char *filename,
975 unsigned section_line,
990 if (isempty(rvalue)) {
995 if (!ifname_valid(rvalue)) {
996 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1000 r = free_and_strdup(s, rvalue);
1007 int config_parse_ip_port(
1009 const char *filename,
1011 const char *section,
1012 unsigned section_line,
1028 if (isempty(rvalue)) {
1033 r = parse_ip_port(rvalue, &port);
1035 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1044 int config_parse_join_controllers(
1046 const char *filename,
1048 const char *section,
1049 unsigned section_line,
1056 char ****ret = data;
1057 const char *whole_rvalue = rvalue;
1059 _cleanup_(strv_free_freep) char ***controllers = NULL;
1067 _cleanup_free_ char *word = NULL;
1071 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1073 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1079 l = strv_split(word, ",");
1084 if (strv_length(l) <= 1) {
1090 controllers = new(char**, 2);
1097 controllers[1] = NULL;
1104 t = new0(char**, n+2);
1112 for (a = controllers; *a; a++)
1113 if (strv_overlap(*a, l)) {
1114 if (strv_extend_strv(&l, *a, false) < 0) {
1133 t[n++] = strv_uniq(l);
1135 strv_free_free(controllers);
1139 if (!isempty(rvalue))
1140 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1142 /* As a special case, return a single empty strv, to override the default */
1144 controllers = new(char**, 2);
1147 controllers[0] = strv_new(NULL, NULL);
1148 if (!controllers[0])
1150 controllers[1] = NULL;
1153 strv_free_free(*ret);
1154 *ret = TAKE_PTR(controllers);
1160 int config_parse_mtu(
1162 const char *filename,
1164 const char *section,
1165 unsigned section_line,
1172 uint32_t *mtu = data;
1178 r = parse_mtu(ltype, rvalue, mtu);
1180 log_syntax(unit, LOG_ERR, filename, line, r,
1181 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1182 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1187 log_syntax(unit, LOG_ERR, filename, line, r,
1188 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1195 int config_parse_rlimit(
1197 const char *filename,
1199 const char *section,
1200 unsigned section_line,
1207 struct rlimit **rl = data, d = {};
1213 r = rlimit_parse(ltype, rvalue, &d);
1215 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1219 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1226 rl[ltype] = newdup(struct rlimit, &d, 1);
1234 int config_parse_permille(const char* unit,
1235 const char *filename,
1237 const char *section,
1238 unsigned section_line,
1245 unsigned *permille = data;
1253 r = parse_permille(rvalue);
1255 log_syntax(unit, LOG_ERR, filename, line, r,
1256 "Failed to parse permille value, ignoring: %s", rvalue);
1260 *permille = (unsigned) r;