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"
31 #define EXE_TYPE_APPLICATION "app"
32 #define EXE_TYPE_INSTRUCTION "inst"
33 #define EXE_TYPE_SYSTEM "sys"
34 #define EXE_TYPE_SCRIPT "script"
37 #define EXCNS_TIMER_INTERVAL 86400
39 #define EXCEPTION_BUF_MAX 64
40 #define EXCEPTION_STORAGE "/var/lib/stc/exceptions"
42 #define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet"
47 } stc_exceptions_info;
50 (*stc_exceptions_info_cb)(const stc_exceptions_info *info,
53 static GHashTable *g_excns_hash; /**< exception hash table */
54 static GHashTable *g_pkginfo_filter_hash;
55 static guint g_excns_timer_id;
57 static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
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");
72 static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
76 char *exe_type = NULL;
77 stc_exceptions_info data;
78 const stc_exceptions_info_cb excn_cb = user_data;
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);
86 data.process_name = pkgname;
87 data.exe_type = EXE_TYPE_APPLICATION;
89 if (excn_cb(&data, NULL) == STC_CANCEL)
90 STC_LOGE("Failed to insert hash table");
96 static void __excn_hash_foreach_print(gpointer key, gpointer value,
99 const char *process_name = key;
100 const char *exe_type = value;
102 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
103 process_name, exe_type);
106 static void __excn_hash_printall(void)
108 g_hash_table_foreach(g_excns_hash,
109 __excn_hash_foreach_print, NULL);
112 static gboolean __remove_exception_app(gpointer key, gpointer value,
115 const char *exe_type = value;
117 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
123 static void __remove_exception_appall(void)
125 g_hash_table_foreach_remove(g_excns_hash,
126 __remove_exception_app, NULL);
129 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
132 stc_cb_ret_e ret = STC_CONTINUE;
134 if (g_hash_table_insert(g_excns_hash,
135 g_strdup(info->process_name),
136 g_strdup(info->exe_type)) != TRUE)
142 static gboolean __update_exceptions_app_list(void *user_data)
144 stc_plugin_exception_update_list();
148 static stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
152 int err = STC_ERROR_NONE;
153 pkgmgrinfo_pkginfo_filter_h handle;
155 g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash,
156 g_str_equal, g_free, g_free);
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");
162 ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
163 PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,
165 if (ret != PMINFO_R_OK) {
166 STC_LOGE("Failed to add pkginfo filter string");
167 err = STC_ERROR_FAIL;
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;
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;
187 if (g_pkginfo_filter_hash) {
188 g_hash_table_destroy(g_pkginfo_filter_hash);
189 g_pkginfo_filter_hash = NULL;
193 pkgmgrinfo_pkginfo_filter_destroy(handle);
198 static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
201 stc_error_e error_code = STC_ERROR_NONE;
202 stc_exceptions_info data;
205 char buf[EXCEPTION_BUF_MAX] = {0, };
207 fp = fopen(EXCEPTION_STORAGE, "r");
208 ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file",
211 while (fgets(buf, sizeof(buf), fp) != NULL) {
212 char *process_name, *exe_type;
213 char *save_ptr = NULL;
215 process_name = strtok_r(buf, ":", &save_ptr);
216 if (process_name != NULL)
217 data.process_name = process_name;
219 data.process_name = "none";
221 exe_type = strtok_r(NULL, "\n", &save_ptr);
222 if (exe_type != NULL)
223 data.exe_type = exe_type;
225 data.exe_type = "none";
227 if (exception_cb(&data, user_data) == STC_CANCEL)
235 int stc_plugin_exception_initialize(void)
237 g_excns_hash = g_hash_table_new_full(g_str_hash,
238 g_str_equal, g_free, g_free);
240 return STC_ERROR_NONE;
243 int stc_plugin_exception_deinitialize(void)
245 if (g_excns_timer_id > 0) {
246 g_source_remove(g_excns_timer_id);
247 g_excns_timer_id = 0;
250 g_hash_table_destroy(g_excns_hash);
252 return STC_ERROR_NONE;
255 int stc_plugin_exception_fill_list(void)
257 table_exceptions_foreach(__insert_exception_cb, NULL);
258 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
261 __excn_hash_printall();
263 g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
264 __update_exceptions_app_list,
267 return STC_ERROR_NONE;
270 int stc_plugin_exception_update_list(void)
272 __remove_exception_appall();
273 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
276 __excn_hash_printall();
278 return STC_ERROR_NONE;
281 int stc_plugin_exception_check_by_cmdline(char *cmdline)
283 char *exe_type = NULL;
285 exe_type = g_hash_table_lookup(g_excns_hash, cmdline);
287 return STC_ERROR_NO_DATA;
289 return STC_ERROR_NONE;
292 API stc_plugin_exception_s stc_plugin_exception = {
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