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"
54 int config_item_table_lookup(
58 ConfigParserCallback *func,
63 const ConfigTableItem *t;
71 for (t = table; t->lvalue; t++) {
73 if (!streq(lvalue, t->lvalue))
76 if (!streq_ptr(section, t->section))
88 int config_item_perf_lookup(
92 ConfigParserCallback *func,
97 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
98 const ConfigPerfItem *p;
107 p = lookup(lvalue, strlen(lvalue));
111 key = strjoin(section, ".", lvalue);
115 p = lookup(key, strlen(key));
124 *data = (uint8_t*) userdata + p->offset;
128 /* Run the user supplied parser for an assignment */
129 static int next_assignment(
131 const char *filename,
133 ConfigItemLookup lookup,
136 unsigned section_line,
139 ConfigParseFlags flags,
142 ConfigParserCallback func = NULL;
153 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
159 return func(unit, filename, line, section, section_line,
160 lvalue, ltype, rvalue, data, userdata);
165 /* Warn about unknown non-extension fields. */
166 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
167 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
172 /* Parse a single logical line */
173 static int parse_line(
175 const char *filename,
177 const char *sections,
178 ConfigItemLookup lookup,
180 ConfigParseFlags flags,
182 unsigned *section_line,
183 bool *section_ignored,
198 if (strchr(COMMENTS "\n", *l))
201 include = first_word(l, ".include");
203 _cleanup_free_ char *fn = NULL;
205 /* .includes are a bad idea, we only support them here
206 * for historical reasons. They create cyclic include
207 * problems and make it difficult to detect
208 * configuration file changes with an easy
209 * stat(). Better approaches, such as .d/ drop-in
212 * Support for them should be eventually removed. */
214 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
215 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
219 log_syntax(unit, LOG_WARNING, filename, line, 0,
220 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
221 "Please use drop-in files instead.");
223 fn = file_in_same_dir(filename, strstrip(include));
227 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
230 if (!utf8_is_valid(l))
231 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
241 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
245 n = strndup(l+1, k-2);
249 if (sections && !nulstr_contains(sections, n)) {
251 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
252 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
255 *section = mfree(*section);
257 *section_ignored = true;
259 free_and_replace(*section, n);
260 *section_line = line;
261 *section_ignored = false;
267 if (sections && !*section) {
269 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
270 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
277 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
284 return next_assignment(unit,
297 /* Go through the file and parse each line */
298 int config_parse(const char *unit,
299 const char *filename,
301 const char *sections,
302 ConfigItemLookup lookup,
304 ConfigParseFlags flags,
307 _cleanup_free_ char *section = NULL, *continuation = NULL;
308 _cleanup_fclose_ FILE *ours = NULL;
309 unsigned line = 0, section_line = 0;
310 bool section_ignored = false;
317 f = ours = fopen(filename, "re");
319 /* Only log on request, except for ENOENT,
320 * since we return 0 to the caller. */
321 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
322 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
323 "Failed to open configuration file '%s': %m", filename);
324 return errno == ENOENT ? 0 : -errno;
328 fd_warn_permissions(filename, fileno(f));
331 _cleanup_free_ char *buf = NULL;
332 bool escaped = false;
335 r = read_line(f, LONG_LINE_MAX, &buf);
339 if (flags & CONFIG_PARSE_WARN)
340 log_error_errno(r, "%s:%u: Line too long", filename, line);
345 if (CONFIG_PARSE_WARN)
346 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
352 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
355 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
358 flags |= CONFIG_PARSE_REFUSE_BOM;
363 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
364 if (flags & CONFIG_PARSE_WARN)
365 log_error("%s:%u: Continuation line too long", filename, line);
369 if (!strextend(&continuation, l, NULL)) {
370 if (flags & CONFIG_PARSE_WARN)
379 for (e = p; *e; e++) {
390 continuation = strdup(l);
392 if (flags & CONFIG_PARSE_WARN)
414 if (flags & CONFIG_PARSE_WARN)
415 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
419 continuation = mfree(continuation);
436 if (flags & CONFIG_PARSE_WARN)
437 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
445 static int config_parse_many_files(
446 const char *conf_file,
448 const char *sections,
449 ConfigItemLookup lookup,
451 ConfigParseFlags flags,
458 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
463 STRV_FOREACH(fn, files) {
464 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
472 /* Parse each config file in the directories specified as nulstr. */
473 int config_parse_many_nulstr(
474 const char *conf_file,
475 const char *conf_file_dirs,
476 const char *sections,
477 ConfigItemLookup lookup,
479 ConfigParseFlags flags,
482 _cleanup_strv_free_ char **files = NULL;
485 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
489 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
492 #if 0 /// UNNEEDED by elogind
493 /* Parse each config file in the directories specified as strv. */
494 int config_parse_many(
495 const char *conf_file,
496 const char* const* conf_file_dirs,
497 const char *dropin_dirname,
498 const char *sections,
499 ConfigItemLookup lookup,
501 ConfigParseFlags flags,
504 _cleanup_strv_free_ char **dropin_dirs = NULL;
505 _cleanup_strv_free_ char **files = NULL;
509 suffix = strjoina("/", dropin_dirname);
510 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
514 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
518 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
522 #define DEFINE_PARSER(type, vartype, conv_func) \
523 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
525 DEFINE_PARSER(int, int, safe_atoi);
526 DEFINE_PARSER(long, long, safe_atoli);
527 #if 0 /// UNNEEDED by elogind
528 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
529 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
530 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
532 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
533 DEFINE_PARSER(unsigned, unsigned, safe_atou);
534 DEFINE_PARSER(double, double, safe_atod);
535 #if 0 /// UNNEEDED by elogind
536 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
538 DEFINE_PARSER(sec, usec_t, parse_sec);
539 DEFINE_PARSER(mode, mode_t, parse_mode);
541 int config_parse_iec_size(const char* unit,
542 const char *filename,
545 unsigned section_line,
561 r = parse_size(rvalue, 1024, &v);
562 if (r < 0 || (uint64_t) (size_t) v != v) {
563 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
571 #if 0 /// UNNEEDED by elogind
572 int config_parse_si_size(
574 const char *filename,
577 unsigned section_line,
593 r = parse_size(rvalue, 1000, &v);
594 if (r < 0 || (uint64_t) (size_t) v != v) {
595 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
603 int config_parse_iec_uint64(
605 const char *filename,
608 unsigned section_line,
615 uint64_t *bytes = data;
623 r = parse_size(rvalue, 1024, bytes);
625 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
631 int config_parse_bool(const char* unit,
632 const char *filename,
635 unsigned section_line,
651 k = parse_boolean(rvalue);
653 log_syntax(unit, LOG_ERR, filename, line, k,
654 "Failed to parse boolean value%s: %s",
655 fatal ? "" : ", ignoring", rvalue);
656 return fatal ? -ENOEXEC : 0;
663 #if 0 /// UNNEEDED by elogind
664 int config_parse_tristate(
666 const char *filename,
669 unsigned section_line,
683 /* A tristate is pretty much a boolean, except that it can
684 * also take the special value -1, indicating "uninitialized",
685 * much like NULL is for a pointer type. */
687 k = parse_boolean(rvalue);
689 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
698 int config_parse_string(
700 const char *filename,
703 unsigned section_line,
717 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
723 int config_parse_path(
725 const char *filename,
728 unsigned section_line,
735 _cleanup_free_ char *n = NULL;
752 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
754 return fatal ? -ENOEXEC : 0;
757 return free_and_replace(*s, n);
760 int config_parse_strv(
762 const char *filename,
765 unsigned section_line,
780 if (isempty(rvalue)) {
781 *sv = strv_free(*sv);
788 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
794 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
798 r = strv_consume(sv, word);
806 int config_parse_warn_compat(
808 const char *filename,
811 unsigned section_line,
818 Disabled reason = ltype;
822 case DISABLED_CONFIGURATION:
823 log_syntax(unit, LOG_DEBUG, filename, line, 0,
824 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
827 case DISABLED_LEGACY:
828 log_syntax(unit, LOG_INFO, filename, line, 0,
829 "Support for option %s= has been removed and it is ignored", lvalue);
832 case DISABLED_EXPERIMENTAL:
833 log_syntax(unit, LOG_INFO, filename, line, 0,
834 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
841 #if 0 /// UNNEEDED by elogind
842 int config_parse_log_facility(
844 const char *filename,
847 unsigned section_line,
861 x = log_facility_unshifted_from_string(rvalue);
863 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
867 *o = (x << 3) | LOG_PRI(*o);
873 int config_parse_log_level(
875 const char *filename,
878 unsigned section_line,
892 x = log_level_from_string(rvalue);
894 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
898 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
901 *o = (*o & LOG_FACMASK) | x;
906 int config_parse_signal(
908 const char *filename,
911 unsigned section_line,
925 r = signal_from_string(rvalue);
927 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
935 #if 0 /// UNNEEDED by elogind
936 int config_parse_personality(
938 const char *filename,
941 unsigned section_line,
948 unsigned long *personality = data, p;
956 p = PERSONALITY_INVALID;
958 p = personality_from_string(rvalue);
959 if (p == PERSONALITY_INVALID) {
960 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
969 int config_parse_ifname(
971 const char *filename,
974 unsigned section_line,
989 if (isempty(rvalue)) {
994 if (!ifname_valid(rvalue)) {
995 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
999 r = free_and_strdup(s, rvalue);
1006 int config_parse_ip_port(
1008 const char *filename,
1010 const char *section,
1011 unsigned section_line,
1027 if (isempty(rvalue)) {
1032 r = parse_ip_port(rvalue, &port);
1034 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1043 int config_parse_join_controllers(
1045 const char *filename,
1047 const char *section,
1048 unsigned section_line,
1055 char ****ret = data;
1056 const char *whole_rvalue = rvalue;
1058 _cleanup_(strv_free_freep) char ***controllers = NULL;
1066 _cleanup_free_ char *word = NULL;
1070 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1072 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1078 l = strv_split(word, ",");
1083 if (strv_length(l) <= 1) {
1089 controllers = new(char**, 2);
1096 controllers[1] = NULL;
1103 t = new0(char**, n+2);
1111 for (a = controllers; *a; a++)
1112 if (strv_overlap(*a, l)) {
1113 if (strv_extend_strv(&l, *a, false) < 0) {
1132 t[n++] = strv_uniq(l);
1134 strv_free_free(controllers);
1138 if (!isempty(rvalue))
1139 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1141 /* As a special case, return a single empty strv, to override the default */
1143 controllers = new(char**, 2);
1146 controllers[0] = strv_new(NULL, NULL);
1147 if (!controllers[0])
1149 controllers[1] = NULL;
1152 strv_free_free(*ret);
1153 *ret = TAKE_PTR(controllers);
1159 int config_parse_mtu(
1161 const char *filename,
1163 const char *section,
1164 unsigned section_line,
1171 uint32_t *mtu = data;
1177 r = parse_mtu(ltype, rvalue, mtu);
1179 log_syntax(unit, LOG_ERR, filename, line, r,
1180 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1181 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1186 log_syntax(unit, LOG_ERR, filename, line, r,
1187 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1194 int config_parse_rlimit(
1196 const char *filename,
1198 const char *section,
1199 unsigned section_line,
1206 struct rlimit **rl = data, d = {};
1212 r = rlimit_parse(ltype, rvalue, &d);
1214 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1218 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1225 rl[ltype] = newdup(struct rlimit, &d, 1);
1233 int config_parse_permille(const char* unit,
1234 const char *filename,
1236 const char *section,
1237 unsigned section_line,
1244 unsigned *permille = data;
1252 r = parse_permille(rvalue);
1254 log_syntax(unit, LOG_ERR, filename, line, r,
1255 "Failed to parse permille value, ignoring: %s", rvalue);
1259 *permille = (unsigned) r;