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_APP ||
665 job->req_type == REQUEST_TYPE_ENABLE_APP) &&
667 ret = PMINFO_R_ERROR;
669 } else if ((job->req_type == REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID ||
671 REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID) &&
673 ret = PMINFO_R_ERROR;
677 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
678 if (ret != PMINFO_R_OK)
682 job->appid = strdup(job->pkgid);
684 job->pkgid = strdup(pkgid);
687 pkgmgrinfo_appinfo_destroy_appinfo(handle);
692 static int __process_install(struct backend_job *job)
696 char args[MAX_PKG_ARGS_LEN];
699 backend_cmd = job->backend_path;
700 if (backend_cmd == NULL)
703 snprintf(args, sizeof(args), "%s -k %s -i %s -u %d %s", backend_cmd,
704 job->req_id, job->pkgid, (int)job->target_uid, job->args);
706 argv = __generate_argv(args);
708 pid = __fork_and_exec_with_args(argv, APPFW_UID);
714 static int __process_mount_install(struct backend_job *job)
718 char args[MAX_PKG_ARGS_LEN];
721 backend_cmd = job->backend_path;
722 if (backend_cmd == NULL)
725 snprintf(args, sizeof(args), "%s -k %s -w %s -u %d %s", backend_cmd,
726 job->req_id, job->pkgid, (int)job->target_uid, job->args);
728 argv = __generate_argv(args);
730 pid = __fork_and_exec_with_args(argv, APPFW_UID);
736 static int __process_reinstall(struct backend_job *job)
740 char args[MAX_PKG_ARGS_LEN];
743 backend_cmd = job->backend_path;
744 if (backend_cmd == NULL)
747 snprintf(args, sizeof(args), "%s -k %s -r %s -u %d", backend_cmd,
748 job->req_id, job->pkgid, (int)job->target_uid);
749 argv = __generate_argv(args);
751 pid = __fork_and_exec_with_args(argv, APPFW_UID);
758 static int __process_uninstall(struct backend_job *job)
762 char args[MAX_PKG_ARGS_LEN];
765 backend_cmd = job->backend_path;
766 if (backend_cmd == NULL)
769 snprintf(args, sizeof(args), "%s -k %s -d %s -u %d", backend_cmd,
770 job->req_id, job->pkgid, (int)job->target_uid);
771 argv = __generate_argv(args);
773 pid = __fork_and_exec_with_args(argv, APPFW_UID);
780 static int __process_move(struct backend_job *job)
784 char args[MAX_PKG_ARGS_LEN];
787 backend_cmd = job->backend_path;
788 if (backend_cmd == NULL)
791 snprintf(args, sizeof(args), "%s -k %s -m %s -u %d -t %s", backend_cmd,
792 job->req_id, job->pkgid, (int)job->target_uid, job->args);
793 argv = __generate_argv(args);
795 pid = __fork_and_exec_with_args(argv, APPFW_UID);
802 static int __process_enable_pkg(struct backend_job *job)
806 char args[MAX_PKG_ARGS_LEN];
807 pkgmgrinfo_pkginfo_h pkginfo_h;
813 backend_cmd = job->backend_path;
814 if (backend_cmd == NULL)
817 ret = pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(
818 job->pkgid, job->target_uid, &pkginfo_h);
819 if (ret != PMINFO_R_OK) {
820 ERR("Failed to get appinfo");
824 ret = pkgmgrinfo_pkginfo_is_global(pkginfo_h, &is_global);
825 if (ret != PMINFO_R_OK) {
826 ERR("Failed to get global value");
827 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
831 if ((is_global && job->target_uid != GLOBAL_USER) ||
832 (!is_global && job->target_uid == GLOBAL_USER)) {
833 ERR("Invalid attempt to enable pkg");
834 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
838 ret = pkgmgrinfo_pkginfo_is_readonly(pkginfo_h, &is_readonly);
839 if (ret != PMINFO_R_OK) {
840 ERR("Failed to get readonly value");
841 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
845 snprintf(args, sizeof(args), "%s -k %s -u %d -A %s %s",
846 backend_cmd, job->req_id, (int)job->target_uid,
847 job->pkgid, (is_readonly) ? "--preload" : "");
848 argv = __generate_argv(args);
849 pid = __fork_and_exec_with_args(argv, APPFW_UID);
852 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
857 static int __process_disable_pkg(struct backend_job *job)
861 char args[MAX_PKG_ARGS_LEN];
862 pkgmgrinfo_pkginfo_h pkginfo_h;
868 backend_cmd = job->backend_path;
869 if (backend_cmd == NULL)
872 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(
873 job->pkgid, job->target_uid, &pkginfo_h);
874 if (ret != PMINFO_R_OK) {
875 ERR("Failed to get appinfo");
879 ret = pkgmgrinfo_pkginfo_is_global(pkginfo_h, &is_global);
880 if (ret != PMINFO_R_OK) {
881 ERR("Failed to get global value");
882 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
886 if ((is_global && job->target_uid != GLOBAL_USER) ||
887 (!is_global && job->target_uid == GLOBAL_USER)) {
888 ERR("Invalid attempt to disable pkg");
889 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
893 ret = pkgmgrinfo_pkginfo_is_readonly(pkginfo_h, &is_readonly);
894 if (ret != PMINFO_R_OK) {
895 ERR("Failed to get readonly value");
896 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
900 snprintf(args, sizeof(args), "%s -k %s -u %d -D %s %s",
901 backend_cmd, job->req_id, (int)job->target_uid,
902 job->pkgid, (is_readonly) ? "--preload" : "");
903 argv = __generate_argv(args);
905 pid = __fork_and_exec_with_args(argv, APPFW_UID);
909 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
913 static int __process_enable_app(struct backend_job *job)
916 bool is_global = false;
918 /* get actual pkgid and replace it to appid which is currently stored
921 ret = __change_job_info(job, job->target_uid, &is_global);
922 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
923 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
924 PKGMGR_INSTALLER_START_KEY_STR,
925 PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR,
927 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
928 PKGMGR_INSTALLER_END_KEY_STR,
929 PKGMGR_INSTALLER_FAIL_EVENT_STR,
934 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
935 PKGMGR_INSTALLER_START_KEY_STR,
936 PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR, job->req_type);
938 ret = pkgmgr_parser_update_app_disable_info_in_usr_db(job->appid,
940 if (ret != PMINFO_R_OK)
941 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
942 PKGMGR_INSTALLER_END_KEY_STR,
943 PKGMGR_INSTALLER_FAIL_EVENT_STR,
946 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
947 PKGMGR_INSTALLER_END_KEY_STR,
948 PKGMGR_INSTALLER_OK_EVENT_STR,
954 static int __process_disable_app(struct backend_job *job)
957 bool is_global = false;
959 /* get actual pkgid and replace it to appid which is currently stored
962 ret = __change_job_info(job, job->target_uid, &is_global);
963 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
964 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
965 PKGMGR_INSTALLER_START_KEY_STR,
966 PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR,
968 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
969 PKGMGR_INSTALLER_END_KEY_STR,
970 PKGMGR_INSTALLER_FAIL_EVENT_STR,
975 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
976 PKGMGR_INSTALLER_START_KEY_STR,
977 PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR, job->req_type);
979 ret = __kill_app(job->appid, job->target_uid);
981 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
982 PKGMGR_INSTALLER_END_KEY_STR,
983 PKGMGR_INSTALLER_FAIL_EVENT_STR,
987 ret = pkgmgr_parser_update_app_disable_info_in_usr_db(job->appid,
989 if (ret != PMINFO_R_OK)
990 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
991 PKGMGR_INSTALLER_END_KEY_STR,
992 PKGMGR_INSTALLER_FAIL_EVENT_STR,
995 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
996 PKGMGR_INSTALLER_END_KEY_STR,
997 PKGMGR_INSTALLER_OK_EVENT_STR,
1003 static int __process_enable_global_app_for_uid(struct backend_job *job)
1006 bool is_global = true;
1008 /* get actual pkgid and replace it to appid which is currently stored
1011 ret = __change_job_info(job, job->target_uid, &is_global);
1012 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
1013 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1014 PKGMGR_INSTALLER_START_KEY_STR,
1015 PKGMGR_INSTALLER_GLOBAL_APP_ENABLE_FOR_UID,
1017 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1018 PKGMGR_INSTALLER_END_KEY_STR,
1019 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1024 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1025 PKGMGR_INSTALLER_START_KEY_STR,
1026 PKGMGR_INSTALLER_GLOBAL_APP_ENABLE_FOR_UID,
1029 ret = pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1030 job->appid, job->target_uid, 0);
1031 if (ret != PMINFO_R_OK)
1032 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1033 PKGMGR_INSTALLER_END_KEY_STR,
1034 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1037 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1038 PKGMGR_INSTALLER_END_KEY_STR,
1039 PKGMGR_INSTALLER_OK_EVENT_STR,
1045 static int __process_disable_global_app_for_uid(struct backend_job *job)
1048 bool is_global = true;
1050 /* get actual pkgid and replace it to appid which is currently stored
1053 ret = __change_job_info(job, GLOBAL_USER, &is_global);
1054 if (ret != PMINFO_R_OK || strlen(job->appid) == 0) {
1055 _send_app_signal(job->target_uid, job->req_id,
1056 job->pkgid, job->pkgid,
1057 PKGMGR_INSTALLER_START_KEY_STR,
1058 PKGMGR_INSTALLER_GLOBAL_APP_DISABLE_FOR_UID,
1060 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->pkgid,
1061 PKGMGR_INSTALLER_END_KEY_STR,
1062 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1067 _send_app_signal(job->target_uid, job->req_id,
1068 job->pkgid, job->appid,
1069 PKGMGR_INSTALLER_START_KEY_STR,
1070 PKGMGR_INSTALLER_GLOBAL_APP_DISABLE_FOR_UID,
1073 ret = __kill_app(job->appid, job->target_uid);
1074 ret = pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1075 job->appid, job->target_uid, 1);
1077 if (ret != PMINFO_R_OK)
1078 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1079 PKGMGR_INSTALLER_END_KEY_STR,
1080 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1083 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1084 PKGMGR_INSTALLER_END_KEY_STR,
1085 PKGMGR_INSTALLER_OK_EVENT_STR,
1091 static int __process_getsize(struct backend_job *job)
1093 static const char backend_cmd[] = "/usr/bin/pkg_getsize";
1095 char args[MAX_PKG_ARGS_LEN];
1098 snprintf(args, sizeof(args), "%s %s %s %d -k %s -u %d",
1099 backend_cmd, job->pkgid, job->args, job->caller_uid,
1100 job->req_id, job->target_uid);
1101 argv = __generate_argv(args);
1102 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1109 static int __process_getsize_sync(struct backend_job *job)
1111 static const char backend_cmd[] = "/usr/bin/pkg_getsize";
1113 char args[MAX_PKG_ARGS_LEN];
1114 char fifo_path[PATH_MAX];
1117 snprintf(fifo_path, sizeof(fifo_path), "/tmp/pkgmgr/%s",
1120 job->extra = calloc(1, sizeof(struct job_extra_info));
1122 ERR("memory alloc failed");
1126 job->extra->getsize_fifo = strdup(fifo_path);
1127 if (!job->extra->getsize_fifo) {
1128 ERR("out of memory");
1132 if (mkfifo(job->extra->getsize_fifo, 0600) < 0) {
1133 ERR("failed to mkfifo");
1137 snprintf(args, sizeof(args), "%s %s %s %d -k %s -u %d --sync",
1138 backend_cmd, job->pkgid, job->args, job->caller_uid,
1139 job->req_id, job->target_uid);
1140 argv = __generate_argv(args);
1141 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1146 ERR("failed to execute backend");
1149 if (__setup_size_info_io(job) < 0) {
1150 ERR("failed to setup io handler");
1157 _return_value_to_caller(job->req_id,
1158 g_variant_new("(ix)", PKGMGR_R_ERROR, -1));
1162 static int __process_cleardata(struct backend_job *job)
1166 char args[MAX_PKG_ARGS_LEN];
1169 backend_cmd = job->backend_path;
1170 if (backend_cmd == NULL)
1173 /* TODO: set movetype */
1174 snprintf(args, sizeof(args), "%s -k %s -c %s -u %d", backend_cmd,
1175 job->req_id, job->pkgid, (int)job->target_uid);
1176 argv = __generate_argv(args);
1178 pid = __fork_and_exec_with_args(argv, APPFW_UID);
1185 static int __process_clearcache(struct backend_job *job)
1187 static const char *backend_cmd = "/usr/bin/pkg_clearcache";
1189 char args[MAX_PKG_ARGS_LEN];
1192 snprintf(args, sizeof(args), "%s %s", backend_cmd, job->pkgid);
1193 argv = __generate_argv(args);
1194 pid = __fork_and_exec_with_args(argv, job->target_uid);
1201 static int __process_kill(struct backend_job *job)
1204 pkgmgrinfo_pkginfo_h handle;
1207 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(job->pkgid, job->target_uid,
1210 ERR("Failed to get handle");
1211 _return_value_to_caller(job->req_id,
1212 g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
1216 pdata = calloc(1, sizeof(pkgcmd_data));
1217 if (pdata == NULL) {
1218 ERR("memory alloc failed");
1219 _return_value_to_caller(job->req_id,
1220 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1223 pdata->cmd = strdup("kill");
1224 if (pdata->cmd == NULL) {
1225 ERR("out of memory");
1226 _return_value_to_caller(job->req_id,
1227 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1231 pdata->uid = job->target_uid;
1232 ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
1233 __pkgcmd_app_cb, pdata, job->target_uid);
1235 _return_value_to_caller(job->req_id,
1236 g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
1240 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1242 ERR("pkgmgrinfo_appinfo_get_list() failed");
1249 static int __process_check(struct backend_job *job)
1252 pkgmgrinfo_pkginfo_h handle;
1255 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(job->pkgid, job->target_uid,
1258 ERR("Failed to get handle");
1259 _return_value_to_caller(job->req_id,
1260 g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
1264 pdata = calloc(1, sizeof(pkgcmd_data));
1265 if (pdata == NULL) {
1266 ERR("memory alloc failed");
1267 _return_value_to_caller(job->req_id,
1268 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1271 pdata->cmd = strdup("check");
1272 if (pdata->cmd == NULL) {
1273 ERR("out of memory");
1274 _return_value_to_caller(job->req_id,
1275 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1279 pdata->uid = job->target_uid;
1280 ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
1281 __pkgcmd_app_cb, pdata, job->target_uid);
1283 _return_value_to_caller(job->req_id,
1284 g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
1288 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1290 ERR("pkgmgrinfo_appinfo_get_list() failed");
1297 static int __process_generate_license_request(struct backend_job *job)
1301 char req_data[MAX_PKG_ARGS_LEN];
1302 unsigned int req_data_len;
1303 char license_url[MAX_PKG_ARGS_LEN];
1304 unsigned int license_url_len;
1306 resp_data = job->args;
1307 req_data_len = sizeof(req_data);
1308 license_url_len = sizeof(license_url);
1310 ret = drm_tizen_generate_license_request(resp_data, strlen(resp_data),
1311 req_data, &req_data_len, license_url, &license_url_len);
1312 if (ret != TADC_SUCCESS) {
1313 ERR("drm_tizen_generate_license_request failed: %d", ret);
1314 _return_value_to_caller(job->req_id, g_variant_new("(iss)",
1315 PKGMGR_R_ESYSTEM, "", ""));
1319 _return_value_to_caller(job->req_id,
1320 g_variant_new("(iss)", PKGMGR_R_OK, req_data,
1326 static int __process_register_license(struct backend_job *job)
1331 resp_data = job->args;
1333 ret = drm_tizen_register_license(resp_data, strlen(resp_data));
1334 if (ret != TADC_SUCCESS) {
1335 ERR("drm_tizen_register_license failed: %d", ret);
1336 _return_value_to_caller(job->req_id,
1337 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1341 _return_value_to_caller(job->req_id,
1342 g_variant_new("(i)", PKGMGR_R_OK));
1347 static int __process_decrypt_package(struct backend_job *job)
1350 char *drm_file_path;
1351 char *decrypted_file_path;
1353 drm_file_path = job->pkgid;
1354 decrypted_file_path = job->args;
1356 /* TODO: check ownership of decrypted file */
1357 ret = drm_tizen_decrypt_package(drm_file_path, strlen(drm_file_path),
1358 decrypted_file_path, strlen(decrypted_file_path));
1359 if (ret != TADC_SUCCESS) {
1360 ERR("drm_tizen_register_license failed: %d", ret);
1361 _return_value_to_caller(job->req_id,
1362 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1366 _return_value_to_caller(job->req_id,
1367 g_variant_new("(i)", PKGMGR_R_OK));
1372 static int __process_update_app_splash_screen(struct backend_job *job, int flag)
1375 bool is_global = false;
1378 ret = __change_job_info(job, job->target_uid, &is_global);
1379 if (ret != PMINFO_R_OK || strlen(job->appid) == 0)
1382 val = flag ? PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR :
1383 PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR;
1384 _send_app_signal(job->target_uid, job->req_id, job->pkgid, job->appid,
1385 PKGMGR_INSTALLER_START_KEY_STR, val, job->req_type);
1388 ret = pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(job->appid, job->target_uid, flag);
1390 ret = pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
1391 job->appid, job->target_uid, flag);
1392 if (ret != PMINFO_R_OK)
1393 _send_app_signal(job->target_uid, job->req_id, job->pkgid,
1394 job->appid, PKGMGR_INSTALLER_END_KEY_STR,
1395 PKGMGR_INSTALLER_FAIL_EVENT_STR,
1398 _send_app_signal(job->target_uid, job->req_id, job->pkgid,
1399 job->appid, PKGMGR_INSTALLER_END_KEY_STR,
1400 PKGMGR_INSTALLER_OK_EVENT_STR,
1406 static int __process_set_restriction_mode(struct backend_job *job)
1411 mode = atoi(job->args);
1412 ret = _set_restriction_mode(job->target_uid, job->pkgid, mode);
1414 _return_value_to_caller(job->req_id,
1415 g_variant_new("(i)", ret));
1420 static int __process_unset_restriction_mode(struct backend_job *job)
1425 mode = atoi(job->args);
1426 ret = _unset_restriction_mode(job->target_uid, job->pkgid, mode);
1428 _return_value_to_caller(job->req_id,
1429 g_variant_new("(i)", ret));
1434 static int __process_get_restriction_mode(struct backend_job *job)
1439 ret = _get_restriction_mode(job->target_uid, job->pkgid, &mode);
1441 _return_value_to_caller(job->req_id,
1442 g_variant_new("(ii)", mode, ret));
1447 static int __process_set_app_label(struct backend_job *job)
1451 ret = pkgmgr_parser_update_app_label_info_in_usr_db(job->pkgid,
1452 job->target_uid, job->args);
1453 _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));
1458 static int __post_process(int ret, int x, struct backend_job *job)
1461 __set_backend_free(x);
1462 _free_backend_job(job);
1464 g_hash_table_insert(backend_info_table, (gpointer)ret,
1470 gboolean queue_job(void *data)
1472 struct backend_job *job = NULL;
1476 /* Pop a job from queue */
1477 for (x = 0; x < num_of_backends; x++) {
1478 if (__is_backend_busy(x))
1481 job = _pop_queue(x);
1486 /* all backend messages queue are empty or busy */
1487 if (x == num_of_backends || job == NULL) {
1492 /* set current backend job */
1493 DBG("handle request type [%d]", job->req_type);
1495 #ifdef TIZEN_FEATURE_CSR
1496 if (job->req_type == REQUEST_TYPE_INSTALL ||
1497 job->req_type == REQUEST_TYPE_MOUNT_INSTALL ||
1498 job->req_type == REQUEST_TYPE_REINSTALL) {
1499 ret = __check_csr(job->pkgid);
1502 _send_fail_signal(job);
1503 _free_backend_job(job);
1509 switch (job->req_type) {
1510 case REQUEST_TYPE_INSTALL:
1511 __set_backend_busy(x);
1512 ret = __process_install(job);
1513 __post_process(ret, x, job);
1515 case REQUEST_TYPE_MOUNT_INSTALL:
1516 __set_backend_busy(x);
1517 ret = __process_mount_install(job);
1518 __post_process(ret, x, job);
1520 case REQUEST_TYPE_REINSTALL:
1521 __set_backend_busy(x);
1522 ret = __process_reinstall(job);
1523 __post_process(ret, x, job);
1525 case REQUEST_TYPE_UNINSTALL:
1526 __set_backend_busy(x);
1527 ret = __process_uninstall(job);
1528 __post_process(ret, x, job);
1530 case REQUEST_TYPE_MOVE:
1531 __set_backend_busy(x);
1532 ret = __process_move(job);
1533 __post_process(ret, x, job);
1535 case REQUEST_TYPE_ENABLE_PKG:
1536 __set_backend_busy(x);
1537 ret = __process_enable_pkg(job);
1539 _send_fail_signal(job);
1540 __post_process(ret, x, job);
1542 case REQUEST_TYPE_DISABLE_PKG:
1543 __set_backend_busy(x);
1544 ret = __process_disable_pkg(job);
1546 _send_fail_signal(job);
1547 __post_process(ret, x, job);
1549 case REQUEST_TYPE_ENABLE_APP:
1550 ret = __process_enable_app(job);
1551 _free_backend_job(job);
1553 case REQUEST_TYPE_DISABLE_APP:
1554 ret = __process_disable_app(job);
1555 _free_backend_job(job);
1557 case REQUEST_TYPE_GETSIZE:
1558 __set_backend_busy(x);
1559 ret = __process_getsize(job);
1560 __post_process(ret, x, job);
1562 case REQUEST_TYPE_GETSIZE_SYNC:
1563 __set_backend_busy(x);
1564 ret = __process_getsize_sync(job);
1566 __free_extra_info(job);
1567 __post_process(ret, x, job);
1569 case REQUEST_TYPE_CLEARDATA:
1570 __set_backend_busy(x);
1571 ret = __process_cleardata(job);
1572 __post_process(ret, x, job);
1574 case REQUEST_TYPE_CLEARCACHE:
1575 __set_backend_busy(x);
1576 ret = __process_clearcache(job);
1577 __post_process(ret, x, job);
1579 case REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID:
1580 ret = __process_enable_global_app_for_uid(job);
1581 _free_backend_job(job);
1583 case REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID:
1584 ret = __process_disable_global_app_for_uid(job);
1585 _free_backend_job(job);
1587 case REQUEST_TYPE_KILL:
1588 ret = __process_kill(job);
1589 _free_backend_job(job);
1591 case REQUEST_TYPE_CHECK:
1592 ret = __process_check(job);
1593 _free_backend_job(job);
1595 case REQUEST_TYPE_GENERATE_LICENSE_REQUEST:
1596 ret = __process_generate_license_request(job);
1597 _free_backend_job(job);
1599 case REQUEST_TYPE_REGISTER_LICENSE:
1600 ret = __process_register_license(job);
1601 _free_backend_job(job);
1603 case REQUEST_TYPE_DECRYPT_PACKAGE:
1604 ret = __process_decrypt_package(job);
1605 _free_backend_job(job);
1607 case REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN:
1608 ret = __process_update_app_splash_screen(job, 1);
1609 _free_backend_job(job);
1611 case REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN:
1612 ret = __process_update_app_splash_screen(job, 0);
1613 _free_backend_job(job);
1615 case REQUEST_TYPE_SET_RESTRICTION_MODE:
1616 ret = __process_set_restriction_mode(job);
1617 _free_backend_job(job);
1619 case REQUEST_TYPE_UNSET_RESTRICTION_MODE:
1620 ret = __process_unset_restriction_mode(job);
1621 _free_backend_job(job);
1623 case REQUEST_TYPE_GET_RESTRICTION_MODE:
1624 ret = __process_get_restriction_mode(job);
1625 _free_backend_job(job);
1627 case REQUEST_TYPE_SET_APP_LABEL:
1628 ret = __process_set_app_label(job);
1629 _free_backend_job(job);
1639 int main(int argc, char *argv[])
1643 DBG("server start");
1645 r = _init_backend_queue();
1647 DBG("Queue Initialization Failed");
1651 r = __init_backend_info();
1653 DBG("backend info init failed");
1657 r = _init_request_handler();
1659 ERR("dbus init failed");
1663 if (__register_signal_handler()) {
1664 ERR("failed to register signal handler");
1668 #if !GLIB_CHECK_VERSION(2, 35, 0)
1671 mainloop = g_main_loop_new(NULL, FALSE);
1673 ERR("g_main_loop_new failed");
1677 DBG("Main loop is created.");
1679 g_main_loop_run(mainloop);
1681 DBG("Quit main loop.");
1682 _fini_request_handler();
1683 __fini_backend_info();
1684 _fini_backend_queue();
1686 DBG("package manager server terminated.");