799839e59341c6658ab7fec794256dd3331f2916
[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 <errno.h>
19 #include <sys/wait.h>
20 #include "stc-manager.h"
21 #include "stc-emulator.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 "helper-iptables.h"
29 #include "stc-monitor.h"
30 #include "stc-firewall.h"
31 #include "stc-manager-plugin-appstatus.h"
32 #include "stc-manager-plugin-exception.h"
33 #include "stc-manager-plugin-procfs.h"
34
35 #define BUF_SIZE_FOR_ERR 100
36
37 static stc_s *g_stc = NULL;
38
39 /*
40 static gboolean __validate_ident(const char *ident)
41 {
42         unsigned int i;
43
44         if (!ident)
45                 return FALSE;
46
47         for (i = 0; i < strlen(ident); ++i)
48                 if (!g_ascii_isprint(ident[i]))
49                         return FALSE;
50
51         return TRUE;
52 }
53 */
54
55 static void __stc_manager_deinit(void)
56 {
57         __STC_LOG_FUNC_ENTER__;
58
59         if (!g_stc) {
60                 STC_LOGE("Memory for manager structure is not allocated");
61                 return;
62         }
63
64         stc_monitor_deinit();
65         stc_deinit_db_guard();
66         stc_db_deinitialize();
67
68         iptables_flush_chains();
69         iptables_deinit();
70
71         stc_manager_gdbus_deinit((gpointer)g_stc);
72
73         stc_firewall_deinit();
74
75         stc_plugin_appstatus_deinit();
76         stc_plugin_exception_deinit();
77         stc_plugin_procfs_deinit();
78
79         STC_LOGI("stc manager deinitialized");
80         FREE(g_stc);
81         __STC_LOG_FUNC_EXIT__;
82 }
83
84 static stc_s *__stc_manager_init(void)
85 {
86         __STC_LOG_FUNC_ENTER__;
87         stc_s *stc;
88         stc_error_e err = STC_ERROR_NONE;
89
90         stc = MALLOC0(stc_s, 1);
91         if (!stc) {
92                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
93                 return NULL; //LCOV_EXCL_LINE
94         }
95         g_stc = stc;
96
97         stc_util_initialize_config();
98
99         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
100
101         EXEC(STC_ERROR_NONE, stc_db_initialize());
102
103         stc_plugin_appstatus_init();
104         stc_plugin_exception_init();
105         stc_plugin_procfs_init();
106
107         stc_firewall_init();
108
109         err = stc_monitor_init();
110         if (err != STC_ERROR_NONE)
111                 goto handle_error;
112
113         stc_manager_gdbus_init((gpointer)stc);
114
115         STC_LOGI("stc manager initialized");
116         __STC_LOG_FUNC_EXIT__;
117         return stc;
118
119 handle_error:
120         STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
121         __stc_manager_deinit(); //LCOV_EXCL_LINE
122         return NULL; //LCOV_EXCL_LINE
123 }
124
125 stc_s *stc_get_manager(void)
126 {
127         return g_stc;
128 }
129
130 void stc_stop_manager(void)
131 {
132         if (g_stc && g_stc->main_loop)
133                 g_main_loop_quit(g_stc->main_loop);
134 }
135
136 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
137 {
138         pid_t pid = 0;
139         int status = 0;
140         int ret = 0;
141         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
142         gchar **args = NULL;
143
144         if (cmd == NULL) {
145                 STC_LOGE("Invalid arguments");
146                 return STC_ERROR_INVALID_PARAMETER;
147         }
148
149         args = g_strsplit_set(cmd, " ", -1);
150
151         errno = 0;
152         pid = fork();
153
154         if (pid == 0) {
155                 errno = 0;
156                 if (execv(args[0], args) == -1) {
157                         STC_LOGE("Failed to execute [%s]", err_str);
158                         g_strfreev(args);
159                         exit(-1);
160                 }
161         } else if (pid > 0) {
162                 if (waitpid(pid, &status, 0) == -1)
163                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
164
165                 if (WIFEXITED(status)) {
166                         ret = WEXITSTATUS(status);
167                         STC_LOGD("exited, status [%d]", status);
168                 } else if (WIFSIGNALED(status)) {
169                         STC_LOGD("killed by signal [%d]", WTERMSIG(status));
170                 } else if (WIFSTOPPED(status)) {
171                         STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
172                 } else if (WIFCONTINUED(status)) {
173                         STC_LOGD("continued");
174                 }
175
176                 *err_num = ret;
177                 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
178                 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
179
180                 g_strfreev(args);
181                 if (ret == 0)
182                         return STC_ERROR_NONE;
183                 else
184                         return STC_ERROR_FAIL;
185         }
186
187         *err_num = errno;
188         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
189         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
190
191         g_strfreev(args);
192         return STC_ERROR_FAIL;
193 }
194
195 gint32 main(gint32 argc, gchar *argv[])
196 {
197         GMainLoop *main_loop = NULL;
198         gint32 ret = -1;
199
200         STC_LOGI("Smart Traffic Control Manager");
201
202 #ifdef TIZEN_GTESTS
203         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
204 #endif
205
206         if (daemon(0, 0) != 0)
207                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
208
209         /* Initialize required subsystems */
210 #if !GLIB_CHECK_VERSION(2, 35, 0)
211         g_type_init();
212 #endif
213
214         /* Crate the GLIB main loop */
215         main_loop = g_main_loop_new(NULL, FALSE);
216
217         stc_emulator_check_environment();
218         if (stc_emulator_is_emulated() == FALSE) {
219                 g_stc = __stc_manager_init();
220                 if (!g_stc)
221                         goto fail;
222                 g_stc->main_loop = main_loop;
223         }
224
225         /* Run the main loop */
226         g_main_loop_run(main_loop);
227
228         ret = 0;
229
230 fail:
231         if (stc_emulator_is_emulated() == FALSE)
232                 __stc_manager_deinit();
233
234         if (main_loop)
235                 g_main_loop_unref(main_loop);
236
237         return ret;
238 }