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_plugin_procfs_load_pid();
114 stc_manager_gdbus_init((gpointer)stc);
116 STC_LOGI("stc manager initialized");
117 __STC_LOG_FUNC_EXIT__;
121 STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
122 __stc_manager_deinit(); //LCOV_EXCL_LINE
123 return NULL; //LCOV_EXCL_LINE
126 stc_s *stc_get_manager(void)
131 void stc_stop_manager(void)
133 if (g_stc && g_stc->main_loop)
134 g_main_loop_quit(g_stc->main_loop);
137 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
142 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
146 STC_LOGE("Invalid arguments");
147 return STC_ERROR_INVALID_PARAMETER;
150 args = g_strsplit_set(cmd, " ", -1);
157 if (execv(args[0], args) == -1) {
158 STC_LOGE("Failed to execute [%s]", err_str);
162 } else if (pid > 0) {
163 if (waitpid(pid, &status, 0) == -1)
164 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
166 if (WIFEXITED(status)) {
167 ret = WEXITSTATUS(status);
168 STC_LOGD("exited, status [%d]", status);
169 } else if (WIFSIGNALED(status)) {
170 STC_LOGD("killed by signal [%d]", WTERMSIG(status));
171 } else if (WIFSTOPPED(status)) {
172 STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
173 } else if (WIFCONTINUED(status)) {
174 STC_LOGD("continued");
178 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
179 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
183 return STC_ERROR_NONE;
185 return STC_ERROR_FAIL;
189 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
190 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
193 return STC_ERROR_FAIL;
196 gint32 main(gint32 argc, gchar *argv[])
198 GMainLoop *main_loop = NULL;
201 STC_LOGI("Smart Traffic Control Manager");
204 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
207 if (daemon(0, 0) != 0)
208 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
210 /* Initialize required subsystems */
211 #if !GLIB_CHECK_VERSION(2, 35, 0)
215 /* Crate the GLIB main loop */
216 main_loop = g_main_loop_new(NULL, FALSE);
218 stc_emulator_check_environment();
219 if (stc_emulator_is_emulated() == FALSE) {
220 g_stc = __stc_manager_init();
223 g_stc->main_loop = main_loop;
226 /* Run the main loop */
227 g_main_loop_run(main_loop);
232 if (stc_emulator_is_emulated() == FALSE)
233 __stc_manager_deinit();
236 g_main_loop_unref(main_loop);