Add initial source codes for gtest
[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 "helper-iptables.h"
27 #include "stc-monitor.h"
28 #include "stc-manager-plugin.h"
29 #include "stc-app-lifecycle.h"
30
31 static stc_s *g_stc = NULL;
32
33 /*
34 static gboolean __validate_ident(const char *ident)
35 {
36         unsigned int i;
37
38         if (!ident)
39                 return FALSE;
40
41         for (i = 0; i < strlen(ident); ++i)
42                 if (!g_ascii_isprint(ident[i]))
43                         return FALSE;
44
45         return TRUE;
46 }
47 */
48
49 static void __stc_manager_deinit(void)
50 {
51         __STC_LOG_FUNC_ENTER__;
52
53         if (!g_stc) {
54                 STC_LOGE("Memory for manager structure is not allocated");
55                 return;
56         }
57
58         stc_monitor_deinit();
59         stc_deinit_db_guard();
60         stc_db_deinitialize();
61
62         iptables_flush_chains();
63         iptables_deinit();
64         stc_manager_gdbus_deinit((gpointer)g_stc);
65         stc_app_lifecycle_monitor_deinit();
66         stc_manager_plugin_deinit();
67
68         STC_LOGI("stc manager deinitialized");
69         FREE(g_stc);
70         __STC_LOG_FUNC_EXIT__;
71 }
72
73 static stc_s *__stc_manager_init(void)
74 {
75         __STC_LOG_FUNC_ENTER__;
76         stc_s *stc;
77         stc_error_e err = STC_ERROR_NONE;
78
79         stc = MALLOC0(stc_s, 1);
80         if (!stc) {
81                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
82                 return NULL; //LCOV_EXCL_LINE
83         }
84         g_stc = stc;
85
86         stc_util_initialize_config();
87
88         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
89
90         EXEC(STC_ERROR_NONE, stc_db_initialize());
91
92         err = stc_monitor_init();
93         if (err != STC_ERROR_NONE)
94                 goto handle_error;
95
96         stc_manager_plugin_init();
97         stc_app_lifecycle_monitor_init();
98         stc_manager_gdbus_init((gpointer)stc);
99
100         STC_LOGI("stc manager initialized");
101         __STC_LOG_FUNC_EXIT__;
102         return stc;
103
104 handle_error:
105         STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
106         __stc_manager_deinit(); //LCOV_EXCL_LINE
107         return NULL; //LCOV_EXCL_LINE
108 }
109
110 stc_s *stc_get_manager(void)
111 {
112         return g_stc;
113 }
114
115 void stc_stop_manager(void)
116 {
117         if (g_stc && g_stc->main_loop)
118                 g_main_loop_quit(g_stc->main_loop);
119 }
120
121 gint32 main(gint32 argc, gchar *argv[])
122 {
123         GMainLoop *main_loop = NULL;
124         gint32 ret = -1;
125
126         STC_LOGI("Smart Traffic Control Manager");
127
128 #ifdef TIZEN_GTESTS
129         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
130 #endif
131
132         if (daemon(0, 0) != 0)
133                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
134
135         /* Initialize required subsystems */
136 #if !GLIB_CHECK_VERSION(2, 35, 0)
137         g_type_init();
138 #endif
139
140         /* Crate the GLIB main loop */
141         main_loop = g_main_loop_new(NULL, FALSE);
142
143         stc_emulator_check_environment();
144         if (stc_emulator_is_emulated() == FALSE) {
145                 g_stc = __stc_manager_init();
146                 if (!g_stc)
147                         goto fail;
148                 g_stc->main_loop = main_loop;
149         }
150
151         /* Run the main loop */
152         g_main_loop_run(main_loop);
153
154         ret = 0;
155
156 fail:
157         if (stc_emulator_is_emulated() == FALSE)
158                 __stc_manager_deinit();
159
160         if (main_loop)
161                 g_main_loop_unref(main_loop);
162
163         return ret;
164 }