From: Zbigniew Jędrzejewski-Szmek Date: Wed, 27 Sep 2017 15:54:06 +0000 (+0200) Subject: core/cgroup: add a helper macro for a common pattern (#6926) X-Git-Tag: v235~53 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2e4025c0f93cbd7d6cd1f946f4794d6d06b647da;p=thirdparty%2Fsystemd.git core/cgroup: add a helper macro for a common pattern (#6926) --- diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 9a0d374aa8d..c2e57a0cdd9 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -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); diff --git a/src/core/cgroup.h b/src/core/cgroup.h index fcbf8d01ca0..10ac322aed2 100644 --- a/src/core/cgroup.h +++ b/src/core/cgroup.h @@ -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); diff --git a/src/core/unit.c b/src/core/unit.c index 0fe881436ea..6d9b85fb856 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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) {