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-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"
37 #define BUF_SIZE_FOR_ERR 100
39 static stc_s *g_stc = NULL;
41 static gboolean __validate_ident(const char *ident)
48 for (i = 0; i < strlen(ident); ++i)
49 if (!g_ascii_isprint(ident[i]))
55 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
60 if (!__validate_ident(ident)) {
61 STC_LOGE("Invalid ident [%s]", ident);
65 if (!g_strcmp0(ident, INFO_CONFIG))
66 stc_util_update_log_state();
69 static void __stc_manager_deinit(void)
71 __STC_LOG_FUNC_ENTER__;
74 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_firewall_deinit();
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();
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);
122 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
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
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();
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
148 stc_plugin_procfs_load_pid();
151 stc_manager_gdbus_init((gpointer)stc);
153 STC_LOGI("stc manager initialized");
154 __STC_LOG_FUNC_EXIT__;
158 stc_s *stc_get_manager(void)
163 void stc_stop_manager(void)
165 if (g_stc && g_stc->main_loop)
166 g_main_loop_quit(g_stc->main_loop);
169 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
174 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
178 STC_LOGE("Invalid arguments");
179 return STC_ERROR_INVALID_PARAMETER;
182 args = g_strsplit_set(cmd, " ", -1);
190 if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
191 STC_LOGE("Invalid arguments");
196 if (execv(args[0], args) == -1) {
197 STC_LOGE("Failed to execute [%s]", *err_str);
201 } else if (pid > 0) {
202 if (waitpid(pid, &status, 0) == -1)
203 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
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");
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);
222 return STC_ERROR_NONE;
224 return STC_ERROR_FAIL;
228 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
229 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
232 return STC_ERROR_FAIL;
235 gint32 main(gint32 argc, gchar *argv[])
237 GMainLoop *main_loop = NULL;
240 STC_LOGI("Smart Traffic Control Manager");
243 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
246 if (daemon(0, 0) != 0)
247 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
249 /* Initialize required subsystems */
250 #if !GLIB_CHECK_VERSION(2, 35, 0)
254 /* Crate the GLIB main loop */
255 main_loop = g_main_loop_new(NULL, FALSE);
257 g_stc = __stc_manager_init();
261 g_stc->main_loop = main_loop;
263 /* Run the main loop */
264 g_main_loop_run(main_loop);
269 __stc_manager_deinit();
272 g_main_loop_unref(main_loop);