X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=conf-parser.c;h=2ea6911257db25ab302c5af7b20ffee3dfbeea9b;hp=e50597491ba4a2e6cce8fef6b5a890fdc5aa302d;hb=16354eff99fe673f912862138c4dce30bbd0afb5;hpb=57d42a5f661084af3d460a80112fab5dffd91591 diff --git a/conf-parser.c b/conf-parser.c index e50597491..2ea691125 100644 --- a/conf-parser.c +++ b/conf-parser.c @@ -10,6 +10,7 @@ #include "util.h" #include "macro.h" #include "strv.h" +#include "log.h" #define WHITESPACE " \t\n" #define COMMENTS "#;\n" @@ -44,7 +45,7 @@ static int next_assignment( return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata); } - fprintf(stderr, "[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section)); + log_error("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section)); return -EBADMSG; } @@ -76,7 +77,7 @@ static char *strip(char *s) { } /* Parse a variable assignment line */ -static int parse_line(const char *filename, unsigned line, char **section, const ConfigItem *t, char *l, void *userdata) { +static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) { char *e, *c, *b; b = l+strspn(l, WHITESPACE); @@ -109,7 +110,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const } } - r = config_parse(fn, t, userdata); + r = config_parse(fn, sections, t, userdata); free(path); return r; } @@ -122,13 +123,28 @@ static int parse_line(const char *filename, unsigned line, char **section, const assert(k > 0); if (b[k-1] != ']') { - fprintf(stderr, "[%s:%u] Invalid section header.", filename, line); + log_error("[%s:%u] Invalid section header.", filename, line); return -EBADMSG; } if (!(n = strndup(b+1, k-2))) return -ENOMEM; + if (sections) { + const char * const * i; + bool good = false; + STRV_FOREACH(i, sections) + if (streq(*i, n)) { + good = true; + break; + } + + if (!good) { + free(n); + return -EBADMSG; + } + } + free(*section); *section = n; @@ -136,7 +152,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const } if (!(e = strchr(b, '='))) { - fprintf(stderr, "[%s:%u] Missing '='.", filename, line); + log_error("[%s:%u] Missing '='.", filename, line); return -EBADMSG; } @@ -147,7 +163,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const } /* Go through the file and parse each line */ -int config_parse(const char *filename, const ConfigItem *t, void *userdata) { +int config_parse(const char *filename, const char* const * sections, const ConfigItem *t, void *userdata) { unsigned line = 0; char *section = NULL; FILE *f; @@ -158,7 +174,7 @@ int config_parse(const char *filename, const ConfigItem *t, void *userdata) { if (!(f = fopen(filename, "re"))) { r = -errno; - fprintf(stderr, "Failed to open configuration file '%s': %s", filename, strerror(-r)); + log_error("Failed to open configuration file '%s': %s", filename, strerror(-r)); goto finish; } @@ -170,11 +186,11 @@ int config_parse(const char *filename, const ConfigItem *t, void *userdata) { break; r = -errno; - fprintf(stderr, "Failed to read configuration file '%s': %s", filename, strerror(-r)); + log_error("Failed to read configuration file '%s': %s", filename, strerror(-r)); goto finish; } - if ((r = parse_line(filename, ++line, §ion, t, l, userdata)) < 0) + if ((r = parse_line(filename, ++line, §ion, sections, t, l, userdata)) < 0) goto finish; } @@ -207,7 +223,7 @@ int config_parse_int( assert(data); if ((r = safe_atoi(rvalue, i)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -232,7 +248,7 @@ int config_parse_unsigned( assert(data); if ((r = safe_atou(rvalue, u)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -258,7 +274,7 @@ int config_parse_size( assert(data); if ((r = safe_atou(rvalue, &u)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -284,7 +300,7 @@ int config_parse_bool( assert(data); if ((k = parse_boolean(rvalue)) < 0) { - fprintf(stderr, "[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue); return k; }