Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [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 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 21 | #include <osmocom/core/signal.h> |
| 22 | #include <osmocom/core/talloc.h> |
| 23 | #include <osmocom/core/linuxlist.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 28 | /*! \addtogroup signal |
| 29 | * @{ |
| 30 | */ |
| 31 | /*! \file signal.c */ |
| 32 | |
| 33 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | void *tall_sigh_ctx; |
| 35 | static LLIST_HEAD(signal_handler_list); |
| 36 | |
| 37 | struct signal_handler { |
| 38 | struct llist_head entry; |
| 39 | unsigned int subsys; |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 40 | osmo_signal_cbfn *cbfn; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | void *data; |
| 42 | }; |
| 43 | |
| 44 | |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 45 | /*! \brief Register a new signal handler |
| 46 | * \param[in] subsys Subsystem number |
| 47 | * \param[in] cbfn Callback function |
| 48 | * \param[in] data Data passed through to callback |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 49 | * \returns 0 on success; negative in case of error |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 50 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 51 | int osmo_signal_register_handler(unsigned int subsys, |
| 52 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 53 | { |
| 54 | struct signal_handler *sig_data; |
| 55 | |
| 56 | sig_data = talloc(tall_sigh_ctx, struct signal_handler); |
| 57 | if (!sig_data) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | memset(sig_data, 0, sizeof(*sig_data)); |
| 61 | |
| 62 | sig_data->subsys = subsys; |
| 63 | sig_data->data = data; |
| 64 | sig_data->cbfn = cbfn; |
| 65 | |
| 66 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 67 | |
| 68 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 73 | /*! \brief Unregister signal handler |
| 74 | * \param[in] subsys Subsystem number |
| 75 | * \param[in] cbfn Callback function |
| 76 | * \param[in] data Data passed through to callback |
| 77 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 78 | void osmo_signal_unregister_handler(unsigned int subsys, |
| 79 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 80 | { |
| 81 | struct signal_handler *handler; |
| 82 | |
| 83 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 84 | if (handler->cbfn == cbfn && handler->data == data |
| 85 | && subsys == handler->subsys) { |
| 86 | llist_del(&handler->entry); |
| 87 | talloc_free(handler); |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 93 | /*! \brief dispatch (deliver) a new signal to all registered handlers |
| 94 | * \param[in] subsys Subsystem number |
| 95 | * \param[in] signal Signal number, |
| 96 | * \param[in] signal_data Data to be passed along to handlers |
| 97 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 98 | void osmo_signal_dispatch(unsigned int subsys, unsigned int signal, |
| 99 | void *signal_data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 100 | { |
| 101 | struct signal_handler *handler; |
| 102 | |
| 103 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 104 | if (handler->subsys != subsys) |
| 105 | continue; |
| 106 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
| 107 | } |
| 108 | } |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 109 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 110 | /*! @} */ |