From 08d05a30a3ca7002352104ad574552d288d7b429 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 10 Jul 2015 17:49:59 +0200 Subject: [PATCH] Automatically start and mount document portal in sandbox --- app/xdg-app-builtins-build.c | 2 +- app/xdg-app-builtins-run.c | 26 +++++++++++++++++++++++++- document-portal/xdp-main.c | 13 ++++++------- lib/xdg-app-run.c | 19 ++++++++++++++++++- lib/xdg-app-run.h | 1 + 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/xdg-app-builtins-build.c b/app/xdg-app-builtins-build.c index 8fccfa5ce..92a01ca6f 100644 --- a/app/xdg-app-builtins-build.c +++ b/app/xdg-app-builtins-build.c @@ -154,7 +154,7 @@ xdg_app_builtin_build (int argc, char **argv, GCancellable *cancellable, GError xdg_app_context_allow_host_fs (app_context); - xdg_app_run_add_environment_args (argv_array, NULL, app_id, + xdg_app_run_add_environment_args (argv_array, NULL, NULL, app_id, app_context, NULL); g_ptr_array_add (argv_array, g_strdup ("-a")); diff --git a/app/xdg-app-builtins-run.c b/app/xdg-app-builtins-run.c index 9ece439b0..9c392af0e 100644 --- a/app/xdg-app-builtins-run.c +++ b/app/xdg-app-builtins-run.c @@ -164,6 +164,7 @@ xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError ** g_autofree char *runtime_ref = NULL; g_autofree char *app_ref = NULL; g_autofree char *path = NULL; + g_autofree char *doc_mount_path = NULL; g_autoptr(GKeyFile) metakey = NULL; g_autoptr(GKeyFile) runtime_metakey = NULL; g_autoptr(GPtrArray) argv_array = NULL; @@ -178,6 +179,7 @@ xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError ** int sync_proxy_pipes[2]; g_autoptr(XdgAppContext) arg_context = NULL; g_autoptr(XdgAppContext) app_context = NULL; + g_autoptr(GDBusConnection) session_bus = NULL; context = g_option_context_new ("APP [args...] - Run an app"); @@ -300,7 +302,29 @@ xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError ** else g_ptr_array_add (argv_array, g_strdup ("-r")); - xdg_app_run_add_environment_args (argv_array, dbus_proxy_argv, + session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); + if (session_bus) + { + g_autoptr (GDBusMessage) reply = NULL; + g_autoptr (GDBusMessage) msg = g_dbus_message_new_method_call ("org.freedesktop.portal.Documents", + "/org/freedesktop/portal/documents", + "org.freedesktop.portal.Documents", + "GetMountPoint"); + g_dbus_message_set_body (msg, g_variant_new ("()")); + reply = g_dbus_connection_send_message_with_reply_sync (session_bus, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, + 30000, + NULL, + NULL, + NULL); + if (reply) + { + g_variant_get (g_dbus_message_get_body (reply), + "(^ay)", &doc_mount_path); + } + } + + xdg_app_run_add_environment_args (argv_array, dbus_proxy_argv, doc_mount_path, app, app_context, app_id_dir); g_ptr_array_add (argv_array, g_strdup ("-b")); diff --git a/document-portal/xdp-main.c b/document-portal/xdp-main.c index fc250f1e8..bd0d868a9 100644 --- a/document-portal/xdp-main.c +++ b/document-portal/xdp-main.c @@ -388,13 +388,6 @@ on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { - g_autoptr(GError) error = NULL; - if (!xdp_fuse_init (db, &error)) - { - g_printerr ("fuse init failed: %s\n", error->message); - exit (1); - } - } static void @@ -544,6 +537,12 @@ main (int argc, introspection_data = g_dbus_node_info_new_for_xml (g_bytes_get_data (introspection_bytes, NULL), NULL); + if (!xdp_fuse_init (db, &error)) + { + g_printerr ("fuse init failed: %s\n", error->message); + return 1; + } + owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, "org.freedesktop.portal.Documents", G_BUS_NAME_OWNER_FLAGS_NONE, diff --git a/lib/xdg-app-run.c b/lib/xdg-app-run.c index 2d4a7122e..87a9b1eb1 100644 --- a/lib/xdg-app-run.c +++ b/lib/xdg-app-run.c @@ -1065,6 +1065,7 @@ xdg_app_add_bus_filters (GPtrArray *dbus_proxy_argv, void xdg_app_run_add_environment_args (GPtrArray *argv_array, GPtrArray *dbus_proxy_argv, + const char *doc_mount_path, const char *app_id, XdgAppContext *context, GFile *app_id_dir) @@ -1103,17 +1104,34 @@ xdg_app_run_add_environment_args (GPtrArray *argv_array, g_debug ("Allowing host-fs access"); opts[i++] = 'f'; home_access = TRUE; + + if (doc_mount_path) + { + g_ptr_array_add (argv_array, g_strdup ("-b")); + g_ptr_array_add (argv_array, g_strdup_printf ("/run/user/%d/doc=%s", getuid(), doc_mount_path)); + } } else if (g_hash_table_lookup (context->filesystems, "home")) { g_debug ("Allowing homedir access"); opts[i++] = 'H'; home_access = TRUE; + if (doc_mount_path) + { + g_ptr_array_add (argv_array, g_strdup ("-b")); + g_ptr_array_add (argv_array, g_strdup_printf ("/run/user/%d/doc=%s/in-homedir", getuid(), doc_mount_path)); + } } else { /* Enable persistant mapping only if no access to real home dir */ + if (doc_mount_path && app_id) + { + g_ptr_array_add (argv_array, g_strdup ("-b")); + g_ptr_array_add (argv_array, g_strdup_printf ("/run/user/%d/doc=%s/by-app/%s", getuid(), doc_mount_path, app_id)); + } + g_hash_table_iter_init (&iter, context->persistent); while (g_hash_table_iter_next (&iter, &key, NULL)) { @@ -1126,7 +1144,6 @@ xdg_app_run_add_environment_args (GPtrArray *argv_array, g_ptr_array_add (argv_array, g_strdup ("-B")); g_ptr_array_add (argv_array, g_strdup_printf ("%s=%s", dest, src)); } - } g_hash_table_iter_init (&iter, context->filesystems); diff --git a/lib/xdg-app-run.h b/lib/xdg-app-run.h index 32f565372..28b44b439 100644 --- a/lib/xdg-app-run.h +++ b/lib/xdg-app-run.h @@ -52,6 +52,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(XdgAppContext, xdg_app_context_free) void xdg_app_run_add_environment_args (GPtrArray *argv_array, GPtrArray *dbus_proxy_argv, + const char *doc_mount_path, const char *app_id, XdgAppContext *context, GFile *app_id_dir);