db22b2fbbaa9edca00212bcae77585c785fe22ab
[platform/core/connectivity/stc-manager.git] / plugin / exception / stc-plugin-exception.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 <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <syspopup_caller.h>
22 #include <bundle.h>
23 #include <bundle_internal.h>
24 #include <dlog.h>
25 #include <gio/gio.h>
26 #include <pkgmgr-info.h>
27
28 #include "stc-plugin-exception.h"
29
30 //LCOV_EXCL_START
31 #define EXE_TYPE_APPLICATION "app"
32 #define EXE_TYPE_INSTRUCTION "inst"
33 #define EXE_TYPE_SYSTEM      "sys"
34 #define EXE_TYPE_SCRIPT      "script"
35
36 /* 1 day */
37 #define EXCNS_TIMER_INTERVAL 86400
38
39 #define EXCEPTION_BUF_MAX   64
40 #define EXCEPTION_STORAGE   "/var/lib/stc/exceptions"
41
42 #define INTERNET_PRIVILEGE  "http://tizen.org/privilege/internet"
43
44 typedef struct {
45         char *process_name;
46         char *exe_type;
47 } stc_exceptions_info;
48
49 typedef stc_cb_ret_e
50 (*stc_exceptions_info_cb)(const stc_exceptions_info *info,
51                               void *user_data);
52
53 static GHashTable *g_excns_hash;  /**< exception hash table */
54 static GHashTable *g_pkginfo_filter_hash;
55 static guint g_excns_timer_id;
56
57 static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
58 {
59         int ret = 0;
60         char *pkgname = NULL;
61
62         ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
63         if (ret == PMINFO_R_OK) {
64                 if (g_hash_table_insert(g_pkginfo_filter_hash,
65                                 g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE)
66                         STC_LOGE("Failed to insert hash table");
67         }
68
69         return STC_CONTINUE;
70 }
71
72 static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
73 {
74         int ret = 0;
75         char *pkgname = NULL;
76         char *exe_type = NULL;
77         stc_exceptions_info data;
78         const stc_exceptions_info_cb excn_cb = user_data;
79
80         ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
81         if (ret == PMINFO_R_OK) {
82                 exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname);
83                 if (exe_type)
84                         return STC_CONTINUE;
85
86                 data.process_name = pkgname;
87                 data.exe_type = EXE_TYPE_APPLICATION;
88
89                 if (excn_cb(&data, NULL) == STC_CANCEL)
90                         STC_LOGE("Failed to insert hash table");
91         }
92
93         return STC_CONTINUE;
94 }
95
96 static void __excn_hash_foreach_print(gpointer key, gpointer value,
97                                           gpointer data)
98 {
99         const char *process_name = key;
100         const char *exe_type = value;
101
102         STC_LOGI("excn info => process_name [%s] exe_type [%s]",
103                 process_name, exe_type);
104 }
105
106 static void __excn_hash_printall(void)
107 {
108         g_hash_table_foreach(g_excns_hash,
109                 __excn_hash_foreach_print, NULL);
110 }
111
112 static gboolean __remove_exception_app(gpointer key, gpointer value,
113                                         gpointer data)
114 {
115         const char *exe_type = value;
116
117         if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
118                 return TRUE;
119
120         return FALSE;
121 }
122
123 static void __remove_exception_appall(void)
124 {
125         g_hash_table_foreach_remove(g_excns_hash,
126                 __remove_exception_app, NULL);
127 }
128
129 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
130                                             void *user_data)
131 {
132         stc_cb_ret_e ret = STC_CONTINUE;
133
134         if (g_hash_table_insert(g_excns_hash,
135                         g_strdup(info->process_name),
136                         g_strdup(info->exe_type)) != TRUE)
137                 ret = STC_CANCEL;
138
139         return ret;
140 }
141
142 static gboolean __update_exceptions_app_list(void *user_data)
143 {
144         stc_plugin_exception_update_list();
145         return TRUE;
146 }
147
148 static stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
149                                        void *user_data)
150 {
151         int ret = 0;
152         int err = STC_ERROR_NONE;
153         pkgmgrinfo_pkginfo_filter_h handle;
154
155         g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash,
156                                                 g_str_equal, g_free, g_free);
157
158         ret = pkgmgrinfo_pkginfo_filter_create(&handle);
159         ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL,
160                         "Failed to create pkginfo filter");
161
162         ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
163                         PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,
164                         INTERNET_PRIVILEGE);
165         if (ret != PMINFO_R_OK) {
166                 STC_LOGE("Failed to add pkginfo filter string");
167                 err = STC_ERROR_FAIL;
168                 goto out;
169         }
170
171         ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle,
172                         __pkginfo_filter_list_cb, NULL);
173         if (ret != PMINFO_R_OK) {
174                 STC_LOGE("Failed to foreach pkginfo filter");
175                 err = STC_ERROR_FAIL;
176                 goto out;
177         }
178
179         ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb);
180         if (ret != PMINFO_R_OK) {
181                 STC_LOGE("Failed to get pkginfo list");
182                 err = STC_ERROR_FAIL;
183                 goto out;
184         }
185
186 out:
187         if (g_pkginfo_filter_hash) {
188                 g_hash_table_destroy(g_pkginfo_filter_hash);
189                 g_pkginfo_filter_hash = NULL;
190         }
191
192         if (handle)
193                 pkgmgrinfo_pkginfo_filter_destroy(handle);
194
195         return err;
196 }
197
198 static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
199                                        void *user_data)
200 {
201         stc_error_e error_code = STC_ERROR_NONE;
202         stc_exceptions_info data;
203
204         FILE *fp = NULL;
205         char buf[EXCEPTION_BUF_MAX] = {0, };
206
207         fp = fopen(EXCEPTION_STORAGE, "r");
208         ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file",
209                          EXCEPTION_STORAGE);
210
211         while (fgets(buf, sizeof(buf), fp) != NULL) {
212                 char *process_name, *exe_type;
213                 char *save_ptr = NULL;
214
215                 process_name = strtok_r(buf, ":", &save_ptr);
216                 if (process_name != NULL)
217                         data.process_name = process_name;
218                 else
219                         data.process_name = "none";
220
221                 exe_type = strtok_r(NULL, "\n", &save_ptr);
222                 if (exe_type != NULL)
223                         data.exe_type = exe_type;
224                 else
225                         data.exe_type = "none";
226
227                 if (exception_cb(&data, user_data) == STC_CANCEL)
228                         break;
229         }
230         fclose(fp);
231
232         return error_code;
233 }
234
235 int stc_plugin_exception_initialize(void)
236 {
237         g_excns_hash = g_hash_table_new_full(g_str_hash,
238                                         g_str_equal, g_free, g_free);
239
240         return STC_ERROR_NONE;
241 }
242
243 int stc_plugin_exception_deinitialize(void)
244 {
245         if (g_excns_timer_id > 0) {
246                 g_source_remove(g_excns_timer_id);
247                 g_excns_timer_id = 0;
248         }
249
250         g_hash_table_destroy(g_excns_hash);
251
252         return STC_ERROR_NONE;
253 }
254
255 int stc_plugin_exception_fill_list(void)
256 {
257         table_exceptions_foreach(__insert_exception_cb, NULL);
258         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
259
260         if (STC_DEBUG_LOG)
261                 __excn_hash_printall();
262
263         g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
264                         __update_exceptions_app_list,
265                         NULL);
266
267         return STC_ERROR_NONE;
268 }
269
270 int stc_plugin_exception_update_list(void)
271 {
272         __remove_exception_appall();
273         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
274
275         if (STC_DEBUG_LOG)
276                 __excn_hash_printall();
277
278         return STC_ERROR_NONE;
279 }
280
281 int stc_plugin_exception_check_by_cmdline(char *cmdline)
282 {
283         char *exe_type = NULL;
284
285         exe_type = g_hash_table_lookup(g_excns_hash, cmdline);
286         if (!exe_type)
287                 return STC_ERROR_NO_DATA;
288
289         return STC_ERROR_NONE;
290 }
291
292 API stc_plugin_exception_s stc_plugin_exception = {
293         .initialize_plugin =
294                 stc_plugin_exception_initialize,
295         .deinitialize_plugin =
296                 stc_plugin_exception_deinitialize,
297         .fill_exception_list =
298                 stc_plugin_exception_fill_list,
299         .update_exception_list =
300                 stc_plugin_exception_update_list,
301         .check_exception_by_cmdline =
302                 stc_plugin_exception_check_by_cmdline
303 };
304 //LCOV_EXCL_STOP