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
59 #define OPTVAL_KEEP_RWDATA 1004
60 #define OPTVAL_PARTIAL_RW 1005
62 /* Supported options */
63 const char *short_opts = "k:l:i:d:c:m:t:o:r:p:s:b:e:M:y:u:w:D:A:q";
64 const struct option long_opts[] = {
65 { "session-id", 1, NULL, 'k' },
66 { "license-path", 1, NULL, 'l' },
67 { "install", 1, NULL, 'i' },
68 { "uninstall", 1, NULL, 'd' },
69 { "clear", 1, NULL, 'c' },
70 { "move", 1, NULL, 'm' },
71 { "move-type", 1, NULL, 't' },
72 { "optional-data", 0, NULL, 'o' },
73 { "reinstall", 0, NULL, 'r' },
74 { "caller-pkgid", 1, NULL, 'p' },
75 { "tep-path", 1, NULL, 'e' },
76 { "tep-move", 1, NULL, 'M' },
77 { "smack", 1, NULL, 's' },
78 { "direct-manifest-install", 1, NULL, 'y' },
79 { "mount-install", 1, NULL, 'w' },
80 { "recovery", 1, NULL, 'b' },
81 { "preload", 0, NULL, OPTVAL_PRELOAD }, /* for preload RO */
82 { "force-remove", 0, NULL, OPTVAL_FORCE_REMOVAL }, /* for preload RO/RW */
83 { "preload-rw", 0, NULL, OPTVAL_PRELOAD_RW }, /* for preload RW */
84 { "no-remove", 0, NULL, OPTVAL_NO_REMOVAL }, /* for preload RW */
85 { "keep-rwdata", 0, NULL, OPTVAL_KEEP_RWDATA }, /* for preload RW */
86 { "partial-rw", 0, NULL, OPTVAL_PARTIAL_RW }, /* for preload RO */
87 { 0, 0, 0, 0 } /* sentinel */
90 struct pkgmgr_installer {
108 GDBusConnection *conn;
111 static uid_t g_target_uid;
113 static const char *__get_signal_name(pkgmgr_installer *pi, const char *key)
115 if (strcmp(key, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR) == 0)
117 else if (strcmp(key, PKGMGR_INSTALLER_GET_SIZE_KEY_STR) == 0)
119 else if (strcmp(key, PKGMGR_INSTALLER_APPID_KEY_STR) == 0)
120 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
122 switch (pi->request_type) {
123 case PKGMGR_REQ_INSTALL:
124 case PKGMGR_REQ_MANIFEST_DIRECT_INSTALL:
125 case PKGMGR_REQ_MOUNT_INSTALL:
126 case PKGMGR_REQ_REINSTALL:
127 case PKGMGR_REQ_ENABLE_PKG:
128 case PKGMGR_REQ_RECOVER:
129 return PKGMGR_INSTALLER_INSTALL_EVENT_STR;
130 case PKGMGR_REQ_UNINSTALL:
131 case PKGMGR_REQ_DISABLE_PKG:
132 return PKGMGR_INSTALLER_UNINSTALL_EVENT_STR;
133 case PKGMGR_REQ_UPGRADE:
134 return PKGMGR_INSTALLER_UPGRADE_EVENT_STR;
135 case PKGMGR_REQ_MOVE:
136 return PKGMGR_INSTALLER_MOVE_EVENT_STR;
137 case PKGMGR_REQ_ENABLE_APP:
138 return PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR;
139 case PKGMGR_REQ_DISABLE_APP:
140 return PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR;
141 case PKGMGR_REQ_ENABLE_APP_SPLASH_SCREEN:
142 return PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR;
143 case PKGMGR_REQ_DISABLE_APP_SPLASH_SCREEN:
144 return PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR;
145 case PKGMGR_REQ_CLEAR:
146 return PKGMGR_INSTALLER_CLEAR_EVENT_STR;
147 case PKGMGR_REQ_GETSIZE:
148 return PKGMGR_INSTALLER_GET_SIZE_KEY_STR;
151 ERR("cannot find type");
156 static int __send_signal_for_event(pkgmgr_installer *pi, const char *pkg_type,
157 const char *pkgid, const char *appid, const char *key,
164 if (!pi || pi->conn == NULL)
167 sid = pi->session_id;
171 name = __get_signal_name(pi, key);
173 ERR("unknown signal type");
177 if (g_dbus_connection_emit_signal(pi->conn, NULL,
178 PKGMGR_INSTALLER_DBUS_OBJECT_PATH,
179 PKGMGR_INSTALLER_DBUS_INTERFACE, name,
180 g_variant_new("(ussssss)", pi->target_uid, sid,
181 pkg_type, pkgid, appid ? appid : "",
184 ERR("failed to send dbus signal: %s", err->message);
192 static int __send_signal_to_agent(uid_t uid, void *data, size_t len)
195 struct sockaddr_un sa;
198 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
200 ERR("failed to create socket: %d", errno);
204 sa.sun_family = AF_UNIX;
205 snprintf(sa.sun_path, sizeof(sa.sun_path), "/run/pkgmgr/agent/%d", uid);
207 r = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
209 ERR("failed to connect socket(%s): %d", sa.sun_path, errno);
214 r = send(fd, data, len, 0);
216 ERR("failed to send data: %d", errno);
226 static int __send_signal_for_event_for_uid(pkgmgr_installer *pi, uid_t uid,
227 const char *pkg_type, const char *pkgid, const char *appid,
228 const char *key, const char *val)
240 if (!pi || pi->conn == NULL)
243 sid = pi->session_id;
247 data_len = sizeof(size_t) + sizeof(gsize);
249 name = __get_signal_name(pi, key);
251 ERR("unknown signal type");
254 /* including null byte */
255 name_size = strlen(name) + 1;
256 data_len += name_size;
258 gv = g_variant_new("(ussssss)", pi->target_uid, sid,
259 pkg_type, pkgid, appid ? appid : "", key, val);
261 ERR("failed to create GVariant instance");
264 gv_len = g_variant_get_size(gv);
265 gv_data = g_malloc(gv_len);
266 g_variant_store(gv, gv_data);
270 data = malloc(data_len);
272 memcpy(ptr, &name_size, sizeof(size_t));
273 ptr += sizeof(size_t);
274 memcpy(ptr, &gv_len, sizeof(gsize));
275 ptr += sizeof(gsize);
276 memcpy(ptr, name, name_size);
278 memcpy(ptr, gv_data, gv_len);
281 if (__send_signal_to_agent(uid, data, data_len)) {
282 ERR("failed to send signal to agent");
292 API pkgmgr_installer *pkgmgr_installer_new(void)
294 pkgmgr_installer *pi;
297 pi = calloc(1, sizeof(struct pkgmgr_installer));
301 pi->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
302 if (pi->conn == NULL) {
303 ERR("failed to get bus: %s", err->message);
311 pi->request_type = PKGMGR_REQ_INVALID;
316 API pkgmgr_installer *pkgmgr_installer_offline_new(void)
318 pkgmgr_installer *pi;
320 pi = calloc(1, sizeof(struct pkgmgr_installer));
326 pi->request_type = PKGMGR_REQ_INVALID;
331 API int pkgmgr_installer_free(pkgmgr_installer *pi)
337 free(pi->pkgmgr_info);
339 free(pi->session_id);
340 if (pi->optional_data)
341 free(pi->optional_data);
342 if (pi->caller_pkgid)
343 free(pi->caller_pkgid);
348 g_dbus_connection_flush_sync(pi->conn, NULL, NULL);
349 g_object_unref(pi->conn);
358 pkgmgr_installer_receive_request(pkgmgr_installer *pi,
359 const int argc, char **argv)
366 optind = 1; /* Initialize optind to clear prev. index */
371 pi->target_uid = getuid();
372 g_target_uid = pi->target_uid;
374 c = getopt_long(argc, argv, short_opts, long_opts, &opt_idx);
375 /* printf("c=%d %c\n", c, c); //debug */
377 break; /* Parse is end */
379 case OPTVAL_PRELOAD: /* request for preload app */
381 DBG("preload request [%d]", pi->is_preload);
383 case OPTVAL_FORCE_REMOVAL: /* request for force-remove */
384 pi->force_removal = 1;
385 DBG("force-remove request [%d]", pi->force_removal);
387 case OPTVAL_PRELOAD_RW: /* request for preload-rw app */
388 pi->is_preload_rw = 1;
389 DBG("preload-rw request [%d]", pi->is_preload_rw);
391 case OPTVAL_NO_REMOVAL: /* request for no-remove */
393 DBG("no-remove request [%d]", pi->no_removal);
395 case OPTVAL_KEEP_RWDATA: /* request for keep-rwdata */
397 DBG("keep-rwdata request [%d]", pi->keep_rwdata);
399 case OPTVAL_PARTIAL_RW: /* request for partial-rw */
401 DBG("partial-rw request [%d]", pi->partial_rw);
403 case 'k': /* session id */
405 free(pi->session_id);
406 pi->session_id = strndup(optarg, MAX_STRLEN);
409 case 'l': /* license path */
410 if (pi->license_path)
411 free(pi->license_path);
412 pi->license_path = strndup(optarg, MAX_STRLEN);
415 case 'i': /* install */
421 pi->request_type = PKGMGR_REQ_INSTALL;
423 free(pi->pkgmgr_info);
424 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
425 DBG("option is [i] pkgid[%s]", pi->pkgmgr_info);
426 if (pi->pkgmgr_info && strlen(pi->pkgmgr_info) == 0)
427 free(pi->pkgmgr_info);
432 case 'e': /* install */
435 pi->tep_path = strndup(optarg, MAX_STRLEN);
436 pi->is_tep_included = 1;
437 DBG("option is [e] tep_path[%s]", pi->tep_path);
440 case 'M': /* install */
441 if (strcmp(optarg, "tep_move") == 0)
445 DBG("option is [M] tep_move[%d]", pi->tep_move);
448 case 'd': /* uninstall */
454 pi->request_type = PKGMGR_REQ_UNINSTALL;
456 free(pi->pkgmgr_info);
457 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
461 case 'c': /* clear */
467 pi->request_type = PKGMGR_REQ_CLEAR;
469 free(pi->pkgmgr_info);
470 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
479 pi->request_type = PKGMGR_REQ_MOVE;
481 free(pi->pkgmgr_info);
482 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
485 case 'r': /* reinstall */
491 pi->request_type = PKGMGR_REQ_REINSTALL;
493 free(pi->pkgmgr_info);
494 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
497 case 't': /* move type*/
498 pi->move_type = atoi(optarg);
501 case 'p': /* caller pkgid*/
502 if (pi->caller_pkgid)
503 free(pi->caller_pkgid);
504 pi->caller_pkgid = strndup(optarg, MAX_STRLEN);
508 case 's': /* smack */
514 pi->request_type = PKGMGR_REQ_SMACK;
516 free(pi->pkgmgr_info);
517 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
520 case 'o': /* optional data*/
521 pi->optional_data = strndup(optarg, MAX_STRLEN);
524 case 'y': /* pkgid for direct manifest installation */
526 pi->request_type = PKGMGR_REQ_MANIFEST_DIRECT_INSTALL;
527 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
530 case 'w': /* pkgid for mount installation */
532 pi->request_type = PKGMGR_REQ_MOUNT_INSTALL;
533 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
536 case 'b': /* recovery */
542 pi->request_type = PKGMGR_REQ_RECOVER;
544 free(pi->pkgmgr_info);
545 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
548 case 'D': /* disable pkg */
549 pi->request_type = PKGMGR_REQ_DISABLE_PKG;
551 free(pi->pkgmgr_info);
552 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
555 case 'A': /* enable pkg */
556 pi->request_type = PKGMGR_REQ_ENABLE_PKG;
558 free(pi->pkgmgr_info);
559 pi->pkgmgr_info = strndup(optarg, MAX_STRLEN);
563 g_target_uid = (uid_t)atoi(optarg);
564 pi->target_uid = (uid_t)atoi(optarg);
568 case '?': /* Not an option */
577 /* if target user is not set, set as tizenglobalapp user */
578 if (pi->target_uid == 0) {
579 pi->target_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
580 g_target_uid = pi->target_uid;
586 API int pkgmgr_installer_get_request_type(pkgmgr_installer *pi)
588 CHK_PI_RET(PKGMGR_REQ_INVALID);
589 return pi->request_type;
592 API uid_t pkgmgr_installer_get_uid(pkgmgr_installer *pi)
594 CHK_PI_RET(PKGMGR_REQ_INVALID);
595 return pi->target_uid;
598 API const char *pkgmgr_installer_get_request_info(pkgmgr_installer *pi)
600 CHK_PI_RET(PKGMGR_REQ_INVALID);
601 return pi->pkgmgr_info;
604 API const char *pkgmgr_installer_get_tep_path(pkgmgr_installer *pi)
606 CHK_PI_RET(PKGMGR_REQ_INVALID);
610 API int pkgmgr_installer_get_tep_move_type(pkgmgr_installer *pi)
612 CHK_PI_RET(PKGMGR_REQ_INVALID);
616 API const char *pkgmgr_installer_get_session_id(pkgmgr_installer *pi)
618 CHK_PI_RET(PKGMGR_REQ_INVALID);
619 return pi->session_id;
622 API const char *pkgmgr_installer_get_license_path(pkgmgr_installer *pi)
624 CHK_PI_RET(PKGMGR_REQ_INVALID);
625 return pi->license_path;
628 API const char *pkgmgr_installer_get_optional_data(pkgmgr_installer *pi)
630 CHK_PI_RET(PKGMGR_REQ_INVALID);
631 return pi->optional_data;
634 API int pkgmgr_installer_is_quiet(pkgmgr_installer *pi)
639 API int pkgmgr_installer_get_move_type(pkgmgr_installer *pi)
641 CHK_PI_RET(PKGMGR_REQ_INVALID);
642 return pi->move_type;
645 API const char *pkgmgr_installer_get_caller_pkgid(pkgmgr_installer *pi)
647 CHK_PI_RET(PKGMGR_REQ_INVALID);
648 return pi->caller_pkgid;
651 API int pkgmgr_installer_get_is_preload(pkgmgr_installer *pi)
653 CHK_PI_RET(PKGMGR_REQ_INVALID);
654 return pi->is_preload;
657 API int pkgmgr_installer_get_force_removal(pkgmgr_installer *pi)
659 CHK_PI_RET(PKGMGR_REQ_INVALID);
660 return pi->force_removal;
663 API int pkgmgr_installer_get_is_preload_rw(pkgmgr_installer *pi)
665 CHK_PI_RET(PKGMGR_REQ_INVALID);
666 return pi->is_preload_rw;
669 API int pkgmgr_installer_get_no_removal(pkgmgr_installer *pi)
671 CHK_PI_RET(PKGMGR_REQ_INVALID);
672 return pi->no_removal;
675 API int pkgmgr_installer_get_keep_rwdata(pkgmgr_installer *pi)
677 CHK_PI_RET(PKGMGR_REQ_INVALID);
678 return pi->keep_rwdata;
681 API int pkgmgr_installer_get_partial_rw(pkgmgr_installer *pi)
683 CHK_PI_RET(PKGMGR_REQ_INVALID);
684 return pi->partial_rw;
687 API int pkgmgr_installer_send_app_uninstall_signal(pkgmgr_installer *pi,
688 const char *pkg_type,
693 ret = __send_signal_for_event(pi, pkg_type, pkgid, NULL,
694 PKGMGR_INSTALLER_APPID_KEY_STR, val);
698 API int pkgmgr_installer_send_app_uninstall_signal_for_uid(
699 pkgmgr_installer *pi, uid_t uid, const char *pkg_type,
700 const char *pkgid, const char *val)
703 ret = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, NULL,
704 PKGMGR_INSTALLER_APPID_KEY_STR, val);
708 API int pkgmgr_installer_set_uid(pkgmgr_installer *pi, uid_t uid)
713 pi->target_uid = uid;
714 g_target_uid = pi->target_uid;
720 pkgmgr_installer_send_app_signal(pkgmgr_installer *pi,
721 const char *pkg_type,
724 const char *key, const char *val)
729 ERR("connection is NULL");
733 r = __send_signal_for_event(pi, pkg_type, pkgid, appid, key, val);
739 pkgmgr_installer_send_signal(pkgmgr_installer *pi,
740 const char *pkg_type,
742 const char *key, const char *val)
747 ERR("connection is NULL");
751 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
752 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
753 pi->request_type = PKGMGR_REQ_UPGRADE;
755 r = __send_signal_for_event(pi, pkg_type, pkgid, NULL, key, val);
760 API int pkgmgr_installer_send_app_signal_for_uid(pkgmgr_installer *pi,
761 uid_t uid, const char *pkg_type, const char *pkgid,
762 const char *appid, const char *key, const char *val)
767 ERR("connection is NULL");
771 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, appid,
777 API int pkgmgr_installer_send_signal_for_uid(pkgmgr_installer *pi,
778 uid_t uid, const char *pkg_type, const char *pkgid,
779 const char *key, const char *val)
784 ERR("connection is NULL");
788 if (strcmp(key, PKGMGR_INSTALLER_START_KEY_STR) == 0 &&
789 strcmp(val, PKGMGR_INSTALLER_UPGRADE_EVENT_STR) == 0)
790 pi->request_type = PKGMGR_REQ_UPGRADE;
792 r = __send_signal_for_event_for_uid(pi, uid, pkg_type, pkgid, NULL,
798 API int pkgmgr_installer_set_request_type(pkgmgr_installer *pi, int request_type)
803 pi->request_type = request_type;
807 API int pkgmgr_installer_set_session_id(pkgmgr_installer *pi, const char *session_id)
809 if (pi == NULL || session_id == NULL)
812 pi->session_id = strndup(session_id, MAX_STRLEN);
816 API int pkgmgr_installer_create_certinfo_set_handle(pkgmgr_instcertinfo_h *handle)
819 ret = pkgmgrinfo_create_certinfo_set_handle(handle);
823 API int pkgmgr_installer_set_cert_value(pkgmgr_instcertinfo_h handle, pkgmgr_instcert_type cert_type, char *cert_value)
826 ret = pkgmgrinfo_set_cert_value(handle, cert_type, cert_value);
830 API int pkgmgr_installer_save_certinfo(const char *pkgid, pkgmgr_instcertinfo_h handle, uid_t uid)
833 ret = pkgmgrinfo_save_certinfo(pkgid, handle, uid);
837 API int pkgmgr_installer_destroy_certinfo_set_handle(pkgmgr_instcertinfo_h handle)
840 ret = pkgmgrinfo_destroy_certinfo_set_handle(handle);
844 API int pkgmgr_installer_delete_certinfo(const char *pkgid)
847 ret = pkgmgrinfo_delete_certinfo(pkgid);
851 API int pkgmgr_installer_info_get_target_uid(uid_t *uid)