4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
32 /* An abstract parser for simple, line based, shallow configuration
33 * files consisting of variable assignments only. */
35 /* Prototype for a parser for a specific configuration setting */
36 typedef int (*ConfigParserCallback)(const char *unit,
40 unsigned section_line,
47 /* Wraps information for parsing a specific configuration variable, to
48 * be stored in a simple array */
49 typedef struct ConfigTableItem {
50 const char *section; /* Section */
51 const char *lvalue; /* Name of the variable */
52 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
53 int ltype; /* Distinguish different variables passed to the same callback */
54 void *data; /* Where to store the variable's data */
57 /* Wraps information for parsing a specific configuration variable, to
58 * be stored in a gperf perfect hashtable */
59 typedef struct ConfigPerfItem {
60 const char *section_and_lvalue; /* Section + "." + name of the variable */
61 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
62 int ltype; /* Distinguish different variables passed to the same callback */
63 size_t offset; /* Offset where to store data, from the beginning of userdata */
66 /* Prototype for a low-level gperf lookup function */
67 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
69 /* Prototype for a generic high-level lookup function */
70 typedef int (*ConfigItemLookup)(
74 ConfigParserCallback *func,
79 /* Linear table search implementation of ConfigItemLookup, based on
80 * ConfigTableItem arrays */
81 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
83 /* gperf implementation of ConfigItemLookup, based on gperf
84 * ConfigPerfItem tables */
85 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
91 const char *sections, /* nulstr */
92 ConfigItemLookup lookup,
99 int config_parse_many_nulstr(
100 const char *conf_file, /* possibly NULL */
101 const char *conf_file_dirs, /* nulstr */
102 const char *sections, /* nulstr */
103 ConfigItemLookup lookup,
108 int config_parse_many(
109 const char *conf_file, /* possibly NULL */
110 const char* const* conf_file_dirs,
111 const char *dropin_dirname,
112 const char *sections, /* nulstr */
113 ConfigItemLookup lookup,
118 /* Generic parsers */
119 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);
120 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);
121 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);
122 int config_parse_uint16(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);
123 int config_parse_uint32(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);
124 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);
125 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);
126 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);
127 #if 0 /// UNNEEDED by elogind
128 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);
129 int config_parse_iec_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);
131 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);
132 #if 0 /// UNNEEDED by elogind
133 int config_parse_tristate(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);
135 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);
136 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);
137 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);
138 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);
139 #if 0 /// UNNEEDED by elogind
140 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);
142 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);
143 #if 0 /// UNNEEDED by elogind
144 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);
146 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);
147 int config_parse_signal(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);
148 #if 0 /// UNNEEDED by elogind
149 int config_parse_personality(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);
150 int config_parse_ifname(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);
153 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
154 int function(const char *unit, \
155 const char *filename, \
157 const char *section, \
158 unsigned section_line, \
159 const char *lvalue, \
161 const char *rvalue, \
172 if ((x = name##_from_string(rvalue)) < 0) { \
173 log_syntax(unit, LOG_ERR, filename, line, -x, \
174 msg ", ignoring: %s", rvalue); \
182 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
183 int function(const char *unit, \
184 const char *filename, \
186 const char *section, \
187 unsigned section_line, \
188 const char *lvalue, \
190 const char *rvalue, \
194 type **enums = data, x, *ys; \
195 _cleanup_free_ type *xs = NULL; \
196 const char *word, *state; \
204 xs = new0(type, 1); \
210 FOREACH_WORD(word, l, rvalue, state) { \
211 _cleanup_free_ char *en = NULL; \
214 en = strndup(word, l); \
218 if ((x = name##_from_string(en)) < 0) { \
219 log_syntax(unit, LOG_ERR, filename, line, \
220 -x, msg ", ignoring: %s", en); \
224 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
226 log_syntax(unit, LOG_ERR, filename, \
228 "Duplicate entry, ignoring: %s", \
238 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
244 *(xs + i) = invalid; \