X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fcondition-util.c;h=33752c8f9104ed5a904e83a73e8a1336ccbe2c95;hb=592fd144ae313855f48d0ca52a103013b41e5d59;hp=928edeeb9dbc9d7dfe5f1bbf5d0bf2fa88bdadf2;hpb=e26807239bd65bc17535a53cd540f38600e7ef24;p=elogind.git diff --git a/src/shared/condition-util.c b/src/shared/condition-util.c index 928edeeb9..33752c8f9 100644 --- a/src/shared/condition-util.c +++ b/src/shared/condition-util.c @@ -26,7 +26,7 @@ #include #include -#include +#include "systemd/sd-id128.h" #include "util.h" #include "condition-util.h" #include "virt.h" @@ -73,12 +73,11 @@ void condition_free_list(Condition *first) { condition_free(c); } -bool condition_test_kernel_command_line(Condition *c) { - char *line, *w, *state, *word = NULL; +int condition_test_kernel_command_line(Condition *c) { + _cleanup_free_ char *line = NULL; + const char *p; bool equal; int r; - size_t l, pl; - bool found = false; assert(c); assert(c->parameter); @@ -86,41 +85,40 @@ bool condition_test_kernel_command_line(Condition *c) { r = proc_cmdline(&line); if (r < 0) - log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); - if (r <= 0) + return r; + if (r == 0) return c->negate; equal = !!strchr(c->parameter, '='); - pl = strlen(c->parameter); - - FOREACH_WORD_QUOTED(w, l, line, state) { - - free(word); - word = strndup(w, l); - if (!word) - break; - - if (equal) { - if (streq(word, c->parameter)) { - found = true; - break; - } - } else { - if (startswith(word, c->parameter) && (word[pl] == '=' || word[pl] == 0)) { - found = true; - break; - } + p = line; + + for (;;) { + _cleanup_free_ char *word = NULL; + bool found; + + r = unquote_first_word(&p, &word); + if (r < 0) + return r; + if (r == 0) + return c->negate; + + if (equal) + found = streq(word, c->parameter); + else { + const char *f; + + f = startswith(word, c->parameter); + found = f && (*f == '=' || *f == 0); } + if (found) + return !c->negate; } - free(word); - free(line); - - return found == !c->negate; + return c->negate; } -bool condition_test_virtualization(Condition *c) { +int condition_test_virtualization(Condition *c) { int b, v; const char *id; @@ -129,10 +127,8 @@ bool condition_test_virtualization(Condition *c) { assert(c->type == CONDITION_VIRTUALIZATION); v = detect_virtualization(&id); - if (v < 0) { - log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v)); - return c->negate; - } + if (v < 0) + return v; /* First, compare with yes/no */ b = parse_boolean(c->parameter); @@ -154,8 +150,8 @@ bool condition_test_virtualization(Condition *c) { return (v > 0 && streq(c->parameter, id)) == !c->negate; } -bool condition_test_architecture(Condition *c) { - Architecture a, b; +int condition_test_architecture(Condition *c) { + int a, b; assert(c); assert(c->parameter); @@ -163,20 +159,19 @@ bool condition_test_architecture(Condition *c) { a = uname_architecture(); if (a < 0) - return c->negate; + return a; if (streq(c->parameter, "native")) b = native_architecture(); else b = architecture_from_string(c->parameter); - if (b < 0) - return c->negate; + return b; return (a == b) == !c->negate; } -bool condition_test_host(Condition *c) { +int condition_test_host(Condition *c) { _cleanup_free_ char *h = NULL; sd_id128_t x, y; int r; @@ -189,19 +184,19 @@ bool condition_test_host(Condition *c) { r = sd_id128_get_machine(&y); if (r < 0) - return c->negate; + return r; return sd_id128_equal(x, y) == !c->negate; } h = gethostname_malloc(); if (!h) - return c->negate; + return -ENOMEM; return (fnmatch(c->parameter, h, FNM_CASEFOLD) == 0) == !c->negate; } -bool condition_test_ac_power(Condition *c) { +int condition_test_ac_power(Condition *c) { int r; assert(c); @@ -210,7 +205,7 @@ bool condition_test_ac_power(Condition *c) { r = parse_boolean(c->parameter); if (r < 0) - return !c->negate; + return r; return ((on_ac_power() != 0) == !!r) == !c->negate; } @@ -229,7 +224,7 @@ void condition_dump(Condition *c, FILE *f, const char *prefix) { c->trigger ? "|" : "", c->negate ? "!" : "", c->parameter, - c->state < 0 ? "failed" : c->state > 0 ? "succeeded" : "untested"); + CONDITION_STATE_IS_FAILED(c->state) ? "failed" : CONDITION_STATE_IS_SUCCEEDED(c->state) ? "succeeded" : "untested"); } void condition_dump_list(Condition *first, FILE *f, const char *prefix) {