2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <syspopup_caller.h>
23 #include <bundle_internal.h>
26 #include <pkgmgr-info.h>
28 #include "stc-plugin-exception.h"
30 #define EXE_TYPE_APPLICATION "app"
31 #define EXE_TYPE_INSTRUCTION "inst"
32 #define EXE_TYPE_SYSTEM "sys"
33 #define EXE_TYPE_SCRIPT "script"
36 #define EXCNS_TIMER_INTERVAL 86400
38 #define EXCEPTION_BUF_MAX 64
39 #define EXCEPTION_STORAGE "/var/lib/stc/exceptions"
41 #define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet"
46 } stc_exceptions_info;
49 (*stc_exceptions_info_cb)(const stc_exceptions_info *info,
52 static GHashTable *g_excns_hash; /**< exception hash table */
53 static GHashTable *g_pkginfo_filter_hash;
54 static guint g_excns_timer_id;
56 static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
61 ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
62 if (ret == PMINFO_R_OK) {
63 if (g_hash_table_insert(g_pkginfo_filter_hash,
64 g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE)
65 STC_LOGE("Failed to insert hash table");
71 static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
75 char *exe_type = NULL;
76 stc_exceptions_info data;
77 const stc_exceptions_info_cb excn_cb = user_data;
79 ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
80 if (ret == PMINFO_R_OK) {
81 exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname);
85 data.process_name = pkgname;
86 data.exe_type = EXE_TYPE_APPLICATION;
88 if (excn_cb(&data, NULL) == STC_CANCEL)
89 STC_LOGE("Failed to insert hash table");
95 static void __excn_hash_foreach_print(gpointer key, gpointer value,
98 const char *process_name = key;
99 const char *exe_type = value;
101 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
102 process_name, exe_type);
105 static void __excn_hash_printall(void)
107 g_hash_table_foreach(g_excns_hash,
108 __excn_hash_foreach_print, NULL);
111 static gboolean __remove_exception_app(gpointer key, gpointer value,
114 const char *exe_type = value;
116 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
122 static void __remove_exception_appall(void)
124 g_hash_table_foreach_remove(g_excns_hash,
125 __remove_exception_app, NULL);
128 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
131 stc_cb_ret_e ret = STC_CONTINUE;
133 if (g_hash_table_insert(g_excns_hash,
134 g_strdup(info->process_name),
135 g_strdup(info->exe_type)) != TRUE)
141 static gboolean __update_exceptions_app_list(void *user_data)
143 stc_plugin_exception_update_list();
147 static stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
151 int err = STC_ERROR_NONE;
152 pkgmgrinfo_pkginfo_filter_h handle;
154 g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash,
155 g_str_equal, g_free, g_free);
157 ret = pkgmgrinfo_pkginfo_filter_create(&handle);
158 ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL,
159 "Failed to create pkginfo filter");
161 ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
162 PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,
164 if (ret != PMINFO_R_OK) {
165 STC_LOGE("Failed to add pkginfo filter string");
166 err = STC_ERROR_FAIL;
170 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle,
171 __pkginfo_filter_list_cb, NULL);
172 if (ret != PMINFO_R_OK) {
173 STC_LOGE("Failed to foreach pkginfo filter");
174 err = STC_ERROR_FAIL;
178 ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb);
179 if (ret != PMINFO_R_OK) {
180 STC_LOGE("Failed to get pkginfo list");
181 err = STC_ERROR_FAIL;
186 if (g_pkginfo_filter_hash) {
187 g_hash_table_destroy(g_pkginfo_filter_hash);
188 g_pkginfo_filter_hash = NULL;
192 pkgmgrinfo_pkginfo_filter_destroy(handle);
197 static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
200 stc_error_e error_code = STC_ERROR_NONE;
201 stc_exceptions_info data;
204 char buf[EXCEPTION_BUF_MAX] = {0, };
206 fp = fopen(EXCEPTION_STORAGE, "r");
207 ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file");
209 while (fgets(buf, sizeof(buf), fp) != NULL) {
210 char *process_name, *exe_type;
211 char *save_ptr = NULL;
213 process_name = strtok_r(buf, ":", &save_ptr);
214 if (process_name != NULL)
215 data.process_name = process_name;
217 data.process_name = "none";
219 exe_type = strtok_r(NULL, "\n", &save_ptr);
220 if (exe_type != NULL)
221 data.exe_type = exe_type;
223 data.exe_type = "none";
225 if (exception_cb(&data, user_data) == STC_CANCEL)
233 int stc_plugin_exception_initialize(void)
235 g_excns_hash = g_hash_table_new_full(g_str_hash,
236 g_str_equal, g_free, g_free);
238 return STC_ERROR_NONE;
241 int stc_plugin_exception_deinitialize(void)
243 if (g_excns_timer_id > 0) {
244 g_source_remove(g_excns_timer_id);
245 g_excns_timer_id = 0;
248 g_hash_table_destroy(g_excns_hash);
250 return STC_ERROR_NONE;
253 int stc_plugin_exception_fill_list(void)
255 table_exceptions_foreach(__insert_exception_cb, NULL);
256 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
259 __excn_hash_printall();
261 g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
262 __update_exceptions_app_list,
265 return STC_ERROR_NONE;
268 int stc_plugin_exception_update_list(void)
270 __remove_exception_appall();
271 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
274 __excn_hash_printall();
276 return STC_ERROR_NONE;
279 int stc_plugin_exception_check_by_cmdline(char *cmdline)
281 char *exe_type = NULL;
283 exe_type = g_hash_table_lookup(g_excns_hash, cmdline);
285 return STC_ERROR_NO_DATA;
287 return STC_ERROR_NONE;
290 API stc_plugin_exception_s stc_plugin_exception = {
292 stc_plugin_exception_initialize,
293 .deinitialize_plugin =
294 stc_plugin_exception_deinitialize,
295 .fill_exception_list =
296 stc_plugin_exception_fill_list,
297 .update_exception_list =
298 stc_plugin_exception_update_list,
299 .check_exception_by_cmdline =
300 stc_plugin_exception_check_by_cmdline