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"
53 int config_item_table_lookup(
57 ConfigParserCallback *func,
62 const ConfigTableItem *t;
70 for (t = table; t->lvalue; t++) {
72 if (!streq(lvalue, t->lvalue))
75 if (!streq_ptr(section, t->section))
87 int config_item_perf_lookup(
91 ConfigParserCallback *func,
96 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
97 const ConfigPerfItem *p;
106 p = lookup(lvalue, strlen(lvalue));
110 key = strjoin(section, ".", lvalue);
114 p = lookup(key, strlen(key));
123 *data = (uint8_t*) userdata + p->offset;
127 /* Run the user supplied parser for an assignment */
128 static int next_assignment(
130 const char *filename,
132 ConfigItemLookup lookup,
135 unsigned section_line,
138 ConfigParseFlags flags,
141 ConfigParserCallback func = NULL;
152 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
158 return func(unit, filename, line, section, section_line,
159 lvalue, ltype, rvalue, data, userdata);
164 /* Warn about unknown non-extension fields. */
165 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
166 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
171 /* Parse a single logical line */
172 static int parse_line(
174 const char *filename,
176 const char *sections,
177 ConfigItemLookup lookup,
179 ConfigParseFlags flags,
181 unsigned *section_line,
182 bool *section_ignored,
197 if (strchr(COMMENTS "\n", *l))
200 include = first_word(l, ".include");
202 _cleanup_free_ char *fn = NULL;
204 /* .includes are a bad idea, we only support them here
205 * for historical reasons. They create cyclic include
206 * problems and make it difficult to detect
207 * configuration file changes with an easy
208 * stat(). Better approaches, such as .d/ drop-in
211 * Support for them should be eventually removed. */
213 if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
214 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
218 log_syntax(unit, LOG_WARNING, filename, line, 0,
219 ".include directives are deprecated, and support for them will be removed in a future version of elogind. "
220 "Please use drop-in files instead.");
222 fn = file_in_same_dir(filename, strstrip(include));
226 return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
229 if (!utf8_is_valid(l))
230 return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l);
240 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
244 n = strndup(l+1, k-2);
248 if (sections && !nulstr_contains(sections, n)) {
250 if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
251 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
254 *section = mfree(*section);
256 *section_ignored = true;
258 free_and_replace(*section, n);
259 *section_line = line;
260 *section_ignored = false;
266 if (sections && !*section) {
268 if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
269 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
276 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
283 return next_assignment(unit,
296 /* Go through the file and parse each line */
297 int config_parse(const char *unit,
298 const char *filename,
300 const char *sections,
301 ConfigItemLookup lookup,
303 ConfigParseFlags flags,
306 _cleanup_free_ char *section = NULL, *continuation = NULL;
307 _cleanup_fclose_ FILE *ours = NULL;
308 unsigned line = 0, section_line = 0;
309 bool section_ignored = false;
316 f = ours = fopen(filename, "re");
318 /* Only log on request, except for ENOENT,
319 * since we return 0 to the caller. */
320 if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
321 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
322 "Failed to open configuration file '%s': %m", filename);
323 return errno == ENOENT ? 0 : -errno;
327 fd_warn_permissions(filename, fileno(f));
330 _cleanup_free_ char *buf = NULL;
331 bool escaped = false;
334 r = read_line(f, LONG_LINE_MAX, &buf);
338 if (flags & CONFIG_PARSE_WARN)
339 log_error_errno(r, "%s:%u: Line too long", filename, line);
344 if (CONFIG_PARSE_WARN)
345 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
351 if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
354 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
357 flags |= CONFIG_PARSE_REFUSE_BOM;
362 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
363 if (flags & CONFIG_PARSE_WARN)
364 log_error("%s:%u: Continuation line too long", filename, line);
368 if (!strextend(&continuation, l, NULL)) {
369 if (flags & CONFIG_PARSE_WARN)
378 for (e = p; *e; e++) {
389 continuation = strdup(l);
391 if (flags & CONFIG_PARSE_WARN)
413 if (flags & CONFIG_PARSE_WARN)
414 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
418 continuation = mfree(continuation);
435 if (flags & CONFIG_PARSE_WARN)
436 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
444 static int config_parse_many_files(
445 const char *conf_file,
447 const char *sections,
448 ConfigItemLookup lookup,
450 ConfigParseFlags flags,
457 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
462 STRV_FOREACH(fn, files) {
463 r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
471 /* Parse each config file in the directories specified as nulstr. */
472 int config_parse_many_nulstr(
473 const char *conf_file,
474 const char *conf_file_dirs,
475 const char *sections,
476 ConfigItemLookup lookup,
478 ConfigParseFlags flags,
481 _cleanup_strv_free_ char **files = NULL;
484 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
488 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
491 #if 0 /// UNNEEDED by elogind
492 /* Parse each config file in the directories specified as strv. */
493 int config_parse_many(
494 const char *conf_file,
495 const char* const* conf_file_dirs,
496 const char *dropin_dirname,
497 const char *sections,
498 ConfigItemLookup lookup,
500 ConfigParseFlags flags,
503 _cleanup_strv_free_ char **dropin_dirs = NULL;
504 _cleanup_strv_free_ char **files = NULL;
508 suffix = strjoina("/", dropin_dirname);
509 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
513 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
517 return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
521 #define DEFINE_PARSER(type, vartype, conv_func) \
522 DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
524 DEFINE_PARSER(int, int, safe_atoi);
525 DEFINE_PARSER(long, long, safe_atoli);
526 #if 0 /// UNNEEDED by elogind
527 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
528 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
529 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
531 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
532 DEFINE_PARSER(unsigned, unsigned, safe_atou);
533 DEFINE_PARSER(double, double, safe_atod);
534 #if 0 /// UNNEEDED by elogind
535 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
537 DEFINE_PARSER(sec, usec_t, parse_sec);
538 DEFINE_PARSER(mode, mode_t, parse_mode);
540 int config_parse_iec_size(const char* unit,
541 const char *filename,
544 unsigned section_line,
560 r = parse_size(rvalue, 1024, &v);
561 if (r < 0 || (uint64_t) (size_t) v != v) {
562 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
570 #if 0 /// UNNEEDED by elogind
571 int config_parse_si_size(
573 const char *filename,
576 unsigned section_line,
592 r = parse_size(rvalue, 1000, &v);
593 if (r < 0 || (uint64_t) (size_t) v != v) {
594 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
602 int config_parse_iec_uint64(
604 const char *filename,
607 unsigned section_line,
614 uint64_t *bytes = data;
622 r = parse_size(rvalue, 1024, bytes);
624 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
630 int config_parse_bool(const char* unit,
631 const char *filename,
634 unsigned section_line,
650 k = parse_boolean(rvalue);
652 log_syntax(unit, LOG_ERR, filename, line, k,
653 "Failed to parse boolean value%s: %s",
654 fatal ? "" : ", ignoring", rvalue);
655 return fatal ? -ENOEXEC : 0;
662 #if 0 /// UNNEEDED by elogind
663 int config_parse_tristate(
665 const char *filename,
668 unsigned section_line,
682 /* A tristate is pretty much a boolean, except that it can
683 * also take the special value -1, indicating "uninitialized",
684 * much like NULL is for a pointer type. */
686 k = parse_boolean(rvalue);
688 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
697 int config_parse_string(
699 const char *filename,
702 unsigned section_line,
716 if (free_and_strdup(s, empty_to_null(rvalue)) < 0)
722 int config_parse_path(
724 const char *filename,
727 unsigned section_line,
734 _cleanup_free_ char *n = NULL;
744 if (isempty(rvalue)) {
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 free_and_replace(*s, n);
763 int config_parse_strv(
765 const char *filename,
768 unsigned section_line,
783 if (isempty(rvalue)) {
784 *sv = strv_free(*sv);
791 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
797 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
801 r = strv_consume(sv, word);
809 int config_parse_warn_compat(
811 const char *filename,
814 unsigned section_line,
821 Disabled reason = ltype;
825 case DISABLED_CONFIGURATION:
826 log_syntax(unit, LOG_DEBUG, filename, line, 0,
827 "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
830 case DISABLED_LEGACY:
831 log_syntax(unit, LOG_INFO, filename, line, 0,
832 "Support for option %s= has been removed and it is ignored", lvalue);
835 case DISABLED_EXPERIMENTAL:
836 log_syntax(unit, LOG_INFO, filename, line, 0,
837 "Support for option %s= has not yet been enabled and it is ignored", lvalue);
844 #if 0 /// UNNEEDED by elogind
845 int config_parse_log_facility(
847 const char *filename,
850 unsigned section_line,
864 x = log_facility_unshifted_from_string(rvalue);
866 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
870 *o = (x << 3) | LOG_PRI(*o);
876 int config_parse_log_level(
878 const char *filename,
881 unsigned section_line,
895 x = log_level_from_string(rvalue);
897 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
901 if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
904 *o = (*o & LOG_FACMASK) | x;
909 int config_parse_signal(
911 const char *filename,
914 unsigned section_line,
928 r = signal_from_string(rvalue);
930 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
938 #if 0 /// UNNEEDED by elogind
939 int config_parse_personality(
941 const char *filename,
944 unsigned section_line,
951 unsigned long *personality = data, p;
959 p = PERSONALITY_INVALID;
961 p = personality_from_string(rvalue);
962 if (p == PERSONALITY_INVALID) {
963 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
972 int config_parse_ifname(
974 const char *filename,
977 unsigned section_line,
992 if (isempty(rvalue)) {
997 if (!ifname_valid(rvalue)) {
998 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
1002 r = free_and_strdup(s, rvalue);
1009 int config_parse_ip_port(
1011 const char *filename,
1013 const char *section,
1014 unsigned section_line,
1030 if (isempty(rvalue)) {
1035 r = parse_ip_port(rvalue, &port);
1037 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
1046 int config_parse_join_controllers(
1048 const char *filename,
1050 const char *section,
1051 unsigned section_line,
1058 char ****ret = data;
1059 const char *whole_rvalue = rvalue;
1061 _cleanup_(strv_free_freep) char ***controllers = NULL;
1069 _cleanup_free_ char *word = NULL;
1073 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
1075 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
1081 l = strv_split(word, ",");
1086 if (strv_length(l) <= 1) {
1092 controllers = new(char**, 2);
1099 controllers[1] = NULL;
1106 t = new0(char**, n+2);
1114 for (a = controllers; *a; a++)
1115 if (strv_overlap(*a, l)) {
1116 if (strv_extend_strv(&l, *a, false) < 0) {
1135 t[n++] = strv_uniq(l);
1137 strv_free_free(controllers);
1141 if (!isempty(rvalue))
1142 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
1144 /* As a special case, return a single empty strv, to override the default */
1146 controllers = new(char**, 2);
1149 controllers[0] = strv_new(NULL, NULL);
1150 if (!controllers[0])
1152 controllers[1] = NULL;
1155 strv_free_free(*ret);
1156 *ret = TAKE_PTR(controllers);
1162 int config_parse_mtu(
1164 const char *filename,
1166 const char *section,
1167 unsigned section_line,
1174 uint32_t *mtu = data;
1180 r = parse_mtu(ltype, rvalue, mtu);
1182 log_syntax(unit, LOG_ERR, filename, line, r,
1183 "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
1184 (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
1189 log_syntax(unit, LOG_ERR, filename, line, r,
1190 "Failed to parse MTU value '%s', ignoring: %m", rvalue);
1197 int config_parse_rlimit(
1199 const char *filename,
1201 const char *section,
1202 unsigned section_line,
1209 struct rlimit **rl = data, d = {};
1215 r = rlimit_parse(ltype, rvalue, &d);
1217 log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
1221 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
1228 rl[ltype] = newdup(struct rlimit, &d, 1);
1236 int config_parse_permille(const char* unit,
1237 const char *filename,
1239 const char *section,
1240 unsigned section_line,
1247 unsigned *permille = data;
1255 r = parse_permille(rvalue);
1257 log_syntax(unit, LOG_ERR, filename, line, r,
1258 "Failed to parse permille value, ignoring: %s", rvalue);
1262 *permille = (unsigned) r;