]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-show: split out delegation xattr check into its own function
authorLennart Poettering <lennart@poettering.net>
Wed, 16 Mar 2022 13:29:49 +0000 (14:29 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 16 Mar 2022 13:30:01 +0000 (14:30 +0100)
Just some refactoring.

src/shared/cgroup-show.c

index f18420c1b6887fd9fd95671f79a6dfd421e09b47..d2fb17ff5d3ae6808ccdb33e6e6c2c04f3c75a8e 100644 (file)
@@ -128,6 +128,27 @@ 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);
+
+        r = getxattr_malloc(cgfd < 0 ? path : FORMAT_PROC_FD_PATH(cgfd), "trusted.delegate", &b);
+        if (r < 0) {
+                if (r == -ENODATA)
+                        return false;
+
+                return log_debug_errno(r, "Failed to read trusted.delegate extended attribute, ignoring: %m");
+        }
+
+        r = parse_boolean(b);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to parse trusted.delegate extended attribute boolean value, ignoring: %m");
+
+        return r;
+}
+
 static int show_cgroup_name(
                 const char *path,
                 const char *prefix,
@@ -137,7 +158,7 @@ static int show_cgroup_name(
         uint64_t cgroupid = UINT64_MAX;
         _cleanup_free_ char *b = NULL;
         _cleanup_close_ int fd = -1;
-        bool delegate = false;
+        bool delegate;
         int r;
 
         if (FLAGS_SET(flags, OUTPUT_CGROUP_XATTRS) || FLAGS_SET(flags, OUTPUT_CGROUP_ID)) {
@@ -146,19 +167,7 @@ static int show_cgroup_name(
                         log_debug_errno(errno, "Failed to open cgroup '%s', ignoring: %m", path);
         }
 
-        r = getxattr_malloc(fd < 0 ? path : FORMAT_PROC_FD_PATH(fd), "trusted.delegate", &b);
-        if (r < 0) {
-                if (r != -ENODATA)
-                        log_debug_errno(r, "Failed to read trusted.delegate extended attribute, ignoring: %m");
-        } else {
-                r = parse_boolean(b);
-                if (r < 0)
-                        log_debug_errno(r, "Failed to parse trusted.delegate extended attribute boolean value, ignoring: %m");
-                else
-                        delegate = r > 0;
-
-                b = mfree(b);
-        }
+        delegate = is_delegated(fd, path) > 0;
 
         if (FLAGS_SET(flags, OUTPUT_CGROUP_ID)) {
                 cg_file_handle fh = CG_FILE_HANDLE_INIT;