Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [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> |
| 22 | #include <malloc.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | |
| 26 | static LLIST_HEAD(signal_handler_list); |
| 27 | |
| 28 | struct signal_handler { |
| 29 | struct llist_head entry; |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 30 | unsigned int subsys; |
| 31 | signal_cbfn *cbfn; |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 32 | void *data; |
| 33 | }; |
| 34 | |
| 35 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 36 | int register_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 37 | { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 38 | struct signal_handler *sig_data = malloc(sizeof(*sig_data)); |
| 39 | |
| 40 | if (!sig_data) |
| 41 | return -ENOMEM; |
| 42 | |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 43 | memset(sig_data, 0, sizeof(*sig_data)); |
| 44 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 45 | sig_data->subsys = subsys; |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 46 | sig_data->data = data; |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 47 | sig_data->cbfn = cbfn; |
| 48 | |
| 49 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 50 | |
Holger Freyther | 36b5d65 | 2009-02-14 23:35:06 +0000 | [diff] [blame] | 51 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 52 | |
| 53 | return 0; |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 56 | void unregister_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 57 | { |
| 58 | struct signal_handler *handler; |
| 59 | |
| 60 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 61 | if (handler->cbfn == cbfn && handler->data == data |
| 62 | && subsys == handler->subsys) { |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 63 | llist_del(&handler->entry); |
| 64 | free(handler); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 71 | void dispatch_signal(unsigned int subsys, unsigned int signal, void *signal_data) |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 72 | { |
| 73 | struct signal_handler *handler; |
| 74 | |
| 75 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 76 | if (handler->subsys != subsys) |
| 77 | continue; |
| 78 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 79 | } |
| 80 | } |