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; |
| 30 | int areas; |
| 31 | |
| 32 | int (*sig_handler)(struct signal_data *, void*); |
| 33 | void *data; |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | void register_signal_handler(int areas, |
| 38 | int (*handler)(struct signal_data *, void *), void *data) |
| 39 | { |
| 40 | struct signal_handler *sig_data = |
| 41 | (struct signal_handler *)malloc(sizeof(*sig_data)); |
| 42 | memset(sig_data, 0, sizeof(*sig_data)); |
| 43 | |
| 44 | |
| 45 | sig_data->areas = areas; |
| 46 | sig_data->data = data; |
| 47 | sig_data->sig_handler = handler; |
Holger Freyther | 36b5d65 | 2009-02-14 23:35:06 +0000 | [diff] [blame] | 48 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Holger Freyther | e4b3fa3 | 2009-02-14 22:51:10 +0000 | [diff] [blame] | 51 | void remove_signal_handler(int areas, int (*sig_handler)(struct signal_data *, void *), void *data) |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 52 | { |
| 53 | struct signal_handler *handler; |
| 54 | |
| 55 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
Holger Freyther | e4b3fa3 | 2009-02-14 22:51:10 +0000 | [diff] [blame] | 56 | if (handler->sig_handler == sig_handler |
| 57 | && handler->data == data && areas == handler->areas) { |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 58 | llist_del(&handler->entry); |
| 59 | free(handler); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
Holger Freyther | e4b3fa3 | 2009-02-14 22:51:10 +0000 | [diff] [blame] | 66 | void dispatch_signal(struct signal_data *data) |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 67 | { |
| 68 | struct signal_handler *handler; |
| 69 | |
| 70 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
Holger Freyther | e4b3fa3 | 2009-02-14 22:51:10 +0000 | [diff] [blame] | 71 | if (handler->areas & data->area) { |
Holger Freyther | 2b2d2e3 | 2009-02-14 22:51:00 +0000 | [diff] [blame] | 72 | (*handler->sig_handler)(data, handler->data); |
| 73 | } |
| 74 | } |
| 75 | } |