6af6b7b3d3b898d5a1d54f05dbd7ab12d80526a4
[platform/core/connectivity/stc-manager.git] / src / stc-manager.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "stc-manager.h"
18 #include "stc-emulator.h"
19 #include "stc-statistics.h"
20 #include "stc-restriction.h"
21 #include "stc-manager-gdbus.h"
22 #include "stc-db.h"
23 #include "counter.h"
24 #include "table-restrictions.h"
25 #include "helper-cgroup.h"
26 #include "helper-nfacct-rule.h"
27 #include "stc-monitor.h"
28 #include "stc-manager-plugin.h"
29
30 static stc_s *g_stc = NULL;
31
32 static void __stc_manager_deinit(void)
33 {
34         __STC_LOG_FUNC_ENTER__;
35
36         if (!g_stc) {
37                 STC_LOGE("Memory for manager structure is not allocated");
38                 return;
39         }
40
41         stc_monitor_deinit();
42         stc_deinit_db_guard();
43         stc_db_deinitialize();
44         stc_manager_gdbus_deinit((gpointer)g_stc);
45         stc_manager_plugin_deinit();
46
47         STC_LOGI("stc manager deinitialized");
48         FREE(g_stc);
49         __STC_LOG_FUNC_EXIT__;
50 }
51
52 static stc_s *__stc_manager_init(void)
53 {
54         __STC_LOG_FUNC_ENTER__;
55         stc_s *stc;
56
57         stc = MALLOC0(stc_s, 1);
58         if (!stc) {
59                 STC_LOGE("Failed to allocate memory for manager structure");
60                 return NULL;
61         }
62         g_stc = stc;
63
64         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
65
66         EXEC(STC_ERROR_NONE, stc_db_initialize());
67
68         stc_monitor_init();
69         stc_manager_gdbus_init((gpointer)stc);
70         stc_manager_plugin_init();
71
72         STC_LOGI("stc manager initialized");
73         __STC_LOG_FUNC_EXIT__;
74         return stc;
75
76 handle_error:
77         STC_LOGD("Failed to initialize stc manager");
78         __stc_manager_deinit();
79         return NULL;
80 }
81
82 stc_s *stc_get_manager(void)
83 {
84         return g_stc;
85 }
86
87 gint32 main(gint32 argc, gchar *argv[])
88 {
89         GMainLoop *main_loop = NULL;
90         gint32 ret = -1;
91
92         STC_LOGI("Smart Traffic Control Manager");
93
94         if (daemon(0, 0) != 0)
95                 STC_LOGE("Can't start daemon");
96
97         /* Initialize required subsystems */
98 #if !GLIB_CHECK_VERSION(2, 35, 0)
99         g_type_init();
100 #endif
101
102         /* Crate the GLIB main loop */
103         main_loop = g_main_loop_new(NULL, FALSE);
104
105         stc_emulator_check_environment();
106         if (stc_emulator_is_emulated() == FALSE) {
107                 g_stc = __stc_manager_init();
108                 if (!g_stc)
109                         goto fail;
110                 g_stc->main_loop = main_loop;
111         }
112
113         /* Run the main loop */
114         g_main_loop_run(main_loop);
115
116         ret = 0;
117
118 fail:
119         if (stc_emulator_is_emulated() == FALSE)
120                 __stc_manager_deinit();
121
122         if (main_loop)
123                 g_main_loop_unref(main_loop);
124
125         return ret;
126 }