]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: add cg_is_delegated helper
authorNick Rosbrook <enr0n@ubuntu.com>
Thu, 28 Sep 2023 18:10:59 +0000 (14:10 -0400)
committerNick Rosbrook <enr0n@ubuntu.com>
Fri, 13 Oct 2023 19:13:11 +0000 (15:13 -0400)
Take is_delegated from cgroup-show.c, and make it a generic helper
function. This new helper will be used again in a later commit.

src/basic/cgroup-util.c
src/basic/cgroup-util.h
src/shared/cgroup-show.c

index db803084aef037b0a8bf369f4429bf3c874647f4..7df419d60766de54baaf5366348ac51379cc1638 100644 (file)
@@ -2268,6 +2268,25 @@ int cg_hybrid_unified(void) {
         return r == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
 }
 
+int cg_is_delegated(const char *path) {
+        int r;
+
+        assert(path);
+
+        r = cg_get_xattr_bool(path, "trusted.delegate");
+        if (ERRNO_IS_NEG_XATTR_ABSENT(r)) {
+                /* If the trusted xattr isn't set (preferred), then check the
+                 * untrusted one. Under the assumption that whoever is trusted
+                 * enough to own the cgroup, is also trusted enough to decide
+                 * if it is delegated or not this should be safe. */
+                r = cg_get_xattr_bool(path, "user.delegate");
+                if (ERRNO_IS_NEG_XATTR_ABSENT(r))
+                        return false;
+        }
+
+        return r;
+}
+
 const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
         [CGROUP_IO_RBPS_MAX]    = CGROUP_LIMIT_MAX,
         [CGROUP_IO_WBPS_MAX]    = CGROUP_LIMIT_MAX,
index 625816d9cf1de2960f68918681d7115367323d35..7e79735c3f22262cf65b42f4103a59bf01f3a0ea 100644 (file)
@@ -210,6 +210,8 @@ int cg_rmdir(const char *controller, const char *path);
 
 int cg_is_threaded(const char *path);
 
+int cg_is_delegated(const char *path);
+
 typedef enum  {
         CG_KEY_MODE_GRACEFUL = 1 << 0,
 } CGroupKeyMode;
index d2d0339910844f9e8256212c0a222d5b7d350095..32e1176a5769169563ae326f82f0b58d37ef6458 100644 (file)
@@ -128,33 +128,6 @@ static int show_cgroup_one_by_path(
         return 0;
 }
 
-static int is_delegated(int cgfd, const char *path) {
-        _cleanup_free_ char *b = NULL;
-        int r;
-
-        assert(cgfd >= 0 || path);
-
-        const char *t = cgfd >= 0 ? FORMAT_PROC_FD_PATH(cgfd) : path;
-
-        r = getxattr_malloc(t, "trusted.delegate", &b);
-        if (ERRNO_IS_NEG_XATTR_ABSENT(r)) {
-                /* If the trusted xattr isn't set (preferred), then check the untrusted one. Under the
-                 * assumption that whoever is trusted enough to own the cgroup, is also trusted enough to
-                 * decide if it is delegated or not this should be safe. */
-                r = getxattr_malloc(t, "user.delegate", &b);
-                if (ERRNO_IS_NEG_XATTR_ABSENT(r))
-                        return false;
-        }
-        if (r < 0)
-                return log_debug_errno(r, "Failed to read delegate xattr from %s, ignoring: %m", t);
-
-        r = parse_boolean(b);
-        if (r < 0)
-                return log_debug_errno(r, "Failed to parse delegate xattr from %s, ignoring: %m", t);
-
-        return r;
-}
-
 static int show_cgroup_name(
                 const char *path,
                 const char *prefix,
@@ -173,7 +146,10 @@ static int show_cgroup_name(
                         log_debug_errno(errno, "Failed to open cgroup '%s', ignoring: %m", path);
         }
 
-        delegate = is_delegated(fd, path) > 0;
+        r = cg_is_delegated(fd >= 0 ? FORMAT_PROC_FD_PATH(fd) : path);
+        if (r < 0)
+                log_debug_errno(r, "Failed to check if cgroup is delegated, ignoring: %m");
+        delegate = r > 0;
 
         if (FLAGS_SET(flags, OUTPUT_CGROUP_ID)) {
                 cg_file_handle fh = CG_FILE_HANDLE_INIT;