2012-08-26 18:26:53 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx_c_task.c
|
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2012-08-26 18:26:53 +02: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, 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 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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_TASK_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
#include <errno.h>
|
2012-08-26 18:26:53 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-08-29 02:43:48 +02:00
|
|
|
#include <sys/time.h>
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_list.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_signal.h"
|
|
|
|
#include "gbx_event.h"
|
2021-06-05 03:11:54 +02:00
|
|
|
#include "gbx_component.h"
|
2012-08-26 18:26:53 +02:00
|
|
|
|
2014-08-01 22:29:01 +02:00
|
|
|
#include "gbx_c_file.h"
|
2012-08-26 18:26:53 +02:00
|
|
|
#include "gbx_c_task.h"
|
|
|
|
|
2013-05-30 23:16:09 +02:00
|
|
|
//#define DEBUG_ME 1
|
2014-08-28 15:50:09 +02:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#define FIONREAD TIOCINQ
|
|
|
|
#endif
|
2013-05-30 23:16:09 +02:00
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
DECLARE_EVENT(EVENT_Read);
|
|
|
|
DECLARE_EVENT(EVENT_Error);
|
|
|
|
DECLARE_EVENT(EVENT_Kill);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CHILD_OK = 0,
|
|
|
|
CHILD_ERROR = 1,
|
|
|
|
CHILD_STDOUT = 2,
|
|
|
|
CHILD_STDERR = 3,
|
|
|
|
CHILD_RETURN = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
static SIGNAL_CALLBACK *_signal_handler = NULL;
|
|
|
|
static CTASK *_task_list = NULL;
|
|
|
|
static int _task_count = 0;
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2013-09-29 22:54:47 +02:00
|
|
|
static void cleanup_task(CTASK *_object);
|
2012-08-26 18:26:53 +02:00
|
|
|
static void stop_task(CTASK *_object);
|
2012-08-29 02:43:48 +02:00
|
|
|
static void close_fd(int *pfd);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
static void has_forked(void)
|
|
|
|
{
|
2012-08-29 02:43:48 +02:00
|
|
|
CTASK *task;
|
|
|
|
pid_t pid = getpid();
|
2014-08-01 22:29:01 +02:00
|
|
|
STREAM *stream;
|
2012-08-29 02:43:48 +02:00
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
FILE_init();
|
|
|
|
EXEC_debug = FALSE;
|
|
|
|
EXEC_task = TRUE;
|
2014-08-22 21:31:51 +02:00
|
|
|
if (EXEC_profile)
|
|
|
|
DEBUG.Profile.Cancel();
|
|
|
|
EXEC_profile = FALSE;
|
|
|
|
EXEC_profile_instr = FALSE;
|
|
|
|
|
2012-08-31 21:56:41 +02:00
|
|
|
EXEC_Hook.loop = NULL;
|
|
|
|
EXEC_Hook.wait = NULL;
|
|
|
|
EXEC_Hook.timer = NULL;
|
|
|
|
EXEC_Hook.watch = NULL;
|
|
|
|
EXEC_Hook.post = NULL;
|
|
|
|
//EXEC_Hook.quit = NULL;
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
stream = CSTREAM_TO_STREAM(CFILE_out);
|
2014-08-01 22:29:01 +02:00
|
|
|
|
|
|
|
stream->common.eol = 0;
|
|
|
|
STREAM_blocking(stream, TRUE);
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
SIGNAL_has_forked();
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
task = _task_list;
|
|
|
|
while (task)
|
|
|
|
{
|
|
|
|
if (task->pid != pid)
|
|
|
|
{
|
|
|
|
close_fd(&task->fd_out);
|
|
|
|
close_fd(&task->fd_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
task = task->list.next;
|
|
|
|
}
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void callback_child(int signum, intptr_t data)
|
|
|
|
{
|
|
|
|
CTASK *_object, *next;
|
|
|
|
int status;
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, ">> callback_child\n");
|
|
|
|
#endif
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
_object = _task_list;
|
|
|
|
while (_object)
|
|
|
|
{
|
|
|
|
next = THIS->list.next;
|
|
|
|
if (wait4(THIS->pid, &status, WNOHANG, NULL) == THIS->pid)
|
|
|
|
{
|
|
|
|
THIS->status = status;
|
|
|
|
stop_task(THIS);
|
|
|
|
}
|
|
|
|
_object = next;
|
|
|
|
}
|
2017-01-11 17:57:44 +01:00
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "<< callback_child\n");
|
|
|
|
#endif
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_readable(int fd)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (ioctl(fd, FIONREAD, &len) < 0 || len <= 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
static bool callback_read(int fd, int type, CTASK *_object)
|
2012-08-26 18:26:53 +02:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *data;
|
|
|
|
char *p;
|
|
|
|
int n;
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
//fprintf(stderr, "callback_read: %d %p\n", fd, THIS);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
len = get_readable(fd);
|
2012-08-29 02:43:48 +02:00
|
|
|
if (len == 0)
|
|
|
|
return TRUE;
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
data = STRING_new(NULL, len);
|
|
|
|
p = data;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
n = read(fd, p, len);
|
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_Raise(THIS, EVENT_Read, 1, GB_T_STRING, data, STRING_length(data) - len);
|
|
|
|
|
|
|
|
STRING_free(&data);
|
2012-08-29 02:43:48 +02:00
|
|
|
return FALSE;
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int callback_error(int fd, int type, CTASK *_object)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
//fprintf(stderr, "callback_error: %d %p\n", fd, THIS);
|
|
|
|
|
|
|
|
n = read(fd, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
GB_Raise(THIS, EVENT_Error, 1, GB_T_STRING, buffer, n);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool create_return_directory(void)
|
|
|
|
{
|
|
|
|
static bool mkdir_done = FALSE;
|
|
|
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
|
|
|
if (mkdir_done)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sprintf(buf, RETURN_DIR_PATTERN, getuid(), getpid());
|
|
|
|
|
|
|
|
if (mkdir(buf, S_IRWXU) != 0)
|
|
|
|
{
|
|
|
|
GB_Error("Cannot create task return directory");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir_done = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_task(void)
|
|
|
|
{
|
|
|
|
_task_count++;
|
2013-09-29 22:54:47 +02:00
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
if (_task_count > 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_signal_handler = SIGNAL_register(SIGCHLD, callback_child, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_task(void)
|
|
|
|
{
|
|
|
|
_task_count--;
|
2013-09-29 22:54:47 +02:00
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
if (_task_count > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SIGNAL_unregister(SIGCHLD, _signal_handler);
|
|
|
|
_signal_handler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prepare_task(CTASK *_object)
|
|
|
|
{
|
|
|
|
THIS->fd_out = -1;
|
|
|
|
THIS->fd_err = -1;
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:56:13 +01:00
|
|
|
static void exit_child(int ret)
|
|
|
|
{
|
2018-01-28 17:57:13 +01:00
|
|
|
FILE_exit();
|
|
|
|
_exit(ret);
|
2018-01-19 01:56:13 +01:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
static bool start_task(CTASK *_object)
|
|
|
|
{
|
|
|
|
const char *err = NULL;
|
|
|
|
pid_t pid;
|
|
|
|
sigset_t sig, old;
|
|
|
|
GB_FUNCTION func;
|
|
|
|
int fd_out[2], fd_err[2];
|
|
|
|
bool has_read, has_error;
|
|
|
|
GB_VALUE *ret;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
FILE *f;
|
2012-08-29 02:43:48 +02:00
|
|
|
|
|
|
|
if (EXEC_task)
|
|
|
|
return TRUE;
|
2013-09-29 22:54:47 +02:00
|
|
|
|
|
|
|
if (THIS->stopped)
|
|
|
|
{
|
|
|
|
cleanup_task(THIS);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-08-29 02:43:48 +02:00
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
init_task();
|
|
|
|
|
|
|
|
LIST_insert(&_task_list, THIS, &THIS->list);
|
|
|
|
|
|
|
|
// Create pipes
|
|
|
|
|
|
|
|
has_read = GB_CanRaise(THIS, EVENT_Read);
|
|
|
|
has_error = GB_CanRaise(THIS, EVENT_Error);
|
|
|
|
|
|
|
|
if (has_read && pipe(fd_out) != 0)
|
|
|
|
goto __ERROR;
|
|
|
|
|
|
|
|
if (has_error && pipe(fd_err) != 0)
|
|
|
|
goto __ERROR;
|
|
|
|
|
2021-06-05 03:11:54 +02:00
|
|
|
COMPONENT_before_fork();
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
// Block SIGCHLD
|
|
|
|
|
|
|
|
sigemptyset(&sig);
|
|
|
|
|
|
|
|
sigaddset(&sig, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &sig, &old);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == (-1))
|
|
|
|
{
|
|
|
|
stop_task(THIS);
|
2017-01-11 17:57:44 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2012-08-26 18:26:53 +02:00
|
|
|
goto __ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid)
|
|
|
|
{
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "start_task: %p %d\n", THIS, pid);
|
|
|
|
#endif
|
2012-08-26 18:26:53 +02:00
|
|
|
THIS->pid = pid;
|
|
|
|
|
|
|
|
if (has_read)
|
|
|
|
{
|
|
|
|
close(fd_out[1]);
|
|
|
|
THIS->fd_out = fd_out[0];
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
GB_Watch(THIS->fd_out, GB_WATCH_READ, (void *)callback_read, (intptr_t)THIS);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_error)
|
|
|
|
{
|
|
|
|
close(fd_err[1]);
|
|
|
|
THIS->fd_err = fd_err[0];
|
|
|
|
|
|
|
|
fcntl(THIS->fd_err, F_SETFL, fcntl(THIS->fd_err, F_GETFL) | O_NONBLOCK);
|
|
|
|
GB_Watch(THIS->fd_err, GB_WATCH_READ, (void *)callback_error, (intptr_t)THIS);
|
|
|
|
}
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
else // child task
|
|
|
|
{
|
|
|
|
THIS->child = TRUE;
|
|
|
|
THIS->pid = getpid();
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
if (has_read)
|
|
|
|
{
|
|
|
|
close(fd_out[0]);
|
|
|
|
|
|
|
|
if (dup2(fd_out[1], STDOUT_FILENO) == -1)
|
2018-01-19 01:56:13 +01:00
|
|
|
exit_child(CHILD_STDOUT);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
setlinebuf(stdout);
|
|
|
|
}
|
2012-08-31 00:13:25 +02:00
|
|
|
else
|
|
|
|
close(CHILD_STDOUT);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
if (has_error)
|
|
|
|
{
|
|
|
|
close(fd_err[0]);
|
|
|
|
|
|
|
|
if (dup2(fd_err[1], STDERR_FILENO) == -1)
|
2018-01-19 01:56:13 +01:00
|
|
|
exit_child(CHILD_STDERR);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
setlinebuf(stderr);
|
|
|
|
}
|
2012-08-31 00:13:25 +02:00
|
|
|
else
|
|
|
|
close(CHILD_STDERR);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
has_forked(); // After the redirection
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
GB_GetFunction(&func, THIS, "Main", "", NULL);
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
ret = GB_Call(&func, 0, FALSE);
|
|
|
|
if (ret->type != GB_T_VOID)
|
|
|
|
{
|
|
|
|
sprintf(buf, RETURN_FILE_PATTERN, getuid(), getppid(), getpid());
|
2013-05-30 23:16:09 +02:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "serialize to: %s\n", buf);
|
|
|
|
#endif
|
2012-08-26 18:26:53 +02:00
|
|
|
GB_ReturnConvVariant();
|
|
|
|
if (GB_Serialize(buf, ret))
|
|
|
|
{
|
2013-05-30 23:16:09 +02:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "gb.task: serialization has failed\n");
|
|
|
|
#endif
|
2018-01-19 01:56:13 +01:00
|
|
|
exit_child(CHILD_RETURN);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (ERROR_current->info.code && ERROR_current->info.code != E_ABORT)
|
|
|
|
{
|
|
|
|
sprintf(buf, RETURN_FILE_PATTERN, getuid(), getppid(), getpid());
|
|
|
|
f = fopen(buf, "w+");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
ERROR_print_at(f, FALSE, FALSE);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:56:13 +01:00
|
|
|
exit_child(CHILD_ERROR);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2018-01-19 01:56:13 +01:00
|
|
|
exit_child(CHILD_OK);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
__ERROR:
|
|
|
|
|
|
|
|
// TODO: as the routine is posted, nobody will see the error!
|
|
|
|
if (!err)
|
|
|
|
err = strerror(errno);
|
|
|
|
fprintf(stderr, "gb.task: cannot run task: %s\n", err);
|
|
|
|
GB_Error("Cannot run task: &1", err);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_fd(int *pfd)
|
|
|
|
{
|
|
|
|
int fd = *pfd;
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
GB_Watch(fd, GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(fd);
|
|
|
|
*pfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 16:43:13 +01:00
|
|
|
static bool get_return_value(CTASK *_object, bool cleanup)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
GB_VALUE value;
|
|
|
|
bool fail = FALSE;
|
|
|
|
struct stat info;
|
|
|
|
char *err = NULL;
|
|
|
|
int fd;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
sprintf(path, RETURN_FILE_PATTERN, getuid(), getpid(), THIS->pid);
|
|
|
|
|
|
|
|
if (!cleanup)
|
|
|
|
{
|
|
|
|
switch (THIS->status)
|
|
|
|
{
|
|
|
|
case CHILD_OK:
|
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr,"unserialize from: %s\n", path);
|
|
|
|
#endif
|
|
|
|
if (!THIS->got_value)
|
|
|
|
{
|
2018-03-14 16:59:57 +01:00
|
|
|
if (stat(path, &info))
|
|
|
|
{
|
|
|
|
GB_Error((char *)E_NRETURN);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-01-20 16:43:13 +01:00
|
|
|
fail = GB_UnSerialize(path, &value);
|
|
|
|
if (!fail)
|
|
|
|
GB_StoreVariant(&value._variant, &THIS->ret);
|
|
|
|
THIS->got_value = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHILD_ERROR:
|
|
|
|
|
|
|
|
if (stat(path, &info))
|
|
|
|
{
|
|
|
|
fail = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = STRING_new_temp(NULL, info.st_size);
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fail = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
n = read(fd, err, info.st_size);
|
|
|
|
if (n == info.st_size || errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (n == info.st_size)
|
|
|
|
GB_Error("Task has failed: &1", err);
|
|
|
|
else
|
|
|
|
fail = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
GB_Error("Unable to get task error");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(path);
|
|
|
|
|
|
|
|
return fail;
|
|
|
|
}
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
static void cleanup_task(CTASK *_object)
|
|
|
|
{
|
|
|
|
//printf("cleanup task %p\n", THIS); fflush(stdout);
|
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(_object);
|
2012-08-31 00:13:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
static void stop_task(CTASK *_object)
|
|
|
|
{
|
|
|
|
int len;
|
2012-08-31 00:13:25 +02:00
|
|
|
GB_RAISE_HANDLER handler;
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "stop_task: %p %d\n", THIS, THIS->pid);
|
|
|
|
#endif
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
THIS->stopped = TRUE;
|
|
|
|
|
2013-09-29 22:54:47 +02:00
|
|
|
// Remove task temporary files
|
|
|
|
FILE_remove_temp_file_pid(THIS->pid);
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
// Flush standard error
|
|
|
|
if (THIS->fd_err >= 0)
|
|
|
|
while (callback_error(THIS->fd_err, 0, THIS) == 0);
|
|
|
|
|
|
|
|
// Flush standard output
|
|
|
|
if (THIS->fd_out >= 0)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
len = get_readable(THIS->fd_out);
|
|
|
|
if (len <= 0)
|
|
|
|
break;
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
if (callback_read(THIS->fd_out, 0, THIS))
|
2012-08-26 18:26:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close_fd(&THIS->fd_out);
|
|
|
|
close_fd(&THIS->fd_err);
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
LIST_remove(&_task_list, THIS, &THIS->list);
|
2012-08-26 18:26:53 +02:00
|
|
|
exit_task();
|
2012-08-31 00:13:25 +02:00
|
|
|
|
|
|
|
//printf("Kill event...\n"); fflush(stdout);
|
|
|
|
|
|
|
|
if (GB_CanRaise(THIS, EVENT_Kill))
|
|
|
|
{
|
|
|
|
handler.callback = (GB_CALLBACK)cleanup_task;
|
|
|
|
handler.data = (intptr_t)THIS;
|
|
|
|
|
|
|
|
GB_RaiseBegin(&handler);
|
|
|
|
GB_Raise(THIS, EVENT_Kill, 0);
|
|
|
|
GB_RaiseEnd(&handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_task(THIS);
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Task_new)
|
|
|
|
|
|
|
|
GB_FUNCTION func;
|
|
|
|
|
2015-01-23 01:52:18 +01:00
|
|
|
THIS->ret.type = GB_T_NULL;
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
if (EXEC_task)
|
|
|
|
{
|
|
|
|
GB_Error("A task cannot create other tasks");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
if (create_return_directory())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (GB_GetFunction(&func, THIS, "Main", "", NULL))
|
|
|
|
return;
|
|
|
|
|
2012-08-29 02:43:48 +02:00
|
|
|
if (_task_count > MAX_TASK)
|
|
|
|
{
|
|
|
|
GB_Error("Too many tasks");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
prepare_task(THIS);
|
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(THIS);
|
2012-08-26 18:26:53 +02:00
|
|
|
EVENT_post((GB_CALLBACK)start_task, (intptr_t)THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2015-01-23 01:52:18 +01:00
|
|
|
BEGIN_METHOD_VOID(Task_free)
|
|
|
|
|
2018-01-20 16:43:13 +01:00
|
|
|
get_return_value(THIS, TRUE);
|
2015-01-23 01:52:18 +01:00
|
|
|
GB_StoreVariant(NULL, &THIS->ret);
|
|
|
|
|
|
|
|
END_METHOD
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
BEGIN_PROPERTY(Task_Handle)
|
|
|
|
|
|
|
|
GB_ReturnInteger(THIS->pid);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Task_Stop)
|
|
|
|
|
2013-09-29 22:54:47 +02:00
|
|
|
if (THIS->pid > 0)
|
|
|
|
kill(THIS->pid, SIGKILL);
|
|
|
|
else
|
|
|
|
THIS->stopped = TRUE;
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-08-31 00:49:21 +02:00
|
|
|
static void error_Task_Wait(CTASK *task)
|
|
|
|
{
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(task);
|
2012-08-31 00:49:21 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:26:53 +02:00
|
|
|
BEGIN_METHOD_VOID(Task_Wait)
|
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(THIS);
|
2012-08-31 00:49:21 +02:00
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
//printf("Task_Wait: %p\n", THIS); fflush(stdout);
|
2012-08-31 00:49:21 +02:00
|
|
|
|
|
|
|
ON_ERROR_1(error_Task_Wait, THIS)
|
2012-08-26 18:26:53 +02:00
|
|
|
{
|
2012-08-31 00:49:21 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
//printf("GB_Wait\n"); fflush(stdout);
|
|
|
|
GB_Wait(0);
|
|
|
|
//printf("stopped = %d\n", THIS->stopped); fflush(stdout);
|
|
|
|
if (THIS->stopped)
|
|
|
|
break;
|
|
|
|
//printf("sleep\n"); fflush(stdout);
|
|
|
|
sleep(10);
|
|
|
|
}
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
2012-08-31 00:49:21 +02:00
|
|
|
END_ERROR
|
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(_object);
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(Task_Value)
|
|
|
|
|
|
|
|
if (!THIS->child && THIS->stopped)
|
|
|
|
{
|
|
|
|
if (WIFEXITED(THIS->status))
|
|
|
|
{
|
|
|
|
switch (WEXITSTATUS(THIS->status))
|
|
|
|
{
|
|
|
|
case CHILD_OK:
|
2015-01-23 01:52:18 +01:00
|
|
|
|
2018-01-20 16:43:13 +01:00
|
|
|
if (get_return_value(THIS, FALSE))
|
|
|
|
break;
|
2015-01-23 01:52:18 +01:00
|
|
|
GB_ReturnVariant(&THIS->ret);
|
|
|
|
return;
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
case CHILD_STDOUT:
|
|
|
|
|
|
|
|
GB_Error("Unable to redirect task standard output");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CHILD_STDERR:
|
|
|
|
|
|
|
|
GB_Error("Unable to redirect task error output");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CHILD_RETURN:
|
|
|
|
|
|
|
|
GB_Error("Unable to serialize task return value");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CHILD_ERROR:
|
|
|
|
|
2018-01-20 16:43:13 +01:00
|
|
|
get_return_value(THIS, FALSE);
|
|
|
|
break;
|
2012-08-26 18:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(THIS->status))
|
|
|
|
{
|
|
|
|
GB_Error("Task has aborted: &1", strsignal(WTERMSIG(THIS->status)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnNull();
|
|
|
|
GB_ReturnConvVariant();
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
BEGIN_PROPERTY(Task_Running)
|
|
|
|
|
|
|
|
GB_ReturnBoolean(!THIS->stopped);
|
|
|
|
|
|
|
|
END_PROPERTY
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GB_DESC TaskDesc[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Task", sizeof(CTASK)), GB_NOT_CREATABLE(),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, Task_new, NULL),
|
2015-01-23 01:52:18 +01:00
|
|
|
GB_METHOD("_free", NULL, Task_free, NULL),
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
GB_PROPERTY_READ("Handle", "i", Task_Handle),
|
|
|
|
GB_PROPERTY_READ("Value", "v", Task_Value),
|
2012-08-31 00:13:25 +02:00
|
|
|
GB_PROPERTY_READ("Running", "b", Task_Running),
|
2012-08-26 18:26:53 +02:00
|
|
|
|
|
|
|
GB_METHOD("Stop", NULL, Task_Stop, NULL),
|
2021-03-09 23:01:22 +01:00
|
|
|
GB_METHOD("Kill", NULL, Task_Stop, NULL),
|
2012-08-26 18:26:53 +02:00
|
|
|
GB_METHOD("Wait", NULL, Task_Wait, NULL),
|
|
|
|
|
|
|
|
GB_EVENT("Read", NULL, "(Data)s", &EVENT_Read),
|
|
|
|
GB_EVENT("Error", NULL, "(Data)s", &EVENT_Error),
|
|
|
|
GB_EVENT("Kill", NULL, NULL, &EVENT_Kill),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|