Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Generic signalling/notification infrastructure */ |
| 2 | /* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <openbsc/signal.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 22 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 27 | static void *tall_sigh_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 28 | static LLIST_HEAD(signal_handler_list); |
| 29 | |
| 30 | struct signal_handler { |
| 31 | struct llist_head entry; |
| 32 | unsigned int subsys; |
| 33 | signal_cbfn *cbfn; |
| 34 | void *data; |
| 35 | }; |
| 36 | |
| 37 | |
| 38 | int register_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
| 39 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 40 | struct signal_handler *sig_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 42 | sig_data = talloc(tall_sigh_ctx, struct signal_handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 43 | if (!sig_data) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | memset(sig_data, 0, sizeof(*sig_data)); |
| 47 | |
| 48 | sig_data->subsys = subsys; |
| 49 | sig_data->data = data; |
| 50 | sig_data->cbfn = cbfn; |
| 51 | |
| 52 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 53 | |
| 54 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void unregister_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
| 60 | { |
| 61 | struct signal_handler *handler; |
| 62 | |
| 63 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 64 | if (handler->cbfn == cbfn && handler->data == data |
| 65 | && subsys == handler->subsys) { |
| 66 | llist_del(&handler->entry); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 67 | talloc_free(handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void dispatch_signal(unsigned int subsys, unsigned int signal, void *signal_data) |
| 75 | { |
| 76 | struct signal_handler *handler; |
| 77 | |
| 78 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 79 | if (handler->subsys != subsys) |
| 80 | continue; |
| 81 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
| 82 | } |
| 83 | } |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 84 | |
| 85 | static __attribute__((constructor)) void on_dso_load_signal(void) |
| 86 | { |
| 87 | tall_sigh_ctx = talloc_named_const(NULL, 1, "signal_handler"); |
| 88 | } |