1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <thunar-vfs/thunar-vfs.h>
#include "libsqueeze-module.h"
#include "archive-iter.h"
#include "archive.h"
#include "archive-command.h"
#include "spawn-command.h"
static void
lsq_spawn_command_class_init(LSQSpawnCommandClass *);
static void
lsq_spawn_command_init(LSQSpawnCommand *);
static void
lsq_spawn_command_dispose(GObject *object);
static void
lsq_spawn_command_finalize(GObject *object);
static gboolean
lsq_spawn_command_execute(LSQArchiveCommand *command);
static gboolean
lsq_spawn_command_stop(LSQArchiveCommand *command);
void
lsq_spawn_command_child_watch_func(GPid pid, gint status, gpointer data);
gboolean
lsq_spawn_command_parse_stdout(GIOChannel *, GIOCondition , gpointer );
//static gint lsq_archive_command_signals[0];
static GObjectClass *parent_class;
GType
lsq_spawn_command_get_type ()
{
static GType lsq_spawn_command_type = 0;
if (!lsq_spawn_command_type)
{
static const GTypeInfo lsq_spawn_command_info =
{
sizeof (LSQSpawnCommandClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) lsq_spawn_command_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (LSQSpawnCommand),
0,
(GInstanceInitFunc) lsq_spawn_command_init,
NULL
};
lsq_spawn_command_type = g_type_register_static (LSQ_TYPE_ARCHIVE_COMMAND, "LSQSpawnCommand", &lsq_spawn_command_info, 0);
}
return lsq_spawn_command_type;
}
static void
lsq_spawn_command_class_init(LSQSpawnCommandClass *spawn_command_class)
{
GObjectClass *object_class = G_OBJECT_CLASS(spawn_command_class);
object_class->dispose = lsq_spawn_command_dispose;
object_class->finalize = lsq_spawn_command_finalize;
parent_class = g_type_class_peek(LSQ_TYPE_ARCHIVE_COMMAND);
}
static void
lsq_spawn_command_init(LSQSpawnCommand *spawn_command)
{
LSQArchiveCommand *command = LSQ_ARCHIVE_COMMAND(spawn_command);
command->execute = lsq_spawn_command_execute;
command->stop = lsq_spawn_command_stop;
spawn_command->parse_stdout = NULL;
}
/**
* lsq_spawn_command_dispose:
*
* @object: LSQSpawnCommand object
*
*/
static void
lsq_spawn_command_dispose(GObject *object)
{
parent_class->dispose(object);
}
/**
* lsq_spawn_command_finalize:
*
* @object: LSQSpawnCommand object
*
*/
static void
lsq_spawn_command_finalize(GObject *object)
{
parent_class->finalize(object);
}
/**
* lsq_spawn_command_new:
* @comment: a description, describing what the command does
* @archive: the archive the command modifies
* @command: a formatted string defining the command to be executed.
* @safe: is it safe to terminate this child premature?
*
*
* %%1$s is the application to be executed.
*
* %%2$s are the files to be appended
*
* %%3$s are any additional options
*
* Returns: a new LSQArchiveCommand object
*/
LSQArchiveCommand *
lsq_spawn_command_new(const gchar *comment,
LSQArchive *archive,
const gchar *command,
const gchar *files,
const gchar *options,
const gchar *archive_path)
{
LSQArchiveCommand *archive_command;
#ifdef DEBUG
g_return_val_if_fail(archive, NULL);
g_return_val_if_fail(LSQ_IS_ARCHIVE(archive), NULL);
#endif
archive_command = g_object_new(LSQ_TYPE_SPAWN_COMMAND, NULL);
if(!files)
files = "";
if(!options)
options = "";
LSQ_SPAWN_COMMAND(archive_command)->command = g_strdup(command);
LSQ_SPAWN_COMMAND(archive_command)->files = g_strdup(files);
LSQ_SPAWN_COMMAND(archive_command)->options = g_strdup(options);
archive_path = g_object_get_data(G_OBJECT(archive_command), LSQ_ARCHIVE_TEMP_FILE);
if(archive_path)
LSQ_SPAWN_COMMAND(archive_command)->archive_path = g_strdup(archive_path);
else
LSQ_SPAWN_COMMAND(archive_command)->archive_path = g_strdup(lsq_archive_get_path(archive));
g_object_ref(G_OBJECT(archive));
archive_command->archive = archive;
if(comment)
archive_command->comment = g_strdup(comment);
return archive_command;
}
static gboolean
lsq_spawn_command_execute(LSQArchiveCommand *command)
{
gchar **argvp;
gint argcp;
gint fd_in, fd_out, fd_err;
LSQSpawnCommand *spawn_command = LSQ_SPAWN_COMMAND(command);
gchar *escaped_archive_path = g_shell_quote(spawn_command->archive_path);
gchar *cmd = g_strdup_printf(spawn_command->command, escaped_archive_path, spawn_command->files, spawn_command->options);
g_shell_parse_argv(cmd, &argcp, &argvp, NULL);
if ( ! g_spawn_async_with_pipes (
NULL,
argvp,
NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
NULL,
NULL,
&(spawn_command->child_pid),
&fd_in,
&fd_out,
&fd_err,
NULL) )
{
return FALSE;
}
LSQ_ARCHIVE_COMMAND(command)->running = TRUE;
#ifdef DEBUG
g_debug("executing: %s\n", cmd);
#endif
g_child_watch_add(spawn_command->child_pid, lsq_spawn_command_child_watch_func, spawn_command);
g_object_ref(spawn_command);
if(spawn_command->parse_stdout != NULL)
{
g_object_ref(spawn_command);
spawn_command->ioc_out = g_io_channel_unix_new(fd_out);
g_io_channel_set_encoding (spawn_command->ioc_out, NULL, NULL);
g_io_channel_set_flags (spawn_command->ioc_out , G_IO_FLAG_NONBLOCK , NULL );
g_io_add_watch (spawn_command->ioc_out, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL, lsq_spawn_command_parse_stdout, spawn_command);
}
g_free(escaped_archive_path);
g_free(cmd);
return TRUE;
}
static gboolean
lsq_spawn_command_stop(LSQArchiveCommand *command)
{
LSQSpawnCommand *spawn_command = LSQ_SPAWN_COMMAND(command);
if(command->running)
{
if(spawn_command->child_pid != 0)
kill ( spawn_command->child_pid , SIGHUP);
else
return FALSE; /* command isn't running */
}
else
return FALSE; /* command isn't running */
return TRUE;
}
/**
* lsq_spawn_command_child_watch_func:
* @pid:
* @status:
* @data:
*/
void
lsq_spawn_command_child_watch_func(GPid pid, gint status, gpointer data)
{
LSQArchiveCommand *command = LSQ_ARCHIVE_COMMAND(data);
gchar *tmp_file;
if(WIFEXITED(status))
{
if(WEXITSTATUS(status))
{
if(!command->error)
{
command->error = g_error_new(command->domain, status, _("Command exited with status %d."), status);
}
}
}
if(WIFSIGNALED(status))
{
switch(WTERMSIG(status))
{
case SIGHUP:
tmp_file = g_object_get_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE);
g_unlink(tmp_file);
g_free(tmp_file);
g_object_set_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE, NULL);
if(!command->error)
command->error = g_error_new_literal(command->domain, status, _("Command interrupted by user"));
break;
case SIGSEGV:
tmp_file = g_object_get_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE);
g_unlink(tmp_file);
g_free(tmp_file);
g_object_set_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE, NULL);
if(!command->error)
command->error = g_error_new_literal(command->domain, status, _("Command received SIGSEGV"));
break;
case SIGKILL:
case SIGINT:
tmp_file = g_object_get_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE);
g_unlink(tmp_file);
g_free(tmp_file);
g_object_set_data(G_OBJECT(command), LSQ_ARCHIVE_TEMP_FILE, NULL);
if(!command->error)
command->error = g_error_new_literal(command->domain, status, _("Command Terminated"));
break;
}
}
g_spawn_close_pid(pid);
g_object_unref(G_OBJECT(data));
}
/**
* lsq_spawn_command_parse_stdout:
* @ioc:
* @cond:
* @data:
*
* Returns:
*/
gboolean
lsq_spawn_command_parse_stdout(GIOChannel *ioc, GIOCondition cond, gpointer data)
{
gint i = 0;
LSQArchiveCommand *archive_command = LSQ_ARCHIVE_COMMAND(data);
LSQSpawnCommand *spawn_command = LSQ_SPAWN_COMMAND(data);
if(cond & (G_IO_PRI | G_IO_IN))
{
for(; i < 500; i++)
{
/* If parse_stdout returns FALSE, something seriously went wrong and we should cancel right away */
if(spawn_command->parse_stdout(spawn_command, archive_command->user_data) == FALSE)
{
cond |= G_IO_ERR;
}
}
}
if(cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) )
{
g_io_channel_shutdown ( ioc,TRUE,NULL );
g_io_channel_unref (ioc);
g_object_unref(spawn_command);
return FALSE;
}
return TRUE;
}
gboolean
lsq_spawn_command_set_parse_func(LSQSpawnCommand *spawn_command, guint fd, LSQParseFunc func, gpointer user_data)
{
LSQArchiveCommand *archive_command = LSQ_ARCHIVE_COMMAND(spawn_command);
switch(fd)
{
case 1:
spawn_command->parse_stdout = func;
archive_command->user_data = user_data;
default:
break;
}
return TRUE;
}
GIOStatus
lsq_spawn_command_read_line(LSQSpawnCommand *spawn_command, guint fd, gchar **line, gsize *length, GError **error)
{
GIOStatus status = G_IO_STATUS_EOF;
switch(fd)
{
case 1:
status = g_io_channel_read_line(spawn_command->ioc_out, line, length, NULL, error);
break;
default:
break;
}
return status;
}
GIOStatus
lsq_spawn_command_read_bytes(LSQSpawnCommand *spawn_command, guint fd, gchar *buf, gsize max_length, gsize *length, GError **error)
{
GIOStatus status = G_IO_STATUS_EOF;
switch(fd)
{
case 1:
status = g_io_channel_read_chars(spawn_command->ioc_out, buf, max_length, length, error);
break;
default:
break;
}
return status;
}
|