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
58 /* Supported options */
59 const char *short_opts = "k:l:i:d:c:m:t:o:r:p:s:b:e:M:y:u:w:D:A:q";
60 const struct option long_opts[] = {
61 { "session-id", 1, NULL, 'k' },
62 { "license-path", 1, NULL, 'l' },
63 { "install", 1, NULL, 'i' },
64 { "uninstall", 1, NULL, 'd' },
65 { "clear", 1, NULL, 'c' },
66 { "move", 1, NULL, 'm' },
67 { "move-type", 1, NULL, 't' },
68 { "optional-data", 0, NULL, 'o' },
69 { "reinstall", 0, NULL, 'r' },
70 { "caller-pkgid", 1, NULL, 'p' },
71 { "tep-path", 1, NULL, 'e' },
72 { "tep-move", 1, NULL, 'M' },
73 { "smack", 1, NULL, 's' },
74 { "direct-manifest-install", 1, NULL, 'y' },
75 { "mount-install", 1, NULL, 'w' },
76 { "recovery", 1, NULL, 'b' },
77 { "preload", 0, NULL, OPTVAL_PRELOAD },
78 { "force-remove", 0, NULL, OPTVAL_FORCE_REMOVAL },
79 { 0, 0, 0, 0 } /* sentinel */
82 struct pkgmgr_installer {
96 GDBusConnection *conn;
99 static uid_t g_target_uid;
101 static const char *__get_signal_name(pkgmgr_installer *pi, const char *key)
103 if (strcmp(key, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0)
105 else if (strcmp(key, PKGMGR_INSTALLER_GET_SIZE_KEY_STR) == 0)
107 else if (strcmp(key, PKGMGR_INSTALLER_APPID_KEY_STR) == 0)
108 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
110 switch (pi->request_type) {
111 case PKGMGR_REQ_INSTALL:
112 case PKGMGR_REQ_MANIFEST_DIRECT_INSTALL:
113 case PKGMGR_REQ_MOUNT_INSTALL:
114 case PKGMGR_REQ_ENABLE_PKG:
115 return PKGMGR_INSTALLER_INSTALL_EVENT_STR;
116 case PKGMGR_REQ_UNINSTALL:
117 case PKGMGR_REQ_DISABLE_PKG:
118 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
119 case PKGMGR_REQ_UPGRADE:
120 return PKGMGR_INSTALLER_UPGRADE_EVENT_STR;
121 case PKGMGR_REQ_MOVE:
122 return PKGMGR_INSTALLER_MOVE_EVENT_STR;
123 case PKGMGR_REQ_ENABLE_APP:
124 return PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR;
125 case PKGMGR_REQ_DISABLE_APP:
126 return PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR;
127 case PKGMGR_REQ_ENABLE_APP_SPLASH_SCREEN:
128 return PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR;
129 case PKGMGR_REQ_DISABLE_APP_SPLASH_SCREEN:
130 return PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR;
133 ERR("cannot find type");
138 static int __send_signal_for_event(pkgmgr_installer *pi, const char *pkg_type,
139 const char *pkgid, const char *appid, const char *key,
146 if (!pi || pi->conn == NULL)
149 sid = pi->session_id;
153 name = __get_signal_name(pi, key);
155 ERR("unknown signal type");
159 if (g_dbus_connection_emit_signal(pi->conn, NULL,
160 PKGMGR_INSTALLER_DBUS_OBJECT_PATH,
161 PKGMGR_INSTALLER_DBUS_INTERFACE, name,
162 g_variant_new("(ussssss)", pi->target_uid, sid,
163 pkg_type, pkgid, appid ? appid : "",
166 ERR("failed to send dbus signal: %s", err->message);
174 static int __send_signal_to_agent(uid_t uid, void *data, size_t len)
177 struct sockaddr_un sa;
180 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
182 ERR("failed to create socket: %d", errno);
186 sa.sun_family = AF_UNIX;
187 snprintf(sa.sun_path, sizeof(sa.sun_path), "/run/pkgmgr/agent/%d", uid);
189 r = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
191 ERR("failed to connect socket(%s): %d", sa.sun_path, errno);
196 r = send(fd, data, len, 0);
198 ERR("failed to send data: %d", errno);
208 static int __send_signal_for_event_for_uid(pkgmgr_installer *pi, uid_t uid,
209 const char *pkg_type, const char *pkgid, const char *appid,
210 const char *key, const char *val)
222 if (!pi || pi->conn == NULL)
225 sid = pi->session_id;
229 data_len = sizeof(size_t) + sizeof(gsize);
231 name = __get_signal_name(pi, key);
233 ERR("unknown signal type");
236 /* including null byte */
237 name_size = strlen(name) + 1;
238 data_len += name_size;
240 gv = g_variant_new("(ussssss)", pi->target_uid, sid,
241 pkg_type, pkgid, appid ? appid : "", key, val);
243 ERR("failed to create GVariant instance");
246 gv_len = g_variant_get_size(gv);
247 gv_data = g_malloc(gv_len);
248 g_variant_store(gv, gv_data);
252 data = malloc(data_len);
254 memcpy(ptr, &name_size, sizeof(size_t));
255 ptr += sizeof(size_t);
256 memcpy(ptr, &gv_len, sizeof(gsize));
257 ptr += sizeof(gsize);
258 memcpy(ptr, name, name_size);
260 memcpy(ptr, gv_data, gv_len);
262 if (__send_signal_to_agent(uid, data, data_len)) {
263 ERR("failed to send signal to agent");
274 API pkgmgr_installer *pkgmgr_installer_new(void)
276 pkgmgr_installer *pi;
279 pi = calloc(1, sizeof(struct pkgmgr_installer));
283 pi->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
284 if (pi->conn == NULL) {
285 ERR("failed to get bus: %s", err->message);
293 pi->request_type = PKGMGR_REQ_INVALID;
298 API pkgmgr_installer *pkgmgr_installer_offline_new(void)
300 pkgmgr_installer *pi;
302 pi = calloc(1, sizeof(struct pkgmgr_installer));
308 pi->request_type = PKGMGR_REQ_INVALID;
313 API int pkgmgr_installer_free(pkgmgr_installer *pi)
319 free(pi->pkgmgr_info);
321 free(pi->session_id);
322 if (pi->optional_data)
323 free(pi->optional_data);
324 if (pi->caller_pkgid)
325 free(pi->caller_pkgid);
330 g_dbus_connection_flush_sync(pi->conn, NULL, NULL);
331 g_object_unref(pi->conn);
340 pkgmgr_installer_receive_request(pkgmgr_installer *pi,
341 const int argc, char **argv)
348 optind = 1; /* Initialize optind to clear prev. index */
353 pi->target_uid = getuid();
354 g_target_uid = pi->target_uid;
356 c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
357 /* printf("c=%d %c\n", c, c); //debug */
359 break; /* Parse is end */
361 case OPTVAL_PRELOAD: /* request for preload app */
363 DBG("preload request [%d]", pi->is_preload);
365 case OPTVAL_FORCE_REMOVAL: /* request for force-remove */
366 pi->force_removal = 1;
367 DBG("force-remove request [%d]", pi->force_removal);
369 case 'k': /* session id */
371 free(pi->session_id);
372 pi->session_id = strndup(optarg, MAX_STRLEN);
375 case 'l': /* license path */
376 if (pi->license_path)
377 free(pi->license_path);
378 pi->license_path = strndup(optarg, MAX_STRLEN);
381 case 'i': /* install */
387 pi->request_type = PKGMGR_REQ_INSTALL;
389 free(pi->pkgmgr_info);
390 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
391 DBG("option is [i] pkgid[%s]", pi->pkgmgr_info);
392 if (pi->pkgmgr_info && strlen(pi->pkgmgr_info) == 0)
393 free(pi->pkgmgr_info);
398 case 'e': /* install */
401 pi->tep_path = strndup(optarg, MAX_STRLEN);
402 pi->is_tep_included = 1;
403 DBG("option is [e] tep_path[%s]", pi->tep_path);
406 case 'M': /* install */
407 if (strcmp(optarg, "tep_move") == 0)
411 DBG("option is [M] tep_move[%d]", pi->tep_move);
414 case 'd': /* uninstall */
420 pi->request_type = PKGMGR_REQ_UNINSTALL;
422 free(pi->pkgmgr_info);
423 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
427 case 'c': /* clear */
433 pi->request_type = PKGMGR_REQ_CLEAR;
435 free(pi->pkgmgr_info);
436 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
445 pi->request_type = PKGMGR_REQ_MOVE;
447 free(pi->pkgmgr_info);
448 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
451 case 'r': /* reinstall */
457 pi->request_type = PKGMGR_REQ_REINSTALL;
459 free(pi->pkgmgr_info);
460 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
463 case 't': /* move type*/
464 pi->move_type = atoi(optarg);
467 case 'p': /* caller pkgid*/
468 if (pi->caller_pkgid)
469 free(pi->caller_pkgid);
470 pi->caller_pkgid = strndup(optarg, MAX_STRLEN);
474 case 's': /* smack */
480 pi->request_type = PKGMGR_REQ_SMACK;
482 free(pi->pkgmgr_info);
483 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
486 case 'o': /* optional data*/
487 pi->optional_data = strndup(optarg, MAX_STRLEN);
490 case 'y': /* pkgid for direct manifest installation */
492 pi->request_type = PKGMGR_REQ_MANIFEST_DIRECT_INSTALL;
493 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
496 case 'w': /* pkgid for mount installation */
498 pi->request_type = PKGMGR_REQ_MOUNT_INSTALL;
499 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
502 case 'b': /* recovery */
508 pi->request_type = PKGMGR_REQ_RECOVER;
510 free(pi->pkgmgr_info);
511 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
514 case 'D': /* disable pkg */
515 pi->request_type = PKGMGR_REQ_DISABLE_PKG;
517 free(pi->pkgmgr_info);
518 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
521 case 'A': /* enable pkg */
522 pi->request_type = PKGMGR_REQ_ENABLE_PKG;
524 free(pi->pkgmgr_info);
525 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
529 g_target_uid = (uid_t)atoi(optarg);
530 pi->target_uid = (uid_t)atoi(optarg);
534 case '?': /* Not an option */
543 /* if target user is not set, set as tizenglobalapp user */
544 if (pi->target_uid == 0) {
545 pi->target_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
546 g_target_uid = pi->target_uid;
552 API int pkgmgr_installer_get_request_type(pkgmgr_installer *pi)
554 CHK_PI_RET(PKGMGR_REQ_INVALID);
555 return pi->request_type;
558 API uid_t pkgmgr_installer_get_uid(pkgmgr_installer *pi)
560 CHK_PI_RET(PKGMGR_REQ_INVALID);
561 return pi->target_uid;
564 API const char *pkgmgr_installer_get_request_info(pkgmgr_installer *pi)
566 CHK_PI_RET(PKGMGR_REQ_INVALID);
567 return pi->pkgmgr_info;
570 API const char *pkgmgr_installer_get_tep_path(pkgmgr_installer *pi)
572 CHK_PI_RET(PKGMGR_REQ_INVALID);
576 API int pkgmgr_installer_get_tep_move_type(pkgmgr_installer *pi)
578 CHK_PI_RET(PKGMGR_REQ_INVALID);
582 API const char *pkgmgr_installer_get_session_id(pkgmgr_installer *pi)
584 CHK_PI_RET(PKGMGR_REQ_INVALID);
585 return pi->session_id;
588 API const char *pkgmgr_installer_get_license_path(pkgmgr_installer *pi)
590 CHK_PI_RET(PKGMGR_REQ_INVALID);
591 return pi->license_path;
594 API const char *pkgmgr_installer_get_optional_data(pkgmgr_installer *pi)
596 CHK_PI_RET(PKGMGR_REQ_INVALID);
597 return pi->optional_data;
600 API int pkgmgr_installer_is_quiet(pkgmgr_installer *pi)
605 API int pkgmgr_installer_get_move_type(pkgmgr_installer *pi)
607 CHK_PI_RET(PKGMGR_REQ_INVALID);
608 return pi->move_type;
611 API const char *pkgmgr_installer_get_caller_pkgid(pkgmgr_installer *pi)
613 CHK_PI_RET(PKGMGR_REQ_INVALID);
614 return pi->caller_pkgid;
617 API int pkgmgr_installer_get_is_preload(pkgmgr_installer *pi)
619 CHK_PI_RET(PKGMGR_REQ_INVALID);
620 return pi->is_preload;
623 API int pkgmgr_installer_get_force_removal(pkgmgr_installer *pi)
625 CHK_PI_RET(PKGMGR_REQ_INVALID);
626 return pi->force_removal;
629 API int pkgmgr_installer_send_app_uninstall_signal(pkgmgr_installer *pi,
630 const char *pkg_type,
635 ret = __send_signal_for_event(pi, pkg_type, pkgid, NULL,
636 PKGMGR_INSTALLER_APPID_KEY_STR, val);
640 API int pkgmgr_installer_set_uid(pkgmgr_installer *pi, uid_t uid)
645 pi->target_uid = uid;
646 g_target_uid = pi->target_uid;
652 pkgmgr_installer_send_app_signal(pkgmgr_installer *pi,
653 const char *pkg_type,
656 const char *key, const char *val)
661 ERR("connection is NULL");
665 r = __send_signal_for_event(pi, pkg_type, pkgid, appid, key, val);
671 pkgmgr_installer_send_signal(pkgmgr_installer *pi,
672 const char *pkg_type,
674 const char *key, const char *val)
679 ERR("connection is NULL");
683 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
684 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
685 pi->request_type = PKGMGR_REQ_UPGRADE;
687 r = __send_signal_for_event(pi, pkg_type, pkgid, NULL, key, val);
692 API int pkgmgr_installer_send_app_signal_for_uid(pkgmgr_installer *pi,
693 uid_t uid, const char *pkg_type, const char *pkgid,
694 const char *appid, const char *key, const char *val)
699 ERR("connection is NULL");
703 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, appid,
709 API int pkgmgr_installer_send_signal_for_uid(pkgmgr_installer *pi,
710 uid_t uid, const char *pkg_type, const char *pkgid,
711 const char *key, const char *val)
716 ERR("connection is NULL");
720 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
721 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
722 pi->request_type = PKGMGR_REQ_UPGRADE;
724 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, NULL,
730 API int pkgmgr_installer_set_request_type(pkgmgr_installer *pi, int request_type)
735 pi->request_type = request_type;
739 API int pkgmgr_installer_set_session_id(pkgmgr_installer *pi, const char *session_id)
741 if (pi == NULL || session_id == NULL)
744 pi->session_id = strndup(session_id, MAX_STRLEN);
748 API int pkgmgr_installer_create_certinfo_set_handle(pkgmgr_instcertinfo_h *handle)
751 ret = pkgmgrinfo_create_certinfo_set_handle(handle);
755 API int pkgmgr_installer_set_cert_value(pkgmgr_instcertinfo_h handle, pkgmgr_instcert_type cert_type, char *cert_value)
758 ret = pkgmgrinfo_set_cert_value(handle, cert_type, cert_value);
762 API int pkgmgr_installer_save_certinfo(const char *pkgid, pkgmgr_instcertinfo_h handle, uid_t uid)
765 ret = pkgmgrinfo_save_certinfo(pkgid, handle, uid);
769 API int pkgmgr_installer_destroy_certinfo_set_handle(pkgmgr_instcertinfo_h handle)
772 ret = pkgmgrinfo_destroy_certinfo_set_handle(handle);
776 API int pkgmgr_installer_delete_certinfo(const char *pkgid)
779 ret = pkgmgrinfo_delete_certinfo(pkgid);
783 API int pkgmgr_installer_info_get_target_uid(uid_t *uid)