summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-08-22 01:51:53 +0200
committerLennart Poettering <lennart@poettering.net>2012-08-22 01:51:53 +0200
commitc0d6e764d107a81a6439c41edbe92790623ed7de (patch)
treeaf2f601ceef8a3328f5d9c7f9eaece6a0c573d8f /src
parentddfa5101a2e0d94571c10e2bbc7c38b60dc6cba1 (diff)
unit: add new ConditionHost= condition type
Diffstat (limited to 'src')
-rw-r--r--src/core/condition.c31
-rw-r--r--src/core/condition.h1
-rw-r--r--src/core/load-fragment-gperf.gperf.m41
-rw-r--r--src/core/load-fragment.c11
4 files changed, 40 insertions, 4 deletions
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 <unistd.h>
#include <sys/capability.h>
#include <sys/statvfs.h>
+#include <fnmatch.h>
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif
+#include <systemd/sd-id128.h>
#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;