blob: 840259156ac9ae37d256449d9a65f271071bfd99 [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 * @{
30 */
31/*! \file signal.c */
32
33
Harald Welteec8b4502010-02-20 20:34:29 +010034void *tall_sigh_ctx;
35static LLIST_HEAD(signal_handler_list);
36
37struct signal_handler {
38 struct llist_head entry;
39 unsigned int subsys;
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020040 osmo_signal_cbfn *cbfn;
Harald Welteec8b4502010-02-20 20:34:29 +010041 void *data;
42};
43
44
Harald Welteaf8e4352011-08-17 16:19:46 +020045/*! \brief Register a new signal handler
46 * \param[in] subsys Subsystem number
47 * \param[in] cbfn Callback function
48 * \param[in] data Data passed through to callback
Harald Welte2d2e2cc2016-04-25 12:11:20 +020049 * \returns 0 on success; negative in case of error
Harald Welteaf8e4352011-08-17 16:19:46 +020050 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020051int osmo_signal_register_handler(unsigned int subsys,
52 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010053{
54 struct signal_handler *sig_data;
55
56 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
57 if (!sig_data)
58 return -ENOMEM;
59
60 memset(sig_data, 0, sizeof(*sig_data));
61
62 sig_data->subsys = subsys;
63 sig_data->data = data;
64 sig_data->cbfn = cbfn;
65
66 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
67
68 llist_add_tail(&sig_data->entry, &signal_handler_list);
69
70 return 0;
71}
72
Harald Welteaf8e4352011-08-17 16:19:46 +020073/*! \brief Unregister signal handler
74 * \param[in] subsys Subsystem number
75 * \param[in] cbfn Callback function
76 * \param[in] data Data passed through to callback
77 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020078void osmo_signal_unregister_handler(unsigned int subsys,
79 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010080{
81 struct signal_handler *handler;
82
83 llist_for_each_entry(handler, &signal_handler_list, entry) {
84 if (handler->cbfn == cbfn && handler->data == data
85 && subsys == handler->subsys) {
86 llist_del(&handler->entry);
87 talloc_free(handler);
88 break;
89 }
90 }
91}
92
Harald Welteaf8e4352011-08-17 16:19:46 +020093/*! \brief dispatch (deliver) a new signal to all registered handlers
94 * \param[in] subsys Subsystem number
95 * \param[in] signal Signal number,
96 * \param[in] signal_data Data to be passed along to handlers
97 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020098void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
99 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +0100100{
101 struct signal_handler *handler;
102
103 llist_for_each_entry(handler, &signal_handler_list, entry) {
104 if (handler->subsys != subsys)
105 continue;
106 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
107 }
108}
Harald Welteaf8e4352011-08-17 16:19:46 +0200109
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200110/*! @} */