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