summaryrefslogtreecommitdiff
path: root/src/shared/condition.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-03-18 17:44:18 +0100
committerLennart Poettering <lennart@poettering.net>2019-03-19 15:55:08 +0100
commit754f719af2631a0345aba4e8146140840de9c87e (patch)
tree771b4589586c6d32006738c9d497b84048073df5 /src/shared/condition.c
parent2877d4287068f3fdc188b6c0b52bd859da229d80 (diff)
downloadsystemd-754f719af2631a0345aba4e8146140840de9c87e.tar.gz
systemd-754f719af2631a0345aba4e8146140840de9c87e.tar.bz2
systemd-754f719af2631a0345aba4e8146140840de9c87e.zip
condition: add ConditionMemory= and ConditionCPUs=
We have all the building blocks in place already, let's add this. Fixes: #8990
Diffstat (limited to 'src/shared/condition.c')
-rw-r--r--src/shared/condition.c61
1 files changed, 59 insertions, 2 deletions
diff --git a/src/shared/condition.c b/src/shared/condition.c
index 38ebaef62b..5366d30e67 100644
--- a/src/shared/condition.c
+++ b/src/shared/condition.c
@@ -29,6 +29,7 @@
#include "glob-util.h"
#include "hostname-util.h"
#include "ima-util.h"
+#include "limits-util.h"
#include "list.h"
#include "macro.h"
#include "mountpoint-util.h"
@@ -218,6 +219,56 @@ static int condition_test_kernel_version(Condition *c) {
return test_order(str_verscmp(u.release, skip_leading_chars(p, NULL)), order);
}
+static int condition_test_memory(Condition *c) {
+ OrderOperator order;
+ uint64_t m, k;
+ const char *p;
+ int r;
+
+ assert(c);
+ assert(c->parameter);
+ assert(c->type == CONDITION_MEMORY);
+
+ m = physical_memory();
+
+ p = c->parameter;
+ order = parse_order(&p);
+ if (order < 0)
+ order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
+
+ r = safe_atou64(p, &k);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to parse size: %m");
+
+ return test_order(CMP(m, k), order);
+}
+
+static int condition_test_cpus(Condition *c) {
+ OrderOperator order;
+ const char *p;
+ unsigned k;
+ int r, n;
+
+ assert(c);
+ assert(c->parameter);
+ assert(c->type == CONDITION_CPUS);
+
+ n = cpus_in_affinity_mask();
+ if (n < 0)
+ return log_debug_errno(n, "Failed to determine CPUs in affinity mask: %m");
+
+ p = c->parameter;
+ order = parse_order(&p);
+ if (order < 0)
+ order = ORDER_GREATER_OR_EQUAL; /* default to >= check, if nothing is specified. */
+
+ r = safe_atou(p, &k);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to parse number of CPUs: %m");
+
+ return test_order(CMP((unsigned) n, k), order);
+}
+
static int condition_test_user(Condition *c) {
uid_t id;
int r;
@@ -651,6 +702,8 @@ int condition_test(Condition *c) {
[CONDITION_GROUP] = condition_test_group,
[CONDITION_CONTROL_GROUP_CONTROLLER] = condition_test_control_group_controller,
[CONDITION_NULL] = condition_test_null,
+ [CONDITION_CPUS] = condition_test_cpus,
+ [CONDITION_MEMORY] = condition_test_memory,
};
int r, b;
@@ -716,7 +769,9 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_USER] = "ConditionUser",
[CONDITION_GROUP] = "ConditionGroup",
[CONDITION_CONTROL_GROUP_CONTROLLER] = "ConditionControlGroupController",
- [CONDITION_NULL] = "ConditionNull"
+ [CONDITION_NULL] = "ConditionNull",
+ [CONDITION_CPUS] = "ConditionCPUs",
+ [CONDITION_MEMORY] = "ConditionMemory",
};
DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);
@@ -744,7 +799,9 @@ static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_USER] = "AssertUser",
[CONDITION_GROUP] = "AssertGroup",
[CONDITION_CONTROL_GROUP_CONTROLLER] = "AssertControlGroupController",
- [CONDITION_NULL] = "AssertNull"
+ [CONDITION_NULL] = "AssertNull",
+ [CONDITION_CPUS] = "AssertCPUs",
+ [CONDITION_MEMORY] = "AssertMemory",
};
DEFINE_STRING_TABLE_LOOKUP(assert_type, ConditionType);