Merge "Return errors to caller" into tizen_5.5
[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-firewall.h"
30 #include "stc-manager-plugin-appstatus.h"
31 #include "stc-manager-plugin-exception.h"
32 #include "stc-manager-plugin-procfs.h"
33 #include "stc-manager-plugin-tether.h"
34 #include "stc-manager-plugin-pcap.h"
35 #include "stc-manager-plugin-monitor.h"
36 #include "stc-manager-plugin-firewall.h"
37
38 #define BUF_SIZE_FOR_ERR 100
39
40 static stc_s *g_stc = NULL;
41 static gboolean g_is_fail_exit = FALSE;
42
43 static gboolean __validate_ident(const char *ident)
44 {
45         unsigned int i;
46
47         if (!ident)
48                 return FALSE;
49
50         for (i = 0; i < strlen(ident); ++i)
51                 if (!g_ascii_isprint(ident[i]))
52                         return FALSE;
53
54         return TRUE;
55 }
56
57 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
58 {
59         if (!ident)
60                 return;
61
62         if (!__validate_ident(ident)) {
63                 STC_LOGE("Invalid ident [%s]", ident);
64                 return;
65         }
66
67         if (!g_strcmp0(ident, INFO_CONFIG))
68                 stc_util_update_log_state();
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_deinit_db_guard();
81         stc_db_deinitialize();
82
83         iptables_flush_chains();
84         iptables_deinit();
85
86         stc_manager_gdbus_deinit((gpointer)g_stc);
87
88         stc_plugin_appstatus_deinit();
89         stc_plugin_exception_deinit();
90         stc_plugin_procfs_deinit();
91         stc_plugin_tether_deinit();
92         stc_plugin_pcap_deinit();
93         stc_plugin_monitor_deinit();
94         stc_plugin_firewall_deinit();
95
96         inotify_deregister(INFO_STORAGE_DIR);
97         inotify_deinitialize();
98
99         STC_LOGI("stc manager deinitialized");
100         FREE(g_stc);
101         __STC_LOG_FUNC_EXIT__;
102 }
103
104 void __stc_manager_stop_with_fail(void)
105 {
106         STC_LOGI("plugin needs stc-manager to exit");
107
108         g_is_fail_exit = TRUE;
109
110         stc_stop_manager();
111 }
112
113 static stc_s *__stc_manager_init(void)
114 {
115         __STC_LOG_FUNC_ENTER__;
116         stc_s *stc;
117         stc_error_e err = STC_ERROR_NONE;
118
119         stc = MALLOC0(stc_s, 1);
120         if (!stc) {
121                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
122                 return NULL; //LCOV_EXCL_LINE
123         }
124         g_stc = stc;
125
126         stc_util_initialize_config();
127
128         inotify_initialize();
129         inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
130
131         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
132
133         err = stc_db_initialize();
134         if (err != STC_ERROR_NONE) {
135                 STC_LOGD("Failed to initialize stc db"); //LCOV_EXCL_LINE
136                 return NULL; //LCOV_EXCL_LINE
137         }
138
139         g_stc->ondemand_mode = TRUE;
140
141         stc_plugin_appstatus_init();
142         stc_plugin_exception_init();
143         stc_plugin_procfs_init();
144         stc_plugin_tether_init();
145         if (stc_plugin_pcap_init() == STC_ERROR_NONE)
146                 g_stc->ondemand_mode = FALSE;
147         if (stc_plugin_monitor_init(__stc_manager_stop_with_fail) == STC_ERROR_NONE)
148                 g_stc->ondemand_mode = FALSE;
149         stc_plugin_firewall_init();
150
151         stc_plugin_procfs_load_pid();
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 static gboolean __stc_timer_expired(gpointer data)
161 {
162         if (g_stc->keep_alive) {
163                 g_stc->keep_alive = FALSE;
164                 return TRUE;
165         }
166
167         g_main_loop_quit(g_stc->main_loop);
168
169         return FALSE;
170 }
171
172 API stc_s *stc_get_manager(void)
173 {
174         return g_stc;
175 }
176
177 void stc_stop_manager(void)
178 {
179         if (g_stc && g_stc->main_loop)
180                 g_main_loop_quit(g_stc->main_loop);
181 }
182
183 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
184 {
185         pid_t pid = 0;
186         int status = 0;
187         int ret = 0;
188         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
189         gchar **args = NULL;
190
191         if (cmd == NULL) {
192                 STC_LOGE("Invalid arguments");
193                 return STC_ERROR_INVALID_PARAMETER;
194         }
195
196         args = g_strsplit_set(cmd, " ", -1);
197
198         errno = 0;
199         pid = fork();
200
201         if (pid == 0) {
202                 errno = 0;
203
204                 if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
205                         STC_LOGE("Invalid arguments");
206                         g_strfreev(args);
207                         exit(-1);
208                 }
209
210                 if (execv(args[0], args) == -1) {
211                         STC_LOGE("Failed to execute [%s]", *err_str);
212                         g_strfreev(args);
213                         exit(-1);
214                 }
215         } else if (pid > 0) {
216                 if (waitpid(pid, &status, 0) == -1)
217                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
218
219                 if (WIFEXITED(status)) {
220                         ret = WEXITSTATUS(status);
221                         STC_LOGD("exited, status [%d]", status);
222                 } else if (WIFSIGNALED(status)) {
223                         STC_LOGD("killed by signal [%d]", WTERMSIG(status));
224                 } else if (WIFSTOPPED(status)) {
225                         STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
226                 } else if (WIFCONTINUED(status)) {
227                         STC_LOGD("continued");
228                 }
229
230                 *err_num = ret;
231                 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
232                 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
233
234                 g_strfreev(args);
235                 if (ret == 0)
236                         return STC_ERROR_NONE;
237                 else
238                         return STC_ERROR_FAIL;
239         }
240
241         *err_num = errno;
242         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
243         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
244
245         g_strfreev(args);
246         return STC_ERROR_FAIL;
247 }
248
249 void stc_set_keep_alive(gboolean keep_alive)
250 {
251         g_stc->keep_alive = keep_alive;
252 }
253
254 gint32 main(gint32 argc, gchar *argv[])
255 {
256         GMainLoop *main_loop = NULL;
257
258         STC_LOGI("Smart Traffic Control Manager");
259
260 #ifdef TIZEN_GTESTS
261         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
262 #endif
263
264 /*
265         if (daemon(0, 0) != 0)
266                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
267 */
268
269         /* Initialize required subsystems */
270 #if !GLIB_CHECK_VERSION(2, 35, 0)
271         g_type_init();
272 #endif
273
274         g_stc = __stc_manager_init();
275         if (!g_stc)
276                 goto fail;
277
278         if (g_is_fail_exit == TRUE)
279                 goto fail;
280
281         /* Crate the GLIB main loop */
282         main_loop = g_main_loop_new(NULL, FALSE);
283         g_stc->main_loop = main_loop;
284
285         if (g_stc->ondemand_mode) {
286                 g_stc->timer = g_timeout_add_seconds(10, __stc_timer_expired, NULL);
287                 g_stc->keep_alive = FALSE;
288         }
289
290         /* Run the main loop */
291         g_main_loop_run(main_loop);
292
293 fail:
294         __stc_manager_deinit();
295
296         if (main_loop)
297                 g_main_loop_unref(main_loop);
298
299         if (g_is_fail_exit == TRUE)
300                 exit(-1);
301
302         return 0;
303 }