2011-07-25 02:50:11 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx_signal.c
|
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2011-07-25 02:50:11 +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,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2011-07-25 02:50:11 +02:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_SIGNAL_C
|
|
|
|
|
2018-06-09 22:42:35 +02:00
|
|
|
#include "gb_alloc.h"
|
2011-07-25 02:50:11 +02:00
|
|
|
#include "gb_error.h"
|
2012-05-08 17:32:33 +02:00
|
|
|
#include "gb_array.h"
|
|
|
|
#include "gbx_api.h"
|
2011-07-25 02:50:11 +02:00
|
|
|
#include "gbx_signal.h"
|
|
|
|
|
2017-08-18 13:19:10 +02:00
|
|
|
//#define DEBUG_ME 1
|
2012-05-08 19:15:54 +02:00
|
|
|
|
2021-01-21 01:49:38 +01:00
|
|
|
uint64_t SIGNAL_check_mask = 0;
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
static SIGNAL_HANDLER *_handlers = NULL;
|
2012-08-31 00:13:25 +02:00
|
|
|
static int _pipe[2] = { -1, -1 };
|
2012-07-30 13:46:12 +02:00
|
|
|
static volatile int _count = 0;
|
2012-08-31 00:13:25 +02:00
|
|
|
static int _raising_callback = 0;
|
2012-05-08 17:32:33 +02:00
|
|
|
|
2020-10-21 23:31:02 +02:00
|
|
|
enum { CB_NONE = 0, CB_HANDLER = 1, CB_ACTION = 2 };
|
|
|
|
|
|
|
|
static int get_callback(struct sigaction *action, void (**callback)())
|
|
|
|
{
|
|
|
|
if (action->sa_handler != SIG_DFL && action->sa_handler != SIG_IGN)
|
|
|
|
{
|
|
|
|
if (action->sa_flags & SA_SIGINFO)
|
|
|
|
{
|
|
|
|
*callback = (void *)action->sa_sigaction;
|
|
|
|
return CB_ACTION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*callback = (void *)action->sa_handler;
|
|
|
|
return CB_HANDLER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*callback = NULL;
|
|
|
|
return CB_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 02:50:11 +02:00
|
|
|
void SIGNAL_install(SIGNAL_HANDLER *handler, int signum, void (*callback)(int, siginfo_t *, void *))
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2012-05-08 17:32:33 +02:00
|
|
|
fprintf(stderr, "SIGNAL_install: %d %p\n", signum, callback);
|
2012-05-08 19:15:54 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
handler->signum = signum;
|
|
|
|
|
2011-07-25 02:50:11 +02:00
|
|
|
action.sa_flags = SA_SIGINFO;
|
2020-10-21 23:31:02 +02:00
|
|
|
// According to manpage, the emitting signal is blocked by default.
|
2011-07-25 02:50:11 +02:00
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_sigaction = callback;
|
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
if (sigaction(signum, NULL, &handler->old_action) != 0 || sigaction(signum, &action, NULL) != 0)
|
2011-07-25 02:50:11 +02:00
|
|
|
ERROR_panic("Cannot install signal handler: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
void SIGNAL_uninstall(SIGNAL_HANDLER *handler, int signum)
|
2011-07-25 02:50:11 +02:00
|
|
|
{
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2012-05-08 17:32:33 +02:00
|
|
|
fprintf(stderr, "SIGNAL_uninstall: %d\n", signum);
|
2012-05-08 19:15:54 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
if (sigaction(signum, &handler->old_action, NULL) != 0)
|
2011-07-25 02:50:11 +02:00
|
|
|
ERROR_panic("Cannot uninstall signal handler");
|
2012-05-16 20:05:57 +02:00
|
|
|
|
|
|
|
while (handler->callbacks)
|
|
|
|
SIGNAL_unregister(handler->signum, handler->callbacks);
|
2011-07-25 02:50:11 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
void SIGNAL_previous(SIGNAL_HANDLER *handler, int signum, siginfo_t *info, void *context)
|
2011-07-25 02:50:11 +02:00
|
|
|
{
|
2020-10-21 23:31:02 +02:00
|
|
|
void (*cb)();
|
|
|
|
|
|
|
|
switch (get_callback(&handler->old_action, &cb))
|
2011-07-25 02:50:11 +02:00
|
|
|
{
|
2020-10-21 23:31:02 +02:00
|
|
|
case CB_ACTION:
|
|
|
|
(*cb)(signum, info, context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_HANDLER:
|
|
|
|
(*cb)(signum);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
; /* do nothing */
|
2011-07-25 02:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
static SIGNAL_HANDLER *find_handler(int signum)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_count(_handlers); i++)
|
|
|
|
{
|
|
|
|
if (_handlers[i].signum == signum)
|
|
|
|
return &_handlers[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SIGNAL_HANDLER *add_handler(void)
|
|
|
|
{
|
|
|
|
if (!_handlers)
|
|
|
|
ARRAY_create_inc(&_handlers, 1);
|
|
|
|
|
|
|
|
return ARRAY_add_void(&_handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_signal(int signum, siginfo_t *info, void *context)
|
|
|
|
{
|
|
|
|
char buffer;
|
|
|
|
int save_errno;
|
2020-12-28 02:45:56 +01:00
|
|
|
static volatile int64_t lock = 0;
|
2020-10-21 23:31:02 +02:00
|
|
|
|
|
|
|
if (lock & (1 << signum))
|
|
|
|
return;
|
2012-05-08 17:32:33 +02:00
|
|
|
|
|
|
|
save_errno = errno;
|
2020-10-21 23:31:02 +02:00
|
|
|
lock |= 1 << signum;
|
2020-10-06 16:40:58 +02:00
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
char digit;
|
|
|
|
write(2, "[SIGNAL:", 8);
|
|
|
|
digit = '0' + (signum / 10);
|
|
|
|
write(2, &digit, 1);
|
|
|
|
digit = '0' + (signum % 10);
|
|
|
|
write(2, &digit, 1);
|
|
|
|
write(2, "]\n", 2);
|
|
|
|
#endif
|
2012-07-30 13:46:12 +02:00
|
|
|
|
|
|
|
if (_count)
|
2012-05-08 17:32:33 +02:00
|
|
|
{
|
2012-07-30 13:46:12 +02:00
|
|
|
buffer = signum;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (write(_pipe[1], &buffer, 1) == 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
2015-06-27 16:07:34 +02:00
|
|
|
{
|
2015-05-10 15:18:00 +02:00
|
|
|
ERROR_warning("cannot write signal #%d into signal pipe: %s", signum, strerror(errno));
|
2015-06-27 16:07:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-30 13:46:12 +02:00
|
|
|
}
|
2012-05-08 17:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SIGNAL_previous(find_handler(signum), signum, info, context);
|
|
|
|
|
|
|
|
errno = save_errno;
|
2020-10-21 23:31:02 +02:00
|
|
|
lock &= ~(1 << signum);
|
2012-05-08 17:32:33 +02:00
|
|
|
}
|
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
static bool _must_purge_callbacks = FALSE;
|
2012-05-19 04:55:05 +02:00
|
|
|
static int _purge_signum;
|
|
|
|
static SIGNAL_HANDLER *_purge_handler;
|
|
|
|
|
|
|
|
static void purge_callbacks(void)
|
|
|
|
{
|
|
|
|
SIGNAL_CALLBACK *cb, *next_cb;
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2012-08-31 00:13:25 +02:00
|
|
|
_raising_callback--;
|
|
|
|
if (_raising_callback)
|
|
|
|
return;
|
2012-05-19 04:55:05 +02:00
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, ">> purge_callbacks\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (_must_purge_callbacks)
|
2012-05-19 04:55:05 +02:00
|
|
|
{
|
2013-08-12 22:01:24 +02:00
|
|
|
_must_purge_callbacks = FALSE;
|
|
|
|
|
2012-05-19 04:55:05 +02:00
|
|
|
cb = _purge_handler->callbacks;
|
|
|
|
while (cb)
|
|
|
|
{
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, "purge_callbacks: cb = %p\n", cb);
|
|
|
|
#endif
|
2012-05-19 04:55:05 +02:00
|
|
|
next_cb = cb->next;
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2012-05-19 04:55:05 +02:00
|
|
|
if (!cb->callback)
|
|
|
|
SIGNAL_unregister(_purge_signum, cb);
|
|
|
|
|
|
|
|
cb = next_cb;
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, "<< purge_callbacks\n");
|
|
|
|
#endif
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
void SIGNAL_raise_callbacks(int fd, int type, void *data)
|
|
|
|
{
|
|
|
|
SIGNAL_HANDLER *handler;
|
2012-05-19 04:55:05 +02:00
|
|
|
SIGNAL_CALLBACK *cb;
|
2012-05-08 17:32:33 +02:00
|
|
|
char signum;
|
2017-01-11 17:57:44 +01:00
|
|
|
int ret;
|
2012-05-08 17:32:33 +02:00
|
|
|
|
|
|
|
/*old = signal(SIGCHLD, signal_child);*/
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: fd = %d blocking = %d\n", fd, (fcntl(fd, F_GETFL) & O_NONBLOCK) == 0);
|
|
|
|
#endif
|
2012-05-08 17:32:33 +02:00
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
for(;;)
|
2017-01-11 17:57:44 +01:00
|
|
|
{
|
2017-04-27 15:18:35 +02:00
|
|
|
ret = read(fd, &signum, 1);
|
|
|
|
if (ret != 1)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: read -> %d / errno = %d\n", ret, errno);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:46:30 +01:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: signum = %d\n", signum);
|
|
|
|
#endif
|
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
handler = find_handler(signum);
|
|
|
|
if (!handler)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: no handler\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2017-04-27 15:18:35 +02:00
|
|
|
fprintf(stderr, ">> SIGNAL_raise_callbacks (%d)\n", _raising_callback);
|
2017-01-11 17:57:44 +01:00
|
|
|
#endif
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
_raising_callback++;
|
|
|
|
_purge_signum = signum;
|
|
|
|
_purge_handler = handler;
|
|
|
|
|
|
|
|
ON_ERROR(purge_callbacks)
|
2012-05-19 04:55:05 +02:00
|
|
|
{
|
2017-04-27 15:18:35 +02:00
|
|
|
cb = handler->callbacks;
|
|
|
|
while (cb)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: cb = %p cb->callback = %p\n", cb, cb->callback);
|
|
|
|
#endif
|
|
|
|
if (cb->callback)
|
|
|
|
(*cb->callback)((int)signum, cb->data);
|
|
|
|
|
|
|
|
cb = cb->next;
|
|
|
|
}
|
2012-05-19 04:55:05 +02:00
|
|
|
}
|
2017-04-27 15:18:35 +02:00
|
|
|
END_ERROR
|
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_raise_callbacks: purge_callbacks\n");
|
|
|
|
#endif
|
|
|
|
purge_callbacks();
|
2013-08-12 22:01:24 +02:00
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "<< SIGNAL_raise_callbacks (%d)\n", _raising_callback);
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-08 17:32:33 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
static void create_pipe(void)
|
|
|
|
{
|
|
|
|
if (pipe(_pipe) != 0)
|
|
|
|
ERROR_panic("Cannot create signal handler pipes: %s", strerror(errno));
|
|
|
|
|
|
|
|
if (_pipe[0] == 0)
|
|
|
|
BREAKPOINT();
|
|
|
|
|
|
|
|
fcntl(_pipe[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(_pipe[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
// Allows to read the signal pipe without blocking
|
|
|
|
fcntl(_pipe[0], F_SETFL, fcntl(_pipe[0], F_GETFL) | O_NONBLOCK);
|
|
|
|
|
|
|
|
GB_Watch(_pipe[0], GB_WATCH_READ, (void *)SIGNAL_raise_callbacks, 0);
|
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "create_pipe: fd = %d\n", _pipe[0]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_pipe(void)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "delete_pipe: fd = %d\n", _pipe[0]);
|
|
|
|
#endif
|
|
|
|
GB_Watch(_pipe[0], GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(_pipe[0]);
|
|
|
|
close(_pipe[1]);
|
|
|
|
_pipe[0] = -1;
|
|
|
|
_pipe[1] = -1;
|
|
|
|
}
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
SIGNAL_CALLBACK *SIGNAL_register(int signum, void (*callback)(int, intptr_t), intptr_t data)
|
|
|
|
{
|
2012-05-16 20:05:57 +02:00
|
|
|
SIGNAL_HANDLER *handler;
|
2012-05-08 17:32:33 +02:00
|
|
|
SIGNAL_CALLBACK *cb;
|
|
|
|
|
|
|
|
if (!_count)
|
2017-01-11 17:57:44 +01:00
|
|
|
create_pipe();
|
2012-05-08 17:32:33 +02:00
|
|
|
|
|
|
|
_count++;
|
|
|
|
|
2012-05-16 20:05:57 +02:00
|
|
|
handler = find_handler(signum);
|
2020-10-06 16:40:58 +02:00
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "SIGNAL_register: find_handler(%d) -> %p\n", signum, handler);
|
|
|
|
#endif
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
if (!handler)
|
|
|
|
{
|
|
|
|
handler = add_handler();
|
|
|
|
SIGNAL_install(handler, signum, handle_signal);
|
|
|
|
}
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&cb, sizeof(SIGNAL_CALLBACK));
|
2012-05-08 17:32:33 +02:00
|
|
|
|
|
|
|
cb->prev = NULL;
|
|
|
|
cb->next = handler->callbacks;
|
|
|
|
cb->callback = callback;
|
|
|
|
cb->data = data;
|
2012-05-16 20:05:57 +02:00
|
|
|
|
2013-08-12 22:01:24 +02:00
|
|
|
if (cb->next)
|
|
|
|
cb->next->prev = cb;
|
2012-05-16 20:05:57 +02:00
|
|
|
handler->callbacks = cb;
|
2012-05-08 17:32:33 +02:00
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2012-05-16 23:42:28 +02:00
|
|
|
fprintf(stderr, "SIGNAL_register: %d -> %p (%p)\n", signum, cb, cb->callback);
|
2012-05-08 19:15:54 +02:00
|
|
|
#endif
|
2012-05-08 17:32:33 +02:00
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, "handler->callbacks %p:", handler);
|
|
|
|
SIGNAL_CALLBACK *save = cb;
|
|
|
|
cb = handler->callbacks;
|
|
|
|
while (cb)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " -> %p (%p)", cb, cb->callback);
|
|
|
|
cb = cb->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
cb = save;
|
|
|
|
#endif
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SIGNAL_unregister(int signum, SIGNAL_CALLBACK *cb)
|
|
|
|
{
|
|
|
|
SIGNAL_HANDLER *handler = find_handler(signum);
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
return;
|
|
|
|
|
2012-05-16 20:05:57 +02:00
|
|
|
if (_raising_callback)
|
|
|
|
{
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2012-05-16 23:42:28 +02:00
|
|
|
fprintf(stderr, "SIGNAL_unregister: disable %d %p (%p)\n", signum, cb, cb->callback);
|
|
|
|
#endif
|
2012-05-16 20:05:57 +02:00
|
|
|
cb->callback = NULL;
|
2012-05-19 04:55:05 +02:00
|
|
|
_must_purge_callbacks = TRUE;
|
2012-05-16 20:05:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2012-05-16 23:42:28 +02:00
|
|
|
fprintf(stderr, "SIGNAL_unregister: remove %d %p (%p)\n", signum, cb, cb->callback);
|
|
|
|
#endif
|
|
|
|
|
2012-05-08 17:32:33 +02:00
|
|
|
if (cb->prev)
|
|
|
|
cb->prev->next = cb->next;
|
|
|
|
|
|
|
|
if (cb->next)
|
|
|
|
cb->next->prev = cb->prev;
|
|
|
|
|
|
|
|
if (cb == handler->callbacks)
|
|
|
|
handler->callbacks = cb->next;
|
|
|
|
|
2013-08-12 22:01:24 +02:00
|
|
|
IFREE(cb);
|
2012-05-08 17:32:33 +02:00
|
|
|
|
|
|
|
_count--;
|
|
|
|
|
|
|
|
if (_count == 0)
|
2017-01-11 17:57:44 +01:00
|
|
|
delete_pipe();
|
2012-08-31 00:13:25 +02:00
|
|
|
|
2017-01-11 17:57:44 +01:00
|
|
|
#if DEBUG_ME
|
2013-08-12 22:01:24 +02:00
|
|
|
fprintf(stderr, "handler->callbacks %p:", handler);
|
|
|
|
cb = handler->callbacks;
|
|
|
|
while (cb)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " -> %p (%p)", cb, cb->callback);
|
|
|
|
cb = cb->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
2012-05-08 17:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SIGNAL_exit(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SIGNAL_HANDLER *handler;
|
|
|
|
|
|
|
|
if (_handlers)
|
|
|
|
{
|
2020-01-04 22:09:13 +01:00
|
|
|
_raising_callback = 0;
|
2012-05-08 17:32:33 +02:00
|
|
|
for (i = 0; i < ARRAY_count(_handlers); i++)
|
|
|
|
{
|
|
|
|
handler = &_handlers[i];
|
|
|
|
SIGNAL_uninstall(handler, handler->signum);
|
|
|
|
}
|
|
|
|
|
|
|
|
ARRAY_delete(&_handlers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SIGNAL_get_fd(void)
|
|
|
|
{
|
|
|
|
return _pipe[0];
|
|
|
|
}
|
2017-01-11 17:57:44 +01:00
|
|
|
|
|
|
|
void SIGNAL_has_forked(void)
|
|
|
|
{
|
|
|
|
if (!_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GB_Watch(_pipe[0], GB_WATCH_NONE, NULL, 0);
|
|
|
|
close(_pipe[0]);
|
|
|
|
close(_pipe[1]);
|
|
|
|
create_pipe();
|
|
|
|
}
|
2020-10-06 16:40:58 +02:00
|
|
|
|
2021-01-21 01:49:38 +01:00
|
|
|
void SIGNAL_do_check(int signum)
|
2020-10-06 16:40:58 +02:00
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
SIGNAL_HANDLER *handler = find_handler(signum);
|
2020-10-21 23:31:02 +02:00
|
|
|
void (*cb)();
|
2020-10-06 16:40:58 +02:00
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sigaction(signum, NULL, &action);
|
2020-10-21 23:31:02 +02:00
|
|
|
get_callback(&action, &cb);
|
2020-10-06 16:40:58 +02:00
|
|
|
|
|
|
|
#if DEBUG_ME
|
2020-10-21 23:31:02 +02:00
|
|
|
fprintf(stderr, "SIGNAL_check: %d -> %d (action.sa_sigaction = %p)\n", signum, cb == handle_signal, cb);
|
|
|
|
if (cb != handle_signal)
|
|
|
|
BREAKPOINT();
|
2020-10-06 16:40:58 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-21 23:31:02 +02:00
|
|
|
if (cb == handle_signal)
|
2020-10-06 16:40:58 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
SIGNAL_install(handler, signum, handle_signal);
|
|
|
|
}
|
2021-01-21 01:49:38 +01:00
|
|
|
|
|
|
|
void SIGNAL_must_check(int signum)
|
|
|
|
{
|
|
|
|
SIGNAL_check_mask |= (1 << signum);
|
|
|
|
}
|