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-emulator.h"
22 #include "stc-manager-gdbus.h"
25 #include "table-restrictions.h"
26 #include "helper-cgroup.h"
27 #include "helper-nfacct-rule.h"
28 #include "helper-iptables.h"
29 #include "helper-inotify.h"
30 #include "stc-monitor.h"
31 #include "stc-firewall.h"
32 #include "stc-manager-plugin-appstatus.h"
33 #include "stc-manager-plugin-exception.h"
34 #include "stc-manager-plugin-procfs.h"
36 #define BUF_SIZE_FOR_ERR 100
38 static stc_s *g_stc = NULL;
40 static gboolean __validate_ident(const char *ident)
47 for (i = 0; i < strlen(ident); ++i)
48 if (!g_ascii_isprint(ident[i]))
54 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
59 if (!__validate_ident(ident)) {
60 STC_LOGE("Invalid ident [%s]", ident);
64 if (!g_strcmp0(ident, INFO_CONFIG)) {
65 int debug = stc_util_get_config_int(INFO_DEBUGLOG);
66 stc_util_set_debuglog(debug);
70 static void __stc_manager_deinit(void)
72 __STC_LOG_FUNC_ENTER__;
75 STC_LOGE("Memory for manager structure is not allocated");
80 stc_deinit_db_guard();
81 stc_db_deinitialize();
83 iptables_flush_chains();
86 stc_manager_gdbus_deinit((gpointer)g_stc);
88 stc_firewall_deinit();
90 stc_plugin_appstatus_deinit();
91 stc_plugin_exception_deinit();
92 stc_plugin_procfs_deinit();
94 inotify_deregister(INFO_STORAGE_DIR);
95 inotify_deinitialize();
97 STC_LOGI("stc manager deinitialized");
99 __STC_LOG_FUNC_EXIT__;
102 static stc_s *__stc_manager_init(void)
104 __STC_LOG_FUNC_ENTER__;
106 stc_error_e err = STC_ERROR_NONE;
108 stc = MALLOC0(stc_s, 1);
110 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
111 return NULL; //LCOV_EXCL_LINE
115 stc_util_initialize_config();
117 inotify_initialize();
118 inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
120 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
122 EXEC(STC_ERROR_NONE, stc_db_initialize());
124 stc_plugin_appstatus_init();
125 stc_plugin_exception_init();
126 stc_plugin_procfs_init();
130 err = stc_monitor_init();
131 if (err != STC_ERROR_NONE)
134 stc_plugin_procfs_load_pid();
135 stc_manager_gdbus_init((gpointer)stc);
137 STC_LOGI("stc manager initialized");
138 __STC_LOG_FUNC_EXIT__;
142 STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
143 __stc_manager_deinit(); //LCOV_EXCL_LINE
144 return NULL; //LCOV_EXCL_LINE
147 stc_s *stc_get_manager(void)
152 void stc_stop_manager(void)
154 if (g_stc && g_stc->main_loop)
155 g_main_loop_quit(g_stc->main_loop);
158 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
163 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
167 STC_LOGE("Invalid arguments");
168 return STC_ERROR_INVALID_PARAMETER;
171 args = g_strsplit_set(cmd, " ", -1);
178 if (execv(args[0], args) == -1) {
179 STC_LOGE("Failed to execute [%s]", err_str);
183 } else if (pid > 0) {
184 if (waitpid(pid, &status, 0) == -1)
185 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
187 if (WIFEXITED(status)) {
188 ret = WEXITSTATUS(status);
189 STC_LOGD("exited, status [%d]", status);
190 } else if (WIFSIGNALED(status)) {
191 STC_LOGD("killed by signal [%d]", WTERMSIG(status));
192 } else if (WIFSTOPPED(status)) {
193 STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
194 } else if (WIFCONTINUED(status)) {
195 STC_LOGD("continued");
199 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
200 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
204 return STC_ERROR_NONE;
206 return STC_ERROR_FAIL;
210 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
211 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
214 return STC_ERROR_FAIL;
217 gint32 main(gint32 argc, gchar *argv[])
219 GMainLoop *main_loop = NULL;
222 STC_LOGI("Smart Traffic Control Manager");
225 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
228 if (daemon(0, 0) != 0)
229 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
231 /* Initialize required subsystems */
232 #if !GLIB_CHECK_VERSION(2, 35, 0)
236 /* Crate the GLIB main loop */
237 main_loop = g_main_loop_new(NULL, FALSE);
239 stc_emulator_check_environment();
240 if (stc_emulator_is_emulated() == FALSE) {
241 g_stc = __stc_manager_init();
244 g_stc->main_loop = main_loop;
247 /* Run the main loop */
248 g_main_loop_run(main_loop);
253 if (stc_emulator_is_emulated() == FALSE)
254 __stc_manager_deinit();
257 g_main_loop_unref(main_loop);