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"
42 int config_item_table_lookup(
46 ConfigParserCallback *func,
51 const ConfigTableItem *t;
59 for (t = table; t->lvalue; t++) {
61 if (!streq(lvalue, t->lvalue))
64 if (!streq_ptr(section, t->section))
76 int config_item_perf_lookup(
80 ConfigParserCallback *func,
85 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
86 const ConfigPerfItem *p;
95 p = lookup(lvalue, strlen(lvalue));
99 key = strjoin(section, ".", lvalue);
103 p = lookup(key, strlen(key));
112 *data = (uint8_t*) userdata + p->offset;
116 /* Run the user supplied parser for an assignment */
117 static int next_assignment(
119 const char *filename,
121 ConfigItemLookup lookup,
124 unsigned section_line,
127 ConfigParseFlags flags,
130 ConfigParserCallback func = NULL;
141 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
147 return func(unit, filename, line, section, section_line,
148 lvalue, ltype, rvalue, data, userdata);
153 /* Warn about unknown non-extension fields. */
154 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
155 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
160 /* Parse a single logical line */
161 static int parse_line(
163 const char *filename,
165 const char *sections,
166 ConfigItemLookup lookup,
168 ConfigParseFlags flags,
170 unsigned *section_line,
171 bool *section_ignored,
186 if (strchr(COMMENTS "\n", *l))
189 include = first_word(l, ".include");
191 _cleanup_free_ char *fn = NULL;
193 /* .includes are a bad idea, we only support them here
194 * for historical reasons. They create cyclic include
195 * problems and make it difficult to detect
196 * configuration file changes with an easy
197 * stat(). Better approaches, such as .d/ drop-in
200 * Support for them should be eventually removed. */
202 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
203 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
207 log_syntax(unit, LOG_WARNING, filename, line, 0,
208 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
209 "Please use drop-in files instead.");
211 fn = file_in_same_dir(filename, strstrip(include));
215 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
226 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
230 n = strndup(l+1, k-2);
234 if (sections && !nulstr_contains(sections, n)) {
236 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
237 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
240 *section = mfree(*section);
242 *section_ignored = true;
246 *section_line = line;
247 *section_ignored = false;
253 if (sections && !*section) {
255 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
256 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
263 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
270 return next_assignment(unit,
283 /* Go through the file and parse each line */
284 int config_parse(const char *unit,
285 const char *filename,
287 const char *sections,
288 ConfigItemLookup lookup,
290 ConfigParseFlags flags,
293 _cleanup_free_ char *section = NULL, *continuation = NULL;
294 _cleanup_fclose_ FILE *ours = NULL;
295 unsigned line = 0, section_line = 0;
296 bool section_ignored = false;
303 f = ours = fopen(filename, "re");
305 /* Only log on request, except for ENOENT,
306 * since we return 0 to the caller. */
307 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
308 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
309 "Failed to open configuration file '%s': %m", filename);
310 return errno == ENOENT ? 0 : -errno;
314 fd_warn_permissions(filename, fileno(f));
317 _cleanup_free_ char *buf = NULL;
318 bool escaped = false;
321 r = read_line(f, LONG_LINE_MAX, &buf);
325 if (flags & CONFIG_PARSE_WARN)
326 log_error_errno(r, "%s:%u: Line too long", filename, line);
331 if (CONFIG_PARSE_WARN)
332 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
338 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
341 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
344 flags |= CONFIG_PARSE_REFUSE_BOM;
349 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
350 if (flags & CONFIG_PARSE_WARN)
351 log_error("%s:%u: Continuation line too long", filename, line);
355 if (!strextend(&continuation, l, NULL)) {
356 if (flags & CONFIG_PARSE_WARN)
365 for (e = p; *e; e++) {
376 continuation = strdup(l);
378 if (flags & CONFIG_PARSE_WARN)
400 if (flags & CONFIG_PARSE_WARN)
401 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
406 continuation = mfree(continuation);
423 if (flags & CONFIG_PARSE_WARN)
424 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
433 static int config_parse_many_files(
434 const char *conf_file,
436 const char *sections,
437 ConfigItemLookup lookup,
439 ConfigParseFlags flags,
446 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
451 STRV_FOREACH(fn, files) {
452 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
460 /* Parse each config file in the directories specified as nulstr. */
461 int config_parse_many_nulstr(
462 const char *conf_file,
463 const char *conf_file_dirs,
464 const char *sections,
465 ConfigItemLookup lookup,
467 ConfigParseFlags flags,
470 _cleanup_strv_free_ char **files = NULL;
473 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
477 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
480 #if 0 /// UNNEEDED by elogind
481 /* Parse each config file in the directories specified as strv. */
482 int config_parse_many(
483 const char *conf_file,
484 const char* const* conf_file_dirs,
485 const char *dropin_dirname,
486 const char *sections,
487 ConfigItemLookup lookup,
489 ConfigParseFlags flags,
492 _cleanup_strv_free_ char **dropin_dirs = NULL;
493 _cleanup_strv_free_ char **files = NULL;
497 suffix = strjoina("/", dropin_dirname);
498 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
502 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
506 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
510 #define DEFINE_PARSER(type, vartype, conv_func) \
511 int config_parse_##type( \
513 const char *filename, \
515 const char *section, \
516 unsigned section_line, \
517 const char *lvalue, \
519 const char *rvalue, \
531 r = conv_func(rvalue, i); \
533 log_syntax(unit, LOG_ERR, filename, line, r, \
534 "Failed to parse %s value, ignoring: %s", \
540 DEFINE_PARSER(int, int, safe_atoi);
541 DEFINE_PARSER(long, long, safe_atoli);
542 #if 0 /// UNNEEDED by elogind
543 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
544 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
545 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
547 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
548 DEFINE_PARSER(unsigned, unsigned, safe_atou);
549 DEFINE_PARSER(double, double, safe_atod);
550 #if 0 /// UNNEEDED by elogind
551 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
553 DEFINE_PARSER(sec, usec_t, parse_sec);
554 DEFINE_PARSER(mode, mode_t, parse_mode);
556 int config_parse_iec_size(const char* unit,
557 const char *filename,
560 unsigned section_line,
576 r = parse_size(rvalue, 1024, &v);
577 if (r < 0 || (uint64_t) (size_t) v != v) {
578 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
586 #if 0 /// UNNEEDED by elogind
587 int config_parse_si_size(
589 const char *filename,
592 unsigned section_line,
608 r = parse_size(rvalue, 1000, &v);
609 if (r < 0 || (uint64_t) (size_t) v != v) {
610 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
618 int config_parse_iec_uint64(
620 const char *filename,
623 unsigned section_line,
630 uint64_t *bytes = data;
638 r = parse_size(rvalue, 1024, bytes);
640 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
646 int config_parse_bool(const char* unit,
647 const char *filename,
650 unsigned section_line,
666 k = parse_boolean(rvalue);
668 log_syntax(unit, LOG_ERR, filename, line, k,
669 "Failed to parse boolean value%s: %s",
670 fatal ? "" : ", ignoring", rvalue);
671 return fatal ? -ENOEXEC : 0;
678 #if 0 /// UNNEEDED by elogind
679 int config_parse_tristate(
681 const char *filename,
684 unsigned section_line,
698 /* A tristate is pretty much a boolean, except that it can
699 * also take the special value -1, indicating "uninitialized",
700 * much like NULL is for a pointer type. */
702 k = parse_boolean(rvalue);
704 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
713 int config_parse_string(
715 const char *filename,
718 unsigned section_line,
732 if (!utf8_is_valid(rvalue)) {
733 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
751 int config_parse_path(
753 const char *filename,
756 unsigned section_line,
771 if (isempty(rvalue)) {
776 if (!utf8_is_valid(rvalue)) {
777 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
778 return fatal ? -ENOEXEC : 0;
781 if (!path_is_absolute(rvalue)) {
782 log_syntax(unit, LOG_ERR, filename, line, 0,
783 "Not an absolute path%s: %s",
784 fatal ? "" : ", ignoring", rvalue);
785 return fatal ? -ENOEXEC : 0;
792 path_kill_slashes(n);
801 int config_parse_strv(
803 const char *filename,
806 unsigned section_line,
821 if (isempty(rvalue)) {
822 *sv = strv_free(*sv);
829 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
835 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
839 if (!utf8_is_valid(word)) {
840 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
845 r = strv_consume(sv, word);
853 int config_parse_warn_compat(
855 const char *filename,
858 unsigned section_line,
864 Disabled reason = ltype;
867 case DISABLED_CONFIGURATION:
868 log_syntax(unit, LOG_DEBUG, filename, line, 0,
869 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
871 case DISABLED_LEGACY:
872 log_syntax(unit, LOG_INFO, filename, line, 0,
873 "Support for option %s= has been removed and it is ignored", lvalue);
875 case DISABLED_EXPERIMENTAL:
876 log_syntax(unit, LOG_INFO, filename, line, 0,
877 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
884 #if 0 /// UNNEEDED by elogind
885 int config_parse_log_facility(
887 const char *filename,
890 unsigned section_line,
904 x = log_facility_unshifted_from_string(rvalue);
906 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
910 *o = (x << 3) | LOG_PRI(*o);
916 int config_parse_log_level(
918 const char *filename,
921 unsigned section_line,
935 x = log_level_from_string(rvalue);
937 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
941 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
944 *o = (*o & LOG_FACMASK) | x;
949 int config_parse_signal(
951 const char *filename,
954 unsigned section_line,
968 r = signal_from_string(rvalue);
970 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
978 #if 0 /// UNNEEDED by elogind
979 int config_parse_personality(
981 const char *filename,
984 unsigned section_line,
991 unsigned long *personality = data, p;
999 p = PERSONALITY_INVALID;
1001 p = personality_from_string(rvalue);
1002 if (p == PERSONALITY_INVALID) {
1003 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
1012 int config_parse_ifname(
1014 const char *filename,
1016 const char *section,
1017 unsigned section_line,
1032 if (isempty(rvalue)) {
1037 if (!ifname_valid(rvalue)) {
1038 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1042 r = free_and_strdup(s, rvalue);
1049 int config_parse_ip_port(
1051 const char *filename,
1053 const char *section,
1054 unsigned section_line,
1070 if (isempty(rvalue)) {
1075 r = parse_ip_port(rvalue, &port);
1077 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1086 int config_parse_join_controllers(
1088 const char *filename,
1090 const char *section,
1091 unsigned section_line,
1098 char ****ret = data;
1099 const char *whole_rvalue = rvalue;
1101 _cleanup_(strv_free_freep) char ***controllers = NULL;
1109 _cleanup_free_ char *word = NULL;
1113 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1115 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1121 l = strv_split(word, ",");
1126 if (strv_length(l) <= 1) {
1132 controllers = new(char**, 2);
1139 controllers[1] = NULL;
1146 t = new0(char**, n+2);
1154 for (a = controllers; *a; a++)
1155 if (strv_overlap(*a, l)) {
1156 if (strv_extend_strv(&l, *a, false) < 0) {
1175 t[n++] = strv_uniq(l);
1177 strv_free_free(controllers);
1181 if (!isempty(rvalue))
1182 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1184 /* As a special case, return a single empty strv, to override the default */
1186 controllers = new(char**, 2);
1189 controllers[0] = strv_new(NULL, NULL);
1190 if (!controllers[0])
1192 controllers[1] = NULL;
1195 strv_free_free(*ret);
1196 *ret = TAKE_PTR(controllers);
1202 int config_parse_mtu(
1204 const char *filename,
1206 const char *section,
1207 unsigned section_line,
1214 uint32_t *mtu = data;
1220 r = parse_mtu(ltype, rvalue, mtu);
1222 log_syntax(unit, LOG_ERR, filename, line, r,
1223 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1224 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1229 log_syntax(unit, LOG_ERR, filename, line, r,
1230 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1237 int config_parse_rlimit(
1239 const char *filename,
1241 const char *section,
1242 unsigned section_line,
1249 struct rlimit **rl = data, d = {};
1255 r = rlimit_parse(ltype, rvalue, &d);
1257 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1261 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1268 rl[ltype] = newdup(struct rlimit, &d, 1);