blob: 1d11617a5070c4c1eedd43bf6cbed1160993f203 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* 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 Ayuso83419342011-03-22 16:36:13 +010021#include <osmocom/core/signal.h>
22#include <osmocom/core/talloc.h>
23#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010024#include <stdlib.h>
25#include <string.h>
26#include <errno.h>
27
Harald Welteaf8e4352011-08-17 16:19:46 +020028/*! \addtogroup signal
29 * @{
Harald Welte96e2a002017-06-12 21:44:18 +020030 * \brief Generic signalling/notification infrastructure
Harald Welteaf8e4352011-08-17 16:19:46 +020031 */
Harald Welte96e2a002017-06-12 21:44:18 +020032
Harald Welteaf8e4352011-08-17 16:19:46 +020033/*! \file signal.c */
34
35
Harald Welteec8b4502010-02-20 20:34:29 +010036void *tall_sigh_ctx;
37static LLIST_HEAD(signal_handler_list);
38
39struct signal_handler {
40 struct llist_head entry;
41 unsigned int subsys;
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020042 osmo_signal_cbfn *cbfn;
Harald Welteec8b4502010-02-20 20:34:29 +010043 void *data;
44};
45
46
Harald Welteaf8e4352011-08-17 16:19:46 +020047/*! \brief Register a new signal handler
48 * \param[in] subsys Subsystem number
49 * \param[in] cbfn Callback function
50 * \param[in] data Data passed through to callback
Harald Welte2d2e2cc2016-04-25 12:11:20 +020051 * \returns 0 on success; negative in case of error
Harald Welteaf8e4352011-08-17 16:19:46 +020052 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020053int osmo_signal_register_handler(unsigned int subsys,
54 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010055{
56 struct signal_handler *sig_data;
57
58 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
59 if (!sig_data)
60 return -ENOMEM;
61
62 memset(sig_data, 0, sizeof(*sig_data));
63
64 sig_data->subsys = subsys;
65 sig_data->data = data;
66 sig_data->cbfn = cbfn;
67
68 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
69
70 llist_add_tail(&sig_data->entry, &signal_handler_list);
71
72 return 0;
73}
74
Harald Welteaf8e4352011-08-17 16:19:46 +020075/*! \brief Unregister signal handler
76 * \param[in] subsys Subsystem number
77 * \param[in] cbfn Callback function
78 * \param[in] data Data passed through to callback
79 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020080void osmo_signal_unregister_handler(unsigned int subsys,
81 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010082{
83 struct signal_handler *handler;
84
85 llist_for_each_entry(handler, &signal_handler_list, entry) {
86 if (handler->cbfn == cbfn && handler->data == data
87 && subsys == handler->subsys) {
88 llist_del(&handler->entry);
89 talloc_free(handler);
90 break;
91 }
92 }
93}
94
Harald Welteaf8e4352011-08-17 16:19:46 +020095/*! \brief dispatch (deliver) a new signal to all registered handlers
96 * \param[in] subsys Subsystem number
97 * \param[in] signal Signal number,
98 * \param[in] signal_data Data to be passed along to handlers
99 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +0200100void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
101 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +0100102{
103 struct signal_handler *handler;
104
105 llist_for_each_entry(handler, &signal_handler_list, entry) {
106 if (handler->subsys != subsys)
107 continue;
108 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
109 }
110}
Harald Welteaf8e4352011-08-17 16:19:46 +0200111
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200112/*! @} */