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"
36 #include "../client/include/comm_config.h"
38 #include <pkgmgr-info.h>
40 /* API export macro */
42 #define API __attribute__ ((visibility("default")))
45 #define MAX_STRLEN 1024
46 #define MAX_QUERY_LEN 4096
48 #define CHK_PI_RET(r) \
49 do { if (NULL == pi) return (r); } while (0)
51 struct pkgmgr_installer {
65 GDBusConnection *conn;
68 static const char *__get_signal_name(pkgmgr_installer *pi, const char *key)
70 if (strcmp(key, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0)
71 return COMM_STATUS_BROADCAST_EVENT_INSTALL_PROGRESS;
72 else if (strcmp(key, PKGMGR_INSTALLER_GET_SIZE_KEY_STR) == 0)
73 return COMM_STATUS_BROADCAST_EVENT_GET_SIZE;
74 else if (strcmp(key, PKGMGR_INSTALLER_APPID_KEY_STR) == 0)
75 return COMM_STATUS_BROADCAST_EVENT_UNINSTALL;
77 switch (pi->request_type) {
78 case PKGMGR_REQ_INSTALL:
79 return COMM_STATUS_BROADCAST_EVENT_INSTALL;
80 case PKGMGR_REQ_UNINSTALL:
81 return COMM_STATUS_BROADCAST_EVENT_UNINSTALL;
82 case PKGMGR_REQ_UPGRADE:
83 return COMM_STATUS_BROADCAST_EVENT_UPGRADE;
85 return COMM_STATUS_BROADCAST_EVENT_MOVE;
86 case PKGMGR_REQ_ENABLE_APP:
87 return COMM_STATUS_BROADCAST_EVENT_ENABLE_APP;
88 case PKGMGR_REQ_DISABLE_APP:
89 return COMM_STATUS_BROADCAST_EVENT_DISABLE_APP;
92 ERR("cannot find type, send signal with type SIGNAL_STATUS");
94 return COMM_STATUS_BROADCAST_SIGNAL_STATUS;
97 static int __send_signal_for_app_event(pkgmgr_installer *pi, const char *pkg_type,
98 const char *pkgid, const char *appid, const char *key, const char *val)
104 if (!pi || pi->conn == NULL || appid == NULL)
107 sid = pi->session_id;
111 name = __get_signal_name(pi, key);
113 ERR("unknown signal type");
117 if (g_dbus_connection_emit_signal(pi->conn, NULL,
118 COMM_STATUS_BROADCAST_OBJECT_PATH,
119 COMM_STATUS_BROADCAST_INTERFACE, name,
120 g_variant_new("(ussssss)", pi->target_uid, sid,
121 pkg_type, pkgid, appid, key, val), &err)
123 ERR("failed to send dbus signal: %s", err->message);
131 static int __send_signal_for_event(pkgmgr_installer *pi, const char *pkg_type,
132 const char *pkgid, const char *key, const char *val)
138 if (!pi || pi->conn == NULL)
141 sid = pi->session_id;
145 name = __get_signal_name(pi, key);
147 ERR("unknown signal type");
151 if (g_dbus_connection_emit_signal(pi->conn, NULL,
152 COMM_STATUS_BROADCAST_OBJECT_PATH,
153 COMM_STATUS_BROADCAST_INTERFACE, name,
154 g_variant_new("(ussssss)", pi->target_uid, sid,
155 pkg_type, pkgid, "", key, val), &err)
157 ERR("failed to send dbus signal: %s", err->message);
165 API pkgmgr_installer *pkgmgr_installer_new(void)
167 pkgmgr_installer *pi;
170 pi = calloc(1, sizeof(struct pkgmgr_installer));
174 pi->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
175 if (pi->conn == NULL) {
176 ERR("failed to get bus: %s", err->message);
184 pi->request_type = PKGMGR_REQ_INVALID;
189 API pkgmgr_installer *pkgmgr_installer_offline_new(void)
191 pkgmgr_installer *pi;
193 pi = calloc(1, sizeof(struct pkgmgr_installer));
199 pi->request_type = PKGMGR_REQ_INVALID;
204 API int pkgmgr_installer_free(pkgmgr_installer *pi)
210 free(pi->pkgmgr_info);
212 free(pi->session_id);
213 if (pi->optional_data)
214 free(pi->optional_data);
215 if (pi->caller_pkgid)
216 free(pi->caller_pkgid);
221 g_dbus_connection_flush_sync(pi->conn, NULL, NULL);
222 g_object_unref(pi->conn);
231 pkgmgr_installer_receive_request(pkgmgr_installer *pi,
232 const int argc, char **argv)
239 optind = 1; /* Initialize optind to clear prev. index */
244 pi->target_uid = getuid();
246 c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
247 /* printf("c=%d %c\n", c, c); //debug */
249 break; /* Parse is end */
251 case OPTVAL_PRELOAD: /* request for preload app */
253 DBG("preload request [%d]", pi->is_preload );
255 case OPTVAL_FORCE_REMOVAL: /* request for force-remove */
256 pi->force_removal = 1;
257 DBG("force-remove request [%d]", pi->force_removal );
259 case 'k': /* session id */
261 free(pi->session_id);
262 pi->session_id = strndup(optarg, MAX_STRLEN);
265 case 'l': /* license path */
266 if (pi->license_path)
267 free(pi->license_path);
268 pi->license_path = strndup(optarg, MAX_STRLEN);
271 case 'i': /* install */
277 pi->request_type = PKGMGR_REQ_INSTALL;
279 free(pi->pkgmgr_info);
280 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
281 DBG("option is [i] pkgid[%s]", pi->pkgmgr_info );
282 if (pi->pkgmgr_info && strlen(pi->pkgmgr_info)==0){
283 free(pi->pkgmgr_info);
289 case 'e': /* install */
292 pi->tep_path = strndup(optarg, MAX_STRLEN);
293 pi->is_tep_included = 1;
294 DBG("option is [e] tep_path[%s]", pi->tep_path);
297 case 'M': /* install */
298 if (strcmp(optarg, "tep_move") == 0)
302 DBG("option is [M] tep_move[%d]", pi->tep_move);
305 case 'd': /* uninstall */
311 pi->request_type = PKGMGR_REQ_UNINSTALL;
313 free(pi->pkgmgr_info);
314 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
318 case 'c': /* clear */
324 pi->request_type = PKGMGR_REQ_CLEAR;
326 free(pi->pkgmgr_info);
327 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
336 pi->request_type = PKGMGR_REQ_MOVE;
338 free(pi->pkgmgr_info);
339 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
342 case 'r': /* reinstall */
348 pi->request_type = PKGMGR_REQ_REINSTALL;
350 free(pi->pkgmgr_info);
351 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
354 case 't': /* move type*/
355 pi->move_type = atoi(optarg);
358 case 'p': /* caller pkgid*/
359 if (pi->caller_pkgid)
360 free(pi->caller_pkgid);
361 pi->caller_pkgid = strndup(optarg, MAX_STRLEN);
365 case 's': /* smack */
371 pi->request_type = PKGMGR_REQ_SMACK;
373 free(pi->pkgmgr_info);
374 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
377 case 'o': /* optional data*/
378 pi->optional_data = strndup(optarg, MAX_STRLEN);
381 case 'y': /* pkgid for direct manifest installation */
383 pi->request_type = PKGMGR_REQ_MANIFEST_DIRECT_INSTALL;
384 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
387 case 'b': /* recovery */
393 pi->request_type = PKGMGR_REQ_RECOVER;
395 free(pi->pkgmgr_info);
396 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
400 case '?': /* Not an option */
413 API int pkgmgr_installer_get_request_type(pkgmgr_installer *pi)
415 CHK_PI_RET(PKGMGR_REQ_INVALID);
416 return pi->request_type;
419 API const char *pkgmgr_installer_get_request_info(pkgmgr_installer *pi)
421 CHK_PI_RET(PKGMGR_REQ_INVALID);
422 return pi->pkgmgr_info;
425 API const char *pkgmgr_installer_get_tep_path(pkgmgr_installer *pi)
427 CHK_PI_RET(PKGMGR_REQ_INVALID);
431 API int pkgmgr_installer_get_tep_move_type(pkgmgr_installer *pi)
433 CHK_PI_RET(PKGMGR_REQ_INVALID);
437 API const char *pkgmgr_installer_get_session_id(pkgmgr_installer *pi)
439 CHK_PI_RET(PKGMGR_REQ_INVALID);
440 return pi->session_id;
443 API const char *pkgmgr_installer_get_license_path(pkgmgr_installer *pi)
445 CHK_PI_RET(PKGMGR_REQ_INVALID);
446 return pi->license_path;
449 API const char *pkgmgr_installer_get_optional_data(pkgmgr_installer *pi)
451 CHK_PI_RET(PKGMGR_REQ_INVALID);
452 return pi->optional_data;
455 API int pkgmgr_installer_is_quiet(pkgmgr_installer *pi)
460 API int pkgmgr_installer_get_move_type(pkgmgr_installer *pi)
462 CHK_PI_RET(PKGMGR_REQ_INVALID);
463 return pi->move_type;
466 API const char *pkgmgr_installer_get_caller_pkgid(pkgmgr_installer *pi)
468 CHK_PI_RET(PKGMGR_REQ_INVALID);
469 return pi->caller_pkgid;
472 API int pkgmgr_installer_get_is_preload(pkgmgr_installer *pi)
474 CHK_PI_RET(PKGMGR_REQ_INVALID);
475 return pi->is_preload;
478 API int pkgmgr_installer_get_force_removal(pkgmgr_installer *pi)
480 CHK_PI_RET(PKGMGR_REQ_INVALID);
481 return pi->force_removal;
484 API int pkgmgr_installer_send_app_uninstall_signal(pkgmgr_installer *pi,
485 const char *pkg_type,
490 ret = __send_signal_for_event(pi, pkg_type, pkgid,
491 PKGMGR_INSTALLER_APPID_KEY_STR, val);
495 API int pkgmgr_installer_set_uid(pkgmgr_installer *pi, uid_t uid)
500 pi->target_uid = uid;
506 pkgmgr_installer_send_app_signal(pkgmgr_installer *pi,
507 const char *pkg_type,
510 const char *key, const char *val)
515 ERR("connection is NULL");
519 r = __send_signal_for_app_event(pi, pkg_type, pkgid, appid, key, val);
525 pkgmgr_installer_send_signal(pkgmgr_installer *pi,
526 const char *pkg_type,
528 const char *key, const char *val)
533 ERR("connection is NULL");
537 if (strcmp(key, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
538 pi->request_type = PKGMGR_REQ_UPGRADE;
540 r = __send_signal_for_event(pi, pkg_type, pkgid, key, val);
545 API int pkgmgr_installer_set_request_type(pkgmgr_installer *pi, int request_type)
550 pi->request_type = request_type;
554 API int pkgmgr_installer_set_session_id(pkgmgr_installer *pi, const char *session_id)
556 if (pi == NULL || session_id == NULL)
559 pi->session_id = strndup(session_id, MAX_STRLEN);
563 API int pkgmgr_installer_create_certinfo_set_handle(pkgmgr_instcertinfo_h *handle)
566 ret = pkgmgrinfo_create_certinfo_set_handle(handle);
570 API int pkgmgr_installer_set_cert_value(pkgmgr_instcertinfo_h handle, pkgmgr_instcert_type cert_type, char *cert_value)
573 ret = pkgmgrinfo_set_cert_value(handle, cert_type, cert_value);
577 API int pkgmgr_installer_save_certinfo(const char *pkgid, pkgmgr_instcertinfo_h handle, uid_t uid)
580 ret = pkgmgrinfo_save_certinfo(pkgid, handle, uid);
584 API int pkgmgr_installer_destroy_certinfo_set_handle(pkgmgr_instcertinfo_h handle)
587 ret = pkgmgrinfo_destroy_certinfo_set_handle(handle);
591 API int pkgmgr_installer_delete_certinfo(const char *pkgid)
594 ret = pkgmgrinfo_delete_certinfo(pkgid);