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
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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>
|
2012-01-13 09:04:11 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_replace.h"
|
2012-01-06 02:47:50 +01:00
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gb_array.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_stream.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"
|
2011-07-25 02:50:11 +02:00
|
|
|
#include "gbx_signal.h"
|
2017-06-10 01:00:45 +02:00
|
|
|
#include "gbx_event.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gbx_c_process.h"
|
|
|
|
|
2017-08-18 13:19:10 +02:00
|
|
|
//#define DEBUG_ME
|
2016-11-27 03:44:07 +01:00
|
|
|
//#define DEBUG_CHILD
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-05 17:18:57 +01:00
|
|
|
char *CPROCESS_shell = NULL;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
DECLARE_EVENT(EVENT_Read);
|
|
|
|
DECLARE_EVENT(EVENT_Error);
|
|
|
|
DECLARE_EVENT(EVENT_Kill);
|
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
static CPROCESS *_running_process_list = NULL;
|
|
|
|
static int _running_process = 0;
|
|
|
|
static int _ignore_process = 0;
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
static SIGNAL_CALLBACK *_SIGCHLD_callback;
|
2007-12-30 17:41:49 +01:00
|
|
|
static bool _init = FALSE;
|
|
|
|
|
|
|
|
static int _last_status = 0;
|
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
static int _last_child_error = 0;
|
|
|
|
static int _last_child_error_errno = 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static void init_child(void);
|
|
|
|
static void exit_child(void);
|
2017-06-10 01:00:45 +02:00
|
|
|
static bool wait_child(CPROCESS *process);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
enum {
|
|
|
|
CHILD_NO_ERROR,
|
|
|
|
CHILD_CANNOT_OPEN_TTY,
|
|
|
|
CHILD_CANNOT_INIT_TTY,
|
|
|
|
CHILD_CANNOT_PLUG_INPUT,
|
|
|
|
CHILD_CANNOT_PLUG_OUTPUT,
|
|
|
|
CHILD_CANNOT_EXEC,
|
|
|
|
};
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
static const char *const _child_error[] = {
|
|
|
|
NULL,
|
|
|
|
"cannot open slave pseudo-terminal: ",
|
|
|
|
"cannot initialize pseudo-terminal: ",
|
|
|
|
"cannot plug standard input: ",
|
|
|
|
"cannot plug standard output and standard error: ",
|
2013-03-28 22:33:19 +01:00
|
|
|
"cannot run executable: "
|
2012-01-06 02:47:50 +01:00
|
|
|
};
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void close_fd(int *pfd)
|
|
|
|
{
|
|
|
|
int fd = *pfd;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2013-01-19 18:42:09 +01:00
|
|
|
#ifdef DEBUG_ME
|
2014-01-14 13:06:39 +01:00
|
|
|
fprintf(stderr, "unwatch & close: %d\n", fd);
|
2013-01-19 18:42:09 +01:00
|
|
|
#endif
|
2012-08-31 00:13:25 +02:00
|
|
|
GB_Watch(fd, GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(fd);
|
|
|
|
*pfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
static void add_process_to_running_list(CPROCESS *process)
|
|
|
|
{
|
2013-08-12 22:48:45 +02:00
|
|
|
if (_running_process_list)
|
|
|
|
_running_process_list->prev = process;
|
2013-03-30 13:15:18 +01:00
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
process->next = _running_process_list;
|
2013-03-30 13:15:18 +01:00
|
|
|
process->prev = NULL;
|
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
_running_process_list = process;
|
2013-03-30 13:15:18 +01:00
|
|
|
|
|
|
|
process->running = TRUE;
|
2013-08-12 22:48:45 +02:00
|
|
|
_running_process++;
|
2013-03-30 13:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_process_from_running_list(CPROCESS *process)
|
|
|
|
{
|
|
|
|
if (process->prev)
|
|
|
|
process->prev->next = process->next;
|
|
|
|
|
|
|
|
if (process->next)
|
|
|
|
process->next->prev = process->prev;
|
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
if (process == _running_process_list)
|
|
|
|
_running_process_list = process->next;
|
2013-03-30 13:15:18 +01:00
|
|
|
|
|
|
|
process->running = FALSE;
|
2017-08-18 13:15:03 +02:00
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
_running_process--;
|
|
|
|
if (process->ignore)
|
|
|
|
_ignore_process--;
|
2013-03-30 13:15:18 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
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
|
2014-11-28 00:31:51 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (process->to_string)
|
|
|
|
{
|
2014-12-01 17:22:34 +01:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
n = read(fd, COMMON_buffer, 256);
|
|
|
|
if (n >= 0 || errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (n > 0)
|
2014-12-01 17:22:34 +01:00
|
|
|
{
|
2011-09-07 14:38:29 +02:00
|
|
|
process->result = STRING_add(process->result, COMMON_buffer, n);
|
2014-12-01 17:22:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
2014-11-30 23:41:41 +01:00
|
|
|
|
|
|
|
if (GB_CanRaise(process, EVENT_Read))
|
2014-11-29 15:45:33 +01:00
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
if (!STREAM_read_ahead(CSTREAM_TO_STREAM(process)))
|
2014-11-30 23:41:41 +01:00
|
|
|
{
|
2014-11-29 15:45:33 +01:00
|
|
|
GB_Raise(process, EVENT_Read, 0);
|
2014-11-30 23:41:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-11-29 15:45:33 +01:00
|
|
|
}
|
2014-11-30 23:41:41 +01:00
|
|
|
|
|
|
|
close_fd(&process->out);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-01 12:51:27 +01:00
|
|
|
static void callback_error(int fd, int type, CPROCESS *process)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
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
|
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
if (process->to_string && process->with_error)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
n = read(fd, COMMON_buffer, 256);
|
|
|
|
if (n >= 0 || errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
process->result = STRING_add(process->result, COMMON_buffer, n);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:51:27 +01:00
|
|
|
if (GB_CanRaise(process, EVENT_Error))
|
2008-10-04 16:07:53 +02:00
|
|
|
{
|
2014-12-01 12:51:27 +01:00
|
|
|
n = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
GB_Raise(process, EVENT_Error, 1, GB_T_STRING, buffer, n);
|
|
|
|
return;
|
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-12-01 12:51:27 +01:00
|
|
|
close_fd(&process->err);
|
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)
|
|
|
|
{
|
2017-08-23 18:10:58 +02:00
|
|
|
//process->watch = GB_WATCH_NONE;
|
2008-10-04 16:07:53 +02:00
|
|
|
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
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
close_fd(&THIS->out);
|
|
|
|
close_fd(&THIS->err);
|
2017-08-23 18:10:58 +02:00
|
|
|
|
2009-02-04 23:51:20 +01:00
|
|
|
STREAM_close(&THIS->ob.stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
static void prepare_child_error(CPROCESS *_object)
|
2012-01-06 02:47:50 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char path[PATH_MAX];
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
snprintf(path, sizeof(path), FILE_TEMP_DIR "/%d.child", (int)getuid(), (int)getpid(), (int)THIS->pid);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
#ifdef DEBUG_ME
|
2012-04-15 01:35:33 +02:00
|
|
|
fprintf(stderr, "prepare_child_error: %p: %s\n", _object, path);
|
2012-01-06 02:47:50 +01:00
|
|
|
#endif
|
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
if (_last_child_error == 0)
|
2012-01-06 02:47:50 +01:00
|
|
|
{
|
2012-04-15 01:35:33 +02:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd >= 0)
|
2012-01-06 02:47:50 +01:00
|
|
|
{
|
2012-04-15 01:35:33 +02:00
|
|
|
if (read(fd, &_last_child_error, sizeof(int)) != sizeof(int)
|
|
|
|
|| read(fd, &_last_child_error_errno, sizeof(int)) != sizeof(int))
|
|
|
|
{
|
|
|
|
_last_child_error = -1;
|
|
|
|
_last_child_error_errno = 0;
|
|
|
|
}
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
close(fd);
|
2012-01-06 02:47:50 +01:00
|
|
|
}
|
2012-04-15 01:35:33 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "prepare_child_error: error = %d errno = %d\n", _last_child_error, _last_child_error_errno);
|
|
|
|
#endif
|
2012-01-06 02:47:50 +01:00
|
|
|
}
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
unlink(path);
|
|
|
|
}
|
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
static void throw_last_child_error()
|
|
|
|
{
|
|
|
|
int child_error, child_errno;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
if (_last_child_error == 0)
|
|
|
|
return;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
child_error = _last_child_error;
|
|
|
|
child_errno = _last_child_error_errno;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
_last_child_error = 0;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "throw_last_child_error: %d %d\n", child_error, child_errno);
|
|
|
|
#endif
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
if (child_error < 0)
|
|
|
|
THROW(E_CHILD, "unknown error", "");
|
|
|
|
else
|
|
|
|
THROW(E_CHILD, _child_error[child_error], strerror(child_errno));
|
|
|
|
}
|
|
|
|
|
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;
|
2016-11-27 03:43:15 +01:00
|
|
|
bool do_exit_process = THIS->in >= 0;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
2014-11-28 21:15:02 +01:00
|
|
|
fprintf(stderr, "stop_process_after: %p out = %d err = %d\n", _object, THIS->out, THIS->err);
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
if (WIFEXITED(THIS->status) && WEXITSTATUS(THIS->status) == 255)
|
2012-04-15 01:35:33 +02:00
|
|
|
{
|
|
|
|
prepare_child_error(THIS);
|
|
|
|
exit_process(THIS);
|
2013-11-01 01:29:32 +01:00
|
|
|
//OBJECT_detach((OBJECT *)THIS);
|
2012-04-15 01:35:33 +02:00
|
|
|
}
|
2012-01-06 02:47:50 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
/* Vidage du tampon d'erreur */
|
2014-12-01 12:51:27 +01:00
|
|
|
while (THIS->err >= 0)
|
2014-01-14 13:06:39 +01:00
|
|
|
{
|
2014-12-01 12:51:27 +01:00
|
|
|
callback_error(THIS->err, 0, THIS);
|
2014-01-14 13:06:39 +01:00
|
|
|
do_exit_process = TRUE;
|
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
/* Vidage du tampon de sortie */
|
|
|
|
if (THIS->out >= 0)
|
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
stream = CSTREAM_TO_STREAM(THIS);
|
2014-12-12 00:41:48 +01:00
|
|
|
while (THIS->out >= 0 && !STREAM_is_closed(stream))
|
|
|
|
callback_write(THIS->out, 0, THIS);
|
2008-10-04 16:07:53 +02:00
|
|
|
|
2014-01-14 13:06:39 +01:00
|
|
|
do_exit_process = TRUE;
|
2013-11-01 01:29:32 +01:00
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
|
2014-01-14 13:06:39 +01:00
|
|
|
if (do_exit_process)
|
|
|
|
exit_process(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
static void stop_process(CPROCESS *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-11-30 23:41:41 +01:00
|
|
|
if (!THIS->running)
|
2008-10-04 16:07:53 +02:00
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
2014-11-30 23:41:41 +01:00
|
|
|
fprintf(stderr, "stop_process: %p\n", THIS);
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
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
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
stop_process_after(THIS);
|
|
|
|
remove_process_from_running_list(THIS);
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "Raising Kill event for %p: parent = %p can raise = %d\n", THIS, OBJECT_parent(THIS), GB_CanRaise(THIS, EVENT_Kill));
|
|
|
|
#endif
|
|
|
|
GB_Raise(THIS, EVENT_Kill, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
OBJECT_detach((OBJECT *)THIS);
|
|
|
|
OBJECT_UNREF(_object);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
//if (!_running_process_list)
|
|
|
|
if (_running_process <= _ignore_process)
|
2008-10-04 16:07:53 +02:00
|
|
|
exit_child();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
static void abort_child(int error)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int save_errno;
|
|
|
|
char path[PATH_MAX];
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
save_errno = errno;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-04-20 03:01:48 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "abort_child: %d %d\n", error, save_errno);
|
|
|
|
#endif
|
2014-11-28 00:31:51 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
snprintf(path, sizeof(path), FILE_TEMP_DIR "/%d.child", (int)getuid(), (int)getppid(), (int)getpid());
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
fd = open(path, O_CREAT | O_WRONLY, 0600);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
write(fd, &error, sizeof(int)) == sizeof(int)
|
|
|
|
&& write(fd, &save_errno, sizeof(int)) == sizeof(int);
|
|
|
|
close(fd);
|
|
|
|
}
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
_exit(255);
|
2012-01-06 02:47:50 +01:00
|
|
|
}
|
|
|
|
|
2009-08-06 02:22:28 +02:00
|
|
|
static void init_child_tty(int fd)
|
|
|
|
{
|
|
|
|
struct termios terminal = { 0 };
|
2017-08-23 18:10:58 +02:00
|
|
|
struct termios check;
|
|
|
|
|
2009-08-06 02:22:28 +02:00
|
|
|
tcgetattr(fd, &terminal);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2017-08-28 19:04:25 +02:00
|
|
|
terminal.c_iflag |= IXON | IXOFF | ICRNL;
|
2010-03-05 02:20:09 +01:00
|
|
|
#ifdef IUTF8
|
2015-12-27 19:16:32 +01:00
|
|
|
if (LOCAL_is_UTF8)
|
2009-08-06 02:22:28 +02:00
|
|
|
terminal.c_iflag |= IUTF8;
|
2010-03-05 02:20:09 +01:00
|
|
|
#endif
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2009-08-06 02:22:28 +02:00
|
|
|
terminal.c_oflag |= OPOST;
|
2017-08-23 18:10:58 +02:00
|
|
|
//terminal.c_oflag &= ~ONLCR;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2017-08-23 18:10:58 +02:00
|
|
|
terminal.c_lflag |= ISIG | ICANON | IEXTEN | ECHO;
|
2017-08-21 13:57:40 +02:00
|
|
|
///terminal.c_lflag &= ~ECHO;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
#ifdef DEBUG_CHILD
|
2012-01-06 02:47:50 +01:00
|
|
|
fprintf(stderr, "init_child_tty: %s\n", isatty(fd) ? ttyname(fd) : "not a tty!");
|
|
|
|
#endif
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-01-06 02:47:50 +01:00
|
|
|
if (tcsetattr(fd, TCSANOW, &terminal))
|
|
|
|
{
|
2014-11-30 23:41:41 +01:00
|
|
|
#ifdef DEBUG_CHILD
|
2012-01-06 02:47:50 +01:00
|
|
|
int save_errno = errno;
|
|
|
|
fprintf(stderr, "init_child_tty: errno = %d\n", errno);
|
|
|
|
errno = save_errno;
|
|
|
|
#endif
|
|
|
|
abort_child(CHILD_CANNOT_INIT_TTY);
|
|
|
|
}
|
2017-08-23 18:10:58 +02:00
|
|
|
|
|
|
|
tcgetattr(fd, &check);
|
|
|
|
if (check.c_iflag != terminal.c_iflag || check.c_oflag != terminal.c_oflag || check.c_lflag != terminal.c_lflag)
|
|
|
|
abort_child(CHILD_CANNOT_INIT_TTY);
|
2009-08-06 02:22:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 22:05:43 +02:00
|
|
|
const char *CPROCESS_search_program_in_path(char *name)
|
2014-09-13 22:37:50 +02:00
|
|
|
{
|
|
|
|
char *search;
|
|
|
|
char *p, *p2;
|
|
|
|
int len;
|
2014-09-15 22:05:43 +02:00
|
|
|
const char *path;
|
2016-10-28 12:12:09 +02:00
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2014-09-13 22:37:50 +02:00
|
|
|
|
|
|
|
if (strchr(name, '/'))
|
2014-09-15 22:05:43 +02:00
|
|
|
{
|
|
|
|
if (access(name, X_OK) == 0)
|
|
|
|
return name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-13 22:37:50 +02:00
|
|
|
|
|
|
|
search = getenv("PATH");
|
|
|
|
if (!search || !*search)
|
|
|
|
search = "/usr/bin:/bin";
|
|
|
|
|
|
|
|
search = STRING_new_zero(search);
|
|
|
|
|
2014-09-15 22:05:43 +02:00
|
|
|
path = NULL;
|
|
|
|
|
2014-09-13 22:37:50 +02:00
|
|
|
//fprintf(stderr, "search_program_in_path: '%s' in '%s'\n", name, search);
|
|
|
|
|
|
|
|
p = search;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
p2 = strchr(p, ':');
|
|
|
|
if (p2)
|
|
|
|
len = p2 - p;
|
|
|
|
else
|
|
|
|
len = strlen(p);
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
p[len] = 0;
|
|
|
|
//fprintf(stderr, "trying: %s\n", p);
|
|
|
|
path = FILE_cat(p, name, NULL);
|
|
|
|
if (access(path, X_OK) == 0)
|
|
|
|
break;
|
2014-09-15 22:05:43 +02:00
|
|
|
path = NULL;
|
2014-09-13 22:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!p2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = p2 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
STRING_free(&search);
|
|
|
|
//fprintf(stderr, "--> %s\n", path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-11-29 15:20:31 +01:00
|
|
|
static const char *shell[] = { NULL, "-c", NULL, NULL };
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
int fdin[2], fdout[2], fderr[2];
|
|
|
|
pid_t pid;
|
|
|
|
char **argv;
|
|
|
|
CARRAY *array;
|
|
|
|
int i, n;
|
|
|
|
sigset_t sig, old;
|
2013-03-30 13:15:18 +01:00
|
|
|
int save_errno;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
// for virtual terminal
|
2008-10-04 16:07:53 +02:00
|
|
|
int fd_master = -1;
|
|
|
|
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;
|
2014-09-13 22:37:50 +02:00
|
|
|
const char *exec;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
if (mode & PM_SHELL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "run_process %p: %s\n", process, (char *)cmd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
argv = (char **)shell;
|
|
|
|
|
2012-02-05 17:18:57 +01:00
|
|
|
if (CPROCESS_shell)
|
|
|
|
argv[0] = CPROCESS_shell;
|
2014-11-29 15:20:31 +01:00
|
|
|
else
|
|
|
|
argv[0] = "/bin/sh";
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
argv[2] = (char *)cmd;
|
|
|
|
|
|
|
|
if (argv[2] == NULL || *argv[2] == 0)
|
|
|
|
return;
|
2014-09-13 22:37:50 +02:00
|
|
|
|
|
|
|
exec = argv[0];
|
2015-12-27 19:16:32 +01:00
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&argv, sizeof(*argv) * (n + 1));
|
2008-10-04 16:07:53 +02:00
|
|
|
memcpy(argv, array->data, sizeof(*argv) * n);
|
|
|
|
argv[n] = NULL;
|
|
|
|
|
2014-09-15 22:05:43 +02:00
|
|
|
exec = CPROCESS_search_program_in_path(argv[0]);
|
2014-09-13 22:37:50 +02:00
|
|
|
if (!exec)
|
|
|
|
{
|
|
|
|
IFREE(argv);
|
|
|
|
THROW(E_NEXIST);
|
|
|
|
}
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_STRING)
|
|
|
|
{
|
|
|
|
process->to_string = TRUE;
|
2018-06-25 19:17:44 +02:00
|
|
|
process->with_error = (mode & PM_ERROR) != 0;
|
2008-10-04 16:07:53 +02:00
|
|
|
process->result = NULL;
|
|
|
|
mode |= PM_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
2012-01-06 02:47:50 +01:00
|
|
|
fd_master = posix_openpt(O_RDWR | O_NOCTTY);
|
2008-10-04 16:07:53 +02:00
|
|
|
if (fd_master < 0)
|
2013-03-30 13:15:18 +01:00
|
|
|
goto __ABORT_ERRNO;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
grantpt(fd_master);
|
|
|
|
unlockpt(fd_master);
|
2009-05-22 16:52:36 +02:00
|
|
|
slave = ptsname(fd_master);
|
2012-01-06 02:47:50 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "run_process: slave = %s\n", slave);
|
|
|
|
#endif
|
2017-08-26 13:11:59 +02:00
|
|
|
|
|
|
|
if (mode & PM_TERM)
|
|
|
|
{
|
|
|
|
if (tcgetattr(fd_master, &termios_master))
|
|
|
|
goto __ABORT_ERRNO;
|
|
|
|
|
|
|
|
cfmakeraw(&termios_master);
|
|
|
|
//termios_master.c_lflag &= ~ECHO;
|
|
|
|
|
|
|
|
if (tcsetattr(fd_master, TCSANOW, &termios_master))
|
|
|
|
goto __ABORT_ERRNO;
|
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Create pipes */
|
|
|
|
|
|
|
|
if ((mode & PM_WRITE) && pipe(fdin) != 0)
|
2013-03-30 13:15:18 +01:00
|
|
|
goto __ABORT_ERRNO;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
if ((mode & PM_READ) && (pipe(fdout) != 0 || pipe(fderr) != 0))
|
2013-03-30 13:15:18 +01:00
|
|
|
goto __ABORT_ERRNO;
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
// Adding to the running process list
|
|
|
|
|
|
|
|
add_process_to_running_list(process);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(process);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
// Start the SIGCHLD callback
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
init_child();
|
|
|
|
|
|
|
|
// Block SIGCHLD and fork
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
sigemptyset(&sig);
|
|
|
|
|
|
|
|
sigaddset(&sig, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &sig, &old);
|
|
|
|
|
2014-05-27 12:32:24 +02:00
|
|
|
if (mode & PM_SHELL || mode & PM_TERM || env)
|
2014-05-29 18:01:16 +02:00
|
|
|
pid = fork();
|
2014-05-27 12:32:24 +02:00
|
|
|
else
|
|
|
|
pid = vfork();
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (pid == (-1))
|
|
|
|
{
|
|
|
|
stop_process(process);
|
2014-03-02 21:18:06 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2013-03-30 13:15:18 +01:00
|
|
|
goto __ABORT_ERRNO;
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
// parent process
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (pid)
|
|
|
|
{
|
|
|
|
process->pid = pid;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "fork: pid = %d\n", pid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2013-01-19 18:42:09 +01:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "watch: out = %d err = %d\n", process->out, process->err);
|
|
|
|
#endif
|
2008-10-04 16:07:53 +02:00
|
|
|
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)
|
|
|
|
{
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&argv);
|
2008-10-04 14:31:49 +02:00
|
|
|
}
|
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
2014-03-02 21:18:06 +01:00
|
|
|
else //------------ child process ----------------------------
|
2008-10-04 16:07:53 +02:00
|
|
|
{
|
2014-03-02 21:18:06 +01:00
|
|
|
int fd_slave;
|
|
|
|
bool pwd;
|
|
|
|
int ch_i, ch_n;
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
//bool stdin_isatty = isatty(STDIN_FILENO);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2011-03-26 10:11:52 +01:00
|
|
|
if (mode & PM_SHELL)
|
2011-11-19 16:31:40 +01:00
|
|
|
setpgid(0, 0);
|
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)
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_OPEN_TTY);
|
2017-08-23 18:10:58 +02:00
|
|
|
|
|
|
|
init_child_tty(fd_slave);
|
2014-11-28 00:31:51 +01:00
|
|
|
|
|
|
|
/*#ifdef DEBUG_ME
|
2012-01-13 09:04:11 +01:00
|
|
|
fprintf(stderr, "run_process (child): slave = %s isatty = %d\n", slave, isatty(fd_slave));
|
2014-11-28 00:31:51 +01:00
|
|
|
#endif*/
|
2012-01-13 09:04:11 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
if (mode & PM_WRITE)
|
|
|
|
{
|
|
|
|
if (dup2(fd_slave, STDIN_FILENO) == -1)
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_PLUG_INPUT);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_READ)
|
|
|
|
{
|
|
|
|
if ((dup2(fd_slave, STDOUT_FILENO) == -1)
|
|
|
|
|| (dup2(fd_slave, STDERR_FILENO) == -1))
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_PLUG_OUTPUT);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
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.
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2017-08-23 18:10:58 +02:00
|
|
|
/*if (mode & PM_WRITE)
|
2012-01-06 02:47:50 +01:00
|
|
|
init_child_tty(STDIN_FILENO);
|
|
|
|
else if (mode & PM_READ)
|
2017-08-23 18:10:58 +02:00
|
|
|
init_child_tty(STDOUT_FILENO);*/
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2009-08-06 02:22:28 +02:00
|
|
|
/*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)
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_PLUG_INPUT);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & PM_READ)
|
|
|
|
{
|
|
|
|
close(fdout[0]);
|
|
|
|
close(fderr[0]);
|
|
|
|
|
|
|
|
if ((dup2(fdout[1], STDOUT_FILENO) == -1)
|
|
|
|
|| (dup2(fderr[1], STDERR_FILENO) == -1))
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_PLUG_OUTPUT);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-02 23:42:05 +01:00
|
|
|
pwd = FALSE;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2009-05-22 16:52:36 +02:00
|
|
|
if (env)
|
|
|
|
{
|
|
|
|
char *str;
|
2014-03-02 21:18:06 +01:00
|
|
|
ch_n = ARRAY_count(env->data);
|
|
|
|
for (ch_i = 0; ch_i < ch_n; ch_i++)
|
2009-05-22 16:52:36 +02:00
|
|
|
{
|
2014-03-02 21:18:06 +01:00
|
|
|
str = ((char **)env->data)[ch_i];
|
2009-05-22 16:52:36 +02:00
|
|
|
if (putenv(str))
|
2010-08-28 14:33:05 +02:00
|
|
|
ERROR_warning("cannot set environment string: %s", str);
|
2013-03-02 23:42:05 +01:00
|
|
|
if (strncmp(str, "PWD=", 4) == 0 && chdir(&str[4]) == 0)
|
|
|
|
pwd = TRUE;
|
2009-05-22 16:52:36 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-02 23:42:05 +01:00
|
|
|
// Return to the parent working directory if the PWD environment variable has not been set
|
|
|
|
if (!pwd)
|
|
|
|
FILE_chdir(PROJECT_oldcwd);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2014-09-13 22:37:50 +02:00
|
|
|
execv(exec, (char **)argv);
|
2012-01-06 02:47:50 +01:00
|
|
|
abort_child(CHILD_CANNOT_EXEC);
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-11-28 00:31:51 +01:00
|
|
|
update_stream(process);
|
2014-03-02 21:18:06 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "run_process: child is OK\n");
|
|
|
|
#endif
|
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
return;
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-30 13:15:18 +01:00
|
|
|
__ABORT_ERRNO:
|
|
|
|
|
|
|
|
save_errno = errno;
|
|
|
|
stop_process(process);
|
|
|
|
THROW_SYSTEM(save_errno, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2014-11-28 00:31:51 +01:00
|
|
|
void CPROCESS_check(void *_object)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "CPROCESS_check: %p\n", THIS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
usleep(100);
|
2017-06-10 01:00:45 +02:00
|
|
|
if (wait_child(THIS))
|
|
|
|
{
|
2017-08-21 01:45:59 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "CPROCESS_check: stop process later\n");
|
|
|
|
#endif
|
|
|
|
stop_process_after(THIS);
|
2017-06-10 01:00:45 +02:00
|
|
|
EVENT_post(stop_process, (intptr_t)THIS);
|
|
|
|
throw_last_child_error();
|
|
|
|
}
|
2014-11-28 21:15:02 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "CPROCESS_check: %p END\n", THIS);
|
2014-11-28 00:31:51 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 01:00:45 +02:00
|
|
|
static bool wait_child(CPROCESS *process)
|
2014-03-02 21:18:06 +01:00
|
|
|
{
|
|
|
|
int status;
|
2019-03-12 03:46:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2017-08-18 13:15:03 +02:00
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "wait_child: check process %d\n", process->pid);
|
|
|
|
#endif
|
|
|
|
|
2019-03-12 03:46:30 +01:00
|
|
|
if ((ret = waitpid(process->pid, &status, WNOHANG)) == process->pid)
|
2014-03-02 21:18:06 +01:00
|
|
|
{
|
|
|
|
process->status = status;
|
2017-08-18 13:15:03 +02:00
|
|
|
process->wait = TRUE;
|
2014-03-02 21:18:06 +01:00
|
|
|
_last_status = status;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
2017-08-18 13:15:03 +02:00
|
|
|
fprintf(stderr, "wait_child: process %d has returned %d\n", process->pid, status);
|
2014-03-02 21:18:06 +01:00
|
|
|
#endif
|
|
|
|
|
2017-06-10 01:00:45 +02:00
|
|
|
return TRUE;
|
2014-03-02 21:18:06 +01:00
|
|
|
}
|
2017-06-10 01:00:45 +02:00
|
|
|
else
|
2019-03-12 03:46:30 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "wait_child: waitpid() has returned %d\n", ret);
|
|
|
|
#endif
|
|
|
|
|
2017-06-10 01:00:45 +02:00
|
|
|
return FALSE;
|
2019-03-12 03:46:30 +01:00
|
|
|
}
|
2014-03-02 21:18:06 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
static void callback_child(int signum, intptr_t data)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
CPROCESS *process, *next;
|
2014-03-02 21:18:06 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, ">> callback_child\n");
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
for (process = _running_process_list; process; )
|
2008-10-04 16:07:53 +02:00
|
|
|
{
|
|
|
|
next = process->next;
|
2017-06-10 01:00:45 +02:00
|
|
|
if (wait_child(process))
|
|
|
|
stop_process(process);
|
2008-10-04 16:07:53 +02:00
|
|
|
process = next;
|
|
|
|
}
|
2019-03-12 03:46:30 +01:00
|
|
|
|
2012-04-15 01:35:33 +02:00
|
|
|
throw_last_child_error();
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, "<< callback_child\n");
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_child(void)
|
|
|
|
{
|
2020-10-06 16:40:58 +02:00
|
|
|
if (!_init)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ME
|
|
|
|
fprintf(stderr, "init_child()\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-10-06 16:40:58 +02:00
|
|
|
_SIGCHLD_callback = SIGNAL_register(SIGCHLD, callback_child, 0);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2020-10-06 16:40:58 +02:00
|
|
|
_init = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIGNAL_check(SIGCHLD);
|
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
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
SIGNAL_unregister(SIGCHLD, _SIGCHLD_callback);
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
_init = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
static void error_CPROCESS_create(CPROCESS *process)
|
2013-04-07 16:21:48 +02:00
|
|
|
{
|
2017-09-28 02:05:43 +02:00
|
|
|
OBJECT_UNREF(process);
|
2013-04-07 16:21:48 +02: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
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
process = OBJECT_new(CLASS_Process, name, OP ? (OBJECT *)OP : (OBJECT *)CP);
|
2014-11-28 21:15:02 +01:00
|
|
|
//fprintf(stderr, "CPROCESS_create: %p\n", process);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
ON_ERROR_1(error_CPROCESS_create, process)
|
2013-04-07 16:21:48 +02:00
|
|
|
{
|
|
|
|
init_process(process);
|
|
|
|
run_process(process, mode, cmd, env);
|
|
|
|
}
|
|
|
|
END_ERROR
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF_KEEP(process);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (!name || !*name)
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_blocking(CSTREAM_TO_STREAM(process), TRUE);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
return process;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
void CPROCESS_wait_for(CPROCESS *process, int timeout)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-10-04 16:07:53 +02:00
|
|
|
int ret;
|
2012-05-08 17:32:33 +02:00
|
|
|
int sigfd;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ME
|
2013-11-01 01:29:32 +01:00
|
|
|
fprintf(stderr, "Waiting for %d\n", process->pid);
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
2017-08-18 13:15:03 +02:00
|
|
|
|
2018-10-14 21:52:07 +02:00
|
|
|
// If CPROCESS_check() caught the process end, process->running is not set yet, because
|
2017-08-18 13:15:03 +02:00
|
|
|
// stop_process() will be raised at the next event loop. So no need to wait for it.
|
|
|
|
|
|
|
|
if (process->wait)
|
|
|
|
return;
|
2008-10-04 16:07:53 +02:00
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(process);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
ON_ERROR_1(error_CPROCESS_create, process)
|
2008-10-04 16:07:53 +02:00
|
|
|
{
|
2012-04-15 01:35:33 +02:00
|
|
|
while (process->running)
|
|
|
|
{
|
2020-10-06 16:40:58 +02:00
|
|
|
sigfd = SIGNAL_get_fd();
|
2017-01-11 17:57:44 +01:00
|
|
|
#ifdef DEBUG_ME
|
2020-10-06 16:40:58 +02:00
|
|
|
fprintf(stderr, "Watch process %d (end = %d)\n", process->pid, sigfd);
|
2017-01-11 17:57:44 +01:00
|
|
|
#endif
|
2020-12-28 02:45:56 +01:00
|
|
|
SIGNAL_check(SIGCHLD);
|
2018-11-24 20:00:45 +01:00
|
|
|
ret = WATCH_process(sigfd, process->out, process->err, timeout);
|
2017-01-11 17:57:44 +01:00
|
|
|
#ifdef DEBUG_ME
|
2018-11-24 20:00:45 +01:00
|
|
|
fprintf(stderr, "Watch process %d ->%s%s%s%s\n", process->pid, ret & WP_END ? " END" : "", ret & WP_OUTPUT ? " OUTPUT" : "", ret & WP_ERROR ? " ERROR" : "", ret & WP_TIMEOUT ? " TIMEOUT" : "");
|
2017-01-11 17:57:44 +01:00
|
|
|
#endif
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
if (ret & WP_OUTPUT)
|
2012-04-15 01:35:33 +02:00
|
|
|
callback_write(process->out, GB_WATCH_READ, process);
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2018-11-24 20:00:45 +01:00
|
|
|
if (ret & WP_ERROR)
|
|
|
|
callback_error(process->err, GB_WATCH_READ, process);
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
if (ret & WP_END)
|
|
|
|
SIGNAL_raise_callbacks(sigfd, GB_WATCH_READ, 0);
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
if (ret & WP_TIMEOUT)
|
|
|
|
break;
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
if (ret == 0)
|
2012-04-15 01:35:33 +02:00
|
|
|
usleep(1000);
|
|
|
|
}
|
2008-10-04 16:07:53 +02:00
|
|
|
}
|
2012-04-15 01:35:33 +02:00
|
|
|
END_ERROR
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(process);
|
2008-10-04 16:07:53 +02:00
|
|
|
|
|
|
|
#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
|
2013-11-01 01:29:32 +01:00
|
|
|
fprintf(stderr, "Waiting for: got it !\n");
|
2008-10-04 16:07:53 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2014-09-13 22:37:50 +02:00
|
|
|
//-------------------------------------------------------------------------
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_METHOD_VOID(Process_exit)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
//fprintf(stderr, "Process_exit\n");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-12 22:48:45 +02:00
|
|
|
while (_running_process_list)
|
|
|
|
stop_process(_running_process_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
exit_child();
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-12-27 16:18:16 +01:00
|
|
|
STRING_free(&CPROCESS_shell);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_METHOD_VOID(Process_new)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
init_process(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_METHOD_VOID(Process_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
#ifdef DEBUG_ME
|
2013-11-01 01:29:32 +01:00
|
|
|
fprintf(stderr, "Process_free %p\n", THIS);
|
2008-10-04 16:07:53 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_PROPERTY(Process_Id)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
GB_ReturnInt(THIS->pid);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_METHOD_VOID(Process_Kill)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-05-31 14:11:35 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
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)
|
2016-05-31 14:11:35 +02:00
|
|
|
{
|
|
|
|
pid = getpgid(THIS->pid);
|
|
|
|
if (pid < 0)
|
|
|
|
return;
|
|
|
|
pid = - pid;
|
|
|
|
}
|
2011-03-26 10:11:52 +01:00
|
|
|
else
|
2016-05-31 14:11:35 +02:00
|
|
|
pid = THIS->pid;
|
|
|
|
|
|
|
|
kill(pid, SIGKILL);
|
2015-12-27 19:16:32 +01:00
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
//CPROCESS_wait_for(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_METHOD_VOID(Process_Signal)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
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
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_PROPERTY(Process_State)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_PROPERTY(Process_Value)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
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
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_PROPERTY(Process_LastState)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (WIFEXITED(_last_status))
|
|
|
|
GB_ReturnInteger(0);
|
|
|
|
else
|
|
|
|
GB_ReturnInteger(2);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
BEGIN_PROPERTY(Process_LastValue)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
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
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
BEGIN_METHOD(Process_Wait, GB_FLOAT timeout)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
// FIXME: Does not work if Ignore is set
|
2012-08-31 00:49:21 +02:00
|
|
|
CPROCESS_wait_for(THIS, (int)(VARGOPT(timeout, 0.0) * 1000));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2013-10-13 19:55:35 +02:00
|
|
|
BEGIN_PROPERTY(Process_Ignore)
|
2013-08-12 22:48:45 +02:00
|
|
|
|
2013-10-13 19:55:35 +02:00
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnBoolean(THIS->ignore);
|
|
|
|
else
|
2013-08-12 22:48:45 +02:00
|
|
|
{
|
2013-10-13 19:55:35 +02:00
|
|
|
bool ignore = VPROP(GB_BOOLEAN);
|
|
|
|
if (THIS->ignore != ignore)
|
|
|
|
{
|
|
|
|
THIS->ignore = ignore;
|
|
|
|
if (ignore)
|
|
|
|
{
|
|
|
|
_ignore_process++;
|
|
|
|
if (_running_process <= _ignore_process)
|
|
|
|
exit_child();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ignore_process--;
|
|
|
|
if (_running_process > _ignore_process)
|
|
|
|
init_child();
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 22:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2016-08-10 20:37:25 +02:00
|
|
|
BEGIN_METHOD_VOID(Process_CloseInput)
|
|
|
|
|
|
|
|
close_fd(&THIS->in);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#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),
|
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
GB_STATIC_PROPERTY_READ("LastState", "i", Process_LastState),
|
|
|
|
GB_STATIC_PROPERTY_READ("LastValue", "i", Process_LastValue),
|
2014-09-15 22:05:43 +02:00
|
|
|
|
2012-01-13 09:04:11 +01:00
|
|
|
GB_PROPERTY_READ("Id", "i", Process_Id),
|
|
|
|
GB_PROPERTY_READ("Handle", "i", Process_Id),
|
|
|
|
GB_PROPERTY_READ("State", "i", Process_State),
|
|
|
|
GB_PROPERTY_READ("Value", "i", Process_Value),
|
|
|
|
|
|
|
|
GB_STATIC_METHOD("_exit", NULL, Process_exit, NULL),
|
|
|
|
GB_METHOD("_new", NULL, Process_new, NULL),
|
|
|
|
GB_METHOD("_free", NULL, Process_free, NULL),
|
|
|
|
|
|
|
|
GB_METHOD("Kill", NULL, Process_Kill, NULL),
|
|
|
|
GB_METHOD("Signal", NULL, Process_Signal, NULL),
|
2012-08-31 00:49:21 +02:00
|
|
|
GB_METHOD("Wait", NULL, Process_Wait, "[(Timeout)f]"),
|
2016-08-10 20:37:25 +02:00
|
|
|
|
|
|
|
GB_METHOD("CloseInput", NULL, Process_CloseInput, NULL),
|
2013-10-13 19:55:35 +02:00
|
|
|
|
|
|
|
GB_PROPERTY("Ignore", "b", Process_Ignore),
|
2017-08-23 18:10:58 +02:00
|
|
|
|
2017-06-10 01:00:45 +02:00
|
|
|
/*GB_METHOD("Exec", NULL, Process_Exec, "(Command)String[];[(Mode)i(Environment)String[];]"),
|
|
|
|
GB_METHOD("Shell", NULL, Process_Shell, "(Command)s;[(Mode)i(Environment)String[];]"),*/
|
|
|
|
|
2008-10-04 16:07:53 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|