misc: Put git-version-gen into the tarball
[osmocom-bb.git] / src / signal.c
1 /* 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
21 #include <osmocom/core/signal.h>
22 #include <osmocom/core/talloc.h>
23 #include <osmocom/core/linuxlist.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 void *tall_sigh_ctx;
29 static LLIST_HEAD(signal_handler_list);
30
31 struct signal_handler {
32         struct llist_head entry;
33         unsigned int subsys;
34         osmo_signal_cbfn *cbfn;
35         void *data;
36 };
37
38
39 int osmo_signal_register_handler(unsigned int subsys,
40                                  osmo_signal_cbfn *cbfn, void *data)
41 {
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
61 void osmo_signal_unregister_handler(unsigned int subsys,
62                                     osmo_signal_cbfn *cbfn, void *data)
63 {
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
77 void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
78                           void *signal_data)
79 {
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 }