2010-07-13 01:09:57 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
csignal.c
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +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.
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
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.
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
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.
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CSIGNAL_C
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "csignal.h"
|
|
|
|
|
2010-12-08 18:15:18 +01:00
|
|
|
#ifndef SIGPOLL
|
|
|
|
#define SIGPOLL SIGIO
|
|
|
|
#endif
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
// The "-1" signal is used for ignoring signal numbers
|
|
|
|
|
2010-12-08 18:15:18 +01:00
|
|
|
#ifndef SIGPWR
|
|
|
|
#define SIGPWR -1
|
|
|
|
#endif
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
#ifdef OS_CYGWIN
|
|
|
|
#define SIGIOT -1
|
|
|
|
#endif
|
2011-12-18 04:03:07 +01:00
|
|
|
|
2010-12-08 18:15:18 +01:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
#define NUM_SIGNALS 32
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
enum {
|
|
|
|
SH_DEFAULT = 0,
|
|
|
|
SH_IGNORE = 1,
|
|
|
|
SH_CATCH = 2
|
|
|
|
};
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
typedef
|
|
|
|
struct SIGNAL_HANDLER {
|
2012-08-14 11:53:03 +02:00
|
|
|
GB_SIGNAL_CALLBACK *handler;
|
2010-07-13 01:09:57 +02:00
|
|
|
struct sigaction action;
|
2012-08-14 11:53:03 +02:00
|
|
|
char state;
|
2010-07-13 01:09:57 +02:00
|
|
|
}
|
|
|
|
SIGNAL_HANDLER;
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
static SIGNAL_HANDLER _signals[NUM_SIGNALS] = { { 0 } };
|
2010-12-08 18:15:18 +01:00
|
|
|
static int _signal = -1;
|
2010-07-13 01:09:57 +02:00
|
|
|
static GB_FUNCTION _application_signal_func;
|
2012-08-14 11:53:03 +02:00
|
|
|
static bool _init_signal = FALSE;
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
static void init_signal(void)
|
|
|
|
{
|
2010-10-15 02:23:11 +02:00
|
|
|
if (GB.GetFunction(&_application_signal_func, (void *)GB.Application.StartupClass(), "Application_Signal", "i", ""))
|
2010-07-13 01:09:57 +02:00
|
|
|
{
|
|
|
|
GB.Error("No Application_Signal event handler defined in startup class");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_init_signal = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
static void catch_signal(int num, intptr_t data)
|
2010-07-13 01:09:57 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
GB.Push(1, GB_T_INTEGER, num);
|
|
|
|
GB.Call(&_application_signal_func, 1, TRUE);
|
2010-07-13 01:09:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
static void handle_signal(int num, char state)
|
2010-07-13 01:09:57 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
struct sigaction action;
|
2010-07-13 01:09:57 +02:00
|
|
|
SIGNAL_HANDLER *sh;
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
if (num < 0)
|
2010-07-13 01:09:57 +02:00
|
|
|
return;
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
sh = &_signals[num];
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
if (sh->state == state)
|
2010-07-13 01:09:57 +02:00
|
|
|
return;
|
2012-05-08 19:15:54 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
if (sh->state == SH_IGNORE)
|
2012-05-08 19:15:54 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
if (sigaction(num, &sh->action, NULL))
|
|
|
|
{
|
|
|
|
GB.Error("Unable to reset signal handler");
|
|
|
|
return;
|
|
|
|
}
|
2012-05-08 19:15:54 +02:00
|
|
|
}
|
2012-08-14 11:53:03 +02:00
|
|
|
else if (sh->state == SH_CATCH)
|
2012-05-08 19:15:54 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
if (sh->handler)
|
2012-05-08 19:15:54 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
GB.Signal.Unregister(num, sh->handler);
|
|
|
|
sh->handler = NULL;
|
2012-05-08 19:15:54 +02:00
|
|
|
}
|
2012-08-14 11:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == SH_IGNORE)
|
|
|
|
{
|
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
|
|
|
|
if (sigaction(num, &action, &sh->action))
|
2012-05-08 19:15:54 +02:00
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
GB.Error("Unable to modify signal handler");
|
|
|
|
return;
|
2012-05-08 19:15:54 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 11:53:03 +02:00
|
|
|
else if (state == SH_CATCH)
|
|
|
|
{
|
|
|
|
if (!_init_signal)
|
|
|
|
init_signal();
|
|
|
|
|
|
|
|
sh->handler = GB.Signal.Register(num, catch_signal, 0);
|
|
|
|
}
|
2012-05-08 19:15:54 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
sh->state = state;
|
2010-07-13 01:09:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
static void exit_signal(void)
|
|
|
|
{
|
|
|
|
int i;
|
2010-12-08 18:15:18 +01:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
for (i = 0; i < NUM_SIGNALS; i++)
|
|
|
|
handle_signal(i, SH_DEFAULT);
|
|
|
|
}
|
2010-12-08 18:15:18 +01:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Signal_Reset)
|
|
|
|
|
|
|
|
handle_signal(_signal, SH_DEFAULT);
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Signal_Ignore)
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
handle_signal(_signal, SH_IGNORE);
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Signal_Catch)
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
handle_signal(_signal, SH_CATCH);
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD(Signal_get, GB_INTEGER num)
|
|
|
|
|
|
|
|
int num = VARG(num);
|
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
if (num < -1 || num >= NUM_SIGNALS)
|
2010-07-13 01:09:57 +02:00
|
|
|
{
|
|
|
|
GB.Error("Bad signal number");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_signal = num;
|
|
|
|
RETURN_SELF();
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Signal_exit)
|
|
|
|
|
|
|
|
exit_signal();
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CSignalHandlerDesc[] =
|
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
GB_DECLARE_VIRTUAL(".SignalHandler"),
|
|
|
|
|
|
|
|
GB_STATIC_METHOD("Reset", NULL, Signal_Reset, NULL),
|
|
|
|
GB_STATIC_METHOD("Ignore", NULL, Signal_Ignore, NULL),
|
|
|
|
GB_STATIC_METHOD("Catch", NULL, Signal_Catch, NULL),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
2010-07-13 01:09:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
GB_DESC CSignalDesc[] =
|
|
|
|
{
|
2012-08-14 11:53:03 +02:00
|
|
|
GB_DECLARE_VIRTUAL("Signal"),
|
2010-07-13 01:09:57 +02:00
|
|
|
|
|
|
|
GB_CONSTANT("SIGHUP", "i", SIGHUP),
|
|
|
|
GB_CONSTANT("SIGINT", "i", SIGINT),
|
|
|
|
GB_CONSTANT("SIGQUIT", "i", SIGQUIT),
|
|
|
|
GB_CONSTANT("SIGILL", "i", SIGILL),
|
|
|
|
GB_CONSTANT("SIGTRAP", "i", SIGTRAP),
|
|
|
|
GB_CONSTANT("SIGABRT", "i", SIGABRT),
|
|
|
|
GB_CONSTANT("SIGIOT", "i", SIGIOT),
|
|
|
|
GB_CONSTANT("SIGBUS", "i", SIGBUS),
|
|
|
|
GB_CONSTANT("SIGFPE", "i", SIGFPE),
|
|
|
|
GB_CONSTANT("SIGKILL", "i", SIGKILL),
|
|
|
|
GB_CONSTANT("SIGUSR1", "i", SIGUSR1),
|
|
|
|
GB_CONSTANT("SIGSEGV", "i", SIGSEGV),
|
|
|
|
GB_CONSTANT("SIGUSR2", "i", SIGUSR2),
|
|
|
|
GB_CONSTANT("SIGPIPE", "i", SIGPIPE),
|
|
|
|
GB_CONSTANT("SIGALRM", "i", SIGALRM),
|
|
|
|
GB_CONSTANT("SIGTERM", "i", SIGTERM),
|
2010-12-08 18:15:18 +01:00
|
|
|
//GB_CONSTANT("SIGSTKFLT", "i", SIGSTKFLT),
|
|
|
|
//GB_CONSTANT("SIGCLD", "i", SIGCLD),
|
2010-07-13 01:09:57 +02:00
|
|
|
GB_CONSTANT("SIGCHLD", "i", SIGCHLD),
|
|
|
|
GB_CONSTANT("SIGCONT", "i", SIGCONT),
|
|
|
|
GB_CONSTANT("SIGSTOP", "i", SIGSTOP),
|
|
|
|
GB_CONSTANT("SIGTSTP", "i", SIGTSTP),
|
|
|
|
GB_CONSTANT("SIGTTIN", "i", SIGTTIN),
|
|
|
|
GB_CONSTANT("SIGTTOU", "i", SIGTTOU),
|
|
|
|
GB_CONSTANT("SIGURG", "i", SIGURG),
|
|
|
|
GB_CONSTANT("SIGXCPU", "i", SIGXCPU),
|
|
|
|
GB_CONSTANT("SIGXFSZ", "i", SIGXFSZ),
|
|
|
|
GB_CONSTANT("SIGVTALRM", "i", SIGVTALRM),
|
|
|
|
GB_CONSTANT("SIGPROF", "i", SIGPROF),
|
|
|
|
GB_CONSTANT("SIGWINCH", "i", SIGWINCH),
|
|
|
|
GB_CONSTANT("SIGPOLL", "i", SIGPOLL),
|
|
|
|
GB_CONSTANT("SIGIO", "i", SIGIO),
|
|
|
|
GB_CONSTANT("SIGPWR", "i", SIGPWR),
|
|
|
|
GB_CONSTANT("SIGSYS", "i", SIGSYS),
|
|
|
|
|
|
|
|
GB_STATIC_METHOD("_exit", NULL, Signal_exit, NULL),
|
2012-08-14 11:53:03 +02:00
|
|
|
|
|
|
|
GB_STATIC_METHOD("_get", ".SignalHandler", Signal_get, "(Signal)i"),
|
2010-07-13 01:09:57 +02:00
|
|
|
|
2012-08-14 11:53:03 +02:00
|
|
|
GB_END_DECLARE
|
2010-07-13 01:09:57 +02:00
|
|
|
};
|
|
|
|
|