X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fconf-parser.c;h=fbacf986a515b3e30ceeb0a1c5fe1c958733d283;hp=65035e4c6a1873daac4688616b05092a7c916032;hb=245802dd89ccf10de446faff5577e041d5372062;hpb=9eb977db5b89b44f254ab40c1876a76b7d7ea2d0 diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 65035e4c6..fbacf986a 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -32,6 +32,8 @@ #include "log.h" #include "utf8.h" #include "path-util.h" +#include "set.h" +#include "exit-status.h" int config_item_table_lookup( void *table, @@ -90,7 +92,7 @@ int config_item_perf_lookup( else { char *key; - key = join(section, ".", lvalue, NULL); + key = strjoin(section, ".", lvalue, NULL); if (!key) return -ENOMEM; @@ -260,23 +262,19 @@ int config_parse( void *userdata) { unsigned line = 0; - char *section = NULL; + char _cleanup_free_ *section = NULL, *continuation = NULL; + FILE _cleanup_fclose_ *ours = NULL; int r; - bool ours = false; - char *continuation = NULL; assert(filename); assert(lookup); if (!f) { - f = fopen(filename, "re"); + f = ours = fopen(filename, "re"); if (!f) { - r = -errno; - log_error("Failed to open configuration file '%s': %s", filename, strerror(-r)); - goto finish; + log_error("Failed to open configuration file '%s': %m", filename); + return -errno; } - - ours = true; } while (!feof(f)) { @@ -287,19 +285,16 @@ int config_parse( if (feof(f)) break; - r = -errno; - log_error("Failed to read configuration file '%s': %s", filename, strerror(-r)); - goto finish; + log_error("Failed to read configuration file '%s': %m", filename); + return -errno; } truncate_nl(l); if (continuation) { c = strappend(continuation, l); - if (!c) { - r = -ENOMEM; - goto finish; - } + if (!c) + return -ENOMEM; free(continuation); continuation = NULL; @@ -321,10 +316,8 @@ int config_parse( continuation = c; else { continuation = strdup(l); - if (!continuation) { - r = -ENOMEM; - goto finish; - } + if (!continuation) + return -ENOMEM; } continue; @@ -342,19 +335,10 @@ int config_parse( free(c); if (r < 0) - goto finish; + return r; } - r = 0; - -finish: - free(section); - free(continuation); - - if (f && ours) - fclose(f); - - return r; + return 0; } int config_parse_int( @@ -375,7 +359,8 @@ int config_parse_int( assert(rvalue); assert(data); - if ((r = safe_atoi(rvalue, i)) < 0) { + r = safe_atoi(rvalue, i); + if (r < 0) { log_error("[%s:%u] Failed to parse numeric value, ingoring: %s", filename, line, rvalue); return 0; } @@ -401,7 +386,8 @@ int config_parse_long( assert(rvalue); assert(data); - if ((r = safe_atoli(rvalue, i)) < 0) { + r = safe_atoli(rvalue, i); + if (r < 0) { log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue); return 0; } @@ -427,7 +413,8 @@ int config_parse_uint64( assert(rvalue); assert(data); - if ((r = safe_atou64(rvalue, u)) < 0) { + r = safe_atou64(rvalue, u); + if (r < 0) { log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue); return 0; } @@ -453,7 +440,35 @@ int config_parse_unsigned( assert(rvalue); assert(data); - if ((r = safe_atou(rvalue, u)) < 0) { + r = safe_atou(rvalue, u); + if (r < 0) { + log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); + return r; + } + + return 0; +} + +int config_parse_double( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + double *d = data; + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + r = safe_atod(rvalue, d); + if (r < 0) { log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue); return r; } @@ -591,9 +606,9 @@ int config_parse_string( assert(rvalue); assert(data); - n = cunescape(rvalue); + n = strdup(rvalue); if (!n) - return -ENOMEM; + return log_oom(); if (!utf8_is_valid(n)) { log_error("[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue); @@ -642,7 +657,7 @@ int config_parse_path( n = strdup(rvalue); if (!n) - return -ENOMEM; + return log_oom(); path_kill_slashes(n); @@ -662,12 +677,8 @@ int config_parse_strv( void *data, void *userdata) { - char*** sv = data; - char **n; - char *w; - unsigned k; + char *** sv = data, *w, *state; size_t l; - char *state; int r; assert(filename); @@ -675,48 +686,40 @@ int config_parse_strv( assert(rvalue); assert(data); - k = strv_length(*sv); - FOREACH_WORD_QUOTED(w, l, rvalue, state) - k++; + if (isempty(rvalue)) { + char **empty; - n = new(char*, k+1); - if (!n) - return -ENOMEM; + /* 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... */ + empty = strv_new(NULL, NULL); + if (!empty) + return log_oom(); - if (*sv) - for (k = 0; (*sv)[k]; k++) - n[k] = (*sv)[k]; - else - k = 0; + strv_free(*sv); + *sv = empty; + return 0; + } FOREACH_WORD_QUOTED(w, l, rvalue, state) { - n[k] = cunescape_length(w, l); - if (!n[k]) { - r = -ENOMEM; - goto fail; - } + _cleanup_free_ char *n; + + n = cunescape_length(w, l); + if (!n) + return log_oom(); - if (!utf8_is_valid(n[k])) { - log_error("[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue); - free(n[k]); + if (!utf8_is_valid(n)) { + log_error("[%s:%u] String is not UTF-8 clean, ignoring: %s", filename, line, rvalue); continue; } - k++; + r = strv_extend(sv, n); + if (r < 0) + return log_oom(); } - n[k] = NULL; - free(*sv); - *sv = n; - return 0; - -fail: - for (; k > 0; k--) - free(n[k-1]); - free(n); - - return r; } int config_parse_path_strv( @@ -729,12 +732,8 @@ int config_parse_path_strv( void *data, void *userdata) { - char*** sv = data; - char **n; - char *w; - unsigned k; + char*** sv = data, *w, *state; size_t l; - char *state; int r; assert(filename); @@ -742,57 +741,65 @@ int config_parse_path_strv( assert(rvalue); assert(data); - k = strv_length(*sv); - FOREACH_WORD_QUOTED(w, l, rvalue, state) - k++; - - n = new(char*, k+1); - if (!n) - return -ENOMEM; - - k = 0; - if (*sv) - for (; (*sv)[k]; k++) - n[k] = (*sv)[k]; + if (isempty(rvalue)) { + /* Empty assignment resets the list */ + strv_free(*sv); + *sv = NULL; + return 0; + } FOREACH_WORD_QUOTED(w, l, rvalue, state) { - n[k] = strndup(w, l); - if (!n[k]) { - r = -ENOMEM; - goto fail; - } + _cleanup_free_ char *n; - if (!utf8_is_valid(n[k])) { + n = strndup(w, l); + if (!n) + return log_oom(); + + if (!utf8_is_valid(n)) { log_error("[%s:%u] Path is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue); - free(n[k]); continue; } - if (!path_is_absolute(n[k])) { + if (!path_is_absolute(n)) { log_error("[%s:%u] Not an absolute path, ignoring: %s", filename, line, rvalue); - free(n[k]); continue; } - path_kill_slashes(n[k]); - k++; + path_kill_slashes(n); + r = strv_extend(sv, n); + if (r < 0) + return log_oom(); } - n[k] = NULL; - free(*sv); - *sv = n; - return 0; +} -fail: - for (; k > 0; k--) - free(n[k-1]); - free(n); +int config_parse_sec( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + usec_t *usec = data; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if (parse_sec(rvalue, usec) < 0) { + log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue); + return 0; + } - return r; + return 0; } -int config_parse_usec( +int config_parse_nsec( const char *filename, unsigned line, const char *section, @@ -802,14 +809,14 @@ int config_parse_usec( void *data, void *userdata) { - usec_t *usec = data; + nsec_t *nsec = data; assert(filename); assert(lvalue); assert(rvalue); assert(data); - if (parse_usec(rvalue, usec) < 0) { + if (parse_nsec(rvalue, nsec) < 0) { log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue); return 0; } @@ -838,7 +845,7 @@ int config_parse_mode( errno = 0; l = strtol(rvalue, &x, 8); - if (!x || *x || errno) { + if (!x || x == rvalue || *x || errno) { log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue); return 0; } @@ -851,3 +858,140 @@ int config_parse_mode( *m = (mode_t) l; return 0; } + +int config_parse_facility( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + + int *o = data, x; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + x = log_facility_unshifted_from_string(rvalue); + if (x < 0) { + log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue); + return 0; + } + + *o = (x << 3) | LOG_PRI(*o); + + return 0; +} + +int config_parse_level( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + + int *o = data, x; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + x = log_level_from_string(rvalue); + if (x < 0) { + log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue); + return 0; + } + + *o = (*o & LOG_FACMASK) | x; + return 0; +} + +int config_parse_set_status( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + char *w; + size_t l; + char *state; + int r; + ExitStatusSet *status_set = data; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if (isempty(rvalue)) { + /* Empty assignment resets the list */ + + set_free(status_set->signal); + set_free(status_set->code); + + status_set->signal = status_set->code = NULL; + return 0; + } + + FOREACH_WORD(w, l, rvalue, state) { + int val; + char *temp; + + temp = strndup(w, l); + if (!temp) + return log_oom(); + + r = safe_atoi(temp, &val); + if (r < 0) { + val = signal_from_string_try_harder(temp); + free(temp); + + if (val > 0) { + r = set_ensure_allocated(&status_set->signal, trivial_hash_func, trivial_compare_func); + if (r < 0) + return log_oom(); + + r = set_put(status_set->signal, INT_TO_PTR(val)); + if (r < 0) { + log_error("[%s:%u] Unable to store: %s", filename, line, w); + return r; + } + } else { + log_error("[%s:%u] Failed to parse value, ignoring: %s", filename, line, w); + return 0; + } + } else { + free(temp); + + if (val < 0 || val > 255) + log_warning("[%s:%u] Value %d is outside range 0-255, ignoring", filename, line, val); + else { + r = set_ensure_allocated(&status_set->code, trivial_hash_func, trivial_compare_func); + if (r < 0) + return log_oom(); + + r = set_put(status_set->code, INT_TO_PTR(val)); + if (r < 0) { + log_error("[%s:%u] Unable to store: %s", filename, line, w); + return r; + } + } + } + } + + return 0; +}