1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
33 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
35 typedef enum ConfigParseFlags
{
36 CONFIG_PARSE_RELAXED
= 1U << 0,
37 CONFIG_PARSE_ALLOW_INCLUDE
= 1U << 1,
38 CONFIG_PARSE_WARN
= 1U << 2,
39 CONFIG_PARSE_REFUSE_BOM
= 1U << 3,
42 /* Prototype for a parser for a specific configuration setting */
43 typedef int (*ConfigParserCallback
)(const char *unit
,
47 unsigned section_line
,
54 /* Wraps information for parsing a specific configuration variable, to
55 * be stored in a simple array */
56 typedef struct ConfigTableItem
{
57 const char *section
; /* Section */
58 const char *lvalue
; /* Name of the variable */
59 ConfigParserCallback parse
; /* Function that is called to parse the variable's value */
60 int ltype
; /* Distinguish different variables passed to the same callback */
61 void *data
; /* Where to store the variable's data */
64 /* Wraps information for parsing a specific configuration variable, to
65 * be stored in a gperf perfect hashtable */
66 typedef struct ConfigPerfItem
{
67 const char *section_and_lvalue
; /* Section + "." + name of the variable */
68 ConfigParserCallback parse
; /* Function that is called to parse the variable's value */
69 int ltype
; /* Distinguish different variables passed to the same callback */
70 size_t offset
; /* Offset where to store data, from the beginning of userdata */
73 /* Prototype for a low-level gperf lookup function */
74 typedef const ConfigPerfItem
* (*ConfigPerfItemLookup
)(const char *section_and_lvalue
, unsigned length
);
76 /* Prototype for a generic high-level lookup function */
77 typedef int (*ConfigItemLookup
)(
81 ConfigParserCallback
*func
,
86 /* Linear table search implementation of ConfigItemLookup, based on
87 * ConfigTableItem arrays */
88 int config_item_table_lookup(const void *table
, const char *section
, const char *lvalue
, ConfigParserCallback
*func
, int *ltype
, void **data
, void *userdata
);
90 /* gperf implementation of ConfigItemLookup, based on gperf
91 * ConfigPerfItem tables */
92 int config_item_perf_lookup(const void *table
, const char *section
, const char *lvalue
, ConfigParserCallback
*func
, int *ltype
, void **data
, void *userdata
);
98 const char *sections
, /* nulstr */
99 ConfigItemLookup lookup
,
101 ConfigParseFlags flags
,
104 int config_parse_many_nulstr(
105 const char *conf_file
, /* possibly NULL */
106 const char *conf_file_dirs
, /* nulstr */
107 const char *sections
, /* nulstr */
108 ConfigItemLookup lookup
,
110 ConfigParseFlags flags
,
113 int config_parse_many(
114 const char *conf_file
, /* possibly NULL */
115 const char* const* conf_file_dirs
,
116 const char *dropin_dirname
,
117 const char *sections
, /* nulstr */
118 ConfigItemLookup lookup
,
120 ConfigParseFlags flags
,
123 /* Generic parsers */
124 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
);
125 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
);
126 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
);
127 int config_parse_uint8(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
);
128 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
);
129 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
);
130 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
);
131 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
);
132 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
);
133 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
);
134 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
);
135 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
);
136 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
);
137 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
);
138 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
);
139 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
);
140 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
);
141 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 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
);
144 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
);
145 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
);
146 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
);
147 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
);
148 int config_parse_ip_port(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 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
151 int function(const char *unit, \
152 const char *filename, \
154 const char *section, \
155 unsigned section_line, \
156 const char *lvalue, \
158 const char *rvalue, \
169 if ((x = name##_from_string(rvalue)) < 0) { \
170 log_syntax(unit, LOG_ERR, filename, line, -x, \
171 msg ", ignoring: %s", rvalue); \
179 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
180 int function(const char *unit, \
181 const char *filename, \
183 const char *section, \
184 unsigned section_line, \
185 const char *lvalue, \
187 const char *rvalue, \
191 type **enums = data, x, *ys; \
192 _cleanup_free_ type *xs = NULL; \
193 const char *word, *state; \
201 xs = new0(type, 1); \
207 FOREACH_WORD(word, l, rvalue, state) { \
208 _cleanup_free_ char *en = NULL; \
211 en = strndup(word, l); \
215 if ((x = name##_from_string(en)) < 0) { \
216 log_syntax(unit, LOG_ERR, filename, line, \
217 -x, msg ", ignoring: %s", en); \
221 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
223 log_syntax(unit, LOG_ERR, filename, \
225 "Duplicate entry, ignoring: %s", \
235 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
241 *(xs + i) = invalid; \