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>
32 #include <sys/types.h>
41 #include <pkgmgr-info.h>
42 #include <pkgmgr/pkgmgr_parser_db.h>
43 #include <tzplatform_config.h>
44 #include <drm-tizen-apps.h>
45 #ifdef TIZEN_FEATURE_CSR
46 #include <csr-content-screening.h>
49 #include "pkgmgr_installer.h"
50 #include "pkgmgr-server.h"
52 #include "package-manager.h"
55 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
59 #define EXT_STORAGE_GROUP 10001
60 #define EXT_STORAGE_APPDATA_GROUP 10002
61 #define MEDIA_STORAGE_GROUP 10502
62 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
63 #define MAX_LONGLONG_LENGTH 32
78 8 bit value to represent maximum 8 backends.
79 Each bit position corresponds to a queue slot which
80 is dynamically determined.
82 static char backend_busy = 0;
83 extern int num_of_backends;
85 struct signal_info_t {
90 static int pipe_sig[2];
91 static GIOChannel *pipe_io;
92 static guint pipe_wid;
93 static GHashTable *backend_info_table;
94 static GMainLoop *mainloop;
96 static int __check_backend_status_for_exit(void);
97 static int __check_queue_status_for_exit(void);
98 static int __is_backend_busy(int position);
99 static void __set_backend_busy(int position);
100 static void __set_backend_free(int position);
101 static void sighandler(int signo);
103 gboolean exit_server(void *data);
105 /* To check whether a particular backend is free/busy */
106 static int __is_backend_busy(int position)
108 return backend_busy & (1 << position);
110 /* To set a particular backend as busy */
111 static void __set_backend_busy(int position)
113 backend_busy = backend_busy | (1 << position);
115 /* To set a particular backend as free */
116 static void __set_backend_free(int position)
118 backend_busy = backend_busy & ~(1 << position);
121 static gboolean getsize_io_handler(GIOChannel *io, GIOCondition cond,
127 char buf[MAX_LONGLONG_LENGTH];
128 long long result = 0;
129 struct backend_job *job = (struct backend_job *)data;
131 s = g_io_channel_read_chars(io, (gchar *)buf, sizeof(buf), &len, &err);
132 if (s != G_IO_STATUS_NORMAL) {
133 ERR("getsize fd read failed: %s", err->message);
137 if (strlen(buf) == 0) {
145 _return_value_to_caller(job->req_id, g_variant_new("(ix)",
146 (result < 0) ? PKGMGR_R_ERROR : PKGMGR_R_OK, result));
148 unlink(job->extra->getsize_fifo);
149 free(job->extra->getsize_fifo);
150 job->extra->getsize_fifo = NULL;
155 static int __setup_size_info_io(struct backend_job *job)
159 job->extra->getsize_fd = open(job->extra->getsize_fifo,
160 O_RDONLY | O_NONBLOCK);
161 if (job->extra->getsize_fd < 0) {
162 ERR("failed to open the fifo(%s), errno(%d)",
163 job->extra->getsize_fifo, errno);
167 job->extra->getsize_io = g_io_channel_unix_new(job->extra->getsize_fd);
168 if (!job->extra->getsize_io)
170 g_io_channel_set_encoding(job->extra->getsize_io, NULL, NULL);
171 g_io_channel_set_buffered(job->extra->getsize_io, FALSE);
172 getsize_wid = g_io_add_watch(job->extra->getsize_io,
173 G_IO_IN, getsize_io_handler, job);
175 ERR("failed to add io watch");
182 static gboolean pipe_io_handler(GIOChannel *io, GIOCondition cond,
188 struct signal_info_t info;
189 struct backend_job *job;
191 s = g_io_channel_read_chars(io, (gchar *)&info,
192 sizeof(struct signal_info_t), &len, &err);
193 if (s != G_IO_STATUS_NORMAL) {
194 ERR("Signal pipe read failed: %s", err->message);
199 job = (struct backend_job *)g_hash_table_lookup(backend_info_table,
200 (gconstpointer)info.pid);
202 ERR("Unknown child exit");
206 __set_backend_free(job->backend_slot);
207 if (WIFSIGNALED(info.status)) {
208 _send_fail_signal(job);
209 DBG("backend[%s] exit with signal[%d]", job->backend_type,
210 WTERMSIG(info.status));
211 } else if (WEXITSTATUS(info.status)) {
212 DBG("backend[%s] exit with error", job->backend_type);
214 DBG("backend[%s] exit", job->backend_type);
218 if (job->extra->getsize_fifo) {
219 ERR("invalid backend close");
220 _return_value_to_caller(job->req_id,
221 g_variant_new("(ix)", PKGMGR_R_ERROR, -1));
225 g_hash_table_remove(backend_info_table, (gconstpointer)info.pid);
226 g_idle_add(queue_job, NULL);
231 static int __init_backend_info(void)
233 backend_info_table = g_hash_table_new_full(g_direct_hash,
234 g_direct_equal, NULL,
235 (GDestroyNotify)_free_backend_job);
237 if (pipe(pipe_sig)) {
238 ERR("create pipe failed");
242 pipe_io = g_io_channel_unix_new(pipe_sig[0]);
243 g_io_channel_set_encoding(pipe_io, NULL, NULL);
244 g_io_channel_set_buffered(pipe_io, FALSE);
245 pipe_wid = g_io_add_watch(pipe_io, G_IO_IN, pipe_io_handler, NULL);
250 static void __fini_backend_info(void)
252 g_source_remove(pipe_wid);
253 g_io_channel_unref(pipe_io);
257 /*Free backend info */
258 g_hash_table_destroy(backend_info_table);
261 static void sighandler(int signo)
263 struct signal_info_t info;
264 char buf[1024] = {0, };
266 info.pid = waitpid(-1, &info.status, WNOHANG);
267 if (write(pipe_sig[1], &info, sizeof(struct signal_info_t)) < 0)
268 ERR("failed to write result: %s",
269 strerror_r(errno, buf, sizeof(buf)));
272 static int __register_signal_handler(void)
274 static int sig_reg = 0;
275 struct sigaction act;
280 act.sa_handler = sighandler;
281 sigemptyset(&act.sa_mask);
282 act.sa_flags = SA_NOCLDSTOP;
283 if (sigaction(SIGCHLD, &act, NULL) < 0) {
284 ERR("signal: SIGCHLD failed");
288 g_timeout_add_seconds(2, exit_server, NULL);
294 static int __check_backend_status_for_exit(void)
297 for (i = 0; i < num_of_backends; i++) {
298 if (__is_backend_busy(i))
304 static int __check_queue_status_for_exit(void)
307 for (i = 0; i < num_of_backends; i++) {
308 if (_is_queue_empty(i) == 0)
314 gboolean exit_server(void *data)
316 DBG("exit_server Start");
317 if (__check_backend_status_for_exit() &&
318 __check_queue_status_for_exit()) {
319 g_main_loop_quit(mainloop);
325 #ifdef TIZEN_FEATURE_CSR
326 static int __check_csr(const char *path)
328 csr_cs_context_h context = NULL;
329 csr_cs_malware_h detected = NULL;
332 ret = csr_cs_context_create(&context);
333 if (ret != CSR_ERROR_NONE) {
334 ERR("Failed to create csr context");
339 ret = csr_cs_scan_file(context, path, &detected);
340 DBG("CSR result[%d]", ret);
342 csr_cs_context_destroy(context);
343 if (detected != NULL) {
344 ERR("CSR Denied[%s] Installation", path);
353 static int __kill_app(char *appid, uid_t uid)
359 is_running = aul_app_is_running_for_uid(appid, uid);
360 /* app is not running */
364 pid = aul_app_get_pid_for_uid(appid, uid);
368 ret = aul_terminate_pid_for_uid(pid, uid);
369 if (ret != AUL_R_OK) {
370 ERR("failed to terminate app(%d)", appid);
377 static int __check_app(char *appid, uid_t uid)
382 is_running = aul_app_is_running_for_uid(appid, uid);
386 pid = aul_app_get_pid_for_uid(appid, uid);
391 static int __pkgcmd_app_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
397 pkgcmd_data *pdata = (pkgcmd_data *)user_data;
399 if (handle == NULL) {
400 perror("appinfo handle is NULL\n");
403 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
405 perror("Failed to get app exec path\n");
408 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
410 perror("Failed to get appid\n");
413 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
415 perror("Failed to get pkgid\n");
419 if (strcmp(pdata->cmd, "kill") == 0) {
420 pdata->pid = __check_app(appid, pdata->uid);
422 ret = __kill_app(appid, pdata->uid);
423 } else if (strcmp(pdata->cmd, "check") == 0) {
424 pdata->pid = __check_app(appid, pdata->uid);
430 void free_user_context(user_ctx *ctx)
437 /* env variable ends by NULL element */
447 int set_environement(user_ctx *ctx)
458 if (ctx->uid != APPFW_UID) {
459 if (setuid(ctx->uid)) {
460 ERR("setuid failed: %d", errno);
464 if (setgid(ctx->gid)) {
465 ERR("setgid failed: %d", errno);
470 n = getgroups(0, NULL);
472 ERR("Failed to get the number of supplementary group IDs");
476 groups = (gid_t *)calloc(1, sizeof(gid_t) * (n + 3));
477 if (groups == NULL) {
478 ERR("out of memory");
482 n = getgroups(n, groups);
484 ERR("Failed to get list of supplementary group IDs");
489 groups[n++] = EXT_STORAGE_GROUP;
490 groups[n++] = EXT_STORAGE_APPDATA_GROUP;
491 groups[n++] = MEDIA_STORAGE_GROUP;
493 if (setgroups(n, groups) < 0) {
494 ERR("setgroups failed: %d", errno);
501 /* env variable ends by NULL element */
503 if (putenv(env[i]) != 0)
510 user_ctx *get_user_context(uid_t uid)
512 /* we can use getpwnam because this is used only after a
513 * fork and just before an execv
514 * No concurrencial call can corrupt the data
515 * returned by getpwuid
517 user_ctx *context_res;
519 char buf[1024] = {0, };
520 struct passwd pwd, *result;
525 ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
526 if (ret != 0 || result == NULL)
530 context_res = (user_ctx *)malloc(sizeof(user_ctx));
535 env = (char **)malloc(3 * sizeof(char *));
540 /* Build environment context */
541 len = snprintf(NULL, 0, "HOME=%s", pwd.pw_dir);
542 env[0] = (char *)malloc((len + 1) * sizeof(char));
543 if (env[0] == NULL) {
547 snprintf(env[0], len + 1, "HOME=%s", pwd.pw_dir);
548 len = snprintf(NULL, 0, "USER=%s", pwd.pw_name);
549 env[1] = (char *)malloc((len + 1) * sizeof(char));
550 if (env[1] == NULL) {
554 snprintf(env[1], len + 1, "USER=%s", pwd.pw_name);
562 /* env variable ends by NULL element */
563 while (env && env[i]) {
570 context_res->env = env;
571 context_res->uid = uid;
572 context_res->gid = pwd.pw_gid;
577 static char **__generate_argv(const char *args)
579 /* Create args vector
580 * req_id + pkgid + args
582 * vector size = # of args +
583 *(req_id + pkgid + NULL termination = 3)
584 * Last value must be NULL for execv.
592 ret_parse = g_shell_parse_argv(args, &argcp, &argvp, &gerr);
593 if (FALSE == ret_parse) {
594 DBG("Failed to split args: %s", args);
595 DBG("messsage: %s", gerr->message);
600 for (i = 0; i < argcp; i++)
601 DBG(">>>>>> argsv[%d]=%s", i, argvp[i]);
606 void __set_environment(gpointer user_data)
608 user_ctx *ctx = (user_ctx *)user_data;
610 if (set_environement(ctx))
611 DBG("Failed to set env for the user : %d", ctx->uid);
614 static int __fork_and_exec_with_args(char **argv, uid_t uid)
616 user_ctx *user_context;
617 GError *error = NULL;
621 user_context = get_user_context(uid);
623 DBG("Failed to getenv");
627 ret = g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
628 __set_environment, (gpointer)user_context, &pid,
631 ERR("Failed to excute backend: %s", error->message);
633 free_user_context(user_context);
637 free_user_context(user_context);
642 static int __change_job_info(struct backend_job *job, uid_t uid,
647 pkgmgrinfo_appinfo_h handle = NULL;
648 if (job->req_type != REQUEST_TYPE_DISABLE_APP &&
649 job->req_type != REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID &&
650 job->req_type != REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN &&
651 job->req_type != REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN &&
652 job->req_type != REQUEST_TYPE_ENABLE_APP &&
653 job->req_type != REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID)
654 return PMINFO_R_ERROR;
656 ret = pkgmgrinfo_appinfo_get_usr_all_appinfo(job->pkgid, uid, &handle);
657 if (ret != PMINFO_R_OK)
658 return PMINFO_R_ERROR;
660 ret = pkgmgrinfo_appinfo_is_global(handle, is_global);
661 if (ret != PMINFO_R_OK)
664 if ((job->req_type == REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID ||
666 REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID) &&
668 ret = PMINFO_R_ERROR;
672 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
673 if (ret != PMINFO_R_OK)
677 job->appid = strdup(job->pkgid);
679 job->pkgid = strdup(pkgid);
682 pkgmgrinfo_appinfo_destroy_appinfo(handle);
687 static int __process_install(struct backend_job *job)
691 char args[MAX_PKG_ARGS_LEN];
694 backend_cmd = job->backend_path;
695 if (backend_cmd == NULL)
698 snprintf(args, sizeof(args), "%s -k %s -i %s -u %d %s", backend_cmd,
699 job->req_id, job->pkgid, (int)job->target_uid, job->args);
701 argv = __generate_argv(args);
703 pid = __fork_and_exec_with_args(argv, APPFW_UID);
709 static int __process_mount_install(struct backend_job *job)
713 char args[MAX_PKG_ARGS_LEN];
716 backend_cmd = job->backend_path;
717 if (backend_cmd == NULL)
720 snprintf(args, sizeof(args), "%s -k %s -w %s -u %d %s", backend_cmd,
721 job->req_id, job->pkgid, (int)job->target_uid, job->args);
723 argv = __generate_argv(args);
725 pid = __fork_and_exec_with_args(argv, APPFW_UID);
731 static int __process_reinstall(struct backend_job *job)
735 char args[MAX_PKG_ARGS_LEN];
738 backend_cmd = job->backend_path;
739 if (backend_cmd == NULL)
742 snprintf(args, sizeof(args), "%s -k %s -r %s -u %d", backend_cmd,
743 job->req_id, job->pkgid, (int)job->target_uid);
744 argv = __generate_argv(args);
746 pid = __fork_and_exec_with_args(argv, APPFW_UID);
753 static int __process_uninstall(struct backend_job *job)
757 char args[MAX_PKG_ARGS_LEN];
760 backend_cmd = job->backend_path;
761 if (backend_cmd == NULL)
764 snprintf(args, sizeof(args), "%s -k %s -d %s -u %d", backend_cmd,
765 job->req_id, job->pkgid, (int)job->target_uid);
766 argv = __generate_argv(args);
768 pid = __fork_and_exec_with_args(argv, APPFW_UID);
775 static int __process_move(struct backend_job *job)
779 char args[MAX_PKG_ARGS_LEN];
782 backend_cmd = job->backend_path;
783 if (backend_cmd == NULL)
786 snprintf(args, sizeof(args), "%s -k %s -m %s -u %d -t %s", backend_cmd,
787 job->req_id, job->pkgid, (int)job->target_uid, job->args);
788 argv = __generate_argv(args);
790 pid = __fork_and_exec_with_args(argv, APPFW_UID);
797 static int __process_enable_pkg(struct backend_job *job)
801 char args[MAX_PKG_ARGS_LEN];
802 pkgmgrinfo_pkginfo_h pkginfo_h;
808 backend_cmd = job->backend_path;
809 if (backend_cmd == NULL)
812 ret = pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(
813 job->pkgid, job->target_uid, &pkginfo_h);
814 if (ret != PMINFO_R_OK) {
815 ERR("Failed to get appinfo");
819 ret = pkgmgrinfo_pkginfo_is_global(pkginfo_h, &is_global);
820 if (ret != PMINFO_R_OK) {
821 ERR("Failed to get global value");
822 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
826 if ((is_global && job->target_uid != GLOBAL_USER) ||
827 (!is_global && job->target_uid == GLOBAL_USER)) {
828 ERR("Invalid attempt to enable pkg");
829 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
833 ret = pkgmgrinfo_pkginfo_is_readonly(pkginfo_h, &is_readonly);
834 if (ret != PMINFO_R_OK) {
835 ERR("Failed to get readonly value");
836 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
840 snprintf(args, sizeof(args), "%s -k %s -u %d -A %s %s",
841 backend_cmd, job->req_id, (int)job->target_uid,
842 job->pkgid, (is_readonly) ? "--preload" : "");
843 argv = __generate_argv(args);
844 pid = __fork_and_exec_with_args(argv, APPFW_UID);
847 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
852 static int __process_disable_pkg(struct backend_job *job)
856 char args[MAX_PKG_ARGS_LEN];
857 pkgmgrinfo_pkginfo_h pkginfo_h;
863 backend_cmd = job->backend_path;
864 if (backend_cmd == NULL)
867 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(
868 job->pkgid, job->target_uid, &pkginfo_h);
869 if (ret != PMINFO_R_OK) {
870 ERR("Failed to get appinfo");
874 ret = pkgmgrinfo_pkginfo_is_global(pkginfo_h, &is_global);
875 if (ret != PMINFO_R_OK) {
876 ERR("Failed to get global value");
877 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
881 if ((is_global && job->target_uid != GLOBAL_USER) ||
882 (!is_global && job->target_uid == GLOBAL_USER)) {
883 ERR("Invalid attempt to disable pkg");
884 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
888 ret = pkgmgrinfo_pkginfo_is_readonly(pkginfo_h, &is_readonly);
889 if (ret != PMINFO_R_OK) {
890 ERR("Failed to get readonly value");
891 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
895 snprintf(args, sizeof(args), "%s -k %s -u %d -D %s %s",
896 backend_cmd, job->req_id, (int)job->target_uid,
897 job->pkgid, (is_readonly) ? "--preload" : "");
898 argv = __generate_argv(args);
900 pid = __fork_and_exec_with_args(argv, APPFW_UID);
904 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
908 static int __process_enable_app(struct backend_job *job)
911 bool is_global = false;
913 /* get actual pkgid and replace it to appid which is currently stored
916 ret = __change_job_info(job, job->target_uid, &is_global);
917 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
918 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
919 PKGMGR_INSTALLER_START_KEY_STR,
920 PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR,
922 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
923 PKGMGR_INSTALLER_END_KEY_STR,
924 PKGMGR_INSTALLER_FAIL_EVENT_STR,
929 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
930 PKGMGR_INSTALLER_START_KEY_STR,
931 PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR, job->req_type);
933 ret = pkgmgr_parser_update_app_disable_info_in_usr_db(job->appid,
935 if (ret != PMINFO_R_OK)
936 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
937 PKGMGR_INSTALLER_END_KEY_STR,
938 PKGMGR_INSTALLER_FAIL_EVENT_STR,
941 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
942 PKGMGR_INSTALLER_END_KEY_STR,
943 PKGMGR_INSTALLER_OK_EVENT_STR,
949 static int __process_disable_app(struct backend_job *job)
952 bool is_global = false;
954 /* get actual pkgid and replace it to appid which is currently stored
957 ret = __change_job_info(job, job->target_uid, &is_global);
958 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
959 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
960 PKGMGR_INSTALLER_START_KEY_STR,
961 PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR,
963 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
964 PKGMGR_INSTALLER_END_KEY_STR,
965 PKGMGR_INSTALLER_FAIL_EVENT_STR,
970 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
971 PKGMGR_INSTALLER_START_KEY_STR,
972 PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR, job->req_type);
974 ret = __kill_app(job->appid, job->target_uid);
976 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
977 PKGMGR_INSTALLER_END_KEY_STR,
978 PKGMGR_INSTALLER_FAIL_EVENT_STR,
982 ret = pkgmgr_parser_update_app_disable_info_in_usr_db(job->appid,
984 if (ret != PMINFO_R_OK)
985 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
986 PKGMGR_INSTALLER_END_KEY_STR,
987 PKGMGR_INSTALLER_FAIL_EVENT_STR,
990 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
991 PKGMGR_INSTALLER_END_KEY_STR,
992 PKGMGR_INSTALLER_OK_EVENT_STR,
998 static int __process_enable_global_app_for_uid(struct backend_job *job)
1001 bool is_global = true;
1003 /* get actual pkgid and replace it to appid which is currently stored
1006 ret = __change_job_info(job, job->target_uid, &is_global);
1007 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
1008 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1009 PKGMGR_INSTALLER_START_KEY_STR,
1010 PKGMGR_INSTALLER_GLOBAL_APP_ENABLE_FOR_UID,
1012 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1013 PKGMGR_INSTALLER_END_KEY_STR,
1014 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1019 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1020 PKGMGR_INSTALLER_START_KEY_STR,
1021 PKGMGR_INSTALLER_GLOBAL_APP_ENABLE_FOR_UID,
1024 ret = pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1025 job->appid, job->target_uid, 0);
1026 if (ret != PMINFO_R_OK)
1027 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1028 PKGMGR_INSTALLER_END_KEY_STR,
1029 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1032 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1033 PKGMGR_INSTALLER_END_KEY_STR,
1034 PKGMGR_INSTALLER_OK_EVENT_STR,
1040 static int __process_disable_global_app_for_uid(struct backend_job *job)
1043 bool is_global = true;
1045 /* get actual pkgid and replace it to appid which is currently stored
1048 ret = __change_job_info(job, GLOBAL_USER, &is_global);
1049 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
1050 _send_app_signal(job->target_uid, job->req_id,
1051 job->pkgid, job->pkgid,
1052 PKGMGR_INSTALLER_START_KEY_STR,
1053 PKGMGR_INSTALLER_GLOBAL_APP_DISABLE_FOR_UID,
1055 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1056 PKGMGR_INSTALLER_END_KEY_STR,
1057 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1062 _send_app_signal(job->target_uid, job->req_id,
1063 job->pkgid, job->appid,
1064 PKGMGR_INSTALLER_START_KEY_STR,
1065 PKGMGR_INSTALLER_GLOBAL_APP_DISABLE_FOR_UID,
1068 ret = __kill_app(job->appid, job->target_uid);
1069 ret = pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1070 job->appid, job->target_uid, 1);
1072 if (ret != PMINFO_R_OK)
1073 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1074 PKGMGR_INSTALLER_END_KEY_STR,
1075 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1078 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1079 PKGMGR_INSTALLER_END_KEY_STR,
1080 PKGMGR_INSTALLER_OK_EVENT_STR,
1086 static int __process_getsize(struct backend_job *job)
1088 static const char backend_cmd[] = "/usr/bin/pkg_getsize";
1090 char args[MAX_PKG_ARGS_LEN];
1093 snprintf(args, sizeof(args), "%s %s %s %d -k %s -u %d",
1094 backend_cmd, job->pkgid, job->args, job->caller_uid,
1095 job->req_id, job->target_uid);
1096 argv = __generate_argv(args);
1097 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1104 static int __process_getsize_sync(struct backend_job *job)
1106 static const char backend_cmd[] = "/usr/bin/pkg_getsize";
1108 char args[MAX_PKG_ARGS_LEN];
1109 char fifo_path[PATH_MAX];
1112 snprintf(fifo_path, sizeof(fifo_path), "/tmp/pkgmgr/%s",
1115 job->extra = calloc(1, sizeof(struct job_extra_info));
1117 ERR("memory alloc failed");
1121 job->extra->getsize_fifo = strdup(fifo_path);
1122 if (!job->extra->getsize_fifo) {
1123 ERR("out of memory");
1127 if (mkfifo(job->extra->getsize_fifo, 0600) < 0) {
1128 ERR("failed to mkfifo");
1132 snprintf(args, sizeof(args), "%s %s %s %d -k %s -u %d --sync",
1133 backend_cmd, job->pkgid, job->args, job->caller_uid,
1134 job->req_id, job->target_uid);
1135 argv = __generate_argv(args);
1136 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1141 ERR("failed to execute backend");
1144 if (__setup_size_info_io(job) < 0) {
1145 ERR("failed to setup io handler");
1152 _return_value_to_caller(job->req_id,
1153 g_variant_new("(ix)", PKGMGR_R_ERROR, -1));
1157 static int __process_cleardata(struct backend_job *job)
1161 char args[MAX_PKG_ARGS_LEN];
1164 backend_cmd = job->backend_path;
1165 if (backend_cmd == NULL)
1168 /* TODO: set movetype */
1169 snprintf(args, sizeof(args), "%s -k %s -c %s -u %d", backend_cmd,
1170 job->req_id, job->pkgid, (int)job->target_uid);
1171 argv = __generate_argv(args);
1173 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1180 static int __process_clearcache(struct backend_job *job)
1182 static const char *backend_cmd = "/usr/bin/pkg_clearcache";
1184 char args[MAX_PKG_ARGS_LEN];
1187 snprintf(args, sizeof(args), "%s %s", backend_cmd, job->pkgid);
1188 argv = __generate_argv(args);
1189 pid = __fork_and_exec_with_args(argv, job->target_uid);
1196 static int __process_kill(struct backend_job *job)
1199 pkgmgrinfo_pkginfo_h handle;
1202 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(job->pkgid, job->target_uid,
1205 ERR("Failed to get handle");
1206 _return_value_to_caller(job->req_id,
1207 g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
1211 pdata = calloc(1, sizeof(pkgcmd_data));
1212 if (pdata == NULL) {
1213 ERR("memory alloc failed");
1214 _return_value_to_caller(job->req_id,
1215 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1218 pdata->cmd = strdup("kill");
1219 if (pdata->cmd == NULL) {
1220 ERR("out of memory");
1221 _return_value_to_caller(job->req_id,
1222 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1226 pdata->uid = job->target_uid;
1227 ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
1228 __pkgcmd_app_cb, pdata, job->target_uid);
1230 _return_value_to_caller(job->req_id,
1231 g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
1235 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1237 ERR("pkgmgrinfo_appinfo_get_list() failed");
1244 static int __process_check(struct backend_job *job)
1247 pkgmgrinfo_pkginfo_h handle;
1250 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(job->pkgid, job->target_uid,
1253 ERR("Failed to get handle");
1254 _return_value_to_caller(job->req_id,
1255 g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
1259 pdata = calloc(1, sizeof(pkgcmd_data));
1260 if (pdata == NULL) {
1261 ERR("memory alloc failed");
1262 _return_value_to_caller(job->req_id,
1263 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1266 pdata->cmd = strdup("check");
1267 if (pdata->cmd == NULL) {
1268 ERR("out of memory");
1269 _return_value_to_caller(job->req_id,
1270 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1274 pdata->uid = job->target_uid;
1275 ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
1276 __pkgcmd_app_cb, pdata, job->target_uid);
1278 _return_value_to_caller(job->req_id,
1279 g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
1283 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1285 ERR("pkgmgrinfo_appinfo_get_list() failed");
1292 static int __process_generate_license_request(struct backend_job *job)
1296 char req_data[MAX_PKG_ARGS_LEN];
1297 unsigned int req_data_len;
1298 char license_url[MAX_PKG_ARGS_LEN];
1299 unsigned int license_url_len;
1301 resp_data = job->args;
1302 req_data_len = sizeof(req_data);
1303 license_url_len = sizeof(license_url);
1305 ret = drm_tizen_generate_license_request(resp_data, strlen(resp_data),
1306 req_data, &req_data_len, license_url, &license_url_len);
1307 if (ret != TADC_SUCCESS) {
1308 ERR("drm_tizen_generate_license_request failed: %d", ret);
1309 _return_value_to_caller(job->req_id, g_variant_new("(iss)",
1310 PKGMGR_R_ESYSTEM, "", ""));
1314 _return_value_to_caller(job->req_id,
1315 g_variant_new("(iss)", PKGMGR_R_OK, req_data,
1321 static int __process_register_license(struct backend_job *job)
1326 resp_data = job->args;
1328 ret = drm_tizen_register_license(resp_data, strlen(resp_data));
1329 if (ret != TADC_SUCCESS) {
1330 ERR("drm_tizen_register_license failed: %d", ret);
1331 _return_value_to_caller(job->req_id,
1332 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1336 _return_value_to_caller(job->req_id,
1337 g_variant_new("(i)", PKGMGR_R_OK));
1342 static int __process_decrypt_package(struct backend_job *job)
1345 char *drm_file_path;
1346 char *decrypted_file_path;
1348 drm_file_path = job->pkgid;
1349 decrypted_file_path = job->args;
1351 /* TODO: check ownership of decrypted file */
1352 ret = drm_tizen_decrypt_package(drm_file_path, strlen(drm_file_path),
1353 decrypted_file_path, strlen(decrypted_file_path));
1354 if (ret != TADC_SUCCESS) {
1355 ERR("drm_tizen_register_license failed: %d", ret);
1356 _return_value_to_caller(job->req_id,
1357 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1361 _return_value_to_caller(job->req_id,
1362 g_variant_new("(i)", PKGMGR_R_OK));
1367 static int __process_update_app_splash_screen(struct backend_job *job, int flag)
1370 bool is_global = false;
1373 ret = __change_job_info(job, job->target_uid, &is_global);
1374 if (ret != PMINFO_R_OK || strlen(job->appid) == 0)
1377 val = flag ? PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR :
1378 PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR;
1379 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1380 PKGMGR_INSTALLER_START_KEY_STR, val, job->req_type);
1383 ret = pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(job->appid, job->target_uid, flag);
1385 ret = pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
1386 job->appid, job->target_uid, flag);
1387 if (ret != PMINFO_R_OK)
1388 _send_app_signal(job->target_uid, job->req_id, job->pkgid,
1389 job->appid, PKGMGR_INSTALLER_END_KEY_STR,
1390 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1393 _send_app_signal(job->target_uid, job->req_id, job->pkgid,
1394 job->appid, PKGMGR_INSTALLER_END_KEY_STR,
1395 PKGMGR_INSTALLER_OK_EVENT_STR,
1401 static int __process_set_restriction_mode(struct backend_job *job)
1406 mode = atoi(job->args);
1407 ret = _set_restriction_mode(job->target_uid, job->pkgid, mode);
1409 _return_value_to_caller(job->req_id,
1410 g_variant_new("(i)", ret));
1415 static int __process_unset_restriction_mode(struct backend_job *job)
1420 mode = atoi(job->args);
1421 ret = _unset_restriction_mode(job->target_uid, job->pkgid, mode);
1423 _return_value_to_caller(job->req_id,
1424 g_variant_new("(i)", ret));
1429 static int __process_get_restriction_mode(struct backend_job *job)
1434 ret = _get_restriction_mode(job->target_uid, job->pkgid, &mode);
1436 _return_value_to_caller(job->req_id,
1437 g_variant_new("(ii)", mode, ret));
1442 static int __process_set_app_label(struct backend_job *job)
1446 ret = pkgmgr_parser_update_app_label_info_in_usr_db(job->pkgid,
1447 job->target_uid, job->args);
1448 _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));
1453 static int __post_process(int ret, int x, struct backend_job *job)
1456 __set_backend_free(x);
1457 _free_backend_job(job);
1459 g_hash_table_insert(backend_info_table, (gpointer)ret,
1465 gboolean queue_job(void *data)
1467 struct backend_job *job = NULL;
1471 /* Pop a job from queue */
1472 for (x = 0; x < num_of_backends; x++) {
1473 if (__is_backend_busy(x))
1476 job = _pop_queue(x);
1481 /* all backend messages queue are empty or busy */
1482 if (x == num_of_backends || job == NULL) {
1487 /* set current backend job */
1488 DBG("handle request type [%d]", job->req_type);
1490 #ifdef TIZEN_FEATURE_CSR
1491 if (job->req_type == REQUEST_TYPE_INSTALL ||
1492 job->req_type == REQUEST_TYPE_MOUNT_INSTALL ||
1493 job->req_type == REQUEST_TYPE_REINSTALL) {
1494 ret = __check_csr(job->pkgid);
1497 _send_fail_signal(job);
1498 _free_backend_job(job);
1504 switch (job->req_type) {
1505 case REQUEST_TYPE_INSTALL:
1506 __set_backend_busy(x);
1507 ret = __process_install(job);
1508 __post_process(ret, x, job);
1510 case REQUEST_TYPE_MOUNT_INSTALL:
1511 __set_backend_busy(x);
1512 ret = __process_mount_install(job);
1513 __post_process(ret, x, job);
1515 case REQUEST_TYPE_REINSTALL:
1516 __set_backend_busy(x);
1517 ret = __process_reinstall(job);
1518 __post_process(ret, x, job);
1520 case REQUEST_TYPE_UNINSTALL:
1521 __set_backend_busy(x);
1522 ret = __process_uninstall(job);
1523 __post_process(ret, x, job);
1525 case REQUEST_TYPE_MOVE:
1526 __set_backend_busy(x);
1527 ret = __process_move(job);
1528 __post_process(ret, x, job);
1530 case REQUEST_TYPE_ENABLE_PKG:
1531 __set_backend_busy(x);
1532 ret = __process_enable_pkg(job);
1534 _send_fail_signal(job);
1535 __post_process(ret, x, job);
1537 case REQUEST_TYPE_DISABLE_PKG:
1538 __set_backend_busy(x);
1539 ret = __process_disable_pkg(job);
1541 _send_fail_signal(job);
1542 __post_process(ret, x, job);
1544 case REQUEST_TYPE_ENABLE_APP:
1545 ret = __process_enable_app(job);
1546 _free_backend_job(job);
1548 case REQUEST_TYPE_DISABLE_APP:
1549 ret = __process_disable_app(job);
1550 _free_backend_job(job);
1552 case REQUEST_TYPE_GETSIZE:
1553 __set_backend_busy(x);
1554 ret = __process_getsize(job);
1555 __post_process(ret, x, job);
1557 case REQUEST_TYPE_GETSIZE_SYNC:
1558 __set_backend_busy(x);
1559 ret = __process_getsize_sync(job);
1561 __free_extra_info(job);
1562 __post_process(ret, x, job);
1564 case REQUEST_TYPE_CLEARDATA:
1565 __set_backend_busy(x);
1566 ret = __process_cleardata(job);
1567 __post_process(ret, x, job);
1569 case REQUEST_TYPE_CLEARCACHE:
1570 __set_backend_busy(x);
1571 ret = __process_clearcache(job);
1572 __post_process(ret, x, job);
1574 case REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID:
1575 ret = __process_enable_global_app_for_uid(job);
1576 _free_backend_job(job);
1578 case REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID:
1579 ret = __process_disable_global_app_for_uid(job);
1580 _free_backend_job(job);
1582 case REQUEST_TYPE_KILL:
1583 ret = __process_kill(job);
1584 _free_backend_job(job);
1586 case REQUEST_TYPE_CHECK:
1587 ret = __process_check(job);
1588 _free_backend_job(job);
1590 case REQUEST_TYPE_GENERATE_LICENSE_REQUEST:
1591 ret = __process_generate_license_request(job);
1592 _free_backend_job(job);
1594 case REQUEST_TYPE_REGISTER_LICENSE:
1595 ret = __process_register_license(job);
1596 _free_backend_job(job);
1598 case REQUEST_TYPE_DECRYPT_PACKAGE:
1599 ret = __process_decrypt_package(job);
1600 _free_backend_job(job);
1602 case REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN:
1603 ret = __process_update_app_splash_screen(job, 1);
1604 _free_backend_job(job);
1606 case REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN:
1607 ret = __process_update_app_splash_screen(job, 0);
1608 _free_backend_job(job);
1610 case REQUEST_TYPE_SET_RESTRICTION_MODE:
1611 ret = __process_set_restriction_mode(job);
1612 _free_backend_job(job);
1614 case REQUEST_TYPE_UNSET_RESTRICTION_MODE:
1615 ret = __process_unset_restriction_mode(job);
1616 _free_backend_job(job);
1618 case REQUEST_TYPE_GET_RESTRICTION_MODE:
1619 ret = __process_get_restriction_mode(job);
1620 _free_backend_job(job);
1622 case REQUEST_TYPE_SET_APP_LABEL:
1623 ret = __process_set_app_label(job);
1624 _free_backend_job(job);
1634 int main(int argc, char *argv[])
1638 DBG("server start");
1640 r = _init_backend_queue();
1642 DBG("Queue Initialization Failed");
1646 r = __init_backend_info();
1648 DBG("backend info init failed");
1652 r = _init_request_handler();
1654 ERR("dbus init failed");
1658 if (__register_signal_handler()) {
1659 ERR("failed to register signal handler");
1663 #if !GLIB_CHECK_VERSION(2, 35, 0)
1666 mainloop = g_main_loop_new(NULL, FALSE);
1668 ERR("g_main_loop_new failed");
1672 DBG("Main loop is created.");
1674 g_main_loop_run(mainloop);
1676 DBG("Quit main loop.");
1677 _fini_request_handler();
1678 __fini_backend_info();
1679 _fini_backend_queue();
1681 DBG("package manager server terminated.");