Added information config with inotify
[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 "stc-manager.h"
19 #include "stc-emulator.h"
20 #include "stc-manager-gdbus.h"
21 #include "stc-db.h"
22 #include "counter.h"
23 #include "table-restrictions.h"
24 #include "helper-cgroup.h"
25 #include "helper-nfacct-rule.h"
26 #include "helper-inotify.h"
27 #include "stc-monitor.h"
28 #include "stc-manager-plugin.h"
29 #include "stc-app-lifecycle.h"
30
31 static stc_s *g_stc = NULL;
32
33 static gboolean __validate_ident(const char *ident)
34 {
35         unsigned int i;
36
37         if (!ident)
38                 return FALSE;
39
40         for (i = 0; i < strlen(ident); ++i)
41                 if (!g_ascii_isprint(ident[i]))
42                         return FALSE;
43
44         return TRUE;
45 }
46
47 static void __stc_inotify_handler(struct inotify_event *event, const char *ident)
48 {
49         if (!ident)
50                 return;
51
52         if (!__validate_ident(ident)) {
53                 STC_LOGE("Invalid ident [%s]", ident);
54                 return;
55         }
56
57         if (event->mask & IN_MODIFY) {
58                 if (!g_strcmp0(ident, INFO_CONFIG)) {
59                         int debug = 0;
60                         debug = stc_util_get_config_int(INFO_DEBUGLOG);
61                         stc_util_set_debuglog(debug);
62                 }
63         }
64 }
65
66 static void __stc_manager_deinit(void)
67 {
68         __STC_LOG_FUNC_ENTER__;
69
70         if (!g_stc) {
71                 STC_LOGE("Memory for manager structure is not allocated");
72                 return;
73         }
74
75         stc_monitor_deinit();
76         stc_deinit_db_guard();
77         stc_db_deinitialize();
78
79         stc_manager_gdbus_deinit((gpointer)g_stc);
80         stc_app_lifecycle_monitor_deinit();
81         stc_manager_plugin_deinit();
82
83         inotify_deregister(INFO_STORAGE_DIR);
84         inotify_deinitialize();
85
86         STC_LOGI("stc manager deinitialized");
87         FREE(g_stc);
88         __STC_LOG_FUNC_EXIT__;
89 }
90
91 static stc_s *__stc_manager_init(void)
92 {
93         __STC_LOG_FUNC_ENTER__;
94         stc_s *stc;
95         stc_error_e err = STC_ERROR_NONE;
96
97         stc = MALLOC0(stc_s, 1);
98         if (!stc) {
99                 STC_LOGE("Failed to allocate memory for manager structure");
100                 return NULL;
101         }
102         g_stc = stc;
103
104         stc_util_initialize_config();
105
106         inotify_initialize();
107         inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
108
109         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
110
111         EXEC(STC_ERROR_NONE, stc_db_initialize());
112
113         err = stc_monitor_init();
114         if (err != STC_ERROR_NONE)
115                 goto handle_error;
116
117         stc_manager_plugin_init();
118         stc_app_lifecycle_monitor_init();
119         stc_manager_gdbus_init((gpointer)stc);
120
121         STC_LOGI("stc manager initialized");
122         __STC_LOG_FUNC_EXIT__;
123         return stc;
124
125 handle_error:
126         STC_LOGD("Failed to initialize stc manager");
127         __stc_manager_deinit();
128         return NULL;
129 }
130
131 stc_s *stc_get_manager(void)
132 {
133         return g_stc;
134 }
135
136 gint32 main(gint32 argc, gchar *argv[])
137 {
138         GMainLoop *main_loop = NULL;
139         gint32 ret = -1;
140
141         STC_LOGI("Smart Traffic Control Manager");
142
143         if (daemon(0, 0) != 0)
144                 STC_LOGE("Can't start daemon");
145
146         /* Initialize required subsystems */
147 #if !GLIB_CHECK_VERSION(2, 35, 0)
148         g_type_init();
149 #endif
150
151         /* Crate the GLIB main loop */
152         main_loop = g_main_loop_new(NULL, FALSE);
153
154         stc_emulator_check_environment();
155         if (stc_emulator_is_emulated() == FALSE) {
156                 g_stc = __stc_manager_init();
157                 if (!g_stc)
158                         goto fail;
159                 g_stc->main_loop = main_loop;
160         }
161
162         /* Run the main loop */
163         g_main_loop_run(main_loop);
164
165         ret = 0;
166
167 fail:
168         if (stc_emulator_is_emulated() == FALSE)
169                 __stc_manager_deinit();
170
171         if (main_loop)
172                 g_main_loop_unref(main_loop);
173
174         return ret;
175 }