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"
31 #include "extract-word.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "string-util.h"
42 #include "syslog-util.h"
43 #include "time-util.h"
46 int config_item_table_lookup(
50 ConfigParserCallback *func,
55 const ConfigTableItem *t;
63 for (t = table; t->lvalue; t++) {
65 if (!streq(lvalue, t->lvalue))
68 if (!streq_ptr(section, t->section))
80 int config_item_perf_lookup(
84 ConfigParserCallback *func,
89 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
90 const ConfigPerfItem *p;
99 p = lookup(lvalue, strlen(lvalue));
103 key = strjoin(section, ".", lvalue);
107 p = lookup(key, strlen(key));
116 *data = (uint8_t*) userdata + p->offset;
120 /* Run the user supplied parser for an assignment */
121 static int next_assignment(const char *unit,
122 const char *filename,
124 ConfigItemLookup lookup,
127 unsigned section_line,
133 ConfigParserCallback func = NULL;
144 r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
150 return func(unit, filename, line, section, section_line,
151 lvalue, ltype, rvalue, data, userdata);
156 /* Warn about unknown non-extension fields. */
157 if (!relaxed && !startswith(lvalue, "X-"))
158 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
163 /* Parse a variable assignment line */
164 static int parse_line(const char* unit,
165 const char *filename,
167 const char *sections,
168 ConfigItemLookup lookup,
173 unsigned *section_line,
174 bool *section_ignored,
190 if (strchr(COMMENTS "\n", *l))
193 if (startswith(l, ".include ")) {
194 _cleanup_free_ char *fn = NULL;
196 /* .includes are a bad idea, we only support them here
197 * for historical reasons. They create cyclic include
198 * problems and make it difficult to detect
199 * configuration file changes with an easy
200 * stat(). Better approaches, such as .d/ drop-in
203 * Support for them should be eventually removed. */
205 if (!allow_include) {
206 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
210 fn = file_in_same_dir(filename, strstrip(l+9));
214 return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
225 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
229 n = strndup(l+1, k-2);
233 if (sections && !nulstr_contains(sections, n)) {
235 if (!relaxed && !startswith(n, "X-"))
236 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
239 *section = mfree(*section);
241 *section_ignored = true;
245 *section_line = line;
246 *section_ignored = false;
252 if (sections && !*section) {
254 if (!relaxed && !*section_ignored)
255 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
262 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
269 return next_assignment(unit,
282 /* Go through the file and parse each line */
283 int config_parse(const char *unit,
284 const char *filename,
286 const char *sections,
287 ConfigItemLookup lookup,
294 _cleanup_free_ char *section = NULL, *continuation = NULL;
295 _cleanup_fclose_ FILE *ours = NULL;
296 unsigned line = 0, section_line = 0;
297 bool section_ignored = false, allow_bom = true;
304 f = ours = fopen(filename, "re");
306 /* Only log on request, except for ENOENT,
307 * since we return 0 to the caller. */
308 if (warn || errno == ENOENT)
309 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
310 "Failed to open configuration file '%s': %m", filename);
311 return errno == ENOENT ? 0 : -errno;
315 fd_warn_permissions(filename, fileno(f));
318 char buf[LINE_MAX], *l, *p, *c = NULL, *e;
319 bool escaped = false;
321 if (!fgets(buf, sizeof buf, f)) {
325 return log_error_errno(errno, "Failed to read configuration file '%s': %m", filename);
329 if (allow_bom && startswith(l, UTF8_BYTE_ORDER_MARK))
330 l += strlen(UTF8_BYTE_ORDER_MARK);
336 c = strappend(continuation, l);
343 continuation = mfree(continuation);
348 for (e = p; *e; e++) {
361 continuation = strdup(l);
389 log_warning_errno(r, "Failed to parse file '%s': %m",
398 static int config_parse_many_files(
399 const char *conf_file,
401 const char *sections,
402 ConfigItemLookup lookup,
411 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
416 STRV_FOREACH(fn, files) {
417 r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
425 /* Parse each config file in the directories specified as nulstr. */
426 int config_parse_many_nulstr(
427 const char *conf_file,
428 const char *conf_file_dirs,
429 const char *sections,
430 ConfigItemLookup lookup,
435 _cleanup_strv_free_ char **files = NULL;
438 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
442 return config_parse_many_files(conf_file, files,
443 sections, lookup, table, relaxed, userdata);
446 #if 0 /// UNNEEDED by elogind
447 /* Parse each config file in the directories specified as strv. */
448 int config_parse_many(
449 const char *conf_file,
450 const char* const* conf_file_dirs,
451 const char *dropin_dirname,
452 const char *sections,
453 ConfigItemLookup lookup,
458 _cleanup_strv_free_ char **dropin_dirs = NULL;
459 _cleanup_strv_free_ char **files = NULL;
463 suffix = strjoina("/", dropin_dirname);
464 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
468 r = conf_files_list_strv(&files, ".conf", NULL, (const char* const*) dropin_dirs);
472 return config_parse_many_files(conf_file, files,
473 sections, lookup, table, relaxed, userdata);
477 #define DEFINE_PARSER(type, vartype, conv_func) \
478 int config_parse_##type( \
480 const char *filename, \
482 const char *section, \
483 unsigned section_line, \
484 const char *lvalue, \
486 const char *rvalue, \
498 r = conv_func(rvalue, i); \
500 log_syntax(unit, LOG_ERR, filename, line, r, \
501 "Failed to parse %s value, ignoring: %s", \
506 struct __useless_struct_to_allow_trailing_semicolon__
508 DEFINE_PARSER(int, int, safe_atoi);
509 DEFINE_PARSER(long, long, safe_atoli);
510 #if 0 /// UNNEEDED by elogind
511 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
512 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
513 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
515 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
516 DEFINE_PARSER(unsigned, unsigned, safe_atou);
517 DEFINE_PARSER(double, double, safe_atod);
518 #if 0 /// UNNEEDED by elogind
519 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
521 DEFINE_PARSER(sec, usec_t, parse_sec);
522 DEFINE_PARSER(mode, mode_t, parse_mode);
524 int config_parse_iec_size(const char* unit,
525 const char *filename,
528 unsigned section_line,
544 r = parse_size(rvalue, 1024, &v);
545 if (r < 0 || (uint64_t) (size_t) v != v) {
546 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
554 #if 0 /// UNNEEDED by elogind
555 int config_parse_si_size(const char* unit,
556 const char *filename,
559 unsigned section_line,
575 r = parse_size(rvalue, 1000, &v);
576 if (r < 0 || (uint64_t) (size_t) v != v) {
577 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
585 int config_parse_iec_uint64(const char* unit,
586 const char *filename,
589 unsigned section_line,
596 uint64_t *bytes = data;
604 r = parse_size(rvalue, 1024, bytes);
606 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
612 int config_parse_bool(const char* unit,
613 const char *filename,
616 unsigned section_line,
631 k = parse_boolean(rvalue);
633 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
641 #if 0 /// UNNEEDED by elogind
642 int config_parse_tristate(
644 const char *filename,
647 unsigned section_line,
661 /* A tristate is pretty much a boolean, except that it can
662 * also take the special value -1, indicating "uninitialized",
663 * much like NULL is for a pointer type. */
665 k = parse_boolean(rvalue);
667 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
676 int config_parse_string(
678 const char *filename,
681 unsigned section_line,
695 if (!utf8_is_valid(rvalue)) {
696 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
714 int config_parse_path(
716 const char *filename,
719 unsigned section_line,
733 if (!utf8_is_valid(rvalue)) {
734 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
738 if (!path_is_absolute(rvalue)) {
739 log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute path, ignoring: %s", rvalue);
747 path_kill_slashes(n);
755 int config_parse_strv(const char *unit,
756 const char *filename,
759 unsigned section_line,
774 if (isempty(rvalue)) {
777 /* Empty assignment resets the list. As a special rule
778 * we actually fill in a real empty array here rather
779 * than NULL, since some code wants to know if
780 * something was set at all... */
781 empty = new0(char*, 1);
794 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
800 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
804 if (!utf8_is_valid(word)) {
805 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
809 r = strv_consume(sv, word);
817 #if 0 /// UNNEEDED by elogind
818 int config_parse_log_facility(
820 const char *filename,
823 unsigned section_line,
838 x = log_facility_unshifted_from_string(rvalue);
840 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
844 *o = (x << 3) | LOG_PRI(*o);
850 int config_parse_log_level(
852 const char *filename,
855 unsigned section_line,
870 x = log_level_from_string(rvalue);
872 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
876 *o = (*o & LOG_FACMASK) | x;
880 int config_parse_signal(
882 const char *filename,
885 unsigned section_line,
899 r = signal_from_string_try_harder(rvalue);
901 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
909 #if 0 /// UNNEEDED by elogind
910 int config_parse_personality(
912 const char *filename,
915 unsigned section_line,
922 unsigned long *personality = data, p;
929 p = personality_from_string(rvalue);
930 if (p == PERSONALITY_INVALID) {
931 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
939 int config_parse_ifname(
941 const char *filename,
944 unsigned section_line,
959 if (isempty(rvalue)) {
964 if (!ifname_valid(rvalue)) {
965 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
969 r = free_and_strdup(s, rvalue);
977 int config_parse_ip_port(
979 const char *filename,
982 unsigned section_line,
998 if (isempty(rvalue)) {
1003 r = parse_ip_port(rvalue, &port);
1005 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);