chiark / gitweb /
cgroup: add proper API to determine whether our unit manags to root cgroup
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Jan 2018 17:41:42 +0000 (18:41 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:50:17 +0000 (07:50 +0200)
src/core/cgroup.c
src/core/cgroup.h

index 8ebb3d2b93c6a916b1f9440b8643f2f994f02fd7..2e70aa79ad75e5f48fe33cc1cc325b0314599113 100644 (file)
 
 #define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
 
+bool unit_has_root_cgroup(Unit *u) {
+        assert(u);
+
+        /* Returns whether this unit manages the root cgroup. Note that this is different from being named "-.slice",
+         * as inside of containers the root slice won't be identical to the root cgroup. */
+
+        if (!u->cgroup_path)
+                return false;
+
+        return isempty(u->cgroup_path) || path_equal(u->cgroup_path, "/");
+}
+
 #if 0 /// UNNEEDED by elogind
 static void cgroup_compat_warn(void) {
         static bool cgroup_compat_warned = false;
@@ -709,21 +721,17 @@ static void cgroup_context_apply(
 
         assert(u);
 
-        c = unit_get_cgroup_context(u);
-        path = u->cgroup_path;
-
-        assert(c);
-        assert(path);
-
         /* Nothing to do? Exit early! */
         if (apply_mask == 0 && !apply_bpf)
                 return;
 
-        /* Some cgroup attributes are not supported on the root cgroup,
-         * hence silently ignore */
-        is_root = isempty(path) || path_equal(path, "/");
-        if (is_root)
-                /* Make sure we don't try to display messages with an empty path. */
+        /* Some cgroup attributes are not supported on the root cgroup, hence silently ignore */
+        is_root = unit_has_root_cgroup(u);
+
+        assert_se(c = unit_get_cgroup_context(u));
+        assert_se(path = u->cgroup_path);
+
+        if (is_root) /* Make sure we don't try to display messages with an empty path. */
                 path = "/";
 
         /* We generally ignore errors caused by read-only mounted
index 1c43c0d148bc1c52b282ae59291707bc006394ed..3724b8ba19608ddfbafa5c9d26caf9b3ea2a78c1 100644 (file)
@@ -197,8 +197,6 @@ Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
 int unit_search_main_pid(Unit *u, pid_t *ret);
 int unit_watch_all_pids(Unit *u);
 
-int unit_synthesize_cgroup_empty_event(Unit *u);
-
 int unit_get_memory_current(Unit *u, uint64_t *ret);
 int unit_get_tasks_current(Unit *u, uint64_t *ret);
 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
@@ -214,6 +212,8 @@ int unit_reset_ip_accounting(Unit *u);
         })
 
 #endif // 0
+bool unit_has_root_cgroup(Unit *u);
+
 int manager_notify_cgroup_empty(Manager *m, const char *group);
 
 #if 0 /// UNNEEDED by elogind