conf-parser.c revision d450b6f2a9dd8a7fb14e9f8f771ddd70de7afc5e
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2010 Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint log_syntax_internal(const char *unit, int level,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *file, unsigned line, const char *func,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *config_file, unsigned config_line,
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *msg = NULL;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "MESSAGE=[%s:%u] %s", config_file, config_line, msg,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "MESSAGE=[%s:%u] %s", config_file, config_line, msg,
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* Run the user supplied parser for an assignment */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic int next_assignment(const char *unit,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering r = lookup(table, section, lvalue, &func, <ype, &data, userdata);
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna return func(unit, filename, line, section, section_line,
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering /* Warn about unknown non-extension fields. */
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering if (!relaxed && !startswith(lvalue, "X-"))
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering "Unknown lvalue '%s' in section '%s'", lvalue, section);
bac3c8eefe23a820caac930d41629cebafbfc7b2Zbigniew Jędrzejewski-Szmek/* Parse a variable assignment line */
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, EBADMSG,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering ".include not allowed here. Ignoring.");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering fn = file_in_same_dir(filename, strstrip(l+9));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, userdata);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (*l == '[') {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EBADMSG,
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek "Invalid section header '%s'", l);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (sections && !nulstr_contains(sections, n)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
ef42202ac8ed27e7ff1fc90ef8bc2590046dff25Zbigniew Jędrzejewski-Szmek "Unknown section '%s'. Ignoring.", n);
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "Assignment outside of section. Ignoring.");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Missing '='.");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* Go through the file and parse each line */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering _cleanup_free_ char *section = NULL, *continuation = NULL;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_error("Failed to open configuration file '%s': %m", filename);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!fgets(l, sizeof(l), f)) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering log_error("Failed to read configuration file '%s': %m", filename);
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering for (e = p; *e; e++) {
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering else if (*e == '\\')
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering#define DEFINE_PARSER(type, vartype, conv_func) \
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering int config_parse_##type(const char *unit, \
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering const char *lvalue, \
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering const char *rvalue, \
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -r, \
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "Failed to parse %s value, ignoring: %s", \
50cfc579280fb42569488079bd2e249e32a27df2Lennart PoetteringDEFINE_PARSER(uint64, uint64_t, safe_atou64)
50cfc579280fb42569488079bd2e249e32a27df2Lennart PoetteringDEFINE_PARSER(unsigned, unsigned, safe_atou)
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poetteringint config_parse_bytes_size(const char* unit,
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -r,
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "Failed to parse byte value, ignoring: %s", rvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint config_parse_bytes_off(const char* unit,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_cc(sizeof(off_t) == sizeof(uint64_t));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_ERR, filename, line, -r,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "Failed to parse bytes value, ignoring: %s", rvalue);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (k < 0) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers "Failed to parse boolean value, ignoring: %s", rvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint config_parse_show_status(const char* unit,
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, -k,
50cfc579280fb42569488079bd2e249e32a27df2Lennart Poettering "Failed to parse show status setting, ignoring: %s", rvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "String is not UTF-8 clean, ignoring assignment: %s", rvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers "Path is not UTF-8 clean, ignoring assignment: %s", rvalue);
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner offset = rvalue[0] == '-' && (streq(lvalue, "InaccessibleDirectories") ||
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (!path_is_absolute(rvalue + offset)) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering "Not an absolute path, ignoring: %s", rvalue);
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
size_t l;
char **empty;
if (!empty)
return log_oom();
_cleanup_free_ char *n;
n = cunescape_length(w, l);
return log_oom();
if (!utf8_is_valid(n)) {
return log_oom();
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
size_t l;
_cleanup_free_ char *n;
int offset;
n = strndup(w, l);
return log_oom();
if (!utf8_is_valid(n)) {
return log_oom();
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char *x = NULL;
errno = 0;
*m = (mode_t) l;
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int *o = data, x;
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int *o = data, x;
*o = (*o & LOG_FACMASK) | x;
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
size_t l;
char *state;
int val;
char *temp;
if (!temp)
return log_oom();
if (val > 0) {
return log_oom();
return log_oom();