]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pid1,cgroup-show: ignore -EOPNOTSUPP in cg_read_pid()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 Jan 2022 15:09:38 +0000 (00:09 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 18 Jan 2022 11:34:30 +0000 (12:34 +0100)
The function is called in recursion, and cgroup.procs in some subcgroups
may not be read.

Fixes #22089.

src/core/dbus-unit.c
src/shared/cgroup-show.c

index eef491740cf8c9666802f94bb0560b28e6b442e4..1128c42ad94028c14a6787beaf009b04b3c828ba 100644 (file)
@@ -1314,11 +1314,15 @@ static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) {
         for (;;) {
                 pid_t pid;
 
+                /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
+                 * From https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#threads,
+                 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
+                 * the subtree and is not readable in the subtree proper. */
                 r = cg_read_pid(f, &pid);
+                if (IN_SET(r, 0, -EOPNOTSUPP))
+                        break;
                 if (r < 0)
                         return r;
-                if (r == 0)
-                        break;
 
                 if (is_kernel_thread(pid) > 0)
                         continue;
index 40bc2bff0515114a94934a3f17463c7328e83afd..48dd4d800137f0c4b9e8f5685e1f11a0ea64c2fd 100644 (file)
@@ -89,7 +89,6 @@ static int show_cgroup_one_by_path(
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *p = NULL;
         size_t n = 0;
-        pid_t pid;
         char *fn;
         int r;
 
@@ -102,7 +101,18 @@ static int show_cgroup_one_by_path(
         if (!f)
                 return -errno;
 
-        while ((r = cg_read_pid(f, &pid)) > 0) {
+        for (;;) {
+                pid_t pid;
+
+                /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
+                 * From https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#threads,
+                 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
+                 * the subtree and is not readable in the subtree proper. */
+                r = cg_read_pid(f, &pid);
+                if (IN_SET(r, 0, -EOPNOTSUPP))
+                        break;
+                if (r < 0)
+                        return r;
 
                 if (!(flags & OUTPUT_KERNEL_THREADS) && is_kernel_thread(pid) > 0)
                         continue;
@@ -113,9 +123,6 @@ static int show_cgroup_one_by_path(
                 pids[n++] = pid;
         }
 
-        if (r < 0)
-                return r;
-
         show_pid_array(pids, n, prefix, n_columns, false, more, flags);
 
         return 0;