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