Moved procfs for app lifecycle to plugin and separate plugins
[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
210         while (fgets(buf, sizeof(buf), fp) != NULL) {
211                 char *process_name, *exe_type;
212                 char *save_ptr = NULL;
213
214                 process_name = strtok_r(buf, ":", &save_ptr);
215                 if (process_name != NULL)
216                         data.process_name = process_name;
217                 else
218                         data.process_name = "none";
219
220                 exe_type = strtok_r(NULL, "\n", &save_ptr);
221                 if (exe_type != NULL)
222                         data.exe_type = exe_type;
223                 else
224                         data.exe_type = "none";
225
226                 if (exception_cb(&data, user_data) == STC_CANCEL)
227                         break;
228         }
229         fclose(fp);
230
231         return error_code;
232 }
233
234 int stc_plugin_exception_initialize(void)
235 {
236         g_excns_hash = g_hash_table_new_full(g_str_hash,
237                                         g_str_equal, g_free, g_free);
238
239         return STC_ERROR_NONE;
240 }
241
242 int stc_plugin_exception_deinitialize(void)
243 {
244         if (g_excns_timer_id > 0) {
245                 g_source_remove(g_excns_timer_id);
246                 g_excns_timer_id = 0;
247         }
248
249         g_hash_table_destroy(g_excns_hash);
250
251         return STC_ERROR_NONE;
252 }
253
254 int stc_plugin_exception_fill_list(void)
255 {
256         table_exceptions_foreach(__insert_exception_cb, NULL);
257         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
258
259         if (STC_DEBUG_LOG)
260                 __excn_hash_printall();
261
262         g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
263                         __update_exceptions_app_list,
264                         NULL);
265
266         return STC_ERROR_NONE;
267 }
268
269 int stc_plugin_exception_update_list(void)
270 {
271         __remove_exception_appall();
272         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
273
274         if (STC_DEBUG_LOG)
275                 __excn_hash_printall();
276
277         return STC_ERROR_NONE;
278 }
279
280 int stc_plugin_exception_check_by_cmdline(char *cmdline)
281 {
282         char *exe_type = NULL;
283
284         exe_type = g_hash_table_lookup(g_excns_hash, cmdline);
285         if (!exe_type)
286                 return STC_ERROR_NO_DATA;
287
288         return STC_ERROR_NONE;
289 }
290
291 API stc_plugin_exception_s stc_plugin_exception = {
292         .initialize_plugin =
293                 stc_plugin_exception_initialize,
294         .deinitialize_plugin =
295                 stc_plugin_exception_deinitialize,
296         .fill_exception_list =
297                 stc_plugin_exception_fill_list,
298         .update_exception_list =
299                 stc_plugin_exception_update_list,
300         .check_exception_by_cmdline =
301                 stc_plugin_exception_check_by_cmdline
302 };
303 //LCOV_EXCL_STOP