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 "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"
35 #define BUF_SIZE_FOR_ERR 100
37 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]))
55 static void __stc_manager_deinit(void)
57 __STC_LOG_FUNC_ENTER__;
60 STC_LOGE("Memory for manager structure is not allocated");
65 stc_deinit_db_guard();
66 stc_db_deinitialize();
68 iptables_flush_chains();
71 stc_manager_gdbus_deinit((gpointer)g_stc);
73 stc_firewall_deinit();
75 stc_plugin_appstatus_deinit();
76 stc_plugin_exception_deinit();
77 stc_plugin_procfs_deinit();
79 STC_LOGI("stc manager deinitialized");
81 __STC_LOG_FUNC_EXIT__;
84 static stc_s *__stc_manager_init(void)
86 __STC_LOG_FUNC_ENTER__;
88 stc_error_e err = STC_ERROR_NONE;
90 stc = MALLOC0(stc_s, 1);
92 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
93 return NULL; //LCOV_EXCL_LINE
97 stc_util_initialize_config();
99 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
101 EXEC(STC_ERROR_NONE, stc_db_initialize());
103 stc_plugin_appstatus_init();
104 stc_plugin_exception_init();
105 stc_plugin_procfs_init();
109 err = stc_monitor_init();
110 if (err != STC_ERROR_NONE)
113 stc_manager_gdbus_init((gpointer)stc);
115 STC_LOGI("stc manager initialized");
116 __STC_LOG_FUNC_EXIT__;
120 STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
121 __stc_manager_deinit(); //LCOV_EXCL_LINE
122 return NULL; //LCOV_EXCL_LINE
125 stc_s *stc_get_manager(void)
130 void stc_stop_manager(void)
132 if (g_stc && g_stc->main_loop)
133 g_main_loop_quit(g_stc->main_loop);
136 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
141 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
145 STC_LOGE("Invalid arguments");
146 return STC_ERROR_INVALID_PARAMETER;
149 args = g_strsplit_set(cmd, " ", -1);
156 if (execv(args[0], args) == -1) {
157 STC_LOGE("Failed to execute [%s]", err_str);
161 } else if (pid > 0) {
162 if (waitpid(pid, &status, 0) == -1)
163 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
165 if (WIFEXITED(status)) {
166 ret = WEXITSTATUS(status);
167 STC_LOGD("exited, status [%d]", status);
168 } else if (WIFSIGNALED(status)) {
169 STC_LOGD("killed by signal [%d]", WTERMSIG(status));
170 } else if (WIFSTOPPED(status)) {
171 STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
172 } else if (WIFCONTINUED(status)) {
173 STC_LOGD("continued");
177 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
178 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
182 return STC_ERROR_NONE;
184 return STC_ERROR_FAIL;
188 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
189 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
192 return STC_ERROR_FAIL;
195 gint32 main(gint32 argc, gchar *argv[])
197 GMainLoop *main_loop = NULL;
200 STC_LOGI("Smart Traffic Control Manager");
203 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
206 if (daemon(0, 0) != 0)
207 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
209 /* Initialize required subsystems */
210 #if !GLIB_CHECK_VERSION(2, 35, 0)
214 /* Crate the GLIB main loop */
215 main_loop = g_main_loop_new(NULL, FALSE);
217 stc_emulator_check_environment();
218 if (stc_emulator_is_emulated() == FALSE) {
219 g_stc = __stc_manager_init();
222 g_stc->main_loop = main_loop;
225 /* Run the main loop */
226 g_main_loop_run(main_loop);
231 if (stc_emulator_is_emulated() == FALSE)
232 __stc_manager_deinit();
235 g_main_loop_unref(main_loop);