chiark / gitweb /
core: warn when unit files with unsupported options are parsed
[elogind.git] / src / shared / conf-parser.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
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.
14
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.
19
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/>.
22 ***/
23
24 #include <stdio.h>
25 #include <stdbool.h>
26
27 #include "macro.h"
28
29 /* An abstract parser for simple, line based, shallow configuration
30  * files consisting of variable assignments only. */
31
32 /* Prototype for a parser for a specific configuration setting */
33 typedef int (*ConfigParserCallback)(const char *unit,
34                                     const char *filename,
35                                     unsigned line,
36                                     const char *section,
37                                     unsigned section_line,
38                                     const char *lvalue,
39                                     int ltype,
40                                     const char *rvalue,
41                                     void *data,
42                                     void *userdata);
43
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 */
52 } ConfigTableItem;
53
54 /* Wraps information for parsing a specific configuration variable, to
55  * ve srored 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 */
61 } ConfigPerfItem;
62
63 /* Prototype for a low-level gperf lookup function */
64 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
65
66 /* Prototype for a generic high-level lookup function */
67 typedef int (*ConfigItemLookup)(
68                 void *table,
69                 const char *section,
70                 const char *lvalue,
71                 ConfigParserCallback *func,
72                 int *ltype,
73                 void **data,
74                 void *userdata);
75
76 /* Linear table search implementation of ConfigItemLookup, based on
77  * ConfigTableItem arrays */
78 int config_item_table_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
79
80 /* gperf implementation of ConfigItemLookup, based on gperf
81  * ConfigPerfItem tables */
82 int config_item_perf_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
83
84 int config_parse(const char *unit,
85                  const char *filename,
86                  FILE *f,
87                  const char *sections,  /* nulstr */
88                  ConfigItemLookup lookup,
89                  void *table,
90                  bool relaxed,
91                  bool allow_include,
92                  void *userdata);
93
94 /* Generic parsers */
95 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);
96 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);
97 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);
98 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);
99 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);
100 int config_parse_bytes_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);
101 int config_parse_bytes_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);
102 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);
103 int config_parse_show_status(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);
104 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);
105 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);
106 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);
107 int config_parse_path_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);
108 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);
109 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);
110 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);
111 int config_parse_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);
112 int config_parse_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);
113 int config_parse_set_status(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
115 int log_syntax_internal(const char *unit, int level,
116                         const char *file, unsigned line, const char *func,
117                         const char *config_file, unsigned config_line,
118                         int error, const char *format, ...) _printf_(9, 10);
119
120 #define log_syntax(unit, level, config_file, config_line, error, ...)   \
121         log_syntax_internal(unit, level,                                \
122                             __FILE__, __LINE__, __func__,               \
123                             config_file, config_line,                   \
124                             error, __VA_ARGS__)
125
126 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
127         int function(const char *unit,                                  \
128                      const char *filename,                              \
129                      unsigned line,                                     \
130                      const char *section,                               \
131                      unsigned section_line,                             \
132                      const char *lvalue,                                \
133                      int ltype,                                         \
134                      const char *rvalue,                                \
135                      void *data,                                        \
136                      void *userdata) {                                  \
137                                                                         \
138                 type *i = data, x;                                      \
139                                                                         \
140                 assert(filename);                                       \
141                 assert(lvalue);                                         \
142                 assert(rvalue);                                         \
143                 assert(data);                                           \
144                                                                         \
145                 if ((x = name##_from_string(rvalue)) < 0) {             \
146                         log_syntax(unit, LOG_ERR, filename, line, -x,   \
147                                    msg ", ignoring: %s", rvalue);       \
148                         return 0;                                       \
149                 }                                                       \
150                                                                         \
151                 *i = x;                                                 \
152                 return 0;                                               \
153         }
154
155 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg)              \
156         int function(const char *unit,                                         \
157                      const char *filename,                                     \
158                      unsigned line,                                            \
159                      const char *section,                                      \
160                      unsigned section_line,                                    \
161                      const char *lvalue,                                       \
162                      int ltype,                                                \
163                      const char *rvalue,                                       \
164                      void *data,                                               \
165                      void *userdata) {                                         \
166                                                                                \
167                 type **enums = data, *xs, x, *ys;                              \
168                 char *w, *state;                                               \
169                 size_t l, i = 0;                                               \
170                                                                                \
171                 assert(filename);                                              \
172                 assert(lvalue);                                                \
173                 assert(rvalue);                                                \
174                 assert(data);                                                  \
175                                                                                \
176                 xs = new0(type, 1);                                            \
177                 *xs = invalid;                                                 \
178                                                                                \
179                 FOREACH_WORD(w, l, rvalue, state) {                            \
180                         _cleanup_free_ char *en = NULL;                        \
181                                                                                \
182                         en = strndup(w, l);                                    \
183                         if (!en)                                               \
184                                 return -ENOMEM;                                \
185                                                                                \
186                         if ((x = name##_from_string(en)) < 0) {                \
187                                 log_syntax(unit, LOG_ERR, filename, line,      \
188                                        -x, msg ", ignoring: %s", en);          \
189                                 continue;                                      \
190                         }                                                      \
191                                                                                \
192                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
193                                 if (*ys == x) {                                \
194                                         log_syntax(unit, LOG_ERR, filename,    \
195                                               line, -x,                        \
196                                               "Duplicate entry, ignoring: %s", \
197                                               en);                             \
198                                         x = invalid;                           \
199                                 }                                              \
200                         }                                                      \
201                                                                                \
202                         if (x == invalid)                                      \
203                                 continue;                                      \
204                                                                                \
205                         *(xs + i) = x;                                         \
206                         xs = realloc(xs, (++i + 1) * sizeof(type));            \
207                         if (!xs)                                               \
208                                 return -ENOMEM;                                \
209                         *(xs + i) = invalid;                                   \
210                 }                                                              \
211                                                                                \
212                 free(*enums);                                                  \
213                 *enums = xs;                                                   \
214                 return 0;                                                      \
215         }