Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file signal.c |
| 2 | * Generic signalling/notification infrastructure. */ |
| 3 | /* |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 23 | #include <osmocom/core/signal.h> |
| 24 | #include <osmocom/core/talloc.h> |
| 25 | #include <osmocom/core/linuxlist.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 30 | /*! \addtogroup signal |
| 31 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 32 | * Generic signalling/notification infrastructure. |
| 33 | * |
| 34 | * \file signal.c */ |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 35 | |
| 36 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | void *tall_sigh_ctx; |
| 38 | static LLIST_HEAD(signal_handler_list); |
| 39 | |
| 40 | struct signal_handler { |
| 41 | struct llist_head entry; |
| 42 | unsigned int subsys; |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 43 | osmo_signal_cbfn *cbfn; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | void *data; |
| 45 | }; |
| 46 | |
| 47 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 48 | /*! Register a new signal handler |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 49 | * \param[in] subsys Subsystem number |
| 50 | * \param[in] cbfn Callback function |
| 51 | * \param[in] data Data passed through to callback |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 52 | * \returns 0 on success; negative in case of error |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 53 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 54 | int osmo_signal_register_handler(unsigned int subsys, |
| 55 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | { |
| 57 | struct signal_handler *sig_data; |
| 58 | |
| 59 | sig_data = talloc(tall_sigh_ctx, struct signal_handler); |
| 60 | if (!sig_data) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | memset(sig_data, 0, sizeof(*sig_data)); |
| 64 | |
| 65 | sig_data->subsys = subsys; |
| 66 | sig_data->data = data; |
| 67 | sig_data->cbfn = cbfn; |
| 68 | |
| 69 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 70 | |
| 71 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 76 | /*! Unregister signal handler |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 77 | * \param[in] subsys Subsystem number |
| 78 | * \param[in] cbfn Callback function |
| 79 | * \param[in] data Data passed through to callback |
| 80 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 81 | void osmo_signal_unregister_handler(unsigned int subsys, |
| 82 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 83 | { |
| 84 | struct signal_handler *handler; |
| 85 | |
| 86 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 87 | if (handler->cbfn == cbfn && handler->data == data |
| 88 | && subsys == handler->subsys) { |
| 89 | llist_del(&handler->entry); |
| 90 | talloc_free(handler); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | /*! dispatch (deliver) a new signal to all registered handlers |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 97 | * \param[in] subsys Subsystem number |
| 98 | * \param[in] signal Signal number, |
| 99 | * \param[in] signal_data Data to be passed along to handlers |
| 100 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 101 | void osmo_signal_dispatch(unsigned int subsys, unsigned int signal, |
| 102 | void *signal_data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 103 | { |
| 104 | struct signal_handler *handler; |
| 105 | |
| 106 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 107 | if (handler->subsys != subsys) |
| 108 | continue; |
| 109 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
| 110 | } |
| 111 | } |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 112 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 113 | /*! @} */ |