blob: 745d7c38d571533609fa7140f0fdd0fb8199339a [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
Harald Welteec8b4502010-02-20 20:34:29 +010039void *tall_sigh_ctx;
40static 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
49
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050/*! Register a new signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020051 * \param[in] subsys Subsystem number
52 * \param[in] cbfn Callback function
53 * \param[in] data Data passed through to callback
Harald Welte2d2e2cc2016-04-25 12:11:20 +020054 * \returns 0 on success; negative in case of error
Harald Welteaf8e4352011-08-17 16:19:46 +020055 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020056int osmo_signal_register_handler(unsigned int subsys,
57 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010058{
59 struct signal_handler *sig_data;
60
61 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
62 if (!sig_data)
63 return -ENOMEM;
64
65 memset(sig_data, 0, sizeof(*sig_data));
66
67 sig_data->subsys = subsys;
68 sig_data->data = data;
69 sig_data->cbfn = cbfn;
70
71 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
72
73 llist_add_tail(&sig_data->entry, &signal_handler_list);
74
75 return 0;
76}
77
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! Unregister signal handler
Harald Welteaf8e4352011-08-17 16:19:46 +020079 * \param[in] subsys Subsystem number
80 * \param[in] cbfn Callback function
81 * \param[in] data Data passed through to callback
82 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020083void osmo_signal_unregister_handler(unsigned int subsys,
84 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010085{
86 struct signal_handler *handler;
87
88 llist_for_each_entry(handler, &signal_handler_list, entry) {
89 if (handler->cbfn == cbfn && handler->data == data
90 && subsys == handler->subsys) {
91 llist_del(&handler->entry);
92 talloc_free(handler);
93 break;
94 }
95 }
96}
97
Neels Hofmeyr87e45502017-06-20 00:17:59 +020098/*! dispatch (deliver) a new signal to all registered handlers
Harald Welteaf8e4352011-08-17 16:19:46 +020099 * \param[in] subsys Subsystem number
100 * \param[in] signal Signal number,
101 * \param[in] signal_data Data to be passed along to handlers
102 */
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +0200103void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
104 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +0100105{
106 struct signal_handler *handler;
107
108 llist_for_each_entry(handler, &signal_handler_list, entry) {
109 if (handler->subsys != subsys)
110 continue;
111 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
112 }
113}
Harald Welteaf8e4352011-08-17 16:19:46 +0200114
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200115/*! @} */