Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Asynchronous signal delivery functions. |
| 3 | * |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <signal.h> |
Willy Tarreau | be8c736 | 2009-07-23 13:40:20 +0200 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 16 | #include <proto/signal.h> |
| 17 | #include <proto/log.h> |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 18 | #include <proto/task.h> |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 19 | |
| 20 | /* Principle : we keep an in-order list of the first occurrence of all received |
| 21 | * signals. All occurrences of a same signal are grouped though. The signal |
| 22 | * queue does not need to be deeper than the number of signals we can handle. |
| 23 | * The handlers will be called asynchronously with the signal number. They can |
| 24 | * check themselves the number of calls by checking the descriptor this signal. |
| 25 | */ |
| 26 | |
| 27 | int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */ |
| 28 | int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */ |
| 29 | struct signal_descriptor signal_state[MAX_SIGNAL]; |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 30 | struct pool_head *pool2_sig_handlers = NULL; |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 31 | sigset_t blocked_sig; |
| 32 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 33 | /* Common signal handler, used by all signals. Received signals are queued. */ |
| 34 | static void signal_handler(int sig) |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 35 | { |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 36 | if (sig < 0 || sig > MAX_SIGNAL) { |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 37 | /* unhandled signal */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 38 | signal(sig, SIG_IGN); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 39 | qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (!signal_state[sig].count) { |
| 44 | /* signal was not queued yet */ |
| 45 | if (signal_queue_len < MAX_SIGNAL) |
| 46 | signal_queue[signal_queue_len++] = sig; |
| 47 | else |
| 48 | qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig); |
| 49 | } |
| 50 | signal_state[sig].count++; |
| 51 | signal(sig, signal_handler); /* re-arm signal */ |
| 52 | } |
| 53 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 54 | /* Call handlers of all pending signals and clear counts and queue length. The |
| 55 | * handlers may unregister themselves by calling signal_register() while they |
| 56 | * are called, just like it is done with normal signal handlers. |
| 57 | * Note that it is more efficient to call the inline version which checks the |
| 58 | * queue length before getting here. |
| 59 | */ |
| 60 | void __signal_process_queue() |
| 61 | { |
| 62 | int sig, cur_pos = 0; |
| 63 | struct signal_descriptor *desc; |
| 64 | sigset_t old_sig; |
| 65 | |
| 66 | /* block signal delivery during processing */ |
| 67 | sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 68 | |
| 69 | for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) { |
| 70 | sig = signal_queue[cur_pos]; |
| 71 | desc = &signal_state[sig]; |
| 72 | if (desc->count) { |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 73 | struct sig_handler *sh, *shb; |
| 74 | list_for_each_entry_safe(sh, shb, &desc->handlers, list) { |
| 75 | if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler) |
| 76 | ((void (*)(struct sig_handler *))sh->handler)(sh); |
| 77 | else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler) |
| 78 | task_wakeup(sh->handler, sh->arg | TASK_WOKEN_SIGNAL); |
| 79 | } |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 80 | desc->count = 0; |
| 81 | } |
| 82 | } |
| 83 | signal_queue_len = 0; |
| 84 | |
| 85 | /* restore signal delivery */ |
| 86 | sigprocmask(SIG_SETMASK, &old_sig, NULL); |
| 87 | } |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame^] | 88 | |
| 89 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 90 | int signal_init() |
| 91 | { |
| 92 | int sig; |
| 93 | |
| 94 | signal_queue_len = 0; |
| 95 | memset(signal_queue, 0, sizeof(signal_queue)); |
| 96 | memset(signal_state, 0, sizeof(signal_state)); |
| 97 | sigfillset(&blocked_sig); |
| 98 | for (sig = 0; sig < MAX_SIGNAL; sig++) |
| 99 | LIST_INIT(&signal_state[sig].handlers); |
| 100 | |
| 101 | pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED); |
| 102 | return pool2_sig_handlers != NULL; |
| 103 | } |
| 104 | |
| 105 | /* releases all registered signal handlers */ |
| 106 | void deinit_signals() |
| 107 | { |
| 108 | int sig; |
| 109 | struct sig_handler *sh, *shb; |
| 110 | |
| 111 | for (sig = 0; sig < MAX_SIGNAL; sig++) { |
| 112 | signal(sig, SIG_DFL); |
| 113 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 114 | LIST_DEL(&sh->list); |
| 115 | pool_free2(pool2_sig_handlers, sh); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* Register a function and an integer argument on a signal. A pointer to the |
| 121 | * newly allocated sig_handler is returned, or NULL in case of any error. The |
| 122 | * caller is responsible for unregistering the function when not used anymore. |
| 123 | * Note that passing a NULL as the function pointer enables interception of the |
| 124 | * signal without processing, which is identical to SIG_IGN. |
| 125 | */ |
| 126 | struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg) |
| 127 | { |
| 128 | struct sig_handler *sh; |
| 129 | |
| 130 | if (sig < 0 || sig > MAX_SIGNAL) |
| 131 | return NULL; |
| 132 | |
| 133 | signal(sig, signal_handler); |
| 134 | |
| 135 | if (!fct) |
| 136 | return NULL; |
| 137 | |
| 138 | sh = pool_alloc2(pool2_sig_handlers); |
| 139 | if (!sh) |
| 140 | return NULL; |
| 141 | |
| 142 | sh->handler = fct; |
| 143 | sh->arg = arg; |
| 144 | sh->flags = SIG_F_TYPE_FCT; |
| 145 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 146 | return sh; |
| 147 | } |
| 148 | |
| 149 | /* Register a task and a wake-up reason on a signal. A pointer to the newly |
| 150 | * allocated sig_handler is returned, or NULL in case of any error. The caller |
| 151 | * is responsible for unregistering the task when not used anymore. Note that |
| 152 | * passing a NULL as the task pointer enables interception of the signal |
| 153 | * without processing, which is identical to SIG_IGN. |
| 154 | */ |
| 155 | struct sig_handler *signal_register_task(int sig, struct task *task, int reason) |
| 156 | { |
| 157 | struct sig_handler *sh; |
| 158 | |
| 159 | if (sig < 0 || sig > MAX_SIGNAL) |
| 160 | return NULL; |
| 161 | |
| 162 | signal(sig, signal_handler); |
| 163 | |
| 164 | if (!task) |
| 165 | return NULL; |
| 166 | |
| 167 | sh = pool_alloc2(pool2_sig_handlers); |
| 168 | if (!sh) |
| 169 | return NULL; |
| 170 | |
| 171 | sh->handler = task; |
| 172 | sh->arg = reason & ~TASK_WOKEN_ANY; |
| 173 | sh->flags = SIG_F_TYPE_TASK; |
| 174 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 175 | return sh; |
| 176 | } |
| 177 | |
| 178 | /* Immediately unregister a handler so that no further signals may be delivered |
| 179 | * to it. The struct is released so the caller may not reference it anymore. |
| 180 | */ |
| 181 | void signal_unregister_handler(struct sig_handler *handler) |
| 182 | { |
| 183 | LIST_DEL(&handler->list); |
| 184 | pool_free2(pool2_sig_handlers, handler); |
| 185 | } |
| 186 | |
| 187 | /* Immediately unregister a handler so that no further signals may be delivered |
| 188 | * to it. The handler struct does not need to be known, only the function or |
| 189 | * task pointer. This method is expensive because it scans all the list, so it |
| 190 | * should only be used for rare cases (eg: exit). The struct is released so the |
| 191 | * caller may not reference it anymore. |
| 192 | */ |
| 193 | void signal_unregister_target(int sig, void *target) |
| 194 | { |
| 195 | struct sig_handler *sh, *shb; |
| 196 | |
| 197 | if (sig < 0 || sig > MAX_SIGNAL) |
| 198 | return; |
| 199 | |
| 200 | if (!target) |
| 201 | return; |
| 202 | |
| 203 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 204 | if (sh->handler == target) { |
| 205 | LIST_DEL(&sh->list); |
| 206 | pool_free2(pool2_sig_handlers, sh); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |