From 36d5134e97ee118cb474ef6198ad3180883d0fe0 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 3 Oct 2018 23:16:06 -0400 Subject: [PATCH] enter: Allow giving application ID With the instance infrastructure, we can go from the ID of a running application to its monitor PID, so we can accept the application ID here. Whats more, we can offer completion for it. Note that completion only works if you use sudo -E, since the flatpak used for complcations needs to see the session environment to find running instances. Closes: #2181 Approved by: alexlarsson --- app/flatpak-builtins-enter.c | 35 +++++++++++++++++++++++++++++++---- doc/flatpak-enter.xml | 11 +++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/app/flatpak-builtins-enter.c b/app/flatpak-builtins-enter.c index e91cefde..f758497a 100644 --- a/app/flatpak-builtins-enter.c +++ b/app/flatpak-builtins-enter.c @@ -35,6 +35,7 @@ #include "flatpak-utils-private.h" #include "flatpak-dbus-generated.h" #include "flatpak-run-private.h" +#include "flatpak-instance.h" static GOptionEntry options[] = { @@ -78,8 +79,10 @@ flatpak_builtin_enter (int argc, struct stat stat_buf; uid_t uid; gid_t gid; + g_autoptr(GPtrArray) instances = NULL; + int j; - context = g_option_context_new (_("SANDBOXEDPID [COMMAND [args...]] - Run a command inside a running sandbox")); + context = g_option_context_new (_("INSTANCE [COMMAND [args...]] - Run a command inside a running sandbox")); g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); rest_argc = 0; @@ -100,7 +103,7 @@ flatpak_builtin_enter (int argc, if (rest_argc < 2) { - usage_error (context, _("SANDBOXEDPID and COMMAND must be specified"), error); + usage_error (context, _("INSTANCE and COMMAND must be specified"), error); return FALSE; } @@ -108,11 +111,25 @@ flatpak_builtin_enter (int argc, if (geteuid () != 0) g_printerr ("%s\n", _("Not running as root, may be unable to enter namespace")); + pid = 0; pid_s = argv[rest_argv_start]; + i = atoi (pid_s); + instances = flatpak_instance_get_all (); + for (j = 0; j < instances->len; j++) + { + FlatpakInstance *instance = (FlatpakInstance *)g_ptr_array_index (instances, j); + + if (i == flatpak_instance_get_pid (instance) || + strcmp (pid_s, flatpak_instance_get_app (instance)) == 0 || + strcmp (pid_s, flatpak_instance_get_id (instance)) == 0) + { + pid = flatpak_instance_get_pid (instance); + break; + } + } - pid = atoi (pid_s); if (pid <= 0) - return flatpak_fail (error, _("Invalid pid %s"), pid_s); + return flatpak_fail (error, _("%s is neither a pid nor an application or instance ID"), pid_s); stat_path = g_strdup_printf ("/proc/%d/root", pid); if (stat (stat_path, &stat_buf)) @@ -243,6 +260,8 @@ gboolean flatpak_complete_enter (FlatpakCompletion *completion) { g_autoptr(GOptionContext) context = NULL; + g_autoptr(GPtrArray) instances = NULL; + int i; context = g_option_context_new (""); if (!flatpak_option_context_parse (context, options, &completion->argc, &completion->argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, NULL, NULL)) @@ -254,6 +273,14 @@ flatpak_complete_enter (FlatpakCompletion *completion) case 1: flatpak_complete_options (completion, global_entries); flatpak_complete_options (completion, options); + + instances = flatpak_instance_get_all (); + for (i = 0; i < instances->len; i++) + { + FlatpakInstance *instance = (FlatpakInstance *)g_ptr_array_index (instances, i); + flatpak_complete_word (completion, "%s ", flatpak_instance_get_app (instance)); + flatpak_complete_word (completion, "%s ", flatpak_instance_get_id (instance)); + } break; default: diff --git a/doc/flatpak-enter.xml b/doc/flatpak-enter.xml index ebda6c7f..2ea9bc31 100644 --- a/doc/flatpak-enter.xml +++ b/doc/flatpak-enter.xml @@ -32,7 +32,7 @@ flatpak enter OPTION - MONITORPID + INSTANCE COMMAND ARG @@ -43,16 +43,19 @@ Enter a running sandbox. - SANDBOXEDPID must be the pid of a process running in a flatpak sandbox. + INSTANCE must be either the pid of a process running + in a flatpak sandbox, or the ID of a running application or a the instance ID + of a running sandbox. COMMAND is the command to run in the sandbox. Extra arguments are passed on to the command. - This creates a new process within the running sandbox, with the same environment. This is useful - when you want to debug a problem with a running application. + This creates a new process within the running sandbox, with the same environment. + This is useful when you want to debug a problem with a running application. This command requires extra privileges, so must be run as root or via e.g. sudo. + To get TAB completion for the application ID, use sudo -E.