blob: be3b77789fb666aa338300d0f53147a8f9aeab07 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file signal.c
2 * Generic signalling/notification infrastructure. */
3/*
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteec8b4502010-02-20 20:34:29 +01005 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Harald Welteec8b4502010-02-20 20:34:29 +01009 * 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 Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/core/signal.h>
26#include <osmocom/core/talloc.h>
27#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31
Harald Welteaf8e4352011-08-17 16:19:46 +020032/*! \addtogroup signal
33 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020034 * Generic signalling/notification infrastructure.
35 *
36 * \file signal.c */
Harald Welteaf8e4352011-08-17 16:19:46 +020037
38
Oliver Smith9f281422019-03-22 13:22:46 +010039void *tall_sigh_ctx;
Harald Welteec8b4502010-02-20 20:34:29 +010040static LLIST_HEAD(signal_handler_list);
41
42struct signal_handler {
43 struct llist_head entry;
44 unsigned int subsys;
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020045 osmo_signal_cbfn *cbfn;
Harald Welteec8b4502010-02-20 20:34:29 +010046 void *data;
47};
48
Pau Espin Pedrol0b6fcb02018-08-16 19:05:19 +020049/*! Initialize a signal_handler talloc context for \ref osmo_signal_register_handler.
50 * Create a talloc context called "osmo_signal".
51 * \param[in] root_ctx talloc context used as parent for the new "osmo_signal" ctx.
52 * \returns the new osmo_signal talloc context, e.g. for reporting
53 */
54void *osmo_signal_talloc_ctx_init(void *root_ctx) {
Harald Welte8c58af12019-03-20 11:26:58 +010055 tall_sigh_ctx = talloc_named_const(root_ctx, 0, "osmo_signal");
Pau Espin Pedrol0b6fcb02018-08-16 19:05:19 +020056 return tall_sigh_ctx;
57}
Harald Welteec8b4502010-02-20 20:34:29 +010058
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059/*! Register a new signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020060 * \param[in] subsys Subsystem number
61 * \param[in] cbfn Callback function
62 * \param[in] data Data passed through to callback
Harald Welte2d2e2cc2016-04-25 12:11:20 +020063 * \returns 0 on success; negative in case of error
Harald Welteaf8e4352011-08-17 16:19:46 +020064 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020065int osmo_signal_register_handler(unsigned int subsys,
66 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010067{
68 struct signal_handler *sig_data;
69
Vadim Yanitskiya3226f72019-03-20 20:41:54 +070070 sig_data = talloc_zero(tall_sigh_ctx, struct signal_handler);
Harald Welteec8b4502010-02-20 20:34:29 +010071 if (!sig_data)
72 return -ENOMEM;
73
Harald Welteec8b4502010-02-20 20:34:29 +010074 sig_data->subsys = subsys;
75 sig_data->data = data;
76 sig_data->cbfn = cbfn;
77
78 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
79
80 llist_add_tail(&sig_data->entry, &signal_handler_list);
81
82 return 0;
83}
84
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085/*! Unregister signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020086 * \param[in] subsys Subsystem number
87 * \param[in] cbfn Callback function
88 * \param[in] data Data passed through to callback
89 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020090void osmo_signal_unregister_handler(unsigned int subsys,
91 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010092{
93 struct signal_handler *handler;
94
95 llist_for_each_entry(handler, &signal_handler_list, entry) {
96 if (handler->cbfn == cbfn && handler->data == data
97 && subsys == handler->subsys) {
98 llist_del(&handler->entry);
99 talloc_free(handler);
100 break;
101 }
102 }
103}
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105/*! dispatch (deliver) a new signal to all registered handlers
Harald Welteaf8e4352011-08-17 16:19:46 +0200106 * \param[in] subsys Subsystem number
107 * \param[in] signal Signal number,
108 * \param[in] signal_data Data to be passed along to handlers
109 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +0200110void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
111 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +0100112{
113 struct signal_handler *handler;
114
115 llist_for_each_entry(handler, &signal_handler_list, entry) {
116 if (handler->subsys != subsys)
117 continue;
118 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
119 }
120}
Harald Welteaf8e4352011-08-17 16:19:46 +0200121
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200122/*! @} */