diff --git a/app/flatpak-builtins-build.c b/app/flatpak-builtins-build.c
index 9463c040..d7df2033 100644
--- a/app/flatpak-builtins-build.c
+++ b/app/flatpak-builtins-build.c
@@ -537,7 +537,7 @@ flatpak_builtin_build (int argc, char **argv, GCancellable *cancellable, GError
runtime_ref,
app_id_dir, app_context, NULL,
FALSE, TRUE, TRUE,
- &app_info_path,
+ &app_info_path, 0,
&instance_id_host_dir,
error))
return FALSE;
diff --git a/app/flatpak-builtins-run.c b/app/flatpak-builtins-run.c
index 3c82d671..a226eea2 100644
--- a/app/flatpak-builtins-run.c
+++ b/app/flatpak-builtins-run.c
@@ -57,6 +57,7 @@ static char *opt_commit;
static char *opt_runtime_commit;
static int opt_parent_pid;
static gboolean opt_parent_expose_pids;
+static int opt_instance_id_fd = -1;
static GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, N_("Arch to use"), N_("ARCH") },
@@ -81,6 +82,7 @@ static GOptionEntry options[] = {
{ "die-with-parent", 'p', 0, G_OPTION_ARG_NONE, &opt_die_with_parent, N_("Kill processes when the parent process dies"), NULL },
{ "parent-pid", 0, 0, G_OPTION_ARG_INT, &opt_parent_pid, N_("Use PID as parent pid for sharing namespaces"), N_("PID") },
{ "parent-expose-pids", 0, 0, G_OPTION_ARG_NONE, &opt_parent_expose_pids, N_("Make processes visible in parent namespace"), NULL },
+ { "instance-id-fd", 0, 0, G_OPTION_ARG_INT, &opt_instance_id_fd, N_("Write the instance ID to the given file descriptor"), NULL },
{ NULL }
};
@@ -310,6 +312,7 @@ flatpak_builtin_run (int argc, char **argv, GCancellable *cancellable, GError **
opt_command,
&argv[rest_argv_start + 1],
rest_argc - 1,
+ opt_instance_id_fd,
NULL,
cancellable,
error))
diff --git a/common/flatpak-installation.c b/common/flatpak-installation.c
index 5e030033..097250dc 100644
--- a/common/flatpak-installation.c
+++ b/common/flatpak-installation.c
@@ -705,7 +705,7 @@ flatpak_installation_launch_full (FlatpakInstallation *self,
run_flags,
NULL,
NULL,
- NULL, 0,
+ NULL, 0, 0,
&instance_dir,
cancellable, error))
return FALSE;
diff --git a/common/flatpak-run-private.h b/common/flatpak-run-private.h
index a5704c1c..d9c94360 100644
--- a/common/flatpak-run-private.h
+++ b/common/flatpak-run-private.h
@@ -164,6 +164,7 @@ gboolean flatpak_run_add_app_info_args (FlatpakBwrap *bwrap,
gboolean build,
gboolean devel,
char **app_info_path_out,
+ int instance_id_fd,
char **host_instance_id_host_dir_out,
GError **error);
@@ -179,6 +180,7 @@ gboolean flatpak_run_app (const char *app_ref,
const char *custom_command,
char *args[],
int n_args,
+ int instance_id_fd,
char **instance_dir_out,
GCancellable *cancellable,
GError **error);
diff --git a/common/flatpak-run.c b/common/flatpak-run.c
index 6178e8e1..6e8b9451 100644
--- a/common/flatpak-run.c
+++ b/common/flatpak-run.c
@@ -2198,6 +2198,7 @@ flatpak_run_add_app_info_args (FlatpakBwrap *bwrap,
gboolean build,
gboolean devel,
char **app_info_path_out,
+ int instance_id_fd,
char **instance_id_host_dir_out,
GError **error)
{
@@ -2372,6 +2373,38 @@ flatpak_run_add_app_info_args (FlatpakBwrap *bwrap,
return FALSE;
}
+ /* NOTE: It is important that this takes place after bwrapinfo.json is created,
+ otherwise start notifications in the portal may not work. */
+ if (instance_id_fd != -1)
+ {
+ gsize instance_id_position = 0;
+ gsize instance_id_size = strlen (instance_id);
+
+ while (instance_id_size > 0)
+ {
+ gssize bytes_written = write (instance_id_fd, instance_id + instance_id_position, instance_id_size);
+ if (G_UNLIKELY (bytes_written <= 0))
+ {
+ int errsv = bytes_written == -1 ? errno : ENOSPC;
+ if (errsv == EINTR)
+ continue;
+
+ close (fd);
+ close (fd2);
+ close (fd3);
+
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+ _("Failed to write to instance id fd: %s"), g_strerror (errsv));
+ return FALSE;
+ }
+
+ instance_id_position += bytes_written;
+ instance_id_size -= bytes_written;
+ }
+
+ close (instance_id_fd);
+ }
+
flatpak_bwrap_add_args_data_fd (bwrap, "--info-fd", fd3, NULL);
if (app_info_path_out != NULL)
@@ -3483,6 +3516,7 @@ flatpak_run_app (const char *app_ref,
const char *custom_command,
char *args[],
int n_args,
+ int instance_id_fd,
char **instance_dir_out,
GCancellable *cancellable,
GError **error)
@@ -3801,7 +3835,8 @@ flatpak_run_app (const char *app_ref,
app_ref_parts[1], app_ref_parts[3],
runtime_ref, app_id_dir, app_context, extra_context,
sandboxed, FALSE, flags & FLATPAK_RUN_FLAG_DEVEL,
- &app_info_path, &instance_id_host_dir, error))
+ &app_info_path, instance_id_fd, &instance_id_host_dir,
+ error))
return FALSE;
if (!flatpak_run_add_dconf_args (bwrap, app_ref_parts[1], metakey, error))
diff --git a/doc/flatpak-run.xml b/doc/flatpak-run.xml
index af9d1e41..82582d42 100644
--- a/doc/flatpak-run.xml
+++ b/doc/flatpak-run.xml
@@ -569,6 +569,14 @@ key=v1;v2;
+
+
+
+
+ Write the instance ID string to the given file descriptor.
+
+
+