58f65318e10da6f845ee0a577301a4fbad139954
[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
36 #define BUF_SIZE_FOR_ERR 100
37
38 static stc_s *g_stc = NULL;
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 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
55 {
56         if (!ident)
57                 return;
58
59         if (!__validate_ident(ident)) {
60                 STC_LOGE("Invalid ident [%s]", ident);
61                 return;
62         }
63
64         if (!g_strcmp0(ident, INFO_CONFIG)) {
65                 int debug = stc_util_get_config_int(INFO_DEBUGLOG);
66                 stc_util_set_debuglog(debug);
67         }
68 }
69
70 static void __stc_manager_deinit(void)
71 {
72         __STC_LOG_FUNC_ENTER__;
73
74         if (!g_stc) {
75                 STC_LOGE("Memory for manager structure is not allocated");
76                 return;
77         }
78
79         stc_monitor_deinit();
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_firewall_deinit();
89
90         stc_plugin_appstatus_deinit();
91         stc_plugin_exception_deinit();
92         stc_plugin_procfs_deinit();
93         stc_plugin_tether_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         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
122
123         EXEC(STC_ERROR_NONE, stc_db_initialize());
124
125         stc_plugin_appstatus_init();
126         stc_plugin_exception_init();
127         stc_plugin_procfs_init();
128         stc_plugin_tether_init();
129
130         stc_firewall_init();
131
132         err = stc_monitor_init();
133         if (err != STC_ERROR_NONE)
134                 goto handle_error;
135
136         stc_plugin_procfs_load_pid();
137         stc_manager_gdbus_init((gpointer)stc);
138
139         STC_LOGI("stc manager initialized");
140         __STC_LOG_FUNC_EXIT__;
141         return stc;
142
143 handle_error:
144         STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
145         __stc_manager_deinit(); //LCOV_EXCL_LINE
146         return NULL; //LCOV_EXCL_LINE
147 }
148
149 stc_s *stc_get_manager(void)
150 {
151         return g_stc;
152 }
153
154 void stc_stop_manager(void)
155 {
156         if (g_stc && g_stc->main_loop)
157                 g_main_loop_quit(g_stc->main_loop);
158 }
159
160 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
161 {
162         pid_t pid = 0;
163         int status = 0;
164         int ret = 0;
165         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
166         gchar **args = NULL;
167
168         if (cmd == NULL) {
169                 STC_LOGE("Invalid arguments");
170                 return STC_ERROR_INVALID_PARAMETER;
171         }
172
173         args = g_strsplit_set(cmd, " ", -1);
174
175         errno = 0;
176         pid = fork();
177
178         if (pid == 0) {
179                 errno = 0;
180                 if (execv(args[0], args) == -1) {
181                         STC_LOGE("Failed to execute [%s]", *err_str);
182                         g_strfreev(args);
183                         exit(-1);
184                 }
185         } else if (pid > 0) {
186                 if (waitpid(pid, &status, 0) == -1)
187                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
188
189                 if (WIFEXITED(status)) {
190                         ret = WEXITSTATUS(status);
191                         STC_LOGD("exited, status [%d]", status);
192                 } else if (WIFSIGNALED(status)) {
193                         STC_LOGD("killed by signal [%d]", WTERMSIG(status));
194                 } else if (WIFSTOPPED(status)) {
195                         STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
196                 } else if (WIFCONTINUED(status)) {
197                         STC_LOGD("continued");
198                 }
199
200                 *err_num = ret;
201                 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
202                 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
203
204                 g_strfreev(args);
205                 if (ret == 0)
206                         return STC_ERROR_NONE;
207                 else
208                         return STC_ERROR_FAIL;
209         }
210
211         *err_num = errno;
212         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
213         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
214
215         g_strfreev(args);
216         return STC_ERROR_FAIL;
217 }
218
219 gint32 main(gint32 argc, gchar *argv[])
220 {
221         GMainLoop *main_loop = NULL;
222         gint32 ret = -1;
223
224         STC_LOGI("Smart Traffic Control Manager");
225
226 #ifdef TIZEN_GTESTS
227         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
228 #endif
229
230         if (daemon(0, 0) != 0)
231                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
232
233         /* Initialize required subsystems */
234 #if !GLIB_CHECK_VERSION(2, 35, 0)
235         g_type_init();
236 #endif
237
238         /* Crate the GLIB main loop */
239         main_loop = g_main_loop_new(NULL, FALSE);
240
241         g_stc = __stc_manager_init();
242         if (!g_stc)
243                 goto fail;
244
245         g_stc->main_loop = main_loop;
246
247         /* Run the main loop */
248         g_main_loop_run(main_loop);
249
250         ret = 0;
251
252 fail:
253         __stc_manager_deinit();
254
255         if (main_loop)
256                 g_main_loop_unref(main_loop);
257
258         return ret;
259 }