From d48040f2c97c24940fc7831d90726f2d057f7dcc Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Fri, 19 Jan 2024 16:59:39 +0100 Subject: [PATCH] Move pidns to common --- src/xdp-utils.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/xdp-utils.c b/src/xdp-utils.c index e5718fed9..0a593273c 100644 --- a/src/xdp-utils.c +++ b/src/xdp-utils.c @@ -125,14 +125,15 @@ struct _XdpAppInfo { char *id; XdpAppInfoKind kind; + /* pid namespace mapping */ + GMutex pidns_lock; + ino_t pidns_id; + union { struct { GKeyFile *keyfile; - /* pid namespace mapping */ - GMutex pidns_lock; - ino_t pidns_id; } flatpak; struct { @@ -2357,24 +2358,16 @@ xdp_app_info_get_child_pid (JsonNode *root, } static gboolean -xdp_app_info_ensure_pidns (XdpAppInfo *app_info, - DIR *proc, - GError **error) +xdp_app_info_ensure_pidns_flatpak (XdpAppInfo *app_info, + DIR *proc, + GError **error) { g_autoptr(JsonNode) root = NULL; - g_autoptr(GMutexLocker) guard = NULL; xdp_autofd int fd = -1; pid_t pid; ino_t ns; int r; - g_assert (app_info->kind == XDP_APP_INFO_KIND_FLATPAK); - - guard = g_mutex_locker_new (&(app_info->u.flatpak.pidns_lock)); - - if (app_info->u.flatpak.pidns_id != 0) - return TRUE; - root = xdp_app_info_load_bwrap_info (app_info, error); if (root == NULL) return FALSE; @@ -2386,7 +2379,7 @@ xdp_app_info_ensure_pidns (XdpAppInfo *app_info, if (ns != 0) { g_debug ("Using pid namespace info from bwrap info"); - app_info->u.flatpak.pidns_id = ns; + app_info->pidns_id = ns; return TRUE; } @@ -2408,11 +2401,27 @@ xdp_app_info_ensure_pidns (XdpAppInfo *app_info, return FALSE; } - app_info->u.flatpak.pidns_id = ns; + app_info->pidns_id = ns; return TRUE; } +static gboolean +xdp_app_info_ensure_pidns (XdpAppInfo *app_info, + DIR *proc, + GError **error) +{ + g_autoptr(GMutexLocker) guard = g_mutex_locker_new (&(app_info->pidns_lock)); + + if (app_info->pidns_id != 0) + return TRUE; + + if (app_info->kind == XDP_APP_INFO_KIND_FLATPAK) + return xdp_app_info_ensure_pidns_flatpak (app_info, proc, error); + + return FALSE; +} + /* This is the trunk for xdp_app_info_map_pids()/xdp_app_info_map_tids() */ static gboolean app_info_map_pids (XdpAppInfo *app_info, @@ -2424,7 +2433,6 @@ app_info_map_pids (XdpAppInfo *app_info, gboolean ok; DIR *proc; uid_t uid; - ino_t ns; g_return_val_if_fail (app_info != NULL, FALSE); g_return_val_if_fail (pids != NULL, FALSE); @@ -2460,8 +2468,7 @@ app_info_map_pids (XdpAppInfo *app_info, */ uid = getuid (); - ns = app_info->u.flatpak.pidns_id; - ok = map_pids (proc, ns, pids, n_pids, uid, error); + ok = map_pids (proc, app_info->pidns_id, pids, n_pids, uid, error); out: closedir (proc);