[MEDIUM] signals: add support for registering functions and tasks

The two new functions below make it possible to register any number
of functions or tasks to a system signal. They will be called in the
registration order when the signal is received.

    struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg);
    struct sig_handler *signal_register_task(int sig, struct task *task, int reason);
diff --git a/src/signal.c b/src/signal.c
index 9825c0d..746ae9e 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -1,7 +1,7 @@
 /*
  * Asynchronous signal delivery functions.
  *
- * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
+ * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -15,6 +15,7 @@
 
 #include <proto/signal.h>
 #include <proto/log.h>
+#include <proto/task.h>
 
 /* 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
@@ -26,22 +27,16 @@
 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];
+struct pool_head *pool2_sig_handlers = NULL;
 sigset_t blocked_sig;
 
-void signal_init()
+/* Common signal handler, used by all signals. Received signals are queued. */
+static void signal_handler(int sig)
 {
-	signal_queue_len = 0;
-	memset(signal_queue, 0, sizeof(signal_queue));
-	memset(signal_state, 0, sizeof(signal_state));
-	sigfillset(&blocked_sig);
-}
-
-void signal_handler(int sig)
-{
-	if (sig < 0 || sig > MAX_SIGNAL || !signal_state[sig].handler) {
+	if (sig < 0 || sig > MAX_SIGNAL) {
 		/* unhandled signal */
-		qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
 		signal(sig, SIG_IGN);
+		qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
 		return;
 	}
 
@@ -56,33 +51,6 @@
 	signal(sig, signal_handler); /* re-arm signal */
 }
 
-/* Register a handler for signal <sig>. Set it to NULL, SIG_DFL or SIG_IGN to
- * remove the handler. The signal's queue is flushed and the signal is really
- * registered (or unregistered) for the process. The interface is the same as
- * for standard signal delivery, except that the handler does not need to rearm
- * the signal itself (it can disable it however).
- */
-void signal_register(int sig, void (*handler)(int))
-{
-	if (sig < 0 || sig > MAX_SIGNAL) {
-		qfprintf(stderr, "Failed to register signal %d : out of range [0..%d].\n", sig, MAX_SIGNAL);
-		return;
-	}
-
-	signal_state[sig].count = 0;
-	if (handler == NULL)
-		handler = SIG_IGN;
-
-	if (handler != SIG_IGN && handler != SIG_DFL) {
-		signal_state[sig].handler = handler;
-		signal(sig, signal_handler);
-	}
-	else {
-		signal_state[sig].handler = NULL;
-		signal(sig, handler);
-	}
-}
-
 /* 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.
@@ -102,8 +70,13 @@
 		sig  = signal_queue[cur_pos];
 		desc = &signal_state[sig];
 		if (desc->count) {
-			if (desc->handler)
-				desc->handler(sig);
+			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);
+			}
 			desc->count = 0;
 		}
 	}
@@ -112,3 +85,126 @@
 	/* restore signal delivery */
 	sigprocmask(SIG_SETMASK, &old_sig, NULL);
 }
+
+/* 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;
+		}
+	}
+}