Added application exception list
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-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 /**
18  * This file implements exceptions entity handler methods.
19  *
20  * @file        stc-exception.c
21  */
22
23 #include <pkgmgr-info.h>
24
25 #include "stc-manager.h"
26 #include "stc-exception.h"
27
28 #define EXCEPTION_BUF_MAX   64
29 #define EXCEPTION_STORAGE   "/var/lib/stc/exceptions"
30
31 #define INTERNET_PRIVILEGE  "http://tizen.org/privilege/internet"
32
33 static GHashTable *g_pkginfo_filter_hash;
34
35 stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
36                                        void *user_data)
37 {
38         __STC_LOG_FUNC_ENTER__;
39
40         stc_error_e error_code = STC_ERROR_NONE;
41         stc_exceptions_info data;
42
43         FILE *fp = NULL;
44         char buf[EXCEPTION_BUF_MAX] = {0, };
45
46         fp = fopen(EXCEPTION_STORAGE, "r");
47         ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file");
48
49         while (fgets(buf, sizeof(buf), fp) != NULL) {
50                 char *process_name, *exe_type;
51                 char *save_ptr = NULL;
52
53                 process_name = strtok_r(buf, ":", &save_ptr);
54                 if (process_name != NULL)
55                         data.process_name = process_name;
56                 else
57                         data.process_name = "none";
58
59                 exe_type = strtok_r(NULL, "\n", &save_ptr);
60                 if (exe_type != NULL)
61                         data.exe_type = exe_type;
62                 else
63                         data.exe_type = "none";
64
65                 if (exception_cb(&data, user_data) == STC_CANCEL)
66                         break;
67         }
68         fclose(fp);
69
70         __STC_LOG_FUNC_EXIT__;
71         return error_code;
72 }
73
74 static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
75 {
76         int ret = 0;
77         char *pkgname = NULL;
78
79         ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
80         if (ret == PMINFO_R_OK) {
81                 if (g_hash_table_insert(g_pkginfo_filter_hash,
82                                 g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE)
83                         STC_LOGE("Failed to insert hash table");
84         }
85
86         return STC_CONTINUE;
87 }
88
89 static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
90 {
91         int ret = 0;
92         char *pkgname = NULL;
93         char *exe_type = NULL;
94         stc_exceptions_info data;
95         const stc_exceptions_info_cb excn_cb = user_data;
96
97         ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname);
98         if (ret == PMINFO_R_OK) {
99                 exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname);
100                 if (exe_type)
101                         return STC_CONTINUE;
102
103                 data.process_name = pkgname;
104                 data.exe_type = EXE_TYPE_APPLICATION;
105
106                 if (excn_cb(&data, NULL) == STC_CANCEL)
107                         STC_LOGE("Failed to insert hash table");
108         }
109
110         return STC_CONTINUE;
111 }
112
113 stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb,
114                                        void *user_data)
115 {
116         __STC_LOG_FUNC_ENTER__;
117
118         int ret = 0;
119         int err = STC_ERROR_NONE;
120         pkgmgrinfo_pkginfo_filter_h handle;
121
122         g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash,
123                                                 g_str_equal, g_free, g_free);
124
125         ret = pkgmgrinfo_pkginfo_filter_create(&handle);
126         ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL,
127                         "Failed to create pkginfo filter");
128
129         ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
130                         PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,
131                         INTERNET_PRIVILEGE);
132         if (ret != PMINFO_R_OK) {
133                 STC_LOGE("Failed to add pkginfo filter string");
134                 err = STC_ERROR_FAIL;
135                 goto out;
136         }
137
138         ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle,
139                         __pkginfo_filter_list_cb, NULL);
140         if (ret != PMINFO_R_OK) {
141                 STC_LOGE("Failed to foreach pkginfo filter");
142                 err = STC_ERROR_FAIL;
143                 goto out;
144         }
145
146         ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb);
147         if (ret != PMINFO_R_OK) {
148                 STC_LOGE("Failed to get pkginfo list");
149                 err = STC_ERROR_FAIL;
150                 goto out;
151         }
152
153 out:
154         if (g_pkginfo_filter_hash) {
155                 g_hash_table_destroy(g_pkginfo_filter_hash);
156                 g_pkginfo_filter_hash = NULL;
157         }
158
159         if (handle)
160                 pkgmgrinfo_pkginfo_filter_destroy(handle);
161
162         __STC_LOG_FUNC_EXIT__;
163         return err;
164 }