[gimp] app, libgimp: use gimp_spawn_async() when spawning plug-ins



commit b9e629abbb2a40aa34cd41e7f093b7171bc249db
Author: Ell <ell_se yahoo com>
Date:   Mon Mar 5 01:40:59 2018 -0500

    app, libgimp: use gimp_spawn_async() when spawning plug-ins
    
    In gimp_plug_in_open(), use gimp_spawn_async(), added in the
    previous commit, instead of g_spawn_async().  See the previous
    commit for the rationale.
    
    Since gimp_spawn_async() doesn't provide a mechanism to perform any
    cleanup in the child before exec()ing, move the closing of the
    parent's end of the read/write pipes from the app to the plug-in's
    gimp_main(), passing the relevant file descriptors to the plug-in
    through argv.

 app/plug-in/gimpplugin.c |   52 ++++++++++++++++++---------------------------
 libgimp/gimp.c           |   42 ++++++++++++++++++++++++++----------
 2 files changed, 51 insertions(+), 43 deletions(-)
---
diff --git a/app/plug-in/gimpplugin.c b/app/plug-in/gimpplugin.c
index 5a3f20f..38dc77d 100644
--- a/app/plug-in/gimpplugin.c
+++ b/app/plug-in/gimpplugin.c
@@ -75,6 +75,7 @@
 #include "plug-in-types.h"
 
 #include "core/gimp.h"
+#include "core/gimp-spawn.h"
 #include "core/gimpprogress.h"
 
 #include "pdb/gimppdbcontext.h"
@@ -108,11 +109,6 @@ static gboolean   gimp_plug_in_recv_message  (GIOChannel   *channel,
                                               GIOCondition  cond,
                                               gpointer      data);
 
-#if !defined(G_OS_WIN32) && !defined (G_WITH_CYGWIN)
-static void       gimp_plug_in_prep_for_exec (gpointer      data);
-#else
-#define           gimp_plug_in_prep_for_exec  NULL
-#endif
 
 
 G_DEFINE_TYPE (GimpPlugIn, gimp_plug_in, GIMP_TYPE_OBJECT)
@@ -226,7 +222,8 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
   gchar       **argv;
   gint          argc;
   gchar        *interp, *interp_arg;
-  gchar        *read_fd, *write_fd;
+  gchar        *his_read_fd, *his_write_fd;
+  gchar        *my_read_fd, *my_write_fd;
   const gchar  *mode;
   gchar        *stm;
   GError       *error = NULL;
@@ -290,10 +287,15 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
 
   /* Remember the file descriptors for the pipes.
    */
-  read_fd  = g_strdup_printf ("%d",
-                              g_io_channel_unix_get_fd (plug_in->his_read));
-  write_fd = g_strdup_printf ("%d",
-                              g_io_channel_unix_get_fd (plug_in->his_write));
+  his_read_fd  = g_strdup_printf ("%d",
+                                  g_io_channel_unix_get_fd (plug_in->his_read));
+  his_write_fd = g_strdup_printf ("%d",
+                                  g_io_channel_unix_get_fd (plug_in->his_write));
+
+  my_read_fd   = g_strdup_printf ("%d",
+                                  g_io_channel_unix_get_fd (plug_in->my_read));
+  my_write_fd  = g_strdup_printf ("%d",
+                                  g_io_channel_unix_get_fd (plug_in->my_write));
 
   switch (call_mode)
     {
@@ -333,8 +335,10 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
 
   args[argc++] = progname;
   args[argc++] = "-gimp";
-  args[argc++] = read_fd;
-  args[argc++] = write_fd;
+  args[argc++] = his_read_fd;
+  args[argc++] = his_write_fd;
+  args[argc++] = my_read_fd;
+  args[argc++] = my_write_fd;
   args[argc++] = mode;
   args[argc++] = stm;
   args[argc++] = NULL;
@@ -364,10 +368,7 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
   /* Fork another process. We'll remember the process id so that we
    * can later use it to kill the filter if necessary.
    */
-  if (! g_spawn_async (NULL, argv, envp, spawn_flags,
-                       gimp_plug_in_prep_for_exec, plug_in,
-                       &plug_in->pid,
-                       &error))
+  if (! gimp_spawn_async (argv, envp, spawn_flags, &plug_in->pid, &error))
     {
       gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                     "Unable to run plug-in \"%s\"\n(%s)\n\n%s",
@@ -408,8 +409,10 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
   if (debug)
     g_free (argv);
 
-  g_free (read_fd);
-  g_free (write_fd);
+  g_free (his_read_fd);
+  g_free (his_write_fd);
+  g_free (my_read_fd);
+  g_free (my_write_fd);
   g_free (stm);
   g_free (interp);
   g_free (interp_arg);
@@ -728,19 +731,6 @@ gimp_plug_in_flush (GIOChannel *channel,
   return TRUE;
 }
 
-#if !defined(G_OS_WIN32) && !defined (G_WITH_CYGWIN)
-
-static void
-gimp_plug_in_prep_for_exec (gpointer data)
-{
-  GimpPlugIn *plug_in = data;
-
-  g_clear_pointer (&plug_in->my_read,  g_io_channel_unref);
-  g_clear_pointer (&plug_in->my_write, g_io_channel_unref);
-}
-
-#endif
-
 GimpPlugInProcFrame *
 gimp_plug_in_get_proc_frame (GimpPlugIn *plug_in)
 {
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index e1de1ef..d15cd89 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -253,6 +253,20 @@ gimp_main (const GimpPlugInInfo *info,
            gint                  argc,
            gchar                *argv[])
 {
+  enum
+  {
+    ARG_PROGNAME,
+    ARG_GIMP,
+    ARG_READ_FD,
+    ARG_WRITE_FD,
+    ARG_PARENT_READ_FD,
+    ARG_PARENT_WRITE_FD,
+    ARG_MODE,
+    ARG_STACK_TRACE_MODE,
+
+    N_ARGS
+  };
+
   gchar       *basename;
   const gchar *env_string;
   gchar       *debug_string;
@@ -376,7 +390,7 @@ gimp_main (const GimpPlugInInfo *info,
                */
               GString *s;
 
-              s = g_string_new (argv[0]);
+              s = g_string_new (argv[ARG_PROGNAME]);
 
               for (j = 1; j <= i; j++)
                 {
@@ -384,7 +398,7 @@ gimp_main (const GimpPlugInInfo *info,
                   s = g_string_append (s, argv[j]);
                 }
 
-              argv[0] = s->str;
+              argv[ARG_PROGNAME] = s->str;
 
               /* Move rest of argv down */
               for (j = 1; j < argc - i; j++)
@@ -403,16 +417,16 @@ gimp_main (const GimpPlugInInfo *info,
 
   PLUG_IN_INFO = *info;
 
-  if ((argc != 6) || (strcmp (argv[1], "-gimp") != 0))
+  if ((argc != N_ARGS) || (strcmp (argv[ARG_GIMP], "-gimp") != 0))
     {
       g_printerr ("%s is a GIMP plug-in and must be run by GIMP to be used\n",
-                  argv[0]);
+                  argv[ARG_PROGNAME]);
       return 1;
     }
 
   gimp_env_init (TRUE);
 
-  progname = argv[0];
+  progname = argv[ARG_PROGNAME];
 
   basename = g_path_get_basename (progname);
 
@@ -461,7 +475,7 @@ gimp_main (const GimpPlugInInfo *info,
 
   g_free (basename);
 
-  stack_trace_mode = (GimpStackTraceMode) CLAMP (atoi (argv[5]),
+  stack_trace_mode = (GimpStackTraceMode) CLAMP (atoi (argv[ARG_STACK_TRACE_MODE]),
                                                  GIMP_STACK_TRACE_NEVER,
                                                  GIMP_STACK_TRACE_ALWAYS);
 
@@ -487,11 +501,15 @@ gimp_main (const GimpPlugInInfo *info,
 #endif
 
 #ifdef G_OS_WIN32
-  _readchannel  = g_io_channel_win32_new_fd (atoi (argv[2]));
-  _writechannel = g_io_channel_win32_new_fd (atoi (argv[3]));
+  _readchannel  = g_io_channel_win32_new_fd (atoi (argv[ARG_READ_FD]));
+  _writechannel = g_io_channel_win32_new_fd (atoi (argv[ARG_WRITE_FD]));
 #else
-  _readchannel  = g_io_channel_unix_new (atoi (argv[2]));
-  _writechannel = g_io_channel_unix_new (atoi (argv[3]));
+  _readchannel  = g_io_channel_unix_new (atoi (argv[ARG_READ_FD]));
+  _writechannel = g_io_channel_unix_new (atoi (argv[ARG_WRITE_FD]));
+
+  /* Close parent end of pipes */
+  close (atoi (argv[ARG_PARENT_READ_FD]));
+  close (atoi (argv[ARG_PARENT_WRITE_FD]));
 #endif
 
   g_io_channel_set_encoding (_readchannel, NULL, NULL);
@@ -577,7 +595,7 @@ gimp_main (const GimpPlugInInfo *info,
       g_log_set_always_fatal (fatal_mask);
     }
 
-  if (strcmp (argv[4], "-query") == 0)
+  if (strcmp (argv[ARG_MODE], "-query") == 0)
     {
       if (PLUG_IN_INFO.init_proc)
         gp_has_init_write (_writechannel, NULL);
@@ -593,7 +611,7 @@ gimp_main (const GimpPlugInInfo *info,
       return EXIT_SUCCESS;
     }
 
-  if (strcmp (argv[4], "-init") == 0)
+  if (strcmp (argv[ARG_MODE], "-init") == 0)
     {
       if (gimp_debug_flags & GIMP_DEBUG_INIT)
         gimp_debug_stop ();


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