blob: 0c5646513220d78776e41f1860b99bf897533acb [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010023#include <osmocom/core/signal.h>
24#include <osmocom/core/talloc.h>
25#include <osmocom/core/linuxlist.h>
Harald Welteec8b4502010-02-20 20:34:29 +010026#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29
Harald Welteaf8e4352011-08-17 16:19:46 +020030/*! \addtogroup signal
31 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020032 * Generic signalling/notification infrastructure.
33 *
34 * \file signal.c */
Harald Welteaf8e4352011-08-17 16:19:46 +020035
36
Harald Welteec8b4502010-02-20 20:34:29 +010037void *tall_sigh_ctx;
38static LLIST_HEAD(signal_handler_list);
39
40struct signal_handler {
41 struct llist_head entry;
42 unsigned int subsys;
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020043 osmo_signal_cbfn *cbfn;
Harald Welteec8b4502010-02-20 20:34:29 +010044 void *data;
45};
46
47
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048/*! Register a new signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020049 * \param[in] subsys Subsystem number
50 * \param[in] cbfn Callback function
51 * \param[in] data Data passed through to callback
Harald Welte2d2e2cc2016-04-25 12:11:20 +020052 * \returns 0 on success; negative in case of error
Harald Welteaf8e4352011-08-17 16:19:46 +020053 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020054int osmo_signal_register_handler(unsigned int subsys,
55 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010056{
57 struct signal_handler *sig_data;
58
59 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
60 if (!sig_data)
61 return -ENOMEM;
62
63 memset(sig_data, 0, sizeof(*sig_data));
64
65 sig_data->subsys = subsys;
66 sig_data->data = data;
67 sig_data->cbfn = cbfn;
68
69 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
70
71 llist_add_tail(&sig_data->entry, &signal_handler_list);
72
73 return 0;
74}
75
Neels Hofmeyr87e45502017-06-20 00:17:59 +020076/*! Unregister signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020077 * \param[in] subsys Subsystem number
78 * \param[in] cbfn Callback function
79 * \param[in] data Data passed through to callback
80 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020081void osmo_signal_unregister_handler(unsigned int subsys,
82 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010083{
84 struct signal_handler *handler;
85
86 llist_for_each_entry(handler, &signal_handler_list, entry) {
87 if (handler->cbfn == cbfn && handler->data == data
88 && subsys == handler->subsys) {
89 llist_del(&handler->entry);
90 talloc_free(handler);
91 break;
92 }
93 }
94}
95
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096/*! dispatch (deliver) a new signal to all registered handlers
Harald Welteaf8e4352011-08-17 16:19:46 +020097 * \param[in] subsys Subsystem number
98 * \param[in] signal Signal number,
99 * \param[in] signal_data Data to be passed along to handlers
100 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +0200101void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
102 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +0100103{
104 struct signal_handler *handler;
105
106 llist_for_each_entry(handler, &signal_handler_list, entry) {
107 if (handler->subsys != subsys)
108 continue;
109 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
110 }
111}
Harald Welteaf8e4352011-08-17 16:19:46 +0200112
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200113/*! @} */