4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
32 #include "pkgmgr_installer.h"
33 #include "pkgmgr_installer_config.h"
34 #include "pkgmgr_installer_debug.h"
35 #include "pkgmgr_installer_info.h"
37 #include "../client/include/comm_config.h"
39 #include <pkgmgr-info.h>
41 /* API export macro */
43 #define API __attribute__ ((visibility("default")))
46 #define MAX_STRLEN 1024
47 #define MAX_QUERY_LEN 4096
49 #define CHK_PI_RET(r) \
50 do { if (NULL == pi) return (r); } while (0)
52 struct pkgmgr_installer {
66 GDBusConnection *conn;
69 static uid_t g_target_uid;
71 static const char *__get_signal_name(pkgmgr_installer *pi, const char *key)
73 if (strcmp(key, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0)
74 return COMM_STATUS_BROADCAST_EVENT_INSTALL_PROGRESS;
75 else if (strcmp(key, PKGMGR_INSTALLER_GET_SIZE_KEY_STR) == 0)
76 return COMM_STATUS_BROADCAST_EVENT_GET_SIZE;
77 else if (strcmp(key, PKGMGR_INSTALLER_APPID_KEY_STR) == 0)
78 return COMM_STATUS_BROADCAST_EVENT_UNINSTALL;
80 switch (pi->request_type) {
81 case PKGMGR_REQ_INSTALL:
82 case PKGMGR_REQ_MANIFEST_DIRECT_INSTALL:
83 case PKGMGR_REQ_MOUNT_INSTALL:
84 return COMM_STATUS_BROADCAST_EVENT_INSTALL;
85 case PKGMGR_REQ_UNINSTALL:
86 return COMM_STATUS_BROADCAST_EVENT_UNINSTALL;
87 case PKGMGR_REQ_UPGRADE:
88 return COMM_STATUS_BROADCAST_EVENT_UPGRADE;
90 return COMM_STATUS_BROADCAST_EVENT_MOVE;
91 case PKGMGR_REQ_ENABLE_APP:
92 return COMM_STATUS_BROADCAST_EVENT_ENABLE_APP;
93 case PKGMGR_REQ_DISABLE_APP:
94 return COMM_STATUS_BROADCAST_EVENT_DISABLE_APP;
95 case PKGMGR_REQ_ENABLE_APP_SPLASH_SCREEN:
96 return COMM_STATUS_BROADCAST_EVENT_ENABLE_APP_SPLASH_SCREEN;
97 case PKGMGR_REQ_DISABLE_APP_SPLASH_SCREEN:
98 return COMM_STATUS_BROADCAST_EVENT_DISABLE_APP_SPLASH_SCREEN;
101 ERR("cannot find type, send signal with type SIGNAL_STATUS");
103 return COMM_STATUS_BROADCAST_SIGNAL_STATUS;
106 static int __send_signal_for_app_event(pkgmgr_installer *pi, const char *pkg_type,
107 const char *pkgid, const char *appid, const char *key, const char *val)
113 if (!pi || pi->conn == NULL || appid == NULL)
116 sid = pi->session_id;
120 name = __get_signal_name(pi, key);
122 ERR("unknown signal type");
126 if (g_dbus_connection_emit_signal(pi->conn, NULL,
127 COMM_STATUS_BROADCAST_OBJECT_PATH,
128 COMM_STATUS_BROADCAST_INTERFACE, name,
129 g_variant_new("(ussssss)", pi->target_uid, sid,
130 pkg_type, pkgid, appid, key, val), &err)
132 ERR("failed to send dbus signal: %s", err->message);
140 static int __send_signal_for_event(pkgmgr_installer *pi, const char *pkg_type,
141 const char *pkgid, const char *key, const char *val)
147 if (!pi || pi->conn == NULL)
150 sid = pi->session_id;
154 name = __get_signal_name(pi, key);
156 ERR("unknown signal type");
160 if (g_dbus_connection_emit_signal(pi->conn, NULL,
161 COMM_STATUS_BROADCAST_OBJECT_PATH,
162 COMM_STATUS_BROADCAST_INTERFACE, name,
163 g_variant_new("(ussssss)", pi->target_uid, sid,
164 pkg_type, pkgid, "", key, val), &err)
166 ERR("failed to send dbus signal: %s", err->message);
174 API pkgmgr_installer *pkgmgr_installer_new(void)
176 pkgmgr_installer *pi;
179 pi = calloc(1, sizeof(struct pkgmgr_installer));
183 pi->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
184 if (pi->conn == NULL) {
185 ERR("failed to get bus: %s", err->message);
193 pi->request_type = PKGMGR_REQ_INVALID;
198 API pkgmgr_installer *pkgmgr_installer_offline_new(void)
200 pkgmgr_installer *pi;
202 pi = calloc(1, sizeof(struct pkgmgr_installer));
208 pi->request_type = PKGMGR_REQ_INVALID;
213 API int pkgmgr_installer_free(pkgmgr_installer *pi)
219 free(pi->pkgmgr_info);
221 free(pi->session_id);
222 if (pi->optional_data)
223 free(pi->optional_data);
224 if (pi->caller_pkgid)
225 free(pi->caller_pkgid);
230 g_dbus_connection_flush_sync(pi->conn, NULL, NULL);
231 g_object_unref(pi->conn);
240 pkgmgr_installer_receive_request(pkgmgr_installer *pi,
241 const int argc, char **argv)
248 optind = 1; /* Initialize optind to clear prev. index */
253 pi->target_uid = getuid();
254 g_target_uid = pi->target_uid;
256 c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
257 /* printf("c=%d %c\n", c, c); //debug */
259 break; /* Parse is end */
261 case OPTVAL_PRELOAD: /* request for preload app */
263 DBG("preload request [%d]", pi->is_preload);
265 case OPTVAL_FORCE_REMOVAL: /* request for force-remove */
266 pi->force_removal = 1;
267 DBG("force-remove request [%d]", pi->force_removal);
269 case 'k': /* session id */
271 free(pi->session_id);
272 pi->session_id = strndup(optarg, MAX_STRLEN);
275 case 'l': /* license path */
276 if (pi->license_path)
277 free(pi->license_path);
278 pi->license_path = strndup(optarg, MAX_STRLEN);
281 case 'i': /* install */
287 pi->request_type = PKGMGR_REQ_INSTALL;
289 free(pi->pkgmgr_info);
290 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
291 DBG("option is [i] pkgid[%s]", pi->pkgmgr_info);
292 if (pi->pkgmgr_info && strlen(pi->pkgmgr_info) == 0)
293 free(pi->pkgmgr_info);
298 case 'e': /* install */
301 pi->tep_path = strndup(optarg, MAX_STRLEN);
302 pi->is_tep_included = 1;
303 DBG("option is [e] tep_path[%s]", pi->tep_path);
306 case 'M': /* install */
307 if (strcmp(optarg, "tep_move") == 0)
311 DBG("option is [M] tep_move[%d]", pi->tep_move);
314 case 'd': /* uninstall */
320 pi->request_type = PKGMGR_REQ_UNINSTALL;
322 free(pi->pkgmgr_info);
323 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
327 case 'c': /* clear */
333 pi->request_type = PKGMGR_REQ_CLEAR;
335 free(pi->pkgmgr_info);
336 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
345 pi->request_type = PKGMGR_REQ_MOVE;
347 free(pi->pkgmgr_info);
348 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
351 case 'r': /* reinstall */
357 pi->request_type = PKGMGR_REQ_REINSTALL;
359 free(pi->pkgmgr_info);
360 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
363 case 't': /* move type*/
364 pi->move_type = atoi(optarg);
367 case 'p': /* caller pkgid*/
368 if (pi->caller_pkgid)
369 free(pi->caller_pkgid);
370 pi->caller_pkgid = strndup(optarg, MAX_STRLEN);
374 case 's': /* smack */
380 pi->request_type = PKGMGR_REQ_SMACK;
382 free(pi->pkgmgr_info);
383 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
386 case 'o': /* optional data*/
387 pi->optional_data = strndup(optarg, MAX_STRLEN);
390 case 'y': /* pkgid for direct manifest installation */
392 pi->request_type = PKGMGR_REQ_MANIFEST_DIRECT_INSTALL;
393 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
396 case 'w': /* pkgid for mount installation */
398 pi->request_type = PKGMGR_REQ_MOUNT_INSTALL;
399 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
402 case 'b': /* recovery */
408 pi->request_type = PKGMGR_REQ_RECOVER;
410 free(pi->pkgmgr_info);
411 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
414 case 'D': /* disable pkg */
415 pi->request_type = PKGMGR_REQ_DISABLE_PKG;
417 free(pi->pkgmgr_info);
418 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
421 case 'A': /* enable pkg */
422 pi->request_type = PKGMGR_REQ_ENABLE_PKG;
424 free(pi->pkgmgr_info);
425 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
429 case '?': /* Not an option */
442 API int pkgmgr_installer_get_request_type(pkgmgr_installer *pi)
444 CHK_PI_RET(PKGMGR_REQ_INVALID);
445 return pi->request_type;
448 API const char *pkgmgr_installer_get_request_info(pkgmgr_installer *pi)
450 CHK_PI_RET(PKGMGR_REQ_INVALID);
451 return pi->pkgmgr_info;
454 API const char *pkgmgr_installer_get_tep_path(pkgmgr_installer *pi)
456 CHK_PI_RET(PKGMGR_REQ_INVALID);
460 API int pkgmgr_installer_get_tep_move_type(pkgmgr_installer *pi)
462 CHK_PI_RET(PKGMGR_REQ_INVALID);
466 API const char *pkgmgr_installer_get_session_id(pkgmgr_installer *pi)
468 CHK_PI_RET(PKGMGR_REQ_INVALID);
469 return pi->session_id;
472 API const char *pkgmgr_installer_get_license_path(pkgmgr_installer *pi)
474 CHK_PI_RET(PKGMGR_REQ_INVALID);
475 return pi->license_path;
478 API const char *pkgmgr_installer_get_optional_data(pkgmgr_installer *pi)
480 CHK_PI_RET(PKGMGR_REQ_INVALID);
481 return pi->optional_data;
484 API int pkgmgr_installer_is_quiet(pkgmgr_installer *pi)
489 API int pkgmgr_installer_get_move_type(pkgmgr_installer *pi)
491 CHK_PI_RET(PKGMGR_REQ_INVALID);
492 return pi->move_type;
495 API const char *pkgmgr_installer_get_caller_pkgid(pkgmgr_installer *pi)
497 CHK_PI_RET(PKGMGR_REQ_INVALID);
498 return pi->caller_pkgid;
501 API int pkgmgr_installer_get_is_preload(pkgmgr_installer *pi)
503 CHK_PI_RET(PKGMGR_REQ_INVALID);
504 return pi->is_preload;
507 API int pkgmgr_installer_get_force_removal(pkgmgr_installer *pi)
509 CHK_PI_RET(PKGMGR_REQ_INVALID);
510 return pi->force_removal;
513 API int pkgmgr_installer_send_app_uninstall_signal(pkgmgr_installer *pi,
514 const char *pkg_type,
519 ret = __send_signal_for_event(pi, pkg_type, pkgid,
520 PKGMGR_INSTALLER_APPID_KEY_STR, val);
524 API int pkgmgr_installer_set_uid(pkgmgr_installer *pi, uid_t uid)
529 pi->target_uid = uid;
530 g_target_uid = pi->target_uid;
536 pkgmgr_installer_send_app_signal(pkgmgr_installer *pi,
537 const char *pkg_type,
540 const char *key, const char *val)
545 ERR("connection is NULL");
549 r = __send_signal_for_app_event(pi, pkg_type, pkgid, appid, key, val);
555 pkgmgr_installer_send_signal(pkgmgr_installer *pi,
556 const char *pkg_type,
558 const char *key, const char *val)
563 ERR("connection is NULL");
567 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
568 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
569 pi->request_type = PKGMGR_REQ_UPGRADE;
571 r = __send_signal_for_event(pi, pkg_type, pkgid, key, val);
576 API int pkgmgr_installer_set_request_type(pkgmgr_installer *pi, int request_type)
581 pi->request_type = request_type;
585 API int pkgmgr_installer_set_session_id(pkgmgr_installer *pi, const char *session_id)
587 if (pi == NULL || session_id == NULL)
590 pi->session_id = strndup(session_id, MAX_STRLEN);
594 API int pkgmgr_installer_create_certinfo_set_handle(pkgmgr_instcertinfo_h *handle)
597 ret = pkgmgrinfo_create_certinfo_set_handle(handle);
601 API int pkgmgr_installer_set_cert_value(pkgmgr_instcertinfo_h handle, pkgmgr_instcert_type cert_type, char *cert_value)
604 ret = pkgmgrinfo_set_cert_value(handle, cert_type, cert_value);
608 API int pkgmgr_installer_save_certinfo(const char *pkgid, pkgmgr_instcertinfo_h handle, uid_t uid)
611 ret = pkgmgrinfo_save_certinfo(pkgid, handle, uid);
615 API int pkgmgr_installer_destroy_certinfo_set_handle(pkgmgr_instcertinfo_h handle)
618 ret = pkgmgrinfo_destroy_certinfo_set_handle(handle);
622 API int pkgmgr_installer_delete_certinfo(const char *pkgid)
625 ret = pkgmgrinfo_delete_certinfo(pkgid);
629 API int pkgmgr_installer_info_get_target_uid(uid_t *uid)