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