From 625a164069aff9efb61dcc5916c572f53c2a7ab0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 20 Aug 2020 13:43:00 +0200 Subject: analyze: rework condition testing Let's drop the private table and just use the generic concepts we have in place already that make the same information available. Fixes: #16781 --- src/analyze/analyze-condition.c | 105 +++++++++++----------------------------- 1 file changed, 28 insertions(+), 77 deletions(-) (limited to 'src/analyze/analyze-condition.c') diff --git a/src/analyze/analyze-condition.c b/src/analyze/analyze-condition.c index 52ad382637..13f75e813a 100644 --- a/src/analyze/analyze-condition.c +++ b/src/analyze/analyze-condition.c @@ -8,83 +8,27 @@ #include "load-fragment.h" #include "service.h" -typedef struct condition_definition { - const char *name; - ConfigParserCallback parser; - ConditionType type; -} condition_definition; - -static const condition_definition condition_definitions[] = { - { "ConditionPathExists", config_parse_unit_condition_path, CONDITION_PATH_EXISTS }, - { "ConditionPathExistsGlob", config_parse_unit_condition_path, CONDITION_PATH_EXISTS_GLOB }, - { "ConditionPathIsDirectory", config_parse_unit_condition_path, CONDITION_PATH_IS_DIRECTORY }, - { "ConditionPathIsSymbolicLink", config_parse_unit_condition_path, CONDITION_PATH_IS_SYMBOLIC_LINK }, - { "ConditionPathIsMountPoint", config_parse_unit_condition_path, CONDITION_PATH_IS_MOUNT_POINT }, - { "ConditionPathIsReadWrite", config_parse_unit_condition_path, CONDITION_PATH_IS_READ_WRITE }, - { "ConditionPathIsEncrypted", config_parse_unit_condition_path, CONDITION_PATH_IS_ENCRYPTED }, - { "ConditionDirectoryNotEmpty", config_parse_unit_condition_path, CONDITION_DIRECTORY_NOT_EMPTY }, - { "ConditionFileNotEmpty", config_parse_unit_condition_path, CONDITION_FILE_NOT_EMPTY }, - { "ConditionFileIsExecutable", config_parse_unit_condition_path, CONDITION_FILE_IS_EXECUTABLE }, - { "ConditionNeedsUpdate", config_parse_unit_condition_path, CONDITION_NEEDS_UPDATE }, - { "ConditionFirstBoot", config_parse_unit_condition_string, CONDITION_FIRST_BOOT }, - { "ConditionKernelCommandLine", config_parse_unit_condition_string, CONDITION_KERNEL_COMMAND_LINE }, - { "ConditionKernelVersion", config_parse_unit_condition_string, CONDITION_KERNEL_VERSION }, - { "ConditionArchitecture", config_parse_unit_condition_string, CONDITION_ARCHITECTURE }, - { "ConditionVirtualization", config_parse_unit_condition_string, CONDITION_VIRTUALIZATION }, - { "ConditionSecurity", config_parse_unit_condition_string, CONDITION_SECURITY }, - { "ConditionCapability", config_parse_unit_condition_string, CONDITION_CAPABILITY }, - { "ConditionHost", config_parse_unit_condition_string, CONDITION_HOST }, - { "ConditionACPower", config_parse_unit_condition_string, CONDITION_AC_POWER }, - { "ConditionUser", config_parse_unit_condition_string, CONDITION_USER }, - { "ConditionGroup", config_parse_unit_condition_string, CONDITION_GROUP }, - { "ConditionControlGroupController", config_parse_unit_condition_string, CONDITION_CONTROL_GROUP_CONTROLLER }, - - { "AssertPathExists", config_parse_unit_condition_path, CONDITION_PATH_EXISTS }, - { "AssertPathExistsGlob", config_parse_unit_condition_path, CONDITION_PATH_EXISTS_GLOB }, - { "AssertPathIsDirectory", config_parse_unit_condition_path, CONDITION_PATH_IS_DIRECTORY }, - { "AssertPathIsSymbolicLink", config_parse_unit_condition_path, CONDITION_PATH_IS_SYMBOLIC_LINK }, - { "AssertPathIsMountPoint", config_parse_unit_condition_path, CONDITION_PATH_IS_MOUNT_POINT }, - { "AssertPathIsReadWrite", config_parse_unit_condition_path, CONDITION_PATH_IS_READ_WRITE }, - { "AssertPathIsEncrypted", config_parse_unit_condition_path, CONDITION_PATH_IS_ENCRYPTED }, - { "AssertDirectoryNotEmpty", config_parse_unit_condition_path, CONDITION_DIRECTORY_NOT_EMPTY }, - { "AssertFileNotEmpty", config_parse_unit_condition_path, CONDITION_FILE_NOT_EMPTY }, - { "AssertFileIsExecutable", config_parse_unit_condition_path, CONDITION_FILE_IS_EXECUTABLE }, - { "AssertNeedsUpdate", config_parse_unit_condition_path, CONDITION_NEEDS_UPDATE }, - { "AssertFirstBoot", config_parse_unit_condition_string, CONDITION_FIRST_BOOT }, - { "AssertKernelCommandLine", config_parse_unit_condition_string, CONDITION_KERNEL_COMMAND_LINE }, - { "AssertKernelVersion", config_parse_unit_condition_string, CONDITION_KERNEL_VERSION }, - { "AssertArchitecture", config_parse_unit_condition_string, CONDITION_ARCHITECTURE }, - { "AssertVirtualization", config_parse_unit_condition_string, CONDITION_VIRTUALIZATION }, - { "AssertSecurity", config_parse_unit_condition_string, CONDITION_SECURITY }, - { "AssertCapability", config_parse_unit_condition_string, CONDITION_CAPABILITY }, - { "AssertHost", config_parse_unit_condition_string, CONDITION_HOST }, - { "AssertACPower", config_parse_unit_condition_string, CONDITION_AC_POWER }, - { "AssertUser", config_parse_unit_condition_string, CONDITION_USER }, - { "AssertGroup", config_parse_unit_condition_string, CONDITION_GROUP }, - { "AssertControlGroupController", config_parse_unit_condition_string, CONDITION_CONTROL_GROUP_CONTROLLER }, - - /* deprecated, but we should still parse them */ - { "ConditionNull", config_parse_unit_condition_null, 0 }, - { "AssertNull", config_parse_unit_condition_null, 0 }, -}; - static int parse_condition(Unit *u, const char *line) { - const char *p; - Condition **target; - - if ((p = startswith(line, "Condition"))) - target = &u->conditions; - else if ((p = startswith(line, "Assert"))) - target = &u->asserts; - else - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line); - - for (size_t i = 0; i < ELEMENTSOF(condition_definitions); i++) { - const condition_definition *c = &condition_definitions[i]; - - p = startswith(line, c->name); - if (!p) - continue; + assert(u); + assert(line); + + for (ConditionType t = 0; t < _CONDITION_TYPE_MAX; t++) { + ConfigParserCallback callback; + Condition **target; + const char *p, *name; + + name = condition_type_to_string(t); + p = startswith(line, name); + if (p) + target = &u->conditions; + else { + name = assert_type_to_string(t); + p = startswith(line, name); + if (!p) + continue; + + target = &u->asserts; + } p += strspn(p, WHITESPACE); @@ -94,7 +38,14 @@ static int parse_condition(Unit *u, const char *line) { p += strspn(p, WHITESPACE); - return c->parser(NULL, "(stdin)", 0, NULL, 0, c->name, c->type, p, target, u); + if (t == CONDITION_NULL) /* deprecated, but we should still parse this for now */ + callback = config_parse_unit_condition_null; + else if (condition_takes_path(t)) + callback = config_parse_unit_condition_path; + else + callback = config_parse_unit_condition_string; + + return callback(NULL, "(cmdline)", 0, NULL, 0, name, t, p, target, u); } return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line); -- cgit v1.2.1