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"
35 #define BUF_SIZE_FOR_ERR 100
37 static stc_s *g_stc = NULL;
39 static gboolean __validate_ident(const char *ident)
46 for (i = 0; i < strlen(ident); ++i)
47 if (!g_ascii_isprint(ident[i]))
53 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
58 if (!__validate_ident(ident)) {
59 STC_LOGE("Invalid ident [%s]", ident);
63 if (!g_strcmp0(ident, INFO_CONFIG)) {
64 int debug = stc_util_get_config_int(INFO_DEBUGLOG);
65 stc_util_set_debuglog(debug);
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();
93 inotify_deregister(INFO_STORAGE_DIR);
94 inotify_deinitialize();
96 STC_LOGI("stc manager deinitialized");
98 __STC_LOG_FUNC_EXIT__;
101 static stc_s *__stc_manager_init(void)
103 __STC_LOG_FUNC_ENTER__;
105 stc_error_e err = STC_ERROR_NONE;
107 stc = MALLOC0(stc_s, 1);
109 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
110 return NULL; //LCOV_EXCL_LINE
114 stc_util_initialize_config();
116 inotify_initialize();
117 inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
119 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
121 EXEC(STC_ERROR_NONE, stc_db_initialize());
123 stc_plugin_appstatus_init();
124 stc_plugin_exception_init();
125 stc_plugin_procfs_init();
129 err = stc_monitor_init();
130 if (err != STC_ERROR_NONE)
133 stc_plugin_procfs_load_pid();
134 stc_manager_gdbus_init((gpointer)stc);
136 STC_LOGI("stc manager initialized");
137 __STC_LOG_FUNC_EXIT__;
141 STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
142 __stc_manager_deinit(); //LCOV_EXCL_LINE
143 return NULL; //LCOV_EXCL_LINE
146 stc_s *stc_get_manager(void)
151 void stc_stop_manager(void)
153 if (g_stc && g_stc->main_loop)
154 g_main_loop_quit(g_stc->main_loop);
157 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
162 char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
166 STC_LOGE("Invalid arguments");
167 return STC_ERROR_INVALID_PARAMETER;
170 args = g_strsplit_set(cmd, " ", -1);
177 if (execv(args[0], args) == -1) {
178 STC_LOGE("Failed to execute [%s]", *err_str);
182 } else if (pid > 0) {
183 if (waitpid(pid, &status, 0) == -1)
184 STC_LOGD("wait pid [%u] status [%d] ", pid, status);
186 if (WIFEXITED(status)) {
187 ret = WEXITSTATUS(status);
188 STC_LOGD("exited, status [%d]", status);
189 } else if (WIFSIGNALED(status)) {
190 STC_LOGD("killed by signal [%d]", WTERMSIG(status));
191 } else if (WIFSTOPPED(status)) {
192 STC_LOGD("stopped by signal [%d]", WSTOPSIG(status));
193 } else if (WIFCONTINUED(status)) {
194 STC_LOGD("continued");
198 *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR);
199 STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str);
203 return STC_ERROR_NONE;
205 return STC_ERROR_FAIL;
209 *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
210 STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
213 return STC_ERROR_FAIL;
216 gint32 main(gint32 argc, gchar *argv[])
218 GMainLoop *main_loop = NULL;
221 STC_LOGI("Smart Traffic Control Manager");
224 setenv("GCOV_PREFIX", "/tmp/daemon", 1);
227 if (daemon(0, 0) != 0)
228 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
230 /* Initialize required subsystems */
231 #if !GLIB_CHECK_VERSION(2, 35, 0)
235 /* Crate the GLIB main loop */
236 main_loop = g_main_loop_new(NULL, FALSE);
238 g_stc = __stc_manager_init();
242 g_stc->main_loop = main_loop;
244 /* Run the main loop */
245 g_main_loop_run(main_loop);
250 __stc_manager_deinit();
253 g_main_loop_unref(main_loop);