2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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.
20 #include <sys/types.h>
26 #include "package-manager.h"
27 #include "pkgmgr_client_debug.h"
28 #include "pkgmgr_client_internal.h"
29 #include "../../installer/pkgmgr_installer.h"
30 #include "../../installer/pkgmgr_installer_config.h"
32 #define CONNECTION_RETRY_MAX 5
33 #define CONNECTION_WAIT_USEC (1000000 / 2) /* 0.5 sec */
34 #define CONNECTION_TIMEOUT_MSEC 5000 /* 5 sec */
35 #define REGULAR_USER 5000
37 static int _is_system_user(void)
41 if (uid < REGULAR_USER)
47 static GBusType __get_bus_type(pkgmgr_client_type type)
49 if (type == PC_REQUEST || _is_system_user())
50 return G_BUS_TYPE_SYSTEM;
52 return G_BUS_TYPE_SESSION;
55 int pkgmgr_client_connection_connect(struct pkgmgr_client_t *pc)
60 #if !GLIB_CHECK_VERSION(2, 35, 0)
63 bus_type = __get_bus_type(pc->pc_type);
64 pc->conn = g_bus_get_sync(bus_type, NULL, &error);
66 ERR("gdbus connection error (%s)", error->message);
68 return PKGMGR_R_ECOMM;
74 void pkgmgr_client_connection_disconnect(struct pkgmgr_client_t *pc)
76 /* flush remaining buffer: blocking mode */
77 g_dbus_connection_flush_sync(pc->conn, NULL, NULL);
78 g_object_unref(pc->conn);
83 const char *signal_str;
87 struct signal_map map[] = {
88 {PKGMGR_INSTALLER_INSTALL_EVENT_STR, PKGMGR_CLIENT_STATUS_INSTALL},
89 {PKGMGR_INSTALLER_UNINSTALL_EVENT_STR, PKGMGR_CLIENT_STATUS_UNINSTALL},
90 {PKGMGR_INSTALLER_UPGRADE_EVENT_STR, PKGMGR_CLIENT_STATUS_UPGRADE},
91 {PKGMGR_INSTALLER_CLEAR_EVENT_STR, PKGMGR_CLIENT_STATUS_CLEAR_DATA},
92 {PKGMGR_INSTALLER_MOVE_EVENT_STR, PKGMGR_CLIENT_STATUS_MOVE},
93 {PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
94 PKGMGR_CLIENT_STATUS_INSTALL_PROGRESS},
95 {PKGMGR_INSTALLER_GET_SIZE_KEY_STR, PKGMGR_CLIENT_STATUS_GET_SIZE},
96 {PKGMGR_INSTALLER_CLEAR_CACHE_KEY_STR,
97 PKGMGR_CLIENT_STATUS_CLEAR_CACHE},
98 {PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR,
99 PKGMGR_CLIENT_STATUS_ENABLE_APP},
100 {PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR,
101 PKGMGR_CLIENT_STATUS_DISABLE_APP},
102 {PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR,
103 PKGMGR_CLIENT_STATUS_ENABLE_APP_SPLASH_SCREEN},
104 {PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR,
105 PKGMGR_CLIENT_STATUS_DISABLE_APP_SPLASH_SCREEN},
109 static int __get_signal_type(const char *name)
116 for (i = 0; map[i].signal_str != NULL; i++) {
117 if (strcmp(map[i].signal_str, name) == 0)
118 return map[i].signal_type;
124 static void __handle_size_info_callback(struct cb_info *cb_info,
125 const char *pkgid, const char *val)
127 pkg_size_info_t size_info;
131 pkgmgr_total_pkg_size_info_receive_cb callback;
133 snprintf(buf, sizeof(buf), "%s", val);
135 DBG("%s, %s", pkgid, val);
137 token = strtok_r(buf, ":", &saveptr);
139 ERR("failed to parse size info");
142 size_info.data_size = atoll(token);
143 token = strtok_r(NULL, ":", &saveptr);
145 ERR("failed to parse size info");
148 size_info.cache_size = atoll(token);
149 token = strtok_r(NULL, ":", &saveptr);
151 ERR("failed to parse size info");
154 size_info.app_size = atoll(token);
155 token = strtok_r(NULL, ":", &saveptr);
157 ERR("failed to parse size info");
160 size_info.ext_data_size = atoll(token);
161 token = strtok_r(NULL, ":", &saveptr);
163 ERR("failed to parse size info");
166 size_info.ext_cache_size = atoll(token);
167 token = strtok_r(NULL, ":", &saveptr);
169 ERR("failed to parse size info");
172 size_info.ext_app_size = atoll(token);
174 DBG("data: %lld, cache: %lld, app: %lld, ext_data: %lld, "
175 "ext_cache: %lld, ext_app: %lld",
176 size_info.data_size, size_info.cache_size,
177 size_info.app_size, size_info.ext_data_size,
178 size_info.ext_cache_size, size_info.ext_app_size);
180 if (strcmp(pkgid, PKG_SIZE_INFO_TOTAL) == 0) {
181 callback = (pkgmgr_total_pkg_size_info_receive_cb)
182 cb_info->size_info_cb;
183 callback(cb_info->client, &size_info, cb_info->data);
185 cb_info->size_info_cb(cb_info->client, pkgid, &size_info,
190 static void __signal_handler(GDBusConnection *conn, const gchar *sender_name,
191 const gchar *object_path, const gchar *interface_name,
192 const gchar *signal_name, GVariant *parameters,
197 char *pkg_type = NULL;
203 struct cb_info *cb_info = (struct cb_info *)user_data;
205 g_variant_get(parameters, "(u&s&s&s&s&s&s)",
206 &target_uid, &req_id, &pkg_type,
207 &pkgid, &appid, &key, &val);
209 /* in case of request, check reqkey */
210 if (cb_info->req_key) {
211 if (strcmp(cb_info->req_key, req_id) != 0)
214 signal_type = __get_signal_type(signal_name);
215 if (signal_type < 0 || !(cb_info->status_type & signal_type))
219 /* each cb_data can only has one callback */
220 if (cb_info->event_cb && strcmp(appid, "") == 0)
221 cb_info->event_cb(target_uid, cb_info->req_id,
222 pkg_type, pkgid, key, val, NULL, cb_info->data);
223 else if (cb_info->app_event_cb && strcmp(appid, "") != 0)
224 cb_info->app_event_cb(target_uid, cb_info->req_id,
225 pkg_type, pkgid, appid, key, val, NULL,
227 else if (cb_info->size_info_cb)
228 __handle_size_info_callback(cb_info, pkgid, val);
230 /* TODO: unsubscribe request callback */
233 int pkgmgr_client_connection_set_callback(struct pkgmgr_client_t *pc,
234 struct cb_info *cb_info)
236 cb_info->sid = g_dbus_connection_signal_subscribe(pc->conn, NULL,
237 PKGMGR_INSTALLER_DBUS_INTERFACE, NULL,
238 PKGMGR_INSTALLER_DBUS_OBJECT_PATH, NULL,
239 G_DBUS_SIGNAL_FLAGS_NONE, __signal_handler,
240 (gpointer)cb_info, NULL);
242 ERR("failed to subscribe singal");
243 return PKGMGR_R_ERROR;
249 void pkgmgr_client_connection_unset_callback(struct pkgmgr_client_t *pc,
250 struct cb_info *cb_info)
252 g_dbus_connection_signal_unsubscribe(pc->conn, cb_info->sid);
255 int pkgmgr_client_connection_send_request(struct pkgmgr_client_t *pc,
256 const char *method, GVariant *params, GVariant **result)
258 GError *error = NULL;
262 int ret = PKGMGR_R_ECOMM;
264 /* convert floating ref into normal ref */
265 g_variant_ref_sink(params);
268 proxy = g_dbus_proxy_new_sync(pc->conn,
269 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
270 NULL, PKGMGR_DBUS_SERVICE,
271 PKGMGR_DBUS_OBJECT_PATH,
272 PKGMGR_DBUS_INTERFACE, NULL, &error);
274 ERR("failed to get proxy object, sleep and retry[%s]",
278 usleep(CONNECTION_WAIT_USEC);
283 r = g_dbus_proxy_call_sync(proxy, method, params,
284 G_DBUS_CALL_FLAGS_NONE,
285 CONNECTION_TIMEOUT_MSEC, NULL, &error);
286 g_object_unref(proxy);
287 if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED) {
288 ERR("failed to send request, privilege denied[%s]",
291 ret = PKGMGR_R_EPRIV;
300 ERR("failed to send request, sleep and retry[%s]",
304 usleep(CONNECTION_WAIT_USEC);
306 } while (retry_cnt <= CONNECTION_RETRY_MAX);
308 /* decrease ref count to 0 to free resource */
309 g_variant_unref(params);