436ee29dbc56fa290394a2b9aecdc72d4047cc6a
[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
35 #define BUF_SIZE_FOR_ERR 100
36
37 static stc_s *g_stc = NULL;
38
39 static gboolean __validate_ident(const char *ident)
40 {
41         unsigned int i;
42
43         if (!ident)
44                 return FALSE;
45
46         for (i = 0; i < strlen(ident); ++i)
47                 if (!g_ascii_isprint(ident[i]))
48                         return FALSE;
49
50         return TRUE;
51 }
52
53 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
54 {
55         if (!ident)
56                 return;
57
58         if (!__validate_ident(ident)) {
59                 STC_LOGE("Invalid ident [%s]", ident);
60                 return;
61         }
62
63         if (!g_strcmp0(ident, INFO_CONFIG)) {
64                 int debug = stc_util_get_config_int(INFO_DEBUGLOG);
65                 stc_util_set_debuglog(debug);
66         }
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
93         inotify_deregister(INFO_STORAGE_DIR);
94         inotify_deinitialize();
95
96         STC_LOGI("stc manager deinitialized");
97         FREE(g_stc);
98         __STC_LOG_FUNC_EXIT__;
99 }
100
101 static stc_s *__stc_manager_init(void)
102 {
103         __STC_LOG_FUNC_ENTER__;
104         stc_s *stc;
105         stc_error_e err = STC_ERROR_NONE;
106
107         stc = MALLOC0(stc_s, 1);
108         if (!stc) {
109                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
110                 return NULL; //LCOV_EXCL_LINE
111         }
112         g_stc = stc;
113
114         stc_util_initialize_config();
115
116         inotify_initialize();
117         inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
118
119         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
120
121         EXEC(STC_ERROR_NONE, stc_db_initialize());
122
123         stc_plugin_appstatus_init();
124         stc_plugin_exception_init();
125         stc_plugin_procfs_init();
126
127         stc_firewall_init();
128
129         err = stc_monitor_init();
130         if (err != STC_ERROR_NONE)
131                 goto handle_error;
132
133         stc_plugin_procfs_load_pid();
134         stc_manager_gdbus_init((gpointer)stc);
135
136         STC_LOGI("stc manager initialized");
137         __STC_LOG_FUNC_EXIT__;
138         return stc;
139
140 handle_error:
141         STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
142         __stc_manager_deinit(); //LCOV_EXCL_LINE
143         return NULL; //LCOV_EXCL_LINE
144 }
145
146 stc_s *stc_get_manager(void)
147 {
148         return g_stc;
149 }
150
151 void stc_stop_manager(void)
152 {
153         if (g_stc && g_stc->main_loop)
154                 g_main_loop_quit(g_stc->main_loop);
155 }
156
157 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
158 {
159         pid_t pid = 0;
160         int status = 0;
161         int ret = 0;
162         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
163         gchar **args = NULL;
164
165         if (cmd == NULL) {
166                 STC_LOGE("Invalid arguments");
167                 return STC_ERROR_INVALID_PARAMETER;
168         }
169
170         args = g_strsplit_set(cmd, " ", -1);
171
172         errno = 0;
173         pid = fork();
174
175         if (pid == 0) {
176                 errno = 0;
177                 if (execv(args[0], args) == -1) {
178                         STC_LOGE("Failed to execute [%s]", *err_str);
179                         g_strfreev(args);
180                         exit(-1);
181                 }
182         } else if (pid > 0) {
183                 if (waitpid(pid, &status, 0) == -1)
184                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
185
186                 if (WIFEXITED(status)) {
187                         ret = WEXITSTATUS(status);
188                         STC_LOGD("exited, status [%d]", status);
189                 } else if (WIFSIGNALED(status)) {
190                         STC_LOGD("killed by signal [%d]", WTERMSIG(status));
191                 } else if (WIFSTOPPED(status)) {
192                         STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
193                 } else if (WIFCONTINUED(status)) {
194                         STC_LOGD("continued");
195                 }
196
197                 *err_num = ret;
198                 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
199                 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
200
201                 g_strfreev(args);
202                 if (ret == 0)
203                         return STC_ERROR_NONE;
204                 else
205                         return STC_ERROR_FAIL;
206         }
207
208         *err_num = errno;
209         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
210         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
211
212         g_strfreev(args);
213         return STC_ERROR_FAIL;
214 }
215
216 gint32 main(gint32 argc, gchar *argv[])
217 {
218         GMainLoop *main_loop = NULL;
219         gint32 ret = -1;
220
221         STC_LOGI("Smart Traffic Control Manager");
222
223 #ifdef TIZEN_GTESTS
224         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
225 #endif
226
227         if (daemon(0, 0) != 0)
228                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
229
230         /* Initialize required subsystems */
231 #if !GLIB_CHECK_VERSION(2, 35, 0)
232         g_type_init();
233 #endif
234
235         /* Crate the GLIB main loop */
236         main_loop = g_main_loop_new(NULL, FALSE);
237
238         g_stc = __stc_manager_init();
239         if (!g_stc)
240                 goto fail;
241
242         g_stc->main_loop = main_loop;
243
244         /* Run the main loop */
245         g_main_loop_run(main_loop);
246
247         ret = 0;
248
249 fail:
250         __stc_manager_deinit();
251
252         if (main_loop)
253                 g_main_loop_unref(main_loop);
254
255         return ret;
256 }