[console] tab: fix ssh detection when invoked with an absolute path



commit 381e3e87fc55f21ccbb8b4dadfd08176ee9bfa0e
Author: Anders Kaseorg <andersk mit edu>
Date:   Mon Mar 28 22:40:54 2022 -0700

    tab: fix ssh detection when invoked with an absolute path
    
    Signed-off-by: Anders Kaseorg <andersk mit edu>

 src/kgx-process.c | 33 ++++++++++++++++++++++++++-------
 src/kgx-process.h |  1 +
 src/kgx-tab.c     | 11 ++++++++---
 3 files changed, 35 insertions(+), 10 deletions(-)
---
diff --git a/src/kgx-process.c b/src/kgx-process.c
index 1ceddae..c173388 100644
--- a/src/kgx-process.c
+++ b/src/kgx-process.c
@@ -38,12 +38,14 @@ struct _KgxProcess {
   GPid    parent;
   gint32  uid;
   gint32  euid;
+  GStrv   argv;
   char   *exec;
 };
 
 static void
 clear_process (KgxProcess *self)
 {
+  g_clear_pointer (&self->argv, g_strfreev);
   g_clear_pointer (&self->exec, g_free);
 }
 
@@ -164,11 +166,33 @@ kgx_process_get_parent (KgxProcess *self)
   return self->parent;
 }
 
+/**
+ * kgx_process_get_argv:
+ * @self: the #KgxProcess
+ *
+ * Get the command line used to invoke to process, as an array
+ *
+ * Stability: Private
+ */
+inline GStrv
+kgx_process_get_argv (KgxProcess *self)
+{
+  g_return_val_if_fail (self != NULL, NULL);
+
+  if (G_LIKELY (self->argv == NULL)) {
+    glibtop_proc_args args_size;
+
+    self->argv = glibtop_get_proc_argv (&args_size, self->pid, 0);
+  }
+
+  return self->argv;
+}
+
 /**
  * kgx_process_get_exec:
  * @self: the #KgxProcess
  *
- * Get the command line used to invoke to process
+ * Get the command line used to invoke to process, as a space-joined string
  *
  * Stability: Private
  */
@@ -178,12 +202,7 @@ kgx_process_get_exec (KgxProcess *self)
   g_return_val_if_fail (self != NULL, NULL);
 
   if (G_LIKELY (self->exec == NULL)) {
-    g_auto(GStrv)     args = NULL;
-    glibtop_proc_args args_size;
-
-    args = glibtop_get_proc_argv (&args_size, self->pid, 0);
-
-    self->exec = g_strjoinv (" ", args);
+    self->exec = g_strjoinv (" ", kgx_process_get_argv (self));
   }
 
   return self->exec;
diff --git a/src/kgx-process.h b/src/kgx-process.h
index c7d6fcc..9b7838b 100644
--- a/src/kgx-process.h
+++ b/src/kgx-process.h
@@ -35,6 +35,7 @@ GPid        kgx_process_get_pid     (KgxProcess *self);
 gint32      kgx_process_get_uid     (KgxProcess *self);
 gboolean    kgx_process_get_is_root (KgxProcess *self);
 GPid        kgx_process_get_parent  (KgxProcess *self);
+GStrv       kgx_process_get_argv    (KgxProcess *self);
 const char *kgx_process_get_exec    (KgxProcess *self);
 GType       kgx_process_get_type    (void);
 void        kgx_process_unref       (KgxProcess *self);
diff --git a/src/kgx-tab.c b/src/kgx-tab.c
index cc3345c..bfc9d88 100644
--- a/src/kgx-tab.c
+++ b/src/kgx-tab.c
@@ -968,7 +968,8 @@ kgx_tab_push_child (KgxTab     *self,
 {
   GtkStyleContext *context;
   GPid pid = 0;
-  const char *exec = NULL;
+  GStrv argv;
+  g_autofree char *program = NULL;
   KgxStatus new_status = KGX_NONE;
   KgxTabPrivate *priv;
 
@@ -978,9 +979,13 @@ kgx_tab_push_child (KgxTab     *self,
 
   context = gtk_widget_get_style_context (GTK_WIDGET (self));
   pid = kgx_process_get_pid (process);
-  exec = kgx_process_get_exec (process);
+  argv = kgx_process_get_argv (process);
 
-  if (G_UNLIKELY (g_str_has_prefix (exec, "ssh "))) {
+  if (G_LIKELY (argv[0] != NULL)) {
+    program = g_path_get_basename (argv[0]);
+  }
+
+  if (G_UNLIKELY (g_strcmp0 (program, "ssh") == 0)) {
     new_status |= push_type (priv->remote, pid, NULL, context, KGX_REMOTE);
   }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]