Merge "Fixed a svace issue for 293218" into tizen_4.0
[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 <signal.h>
18 #include "stc-manager.h"
19 #include "stc-emulator.h"
20 #include "stc-manager-gdbus.h"
21 #include "stc-db.h"
22 #include "counter.h"
23 #include "table-restrictions.h"
24 #include "helper-cgroup.h"
25 #include "helper-nfacct-rule.h"
26 #include "stc-monitor.h"
27 #include "stc-manager-plugin.h"
28 #include "stc-app-lifecycle.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
45         stc_manager_gdbus_deinit((gpointer)g_stc);
46         stc_app_lifecycle_monitor_deinit();
47         stc_manager_plugin_deinit();
48
49         STC_LOGI("stc manager deinitialized");
50         FREE(g_stc);
51         __STC_LOG_FUNC_EXIT__;
52 }
53
54 static stc_s *__stc_manager_init(void)
55 {
56         __STC_LOG_FUNC_ENTER__;
57         stc_s *stc;
58         stc_error_e err = STC_ERROR_NONE;
59
60         stc = MALLOC0(stc_s, 1);
61         if (!stc) {
62                 STC_LOGE("Failed to allocate memory for manager structure");
63                 return NULL;
64         }
65         g_stc = stc;
66
67         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
68
69         EXEC(STC_ERROR_NONE, stc_db_initialize());
70
71         err = stc_monitor_init();
72         if (err != STC_ERROR_NONE)
73                 goto handle_error;
74
75         stc_manager_plugin_init();
76         stc_app_lifecycle_monitor_init();
77         stc_manager_gdbus_init((gpointer)stc);
78
79         STC_LOGI("stc manager initialized");
80         __STC_LOG_FUNC_EXIT__;
81         return stc;
82
83 handle_error:
84         STC_LOGD("Failed to initialize stc manager");
85         __stc_manager_deinit();
86         return NULL;
87 }
88
89 stc_s *stc_get_manager(void)
90 {
91         return g_stc;
92 }
93
94 gint32 main(gint32 argc, gchar *argv[])
95 {
96         GMainLoop *main_loop = NULL;
97         gint32 ret = -1;
98
99         STC_LOGI("Smart Traffic Control Manager");
100
101         if (daemon(0, 0) != 0)
102                 STC_LOGE("Can't start daemon");
103
104         /* Initialize required subsystems */
105 #if !GLIB_CHECK_VERSION(2, 35, 0)
106         g_type_init();
107 #endif
108
109         /* Crate the GLIB main loop */
110         main_loop = g_main_loop_new(NULL, FALSE);
111
112         stc_emulator_check_environment();
113         if (stc_emulator_is_emulated() == FALSE) {
114                 g_stc = __stc_manager_init();
115                 if (!g_stc)
116                         goto fail;
117                 g_stc->main_loop = main_loop;
118         }
119
120         /* Run the main loop */
121         g_main_loop_run(main_loop);
122
123         ret = 0;
124
125 fail:
126         if (stc_emulator_is_emulated() == FALSE)
127                 __stc_manager_deinit();
128
129         if (main_loop)
130                 g_main_loop_unref(main_loop);
131
132         return ret;
133 }