1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 /* An abstract parser for simple, line based, shallow configuration
30 * files consisting of variable assignments only. */
32 /* Prototype for a parser for a specific configuration setting */
33 typedef int (*ConfigParserCallback)(const char *unit,
37 unsigned section_line,
44 /* Wraps information for parsing a specific configuration variable, to
45 * be stored in a simple array */
46 typedef struct ConfigTableItem {
47 const char *section; /* Section */
48 const char *lvalue; /* Name of the variable */
49 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
50 int ltype; /* Distinguish different variables passed to the same callback */
51 void *data; /* Where to store the variable's data */
54 /* Wraps information for parsing a specific configuration variable, to
55 * be stored in a gperf perfect hashtable */
56 typedef struct ConfigPerfItem {
57 const char *section_and_lvalue; /* Section + "." + name of the variable */
58 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
59 int ltype; /* Distinguish different variables passed to the same callback */
60 size_t offset; /* Offset where to store data, from the beginning of userdata */
63 /* Prototype for a low-level gperf lookup function */
64 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
66 /* Prototype for a generic high-level lookup function */
67 typedef int (*ConfigItemLookup)(
71 ConfigParserCallback *func,
76 /* Linear table search implementation of ConfigItemLookup, based on
77 * ConfigTableItem arrays */
78 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
80 /* gperf implementation of ConfigItemLookup, based on gperf
81 * ConfigPerfItem tables */
82 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
84 int config_parse(const char *unit,
87 const char *sections, /* nulstr */
88 ConfigItemLookup lookup,
95 int config_parse_many(const char *conf_file, /* possibly NULL */
96 const char *conf_file_dirs, /* nulstr */
97 const char *sections, /* nulstr */
98 ConfigItemLookup lookup,
103 /* Generic parsers */
104 int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105 int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
106 int config_parse_long(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
107 int config_parse_uint64(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
108 int config_parse_double(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
109 int config_parse_iec_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
110 int config_parse_si_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
111 int config_parse_iec_off(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
112 int config_parse_bool(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
113 int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
114 int config_parse_path(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
115 int config_parse_strv(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
116 int config_parse_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
117 int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
118 int config_parse_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
119 int config_parse_log_facility(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
120 int config_parse_log_level(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
122 int log_syntax_internal(
128 const char *config_file,
129 unsigned config_line,
131 const char *format, ...) _printf_(9, 10);
133 #define log_syntax(unit, level, config_file, config_line, error, ...) \
134 log_syntax_internal(unit, level, \
135 __FILE__, __LINE__, __func__, \
136 config_file, config_line, \
139 #define log_invalid_utf8(unit, level, config_file, config_line, error, rvalue) \
141 _cleanup_free_ char *_p = utf8_escape_invalid(rvalue); \
142 log_syntax(unit, level, config_file, config_line, error, \
143 "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \
146 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
147 int function(const char *unit, \
148 const char *filename, \
150 const char *section, \
151 unsigned section_line, \
152 const char *lvalue, \
154 const char *rvalue, \
165 if ((x = name##_from_string(rvalue)) < 0) { \
166 log_syntax(unit, LOG_ERR, filename, line, -x, \
167 msg ", ignoring: %s", rvalue); \
175 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
176 int function(const char *unit, \
177 const char *filename, \
179 const char *section, \
180 unsigned section_line, \
181 const char *lvalue, \
183 const char *rvalue, \
187 type **enums = data, x, *ys; \
188 _cleanup_free_ type *xs = NULL; \
189 const char *word, *state; \
197 xs = new0(type, 1); \
203 FOREACH_WORD(word, l, rvalue, state) { \
204 _cleanup_free_ char *en = NULL; \
207 en = strndup(word, l); \
211 if ((x = name##_from_string(en)) < 0) { \
212 log_syntax(unit, LOG_ERR, filename, line, \
213 -x, msg ", ignoring: %s", en); \
217 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
219 log_syntax(unit, LOG_ERR, filename, \
221 "Duplicate entry, ignoring: %s", \
231 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
237 *(xs + i) = invalid; \