2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
28 #include "alloc-util.h"
29 #include "conf-files.h"
30 #include "conf-parser.h"
32 #include "extract-word.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "process-util.h"
41 #include "signal-util.h"
42 #include "string-util.h"
44 #include "syslog-util.h"
45 #include "time-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(const char *unit,
124 const char *filename,
126 ConfigItemLookup lookup,
129 unsigned section_line,
135 ConfigParserCallback func = NULL;
146 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
152 return func(unit, filename, line, section, section_line,
153 lvalue, ltype, rvalue, data, userdata);
158 /* Warn about unknown non-extension fields. */
159 if (!relaxed && !startswith(lvalue, "X-"))
160 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
165 /* Parse a variable assignment line */
166 static int parse_line(const char* unit,
167 const char *filename,
169 const char *sections,
170 ConfigItemLookup lookup,
175 unsigned *section_line,
176 bool *section_ignored,
192 if (strchr(COMMENTS "\n", *l))
195 if (startswith(l, ".include ")) {
196 _cleanup_free_ char *fn = NULL;
198 /* .includes are a bad idea, we only support them here
199 * for historical reasons. They create cyclic include
200 * problems and make it difficult to detect
201 * configuration file changes with an easy
202 * stat(). Better approaches, such as .d/ drop-in
205 * Support for them should be eventually removed. */
207 if (!allow_include) {
208 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
212 fn = file_in_same_dir(filename, strstrip(l+9));
216 return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
227 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
231 n = strndup(l+1, k-2);
235 if (sections && !nulstr_contains(sections, n)) {
237 if (!relaxed && !startswith(n, "X-"))
238 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
241 *section = mfree(*section);
243 *section_ignored = true;
247 *section_line = line;
248 *section_ignored = false;
254 if (sections && !*section) {
256 if (!relaxed && !*section_ignored)
257 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
264 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
271 return next_assignment(unit,
284 /* Go through the file and parse each line */
285 int config_parse(const char *unit,
286 const char *filename,
288 const char *sections,
289 ConfigItemLookup lookup,
296 _cleanup_free_ char *section = NULL, *continuation = NULL;
297 _cleanup_fclose_ FILE *ours = NULL;
298 unsigned line = 0, section_line = 0;
299 bool section_ignored = false, allow_bom = true;
306 f = ours = fopen(filename, "re");
308 /* Only log on request, except for ENOENT,
309 * since we return 0 to the caller. */
310 if (warn || errno == ENOENT)
311 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
312 "Failed to open configuration file '%s': %m", filename);
313 return errno == ENOENT ? 0 : -errno;
317 fd_warn_permissions(filename, fileno(f));
320 _cleanup_free_ char *buf = NULL;
321 char *l, *p, *c = NULL, *e;
322 bool escaped = false;
324 r = read_line(f, LONG_LINE_MAX, &buf);
329 log_error_errno(r, "%s:%u: Line too long", filename, line);
335 log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
344 q = startswith(buf, UTF8_BYTE_ORDER_MARK);
352 if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
354 log_error("%s:%u: Continuation line too long", filename, line);
358 c = strappend(continuation, l);
365 continuation = mfree(continuation);
370 for (e = p; *e; e++) {
383 continuation = strdup(l);
411 log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
419 static int config_parse_many_files(
420 const char *conf_file,
422 const char *sections,
423 ConfigItemLookup lookup,
432 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
437 STRV_FOREACH(fn, files) {
438 r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
446 /* Parse each config file in the directories specified as nulstr. */
447 int config_parse_many_nulstr(
448 const char *conf_file,
449 const char *conf_file_dirs,
450 const char *sections,
451 ConfigItemLookup lookup,
456 _cleanup_strv_free_ char **files = NULL;
459 r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
463 return config_parse_many_files(conf_file, files,
464 sections, lookup, table, relaxed, userdata);
467 #if 0 /// UNNEEDED by elogind
468 /* Parse each config file in the directories specified as strv. */
469 int config_parse_many(
470 const char *conf_file,
471 const char* const* conf_file_dirs,
472 const char *dropin_dirname,
473 const char *sections,
474 ConfigItemLookup lookup,
479 _cleanup_strv_free_ char **dropin_dirs = NULL;
480 _cleanup_strv_free_ char **files = NULL;
484 suffix = strjoina("/", dropin_dirname);
485 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
489 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char* const*) dropin_dirs);
493 return config_parse_many_files(conf_file, files,
494 sections, lookup, table, relaxed, userdata);
498 #define DEFINE_PARSER(type, vartype, conv_func) \
499 int config_parse_##type( \
501 const char *filename, \
503 const char *section, \
504 unsigned section_line, \
505 const char *lvalue, \
507 const char *rvalue, \
519 r = conv_func(rvalue, i); \
521 log_syntax(unit, LOG_ERR, filename, line, r, \
522 "Failed to parse %s value, ignoring: %s", \
527 struct __useless_struct_to_allow_trailing_semicolon__
529 DEFINE_PARSER(int, int, safe_atoi);
530 DEFINE_PARSER(long, long, safe_atoli);
531 #if 0 /// UNNEEDED by elogind
532 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
533 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
534 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
536 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
537 DEFINE_PARSER(unsigned, unsigned, safe_atou);
538 DEFINE_PARSER(double, double, safe_atod);
539 #if 0 /// UNNEEDED by elogind
540 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
542 DEFINE_PARSER(sec, usec_t, parse_sec);
543 DEFINE_PARSER(mode, mode_t, parse_mode);
545 int config_parse_iec_size(const char* unit,
546 const char *filename,
549 unsigned section_line,
565 r = parse_size(rvalue, 1024, &v);
566 if (r < 0 || (uint64_t) (size_t) v != v) {
567 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
575 #if 0 /// UNNEEDED by elogind
576 int config_parse_si_size(const char* unit,
577 const char *filename,
580 unsigned section_line,
596 r = parse_size(rvalue, 1000, &v);
597 if (r < 0 || (uint64_t) (size_t) v != v) {
598 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
606 int config_parse_iec_uint64(const char* unit,
607 const char *filename,
610 unsigned section_line,
617 uint64_t *bytes = data;
625 r = parse_size(rvalue, 1024, bytes);
627 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
633 int config_parse_bool(const char* unit,
634 const char *filename,
637 unsigned section_line,
653 k = parse_boolean(rvalue);
655 log_syntax(unit, LOG_ERR, filename, line, k,
656 "Failed to parse boolean value%s: %s",
657 fatal ? "" : ", ignoring", rvalue);
658 return fatal ? -ENOEXEC : 0;
665 #if 0 /// UNNEEDED by elogind
666 int config_parse_tristate(
668 const char *filename,
671 unsigned section_line,
685 /* A tristate is pretty much a boolean, except that it can
686 * also take the special value -1, indicating "uninitialized",
687 * much like NULL is for a pointer type. */
689 k = parse_boolean(rvalue);
691 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
700 int config_parse_string(
702 const char *filename,
705 unsigned section_line,
719 if (!utf8_is_valid(rvalue)) {
720 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
738 int config_parse_path(
740 const char *filename,
743 unsigned section_line,
758 if (!utf8_is_valid(rvalue)) {
759 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
760 return fatal ? -ENOEXEC : 0;
763 if (!path_is_absolute(rvalue)) {
764 log_syntax(unit, LOG_ERR, filename, line, 0,
765 "Not an absolute path%s: %s",
766 fatal ? "" : ", ignoring", rvalue);
767 return fatal ? -ENOEXEC : 0;
774 path_kill_slashes(n);
782 int config_parse_strv(
784 const char *filename,
787 unsigned section_line,
802 if (isempty(rvalue)) {
803 *sv = strv_free(*sv);
810 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
816 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
820 if (!utf8_is_valid(word)) {
821 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
826 r = strv_consume(sv, word);
834 #if 0 /// UNNEEDED by elogind
835 int config_parse_log_facility(
837 const char *filename,
840 unsigned section_line,
855 x = log_facility_unshifted_from_string(rvalue);
857 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
861 *o = (x << 3) | LOG_PRI(*o);
867 int config_parse_log_level(
869 const char *filename,
872 unsigned section_line,
887 x = log_level_from_string(rvalue);
889 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
893 *o = (*o & LOG_FACMASK) | x;
897 int config_parse_signal(
899 const char *filename,
902 unsigned section_line,
916 r = signal_from_string_try_harder(rvalue);
918 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
926 #if 0 /// UNNEEDED by elogind
927 int config_parse_personality(
929 const char *filename,
932 unsigned section_line,
939 unsigned long *personality = data, p;
947 p = PERSONALITY_INVALID;
949 p = personality_from_string(rvalue);
950 if (p == PERSONALITY_INVALID) {
951 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
960 int config_parse_ifname(
962 const char *filename,
965 unsigned section_line,
980 if (isempty(rvalue)) {
985 if (!ifname_valid(rvalue)) {
986 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
990 r = free_and_strdup(s, rvalue);
997 int config_parse_ip_port(
999 const char *filename,
1001 const char *section,
1002 unsigned section_line,
1018 if (isempty(rvalue)) {
1023 r = parse_ip_port(rvalue, &port);
1025 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);