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>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <sys/types.h>
35 #include <bundle_internal.h>
38 #include <proc_stat.h>
39 #include <security-server.h>
43 #include <sys/signalfd.h>
46 #include <pkgmgr-info.h>
48 #include "amd_config.h"
49 #include "simple_util.h"
51 #include "app_signal.h"
53 #include "amd_request.h"
55 #include "amd_launch.h"
56 #include "amd_appinfo.h"
57 #include "amd_status.h"
58 #include "amd_app_group.h"
60 #define INHOUSE_UID 5000
62 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
63 #define METADATA_MULTI_INSTANCE "http://developer.samsung.com/tizen/metadata/multiinstance"
66 struct appinfomgr *_raf;
67 static DBusConnection *bus = NULL;
68 static sigset_t oldmask;
69 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
70 char *home_appid = NULL;
79 GHashTable *restart_tbl;
81 static int __send_result_to_client(int fd, int res);
82 static gboolean __request_handler(gpointer data);
84 // TODO: Replace with pkgmgr-info header
85 int pkgmgrinfo_updateinfo_check_update(const char* pkgid);
87 static int __send_result_data(int fd, int cmd, unsigned char *kb_data, int datalen)
92 app_pkt_t *pkt = NULL;
94 if (datalen > AUL_SOCK_MAXBUFF - 8) {
95 _E("datalen > AUL_SOCK_MAXBUFF\n");
99 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
101 _E("Malloc Failed!");
104 memset(pkt, 0, AUL_SOCK_MAXBUFF);
108 memcpy(pkt->data, kb_data, datalen);
110 if ((len = send(fd, pkt, datalen + 8, MSG_NOSIGNAL)) != datalen + 8) {
111 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
113 while (len != datalen + 8) {
114 ret = send(fd, &pkt->data[len-8], datalen + 8 - len, MSG_NOSIGNAL);
116 _E("second sendto() failed - %d %d (errno %d)", ret, datalen + 8, errno);
125 _D("sendto() len - %d %d", len, datalen + 8);
145 static int __send_result_to_client(int fd, int res)
147 _W("__send_result_to_client, pid: %d", res);
149 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
150 if (errno == EPIPE) {
151 _E("send failed due to EPIPE.\n");
154 _E("send fail to client");
160 static void __real_send(int clifd, int ret)
165 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
166 if (errno == EPIPE) {
167 _E("send failed due to EPIPE.\n");
169 _E("send fail to client");
175 static int __get_caller_pid(bundle *kb)
180 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
184 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
196 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
200 char tmp_pid[MAX_PID_STR_BUFSZ];
205 if ((pid = __get_caller_pid(kb)) < 0)
210 pgid = getpgid(cr_pid);
212 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
213 bundle_del(kb, AUL_K_CALLEE_PID);
214 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
217 _W("__forward_cmd: %d %d", cr_pid, pgid);
219 bundle_encode(kb, &kb_data, &datalen);
220 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
228 static int __app_process_by_pid(int cmd,
229 const char *pkg_name, struct ucred *cr, int clifd)
235 const char *pkgid = NULL;
236 const char *type = NULL;
237 const struct appinfo *ai = NULL;
239 if (pkg_name == NULL)
242 pid = atoi(pkg_name);
248 /* check whether app process is dead or not */
250 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
251 if (access(buf, F_OK) != 0) {
252 _E("pid(%d) is dead. cmd(%d) is canceled", pid, cmd);
253 __real_send(clifd, -ECOMM);
257 if (_status_get_app_info_status(pid) == -1) {
259 if (_status_get_pkgname_bypid(pid, buf, 512) == -1) {
260 _E("request for unknown pid. It might not be a pid of app: %d", pid);
265 appid = _status_app_get_appid_bypid(pid);
266 ai = appinfo_find(_raf, appid);
267 pkgid = appinfo_get_value(ai, AIT_PKGID);
268 type = appinfo_get_value(ai, AIT_COMPTYPE);
270 if (cmd == APP_RESUME_BY_PID)
271 aul_send_app_resume_request_signal(pid, appid, pkgid, type);
273 aul_send_app_terminate_request_signal(pid, appid, pkgid, type);
275 SECURE_LOGD("__app_process_by_pid, pid: %d, ", pid);
277 case APP_RESUME_BY_PID:
278 ret = _resume_app(pid, clifd);
280 case APP_TERM_BY_PID:
281 case APP_TERM_BY_PID_WITHOUT_RESTART:
282 ret = _term_app(pid, clifd);
284 case APP_TERM_BGAPP_BY_PID:
285 ret = _term_bgapp(pid, clifd);
287 case APP_KILL_BY_PID:
288 if ((ret = _send_to_sigkill(pid)) < 0)
289 _E("fail to killing - %d\n", pid);
290 __real_send(clifd, ret);
292 case APP_TERM_REQ_BY_PID:
293 ret = _term_req_app(pid, clifd);
295 case APP_TERM_BY_PID_ASYNC:
296 if ((ret = __app_send_raw_with_noreply(pid, cmd, (unsigned char *)&dummy, sizeof(int))) < 0) {
297 _D("terminate req packet send error");
299 __real_send(clifd, ret);
301 case APP_PAUSE_BY_PID:
302 ret = _pause_app(pid, clifd);
311 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
312 static void __set_effective_appid(bundle *kb)
314 const struct appinfo *ai;
315 const struct appinfo *effective_ai;
317 const char *effective_appid;
319 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
321 ai = appinfo_find(_raf, appid);
325 effective_appid = appinfo_get_value(ai, AIT_EFFECTIVE_APPID);
326 if (effective_appid) {
328 const char *effective_pkgid;
329 effective_ai = appinfo_find(_raf, effective_appid);
330 if (effective_ai == NULL)
333 pkgid = appinfo_get_value(ai, AIT_PKGID);
334 effective_pkgid = appinfo_get_value(effective_ai, AIT_PKGID);
335 if (pkgid && effective_pkgid && strcmp(pkgid, effective_pkgid) == 0) {
336 _D("use effective appid instead of the real appid");
337 bundle_del(kb, AUL_K_PKG_NAME);
338 bundle_add(kb, AUL_K_PKG_NAME, effective_appid);
345 static gboolean __add_history_handler(gpointer user_data)
351 char *app_path = NULL;
352 char *stat_caller = NULL;
353 char *stat_tag = NULL;
356 app_pkt_t *pkt = (app_pkt_t *)user_data;
360 kb = bundle_decode(pkt->data, pkt->len);
362 if (!app_group_is_group_app(kb)) {
364 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
365 __set_effective_appid(kb);
367 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
369 ai = (struct appinfo *)appinfo_find(_raf, appid);
370 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
372 memset((void *)&rec, 0, sizeof(rec));
374 rec.pkg_name = appid;
375 rec.app_path = app_path;
378 rec.arg = (char *)pkt->data;
381 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
383 ret = rua_add_history(&rec);
385 _D("rua add history error");
388 stat_caller = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_CALLER);
389 stat_tag = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_TAG);
391 if (stat_caller != NULL && stat_tag != NULL) {
392 SECURE_LOGD("rua_stat_caller: %s, rua_stat_tag: %s", stat_caller, stat_tag);
393 rua_stat_update(stat_caller, stat_tag);
403 static int __get_pid_cb(void *user_data, const char *group, pid_t pid)
407 _D("%s: %d : %d", *sz, pid);
408 *sz = 1; /* 1 is enough */
410 return -1; /* stop the iteration */
413 int _release_srv(const char *appid)
416 const struct appinfo *ai;
418 ai = (struct appinfo *)appinfo_find(_raf, appid);
420 _E("appid not found");
421 SECURE_LOGE("release service: '%s' not found", appid);
425 r = appinfo_get_boolean(ai, AIT_RESTART);
428 SECURE_LOGD("Auto restart set: '%s'", appid);
429 return _start_srv(ai);
435 static Eina_Bool __restart_timeout_handler(void *data)
437 struct restart_info *ri = (struct restart_info *)data;
440 SECURE_LOGD("appid (%s)", ri->appid);
442 g_hash_table_remove(restart_tbl, ri->appid);
446 return ECORE_CALLBACK_CANCEL;
449 static bool __check_restart(const char *appid)
451 struct restart_info *ri = NULL;
452 //struct appinfo *ai = NULL;
454 ri = g_hash_table_lookup(restart_tbl, appid);
457 ri = calloc(1, sizeof(*ri));
459 _E("create restart info: %s", strerror(errno));
462 memset(ri, 0, sizeof(struct restart_info));
463 ri->appid = strdup(appid);
465 g_hash_table_insert(restart_tbl, ri->appid, ri);
468 SECURE_LOGD("appid (%s)", appid);
470 ri->timer = ecore_timer_add(10, __restart_timeout_handler, ri);
473 _D("count (%d)", ri->count);
475 /*ai = appinfo_find(_raf, appid);
477 appinfo_set_value(ai, AIT_STATUS, "norestart");
479 ecore_timer_del(ri->timer);
486 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
487 static inline int __send_home_launch_signal(int pid)
489 DBusMessage *message;
494 message = dbus_message_new_signal(AUL_DBUS_PATH,
495 AUL_DBUS_SIGNAL_INTERFACE,
496 AUL_DBUS_HOMELAUNCH_SIGNAL);
498 if (dbus_message_append_args(message,
499 DBUS_TYPE_UINT32, &pid,
500 DBUS_TYPE_INVALID) == FALSE) {
501 _E("Failed to load data error");
505 if (dbus_connection_send(bus, message, NULL) == FALSE) {
506 _E("dbus send error");
510 dbus_connection_flush(bus);
511 dbus_message_unref(message);
513 _W("send a home launch signal");
519 static inline int __send_app_termination_signal(int dead_pid)
521 DBusMessage *message;
526 message = dbus_message_new_signal(AUL_DBUS_PATH,
527 AUL_DBUS_SIGNAL_INTERFACE,
528 AUL_DBUS_APPDEAD_SIGNAL);
530 if (dbus_message_append_args(message,
531 DBUS_TYPE_UINT32, &dead_pid,
532 DBUS_TYPE_INVALID) == FALSE) {
533 _E("Failed to load data error");
537 if (dbus_connection_send(bus, message, NULL) == FALSE) {
538 _E("dbus send error");
542 dbus_connection_flush(bus);
543 dbus_message_unref(message);
545 _W("send dead signal done");
550 int _send_set_process_group_signal_signal(int owner_pid, int child_pid)
552 DBusMessage *message;
557 message = dbus_message_new_signal(RESOURCED_PROC_OBJECT,
558 RESOURCED_PROC_INTERFACE,
559 RESOURCED_PROC_GROUP_SIGNAL);
561 if (dbus_message_append_args(message,
562 DBUS_TYPE_INT32, &owner_pid,
563 DBUS_TYPE_INT32, &child_pid,
564 DBUS_TYPE_INVALID) == FALSE) {
565 _E("Failed to load data error");
569 if (dbus_connection_send(bus, message, NULL) == FALSE) {
570 _E("dbus send error");
574 dbus_connection_flush(bus);
575 dbus_message_unref(message);
577 _W("send set_process_group signal done");
582 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
583 static char* __get_metadata_value(const char *appid, const char *metadata_key)
586 pkgmgrinfo_appinfo_h handle;
587 char *metadata_value = NULL;
588 char *multi_appid = NULL;
590 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
591 if (ret != PMINFO_R_OK)
594 ret = pkgmgrinfo_appinfo_get_metadata_value(handle, metadata_key, &metadata_value);
595 if (ret != PMINFO_R_OK) {
596 pkgmgrinfo_appinfo_destroy_appinfo(handle);
600 multi_appid = strdup(metadata_value);
602 pkgmgrinfo_appinfo_destroy_appinfo(handle);
607 static const char* __check_target_appid(const struct appinfo* ai, const char *appid, const char *multi_appid)
609 const char* target = NULL;
611 // Both apps are running
612 if (_status_app_is_running(appid) != -1 && _status_app_is_running(multi_appid) != -1) {
613 const char* toggle = appinfo_get_value(ai, AIT_TOGGLE_ORDER);
614 int order = atoi(toggle);
616 _D("launch a multi-instance app with toggle mode: %d", order);
619 target = multi_appid;
620 appinfo_set_value((struct appinfo *)ai,
621 AIT_TOGGLE_ORDER, "1");
626 appinfo_set_value((struct appinfo *)ai,
627 AIT_TOGGLE_ORDER, "0");
634 // Main app is running
635 if (_status_app_is_running(appid) != -1) {
636 SECURE_LOGD("Send a request to the running main appid: %s", appid);
638 // Sub app is running
639 } else if (_status_app_is_running(multi_appid) != -1) {
640 SECURE_LOGD("Send a request to the running sub appid: %s", multi_appid);
641 target = multi_appid;
643 SECURE_LOGD("Both apps are not running, launch a main app - %s", appid);
652 static void __dispatch_app_group_get_window(int clifd, const app_pkt_t *pkt)
659 b = bundle_decode(pkt->data, pkt->len);
660 bundle_get_str(b, AUL_K_PID, &buf);
663 wid = app_group_get_window(pid);
664 __real_send(clifd, wid);
667 static void __dispatch_app_group_set_window(int clifd, const app_pkt_t *pkt, int pid)
674 b = bundle_decode(pkt->data, pkt->len);
675 bundle_get_str(b, AUL_K_WID, &buf);
678 ret = app_group_set_window(pid, wid);
679 __real_send(clifd, ret);
682 static void __dispatch_app_group_get_fg_flag(int clifd, const app_pkt_t *pkt)
689 b = bundle_decode(pkt->data, pkt->len);
690 bundle_get_str(b, AUL_K_PID, &buf);
693 fg = app_group_get_fg_flag(pid);
694 __real_send(clifd, fg);
697 static void __dispatch_app_group_clear_top(int clifd, int pid)
699 app_group_clear_top(pid);
700 __real_send(clifd, 0);
703 static void __dispatch_app_group_get_leader_pid(int clifd,
704 const app_pkt_t *pkt)
711 b = bundle_decode(pkt->data, pkt->len);
712 bundle_get_str(b, AUL_K_PID, &buf);
715 lpid = app_group_get_leader_pid(pid);
716 __real_send(clifd, lpid);
719 static void __dispatch_app_group_get_leader_pids(int clifd,
720 const app_pkt_t *pkt)
724 unsigned char empty[1] = { 0 };
726 app_group_get_leader_pids(&cnt, &pids);
728 if (pids == NULL || cnt == 0) {
729 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS, empty, 0);
731 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS,
732 (unsigned char *)pids, cnt * sizeof(int));
738 static void __dispatch_app_group_get_idle_pids(int clifd,
739 const app_pkt_t *pkt)
743 unsigned char empty[1] = { 0 };
745 app_group_get_idle_pids(&cnt, &pids);
747 if (pids == NULL || cnt == 0) {
748 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS, empty, 0);
750 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS,
751 (unsigned char *)pids, cnt * sizeof(int));
757 static void __dispatch_app_group_get_group_pids(int clifd, const app_pkt_t *pkt)
764 unsigned char empty[1] = { 0 };
766 b = bundle_decode(pkt->data, pkt->len);
767 bundle_get_str(b, AUL_K_LEADER_PID, &buf);
768 leader_pid = atoi(buf);
771 app_group_get_group_pids(leader_pid, &cnt, &pids);
772 if (pids == NULL || cnt == 0) {
773 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS, empty, 0);
775 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS,
776 (unsigned char *)pids, cnt * sizeof(int));
782 static void __dispatch_app_group_lower(int clifd, int pid)
786 app_group_lower(pid, &ret);
787 __real_send(clifd, ret);
790 static void __check_host_pid(bundle *kb, struct ucred *cr)
793 SECURE_LOGD("check host pid");
797 bundle_get_str(kb, AUL_K_HOST_PID, &spid);
799 cr->pid = atoi(spid);
800 SECURE_LOGD("caller pid was changed by host pid %s", spid);
805 static gboolean __request_handler(gpointer data)
807 GPollFD *gpollfd = (GPollFD *) data;
808 int fd = gpollfd->fd;
816 char *term_pid = NULL;
819 const struct appinfo *ai;
823 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:REQ_HANDLER");
824 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
826 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
830 kb = bundle_decode(pkt->data, pkt->len);
831 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
832 __set_effective_appid(kb);
834 _D("__request_handler: %d", pkt->cmd);
841 case APP_START_ASYNC:
842 ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
843 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
844 _E("launch request has been denied by smack");
845 ret = -EILLEGALACCESS;
846 __real_send(clifd, ret);
848 __check_host_pid(kb, &cr);
849 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
851 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
852 // Check the multi-instance app
853 ai = appinfo_find(_raf, appid);
856 __real_send(clifd, -ENOAPP);
858 const char* multi = appinfo_get_value(ai, AIT_MULTI_INSTANCE);
859 if( multi && strncmp(multi, "true", strlen("true")) == 0 ) {
861 char* multi_appid =__get_metadata_value(appid, METADATA_MULTI_INSTANCE);
862 if (multi_appid != NULL)
864 SECURE_LOGD("Multi-instance main: %s, sub: %s", appid, multi_appid);
865 const char* target_appid = __check_target_appid(ai, appid, multi_appid);
867 SECURE_LOGD("launch a target appid: - %s", target_appid);
868 ret = _start_app(target_appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
870 SECURE_LOGD("No multi-instance app information, launch a main appid: - %s", appid);
871 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
878 SECURE_LOGD("launch a single-instance appid: %s", appid);
879 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
883 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
886 if(ret > 0 && bundle_get_type(kb, AUL_K_PRELAUCHING) == BUNDLE_TYPE_NONE) {
887 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
888 if (!app_group_is_group_app(kb)) {
889 item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
891 strncpy(item->appid, appid, 511);
892 __add_item_running_list(item);
896 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
897 if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
898 __send_home_launch_signal(ret);
900 g_timeout_add(1500, __add_history_handler, pkt);
905 bundle_free(kb), kb = NULL;
908 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
909 case APP_START_MULTI_INSTANCE:
910 ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
911 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
912 _E("launch request has been denied by smack");
913 ret = -EILLEGALACCESS;
914 __real_send(clifd, ret);
916 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
918 SECURE_LOGD("launch a multi-instance appid: %s", appid);
919 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
923 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
924 if (!app_group_is_group_app(kb)) {
925 item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
927 strncpy(item->appid, appid, 511);
928 __add_item_running_list(item);
932 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
933 if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
934 __send_home_launch_signal(ret);
936 g_timeout_add(1500, __add_history_handler, pkt);
941 bundle_free(kb), kb = NULL;
947 ret = __foward_cmd(pkt->cmd, kb, cr.pid);
948 //__real_send(clifd, ret);
952 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
953 ret = _status_app_is_running_v2_cached(appid);
955 _pause_app(ret, clifd);
957 _E("%s is not running", appid);
961 case APP_RESUME_BY_PID:
962 case APP_PAUSE_BY_PID:
963 case APP_TERM_REQ_BY_PID:
964 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
965 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
967 case APP_TERM_BY_PID_WITHOUT_RESTART:
968 case APP_TERM_BY_PID_ASYNC:
969 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
970 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
971 _E("terminate request has been denied by smack");
972 ret = -EILLEGALACCESS;
973 __real_send(clifd, ret);
975 term_pid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
976 appid = _status_app_get_appid_bypid(atoi(term_pid));
977 ai = appinfo_find(_raf, appid);
979 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
980 ret = __app_process_by_pid(pkt->cmd, term_pid, &cr, clifd);
987 case APP_TERM_BY_PID:
988 case APP_KILL_BY_PID:
989 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
990 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
991 _E("terminate request has been denied by smack");
992 ret = -EILLEGALACCESS;
993 __real_send(clifd, ret);
995 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
996 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
999 case APP_TERM_BGAPP_BY_PID:
1000 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminatebgapp", "x");
1001 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1002 _E("terminate request has been denied by smack");
1003 ret = -EILLEGALACCESS;
1004 __real_send(clifd, ret);
1006 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
1007 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
1010 case APP_RUNNING_INFO:
1011 _status_send_running_appinfo_v2(clifd);
1013 case APP_RUNNING_INFO_MEMORY:
1014 _status_send_running_appinfo(clifd);
1016 case APP_IS_RUNNING:
1017 appid = malloc(MAX_PACKAGE_STR_SIZE);
1018 if (appid == NULL) {
1019 _E("Failed to allocate memory");
1020 __send_result_to_client(clifd, -1);
1023 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
1024 ret = _status_app_is_running_v2_cached(appid);
1025 SECURE_LOGD("APP_IS_RUNNING : %s : %d",appid, ret);
1026 if (ret > 0 && _status_get_app_info_status(ret) == STATUS_DYING) {
1027 SECURE_LOGD("APP_IS_RUNNING: %d is dying", ret);
1030 __send_result_to_client(clifd, ret);
1033 case APP_GET_APPID_BYPID:
1034 memcpy(&pid, pkt->data, sizeof(int));
1035 ret = _status_get_appid_bypid(clifd, pid);
1036 _D("APP_GET_APPID_BYPID : %d : %d", pid, ret);
1038 case APP_GET_PKGID_BYPID:
1039 memcpy(&pid, pkt->data, sizeof(int));
1040 ret = _status_get_pkgid_bypid(clifd, pid);
1041 _D("APP_GET_PKGID_BYPID : %d : %d", pid, ret);
1043 case APP_KEY_RESERVE:
1044 ret = _register_key_event(cr.pid);
1045 __send_result_to_client(clifd, ret);
1047 case APP_KEY_RELEASE:
1048 ret = _unregister_key_event(cr.pid);
1049 __send_result_to_client(clifd, ret);
1051 case APP_STATUS_UPDATE:
1052 status = (int *)pkt->data;
1053 _W("app status : %d", *status);
1054 if(*status == STATUS_NORESTART) {
1055 appid = _status_app_get_appid_bypid(cr.pid);
1056 ai = appinfo_find(_raf, appid);
1057 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
1059 ret = _status_update_app_info_list(cr.pid, *status);
1061 //__send_result_to_client(clifd, ret);
1064 case APP_GET_STATUS:
1065 memcpy(&pid, pkt->data, sizeof(int));
1066 ret = _status_get_app_info_status(pid);
1067 __send_result_to_client(clifd, ret);
1070 case APP_RUNNING_LIST_UPDATE:
1071 /*appid = (char *)bundle_get_val(kb, AUL_K_APPID);
1072 app_path = (char *)bundle_get_val(kb, AUL_K_EXEC);
1073 tmp_pid = (char *)bundle_get_val(kb, AUL_K_PID);
1074 pid = atoi(tmp_pid);
1075 ret = _status_add_app_info_list(appid, app_path, pid);*/
1077 __send_result_to_client(clifd, ret);
1080 case APP_GROUP_GET_WINDOW:
1081 __dispatch_app_group_get_window(clifd, pkt);
1084 case APP_GROUP_SET_WINDOW:
1085 __dispatch_app_group_set_window(clifd, pkt, cr.pid);
1088 case APP_GROUP_GET_FG:
1089 __dispatch_app_group_get_fg_flag(clifd, pkt);
1092 case APP_GROUP_GET_LEADER_PIDS:
1093 __dispatch_app_group_get_leader_pids(clifd, pkt);
1096 case APP_GROUP_GET_GROUP_PIDS:
1097 __dispatch_app_group_get_group_pids(clifd, pkt);
1100 case APP_GROUP_CLEAR_TOP:
1101 __dispatch_app_group_clear_top(clifd, cr.pid);
1104 case APP_GROUP_GET_LEADER_PID:
1105 __dispatch_app_group_get_leader_pid(clifd, pkt);
1108 case APP_GROUP_LOWER:
1109 __dispatch_app_group_lower(clifd, cr.pid);
1112 case APP_GROUP_GET_IDLE_PIDS:
1113 __dispatch_app_group_get_idle_pids(clifd, pkt);
1116 case APP_GET_CMDLINE:
1117 memcpy(&pid, pkt->data, sizeof(int));
1118 ret = _status_get_cmdline(clifd, pid);
1119 _D("APP_GET_CMDLINE : %d : %d", pid, ret);
1123 appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1124 if (appid == NULL) {
1125 _E("failed to allocate appid");
1126 __send_result_to_client(clifd, -1);
1129 strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1130 ret = _status_app_is_running_v2_cached(appid);
1131 SECURE_LOGD("APP_GET_PID: %s : %d", appid, ret);
1132 __send_result_to_client(clifd, ret);
1136 case APP_GET_PID_CACHE:
1137 appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1138 if (appid == NULL) {
1139 _E("failed to allocate appid");
1140 __send_result_to_client(clifd, -1);
1143 strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1144 ret = _status_app_is_running_from_cache(appid);
1145 SECURE_LOGD("APP_GET_PID_CACHE: %s : %d", appid, ret);
1146 __send_result_to_client(clifd, ret);
1150 case APP_GET_LAST_CALLER_PID:
1151 memcpy(&pid, pkt->data, sizeof(int));
1152 ret = _status_get_app_info_last_caller_pid(pid);
1153 SECURE_LOGD("APP_GET_LAST_CALLER_PID: %d : %d", pid, ret);
1154 __send_result_to_client(clifd, ret);
1156 case APP_SET_PROCESS_GROUP:
1157 owner_pid = atoi(bundle_get_val(kb, AUL_K_OWNER_PID));
1158 child_pid = atoi(bundle_get_val(kb, AUL_K_CHILD_PID));
1159 ret = _send_set_process_group_signal_signal(owner_pid, child_pid);
1161 bundle_free(kb), kb = NULL;
1162 __send_result_to_client(clifd, ret);
1164 case APP_GET_GROUP_INFO:
1165 _status_send_group_info(clifd);
1168 _E("no support packet");
1176 bundle_free(kb), kb = NULL;
1178 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1182 static gboolean __au_glib_check(GSource *src)
1187 fd_list = src->poll_fds;
1189 tmp = (GPollFD *) fd_list->data;
1190 if ((tmp->revents & (POLLIN | POLLPRI)))
1192 fd_list = fd_list->next;
1198 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
1205 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
1210 static GSourceFuncs funcs = {
1211 .prepare = __au_glib_prepare,
1212 .check = __au_glib_check,
1213 .dispatch = __au_glib_dispatch,
1217 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1218 static void __home_appid_vconf_cb(keynode_t *key, void *data)
1222 tmpstr = vconf_keynode_get_str(key);
1223 if (tmpstr == NULL) {
1230 home_appid = strdup(tmpstr);
1234 static int __signal_get_sigchld_fd(sigset_t mask)
1238 sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
1241 _E("failed to create signalfd for SIGCHLD");
1248 static void __release_app(int pid)
1250 const char *pkg_status;
1251 const char *appid = NULL;
1252 const struct appinfo *ai = NULL;
1254 appid = _status_app_get_appid_bypid(pid);
1255 ai = appinfo_find(_raf, appid);
1256 pkg_status = appinfo_get_value(ai, AIT_STATUS);
1257 SECURE_LOGI("appid: %s", appid);
1258 if (ai && pkg_status && strncmp(pkg_status, "blocking", 8) == 0) {
1259 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "restart");
1260 } else if (ai && pkg_status && strncmp(pkg_status, "norestart", 9) == 0) {
1261 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "installed");
1263 if (appid != NULL && __check_restart(appid)) {
1264 _release_srv(appid);
1268 __send_app_termination_signal(pid);
1271 static gboolean __sigchld_handler(gpointer data)
1274 struct signalfd_siginfo si;
1277 int nr = read(fd, &si, sizeof(struct signalfd_siginfo));
1279 if (nr != sizeof(struct signalfd_siginfo))
1283 pid_t pid = waitpid(-1, &status, WNOHANG);
1287 _D("Sig child %d", pid);
1296 int _signal_block_sigchld(void)
1301 sigaddset(&mask, SIGCHLD);
1303 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1) {
1304 _E("failed to sigprocmask");
1308 return __signal_get_sigchld_fd(mask);
1311 int _signal_unblock_sigchld(void)
1313 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
1314 _E("SIG_SETMASK error");
1318 _D("SIGCHLD unblocked");
1322 int _request_init(struct amdmgr *amd, int fd_sig)
1327 GPollFD *gpollfd_sig;
1332 fd = __create_server_sock(AUL_UTIL_PID);
1334 _E("fail to create server sock");
1337 src = g_source_new(&funcs, sizeof(GSource));
1339 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
1340 if (gpollfd == NULL) {
1341 g_source_unref(src);
1346 gpollfd->events = POLLIN;
1349 g_source_add_poll(src, gpollfd);
1350 g_source_set_callback(src, (GSourceFunc) __request_handler,
1351 (gpointer) gpollfd, NULL);
1352 g_source_set_priority(src, G_PRIORITY_DEFAULT);
1354 r = g_source_attach(src, NULL);
1356 _E("fail to attach the source : %d", r);
1360 src_sig = g_source_new(&funcs, sizeof(GSource));
1362 gpollfd_sig = (GPollFD *) g_malloc(sizeof(GPollFD));
1363 if (gpollfd_sig == NULL) {
1364 g_source_unref(src_sig);
1369 gpollfd_sig->events = G_IO_IN;
1370 gpollfd_sig->fd = fd_sig;
1372 g_source_add_poll(src_sig, gpollfd_sig);
1373 g_source_set_callback(src_sig, (GSourceFunc) __sigchld_handler,
1374 (gpointer) fd_sig, NULL);
1375 g_source_set_priority(src_sig, G_PRIORITY_DEFAULT);
1376 r = g_source_attach(src_sig, NULL);
1378 _E("fail to attach the source : %d", r);
1385 r = rua_clear_history();
1387 _D("rua_clear_history : %d", r);
1389 dbus_error_init(&error);
1390 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1392 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1393 home_appid = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
1394 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, __home_appid_vconf_cb, NULL) != 0) {
1395 _E("Unable to register callback for VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME\n");
1399 restart_tbl = g_hash_table_new(g_str_hash, g_str_equal);