git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / main / manager_system.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jason Parker <jparker@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief System AMI event handling
22  *
23  * \author Jason Parker <jparker@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_REGISTER_FILE()
29
30 #include "asterisk/stasis.h"
31 #include "asterisk/stasis_message_router.h"
32 #include "asterisk/stasis_system.h"
33
34 /*! \brief The \ref stasis subscription returned by the forwarding of the system topic
35  * to the manager topic
36  */
37 static struct stasis_forward *topic_forwarder;
38
39 static void manager_system_shutdown(void)
40 {
41         stasis_forward_cancel(topic_forwarder);
42         topic_forwarder = NULL;
43 }
44
45 int manager_system_init(void)
46 {
47         int ret = 0;
48         struct stasis_topic *manager_topic;
49         struct stasis_topic *system_topic;
50         struct stasis_message_router *message_router;
51
52         manager_topic = ast_manager_get_topic();
53         if (!manager_topic) {
54                 return -1;
55         }
56         message_router = ast_manager_get_message_router();
57         if (!message_router) {
58                 return -1;
59         }
60         system_topic = ast_system_topic();
61         if (!system_topic) {
62                 return -1;
63         }
64
65         topic_forwarder = stasis_forward_all(system_topic, manager_topic);
66         if (!topic_forwarder) {
67                 return -1;
68         }
69
70         ast_register_cleanup(manager_system_shutdown);
71
72         /* If somehow we failed to add any routes, just shut down the whole
73          * thing and fail it.
74          */
75         if (ret) {
76                 manager_system_shutdown();
77                 return -1;
78         }
79
80         return 0;
81 }