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.
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #include <tzplatform_config.h>
37 #include "pkgmgr_installer.h"
38 #include "pkgmgr_installer_config.h"
39 #include "pkgmgr_installer_debug.h"
40 #include "pkgmgr_installer_info.h"
42 #include <pkgmgr-info.h>
44 /* API export macro */
46 #define API __attribute__ ((visibility("default")))
49 #define MAX_STRLEN 1024
50 #define MAX_QUERY_LEN 4096
52 #define CHK_PI_RET(r) \
53 do { if (NULL == pi) return (r); } while (0)
55 #define OPTVAL_PRELOAD 1000
56 #define OPTVAL_FORCE_REMOVAL 1001
57 #define OPTVAL_PRELOAD_RW 1002
58 #define OPTVAL_NO_REMOVAL 1003
60 /* Supported options */
61 const char *short_opts = "k:l:i:d:c:m:t:o:r:p:s:b:e:M:y:u:w:D:A:q";
62 const struct option long_opts[] = {
63 { "session-id", 1, NULL, 'k' },
64 { "license-path", 1, NULL, 'l' },
65 { "install", 1, NULL, 'i' },
66 { "uninstall", 1, NULL, 'd' },
67 { "clear", 1, NULL, 'c' },
68 { "move", 1, NULL, 'm' },
69 { "move-type", 1, NULL, 't' },
70 { "optional-data", 0, NULL, 'o' },
71 { "reinstall", 0, NULL, 'r' },
72 { "caller-pkgid", 1, NULL, 'p' },
73 { "tep-path", 1, NULL, 'e' },
74 { "tep-move", 1, NULL, 'M' },
75 { "smack", 1, NULL, 's' },
76 { "direct-manifest-install", 1, NULL, 'y' },
77 { "mount-install", 1, NULL, 'w' },
78 { "recovery", 1, NULL, 'b' },
79 { "preload", 0, NULL, OPTVAL_PRELOAD },
80 { "force-remove", 0, NULL, OPTVAL_FORCE_REMOVAL },
81 { "preload-rw", 0, NULL, OPTVAL_PRELOAD_RW },
82 { "no-remove", 0, NULL, OPTVAL_NO_REMOVAL },
83 { 0, 0, 0, 0 } /* sentinel */
86 struct pkgmgr_installer {
102 GDBusConnection *conn;
105 static uid_t g_target_uid;
107 static const char *__get_signal_name(pkgmgr_installer *pi, const char *key)
109 if (strcmp(key, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0)
111 else if (strcmp(key, PKGMGR_INSTALLER_GET_SIZE_KEY_STR) == 0)
113 else if (strcmp(key, PKGMGR_INSTALLER_APPID_KEY_STR) == 0)
114 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
116 switch (pi->request_type) {
117 case PKGMGR_REQ_INSTALL:
118 case PKGMGR_REQ_MANIFEST_DIRECT_INSTALL:
119 case PKGMGR_REQ_MOUNT_INSTALL:
120 case PKGMGR_REQ_REINSTALL:
121 case PKGMGR_REQ_ENABLE_PKG:
122 case PKGMGR_REQ_RECOVER:
123 return PKGMGR_INSTALLER_INSTALL_EVENT_STR;
124 case PKGMGR_REQ_UNINSTALL:
125 case PKGMGR_REQ_DISABLE_PKG:
126 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
127 case PKGMGR_REQ_UPGRADE:
128 return PKGMGR_INSTALLER_UPGRADE_EVENT_STR;
129 case PKGMGR_REQ_MOVE:
130 return PKGMGR_INSTALLER_MOVE_EVENT_STR;
131 case PKGMGR_REQ_ENABLE_APP:
132 return PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR;
133 case PKGMGR_REQ_DISABLE_APP:
134 return PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR;
135 case PKGMGR_REQ_ENABLE_APP_SPLASH_SCREEN:
136 return PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR;
137 case PKGMGR_REQ_DISABLE_APP_SPLASH_SCREEN:
138 return PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR;
139 case PKGMGR_REQ_CLEAR:
140 return PKGMGR_INSTALLER_CLEAR_EVENT_STR;
141 case PKGMGR_REQ_GETSIZE:
142 return PKGMGR_INSTALLER_GET_SIZE_KEY_STR;
145 ERR("cannot find type");
150 static int __send_signal_for_event(pkgmgr_installer *pi, const char *pkg_type,
151 const char *pkgid, const char *appid, const char *key,
158 if (!pi || pi->conn == NULL)
161 sid = pi->session_id;
165 name = __get_signal_name(pi, key);
167 ERR("unknown signal type");
171 if (g_dbus_connection_emit_signal(pi->conn, NULL,
172 PKGMGR_INSTALLER_DBUS_OBJECT_PATH,
173 PKGMGR_INSTALLER_DBUS_INTERFACE, name,
174 g_variant_new("(ussssss)", pi->target_uid, sid,
175 pkg_type, pkgid, appid ? appid : "",
178 ERR("failed to send dbus signal: %s", err->message);
186 static int __send_signal_to_agent(uid_t uid, void *data, size_t len)
189 struct sockaddr_un sa;
192 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
194 ERR("failed to create socket: %d", errno);
198 sa.sun_family = AF_UNIX;
199 snprintf(sa.sun_path, sizeof(sa.sun_path), "/run/pkgmgr/agent/%d", uid);
201 r = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
203 ERR("failed to connect socket(%s): %d", sa.sun_path, errno);
208 r = send(fd, data, len, 0);
210 ERR("failed to send data: %d", errno);
220 static int __send_signal_for_event_for_uid(pkgmgr_installer *pi, uid_t uid,
221 const char *pkg_type, const char *pkgid, const char *appid,
222 const char *key, const char *val)
234 if (!pi || pi->conn == NULL)
237 sid = pi->session_id;
241 data_len = sizeof(size_t) + sizeof(gsize);
243 name = __get_signal_name(pi, key);
245 ERR("unknown signal type");
248 /* including null byte */
249 name_size = strlen(name) + 1;
250 data_len += name_size;
252 gv = g_variant_new("(ussssss)", pi->target_uid, sid,
253 pkg_type, pkgid, appid ? appid : "", key, val);
255 ERR("failed to create GVariant instance");
258 gv_len = g_variant_get_size(gv);
259 gv_data = g_malloc(gv_len);
260 g_variant_store(gv, gv_data);
264 data = malloc(data_len);
266 memcpy(ptr, &name_size, sizeof(size_t));
267 ptr += sizeof(size_t);
268 memcpy(ptr, &gv_len, sizeof(gsize));
269 ptr += sizeof(gsize);
270 memcpy(ptr, name, name_size);
272 memcpy(ptr, gv_data, gv_len);
275 if (__send_signal_to_agent(uid, data, data_len)) {
276 ERR("failed to send signal to agent");
286 API pkgmgr_installer *pkgmgr_installer_new(void)
288 pkgmgr_installer *pi;
291 pi = calloc(1, sizeof(struct pkgmgr_installer));
295 pi->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
296 if (pi->conn == NULL) {
297 ERR("failed to get bus: %s", err->message);
305 pi->request_type = PKGMGR_REQ_INVALID;
310 API pkgmgr_installer *pkgmgr_installer_offline_new(void)
312 pkgmgr_installer *pi;
314 pi = calloc(1, sizeof(struct pkgmgr_installer));
320 pi->request_type = PKGMGR_REQ_INVALID;
325 API int pkgmgr_installer_free(pkgmgr_installer *pi)
331 free(pi->pkgmgr_info);
333 free(pi->session_id);
334 if (pi->optional_data)
335 free(pi->optional_data);
336 if (pi->caller_pkgid)
337 free(pi->caller_pkgid);
342 g_dbus_connection_flush_sync(pi->conn, NULL, NULL);
343 g_object_unref(pi->conn);
352 pkgmgr_installer_receive_request(pkgmgr_installer *pi,
353 const int argc, char **argv)
360 optind = 1; /* Initialize optind to clear prev. index */
365 pi->target_uid = getuid();
366 g_target_uid = pi->target_uid;
368 c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
369 /* printf("c=%d %c\n", c, c); //debug */
371 break; /* Parse is end */
373 case OPTVAL_PRELOAD: /* request for preload app */
375 DBG("preload request [%d]", pi->is_preload);
377 case OPTVAL_FORCE_REMOVAL: /* request for force-remove */
378 pi->force_removal = 1;
379 DBG("force-remove request [%d]", pi->force_removal);
381 case OPTVAL_PRELOAD_RW: /* request for preload-rw app */
382 pi->is_preload_rw = 1;
383 DBG("preload-rw request [%d]", pi->is_preload_rw);
385 case OPTVAL_NO_REMOVAL: /* request for no-remove */
387 DBG("no-remove request [%d]", pi->no_removal);
389 case 'k': /* session id */
391 free(pi->session_id);
392 pi->session_id = strndup(optarg, MAX_STRLEN);
395 case 'l': /* license path */
396 if (pi->license_path)
397 free(pi->license_path);
398 pi->license_path = strndup(optarg, MAX_STRLEN);
401 case 'i': /* install */
407 pi->request_type = PKGMGR_REQ_INSTALL;
409 free(pi->pkgmgr_info);
410 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
411 DBG("option is [i] pkgid[%s]", pi->pkgmgr_info);
412 if (pi->pkgmgr_info && strlen(pi->pkgmgr_info) == 0)
413 free(pi->pkgmgr_info);
418 case 'e': /* install */
421 pi->tep_path = strndup(optarg, MAX_STRLEN);
422 pi->is_tep_included = 1;
423 DBG("option is [e] tep_path[%s]", pi->tep_path);
426 case 'M': /* install */
427 if (strcmp(optarg, "tep_move") == 0)
431 DBG("option is [M] tep_move[%d]", pi->tep_move);
434 case 'd': /* uninstall */
440 pi->request_type = PKGMGR_REQ_UNINSTALL;
442 free(pi->pkgmgr_info);
443 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
447 case 'c': /* clear */
453 pi->request_type = PKGMGR_REQ_CLEAR;
455 free(pi->pkgmgr_info);
456 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
465 pi->request_type = PKGMGR_REQ_MOVE;
467 free(pi->pkgmgr_info);
468 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
471 case 'r': /* reinstall */
477 pi->request_type = PKGMGR_REQ_REINSTALL;
479 free(pi->pkgmgr_info);
480 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
483 case 't': /* move type*/
484 pi->move_type = atoi(optarg);
487 case 'p': /* caller pkgid*/
488 if (pi->caller_pkgid)
489 free(pi->caller_pkgid);
490 pi->caller_pkgid = strndup(optarg, MAX_STRLEN);
494 case 's': /* smack */
500 pi->request_type = PKGMGR_REQ_SMACK;
502 free(pi->pkgmgr_info);
503 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
506 case 'o': /* optional data*/
507 pi->optional_data = strndup(optarg, MAX_STRLEN);
510 case 'y': /* pkgid for direct manifest installation */
512 pi->request_type = PKGMGR_REQ_MANIFEST_DIRECT_INSTALL;
513 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
516 case 'w': /* pkgid for mount installation */
518 pi->request_type = PKGMGR_REQ_MOUNT_INSTALL;
519 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
522 case 'b': /* recovery */
528 pi->request_type = PKGMGR_REQ_RECOVER;
530 free(pi->pkgmgr_info);
531 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
534 case 'D': /* disable pkg */
535 pi->request_type = PKGMGR_REQ_DISABLE_PKG;
537 free(pi->pkgmgr_info);
538 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
541 case 'A': /* enable pkg */
542 pi->request_type = PKGMGR_REQ_ENABLE_PKG;
544 free(pi->pkgmgr_info);
545 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
549 g_target_uid = (uid_t)atoi(optarg);
550 pi->target_uid = (uid_t)atoi(optarg);
554 case '?': /* Not an option */
563 /* if target user is not set, set as tizenglobalapp user */
564 if (pi->target_uid == 0) {
565 pi->target_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
566 g_target_uid = pi->target_uid;
572 API int pkgmgr_installer_get_request_type(pkgmgr_installer *pi)
574 CHK_PI_RET(PKGMGR_REQ_INVALID);
575 return pi->request_type;
578 API uid_t pkgmgr_installer_get_uid(pkgmgr_installer *pi)
580 CHK_PI_RET(PKGMGR_REQ_INVALID);
581 return pi->target_uid;
584 API const char *pkgmgr_installer_get_request_info(pkgmgr_installer *pi)
586 CHK_PI_RET(PKGMGR_REQ_INVALID);
587 return pi->pkgmgr_info;
590 API const char *pkgmgr_installer_get_tep_path(pkgmgr_installer *pi)
592 CHK_PI_RET(PKGMGR_REQ_INVALID);
596 API int pkgmgr_installer_get_tep_move_type(pkgmgr_installer *pi)
598 CHK_PI_RET(PKGMGR_REQ_INVALID);
602 API const char *pkgmgr_installer_get_session_id(pkgmgr_installer *pi)
604 CHK_PI_RET(PKGMGR_REQ_INVALID);
605 return pi->session_id;
608 API const char *pkgmgr_installer_get_license_path(pkgmgr_installer *pi)
610 CHK_PI_RET(PKGMGR_REQ_INVALID);
611 return pi->license_path;
614 API const char *pkgmgr_installer_get_optional_data(pkgmgr_installer *pi)
616 CHK_PI_RET(PKGMGR_REQ_INVALID);
617 return pi->optional_data;
620 API int pkgmgr_installer_is_quiet(pkgmgr_installer *pi)
625 API int pkgmgr_installer_get_move_type(pkgmgr_installer *pi)
627 CHK_PI_RET(PKGMGR_REQ_INVALID);
628 return pi->move_type;
631 API const char *pkgmgr_installer_get_caller_pkgid(pkgmgr_installer *pi)
633 CHK_PI_RET(PKGMGR_REQ_INVALID);
634 return pi->caller_pkgid;
637 API int pkgmgr_installer_get_is_preload(pkgmgr_installer *pi)
639 CHK_PI_RET(PKGMGR_REQ_INVALID);
640 return pi->is_preload;
643 API int pkgmgr_installer_get_force_removal(pkgmgr_installer *pi)
645 CHK_PI_RET(PKGMGR_REQ_INVALID);
646 return pi->force_removal;
649 API int pkgmgr_installer_get_is_preload_rw(pkgmgr_installer *pi)
651 CHK_PI_RET(PKGMGR_REQ_INVALID);
652 return pi->is_preload_rw;
655 API int pkgmgr_installer_get_no_removal(pkgmgr_installer *pi)
657 CHK_PI_RET(PKGMGR_REQ_INVALID);
658 return pi->no_removal;
661 API int pkgmgr_installer_send_app_uninstall_signal(pkgmgr_installer *pi,
662 const char *pkg_type,
667 ret = __send_signal_for_event(pi, pkg_type, pkgid, NULL,
668 PKGMGR_INSTALLER_APPID_KEY_STR, val);
672 API int pkgmgr_installer_send_app_uninstall_signal_for_uid(
673 pkgmgr_installer *pi, uid_t uid, const char *pkg_type,
674 const char *pkgid, const char *val)
677 ret = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, NULL,
678 PKGMGR_INSTALLER_APPID_KEY_STR, val);
682 API int pkgmgr_installer_set_uid(pkgmgr_installer *pi, uid_t uid)
687 pi->target_uid = uid;
688 g_target_uid = pi->target_uid;
694 pkgmgr_installer_send_app_signal(pkgmgr_installer *pi,
695 const char *pkg_type,
698 const char *key, const char *val)
703 ERR("connection is NULL");
707 r = __send_signal_for_event(pi, pkg_type, pkgid, appid, key, val);
713 pkgmgr_installer_send_signal(pkgmgr_installer *pi,
714 const char *pkg_type,
716 const char *key, const char *val)
721 ERR("connection is NULL");
725 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
726 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
727 pi->request_type = PKGMGR_REQ_UPGRADE;
729 r = __send_signal_for_event(pi, pkg_type, pkgid, NULL, key, val);
734 API int pkgmgr_installer_send_app_signal_for_uid(pkgmgr_installer *pi,
735 uid_t uid, const char *pkg_type, const char *pkgid,
736 const char *appid, const char *key, const char *val)
741 ERR("connection is NULL");
745 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, appid,
751 API int pkgmgr_installer_send_signal_for_uid(pkgmgr_installer *pi,
752 uid_t uid, const char *pkg_type, const char *pkgid,
753 const char *key, const char *val)
758 ERR("connection is NULL");
762 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
763 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
764 pi->request_type = PKGMGR_REQ_UPGRADE;
766 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, NULL,
772 API int pkgmgr_installer_set_request_type(pkgmgr_installer *pi, int request_type)
777 pi->request_type = request_type;
781 API int pkgmgr_installer_set_session_id(pkgmgr_installer *pi, const char *session_id)
783 if (pi == NULL || session_id == NULL)
786 pi->session_id = strndup(session_id, MAX_STRLEN);
790 API int pkgmgr_installer_create_certinfo_set_handle(pkgmgr_instcertinfo_h *handle)
793 ret = pkgmgrinfo_create_certinfo_set_handle(handle);
797 API int pkgmgr_installer_set_cert_value(pkgmgr_instcertinfo_h handle, pkgmgr_instcert_type cert_type, char *cert_value)
800 ret = pkgmgrinfo_set_cert_value(handle, cert_type, cert_value);
804 API int pkgmgr_installer_save_certinfo(const char *pkgid, pkgmgr_instcertinfo_h handle, uid_t uid)
807 ret = pkgmgrinfo_save_certinfo(pkgid, handle, uid);
811 API int pkgmgr_installer_destroy_certinfo_set_handle(pkgmgr_instcertinfo_h handle)
814 ret = pkgmgrinfo_destroy_certinfo_set_handle(handle);
818 API int pkgmgr_installer_delete_certinfo(const char *pkgid)
821 ret = pkgmgrinfo_delete_certinfo(pkgid);
825 API int pkgmgr_installer_info_get_target_uid(uid_t *uid)