46401fba9109be8c16f19a943fa281fa76ef7d23
[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-manager-gdbus.h"
22 #include "stc-db.h"
23 #include "counter.h"
24 #include "table-restrictions.h"
25 #include "helper-cgroup.h"
26 #include "helper-nfacct-rule.h"
27 #include "helper-iptables.h"
28 #include "helper-inotify.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 #include "stc-manager-plugin-tether.h"
35 #include "stc-manager-plugin-pcap.h"
36
37 #define BUF_SIZE_FOR_ERR 100
38
39 static stc_s *g_stc = NULL;
40
41 static gboolean __validate_ident(const char *ident)
42 {
43         unsigned int i;
44
45         if (!ident)
46                 return FALSE;
47
48         for (i = 0; i < strlen(ident); ++i)
49                 if (!g_ascii_isprint(ident[i]))
50                         return FALSE;
51
52         return TRUE;
53 }
54
55 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
56 {
57         if (!ident)
58                 return;
59
60         if (!__validate_ident(ident)) {
61                 STC_LOGE("Invalid ident [%s]", ident);
62                 return;
63         }
64
65         if (!g_strcmp0(ident, INFO_CONFIG)) {
66                 int debug = stc_util_get_config_int(INFO_DEBUGLOG);
67                 stc_util_set_debuglog(debug);
68         }
69 }
70
71 static void __stc_manager_deinit(void)
72 {
73         __STC_LOG_FUNC_ENTER__;
74
75         if (!g_stc) {
76                 STC_LOGE("Memory for manager structure is not allocated");
77                 return;
78         }
79
80         stc_monitor_deinit();
81         stc_deinit_db_guard();
82         stc_db_deinitialize();
83
84         iptables_flush_chains();
85         iptables_deinit();
86
87         stc_manager_gdbus_deinit((gpointer)g_stc);
88
89         stc_firewall_deinit();
90
91         stc_plugin_appstatus_deinit();
92         stc_plugin_exception_deinit();
93         stc_plugin_procfs_deinit();
94         stc_plugin_tether_deinit();
95         stc_plugin_pcap_deinit();
96
97         inotify_deregister(INFO_STORAGE_DIR);
98         inotify_deinitialize();
99
100         STC_LOGI("stc manager deinitialized");
101         FREE(g_stc);
102         __STC_LOG_FUNC_EXIT__;
103 }
104
105 static stc_s *__stc_manager_init(void)
106 {
107         __STC_LOG_FUNC_ENTER__;
108         stc_s *stc;
109         stc_error_e err = STC_ERROR_NONE;
110
111         stc = MALLOC0(stc_s, 1);
112         if (!stc) {
113                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
114                 return NULL; //LCOV_EXCL_LINE
115         }
116         g_stc = stc;
117
118         stc_util_initialize_config();
119
120         inotify_initialize();
121         inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
122
123 #ifndef TIZEN_TV_EXT
124         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
125 #endif
126
127         err = stc_db_initialize();
128         if (err != STC_ERROR_NONE) {
129                 STC_LOGD("Failed to initialize stc db"); //LCOV_EXCL_LINE
130                 return NULL; //LCOV_EXCL_LINE
131         }
132
133 #ifndef TIZEN_TV_EXT
134         stc_plugin_appstatus_init();
135         stc_plugin_exception_init();
136         stc_plugin_procfs_init();
137         stc_plugin_tether_init();
138         stc_plugin_pcap_init();
139 #endif
140
141         stc_firewall_init();
142
143 #ifndef TIZEN_TV_EXT
144         err = stc_monitor_init();
145         if (err != STC_ERROR_NONE) {
146                 STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
147                 return NULL; //LCOV_EXCL_LINE
148         }
149
150         stc_plugin_procfs_load_pid();
151 #endif
152
153         stc_manager_gdbus_init((gpointer)stc);
154
155         STC_LOGI("stc manager initialized");
156         __STC_LOG_FUNC_EXIT__;
157         return stc;
158 }
159
160 stc_s *stc_get_manager(void)
161 {
162         return g_stc;
163 }
164
165 void stc_stop_manager(void)
166 {
167         if (g_stc && g_stc->main_loop)
168                 g_main_loop_quit(g_stc->main_loop);
169 }
170
171 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
172 {
173         pid_t pid = 0;
174         int status = 0;
175         int ret = 0;
176         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
177         gchar **args = NULL;
178
179         if (cmd == NULL) {
180                 STC_LOGE("Invalid arguments");
181                 return STC_ERROR_INVALID_PARAMETER;
182         }
183
184         args = g_strsplit_set(cmd, " ", -1);
185
186         errno = 0;
187         pid = fork();
188
189         if (pid == 0) {
190                 errno = 0;
191
192                 if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
193                         STC_LOGE("Invalid arguments");
194                         g_strfreev(args);
195                         exit(-1);
196                 }
197
198                 if (execv(args[0], args) == -1) {
199                         STC_LOGE("Failed to execute [%s]", *err_str);
200                         g_strfreev(args);
201                         exit(-1);
202                 }
203         } else if (pid > 0) {
204                 if (waitpid(pid, &status, 0) == -1)
205                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
206
207                 if (WIFEXITED(status)) {
208                         ret = WEXITSTATUS(status);
209                         STC_LOGD("exited, status [%d]", status);
210                 } else if (WIFSIGNALED(status)) {
211                         STC_LOGD("killed by signal [%d]", WTERMSIG(status));
212                 } else if (WIFSTOPPED(status)) {
213                         STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
214                 } else if (WIFCONTINUED(status)) {
215                         STC_LOGD("continued");
216                 }
217
218                 *err_num = ret;
219                 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
220                 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
221
222                 g_strfreev(args);
223                 if (ret == 0)
224                         return STC_ERROR_NONE;
225                 else
226                         return STC_ERROR_FAIL;
227         }
228
229         *err_num = errno;
230         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
231         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
232
233         g_strfreev(args);
234         return STC_ERROR_FAIL;
235 }
236
237 gint32 main(gint32 argc, gchar *argv[])
238 {
239         GMainLoop *main_loop = NULL;
240         gint32 ret = -1;
241
242         STC_LOGI("Smart Traffic Control Manager");
243
244 #ifdef TIZEN_GTESTS
245         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
246 #endif
247
248         if (daemon(0, 0) != 0)
249                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
250
251         /* Initialize required subsystems */
252 #if !GLIB_CHECK_VERSION(2, 35, 0)
253         g_type_init();
254 #endif
255
256         /* Crate the GLIB main loop */
257         main_loop = g_main_loop_new(NULL, FALSE);
258
259         g_stc = __stc_manager_init();
260         if (!g_stc)
261                 goto fail;
262
263         g_stc->main_loop = main_loop;
264
265         /* Run the main loop */
266         g_main_loop_run(main_loop);
267
268         ret = 0;
269
270 fail:
271         __stc_manager_deinit();
272
273         if (main_loop)
274                 g_main_loop_unref(main_loop);
275
276         return ret;
277 }