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