Merge "Monitoring application wise background data usage." into tizen
[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 "stc-manager.h"
18 #include "stc-statistics.h"
19 #include "stc-restriction.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 "stc-monitor.h"
27 #include "stc-manager-plugin.h"
28
29 static stc_s *g_stc = NULL;
30
31 static void __stc_manager_deinit(void)
32 {
33         __STC_LOG_FUNC_ENTER__;
34
35         if (!g_stc) {
36                 STC_LOGE("Memory for manager structure is not allocated");
37                 return;
38         }
39
40         stc_monitor_deinit();
41         stc_deinit_db_guard();
42         stc_db_deinitialize();
43         stc_manager_gdbus_deinit((gpointer)g_stc);
44         stc_manager_plugin_deinit();
45
46         STC_LOGI("stc manager deinitialized");
47         FREE(g_stc);
48         __STC_LOG_FUNC_EXIT__;
49 }
50
51 static stc_s *__stc_manager_init(void)
52 {
53         __STC_LOG_FUNC_ENTER__;
54         stc_s *stc;
55
56         stc = MALLOC0(stc_s, 1);
57         if (!stc) {
58                 STC_LOGE("Failed to allocate memory for manager structure");
59                 return NULL;
60         }
61         g_stc = stc;
62
63         cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
64
65         EXEC(STC_ERROR_NONE, stc_db_initialize());
66
67         stc_monitor_init();
68         stc_manager_gdbus_init((gpointer)stc);
69         stc_manager_plugin_init();
70
71         STC_LOGI("stc manager initialized");
72         __STC_LOG_FUNC_EXIT__;
73         return stc;
74
75 handle_error:
76         STC_LOGD("Failed to initialize stc manager");
77         __stc_manager_deinit();
78         return NULL;
79 }
80
81 stc_s *stc_get_manager(void)
82 {
83         return g_stc;
84 }
85
86 gint32 main(gint32 argc, gchar *argv[])
87 {
88         GMainLoop *main_loop = NULL;
89         gint32 ret = -1;
90
91         STC_LOGI("Smart Traffic Control Manager");
92
93         if (daemon(0, 0) != 0)
94                 STC_LOGE("Can't start daemon");
95
96         /* Initialize required subsystems */
97 #if !GLIB_CHECK_VERSION(2, 35, 0)
98         g_type_init();
99 #endif
100
101         g_stc = __stc_manager_init();
102         if (!g_stc)
103                 goto fail;
104
105         /* Crate the GLIB main loop */
106         main_loop = g_main_loop_new(NULL, FALSE);
107         g_stc->main_loop = main_loop;
108
109         /* Run the main loop */
110         g_main_loop_run(main_loop);
111
112         ret = 0;
113
114 fail:
115         __stc_manager_deinit();
116
117         if (main_loop)
118                 g_main_loop_unref(main_loop);
119
120         return ret;
121 }