2009-05-10 06:53:33 +00:00
|
|
|
/*
|
|
|
|
* Asynchronous signal delivery functions.
|
|
|
|
*
|
2010-08-27 15:56:48 +00:00
|
|
|
* Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
|
2009-05-10 06:53:33 +00: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 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <signal.h>
|
2009-07-23 11:40:20 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-05-10 06:53:33 +00:00
|
|
|
#include <proto/signal.h>
|
|
|
|
#include <proto/log.h>
|
2010-08-27 15:56:48 +00:00
|
|
|
#include <proto/task.h>
|
2009-05-10 06:53:33 +00:00
|
|
|
|
|
|
|
/* Principle : we keep an in-order list of the first occurrence of all received
|
|
|
|
* signals. All occurrences of a same signal are grouped though. The signal
|
|
|
|
* queue does not need to be deeper than the number of signals we can handle.
|
|
|
|
* The handlers will be called asynchronously with the signal number. They can
|
|
|
|
* check themselves the number of calls by checking the descriptor this signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */
|
|
|
|
int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */
|
|
|
|
struct signal_descriptor signal_state[MAX_SIGNAL];
|
2010-08-27 15:56:48 +00:00
|
|
|
struct pool_head *pool2_sig_handlers = NULL;
|
2009-05-10 06:53:33 +00:00
|
|
|
sigset_t blocked_sig;
|
|
|
|
|
2010-08-27 15:56:48 +00:00
|
|
|
/* Common signal handler, used by all signals. Received signals are queued. */
|
|
|
|
static void signal_handler(int sig)
|
2009-05-10 06:53:33 +00:00
|
|
|
{
|
2010-08-27 15:56:48 +00:00
|
|
|
if (sig < 0 || sig > MAX_SIGNAL) {
|
2009-05-10 06:53:33 +00:00
|
|
|
/* unhandled signal */
|
|
|
|
signal(sig, SIG_IGN);
|
2010-08-27 15:56:48 +00:00
|
|
|
qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
|
2009-05-10 06:53:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!signal_state[sig].count) {
|
|
|
|
/* signal was not queued yet */
|
|
|
|
if (signal_queue_len < MAX_SIGNAL)
|
|
|
|
signal_queue[signal_queue_len++] = sig;
|
|
|
|
else
|
|
|
|
qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig);
|
|
|
|
}
|
|
|
|
signal_state[sig].count++;
|
|
|
|
signal(sig, signal_handler); /* re-arm signal */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call handlers of all pending signals and clear counts and queue length. The
|
|
|
|
* handlers may unregister themselves by calling signal_register() while they
|
|
|
|
* are called, just like it is done with normal signal handlers.
|
|
|
|
* Note that it is more efficient to call the inline version which checks the
|
|
|
|
* queue length before getting here.
|
|
|
|
*/
|
|
|
|
void __signal_process_queue()
|
|
|
|
{
|
|
|
|
int sig, cur_pos = 0;
|
|
|
|
struct signal_descriptor *desc;
|
|
|
|
sigset_t old_sig;
|
|
|
|
|
|
|
|
/* block signal delivery during processing */
|
|
|
|
sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig);
|
|
|
|
|
|
|
|
for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) {
|
|
|
|
sig = signal_queue[cur_pos];
|
|
|
|
desc = &signal_state[sig];
|
|
|
|
if (desc->count) {
|
2010-08-27 15:56:48 +00:00
|
|
|
struct sig_handler *sh, *shb;
|
|
|
|
list_for_each_entry_safe(sh, shb, &desc->handlers, list) {
|
|
|
|
if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler)
|
|
|
|
((void (*)(struct sig_handler *))sh->handler)(sh);
|
|
|
|
else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler)
|
|
|
|
task_wakeup(sh->handler, sh->arg | TASK_WOKEN_SIGNAL);
|
|
|
|
}
|
2009-05-10 06:53:33 +00:00
|
|
|
desc->count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
signal_queue_len = 0;
|
|
|
|
|
|
|
|
/* restore signal delivery */
|
|
|
|
sigprocmask(SIG_SETMASK, &old_sig, NULL);
|
|
|
|
}
|
2010-08-27 15:56:48 +00:00
|
|
|
|
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
|
|
|
int signal_init()
|
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
|
|
|
|
signal_queue_len = 0;
|
|
|
|
memset(signal_queue, 0, sizeof(signal_queue));
|
|
|
|
memset(signal_state, 0, sizeof(signal_state));
|
|
|
|
sigfillset(&blocked_sig);
|
|
|
|
for (sig = 0; sig < MAX_SIGNAL; sig++)
|
|
|
|
LIST_INIT(&signal_state[sig].handlers);
|
|
|
|
|
|
|
|
pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED);
|
|
|
|
return pool2_sig_handlers != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* releases all registered signal handlers */
|
|
|
|
void deinit_signals()
|
|
|
|
{
|
|
|
|
int sig;
|
|
|
|
struct sig_handler *sh, *shb;
|
|
|
|
|
|
|
|
for (sig = 0; sig < MAX_SIGNAL; sig++) {
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
|
|
|
|
LIST_DEL(&sh->list);
|
|
|
|
pool_free2(pool2_sig_handlers, sh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a function and an integer argument on a signal. A pointer to the
|
|
|
|
* newly allocated sig_handler is returned, or NULL in case of any error. The
|
|
|
|
* caller is responsible for unregistering the function when not used anymore.
|
|
|
|
* Note that passing a NULL as the function pointer enables interception of the
|
|
|
|
* signal without processing, which is identical to SIG_IGN.
|
|
|
|
*/
|
|
|
|
struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
|
|
|
|
{
|
|
|
|
struct sig_handler *sh;
|
|
|
|
|
|
|
|
if (sig < 0 || sig > MAX_SIGNAL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
signal(sig, signal_handler);
|
|
|
|
|
|
|
|
if (!fct)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sh = pool_alloc2(pool2_sig_handlers);
|
|
|
|
if (!sh)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sh->handler = fct;
|
|
|
|
sh->arg = arg;
|
|
|
|
sh->flags = SIG_F_TYPE_FCT;
|
|
|
|
LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a task and a wake-up reason on a signal. A pointer to the newly
|
|
|
|
* allocated sig_handler is returned, or NULL in case of any error. The caller
|
|
|
|
* is responsible for unregistering the task when not used anymore. Note that
|
|
|
|
* passing a NULL as the task pointer enables interception of the signal
|
|
|
|
* without processing, which is identical to SIG_IGN.
|
|
|
|
*/
|
|
|
|
struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
|
|
|
|
{
|
|
|
|
struct sig_handler *sh;
|
|
|
|
|
|
|
|
if (sig < 0 || sig > MAX_SIGNAL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
signal(sig, signal_handler);
|
|
|
|
|
|
|
|
if (!task)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sh = pool_alloc2(pool2_sig_handlers);
|
|
|
|
if (!sh)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sh->handler = task;
|
|
|
|
sh->arg = reason & ~TASK_WOKEN_ANY;
|
|
|
|
sh->flags = SIG_F_TYPE_TASK;
|
|
|
|
LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediately unregister a handler so that no further signals may be delivered
|
|
|
|
* to it. The struct is released so the caller may not reference it anymore.
|
|
|
|
*/
|
|
|
|
void signal_unregister_handler(struct sig_handler *handler)
|
|
|
|
{
|
|
|
|
LIST_DEL(&handler->list);
|
|
|
|
pool_free2(pool2_sig_handlers, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediately unregister a handler so that no further signals may be delivered
|
|
|
|
* to it. The handler struct does not need to be known, only the function or
|
|
|
|
* task pointer. This method is expensive because it scans all the list, so it
|
|
|
|
* should only be used for rare cases (eg: exit). The struct is released so the
|
|
|
|
* caller may not reference it anymore.
|
|
|
|
*/
|
|
|
|
void signal_unregister_target(int sig, void *target)
|
|
|
|
{
|
|
|
|
struct sig_handler *sh, *shb;
|
|
|
|
|
|
|
|
if (sig < 0 || sig > MAX_SIGNAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
|
|
|
|
if (sh->handler == target) {
|
|
|
|
LIST_DEL(&sh->list);
|
|
|
|
pool_free2(pool2_sig_handlers, sh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|