conf-parser.c revision 7b3e062cb6872e28ef5a2e069810070e28bbe0cd
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sd-messages.h"
#include "alloc-util.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "fd-util.h"
#include "fs-util.h"
#include "log.h"
#include "macro.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "strv.h"
#include "syslog-util.h"
#include "utf8.h"
#include "util.h"
const void *table,
const char *section,
const char *lvalue,
int *ltype,
void **data,
void *userdata) {
const ConfigTableItem *t;
continue;
continue;
return 1;
}
return 0;
}
const void *table,
const char *section,
const char *lvalue,
int *ltype,
void **data,
void *userdata) {
const ConfigPerfItem *p;
if (!section)
else {
char *key;
if (!key)
return -ENOMEM;
}
if (!p)
return 0;
return 1;
}
/* Run the user supplied parser for an assignment */
static int next_assignment(const char *unit,
const char *filename,
unsigned line,
const void *table,
const char *section,
unsigned section_line,
const char *lvalue,
const char *rvalue,
bool relaxed,
void *userdata) {
int ltype = 0;
int r;
if (r < 0)
return r;
if (r > 0) {
if (func)
return 0;
}
/* Warn about unknown non-extension fields. */
log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
return 0;
}
/* Parse a variable assignment line */
static int parse_line(const char* unit,
const char *filename,
unsigned line,
const char *sections,
const void *table,
bool relaxed,
bool allow_include,
char **section,
unsigned *section_line,
bool *section_ignored,
char *l,
void *userdata) {
char *e;
assert(l);
l = strstrip(l);
if (!*l)
return 0;
return 0;
if (startswith(l, ".include ")) {
/* .includes are a bad idea, we only support them here
* for historical reasons. They create cyclic include
* problems and make it difficult to detect
* configuration file changes with an easy
* stat(). Better approaches, such as .d/ drop-in
* snippets exist.
*
* Support for them should be eventually removed. */
if (!allow_include) {
return 0;
}
if (!fn)
return -ENOMEM;
}
if (*l == '[') {
size_t k;
char *n;
k = strlen(l);
assert(k > 0);
if (l[k-1] != ']') {
return -EBADMSG;
}
if (!n)
return -ENOMEM;
free(n);
*section_line = 0;
*section_ignored = true;
} else {
*section = n;
*section_line = line;
*section_ignored = false;
}
return 0;
}
if (!relaxed && !*section_ignored)
return 0;
}
e = strchr(l, '=');
if (!e) {
return -EINVAL;
}
*e = 0;
e++;
return next_assignment(unit,
line,
*section,
strstrip(l),
strstrip(e),
userdata);
}
/* Go through the file and parse each line */
int config_parse(const char *unit,
const char *filename,
FILE *f,
const char *sections,
const void *table,
bool relaxed,
bool allow_include,
bool warn,
void *userdata) {
unsigned line = 0, section_line = 0;
bool section_ignored = false;
int r;
if (!f) {
if (!f) {
/* Only log on request, except for ENOENT,
* since we return 0 to the caller. */
"Failed to open configuration file '%s': %m", filename);
}
}
while (!feof(f)) {
bool escaped = false;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
return -errno;
}
truncate_nl(l);
if (continuation) {
c = strappend(continuation, l);
if (!c) {
if (warn)
log_oom();
return -ENOMEM;
}
p = c;
} else
p = l;
for (e = p; *e; e++) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
}
if (escaped) {
*(e-1) = ' ';
if (c)
continuation = c;
else {
continuation = strdup(l);
if (!continuation) {
if (warn)
log_oom();
return -ENOMEM;
}
}
continue;
}
r = parse_line(unit,
++line,
§ion,
p,
userdata);
free(c);
if (r < 0) {
if (warn)
log_warning_errno(r, "Failed to parse file '%s': %m",
filename);
return r;
}
}
return 0;
}
/* Parse each config file in the specified directories. */
int config_parse_many(const char *conf_file,
const char *conf_file_dirs,
const char *sections,
const void *table,
bool relaxed,
void *userdata) {
char **fn;
int r;
if (r < 0)
return r;
if (conf_file) {
if (r < 0)
return r;
}
if (r < 0)
return r;
}
return 0;
}
int config_parse_##type( \
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) { \
\
int r; \
\
\
if (r < 0) \
"Failed to parse %s value, ignoring: %s", \
\
return 0; \
} \
DEFINE_PARSER(int, int, safe_atoi);
DEFINE_PARSER(long, long, safe_atoli);
DEFINE_PARSER(unsigned, unsigned, safe_atou);
DEFINE_PARSER(double, double, safe_atod);
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) {
uint64_t v;
int r;
return 0;
}
return 0;
}
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) {
uint64_t v;
int r;
return 0;
}
return 0;
}
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) {
int r;
if (r < 0)
return 0;
}
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) {
int k;
bool *b = data;
k = parse_boolean(rvalue);
if (k < 0) {
log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
return 0;
}
*b = !!k;
return 0;
}
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) {
int k, *t = data;
/* A tristate is pretty much a boolean, except that it can
* also take the special value -1, indicating "uninitialized",
* much like NULL is for a pointer type. */
k = parse_boolean(rvalue);
if (k < 0) {
log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
return 0;
}
*t = !!k;
return 0;
}
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) {
char **s = data, *n;
if (!utf8_is_valid(rvalue)) {
return 0;
}
n = NULL;
else {
if (!n)
return log_oom();
}
free(*s);
*s = n;
return 0;
}
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) {
char **s = data, *n;
if (!utf8_is_valid(rvalue)) {
return 0;
}
if (!path_is_absolute(rvalue)) {
return 0;
}
if (!n)
return log_oom();
free(*s);
*s = n;
return 0;
}
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) {
size_t l;
int r;
char **empty;
/* Empty assignment resets the list. As a special rule
* we actually fill in a real empty array here rather
* than NULL, since some code wants to know if
* something was set at all... */
if (!empty)
return log_oom();
return 0;
}
char *n;
if (!n)
return log_oom();
if (!utf8_is_valid(n)) {
free(n);
continue;
}
r = strv_consume(sv, n);
if (r < 0)
return log_oom();
}
return 0;
}
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) {
int *o = data, x;
if (x < 0) {
return 0;
}
*o = (x << 3) | LOG_PRI(*o);
return 0;
}
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) {
int *o = data, x;
x = log_level_from_string(rvalue);
if (x < 0) {
return 0;
}
*o = (*o & LOG_FACMASK) | x;
return 0;
}
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) {
if (r <= 0) {
return 0;
}
*sig = r;
return 0;
}
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) {
unsigned long *personality = data, p;
if (p == PERSONALITY_INVALID) {
return 0;
}
*personality = p;
return 0;
}