Skip to content

Commit

Permalink
Move pidns to common
Browse files Browse the repository at this point in the history
  • Loading branch information
swick committed Jan 19, 2024
1 parent 99f7894 commit d48040f
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d48040f

Please sign in to comment.