Commit 47e48a94 authored by Laurent Bigonville's avatar Laurent Bigonville
Browse files

Properly detect that applications are running in flatpak when using cgroup v2

Closes: #985345
Thanks: Guido Günther <agx@sigxcpu.org>
parent 5350f630
From: =?utf-8?q?Guido_G=C3=BCnther?= <agx@sigxcpu.org>
Date: Tue, 16 Mar 2021 12:22:30 +0100
Subject: client-info: Support cgroup v2
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
For v2 cgroups the /proc/<pid>/cgroup format changed to a single line¹.
Support this too to not misdetect flatpaks as system apps.
1) See https://www.kernel.org/doc/html/v4.18/admin-guide/cgroup-v2.html#processes
Signed-off-by: Guido Günther <agx@sigxcpu.org>
---
src/gclue-client-info.c | 44 +++++++++++++++++++++++++++++++++++++++++---
1 file changed, 41 insertions(+), 3 deletions(-)
diff --git a/src/gclue-client-info.c b/src/gclue-client-info.c
index d609b34..dd403a6 100644
--- a/src/gclue-client-info.c
+++ b/src/gclue-client-info.c
@@ -181,6 +181,42 @@ on_name_vanished (GDBusConnection *connection,
0);
}
+
+static gchar *
+parse_cgroup_v2 (GStrv lines)
+{
+ const char *unit, *name;
+ char *dash, *xdg_id;
+ g_autofree char *scope = NULL;
+
+ /* Cgroup v2 is always a single line:
+ * 0::/user.slice/user-1000.slice/user@1000.service/app.slice/app-flatpak-org.gnome.Maps-3358.scope
+ */
+ if (g_strv_length (lines) != 2)
+ return NULL;
+
+ if (!g_str_has_prefix (lines[0], "0::"))
+ return NULL;
+
+ unit = lines[0] + strlen ("0::");
+ scope = g_path_get_basename (unit);
+ if (!g_str_has_prefix (scope, "app-flatpak-") ||
+ !g_str_has_suffix (scope, ".scope"))
+ return NULL;
+
+ name = scope + strlen("app-flatpak-");
+ dash = strchr (name, '-');
+ if (dash == NULL)
+ return NULL;
+ *dash = 0;
+
+ xdg_id = g_strdup (name);
+ g_debug ("Found xdg_id %s", xdg_id);
+
+ return xdg_id;
+}
+
+
/* Based on got_credentials_cb() from xdg-app source code */
static char *
get_xdg_id (guint32 pid)
@@ -188,7 +224,7 @@ get_xdg_id (guint32 pid)
char *xdg_id = NULL;
g_autofree char *path = NULL;
g_autofree char *content = NULL;
- gchar **lines;
+ g_auto(GStrv) lines = NULL;
int i;
path = g_strdup_printf ("/proc/%u/cgroup", pid);
@@ -197,6 +233,10 @@ get_xdg_id (guint32 pid)
return NULL;
lines = g_strsplit (content, "\n", -1);
+ xdg_id = parse_cgroup_v2 (lines);
+ if (xdg_id != NULL)
+ return xdg_id;
+
for (i = 0; lines[i] != NULL; i++) {
const char *unit = lines[i] + strlen ("1:name=systemd:");
g_autofree char *scope = NULL;
@@ -224,8 +264,6 @@ get_xdg_id (guint32 pid)
xdg_id = g_strdup (name);
}
- g_strfreev (lines);
-
return xdg_id;
}
......@@ -3,3 +3,4 @@
0003-Revert-Fixed-hang-on-startup-when-client-app-is-a-sy.patch
0004-service-Stop-the-client-for-system-applications-when.patch
fix-nowifi-query.patch
0006-client-info-Support-cgroup-v2.patch
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment