From c0d6e764d107a81a6439c41edbe92790623ed7de Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 22 Aug 2012 01:51:53 +0200 Subject: unit: add new ConditionHost= condition type --- src/core/condition.c | 31 +++++++++++++++++++++++++++++++ src/core/condition.h | 1 + src/core/load-fragment-gperf.gperf.m4 | 1 + src/core/load-fragment.c | 11 +++++++---- 4 files changed, 40 insertions(+), 4 deletions(-) (limited to 'src/core') diff --git a/src/core/condition.c b/src/core/condition.c index e4080d569d..e5cda21c37 100644 --- a/src/core/condition.c +++ b/src/core/condition.c @@ -25,11 +25,13 @@ #include #include #include +#include #ifdef HAVE_SELINUX #include #endif +#include #include "util.h" #include "condition.h" #include "virt.h" @@ -194,6 +196,31 @@ static bool test_capability(const char *parameter) { return !!(capabilities & (1ULL << value)); } +static bool test_host(const char *parameter) { + sd_id128_t x, y; + char *h; + int r; + bool b; + + if (sd_id128_from_string(parameter, &x) >= 0) { + + r = sd_id128_get_machine(&y); + if (r < 0) + return false; + + return sd_id128_equal(x, y); + } + + h = gethostname_malloc(); + if (!h) + return false; + + b = fnmatch(parameter, h, FNM_CASEFOLD) == 0; + free(h); + + return b; +} + bool condition_test(Condition *c) { assert(c); @@ -255,6 +282,9 @@ bool condition_test(Condition *c) { case CONDITION_CAPABILITY: return test_capability(c->parameter) == !c->negate; + case CONDITION_HOST: + return test_host(c->parameter) == !c->negate; + case CONDITION_NULL: return !c->negate; @@ -323,6 +353,7 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = { [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine", [CONDITION_VIRTUALIZATION] = "ConditionVirtualization", [CONDITION_SECURITY] = "ConditionSecurity", + [CONDITION_HOST] = "ConditionHost", [CONDITION_NULL] = "ConditionNull" }; diff --git a/src/core/condition.h b/src/core/condition.h index 3dca432f77..55b331edd7 100644 --- a/src/core/condition.h +++ b/src/core/condition.h @@ -38,6 +38,7 @@ typedef enum ConditionType { CONDITION_VIRTUALIZATION, CONDITION_SECURITY, CONDITION_CAPABILITY, + CONDITION_HOST, CONDITION_NULL, _CONDITION_TYPE_MAX, _CONDITION_TYPE_INVALID = -1 diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4 index 84eea1c465..8187cd48c6 100644 --- a/src/core/load-fragment-gperf.gperf.m4 +++ b/src/core/load-fragment-gperf.gperf.m4 @@ -135,6 +135,7 @@ Unit.ConditionKernelCommandLine, config_parse_unit_condition_string, CONDITION_K Unit.ConditionVirtualization, config_parse_unit_condition_string, CONDITION_VIRTUALIZATION, 0 Unit.ConditionSecurity, config_parse_unit_condition_string, CONDITION_SECURITY, 0 Unit.ConditionCapability, config_parse_unit_condition_string, CONDITION_CAPABILITY, 0 +Unit.ConditionHost, config_parse_unit_condition_string, CONDITION_HOST, 0 Unit.ConditionNull, config_parse_unit_condition_null, 0, 0 m4_dnl Service.PIDFile, config_parse_unit_path_printf, 0, offsetof(Service, pid_file) diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 10681307cf..9438aa312b 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -1516,14 +1516,17 @@ int config_parse_unit_condition_string( assert(rvalue); assert(data); - if ((trigger = rvalue[0] == '|')) + trigger = rvalue[0] == '|'; + if (trigger) rvalue++; - if ((negate = rvalue[0] == '!')) + negate = rvalue[0] == '!'; + if (negate) rvalue++; - if (!(c = condition_new(cond, rvalue, trigger, negate))) - return -ENOMEM; + c = condition_new(cond, rvalue, trigger, negate); + if (!c) + return log_oom(); LIST_PREPEND(Condition, conditions, u->conditions, c); return 0; -- cgit v1.2.3-54-g00ecf