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 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 30 | * Generic signalling/notification infrastructure. |
| 31 | * |
| 32 | * \file signal.c */ |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 33 | |
| 34 | |
Oliver Smith | 9f28142 | 2019-03-22 13:22:46 +0100 | [diff] [blame] | 35 | void *tall_sigh_ctx; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 36 | static LLIST_HEAD(signal_handler_list); |
| 37 | |
| 38 | struct signal_handler { |
| 39 | struct llist_head entry; |
| 40 | unsigned int subsys; |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 41 | osmo_signal_cbfn *cbfn; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | void *data; |
| 43 | }; |
| 44 | |
Pau Espin Pedrol | 0b6fcb0 | 2018-08-16 19:05:19 +0200 | [diff] [blame] | 45 | /*! Initialize a signal_handler talloc context for \ref osmo_signal_register_handler. |
| 46 | * Create a talloc context called "osmo_signal". |
| 47 | * \param[in] root_ctx talloc context used as parent for the new "osmo_signal" ctx. |
| 48 | * \returns the new osmo_signal talloc context, e.g. for reporting |
| 49 | */ |
| 50 | void *osmo_signal_talloc_ctx_init(void *root_ctx) { |
Harald Welte | 8c58af1 | 2019-03-20 11:26:58 +0100 | [diff] [blame] | 51 | tall_sigh_ctx = talloc_named_const(root_ctx, 0, "osmo_signal"); |
Pau Espin Pedrol | 0b6fcb0 | 2018-08-16 19:05:19 +0200 | [diff] [blame] | 52 | return tall_sigh_ctx; |
| 53 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 54 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 55 | /*! Register a new signal handler |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 56 | * \param[in] subsys Subsystem number |
| 57 | * \param[in] cbfn Callback function |
| 58 | * \param[in] data Data passed through to callback |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 59 | * \returns 0 on success; negative in case of error |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 60 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 61 | int osmo_signal_register_handler(unsigned int subsys, |
| 62 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | { |
| 64 | struct signal_handler *sig_data; |
| 65 | |
Vadim Yanitskiy | a3226f7 | 2019-03-20 20:41:54 +0700 | [diff] [blame] | 66 | sig_data = talloc_zero(tall_sigh_ctx, struct signal_handler); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 67 | if (!sig_data) |
| 68 | return -ENOMEM; |
| 69 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 70 | sig_data->subsys = subsys; |
| 71 | sig_data->data = data; |
| 72 | sig_data->cbfn = cbfn; |
| 73 | |
| 74 | /* FIXME: check if we already have a handler for this subsys/cbfn/data */ |
| 75 | |
| 76 | llist_add_tail(&sig_data->entry, &signal_handler_list); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 81 | /*! Unregister signal handler |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 82 | * \param[in] subsys Subsystem number |
| 83 | * \param[in] cbfn Callback function |
| 84 | * \param[in] data Data passed through to callback |
| 85 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 86 | void osmo_signal_unregister_handler(unsigned int subsys, |
| 87 | osmo_signal_cbfn *cbfn, void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 88 | { |
| 89 | struct signal_handler *handler; |
| 90 | |
| 91 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 92 | if (handler->cbfn == cbfn && handler->data == data |
| 93 | && subsys == handler->subsys) { |
| 94 | llist_del(&handler->entry); |
| 95 | talloc_free(handler); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 101 | /*! dispatch (deliver) a new signal to all registered handlers |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 102 | * \param[in] subsys Subsystem number |
| 103 | * \param[in] signal Signal number, |
| 104 | * \param[in] signal_data Data to be passed along to handlers |
| 105 | */ |
Pablo Neira Ayuso | a10dd35 | 2011-05-07 12:42:45 +0200 | [diff] [blame] | 106 | void osmo_signal_dispatch(unsigned int subsys, unsigned int signal, |
| 107 | void *signal_data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 108 | { |
| 109 | struct signal_handler *handler; |
| 110 | |
| 111 | llist_for_each_entry(handler, &signal_handler_list, entry) { |
| 112 | if (handler->subsys != subsys) |
| 113 | continue; |
| 114 | (*handler->cbfn)(subsys, signal, handler->data, signal_data); |
| 115 | } |
| 116 | } |
Harald Welte | af8e435 | 2011-08-17 16:19:46 +0200 | [diff] [blame] | 117 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 118 | /*! @} */ |