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"
48 int config_item_table_lookup(
52 ConfigParserCallback *func,
57 const ConfigTableItem *t;
65 for (t = table; t->lvalue; t++) {
67 if (!streq(lvalue, t->lvalue))
70 if (!streq_ptr(section, t->section))
82 int config_item_perf_lookup(
86 ConfigParserCallback *func,
91 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
92 const ConfigPerfItem *p;
101 p = lookup(lvalue, strlen(lvalue));
105 key = strjoin(section, ".", lvalue);
109 p = lookup(key, strlen(key));
118 *data = (uint8_t*) userdata + p->offset;
122 /* Run the user supplied parser for an assignment */
123 static int next_assignment(
125 const char *filename,
127 ConfigItemLookup lookup,
130 unsigned section_line,
133 ConfigParseFlags flags,
136 ConfigParserCallback func = NULL;
147 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
153 return func(unit, filename, line, section, section_line,
154 lvalue, ltype, rvalue, data, userdata);
159 /* Warn about unknown non-extension fields. */
160 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
161 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
166 /* Parse a single logical line */
167 static int parse_line(
169 const char *filename,
171 const char *sections,
172 ConfigItemLookup lookup,
174 ConfigParseFlags flags,
176 unsigned *section_line,
177 bool *section_ignored,
192 if (strchr(COMMENTS "\n", *l))
195 include = first_word(l, ".include");
197 _cleanup_free_ char *fn = NULL;
199 /* .includes are a bad idea, we only support them here
200 * for historical reasons. They create cyclic include
201 * problems and make it difficult to detect
202 * configuration file changes with an easy
203 * stat(). Better approaches, such as .d/ drop-in
206 * Support for them should be eventually removed. */
208 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
209 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
213 log_syntax(unit, LOG_WARNING, filename, line, 0,
214 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
215 "Please use drop-in files instead.");
217 fn = file_in_same_dir(filename, strstrip(include));
221 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
224 if (!utf8_is_valid(l))
225 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
235 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
239 n = strndup(l+1, k-2);
243 if (sections && !nulstr_contains(sections, n)) {
245 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
246 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
249 *section = mfree(*section);
251 *section_ignored = true;
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);
415 continuation = mfree(continuation);
432 if (flags & CONFIG_PARSE_WARN)
433 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 (!utf8_is_valid(rvalue)) {
715 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
719 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
725 int config_parse_path(
727 const char *filename,
730 unsigned section_line,
746 if (isempty(rvalue)) {
755 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
757 return fatal ? -ENOEXEC : 0;
760 free_and_replace(*s, n);
765 int config_parse_strv(
767 const char *filename,
770 unsigned section_line,
785 if (isempty(rvalue)) {
786 *sv = strv_free(*sv);
793 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
799 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
803 if (!utf8_is_valid(word)) {
804 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
809 r = strv_consume(sv, word);
817 int config_parse_warn_compat(
819 const char *filename,
822 unsigned section_line,
829 Disabled reason = ltype;
833 case DISABLED_CONFIGURATION:
834 log_syntax(unit, LOG_DEBUG, filename, line, 0,
835 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
838 case DISABLED_LEGACY:
839 log_syntax(unit, LOG_INFO, filename, line, 0,
840 "Support for option %s= has been removed and it is ignored", lvalue);
843 case DISABLED_EXPERIMENTAL:
844 log_syntax(unit, LOG_INFO, filename, line, 0,
845 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
852 #if 0 /// UNNEEDED by elogind
853 int config_parse_log_facility(
855 const char *filename,
858 unsigned section_line,
872 x = log_facility_unshifted_from_string(rvalue);
874 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
878 *o = (x << 3) | LOG_PRI(*o);
884 int config_parse_log_level(
886 const char *filename,
889 unsigned section_line,
903 x = log_level_from_string(rvalue);
905 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
909 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
912 *o = (*o & LOG_FACMASK) | x;
917 int config_parse_signal(
919 const char *filename,
922 unsigned section_line,
936 r = signal_from_string(rvalue);
938 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
946 #if 0 /// UNNEEDED by elogind
947 int config_parse_personality(
949 const char *filename,
952 unsigned section_line,
959 unsigned long *personality = data, p;
967 p = PERSONALITY_INVALID;
969 p = personality_from_string(rvalue);
970 if (p == PERSONALITY_INVALID) {
971 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
980 int config_parse_ifname(
982 const char *filename,
985 unsigned section_line,
1000 if (isempty(rvalue)) {
1005 if (!ifname_valid(rvalue)) {
1006 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1010 r = free_and_strdup(s, rvalue);
1017 int config_parse_ip_port(
1019 const char *filename,
1021 const char *section,
1022 unsigned section_line,
1038 if (isempty(rvalue)) {
1043 r = parse_ip_port(rvalue, &port);
1045 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1054 int config_parse_join_controllers(
1056 const char *filename,
1058 const char *section,
1059 unsigned section_line,
1066 char ****ret = data;
1067 const char *whole_rvalue = rvalue;
1069 _cleanup_(strv_free_freep) char ***controllers = NULL;
1077 _cleanup_free_ char *word = NULL;
1081 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1083 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1089 l = strv_split(word, ",");
1094 if (strv_length(l) <= 1) {
1100 controllers = new(char**, 2);
1107 controllers[1] = NULL;
1114 t = new0(char**, n+2);
1122 for (a = controllers; *a; a++)
1123 if (strv_overlap(*a, l)) {
1124 if (strv_extend_strv(&l, *a, false) < 0) {
1143 t[n++] = strv_uniq(l);
1145 strv_free_free(controllers);
1149 if (!isempty(rvalue))
1150 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1152 /* As a special case, return a single empty strv, to override the default */
1154 controllers = new(char**, 2);
1157 controllers[0] = strv_new(NULL, NULL);
1158 if (!controllers[0])
1160 controllers[1] = NULL;
1163 strv_free_free(*ret);
1164 *ret = TAKE_PTR(controllers);
1170 int config_parse_mtu(
1172 const char *filename,
1174 const char *section,
1175 unsigned section_line,
1182 uint32_t *mtu = data;
1188 r = parse_mtu(ltype, rvalue, mtu);
1190 log_syntax(unit, LOG_ERR, filename, line, r,
1191 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1192 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1197 log_syntax(unit, LOG_ERR, filename, line, r,
1198 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1205 int config_parse_rlimit(
1207 const char *filename,
1209 const char *section,
1210 unsigned section_line,
1217 struct rlimit **rl = data, d = {};
1223 r = rlimit_parse(ltype, rvalue, &d);
1225 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1229 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1236 rl[ltype] = newdup(struct rlimit, &d, 1);