2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "stc-manager.h"
21 #include "stc-manager-gdbus.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"
38 #define BUF_SIZE_FOR_ERR 100
40 static stc_s *g_stc = NULL;
42 static gboolean __validate_ident(const char *ident)
49 for (i = 0; i < strlen(ident); ++i)
50 if (!g_ascii_isprint(ident[i]))
56 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
61 if (!__validate_ident(ident)) {
62 STC_LOGE("Invalid ident [%s]", ident);
66 if (!g_strcmp0(ident, INFO_CONFIG))
67 stc_util_update_log_state();
70 static void __stc_manager_deinit(void)
72 __STC_LOG_FUNC_ENTER__;
75 STC_LOGE("Memory for manager structure is not allocated");
79 stc_deinit_db_guard();
80 stc_db_deinitialize();
82 iptables_flush_chains();
85 stc_manager_gdbus_deinit((gpointer)g_stc);
87 stc_plugin_appstatus_deinit();
88 stc_plugin_exception_deinit();
89 stc_plugin_procfs_deinit();
90 stc_plugin_tether_deinit();
91 stc_plugin_pcap_deinit();
92 stc_plugin_monitor_deinit();
93 stc_plugin_firewall_deinit();
95 inotify_deregister(INFO_STORAGE_DIR);
96 inotify_deinitialize();
98 STC_LOGI("stc manager deinitialized");
100 __STC_LOG_FUNC_EXIT__;
103 static stc_s *__stc_manager_init(void)
105 __STC_LOG_FUNC_ENTER__;
107 stc_error_e err = STC_ERROR_NONE;
109 stc = MALLOC0(stc_s, 1);
111 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
112 return NULL; //LCOV_EXCL_LINE
116 stc_util_initialize_config();
118 inotify_initialize();
119 inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
121 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
123 err = stc_db_initialize();
124 if (err != STC_ERROR_NONE) {
125 STC_LOGD("Failed to initialize stc db"); //LCOV_EXCL_LINE
126 return NULL; //LCOV_EXCL_LINE
129 g_stc->ondemand_mode = TRUE;
131 stc_plugin_appstatus_init();
132 stc_plugin_exception_init();
133 stc_plugin_procfs_init();
134 stc_plugin_tether_init();
135 if (stc_plugin_pcap_init() == STC_ERROR_NONE)
136 g_stc->ondemand_mode = FALSE;
137 if (stc_plugin_monitor_init() == STC_ERROR_NONE)
138 g_stc->ondemand_mode = FALSE;
139 stc_plugin_firewall_init();
141 stc_plugin_procfs_load_pid();
143 stc_manager_gdbus_init((gpointer)stc);
145 STC_LOGI("stc manager initialized");
146 __STC_LOG_FUNC_EXIT__;
150 static gboolean __stc_timer_expired(gpointer data)
152 if (g_stc->keep_alive) {
153 g_stc->keep_alive = FALSE;
157 g_main_loop_quit(g_stc->main_loop);
162 API stc_s *stc_get_manager(void)
167 void stc_stop_manager(void)
169 if (g_stc && g_stc->main_loop)
170 g_main_loop_quit(g_stc->main_loop);
173 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
178 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
182 STC_LOGE("Invalid arguments");
183 return STC_ERROR_INVALID_PARAMETER;
186 args = g_strsplit_set(cmd, " ", -1);
194 if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
195 STC_LOGE("Invalid arguments");
200 if (execv(args[0], args) == -1) {
201 STC_LOGE("Failed to execute [%s]", *err_str);
205 } else if (pid > 0) {
206 if (waitpid(pid, &status, 0) == -1)
207 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
209 if (WIFEXITED(status)) {
210 ret = WEXITSTATUS(status);
211 STC_LOGD("exited, status [%d]", status);
212 } else if (WIFSIGNALED(status)) {
213 STC_LOGD("killed by signal [%d]", WTERMSIG(status));
214 } else if (WIFSTOPPED(status)) {
215 STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
216 } else if (WIFCONTINUED(status)) {
217 STC_LOGD("continued");
221 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
222 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
226 return STC_ERROR_NONE;
228 return STC_ERROR_FAIL;
232 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
233 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
236 return STC_ERROR_FAIL;
239 void stc_set_keep_alive(gboolean keep_alive)
241 g_stc->keep_alive = keep_alive;
244 gint32 main(gint32 argc, gchar *argv[])
246 GMainLoop *main_loop = NULL;
248 STC_LOGI("Smart Traffic Control Manager");
251 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
255 if (daemon(0, 0) != 0)
256 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
259 /* Initialize required subsystems */
260 #if !GLIB_CHECK_VERSION(2, 35, 0)
264 g_stc = __stc_manager_init();
268 /* Crate the GLIB main loop */
269 main_loop = g_main_loop_new(NULL, FALSE);
270 g_stc->main_loop = main_loop;
272 if (g_stc->ondemand_mode) {
273 g_stc->timer = g_timeout_add_seconds(10, __stc_timer_expired, NULL);
274 g_stc->keep_alive = FALSE;
277 /* Run the main loop */
278 g_main_loop_run(main_loop);
281 __stc_manager_deinit();
284 g_main_loop_unref(main_loop);