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"
51 int config_item_table_lookup(
55 ConfigParserCallback *func,
60 const ConfigTableItem *t;
68 for (t = table; t->lvalue; t++) {
70 if (!streq(lvalue, t->lvalue))
73 if (!streq_ptr(section, t->section))
85 int config_item_perf_lookup(
89 ConfigParserCallback *func,
94 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
95 const ConfigPerfItem *p;
104 p = lookup(lvalue, strlen(lvalue));
108 key = strjoin(section, ".", lvalue);
112 p = lookup(key, strlen(key));
121 *data = (uint8_t*) userdata + p->offset;
125 /* Run the user supplied parser for an assignment */
126 static int next_assignment(
128 const char *filename,
130 ConfigItemLookup lookup,
133 unsigned section_line,
136 ConfigParseFlags flags,
139 ConfigParserCallback func = NULL;
150 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
156 return func(unit, filename, line, section, section_line,
157 lvalue, ltype, rvalue, data, userdata);
162 /* Warn about unknown non-extension fields. */
163 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
164 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
169 /* Parse a single logical line */
170 static int parse_line(
172 const char *filename,
174 const char *sections,
175 ConfigItemLookup lookup,
177 ConfigParseFlags flags,
179 unsigned *section_line,
180 bool *section_ignored,
195 if (strchr(COMMENTS "\n", *l))
198 include = first_word(l, ".include");
200 _cleanup_free_ char *fn = NULL;
202 /* .includes are a bad idea, we only support them here
203 * for historical reasons. They create cyclic include
204 * problems and make it difficult to detect
205 * configuration file changes with an easy
206 * stat(). Better approaches, such as .d/ drop-in
209 * Support for them should be eventually removed. */
211 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
212 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
216 log_syntax(unit, LOG_WARNING, filename, line, 0,
217 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
218 "Please use drop-in files instead.");
220 fn = file_in_same_dir(filename, strstrip(include));
224 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
227 if (!utf8_is_valid(l))
228 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
238 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
242 n = strndup(l+1, k-2);
246 if (sections && !nulstr_contains(sections, n)) {
248 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
249 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
252 *section = mfree(*section);
254 *section_ignored = true;
256 free_and_replace(*section, n);
257 *section_line = line;
258 *section_ignored = false;
264 if (sections && !*section) {
266 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
267 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
274 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
281 return next_assignment(unit,
294 /* Go through the file and parse each line */
295 int config_parse(const char *unit,
296 const char *filename,
298 const char *sections,
299 ConfigItemLookup lookup,
301 ConfigParseFlags flags,
304 _cleanup_free_ char *section = NULL, *continuation = NULL;
305 _cleanup_fclose_ FILE *ours = NULL;
306 unsigned line = 0, section_line = 0;
307 bool section_ignored = false;
314 f = ours = fopen(filename, "re");
316 /* Only log on request, except for ENOENT,
317 * since we return 0 to the caller. */
318 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
319 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
320 "Failed to open configuration file '%s': %m", filename);
321 return errno == ENOENT ? 0 : -errno;
325 fd_warn_permissions(filename, fileno(f));
328 _cleanup_free_ char *buf = NULL;
329 bool escaped = false;
332 r = read_line(f, LONG_LINE_MAX, &buf);
336 if (flags & CONFIG_PARSE_WARN)
337 log_error_errno(r, "%s:%u: Line too long", filename, line);
342 if (CONFIG_PARSE_WARN)
343 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
349 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
352 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
355 flags |= CONFIG_PARSE_REFUSE_BOM;
360 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
361 if (flags & CONFIG_PARSE_WARN)
362 log_error("%s:%u: Continuation line too long", filename, line);
366 if (!strextend(&continuation, l, NULL)) {
367 if (flags & CONFIG_PARSE_WARN)
376 for (e = p; *e; e++) {
387 continuation = strdup(l);
389 if (flags & CONFIG_PARSE_WARN)
411 if (flags & CONFIG_PARSE_WARN)
412 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
416 continuation = mfree(continuation);
433 if (flags & CONFIG_PARSE_WARN)
434 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
442 static int config_parse_many_files(
443 const char *conf_file,
445 const char *sections,
446 ConfigItemLookup lookup,
448 ConfigParseFlags flags,
455 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
460 STRV_FOREACH(fn, files) {
461 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
469 /* Parse each config file in the directories specified as nulstr. */
470 int config_parse_many_nulstr(
471 const char *conf_file,
472 const char *conf_file_dirs,
473 const char *sections,
474 ConfigItemLookup lookup,
476 ConfigParseFlags flags,
479 _cleanup_strv_free_ char **files = NULL;
482 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
486 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
489 #if 0 /// UNNEEDED by elogind
490 /* Parse each config file in the directories specified as strv. */
491 int config_parse_many(
492 const char *conf_file,
493 const char* const* conf_file_dirs,
494 const char *dropin_dirname,
495 const char *sections,
496 ConfigItemLookup lookup,
498 ConfigParseFlags flags,
501 _cleanup_strv_free_ char **dropin_dirs = NULL;
502 _cleanup_strv_free_ char **files = NULL;
506 suffix = strjoina("/", dropin_dirname);
507 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
511 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
515 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
519 #define DEFINE_PARSER(type, vartype, conv_func) \
520 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
522 DEFINE_PARSER(int, int, safe_atoi);
523 DEFINE_PARSER(long, long, safe_atoli);
524 #if 0 /// UNNEEDED by elogind
525 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
526 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
527 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
529 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
530 DEFINE_PARSER(unsigned, unsigned, safe_atou);
531 DEFINE_PARSER(double, double, safe_atod);
532 #if 0 /// UNNEEDED by elogind
533 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
535 DEFINE_PARSER(sec, usec_t, parse_sec);
536 DEFINE_PARSER(mode, mode_t, parse_mode);
538 int config_parse_iec_size(const char* unit,
539 const char *filename,
542 unsigned section_line,
558 r = parse_size(rvalue, 1024, &v);
559 if (r < 0 || (uint64_t) (size_t) v != v) {
560 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
568 #if 0 /// UNNEEDED by elogind
569 int config_parse_si_size(
571 const char *filename,
574 unsigned section_line,
590 r = parse_size(rvalue, 1000, &v);
591 if (r < 0 || (uint64_t) (size_t) v != v) {
592 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
600 int config_parse_iec_uint64(
602 const char *filename,
605 unsigned section_line,
612 uint64_t *bytes = data;
620 r = parse_size(rvalue, 1024, bytes);
622 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
628 int config_parse_bool(const char* unit,
629 const char *filename,
632 unsigned section_line,
648 k = parse_boolean(rvalue);
650 log_syntax(unit, LOG_ERR, filename, line, k,
651 "Failed to parse boolean value%s: %s",
652 fatal ? "" : ", ignoring", rvalue);
653 return fatal ? -ENOEXEC : 0;
660 #if 0 /// UNNEEDED by elogind
661 int config_parse_tristate(
663 const char *filename,
666 unsigned section_line,
680 /* A tristate is pretty much a boolean, except that it can
681 * also take the special value -1, indicating "uninitialized",
682 * much like NULL is for a pointer type. */
684 k = parse_boolean(rvalue);
686 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
695 int config_parse_string(
697 const char *filename,
700 unsigned section_line,
714 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
720 int config_parse_path(
722 const char *filename,
725 unsigned section_line,
732 _cleanup_free_ char *n = NULL;
742 if (isempty(rvalue)) {
751 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
753 return fatal ? -ENOEXEC : 0;
756 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);