From cc50ef134b4104cae8783a4ca40b1a70247e3ef9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 6 Nov 2014 02:27:10 +0100 Subject: [PATCH] condition: record test state internally and beef it up to be a full enum --- src/core/condition.c | 4 +--- src/core/dbus-unit.c | 8 +++++++- src/shared/condition-util.c | 22 ++++++++++++++++++---- src/shared/condition-util.h | 22 ++++++++++++---------- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/core/condition.c b/src/core/condition.c index d8d11528eca..84752586796 100644 --- a/src/core/condition.c +++ b/src/core/condition.c @@ -53,11 +53,9 @@ bool condition_test_list(const char *unit, Condition *first) { c->trigger ? "|" : "", c->negate ? "!" : "", c->parameter, - r > 0 ? "succeeded" : "failed", + condition_result_to_string(c->result), unit); - c->state = r > 0 ? CONDITION_STATE_SUCCEEDED : CONDITION_STATE_FAILED; - if (!c->trigger && r <= 0) return false; diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 2d1862cfc68..3fa427198e2 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -328,10 +328,16 @@ static int property_get_conditions( return r; LIST_FOREACH(conditions, c, u->conditions) { + int tristate; + + tristate = + c->result == CONDITION_UNTESTED ? 0 : + c->result == CONDITION_SUCCEEDED ? 1 : -1; + r = sd_bus_message_append(reply, "(sbbsi)", condition_type_to_string(c->type), c->trigger, c->negate, - c->parameter, c->state); + c->parameter, tristate); if (r < 0) return r; diff --git a/src/shared/condition-util.c b/src/shared/condition-util.c index 749b57712af..640a931ff5b 100644 --- a/src/shared/condition-util.c +++ b/src/shared/condition-util.c @@ -429,17 +429,22 @@ int condition_test(Condition *c) { [CONDITION_FIRST_BOOT] = condition_test_first_boot, [CONDITION_NULL] = condition_test_null, }; - int r; + + int r, b; assert(c); assert(c->type >= 0); assert(c->type < _CONDITION_TYPE_MAX); r = condition_tests[c->type](c); - if (r < 0) + if (r < 0) { + c->result = CONDITION_ERROR; return r; + } - return (r > 0) == !c->negate; + b = (r > 0) == !c->negate; + c->result = b ? CONDITION_SUCCEEDED : CONDITION_FAILED; + return b; } void condition_dump(Condition *c, FILE *f, const char *prefix) { @@ -456,7 +461,7 @@ void condition_dump(Condition *c, FILE *f, const char *prefix) { c->trigger ? "|" : "", c->negate ? "!" : "", c->parameter, - CONDITION_STATE_IS_FAILED(c->state) ? "failed" : CONDITION_STATE_IS_SUCCEEDED(c->state) ? "succeeded" : "untested"); + condition_result_to_string(c->result)); } void condition_dump_list(Condition *first, FILE *f, const char *prefix) { @@ -489,3 +494,12 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = { }; DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType); + +static const char* const condition_result_table[_CONDITION_RESULT_MAX] = { + [CONDITION_UNTESTED] = "untested", + [CONDITION_SUCCEEDED] = "succeeded", + [CONDITION_FAILED] = "failed", + [CONDITION_ERROR] = "error", +}; + +DEFINE_STRING_TABLE_LOOKUP(condition_result, ConditionResult); diff --git a/src/shared/condition-util.h b/src/shared/condition-util.h index 44f672b2e65..08aee94a892 100644 --- a/src/shared/condition-util.h +++ b/src/shared/condition-util.h @@ -51,15 +51,14 @@ typedef enum ConditionType { _CONDITION_TYPE_INVALID = -1 } ConditionType; -#define CONDITION_STATE_IS_SUCCEEDED(state) ((state) > 0) -#define CONDITION_STATE_IS_UNKNOWN(state) ((state) == 0) -#define CONDITION_STATE_IS_FAILED(state) ((state) < 0) - -enum { - CONDITION_STATE_SUCCEEDED = -1, - CONDITION_STATE_UNKNOWN = 0, - CONDITION_STATE_FAILED = 1 -}; +typedef enum ConditionResult { + CONDITION_UNTESTED, + CONDITION_SUCCEEDED, + CONDITION_FAILED, + CONDITION_ERROR, + _CONDITION_RESULT_MAX, + _CONDITION_RESULT_INVALID = -1 +} ConditionResult; typedef struct Condition { ConditionType type; @@ -68,7 +67,7 @@ typedef struct Condition { bool negate:1; char *parameter; - int state; + ConditionResult result; LIST_FIELDS(struct Condition, conditions); } Condition; @@ -84,3 +83,6 @@ void condition_dump_list(Condition *c, FILE *f, const char *prefix); const char* condition_type_to_string(ConditionType t) _const_; int condition_type_from_string(const char *s) _pure_; + +const char* condition_result_to_string(ConditionResult r) _const_; +ConditionResult condition_result_from_string(const char *s) _pure_;