[glib] GSubprocess win32 fixups



commit 358588ed2ad70e68df3d4de2837b0f0e2073910f
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Oct 21 14:55:21 2013 -0400

    GSubprocess win32 fixups
    
    Note: we go out of our way not to pass a child setup function on win32
    (even if it does nothing) because we get a g_warning() from gspawn if we
    do so.

 gio/gsubprocess.c                |   41 ++++++++++++++++++++++++++++++++++++-
 gio/gsubprocesslauncher.c        |   31 ++++++++++++++++++++--------
 gio/gwin32inputstream.c          |    1 +
 gio/gwin32outputstream.c         |    1 +
 gio/tests/gsubprocess-testprog.c |    1 +
 5 files changed, 64 insertions(+), 11 deletions(-)
---
diff --git a/gio/gsubprocess.c b/gio/gsubprocess.c
index d9bcd89..41764ee 100644
--- a/gio/gsubprocess.c
+++ b/gio/gsubprocess.c
@@ -99,8 +99,8 @@
 #include <fcntl.h>
 #endif
 #ifdef G_OS_WIN32
-#define _WIN32_WINNT 0x0500
 #include <windows.h>
+#include <io.h>
 #include "giowin32-priv.h"
 #endif
 
@@ -173,6 +173,7 @@ enum
   N_PROPS
 };
 
+#ifdef G_OS_UNIX
 typedef struct
 {
   gint                 fds[3];
@@ -278,6 +279,7 @@ child_setup (gpointer user_data)
   if (child_data->child_setup_func)
     child_data->child_setup_func (child_data->child_setup_data);
 }
+#endif
 
 static GInputStream *
 platform_input_stream_from_spawn_fd (gint fd)
@@ -391,7 +393,9 @@ initable_init (GInitable     *initable,
                GError       **error)
 {
   GSubprocess *self = G_SUBPROCESS (initable);
+#ifdef G_OS_UNIX
   ChildData child_data = { { -1, -1, -1 }, 0 };
+#endif
   gint *pipe_ptrs[3] = { NULL, NULL, NULL };
   gint pipe_fds[3] = { -1, -1, -1 };
   gint close_fds[3] = { -1, -1, -1 };
@@ -453,10 +457,10 @@ initable_init (GInitable     *initable,
     spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL;
   else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_PIPE)
     pipe_ptrs[2] = &pipe_fds[2];
+#ifdef G_OS_UNIX
   else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_MERGE)
     /* This will work because stderr gets setup after stdout. */
     child_data.fds[2] = 1;
-#ifdef G_OS_UNIX
   else if (self->launcher)
     {
       if (self->launcher->stderr_fd != -1)
@@ -493,13 +497,20 @@ initable_init (GInitable     *initable,
   spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;
   spawn_flags |= G_SPAWN_CLOEXEC_PIPES;
 
+#ifdef G_OS_UNIX
   child_data.child_setup_func = self->launcher ? self->launcher->child_setup_func : NULL;
   child_data.child_setup_data = self->launcher ? self->launcher->child_setup_user_data : NULL;
+#endif
+
   success = g_spawn_async_with_pipes (self->launcher ? self->launcher->cwd : NULL,
                                       self->argv,
                                       self->launcher ? self->launcher->envp : NULL,
                                       spawn_flags,
+#ifdef G_OS_UNIX
                                       child_setup, &child_data,
+#else
+                                      NULL, NULL,
+#endif
                                       &self->pid,
                                       pipe_ptrs[0], pipe_ptrs[1], pipe_ptrs[2],
                                       error);
@@ -532,7 +543,9 @@ initable_init (GInitable     *initable,
       g_source_unref (source);
     }
 
+#ifdef G_OS_UNIX
 out:
+#endif
   /* we don't need this past init... */
   self->launcher = NULL;
 
@@ -1135,7 +1148,11 @@ g_subprocess_get_successful (GSubprocess *subprocess)
   g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE);
   g_return_val_if_fail (subprocess->pid == 0, FALSE);
 
+#ifdef G_OS_UNIX
   return WIFEXITED (subprocess->status) && WEXITSTATUS (subprocess->status) == 0;
+#else
+  return subprocess->status == 0;
+#endif
 }
 
 /**
@@ -1160,7 +1177,11 @@ g_subprocess_get_if_exited (GSubprocess *subprocess)
   g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE);
   g_return_val_if_fail (subprocess->pid == 0, FALSE);
 
+#ifdef G_OS_UNIX
   return WIFEXITED (subprocess->status);
+#else
+  return TRUE;
+#endif
 }
 
 /**
@@ -1185,9 +1206,14 @@ g_subprocess_get_exit_status (GSubprocess *subprocess)
 {
   g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), 1);
   g_return_val_if_fail (subprocess->pid == 0, 1);
+
+#ifdef G_OS_UNIX
   g_return_val_if_fail (WIFEXITED (subprocess->status), 1);
 
   return WEXITSTATUS (subprocess->status);
+#else
+  return subprocess->status;
+#endif
 }
 
 /**
@@ -1211,7 +1237,11 @@ g_subprocess_get_if_signaled (GSubprocess *subprocess)
   g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE);
   g_return_val_if_fail (subprocess->pid == 0, FALSE);
 
+#ifdef G_OS_UNIX
   return WIFSIGNALED (subprocess->status);
+#else
+  return FALSE;
+#endif
 }
 
 /**
@@ -1235,9 +1265,16 @@ g_subprocess_get_term_sig (GSubprocess *subprocess)
 {
   g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), 0);
   g_return_val_if_fail (subprocess->pid == 0, 0);
+
+#ifdef G_OS_UNIX
   g_return_val_if_fail (WIFSIGNALED (subprocess->status), 0);
 
   return WTERMSIG (subprocess->status);
+#else
+  g_critical ("g_subprocess_get_term_sig() called on Windows, where "
+              "g_subprocess_get_if_signaled() always returns FALSE...");
+  return 0;
+#endif
 }
 
 /*< private >*/
diff --git a/gio/gsubprocesslauncher.c b/gio/gsubprocesslauncher.c
index 7471704..d6463e3 100644
--- a/gio/gsubprocesslauncher.c
+++ b/gio/gsubprocesslauncher.c
@@ -130,12 +130,10 @@ static void
 g_subprocess_launcher_finalize (GObject *object)
 {
   GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
-  guint i;
-
-  g_strfreev (self->envp);
-  g_free (self->cwd);
 
 #ifdef G_OS_UNIX
+  guint i;
+
   g_free (self->stdin_path);
   g_free (self->stdout_path);
   g_free (self->stderr_path);
@@ -161,10 +159,13 @@ g_subprocess_launcher_finalize (GObject *object)
         (void) close (g_array_index (self->needdup_fd_assignments, int, i));
       g_array_unref (self->needdup_fd_assignments);
     }
-#endif
 
   if (self->child_setup_destroy_notify)
     (* self->child_setup_destroy_notify) (self->child_setup_user_data);
+#endif
+
+  g_strfreev (self->envp);
+  g_free (self->cwd);
 
   G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->finalize (object);
 }
@@ -174,10 +175,10 @@ g_subprocess_launcher_init (GSubprocessLauncher  *self)
 {
   self->envp = g_listenv ();
 
+#ifdef G_OS_UNIX
   self->stdin_fd = -1;
   self->stdout_fd = -1;
   self->stderr_fd = -1;
-#ifdef G_OS_UNIX
   self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
   self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
 #endif
@@ -364,9 +365,21 @@ void
 g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
                                  GSubprocessFlags     flags)
 {
-  if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, self->stdin_fd, self->stdin_path) &&
-      verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, self->stdout_fd, self->stdout_path) &&
-      verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, self->stderr_fd, self->stderr_path))
+  const gchar *stdin_path = NULL, *stdout_path = NULL, *stderr_path = NULL;
+  gint stdin_fd = -1, stdout_fd = -1, stderr_fd = -1;
+
+#ifdef G_OS_UNIX
+  stdin_fd = self->stdin_fd;
+  stdout_fd = self->stdout_fd;
+  stderr_fd = self->stderr_fd;
+  stdin_path = self->stdin_path;
+  stdout_path = self->stdout_path;
+  stderr_path = self->stderr_path;
+#endif
+
+  if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, stdin_fd, stdin_path) &&
+      verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, stdout_fd, stdout_path) &&
+      verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, stderr_fd, stderr_path))
     self->flags = flags;
 }
 
diff --git a/gio/gwin32inputstream.c b/gio/gwin32inputstream.c
index 9e364bb..0d9f5c0 100644
--- a/gio/gwin32inputstream.c
+++ b/gio/gwin32inputstream.c
@@ -31,6 +31,7 @@
 #include "gioerror.h"
 #include "gsimpleasyncresult.h"
 #include "gwin32inputstream.h"
+#include "giowin32-priv.h"
 #include "gcancellable.h"
 #include "gasynchelper.h"
 #include "glibintl.h"
diff --git a/gio/gwin32outputstream.c b/gio/gwin32outputstream.c
index d707ab7..c36b8cd 100644
--- a/gio/gwin32outputstream.c
+++ b/gio/gwin32outputstream.c
@@ -31,6 +31,7 @@
 #include <glib/gstdio.h>
 #include "gioerror.h"
 #include "gwin32outputstream.h"
+#include "giowin32-priv.h"
 #include "gcancellable.h"
 #include "gsimpleasyncresult.h"
 #include "gasynchelper.h"
diff --git a/gio/tests/gsubprocess-testprog.c b/gio/tests/gsubprocess-testprog.c
index e0903dd..1e5a948 100644
--- a/gio/tests/gsubprocess-testprog.c
+++ b/gio/tests/gsubprocess-testprog.c
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <unistd.h>
 #ifdef G_OS_UNIX
 #include <gio/gunixinputstream.h>
 #include <gio/gunixoutputstream.h>


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