2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_c_process.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-21 01:04:10 +01:00
|
|
|
(c) 2000-2011 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01: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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_PROCESS_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2007-12-31 23:38:56 +01:00
|
|
|
/* granpt(), unlockpt() and ptsname() unavailable under OpenBSD
|
2008-10-04 16:07:53 +02:00
|
|
|
and are replaced with openpty() implementation because of security
|
|
|
|
issues */
|
2007-12-31 23:38:56 +01:00
|
|
|
#ifdef OS_OPENBSD
|
|
|
|
#include <util.h>
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gb_replace.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
#include "gb_array.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_watch.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gbx_c_array.h"
|
2009-08-06 02:22:28 +02:00
|
|
|
#include "gbx_local.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gbx_c_process.h"
|
|
|
|
|
|
|
|
//#define DEBUG_ME
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
DECLARE_EVENT(EVENT_Read);
|
|
|
|
DECLARE_EVENT(EVENT_Error);
|
|
|
|
DECLARE_EVENT(EVENT_Kill);
|
|
|
|
|
|
|
|
static CPROCESS *RunningProcessList = NULL;
|
|
|
|
static int _pipe_child[2];
|
|
|
|
static bool _init = FALSE;
|
|
|
|
|
|
|
|
static int _last_status = 0;
|
|
|
|
|
2011-02-11 00:36:11 +01:00
|
|
|
static struct sigaction _old_SIGCHLD_action;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static void init_child(void);
|
|
|
|
static void exit_child(void);
|
|
|
|
|
|
|
|
static void callback_write(int fd, int type, CPROCESS *process)
|
|
|
|
{
|
2010-02-28 18:26:00 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "callback_write: %d %p\n", fd, process);
|
|
|
|
#endif
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (process->to_string)
|
|
|
|
{
|
|
|
|
int n = read(fd, COMMON_buffer, 256);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
STRING_add(&process->result, COMMON_buffer, n);
|
|
|
|
CSTREAM_stream(process)->process.read_something = TRUE;
|
2010-02-28 18:26:00 +01:00
|
|
|
//#ifdef DEBUG_ME
|
|
|
|
//fprintf(stderr, "callback_write: result = '%s'\n", process->result);
|
|
|
|
//#endif
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 16:45:10 +02:00
|
|
|
else if (!STREAM_is_closed(CSTREAM_stream(process)) && !STREAM_eof(CSTREAM_stream(process))) //process->running &&
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_Raise(process, EVENT_Read, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int callback_error(int fd, int type, CPROCESS *process)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
/*fprintf(stderr, ">> Write\n"); fflush(stderr);*/
|
2010-08-26 16:45:10 +02:00
|
|
|
char buffer[256];
|
2008-10-04 16:07:53 +02:00
|
|
|
int n;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-02-28 18:26:00 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "callback_error: %d %p\n", fd, process);
|
|
|
|
#endif
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
n = read(fd, buffer, sizeof(buffer));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (n <= 0) /* || !process->running)*/
|
|
|
|
{
|
|
|
|
/*close(process->err);*/
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*printf("callback_error: (%d) %.*s\n", n, n, buffer);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-08-28 14:33:05 +02:00
|
|
|
GB_Raise(process, EVENT_Error, 1, GB_T_STRING, buffer, n);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*fprintf(stderr, "<< Write\n"); fflush(stderr);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void update_stream(CPROCESS *process)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
STREAM *stream = &process->ob.stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
stream->type = &STREAM_process;
|
|
|
|
(*stream->type->open)(stream, NULL, 0, process);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void init_process(CPROCESS *process)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
process->watch = GB_WATCH_NONE;
|
|
|
|
process->in = process->out = process->err = -1;
|
|
|
|
update_stream(process);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_process(CPROCESS *_object)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
/* Normalement impossible */
|
|
|
|
/*
|
|
|
|
if (THIS->running)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WARNING: CPROCESS_free: running ?\n");
|
|
|
|
stop_process(THIS);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "exit_process: %p\n", _object);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (THIS->in >= 0)
|
|
|
|
{
|
|
|
|
if (THIS->in != THIS->out)
|
|
|
|
close(THIS->in);
|
|
|
|
THIS->in = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (THIS->out >= 0)
|
|
|
|
{
|
|
|
|
GB_Watch(THIS->out, GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(THIS->out);
|
|
|
|
THIS->out = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (THIS->err >= 0)
|
|
|
|
{
|
|
|
|
GB_Watch(THIS->err, GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(THIS->err);
|
|
|
|
THIS->err = -1;
|
|
|
|
}
|
|
|
|
|
2009-02-04 23:51:20 +01:00
|
|
|
// Why that ? This should be done only at process creation
|
|
|
|
// update_stream(THIS);
|
|
|
|
STREAM_close(&THIS->ob.stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_process_after(CPROCESS *_object)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
STREAM *stream;
|
|
|
|
//long long len;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "stop_process_after: %p\n", _object);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Vidage du tampon d'erreur */
|
|
|
|
if (THIS->err >= 0)
|
|
|
|
while (callback_error(THIS->err, 0, THIS) == 0);
|
|
|
|
|
|
|
|
/* Vidage du tampon de sortie */
|
|
|
|
if (THIS->out >= 0)
|
|
|
|
{
|
|
|
|
stream = CSTREAM_stream(THIS);
|
|
|
|
while (!STREAM_eof(stream))
|
|
|
|
{
|
|
|
|
stream->process.read_something = FALSE;
|
|
|
|
callback_write(THIS->out, 0, THIS);
|
|
|
|
//GB_Raise(THIS, EVENT_Read, 0);
|
|
|
|
if (!stream->process.read_something)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_Raise(THIS, EVENT_Kill, 0);
|
|
|
|
|
|
|
|
exit_process(THIS);
|
|
|
|
|
2010-05-27 00:01:28 +02:00
|
|
|
OBJECT_detach((OBJECT *)THIS);
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
/*printf("** stop_process_after\n");*/
|
|
|
|
//GB_Unref((void **)&_object); /* Ref du post */
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void stop_process(CPROCESS *process)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!process->running)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "stop_process: %p\n", process);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*process->pid = -1;*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/* Remove from running process list */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (process->prev)
|
|
|
|
process->prev->next = process->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (process->next)
|
|
|
|
process->next->prev = process->prev;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (process == RunningProcessList)
|
|
|
|
RunningProcessList = process->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
process->running = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*printf("** stop_process\n");*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
stop_process_after(process);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
OBJECT_UNREF(process, "stop_process"); /* Le processus ne tourne plus */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!RunningProcessList)
|
|
|
|
exit_child();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-08-06 02:22:28 +02:00
|
|
|
static void init_child_tty(int fd)
|
|
|
|
{
|
|
|
|
struct termios terminal = { 0 };
|
|
|
|
tcgetattr(fd, &terminal);
|
|
|
|
|
|
|
|
terminal.c_iflag |= ICRNL | IXON | IXOFF;
|
2010-03-05 02:20:09 +01:00
|
|
|
#ifdef IUTF8
|
2009-08-06 02:22:28 +02:00
|
|
|
if (LOCAL_is_UTF8)
|
|
|
|
terminal.c_iflag |= IUTF8;
|
2010-03-05 02:20:09 +01:00
|
|
|
#endif
|
2009-08-06 02:22:28 +02:00
|
|
|
|
|
|
|
terminal.c_oflag |= OPOST;
|
|
|
|
|
|
|
|
terminal.c_lflag |= ISIG | ICANON | IEXTEN | ECHO;
|
|
|
|
|
|
|
|
if (tcsetattr(fd, TCSADRAIN, &terminal))
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
|
2009-05-22 16:52:36 +02:00
|
|
|
static void run_process(CPROCESS *process, int mode, void *cmd, CARRAY *env)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
static const char *shell[] = { "sh", "-c", NULL, NULL };
|
|
|
|
|
|
|
|
int fdin[2], fdout[2], fderr[2];
|
|
|
|
pid_t pid;
|
|
|
|
char **argv;
|
|
|
|
CARRAY *array;
|
|
|
|
int i, n;
|
|
|
|
sigset_t sig, old;
|
|
|
|
|
|
|
|
/* for terminal */
|
|
|
|
int fd_master = -1;
|
|
|
|
int fd_slave;
|
|
|
|
char *slave = NULL;
|
2009-12-07 18:14:32 +01:00
|
|
|
//struct termios termios_stdin;
|
|
|
|
//struct termios termios_check;
|
2008-10-04 16:07:53 +02:00
|
|
|
struct termios termios_master;
|
|
|
|
|
|
|
|
init_child();
|
|
|
|
|
|
|
|
if (mode & PM_SHELL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "run_process %p: %s\n", process, (char *)cmd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
argv = (char **)shell;
|
|
|
|
|
|
|
|
argv[2] = (char *)cmd;
|
|
|
|
|
|
|
|
if (argv[2] == NULL || *argv[2] == 0)
|
|
|
|
{
|
|
|
|
/*stop_process(process, FALSE);*/
|
|
|
|
return;
|
|
|
|
}
|
2011-03-26 10:11:52 +01:00
|
|
|
|
|
|
|
process->process_group = TRUE;
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
array = (CARRAY *)cmd;
|
|
|
|
n = ARRAY_count(array->data);
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/*stop_process(process, FALSE);*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALLOC(&argv, sizeof(*argv) * (n + 1), "run_process");
|
|
|
|
memcpy(argv, array->data, sizeof(*argv) * n);
|
|
|
|
argv[n] = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (!argv[i])
|
|
|
|
argv[i] = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "run_process %p: ", process);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
fprintf(stderr, "%s ", argv[i]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*printf("** run_process\n");*/
|
2010-05-27 00:01:28 +02:00
|
|
|
OBJECT_REF(process, "run_process"); /* Process is running */
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
/* Adding to the running process list */
|
|
|
|
|
|
|
|
if (RunningProcessList)
|
|
|
|
RunningProcessList->prev = process;
|
|
|
|
|
|
|
|
process->next = RunningProcessList;
|
|
|
|
process->prev = NULL;
|
|
|
|
|
|
|
|
RunningProcessList = process;
|
|
|
|
|
|
|
|
process->running = TRUE;
|
|
|
|
|
|
|
|
if (mode & PM_STRING)
|
|
|
|
{
|
|
|
|
process->to_string = TRUE;
|
|
|
|
process->result = NULL;
|
|
|
|
mode |= PM_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
|
|
|
#ifdef OS_OPENBSD
|
2009-05-22 16:52:36 +02:00
|
|
|
if (openpty(&fd_master, &fd_slave, NULL, NULL, NULL)<0)
|
2008-10-04 16:07:53 +02:00
|
|
|
THROW_SYSTEM(errno, NULL);
|
2009-05-22 16:52:36 +02:00
|
|
|
#else
|
2008-10-04 16:07:53 +02:00
|
|
|
fd_master = getpt();
|
|
|
|
if (fd_master < 0)
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
|
|
|
grantpt(fd_master);
|
|
|
|
unlockpt(fd_master);
|
|
|
|
#endif
|
2009-05-22 16:52:36 +02:00
|
|
|
slave = ptsname(fd_master);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Create pipes */
|
|
|
|
|
|
|
|
if ((mode & PM_WRITE) && pipe(fdin) != 0)
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
|
|
|
if ((mode & PM_READ) && (pipe(fdout) != 0 || pipe(fderr) != 0))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Block SIGCHLD */
|
|
|
|
|
|
|
|
sigemptyset(&sig);
|
|
|
|
|
|
|
|
sigaddset(&sig, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &sig, &old);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == (-1))
|
|
|
|
{
|
|
|
|
stop_process(process);
|
|
|
|
sigprocmask(SIG_SETMASK, &old, &sig);
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid)
|
|
|
|
{
|
|
|
|
process->pid = pid;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "fork: pid = %d\n", pid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*printf("Running process %d\n", pid);
|
|
|
|
fflush(NULL);*/
|
|
|
|
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
2009-08-06 02:22:28 +02:00
|
|
|
if (tcgetattr(fd_master, &termios_master))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
cfmakeraw(&termios_master);
|
|
|
|
//termios_master.c_lflag &= ~ECHO;
|
2009-08-06 02:22:28 +02:00
|
|
|
|
|
|
|
if (tcsetattr(fd_master, TCSANOW, &termios_master))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_WRITE)
|
|
|
|
{
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
|
|
|
process->in = fd_master;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close(fdin[0]);
|
|
|
|
process->in = fdin[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_READ)
|
|
|
|
{
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
|
|
|
process->out = fd_master;
|
|
|
|
process->err = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close(fdout[1]);
|
|
|
|
close(fderr[1]);
|
|
|
|
|
|
|
|
process->out = fdout[0];
|
|
|
|
process->err = fderr[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_Watch(process->out, GB_WATCH_READ, (void *)callback_write, (intptr_t)process);
|
|
|
|
if (process->err >= 0)
|
2009-08-06 02:22:28 +02:00
|
|
|
{
|
|
|
|
fcntl(process->err, F_SETFL, fcntl(process->err, F_GETFL) | O_NONBLOCK);
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_Watch(process->err, GB_WATCH_READ, (void *)callback_error, (intptr_t)process);
|
2009-08-06 02:22:28 +02:00
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((mode & PM_SHELL) == 0)
|
|
|
|
{
|
|
|
|
FREE(&argv, "run_process");
|
2008-10-04 14:31:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &old, &sig);
|
|
|
|
}
|
|
|
|
else /* child */
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
//bool stdin_isatty = isatty(STDIN_FILENO);
|
2009-08-06 02:22:28 +02:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &old, &sig);
|
2011-03-26 10:11:52 +01:00
|
|
|
|
|
|
|
if (mode & PM_SHELL)
|
|
|
|
setpgrp();
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
|
|
|
close(fd_master);
|
|
|
|
setsid();
|
|
|
|
fd_slave = open(slave, O_RDWR);
|
|
|
|
if (fd_slave < 0)
|
|
|
|
exit(127);
|
2009-08-06 02:22:28 +02:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (mode & PM_WRITE)
|
|
|
|
{
|
|
|
|
if (dup2(fd_slave, STDIN_FILENO) == -1)
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_READ)
|
|
|
|
{
|
|
|
|
if ((dup2(fd_slave, STDOUT_FILENO) == -1)
|
|
|
|
|| (dup2(fd_slave, STDERR_FILENO) == -1))
|
|
|
|
exit(127);
|
|
|
|
}
|
2009-08-06 02:22:28 +02:00
|
|
|
|
|
|
|
// Strange Linux behaviour ?
|
|
|
|
// Terminal initialization must be done on STDIN_FILENO after using dup2().
|
|
|
|
// If it is done on fd_slave, before using dup2(), it sometimes fails with no error.
|
|
|
|
init_child_tty(STDIN_FILENO);
|
|
|
|
|
|
|
|
/*puts("---------------------------------");
|
|
|
|
if (stdin_isatty) puts("STDIN is a tty");*/
|
|
|
|
/*tcgetattr(STDIN_FILENO, &termios_check);
|
|
|
|
puts(termios_check.c_lflag & ISIG ? "+ISIG" : "-ISIG");
|
|
|
|
//tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_check);
|
|
|
|
system("stty icanon");
|
|
|
|
system("stty -a");
|
|
|
|
puts("---------------------------------");*/
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mode & PM_WRITE)
|
|
|
|
{
|
|
|
|
close(fdin[1]);
|
|
|
|
|
|
|
|
if (dup2(fdin[0], STDIN_FILENO) == -1)
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_READ)
|
|
|
|
{
|
|
|
|
close(fdout[0]);
|
|
|
|
close(fderr[0]);
|
|
|
|
|
|
|
|
if ((dup2(fdout[1], STDOUT_FILENO) == -1)
|
|
|
|
|| (dup2(fderr[1], STDERR_FILENO) == -1))
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
// Return to the parent working directory
|
2010-10-31 23:05:29 +01:00
|
|
|
FILE_chdir(PROJECT_oldcwd);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-05-22 16:52:36 +02:00
|
|
|
if (env)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
n = ARRAY_count(env->data);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
str = ((char **)env->data)[i];
|
|
|
|
if (putenv(str))
|
2010-08-28 14:33:05 +02:00
|
|
|
ERROR_warning("cannot set environment string: %s", str);
|
2009-05-22 16:52:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
execvp(argv[0], (char **)argv);
|
|
|
|
//execve(argv[0], (char **)argv, environ);
|
|
|
|
exit(127);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
update_stream(process);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void callback_child(int fd, int type, void *data)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
int status;
|
|
|
|
CPROCESS *process, *next;
|
|
|
|
int buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*old = signal(SIGCHLD, signal_child);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-10-31 23:05:29 +01:00
|
|
|
if (read(fd, (char *)&buffer, 1) != 1)
|
|
|
|
ERROR_panic("Cannot read from SIGCHLD pipe");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "<< callback_child\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
for (process = RunningProcessList; process; )
|
|
|
|
{
|
|
|
|
next = process->next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (wait4(process->pid, &status, WNOHANG, NULL) == process->pid)
|
|
|
|
{
|
|
|
|
process->status = status;
|
|
|
|
_last_status = status;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "Process %d has returned %d\n", process->pid, status);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*printf("** signal_child\n");*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
stop_process(process);
|
|
|
|
//stop_process_after(process);
|
|
|
|
//break; // one exit at a time // why ?
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
process = next;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, ">> callback_child\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-11 00:36:11 +01:00
|
|
|
static void signal_child(int sig, siginfo_t *info, void *context)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGCHLD: _pipe_child[0] = %d\n", _pipe_child[0]);
|
|
|
|
#endif
|
|
|
|
char buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!_init)
|
|
|
|
return;
|
|
|
|
|
2011-02-11 00:36:11 +01:00
|
|
|
//fprintf(stderr, "SIGCHLD\n");
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
buffer = 42;
|
2010-10-31 23:05:29 +01:00
|
|
|
if (write(_pipe_child[1], &buffer, 1) != 1)
|
|
|
|
ERROR_panic("Cannot write into SIGCHLD pipe");
|
2011-02-11 00:36:11 +01:00
|
|
|
|
2011-02-12 22:58:31 +01:00
|
|
|
if (_old_SIGCHLD_action.sa_handler != SIG_DFL && _old_SIGCHLD_action.sa_handler != SIG_IGN)
|
2011-02-11 00:36:11 +01:00
|
|
|
{
|
|
|
|
if (_old_SIGCHLD_action.sa_flags & SA_SIGINFO)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "Calling old action %p\n", _old_SIGCHLD_action.sa_sigaction);
|
|
|
|
(*_old_SIGCHLD_action.sa_sigaction)(sig, info, context);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "Calling old handler %p\n", _old_SIGCHLD_action.sa_handler);
|
|
|
|
(*_old_SIGCHLD_action.sa_handler)(sig);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void init_child(void)
|
|
|
|
{
|
2011-02-11 00:36:11 +01:00
|
|
|
struct sigaction action;
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (_init)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "init_child()\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (pipe(_pipe_child) != 0)
|
|
|
|
ERROR_panic("Cannot create SIGCHLD pipes");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
fcntl(_pipe_child[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(_pipe_child[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "_pipe_child[0] = %d\n", _pipe_child[0]);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/* This may work only on Linux. Should use sigaction() instead! */
|
2011-02-11 00:36:11 +01:00
|
|
|
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_sigaction = signal_child;
|
|
|
|
|
|
|
|
if (sigaction(SIGCHLD, NULL, &_old_SIGCHLD_action) != 0)
|
|
|
|
ERROR_panic("Cannot install SIGCHLD handler");
|
|
|
|
|
|
|
|
if (sigaction(SIGCHLD, &action, NULL) != 0)
|
|
|
|
ERROR_panic("Cannot install SIGCHLD handler");
|
|
|
|
|
|
|
|
//fprintf(stderr, "old action = %p / %p\n", _old_SIGCHLD_action.sa_handler, _old_SIGCHLD_action.sa_sigaction);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_Watch(_pipe_child[0], GB_WATCH_READ, (void *)callback_child, 0);
|
|
|
|
_init = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_child(void)
|
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!_init)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "exit_child()\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_Watch(_pipe_child[0], GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(_pipe_child[0]);
|
|
|
|
close(_pipe_child[1]);
|
2011-02-11 00:36:11 +01:00
|
|
|
|
|
|
|
if (sigaction(SIGCHLD, &_old_SIGCHLD_action, NULL) != 0)
|
|
|
|
ERROR_panic("Cannot uninstall SIGCHLD handler");
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
_init = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 16:52:36 +02:00
|
|
|
CPROCESS *CPROCESS_create(int mode, void *cmd, char *name, CARRAY *env)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
CPROCESS *process;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*printf("** CPROCESS_create <<<< \n");*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
//if (!name || !*name)
|
|
|
|
// name = "Process";
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-18 02:14:53 +02:00
|
|
|
process = OBJECT_new(CLASS_Process, name, OP ? (OBJECT *)OP : (OBJECT *)CP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
init_process(process);
|
2009-05-22 16:52:36 +02:00
|
|
|
run_process(process, mode, cmd, env);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
OBJECT_UNREF_KEEP(process, "CPROCESS_create");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (!name || !*name)
|
|
|
|
STREAM_blocking(CSTREAM_stream(process), TRUE);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*printf("** CPROCESS_create >>>> \n");*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
return process;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void CPROCESS_wait_for(CPROCESS *process)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
//sigset_t sig, old;
|
|
|
|
//bool have_sigchld;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
printf("Waiting for %d\n", process->pid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*sigemptyset(&sig);
|
|
|
|
sigaddset(&sig, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &sig, &old);
|
|
|
|
|
|
|
|
have_sigchld = sigismember(&old, SIGCHLD);
|
|
|
|
if (have_sigchld)
|
|
|
|
sigdelset(&old, SIGCHLD);*/
|
|
|
|
|
|
|
|
/*if (process->to_string)
|
|
|
|
{
|
|
|
|
while (process->running)
|
|
|
|
WATCH_loop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (process->running)
|
|
|
|
WATCH_loop();
|
|
|
|
//sigsuspend(&old);
|
|
|
|
}*/
|
|
|
|
OBJECT_REF(process, "CPROCESS_wait_for");
|
|
|
|
while (process->running)
|
|
|
|
{
|
|
|
|
//HOOK_DEFAULT(wait, WATCH_wait)(0);
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "watching _pipe_child[0] = %d\n", _pipe_child[0]);
|
|
|
|
#endif
|
|
|
|
ret = WATCH_process(_pipe_child[0], process->out);
|
|
|
|
if (ret == _pipe_child[0])
|
|
|
|
callback_child(_pipe_child[0], GB_WATCH_READ, 0);
|
|
|
|
else if (ret == process->out)
|
|
|
|
callback_write(process->out, GB_WATCH_READ, process);
|
2010-06-05 01:48:53 +02:00
|
|
|
else
|
|
|
|
usleep(1000);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
OBJECT_UNREF(process, "CPROCESS_wait_for");
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
sigsuspend(&old);
|
|
|
|
if (!process->running)
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "Waiting: %d\n", process->running);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sleep(10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*if (have_sigchld)
|
|
|
|
sigaddset(&old, SIGCHLD);
|
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
printf("Got it !\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_exit)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
//fprintf(stderr, "CPROCESS_exit\n");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
while (RunningProcessList)
|
|
|
|
stop_process(RunningProcessList);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
exit_child();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_new)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
init_process(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_free)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
printf("CPROCESS_free %p\n", THIS);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
exit_process(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CPROCESS_id)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_ReturnInt(THIS->pid);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_kill)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!THIS->running)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-26 10:11:52 +01:00
|
|
|
if (THIS->process_group)
|
|
|
|
kill(-getpgid(THIS->pid), SIGKILL);
|
|
|
|
else
|
|
|
|
kill(THIS->pid, SIGKILL);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
CPROCESS_wait_for(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_signal)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!THIS->running)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/*
|
|
|
|
printf("Send SIGUSR1 to process %d\n", THIS->pid);
|
|
|
|
fflush(NULL);*/
|
|
|
|
kill(THIS->pid, SIGUSR1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
BEGIN_METHOD(CPROCESS_send, GB_STRING text)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (!THIS->running || THIS->in < 0)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
STREAM_write(&THIS->ob.stream, STRING(text), LENGTH(text));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CPROCESS_state)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (THIS->running)
|
|
|
|
GB_ReturnInteger(1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (WIFEXITED(THIS->status))
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(2);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CPROCESS_value)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
int status;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (THIS->running)
|
|
|
|
{
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
status = THIS->status;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
GB_ReturnInteger(WEXITSTATUS(status));
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
GB_ReturnInteger(WTERMSIG(status));
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CPROCESS_last_state)
|
|
|
|
|
|
|
|
if (WIFEXITED(_last_status))
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(2);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CPROCESS_last_value)
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
int status;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
status = _last_status;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
GB_ReturnInteger(WEXITSTATUS(status));
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
GB_ReturnInteger(WTERMSIG(status));
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CPROCESS_wait)
|
|
|
|
|
|
|
|
CPROCESS_wait_for(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_Process[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_DECLARE("Process", sizeof(CPROCESS)), GB_NOT_CREATABLE(),
|
|
|
|
GB_INHERITS("Stream"),
|
|
|
|
|
|
|
|
GB_CONSTANT("Stopped", "i", 0),
|
|
|
|
GB_CONSTANT("Running", "i", 1),
|
|
|
|
GB_CONSTANT("Crashed", "i", 2),
|
|
|
|
GB_CONSTANT("Signaled", "i", 2),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY_READ("LastState", "i", CPROCESS_last_state),
|
|
|
|
GB_STATIC_PROPERTY_READ("LastValue", "i", CPROCESS_last_value),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Id", "i", CPROCESS_id),
|
|
|
|
GB_PROPERTY_READ("Handle", "i", CPROCESS_id),
|
|
|
|
GB_PROPERTY_READ("State", "i", CPROCESS_state),
|
|
|
|
GB_PROPERTY_READ("Value", "i", CPROCESS_value),
|
|
|
|
|
|
|
|
GB_STATIC_METHOD("_exit", NULL, CPROCESS_exit, NULL),
|
|
|
|
GB_METHOD("_new", NULL, CPROCESS_new, NULL),
|
|
|
|
GB_METHOD("_free", NULL, CPROCESS_free, NULL),
|
|
|
|
|
|
|
|
GB_METHOD("Kill", NULL, CPROCESS_kill, NULL),
|
|
|
|
GB_METHOD("Signal", NULL, CPROCESS_signal, NULL),
|
|
|
|
GB_METHOD("Wait", NULL, CPROCESS_wait, NULL),
|
|
|
|
|
|
|
|
//GB_METHOD("Send", NULL, CPROCESS_send, "(Input)s"),
|
|
|
|
//GB_METHOD("_print", NULL, CPROCESS_send, "(Input)s"),
|
|
|
|
|
|
|
|
GB_EVENT("Read", NULL, NULL, &EVENT_Read),
|
|
|
|
GB_EVENT("Error", NULL, "(Error)s", &EVENT_Error),
|
|
|
|
GB_EVENT("Kill", NULL, NULL, &EVENT_Kill),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|