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.
This commit is contained in:
Ryan Lortie 2013-10-21 14:55:21 -04:00
parent b21c86b6a0
commit 358588ed2a
5 changed files with 64 additions and 11 deletions

View File

@ -99,8 +99,8 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
#define _WIN32_WINNT 0x0500
#include <windows.h> #include <windows.h>
#include <io.h>
#include "giowin32-priv.h" #include "giowin32-priv.h"
#endif #endif
@ -173,6 +173,7 @@ enum
N_PROPS N_PROPS
}; };
#ifdef G_OS_UNIX
typedef struct typedef struct
{ {
gint fds[3]; gint fds[3];
@ -278,6 +279,7 @@ child_setup (gpointer user_data)
if (child_data->child_setup_func) if (child_data->child_setup_func)
child_data->child_setup_func (child_data->child_setup_data); child_data->child_setup_func (child_data->child_setup_data);
} }
#endif
static GInputStream * static GInputStream *
platform_input_stream_from_spawn_fd (gint fd) platform_input_stream_from_spawn_fd (gint fd)
@ -391,7 +393,9 @@ initable_init (GInitable *initable,
GError **error) GError **error)
{ {
GSubprocess *self = G_SUBPROCESS (initable); GSubprocess *self = G_SUBPROCESS (initable);
#ifdef G_OS_UNIX
ChildData child_data = { { -1, -1, -1 }, 0 }; ChildData child_data = { { -1, -1, -1 }, 0 };
#endif
gint *pipe_ptrs[3] = { NULL, NULL, NULL }; gint *pipe_ptrs[3] = { NULL, NULL, NULL };
gint pipe_fds[3] = { -1, -1, -1 }; gint pipe_fds[3] = { -1, -1, -1 };
gint close_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; spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL;
else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_PIPE) else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_PIPE)
pipe_ptrs[2] = &pipe_fds[2]; pipe_ptrs[2] = &pipe_fds[2];
#ifdef G_OS_UNIX
else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_MERGE) else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_MERGE)
/* This will work because stderr gets setup after stdout. */ /* This will work because stderr gets setup after stdout. */
child_data.fds[2] = 1; child_data.fds[2] = 1;
#ifdef G_OS_UNIX
else if (self->launcher) else if (self->launcher)
{ {
if (self->launcher->stderr_fd != -1) 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_DO_NOT_REAP_CHILD;
spawn_flags |= G_SPAWN_CLOEXEC_PIPES; 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_func = self->launcher ? self->launcher->child_setup_func : NULL;
child_data.child_setup_data = self->launcher ? self->launcher->child_setup_user_data : 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, success = g_spawn_async_with_pipes (self->launcher ? self->launcher->cwd : NULL,
self->argv, self->argv,
self->launcher ? self->launcher->envp : NULL, self->launcher ? self->launcher->envp : NULL,
spawn_flags, spawn_flags,
#ifdef G_OS_UNIX
child_setup, &child_data, child_setup, &child_data,
#else
NULL, NULL,
#endif
&self->pid, &self->pid,
pipe_ptrs[0], pipe_ptrs[1], pipe_ptrs[2], pipe_ptrs[0], pipe_ptrs[1], pipe_ptrs[2],
error); error);
@ -532,7 +543,9 @@ initable_init (GInitable *initable,
g_source_unref (source); g_source_unref (source);
} }
#ifdef G_OS_UNIX
out: out:
#endif
/* we don't need this past init... */ /* we don't need this past init... */
self->launcher = NULL; 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 (G_IS_SUBPROCESS (subprocess), FALSE);
g_return_val_if_fail (subprocess->pid == 0, FALSE); g_return_val_if_fail (subprocess->pid == 0, FALSE);
#ifdef G_OS_UNIX
return WIFEXITED (subprocess->status) && WEXITSTATUS (subprocess->status) == 0; 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 (G_IS_SUBPROCESS (subprocess), FALSE);
g_return_val_if_fail (subprocess->pid == 0, FALSE); g_return_val_if_fail (subprocess->pid == 0, FALSE);
#ifdef G_OS_UNIX
return WIFEXITED (subprocess->status); 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 (G_IS_SUBPROCESS (subprocess), 1);
g_return_val_if_fail (subprocess->pid == 0, 1); g_return_val_if_fail (subprocess->pid == 0, 1);
#ifdef G_OS_UNIX
g_return_val_if_fail (WIFEXITED (subprocess->status), 1); g_return_val_if_fail (WIFEXITED (subprocess->status), 1);
return WEXITSTATUS (subprocess->status); 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 (G_IS_SUBPROCESS (subprocess), FALSE);
g_return_val_if_fail (subprocess->pid == 0, FALSE); g_return_val_if_fail (subprocess->pid == 0, FALSE);
#ifdef G_OS_UNIX
return WIFSIGNALED (subprocess->status); 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 (G_IS_SUBPROCESS (subprocess), 0);
g_return_val_if_fail (subprocess->pid == 0, 0); g_return_val_if_fail (subprocess->pid == 0, 0);
#ifdef G_OS_UNIX
g_return_val_if_fail (WIFSIGNALED (subprocess->status), 0); g_return_val_if_fail (WIFSIGNALED (subprocess->status), 0);
return WTERMSIG (subprocess->status); 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 >*/ /*< private >*/

View File

@ -130,12 +130,10 @@ static void
g_subprocess_launcher_finalize (GObject *object) g_subprocess_launcher_finalize (GObject *object)
{ {
GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object); GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
guint i;
g_strfreev (self->envp);
g_free (self->cwd);
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
guint i;
g_free (self->stdin_path); g_free (self->stdin_path);
g_free (self->stdout_path); g_free (self->stdout_path);
g_free (self->stderr_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)); (void) close (g_array_index (self->needdup_fd_assignments, int, i));
g_array_unref (self->needdup_fd_assignments); g_array_unref (self->needdup_fd_assignments);
} }
#endif
if (self->child_setup_destroy_notify) if (self->child_setup_destroy_notify)
(* self->child_setup_destroy_notify) (self->child_setup_user_data); (* 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); G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->finalize (object);
} }
@ -174,10 +175,10 @@ g_subprocess_launcher_init (GSubprocessLauncher *self)
{ {
self->envp = g_listenv (); self->envp = g_listenv ();
#ifdef G_OS_UNIX
self->stdin_fd = -1; self->stdin_fd = -1;
self->stdout_fd = -1; self->stdout_fd = -1;
self->stderr_fd = -1; self->stderr_fd = -1;
#ifdef G_OS_UNIX
self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int)); self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int)); self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
#endif #endif
@ -364,9 +365,21 @@ void
g_subprocess_launcher_set_flags (GSubprocessLauncher *self, g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
GSubprocessFlags flags) GSubprocessFlags flags)
{ {
if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, self->stdin_fd, self->stdin_path) && const gchar *stdin_path = NULL, *stdout_path = NULL, *stderr_path = NULL;
verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, self->stdout_fd, self->stdout_path) && gint stdin_fd = -1, stdout_fd = -1, stderr_fd = -1;
verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, self->stderr_fd, self->stderr_path))
#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; self->flags = flags;
} }

View File

@ -31,6 +31,7 @@
#include "gioerror.h" #include "gioerror.h"
#include "gsimpleasyncresult.h" #include "gsimpleasyncresult.h"
#include "gwin32inputstream.h" #include "gwin32inputstream.h"
#include "giowin32-priv.h"
#include "gcancellable.h" #include "gcancellable.h"
#include "gasynchelper.h" #include "gasynchelper.h"
#include "glibintl.h" #include "glibintl.h"

View File

@ -31,6 +31,7 @@
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include "gioerror.h" #include "gioerror.h"
#include "gwin32outputstream.h" #include "gwin32outputstream.h"
#include "giowin32-priv.h"
#include "gcancellable.h" #include "gcancellable.h"
#include "gsimpleasyncresult.h" #include "gsimpleasyncresult.h"
#include "gasynchelper.h" #include "gasynchelper.h"

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
#include <gio/gunixinputstream.h> #include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h> #include <gio/gunixoutputstream.h>