Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XdpAppInfo cleanups #1619

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ else
geoclue_built_sources = []
endif

built_resources = gnome.compile_resources(
enums_sources = gnome.mkenums_simple(
'xdp-enum-types',
sources: files(
'xdp-app-info-private.h',
),
)

built_resources = enums_sources + gnome.compile_resources(
'xdg-desktop-resources',
'xdg-desktop-portal.gresource.xml',
c_name: '_xdg_desktop',
Expand All @@ -58,7 +65,7 @@ xdp_method_info_sources = files('xdp-method-info.c') + xdp_method_info_built_sou

xdp_utils_deps = []
xdp_utils_includes = include_directories('.')
xdp_utils_sources = files(
xdp_utils_sources = enums_sources + files(
'xdp-utils.c',
'xdp-app-info.c',
'xdp-sealed-fd.c',
Expand Down
41 changes: 24 additions & 17 deletions src/xdp-app-info-flatpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ flatpak_is_valid_name (const char *string)
return TRUE;
}

gboolean
static gboolean
xdp_app_info_flatpak_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
Expand Down Expand Up @@ -208,11 +208,11 @@ xdp_app_info_flatpak_remap_path (XdpAppInfo *app_info,
NULL);

/* For apps we translate /app and /usr to the installed locations.
Also, we need to rewrite to drop the /newroot prefix added by
bubblewrap for other files to work. See
https://github.com/projectatomic/bubblewrap/pull/172
for a bit more information on the /newroot issue.
*/
* Also, we need to rewrite to drop the /newroot prefix added by
* bubblewrap for other files to work. See
* https://github.com/projectatomic/bubblewrap/pull/172
* for a bit more information on the /newroot issue.
*/

if (g_str_has_prefix (path, "/newroot/"))
path = path + strlen ("/newroot");
Expand Down Expand Up @@ -723,6 +723,7 @@ xdp_app_info_flatpak_new (int pid,
GError **error)
{
g_autoptr (XdpAppInfoFlatpak) app_info_flatpak = NULL;
XdpAppInfoFlags flags = 0;
g_autofd int info_fd = -1;
struct stat stat_buf;
g_autoptr(GError) local_error = NULL;
Expand All @@ -731,8 +732,6 @@ xdp_app_info_flatpak_new (int pid,
const char *group;
g_autofree char *id = NULL;
g_autofree char *instance = NULL;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;
g_auto(GStrv) shared = NULL;
gboolean has_network;
g_autofd int bwrap_pidfd = -1;
Expand Down Expand Up @@ -787,9 +786,6 @@ xdp_app_info_flatpak_new (int pid,
if (instance == NULL)
return NULL;

desktop_id = g_strconcat (id, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));

shared = g_key_file_get_string_list (metadata,
FLATPAK_METADATA_GROUP_CONTEXT,
FLATPAK_METADATA_KEY_SHARED,
Expand All @@ -807,18 +803,29 @@ xdp_app_info_flatpak_new (int pid,
/* flatpak has a xdg-dbus-proxy running which means we can't get the pidfd
* of the connected process but we can get the pidfd of the bwrap instance
* instead. This is okay because it has the same namespaces as the calling
* process. */
* process.
*/
bwrap_pidfd = get_bwrap_pidfd (instance, error);
if (bwrap_pidfd == -1)
return NULL;

/* TODO: we can use pidfd to make sure we didn't race for sure */

app_info_flatpak = g_object_new (XDP_TYPE_APP_INFO_FLATPAK, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_flatpak),
FLATPAK_ENGINE_ID, id, instance,
bwrap_pidfd, gappinfo,
TRUE, has_network, TRUE);
flags |= XDP_APP_INFO_FLAG_REQUIRE_GAPPINFO;
flags |= XDP_APP_INFO_FLAG_SUPPORTS_OPATH;
if (has_network)
flags |= XDP_APP_INFO_FLAG_HAS_NETWORK;

app_info_flatpak = g_initable_new (XDP_TYPE_APP_INFO_FLATPAK,
NULL,
error,
"engine", FLATPAK_ENGINE_ID,
"flags", flags,
"id", id,
"instance", instance,
"pidfd", bwrap_pidfd,
NULL);

app_info_flatpak->flatpak_info = g_steal_pointer (&metadata);

return XDP_APP_INFO (g_steal_pointer (&app_info_flatpak));
Expand Down
69 changes: 29 additions & 40 deletions src/xdp-app-info-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ xdp_app_info_host_get_usb_queries (XdpAppInfo *app_info)
return app_info_host->usb_queries;
}

gboolean
static gboolean
xdp_app_info_host_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
return TRUE;
}

gboolean
static gboolean
xdp_app_info_host_validate_autostart (XdpAppInfo *app_info,
GKeyFile *keyfile,
const char * const *autostart_exec,
Expand All @@ -61,7 +61,7 @@ xdp_app_info_host_validate_autostart (XdpAppInfo *app_info,
return TRUE;
}

gboolean
static gboolean
xdp_app_info_host_validate_dynamic_launcher (XdpAppInfo *app_info,
GKeyFile *key_file,
GError **error)
Expand Down Expand Up @@ -185,56 +185,45 @@ get_appid_from_pid (pid_t pid)
#endif /* HAVE_LIBSYSTEMD */
}

static XdpAppInfoHost *
xdp_app_info_host_new_full (const char *app_id,
int pidfd,
GAppInfo *gappinfo)
{
XdpAppInfoHost *app_info_host;

app_info_host = g_object_new (XDP_TYPE_APP_INFO_HOST, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_host),
/* engine, app id, instance */
NULL, app_id, NULL,
pidfd, gappinfo,
/* supports_opath */ TRUE,
/* has_network */ TRUE,
/* requires_pid_mapping */ FALSE);

return app_info_host;
}

XdpAppInfo *
xdp_app_info_host_new_registered (int pidfd,
const char *app_id,
GError **error)
{
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;

desktop_id = g_strconcat (app_id, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));
if (!gappinfo)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"App info not found for '%s'", app_id);
return NULL;
}

return XDP_APP_INFO (xdp_app_info_host_new_full (app_id, pidfd, gappinfo));
g_autoptr(XdpAppInfoHost) app_info_host = NULL;

app_info_host = g_initable_new (XDP_TYPE_APP_INFO_HOST,
NULL,
error,
"engine", NULL,
"id", app_id,
"pidfd", pidfd,
"flags", XDP_APP_INFO_FLAG_HAS_NETWORK |
XDP_APP_INFO_FLAG_SUPPORTS_OPATH |
XDP_APP_INFO_FLAG_REQUIRE_GAPPINFO,
NULL);

return XDP_APP_INFO (g_steal_pointer (&app_info_host));
}

XdpAppInfo *
xdp_app_info_host_new (int pid,
int pidfd)
{
g_autoptr(XdpAppInfoHost) app_info_host = NULL;
g_autofree char *app_id = NULL;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;

app_id = get_appid_from_pid (pid);
desktop_id = g_strconcat (app_id, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));

return XDP_APP_INFO (xdp_app_info_host_new_full (app_id, pidfd, gappinfo));
app_info_host = g_initable_new (XDP_TYPE_APP_INFO_HOST,
NULL,
NULL,
"engine", NULL,
"id", app_id,
"pidfd", pidfd,
"flags", XDP_APP_INFO_FLAG_HAS_NETWORK |
XDP_APP_INFO_FLAG_SUPPORTS_OPATH,
NULL);

return XDP_APP_INFO (g_steal_pointer (&app_info_host));
}
18 changes: 9 additions & 9 deletions src/xdp-app-info-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

#include "xdp-app-info.h"

typedef enum
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typedef enum _XdpAppInfoFlags

tbh, I'm unsure why this even is a pattern in e.g. mutter but at least it makes it easy to grep for the declaration...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice because one can use ctags to go directly to the actual struct, instead of the typedef, by adding a _ in front of the name.

{
XDP_APP_INFO_FLAG_HAS_NETWORK = (1 << 0),
XDP_APP_INFO_FLAG_SUPPORTS_OPATH = (1 << 1),
XDP_APP_INFO_FLAG_REQUIRE_GAPPINFO = (1 << 2),
} XdpAppInfoFlags;

struct _XdpAppInfoClass
{
GObjectClass parent_class;
Expand All @@ -44,14 +51,7 @@ struct _XdpAppInfoClass
gboolean (*validate_dynamic_launcher) (XdpAppInfo *app_info,
GKeyFile *key_file,
GError **error);

GAppInfo * (*create_gappinfo) (XdpAppInfo *app_info);
};

void xdp_app_info_initialize (XdpAppInfo *app_info,
const char *engine,
const char *app_id,
const char *instance,
int pidfd,
GAppInfo *gappinfo,
gboolean supports_opath,
gboolean has_network,
gboolean requires_pid_mapping);
112 changes: 104 additions & 8 deletions src/xdp-app-info-snap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,103 @@
struct _XdpAppInfoSnap
{
XdpAppInfo parent;

char *desktop_file;
};

G_DEFINE_FINAL_TYPE (XdpAppInfoSnap, xdp_app_info_snap, XDP_TYPE_APP_INFO)

enum
{
PROP_0,
PROP_DESKTOP_FILE,
N_PROPS,
};

static GParamSpec *properties [N_PROPS];

static GAppInfo *
xdp_app_info_snap_create_gappinfo (XdpAppInfo *app_info)
{
XdpAppInfoSnap *app_info_snap = XDP_APP_INFO_SNAP (app_info);
g_autoptr(GAppInfo) gappinfo = NULL;

gappinfo =
G_APP_INFO (g_desktop_app_info_new (app_info_snap->desktop_file));
g_assert (G_IS_APP_INFO (gappinfo));

return g_steal_pointer (&gappinfo);
}

static void
xdp_app_info_snap_finalize (GObject *object)
{
XdpAppInfoSnap *app_info_snap = XDP_APP_INFO_SNAP (object);

g_clear_pointer (&app_info_snap->desktop_file, g_free);

G_OBJECT_CLASS (xdp_app_info_snap_parent_class)->finalize (object);
}

static void
xdp_app_info_snap_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XdpAppInfoSnap *app_info_snap = XDP_APP_INFO_SNAP (object);

switch (prop_id)
{
case PROP_DESKTOP_FILE:
g_value_set_string (value, app_info_snap->desktop_file);
break;

default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}

static void
xdp_app_info_snap_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
XdpAppInfoSnap *app_info_snap = XDP_APP_INFO_SNAP (object);

switch (prop_id)
{
case PROP_DESKTOP_FILE:
g_assert (app_info_snap->desktop_file == NULL);
app_info_snap->desktop_file = g_value_dup_string (value);
break;

default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}

static void
xdp_app_info_snap_class_init (XdpAppInfoSnapClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
XdpAppInfoClass *app_info_class = XDP_APP_INFO_CLASS (klass);

object_class->finalize = xdp_app_info_snap_finalize;
object_class->get_property = xdp_app_info_snap_get_property;
object_class->set_property = xdp_app_info_snap_set_property;

app_info_class->create_gappinfo = xdp_app_info_snap_create_gappinfo;

properties[PROP_DESKTOP_FILE] =
g_param_spec_string ("desktop-file", NULL, NULL,
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);

g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
Expand Down Expand Up @@ -175,7 +265,7 @@ xdp_app_info_snap_new (int pid,
g_autofree char *snap_name = NULL;
g_autofree char *snap_id = NULL;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;
XdpAppInfoFlags flags = 0;
gboolean has_network;

/* Check the process's cgroup membership to fail quickly for non-snaps */
Expand Down Expand Up @@ -215,18 +305,24 @@ xdp_app_info_snap_new (int pid,
if (desktop_id == NULL)
return NULL;

gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));

has_network = g_key_file_get_boolean (metadata,
SNAP_METADATA_GROUP_INFO,
SNAP_METADATA_KEY_NETWORK,
NULL);

app_info_snap = g_object_new (XDP_TYPE_APP_INFO_SNAP, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_snap),
"io.snapcraft", snap_id, NULL,
pidfd, gappinfo,
FALSE, has_network, TRUE);
flags = XDP_APP_INFO_FLAG_REQUIRE_GAPPINFO;
if (has_network)
flags |= XDP_APP_INFO_FLAG_HAS_NETWORK;

app_info_snap = g_initable_new (XDP_TYPE_APP_INFO_SNAP,
NULL,
error,
"engine", "io.snapcraft",
"id", snap_id,
"pidfd", pidfd,
"flags", flags,
"desktop-file", desktop_id,
NULL);

return XDP_APP_INFO (g_steal_pointer (&app_info_snap));
}
Loading
Loading