]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/cgroup: add a helper macro for a common pattern (#6926)
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 Sep 2017 15:54:06 +0000 (17:54 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 27 Sep 2017 15:54:06 +0000 (17:54 +0200)
src/core/cgroup.c
src/core/cgroup.h
src/core/unit.c

index 9a0d374aa8dcc3e501c3898bee57ba049b356d27..c2e57a0cdd908d0f66697f3fe24796cf285fd2b9 100644 (file)
@@ -2088,16 +2088,12 @@ int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
 
 int unit_get_memory_current(Unit *u, uint64_t *ret) {
         _cleanup_free_ char *v = NULL;
-        CGroupContext *cc;
         int r;
 
         assert(u);
         assert(ret);
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->memory_accounting)
+        if (!UNIT_CGROUP_BOOL(u, memory_accounting))
                 return -ENODATA;
 
         if (!u->cgroup_path)
@@ -2123,16 +2119,12 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) {
 
 int unit_get_tasks_current(Unit *u, uint64_t *ret) {
         _cleanup_free_ char *v = NULL;
-        CGroupContext *cc;
         int r;
 
         assert(u);
         assert(ret);
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->tasks_accounting)
+        if (!UNIT_CGROUP_BOOL(u, tasks_accounting))
                 return -ENODATA;
 
         if (!u->cgroup_path)
@@ -2201,7 +2193,6 @@ static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
 }
 
 int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
-        CGroupContext *cc;
         nsec_t ns;
         int r;
 
@@ -2211,10 +2202,7 @@ int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
          * started. If the cgroup has been removed already, returns the last cached value. To cache the value, simply
          * call this function with a NULL return value. */
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->cpu_accounting)
+        if (!UNIT_CGROUP_BOOL(u, cpu_accounting))
                 return -ENODATA;
 
         r = unit_get_cpu_usage_raw(u, &ns);
@@ -2246,7 +2234,6 @@ int unit_get_ip_accounting(
                 CGroupIPAccountingMetric metric,
                 uint64_t *ret) {
 
-        CGroupContext *cc;
         uint64_t value;
         int fd, r;
 
@@ -2262,10 +2249,7 @@ int unit_get_ip_accounting(
         if (u->type == UNIT_SLICE)
                 return -ENODATA;
 
-        cc = unit_get_cgroup_context(u);
-        if (!cc)
-                return -ENODATA;
-        if (!cc->ip_accounting)
+        if (!UNIT_CGROUP_BOOL(u, ip_accounting))
                 return -ENODATA;
 
         fd = IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_INGRESS_PACKETS) ?
@@ -2325,18 +2309,6 @@ int unit_reset_ip_accounting(Unit *u) {
         return r < 0 ? r : q;
 }
 
-bool unit_cgroup_delegate(Unit *u) {
-        CGroupContext *c;
-
-        assert(u);
-
-        c = unit_get_cgroup_context(u);
-        if (!c)
-                return false;
-
-        return c->delegate;
-}
-
 void unit_invalidate_cgroup(Unit *u, CGroupMask m) {
         assert(u);
 
index fcbf8d01ca07fa8d6b38dec1d93d1ffb4c047787..10ac322aed2fc9cee4a1a3faeec29f9ca5080c8f 100644 (file)
@@ -194,7 +194,11 @@ int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *r
 int unit_reset_cpu_accounting(Unit *u);
 int unit_reset_ip_accounting(Unit *u);
 
-bool unit_cgroup_delegate(Unit *u);
+#define UNIT_CGROUP_BOOL(u, name)                       \
+        ({                                              \
+        CGroupContext *cc = unit_get_cgroup_context(u); \
+        cc ? cc->name : false;                          \
+        })
 
 int unit_notify_cgroup_empty(Unit *u);
 int manager_notify_cgroup_empty(Manager *m, const char *group);
index 0fe881436ea31e660d734cef6fb6a1261c8cabf5..6d9b85fb856186eb43e38bc30ff4baa8f1d32aae 100644 (file)
@@ -4205,7 +4205,7 @@ int unit_kill_context(
                          * them. */
 
                         if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0 ||
-                            (detect_container() == 0 && !unit_cgroup_delegate(u)))
+                            (detect_container() == 0 && !UNIT_CGROUP_BOOL(u, delegate)))
                                 wait_for_exit = true;
 
                         if (send_sighup) {
@@ -4644,7 +4644,7 @@ void unit_set_exec_params(Unit *u, ExecParameters *p) {
         assert(p);
 
         p->cgroup_path = u->cgroup_path;
-        SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, unit_cgroup_delegate(u));
+        SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, UNIT_CGROUP_BOOL(u, delegate));
 }
 
 int unit_fork_helper_process(Unit *u, pid_t *ret) {