Load procfs on booting time
[platform/core/connectivity/stc-manager.git] / src / stc-manager.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <signal.h>
18 #include <errno.h>
19 #include <sys/wait.h>
20 #include "stc-manager.h"
21 #include "stc-emulator.h"
22 #include "stc-manager-gdbus.h"
23 #include "stc-db.h"
24 #include "counter.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"
34
35 #define BUF_SIZE_FOR_ERR 100
36
37 static stc_s *g_stc = NULL;
38
39 /*
40 static gboolean __validate_ident(const char *ident)
41 {
42         unsigned int i;
43
44         if (!ident)
45                 return FALSE;
46
47         for (i = 0; i < strlen(ident); ++i)
48                 if (!g_ascii_isprint(ident[i]))
49                         return FALSE;
50
51         return TRUE;
52 }
53 */
54
55 static void __stc_manager_deinit(void)
56 {
57         __STC_LOG_FUNC_ENTER__;
58
59         if (!g_stc) {
60                 STC_LOGE("Memory for manager structure is not allocated");
61                 return;
62         }
63
64         stc_monitor_deinit();
65         stc_deinit_db_guard();
66         stc_db_deinitialize();
67
68         iptables_flush_chains();
69         iptables_deinit();
70
71         stc_manager_gdbus_deinit((gpointer)g_stc);
72
73         stc_firewall_deinit();
74
75         stc_plugin_appstatus_deinit();
76         stc_plugin_exception_deinit();
77         stc_plugin_procfs_deinit();
78
79         STC_LOGI("stc manager deinitialized");
80         FREE(g_stc);
81         __STC_LOG_FUNC_EXIT__;
82 }
83
84 static stc_s *__stc_manager_init(void)
85 {
86         __STC_LOG_FUNC_ENTER__;
87         stc_s *stc;
88         stc_error_e err = STC_ERROR_NONE;
89
90         stc = MALLOC0(stc_s, 1);
91         if (!stc) {
92                 STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE
93                 return NULL; //LCOV_EXCL_LINE
94         }
95         g_stc = stc;
96
97         stc_util_initialize_config();
98
99         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
100
101         EXEC(STC_ERROR_NONE, stc_db_initialize());
102
103         stc_plugin_appstatus_init();
104         stc_plugin_exception_init();
105         stc_plugin_procfs_init();
106
107         stc_firewall_init();
108
109         err = stc_monitor_init();
110         if (err != STC_ERROR_NONE)
111                 goto handle_error;
112
113         stc_plugin_procfs_load_pid();
114         stc_manager_gdbus_init((gpointer)stc);
115
116         STC_LOGI("stc manager initialized");
117         __STC_LOG_FUNC_EXIT__;
118         return stc;
119
120 handle_error:
121         STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
122         __stc_manager_deinit(); //LCOV_EXCL_LINE
123         return NULL; //LCOV_EXCL_LINE
124 }
125
126 stc_s *stc_get_manager(void)
127 {
128         return g_stc;
129 }
130
131 void stc_stop_manager(void)
132 {
133         if (g_stc && g_stc->main_loop)
134                 g_main_loop_quit(g_stc->main_loop);
135 }
136
137 int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
138 {
139         pid_t pid = 0;
140         int status = 0;
141         int ret = 0;
142         char err_buf[BUF_SIZE_FOR_ERR] = { 0, };
143         gchar **args = NULL;
144
145         if (cmd == NULL) {
146                 STC_LOGE("Invalid arguments");
147                 return STC_ERROR_INVALID_PARAMETER;
148         }
149
150         args = g_strsplit_set(cmd, " ", -1);
151
152         errno = 0;
153         pid = fork();
154
155         if (pid == 0) {
156                 errno = 0;
157                 if (execv(args[0], args) == -1) {
158                         STC_LOGE("Failed to execute [%s]", err_str);
159                         g_strfreev(args);
160                         exit(-1);
161                 }
162         } else if (pid > 0) {
163                 if (waitpid(pid, &status, 0) == -1)
164                         STC_LOGD("wait pid [%u] status [%d] ", pid, status);
165
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");
175                 }
176
177                 *err_num = ret;
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);
180
181                 g_strfreev(args);
182                 if (ret == 0)
183                         return STC_ERROR_NONE;
184                 else
185                         return STC_ERROR_FAIL;
186         }
187
188         *err_num = errno;
189         *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR);
190         STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str);
191
192         g_strfreev(args);
193         return STC_ERROR_FAIL;
194 }
195
196 gint32 main(gint32 argc, gchar *argv[])
197 {
198         GMainLoop *main_loop = NULL;
199         gint32 ret = -1;
200
201         STC_LOGI("Smart Traffic Control Manager");
202
203 #ifdef TIZEN_GTESTS
204         setenv("GCOV_PREFIX", "/tmp/daemon", 1);
205 #endif
206
207         if (daemon(0, 0) != 0)
208                 STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
209
210         /* Initialize required subsystems */
211 #if !GLIB_CHECK_VERSION(2, 35, 0)
212         g_type_init();
213 #endif
214
215         /* Crate the GLIB main loop */
216         main_loop = g_main_loop_new(NULL, FALSE);
217
218         stc_emulator_check_environment();
219         if (stc_emulator_is_emulated() == FALSE) {
220                 g_stc = __stc_manager_init();
221                 if (!g_stc)
222                         goto fail;
223                 g_stc->main_loop = main_loop;
224         }
225
226         /* Run the main loop */
227         g_main_loop_run(main_loop);
228
229         ret = 0;
230
231 fail:
232         if (stc_emulator_is_emulated() == FALSE)
233                 __stc_manager_deinit();
234
235         if (main_loop)
236                 g_main_loop_unref(main_loop);
237
238         return ret;
239 }