blob: c7ca86c487cd8b17ed7d8d0feb973f83e0e5ca7b [file] [log] [blame]
Holger Freyther2b2d2e32009-02-14 22:51:00 +00001/* 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
Harald Weltedfe6c7d2010-02-20 16:24:02 +010021#include <osmocore/signal.h>
22#include <osmocore/talloc.h>
23#include <osmocore/linuxlist.h>
Harald Welte12247c62009-05-21 07:23:02 +000024#include <stdlib.h>
Holger Freyther2b2d2e32009-02-14 22:51:00 +000025#include <string.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010026#include <errno.h>
Holger Freyther2b2d2e32009-02-14 22:51:00 +000027
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020028void *tall_sigh_ctx;
Holger Freyther2b2d2e32009-02-14 22:51:00 +000029static LLIST_HEAD(signal_handler_list);
30
31struct signal_handler {
32 struct llist_head entry;
Harald Welte595ad7b2009-02-16 22:05:44 +000033 unsigned int subsys;
34 signal_cbfn *cbfn;
Holger Freyther2b2d2e32009-02-14 22:51:00 +000035 void *data;
36};
37
38
Harald Welte595ad7b2009-02-16 22:05:44 +000039int register_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data)
Holger Freyther2b2d2e32009-02-14 22:51:00 +000040{
Harald Welte2cf161b2009-06-20 22:36:41 +020041 struct signal_handler *sig_data;
Harald Welte595ad7b2009-02-16 22:05:44 +000042
Harald Welte2cf161b2009-06-20 22:36:41 +020043 sig_data = talloc(tall_sigh_ctx, struct signal_handler);
Harald Welte595ad7b2009-02-16 22:05:44 +000044 if (!sig_data)
45 return -ENOMEM;
46
Holger Freyther2b2d2e32009-02-14 22:51:00 +000047 memset(sig_data, 0, sizeof(*sig_data));
48
Harald Welte595ad7b2009-02-16 22:05:44 +000049 sig_data->subsys = subsys;
Holger Freyther2b2d2e32009-02-14 22:51:00 +000050 sig_data->data = data;
Harald Welte595ad7b2009-02-16 22:05:44 +000051 sig_data->cbfn = cbfn;
52
53 /* FIXME: check if we already have a handler for this subsys/cbfn/data */
54
Holger Freyther36b5d652009-02-14 23:35:06 +000055 llist_add_tail(&sig_data->entry, &signal_handler_list);
Harald Welte595ad7b2009-02-16 22:05:44 +000056
57 return 0;
Holger Freyther2b2d2e32009-02-14 22:51:00 +000058}
59
Harald Welte595ad7b2009-02-16 22:05:44 +000060void unregister_signal_handler(unsigned int subsys, signal_cbfn *cbfn, void *data)
Holger Freyther2b2d2e32009-02-14 22:51:00 +000061{
62 struct signal_handler *handler;
63
64 llist_for_each_entry(handler, &signal_handler_list, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +000065 if (handler->cbfn == cbfn && handler->data == data
66 && subsys == handler->subsys) {
Holger Freyther2b2d2e32009-02-14 22:51:00 +000067 llist_del(&handler->entry);
Harald Welte2cf161b2009-06-20 22:36:41 +020068 talloc_free(handler);
Holger Freyther2b2d2e32009-02-14 22:51:00 +000069 break;
70 }
71 }
72}
73
74
Harald Welte595ad7b2009-02-16 22:05:44 +000075void dispatch_signal(unsigned int subsys, unsigned int signal, void *signal_data)
Holger Freyther2b2d2e32009-02-14 22:51:00 +000076{
77 struct signal_handler *handler;
78
79 llist_for_each_entry(handler, &signal_handler_list, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +000080 if (handler->subsys != subsys)
81 continue;
82 (*handler->cbfn)(subsys, signal, handler->data, signal_data);
Holger Freyther2b2d2e32009-02-14 22:51:00 +000083 }
84}