blob: bc339bb98949051600363d6d11fe27c3d0d79cf3 [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
28void *tall_sigh_ctx;
29static LLIST_HEAD(signal_handler_list);
30
31struct signal_handler {
32 struct llist_head entry;
33 unsigned int subsys;
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020034 osmo_signal_cbfn *cbfn;
Harald Welteec8b4502010-02-20 20:34:29 +010035 void *data;
36};
37
38
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020039int osmo_signal_register_handler(unsigned int subsys,
40 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010041{
42 struct signal_handler *sig_data;
43
44 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
45 if (!sig_data)
46 return -ENOMEM;
47
48 memset(sig_data, 0, sizeof(*sig_data));
49
50 sig_data->subsys = subsys;
51 sig_data->data = data;
52 sig_data->cbfn = cbfn;
53
54 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
55
56 llist_add_tail(&sig_data->entry, &signal_handler_list);
57
58 return 0;
59}
60
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020061void osmo_signal_unregister_handler(unsigned int subsys,
62 osmo_signal_cbfn *cbfn, void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010063{
64 struct signal_handler *handler;
65
66 llist_for_each_entry(handler, &signal_handler_list, entry) {
67 if (handler->cbfn == cbfn && handler->data == data
68 && subsys == handler->subsys) {
69 llist_del(&handler->entry);
70 talloc_free(handler);
71 break;
72 }
73 }
74}
75
76
Pablo Neira Ayusoa10dd352011-05-07 12:42:45 +020077void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
78 void *signal_data)
Harald Welteec8b4502010-02-20 20:34:29 +010079{
80 struct signal_handler *handler;
81
82 llist_for_each_entry(handler, &signal_handler_list, entry) {
83 if (handler->subsys != subsys)
84 continue;
85 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
86 }
87}