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.
18 #include "stc-manager.h"
19 #include "stc-emulator.h"
20 #include "stc-manager-gdbus.h"
23 #include "table-restrictions.h"
24 #include "helper-cgroup.h"
25 #include "helper-nfacct-rule.h"
26 #include "stc-monitor.h"
27 #include "stc-manager-plugin-appstatus.h"
28 #include "stc-manager-plugin-exception.h"
29 #include "stc-manager-plugin-procfs.h"
31 static stc_s *g_stc = NULL;
34 static gboolean __validate_ident(const char *ident)
41 for (i = 0; i < strlen(ident); ++i)
42 if (!g_ascii_isprint(ident[i]))
49 static void __stc_manager_deinit(void)
51 __STC_LOG_FUNC_ENTER__;
54 STC_LOGE("Memory for manager structure is not allocated");
59 stc_deinit_db_guard();
60 stc_db_deinitialize();
62 stc_manager_gdbus_deinit((gpointer)g_stc);
64 stc_plugin_appstatus_deinit();
65 stc_plugin_exception_deinit();
66 stc_plugin_procfs_deinit();
68 STC_LOGI("stc manager deinitialized");
70 __STC_LOG_FUNC_EXIT__;
73 static stc_s *__stc_manager_init(void)
75 __STC_LOG_FUNC_ENTER__;
77 stc_error_e err = STC_ERROR_NONE;
79 stc = MALLOC0(stc_s, 1);
81 STC_LOGE("Failed to allocate memory for manager structure");
86 stc_util_initialize_config();
88 cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
90 EXEC(STC_ERROR_NONE, stc_db_initialize());
92 stc_plugin_appstatus_init();
93 stc_plugin_exception_init();
94 stc_plugin_procfs_init();
96 err = stc_monitor_init();
97 if (err != STC_ERROR_NONE)
100 stc_manager_gdbus_init((gpointer)stc);
102 STC_LOGI("stc manager initialized");
103 __STC_LOG_FUNC_EXIT__;
107 STC_LOGD("Failed to initialize stc manager");
108 __stc_manager_deinit();
112 stc_s *stc_get_manager(void)
117 gint32 main(gint32 argc, gchar *argv[])
119 GMainLoop *main_loop = NULL;
122 STC_LOGI("Smart Traffic Control Manager");
124 if (daemon(0, 0) != 0)
125 STC_LOGE("Can't start daemon");
127 /* Initialize required subsystems */
128 #if !GLIB_CHECK_VERSION(2, 35, 0)
132 /* Crate the GLIB main loop */
133 main_loop = g_main_loop_new(NULL, FALSE);
135 stc_emulator_check_environment();
136 if (stc_emulator_is_emulated() == FALSE) {
137 g_stc = __stc_manager_init();
140 g_stc->main_loop = main_loop;
143 /* Run the main loop */
144 g_main_loop_run(main_loop);
149 if (stc_emulator_is_emulated() == FALSE)
150 __stc_manager_deinit();
153 g_main_loop_unref(main_loop);