From: Yu Watanabe Date: Fri, 8 Mar 2019 05:22:31 +0000 (+0900) Subject: condition: introduce condition_test_list() X-Git-Tag: v242-rc1~90^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=828fa6108b3fac423d3fd8928a53a3c2f7933769;p=thirdparty%2Fsystemd.git condition: introduce condition_test_list() --- diff --git a/src/shared/condition.c b/src/shared/condition.c index fb77966264e..44d88c37dd4 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -644,6 +644,52 @@ int condition_test(Condition *c) { return b; } +bool condition_test_list(Condition *first, const char *(*to_string)(ConditionType t), condition_test_logger_t logger, void *userdata) { + Condition *c; + int triggered = -1; + + assert(!!logger == !!to_string); + + /* If the condition list is empty, then it is true */ + if (!first) + return true; + + /* Otherwise, if all of the non-trigger conditions apply and + * if any of the trigger conditions apply (unless there are + * none) we return true */ + LIST_FOREACH(conditions, c, first) { + int r; + + r = condition_test(c); + + if (logger) { + if (r < 0) + logger(userdata, LOG_WARNING, r, __FILE__, __LINE__, __func__, + "Couldn't determine result for %s=%s%s%s, assuming failed: %m", + to_string(c->type), + c->trigger ? "|" : "", + c->negate ? "!" : "", + c->parameter); + else + logger(userdata, LOG_DEBUG, 0, __FILE__, __LINE__, __func__, + "%s=%s%s%s %s.", + to_string(c->type), + c->trigger ? "|" : "", + c->negate ? "!" : "", + c->parameter, + condition_result_to_string(c->result)); + } + + if (!c->trigger && r <= 0) + return false; + + if (c->trigger && triggered <= 0) + triggered = r > 0; + } + + return triggered != 0; +} + void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) { assert(c); assert(f); diff --git a/src/shared/condition.h b/src/shared/condition.h index e69fc366f78..773490f0525 100644 --- a/src/shared/condition.h +++ b/src/shared/condition.h @@ -68,6 +68,8 @@ void condition_free(Condition *c); Condition* condition_free_list(Condition *c); int condition_test(Condition *c); +typedef int (*condition_test_logger_t)(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(7, 8); +bool condition_test_list(Condition *first, const char *(*to_string)(ConditionType t), condition_test_logger_t logger, void *userdata); void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)); void condition_dump_list(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t));