Harald Welte | c436245 | 2010-03-22 22:25:13 +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 <osmocore/signal.h> |
| 22 | #include <osmocore/talloc.h> |
| 23 | #include <osmocore/linuxlist.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | void *tall_sigh_ctx; |
| 29 | static LLIST_HEAD(signal_handler_list); |
| 30 | |
| 31 | struct signal_handler { |
| 32 | struct llist_head entry; |
| 33 | unsigned int subsys; |
| 34 | signal_cbfn *cbfn; |
| 35 | void *data; |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | int register_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
| 40 | { |
| 41 | struct signal_handler *sig_data; |
| 42 | |
| 43 | sig_data = talloc(tall_sigh_ctx, struct signal_handler); |
| 44 | if (!sig_data) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | memset(sig_data, 0, sizeof(*sig_data)); |
| 48 | |
| 49 | sig_data->subsys = subsys; |
| 50 | sig_data->data = data; |
| 51 | sig_data->cbfn = cbfn; |
| 52 | |
| 53 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 54 | |
| 55 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | void unregister_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data) |
| 61 | { |
| 62 | struct signal_handler *handler; |
| 63 | |
| 64 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 65 | if (handler->cbfn == cbfn && handler->data == data |
| 66 | && subsys == handler->subsys) { |
| 67 | llist_del(&handler->entry); |
| 68 | talloc_free(handler); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void dispatch_signal(unsigned int subsys, unsigned int signal, void *signal_data) |
| 76 | { |
| 77 | struct signal_handler *handler; |
| 78 | |
| 79 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 80 | if (handler->subsys != subsys) |
| 81 | continue; |
| 82 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
| 83 | } |
| 84 | } |