2006-05-21 16:11:05 -07:00
|
|
|
/*
|
2006-08-20 01:49:03 -07:00
|
|
|
* moospawn.c
|
2005-11-27 18:12:50 -08:00
|
|
|
*
|
2007-04-07 01:21:52 -07:00
|
|
|
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
2005-11-27 18:12:50 -08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
2006-08-20 01:49:03 -07:00
|
|
|
#include "mooutils/moospawn.h"
|
2005-11-27 18:12:50 -08:00
|
|
|
#include "mooutils/moomarshals.h"
|
2006-06-10 01:33:42 -07:00
|
|
|
#include "mooutils/mooutils-misc.h"
|
2006-08-23 21:03:41 -07:00
|
|
|
#include "mooutils/moocompat.h"
|
2006-08-20 01:49:03 -07:00
|
|
|
#include <string.h>
|
2005-12-24 11:36:47 -08:00
|
|
|
|
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
#include <sys/wait.h>
|
2007-02-27 20:47:42 -08:00
|
|
|
#include <sys/types.h>
|
2005-11-27 18:12:50 -08:00
|
|
|
#include <unistd.h>
|
2006-02-13 12:37:25 -08:00
|
|
|
#include <signal.h>
|
2005-12-24 11:36:47 -08:00
|
|
|
#else
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
struct _MooCmdPrivate {
|
|
|
|
GSpawnFlags flags;
|
|
|
|
MooCmdFlags cmd_flags;
|
|
|
|
gboolean running;
|
|
|
|
int exit_status;
|
|
|
|
GPid pid;
|
2006-06-11 04:02:54 -07:00
|
|
|
int out;
|
|
|
|
int err;
|
2005-11-27 18:12:50 -08:00
|
|
|
GIOChannel *stdout_io;
|
|
|
|
GIOChannel *stderr_io;
|
|
|
|
guint child_watch;
|
|
|
|
guint stdout_watch;
|
|
|
|
guint stderr_watch;
|
2006-08-20 01:49:03 -07:00
|
|
|
GString *out_buffer;
|
|
|
|
GString *err_buffer;
|
2005-11-27 18:12:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void moo_cmd_finalize (GObject *object);
|
|
|
|
static void moo_cmd_dispose (GObject *object);
|
|
|
|
|
|
|
|
static void moo_cmd_check_stop (MooCmd *cmd);
|
|
|
|
static void moo_cmd_cleanup (MooCmd *cmd);
|
|
|
|
|
|
|
|
static gboolean moo_cmd_abort_real (MooCmd *cmd);
|
2006-09-02 00:33:09 -07:00
|
|
|
static gboolean moo_cmd_stdout_line (MooCmd *cmd,
|
|
|
|
const char *line);
|
|
|
|
static gboolean moo_cmd_stderr_line (MooCmd *cmd,
|
|
|
|
const char *line);
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
static gboolean moo_cmd_run_command (MooCmd *cmd,
|
|
|
|
const char *working_dir,
|
|
|
|
char **argv,
|
|
|
|
char **envp,
|
|
|
|
GSpawnFlags flags,
|
|
|
|
MooCmdFlags cmd_flags,
|
|
|
|
GSpawnChildSetupFunc child_setup,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
|
|
|
|
2006-05-23 10:27:28 -07:00
|
|
|
#if 0 && defined(__WIN32__)
|
|
|
|
static gboolean moo_win32_spawn_async_with_pipes (const gchar *working_directory,
|
|
|
|
gchar **argv,
|
|
|
|
gchar **envp,
|
|
|
|
GSpawnFlags flags,
|
|
|
|
GPid *child_pid,
|
|
|
|
gint *standard_input,
|
|
|
|
gint *standard_output,
|
|
|
|
gint *standard_error,
|
|
|
|
GError **error);
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ABORT,
|
|
|
|
CMD_EXIT,
|
2006-09-02 00:33:09 -07:00
|
|
|
STDOUT_LINE,
|
|
|
|
STDERR_LINE,
|
2005-11-27 18:12:50 -08:00
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL];
|
|
|
|
|
|
|
|
|
|
|
|
/* MOO_TYPE_CMD */
|
2006-08-20 01:49:03 -07:00
|
|
|
G_DEFINE_TYPE (MooCmd, _moo_cmd, G_TYPE_OBJECT)
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-20 01:49:03 -07:00
|
|
|
_moo_cmd_class_init (MooCmdClass *klass)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = moo_cmd_finalize;
|
|
|
|
gobject_class->dispose = moo_cmd_dispose;
|
|
|
|
|
|
|
|
klass->abort = moo_cmd_abort_real;
|
|
|
|
klass->cmd_exit = NULL;
|
2006-09-02 00:33:09 -07:00
|
|
|
klass->stdout_line = moo_cmd_stdout_line;
|
|
|
|
klass->stderr_line = moo_cmd_stderr_line;
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2006-08-20 01:49:03 -07:00
|
|
|
g_type_class_add_private (klass, sizeof (MooCmdPrivate));
|
|
|
|
|
2005-11-27 18:12:50 -08:00
|
|
|
signals[ABORT] =
|
|
|
|
g_signal_new ("abort",
|
|
|
|
G_OBJECT_CLASS_TYPE (klass),
|
|
|
|
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
|
|
|
|
G_STRUCT_OFFSET (MooCmdClass, abort),
|
|
|
|
g_signal_accumulator_true_handled, NULL,
|
|
|
|
_moo_marshal_BOOL__VOID,
|
|
|
|
G_TYPE_BOOLEAN, 0);
|
|
|
|
|
|
|
|
signals[CMD_EXIT] =
|
|
|
|
g_signal_new ("cmd-exit",
|
|
|
|
G_OBJECT_CLASS_TYPE (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (MooCmdClass, cmd_exit),
|
|
|
|
g_signal_accumulator_true_handled, NULL,
|
|
|
|
_moo_marshal_BOOL__INT,
|
|
|
|
G_TYPE_BOOLEAN, 1,
|
|
|
|
G_TYPE_INT);
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
signals[STDOUT_LINE] =
|
|
|
|
g_signal_new ("stdout-line",
|
2005-11-27 18:12:50 -08:00
|
|
|
G_OBJECT_CLASS_TYPE (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
2006-09-02 00:33:09 -07:00
|
|
|
G_STRUCT_OFFSET (MooCmdClass, stdout_line),
|
2005-11-27 18:12:50 -08:00
|
|
|
g_signal_accumulator_true_handled, NULL,
|
|
|
|
_moo_marshal_BOOL__STRING,
|
|
|
|
G_TYPE_BOOLEAN, 1,
|
|
|
|
G_TYPE_STRING);
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
signals[STDERR_LINE] =
|
|
|
|
g_signal_new ("stderr-line",
|
2005-11-27 18:12:50 -08:00
|
|
|
G_OBJECT_CLASS_TYPE (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
2006-09-02 00:33:09 -07:00
|
|
|
G_STRUCT_OFFSET (MooCmdClass, stderr_line),
|
2005-11-27 18:12:50 -08:00
|
|
|
g_signal_accumulator_true_handled, NULL,
|
|
|
|
_moo_marshal_BOOL__STRING,
|
|
|
|
G_TYPE_BOOLEAN, 1,
|
|
|
|
G_TYPE_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-20 01:49:03 -07:00
|
|
|
_moo_cmd_init (MooCmd *cmd)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2006-08-20 01:49:03 -07:00
|
|
|
cmd->priv = G_TYPE_INSTANCE_GET_PRIVATE (cmd, MOO_TYPE_CMD, MooCmdPrivate);
|
|
|
|
cmd->priv->out_buffer = g_string_new (NULL);
|
|
|
|
cmd->priv->err_buffer = g_string_new (NULL);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_cmd_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
MooCmd *cmd = MOO_CMD (object);
|
|
|
|
|
2006-08-20 01:49:03 -07:00
|
|
|
g_string_free (cmd->priv->out_buffer, TRUE);
|
|
|
|
g_string_free (cmd->priv->err_buffer, TRUE);
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2006-08-20 01:49:03 -07:00
|
|
|
G_OBJECT_CLASS (_moo_cmd_parent_class)->finalize (object);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_cmd_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
MooCmd *cmd = MOO_CMD (object);
|
|
|
|
|
|
|
|
if (cmd->priv->pid)
|
2006-08-20 01:49:03 -07:00
|
|
|
_moo_cmd_abort (cmd);
|
2005-11-27 18:12:50 -08:00
|
|
|
moo_cmd_cleanup (cmd);
|
|
|
|
|
2006-08-20 01:49:03 -07:00
|
|
|
G_OBJECT_CLASS (_moo_cmd_parent_class)->dispose (object);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MooCmd*
|
2006-08-20 01:49:03 -07:00
|
|
|
_moo_cmd_new (const char *working_dir,
|
|
|
|
char **argv,
|
|
|
|
char **envp,
|
|
|
|
GSpawnFlags flags,
|
|
|
|
MooCmdFlags cmd_flags,
|
|
|
|
GSpawnChildSetupFunc child_setup,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
MooCmd *cmd;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (argv && *argv, NULL);
|
|
|
|
|
|
|
|
cmd = g_object_new (MOO_TYPE_CMD, NULL);
|
|
|
|
|
|
|
|
result = moo_cmd_run_command (cmd, working_dir, argv, envp,
|
|
|
|
flags, cmd_flags, child_setup,
|
|
|
|
user_data, error);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_object_unref (cmd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
command_exit (GPid pid,
|
|
|
|
gint status,
|
|
|
|
MooCmd *cmd)
|
|
|
|
{
|
|
|
|
g_return_if_fail (pid == cmd->priv->pid);
|
|
|
|
|
|
|
|
cmd->priv->child_watch = 0;
|
|
|
|
cmd->priv->exit_status = status;
|
|
|
|
|
|
|
|
g_spawn_close_pid (cmd->priv->pid);
|
|
|
|
cmd->priv->pid = 0;
|
|
|
|
|
|
|
|
moo_cmd_check_stop (cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-09-02 00:33:09 -07:00
|
|
|
process_line (MooCmd *cmd,
|
|
|
|
const char *line,
|
2006-06-11 04:02:54 -07:00
|
|
|
gboolean err)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
gboolean dummy = FALSE;
|
2006-09-02 00:33:09 -07:00
|
|
|
const char *real_line = line;
|
2005-11-27 18:12:50 -08:00
|
|
|
char *freeme = NULL;
|
|
|
|
|
|
|
|
if (cmd->priv->cmd_flags & MOO_CMD_UTF8_OUTPUT)
|
|
|
|
{
|
|
|
|
const char *charset;
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
real_line = NULL;
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
if (g_get_charset (&charset))
|
|
|
|
{
|
|
|
|
const char *end;
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
if (!g_utf8_validate (line, -1, &end))
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2006-09-02 00:33:09 -07:00
|
|
|
g_warning ("%s: invalid unicode:\n%s", G_STRLOC, line);
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
if (end > line)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2006-09-02 00:33:09 -07:00
|
|
|
freeme = g_strndup (line, end - line);
|
|
|
|
real_line = freeme;
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-02 00:33:09 -07:00
|
|
|
real_line = line;
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2006-09-07 00:02:21 -07:00
|
|
|
freeme = g_convert_with_fallback (line, -1, "UTF-8", charset,
|
2005-11-27 18:12:50 -08:00
|
|
|
NULL, NULL, NULL, &error);
|
|
|
|
|
|
|
|
if (!freeme)
|
|
|
|
{
|
2006-09-07 00:02:21 -07:00
|
|
|
g_warning ("%s: could not convert text to UTF-8:\n%s",
|
2006-09-02 00:33:09 -07:00
|
|
|
G_STRLOC, line);
|
2005-11-27 18:12:50 -08:00
|
|
|
g_warning ("%s: %s", G_STRLOC, error->message);
|
|
|
|
}
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
real_line = freeme;
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-02 00:33:09 -07:00
|
|
|
if (real_line)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2006-06-11 04:02:54 -07:00
|
|
|
if (err)
|
2006-09-02 00:33:09 -07:00
|
|
|
g_signal_emit (cmd, signals[STDERR_LINE], 0, real_line, &dummy);
|
2005-11-27 18:12:50 -08:00
|
|
|
else
|
2006-09-02 00:33:09 -07:00
|
|
|
g_signal_emit (cmd, signals[STDOUT_LINE], 0, real_line, &dummy);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (freeme);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
command_out_or_err (MooCmd *cmd,
|
|
|
|
GIOChannel *channel,
|
|
|
|
GIOCondition condition,
|
2006-06-11 04:02:54 -07:00
|
|
|
gboolean out)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2007-01-15 03:04:39 -08:00
|
|
|
GSList *lines;
|
2005-11-27 18:12:50 -08:00
|
|
|
GError *error = NULL;
|
|
|
|
GIOStatus status;
|
2007-01-15 03:04:39 -08:00
|
|
|
gsize count;
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2007-01-15 03:04:39 -08:00
|
|
|
count = 0;
|
|
|
|
lines = NULL;
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2007-01-15 03:04:39 -08:00
|
|
|
while (count < 4096)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
2007-01-15 03:04:39 -08:00
|
|
|
char *line = NULL;
|
|
|
|
gsize line_end;
|
|
|
|
|
|
|
|
status = g_io_channel_read_line (channel, &line, NULL, &line_end, &error);
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
|
|
|
|
count += line_end;
|
2005-11-27 18:12:50 -08:00
|
|
|
line[line_end] = 0;
|
2007-01-15 03:04:39 -08:00
|
|
|
lines = g_slist_prepend (lines, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = g_slist_reverse (lines);
|
|
|
|
|
|
|
|
while (lines)
|
|
|
|
{
|
|
|
|
process_line (cmd, lines->data, !out);
|
|
|
|
g_free (lines->data);
|
|
|
|
lines = g_slist_delete_link (lines, lines);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("%s: %s", G_STRLOC, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (condition & (G_IO_ERR | G_IO_HUP))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (status == G_IO_STATUS_EOF)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
command_out (GIOChannel *channel,
|
|
|
|
GIOCondition condition,
|
|
|
|
MooCmd *cmd)
|
|
|
|
{
|
|
|
|
return command_out_or_err (cmd, channel, condition, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
command_err (GIOChannel *channel,
|
|
|
|
GIOCondition condition,
|
|
|
|
MooCmd *cmd)
|
|
|
|
{
|
|
|
|
return command_out_or_err (cmd, channel, condition, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
try_channel_leftover (MooCmd *cmd,
|
|
|
|
GIOChannel *channel,
|
2006-06-11 04:02:54 -07:00
|
|
|
gboolean out)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
g_io_channel_read_to_end (channel, &text, NULL, NULL);
|
|
|
|
|
|
|
|
if (text)
|
|
|
|
{
|
|
|
|
char **lines, **p;
|
2005-11-29 09:33:44 -08:00
|
|
|
|
2006-08-15 01:52:38 -07:00
|
|
|
lines = _moo_splitlines (text);
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
if (lines)
|
|
|
|
{
|
|
|
|
for (p = lines; *p != NULL; p++)
|
|
|
|
if (**p)
|
2006-09-02 00:33:09 -07:00
|
|
|
process_line (cmd, *p, !out);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev (lines);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
stdout_watch_removed (MooCmd *cmd)
|
|
|
|
{
|
|
|
|
if (cmd->priv->stdout_io)
|
|
|
|
{
|
|
|
|
try_channel_leftover (cmd, cmd->priv->stdout_io, TRUE);
|
|
|
|
g_io_channel_unref (cmd->priv->stdout_io);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->priv->stdout_io = NULL;
|
|
|
|
cmd->priv->stdout_watch = 0;
|
|
|
|
moo_cmd_check_stop (cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
stderr_watch_removed (MooCmd *cmd)
|
|
|
|
{
|
|
|
|
if (cmd->priv->stderr_io)
|
|
|
|
{
|
2006-09-02 00:33:09 -07:00
|
|
|
try_channel_leftover (cmd, cmd->priv->stderr_io, FALSE);
|
2005-11-27 18:12:50 -08:00
|
|
|
g_io_channel_unref (cmd->priv->stderr_io);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->priv->stderr_io = NULL;
|
|
|
|
cmd->priv->stderr_watch = 0;
|
|
|
|
moo_cmd_check_stop (cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-24 11:36:47 -08:00
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
static void
|
|
|
|
real_child_setup (gpointer user_data)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
GSpawnChildSetupFunc child_setup;
|
|
|
|
gpointer user_data;
|
|
|
|
} *data = user_data;
|
|
|
|
|
2006-02-13 12:37:25 -08:00
|
|
|
setpgid (0, 0);
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
if (data->child_setup)
|
|
|
|
data->child_setup (data->user_data);
|
|
|
|
}
|
2005-12-24 11:36:47 -08:00
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
|
2006-08-30 23:26:26 -07:00
|
|
|
char **
|
|
|
|
_moo_env_add (char **add)
|
2006-08-22 21:24:36 -07:00
|
|
|
{
|
|
|
|
GPtrArray *new_env;
|
|
|
|
char **names, **p;
|
|
|
|
|
|
|
|
if (!add)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new_env = g_ptr_array_new ();
|
|
|
|
names = g_listenv ();
|
|
|
|
|
|
|
|
for (p = names; p && *p; ++p)
|
|
|
|
{
|
|
|
|
const char *val = g_getenv (*p);
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
g_ptr_array_add (new_env, g_strdup_printf ("%s=%s", *p, val));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = add; p && *p; ++p)
|
|
|
|
g_ptr_array_add (new_env, g_strdup (*p));
|
|
|
|
|
|
|
|
g_strfreev (names);
|
|
|
|
|
|
|
|
g_ptr_array_add (new_env, NULL);
|
|
|
|
return (char**) g_ptr_array_free (new_env, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-27 18:12:50 -08:00
|
|
|
static gboolean
|
|
|
|
moo_cmd_run_command (MooCmd *cmd,
|
|
|
|
const char *working_dir,
|
|
|
|
char **argv,
|
|
|
|
char **envp,
|
|
|
|
GSpawnFlags flags,
|
|
|
|
MooCmdFlags cmd_flags,
|
2006-02-24 17:52:41 -08:00
|
|
|
G_GNUC_UNUSED GSpawnChildSetupFunc child_setup,
|
|
|
|
G_GNUC_UNUSED gpointer user_data,
|
2005-11-27 18:12:50 -08:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
int *outp, *errp;
|
2006-08-22 21:24:36 -07:00
|
|
|
char **new_env;
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2006-02-24 17:52:41 -08:00
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
struct {
|
|
|
|
GSpawnChildSetupFunc child_setup;
|
|
|
|
gpointer user_data;
|
2006-10-17 22:24:35 -07:00
|
|
|
} data;
|
2006-02-24 17:52:41 -08:00
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
g_return_val_if_fail (MOO_IS_CMD (cmd), FALSE);
|
|
|
|
g_return_val_if_fail (argv && argv[0], FALSE);
|
|
|
|
g_return_val_if_fail (!cmd->priv->running, FALSE);
|
|
|
|
|
2006-05-23 10:27:28 -07:00
|
|
|
#ifdef __WIN32__
|
|
|
|
if (cmd_flags & MOO_CMD_OPEN_CONSOLE)
|
|
|
|
{
|
2005-11-27 18:12:50 -08:00
|
|
|
outp = NULL;
|
|
|
|
errp = NULL;
|
2006-05-23 10:27:28 -07:00
|
|
|
}
|
2005-11-27 18:12:50 -08:00
|
|
|
else
|
2006-05-23 10:27:28 -07:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if ((flags & G_SPAWN_STDOUT_TO_DEV_NULL) || (cmd_flags & MOO_CMD_STDOUT_TO_PARENT))
|
|
|
|
outp = NULL;
|
|
|
|
else
|
2006-06-11 04:02:54 -07:00
|
|
|
outp = &cmd->priv->out;
|
2006-05-23 10:27:28 -07:00
|
|
|
|
|
|
|
if ((flags & G_SPAWN_STDERR_TO_DEV_NULL) || (cmd_flags & MOO_CMD_STDERR_TO_PARENT))
|
|
|
|
errp = NULL;
|
|
|
|
else
|
2006-06-11 04:02:54 -07:00
|
|
|
errp = &cmd->priv->err;
|
2006-05-23 10:27:28 -07:00
|
|
|
}
|
2005-11-27 18:12:50 -08:00
|
|
|
|
2006-08-30 23:26:26 -07:00
|
|
|
new_env = _moo_env_add (envp);
|
2006-08-22 21:24:36 -07:00
|
|
|
|
2006-10-17 22:24:35 -07:00
|
|
|
#ifndef __WIN32__
|
|
|
|
data.child_setup = child_setup;
|
|
|
|
data.user_data = user_data;
|
|
|
|
#endif
|
|
|
|
|
2006-05-23 10:27:28 -07:00
|
|
|
#if 0 && defined(__WIN32__)
|
|
|
|
if (!(cmd_flags & MOO_CMD_OPEN_CONSOLE))
|
|
|
|
result = moo_win32_spawn_async_with_pipes (working_dir,
|
2006-08-22 21:24:36 -07:00
|
|
|
argv, new_env,
|
2006-05-23 10:27:28 -07:00
|
|
|
flags | G_SPAWN_DO_NOT_REAP_CHILD,
|
|
|
|
&cmd->priv->pid,
|
|
|
|
NULL, outp, errp, error);
|
|
|
|
else
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
result = g_spawn_async_with_pipes (working_dir,
|
2006-08-22 21:24:36 -07:00
|
|
|
argv, new_env,
|
2005-11-27 18:12:50 -08:00
|
|
|
flags | G_SPAWN_DO_NOT_REAP_CHILD,
|
2005-12-24 11:36:47 -08:00
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
real_child_setup, &data,
|
2005-12-24 11:36:47 -08:00
|
|
|
#else
|
|
|
|
NULL, NULL,
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
&cmd->priv->pid,
|
|
|
|
NULL, outp, errp, error);
|
|
|
|
|
2006-08-22 21:24:36 -07:00
|
|
|
g_strfreev (new_env);
|
|
|
|
|
2005-11-27 18:12:50 -08:00
|
|
|
if (!result)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
cmd->priv->running = TRUE;
|
|
|
|
cmd->priv->flags = flags;
|
|
|
|
cmd->priv->cmd_flags = cmd_flags;
|
|
|
|
|
|
|
|
cmd->priv->child_watch =
|
|
|
|
g_child_watch_add (cmd->priv->pid,
|
|
|
|
(GChildWatchFunc) command_exit,
|
|
|
|
cmd);
|
|
|
|
|
|
|
|
if (outp)
|
|
|
|
{
|
2006-06-11 04:02:54 -07:00
|
|
|
cmd->priv->stdout_io = g_io_channel_unix_new (cmd->priv->out);
|
2005-11-27 18:12:50 -08:00
|
|
|
g_io_channel_set_encoding (cmd->priv->stdout_io, NULL, NULL);
|
|
|
|
g_io_channel_set_buffered (cmd->priv->stdout_io, TRUE);
|
|
|
|
g_io_channel_set_flags (cmd->priv->stdout_io, G_IO_FLAG_NONBLOCK, NULL);
|
|
|
|
g_io_channel_set_close_on_unref (cmd->priv->stdout_io, TRUE);
|
|
|
|
cmd->priv->stdout_watch =
|
2007-01-19 18:47:59 -08:00
|
|
|
_moo_io_add_watch_full (cmd->priv->stdout_io,
|
|
|
|
G_PRIORITY_DEFAULT_IDLE,
|
|
|
|
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
|
|
|
|
(GIOFunc) command_out, cmd,
|
|
|
|
(GDestroyNotify) stdout_watch_removed);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (errp)
|
|
|
|
{
|
2006-06-11 04:02:54 -07:00
|
|
|
cmd->priv->stderr_io = g_io_channel_unix_new (cmd->priv->err);
|
2005-11-27 18:12:50 -08:00
|
|
|
g_io_channel_set_encoding (cmd->priv->stderr_io, NULL, NULL);
|
|
|
|
g_io_channel_set_buffered (cmd->priv->stderr_io, TRUE);
|
|
|
|
g_io_channel_set_flags (cmd->priv->stderr_io, G_IO_FLAG_NONBLOCK, NULL);
|
|
|
|
g_io_channel_set_close_on_unref (cmd->priv->stderr_io, TRUE);
|
|
|
|
cmd->priv->stderr_watch =
|
2007-01-19 18:47:59 -08:00
|
|
|
_moo_io_add_watch_full (cmd->priv->stderr_io,
|
|
|
|
G_PRIORITY_DEFAULT_IDLE,
|
|
|
|
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
|
|
|
|
(GIOFunc) command_err, cmd,
|
|
|
|
(GDestroyNotify) stderr_watch_removed);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_cmd_check_stop (MooCmd *cmd)
|
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
if (!cmd->priv->running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!cmd->priv->child_watch && !cmd->priv->stdout_watch && !cmd->priv->stderr_watch)
|
|
|
|
{
|
2005-11-29 09:32:57 -08:00
|
|
|
g_object_ref (cmd);
|
2005-11-27 18:12:50 -08:00
|
|
|
g_signal_emit (cmd, signals[CMD_EXIT], 0, cmd->priv->exit_status, &result);
|
|
|
|
moo_cmd_cleanup (cmd);
|
2005-11-29 09:32:57 -08:00
|
|
|
g_object_unref (cmd);
|
2005-11-27 18:12:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_cmd_cleanup (MooCmd *cmd)
|
|
|
|
{
|
|
|
|
if (!cmd->priv->running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cmd->priv->running = FALSE;
|
|
|
|
|
|
|
|
if (cmd->priv->child_watch)
|
|
|
|
g_source_remove (cmd->priv->child_watch);
|
|
|
|
if (cmd->priv->stdout_watch)
|
|
|
|
g_source_remove (cmd->priv->stdout_watch);
|
|
|
|
if (cmd->priv->stderr_watch)
|
|
|
|
g_source_remove (cmd->priv->stderr_watch);
|
|
|
|
|
|
|
|
if (cmd->priv->stdout_io)
|
|
|
|
g_io_channel_unref (cmd->priv->stdout_io);
|
|
|
|
if (cmd->priv->stderr_io)
|
|
|
|
g_io_channel_unref (cmd->priv->stderr_io);
|
|
|
|
|
|
|
|
if (cmd->priv->pid)
|
|
|
|
{
|
2005-12-24 11:36:47 -08:00
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
kill (-cmd->priv->pid, SIGHUP);
|
2005-12-24 11:36:47 -08:00
|
|
|
#else
|
|
|
|
TerminateProcess (cmd->priv->pid, 1);
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
g_spawn_close_pid (cmd->priv->pid);
|
|
|
|
cmd->priv->pid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->priv->pid = 0;
|
2006-06-11 04:02:54 -07:00
|
|
|
cmd->priv->out = -1;
|
|
|
|
cmd->priv->err = -1;
|
2005-11-27 18:12:50 -08:00
|
|
|
cmd->priv->child_watch = 0;
|
|
|
|
cmd->priv->stdout_watch = 0;
|
|
|
|
cmd->priv->stderr_watch = 0;
|
|
|
|
cmd->priv->stdout_io = NULL;
|
|
|
|
cmd->priv->stderr_io = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
moo_cmd_abort_real (MooCmd *cmd)
|
|
|
|
{
|
|
|
|
if (!cmd->priv->running)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (cmd->priv->pid != 0, TRUE);
|
|
|
|
|
2005-12-24 11:36:47 -08:00
|
|
|
#ifndef __WIN32__
|
2005-11-27 18:12:50 -08:00
|
|
|
kill (-cmd->priv->pid, SIGHUP);
|
2005-12-24 11:36:47 -08:00
|
|
|
#else
|
|
|
|
TerminateProcess (cmd->priv->pid, 1);
|
|
|
|
#endif
|
2005-11-27 18:12:50 -08:00
|
|
|
|
|
|
|
if (cmd->priv->stdout_watch)
|
|
|
|
{
|
|
|
|
g_source_remove (cmd->priv->stdout_watch);
|
|
|
|
cmd->priv->stdout_watch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->priv->stderr_watch > 0)
|
|
|
|
{
|
|
|
|
g_source_remove (cmd->priv->stderr_watch);
|
|
|
|
cmd->priv->stderr_watch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-20 01:49:03 -07:00
|
|
|
_moo_cmd_abort (MooCmd *cmd)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
gboolean handled;
|
|
|
|
g_return_if_fail (MOO_IS_CMD (cmd));
|
|
|
|
g_signal_emit (cmd, signals[ABORT], 0, &handled);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2006-09-02 00:33:09 -07:00
|
|
|
moo_cmd_stdout_line (MooCmd *cmd,
|
|
|
|
const char *line)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
if (cmd->priv->cmd_flags & MOO_CMD_COLLECT_STDOUT)
|
2006-09-02 00:33:09 -07:00
|
|
|
g_string_append (cmd->priv->out_buffer, line);
|
2005-11-27 18:12:50 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2006-09-02 00:33:09 -07:00
|
|
|
moo_cmd_stderr_line (MooCmd *cmd,
|
|
|
|
const char *line)
|
2005-11-27 18:12:50 -08:00
|
|
|
{
|
|
|
|
if (cmd->priv->cmd_flags & MOO_CMD_COLLECT_STDERR)
|
2006-09-02 00:33:09 -07:00
|
|
|
g_string_append (cmd->priv->err_buffer, line);
|
2005-11-27 18:12:50 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-08-20 01:49:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_moo_unix_spawn_async (char **argv,
|
|
|
|
GSpawnFlags g_flags,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_spawn_async (NULL, argv, NULL, g_flags,
|
|
|
|
NULL, NULL, NULL, error);
|
|
|
|
}
|