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)
150 _W("__send_result_to_client, pid: %d", res);
152 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
153 if (errno == EPIPE) {
154 _E("send failed due to EPIPE.\n");
157 _E("send fail to client");
163 static void __real_send(int clifd, int ret)
168 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
169 if (errno == EPIPE) {
170 _E("send failed due to EPIPE.\n");
172 _E("send fail to client");
178 static int __get_caller_pid(bundle *kb)
183 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
187 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
199 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
203 char tmp_pid[MAX_PID_STR_BUFSZ];
208 if ((pid = __get_caller_pid(kb)) < 0)
213 pgid = getpgid(cr_pid);
215 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
216 bundle_del(kb, AUL_K_CALLEE_PID);
217 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
220 _W("__forward_cmd: %d %d", cr_pid, pgid);
222 bundle_encode(kb, &kb_data, &datalen);
223 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
231 static int __app_process_by_pid(int cmd,
232 const char *pkg_name, struct ucred *cr, int clifd)
238 const char *pkgid = NULL;
239 const char *type = NULL;
240 const struct appinfo *ai = NULL;
242 if (pkg_name == NULL)
245 pid = atoi(pkg_name);
251 /* check whether app process is dead or not */
253 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
254 if (access(buf, F_OK) != 0) {
255 _E("pid(%d) is dead. cmd(%d) is canceled", pid, cmd);
256 __real_send(clifd, -ECOMM);
260 if (_status_get_app_info_status(pid) == -1) {
262 if (_status_get_pkgname_bypid(pid, buf, 512) == -1) {
263 _E("request for unknown pid. It might not be a pid of app: %d", pid);
264 __real_send(clifd, -1);
269 appid = _status_app_get_appid_bypid(pid);
270 ai = appinfo_find(_raf, appid);
271 pkgid = appinfo_get_value(ai, AIT_PKGID);
272 type = appinfo_get_value(ai, AIT_COMPTYPE);
274 if (cmd == APP_RESUME_BY_PID)
275 aul_send_app_resume_request_signal(pid, appid, pkgid, type);
277 aul_send_app_terminate_request_signal(pid, appid, pkgid, type);
279 SECURE_LOGD("__app_process_by_pid, pid: %d, ", pid);
281 case APP_RESUME_BY_PID:
282 ret = _resume_app(pid, clifd);
284 case APP_TERM_BY_PID:
285 case APP_TERM_BY_PID_WITHOUT_RESTART:
286 ret = _term_app(pid, clifd);
288 case APP_TERM_BGAPP_BY_PID:
289 ret = _term_bgapp(pid, clifd);
291 case APP_KILL_BY_PID:
292 if ((ret = _send_to_sigkill(pid)) < 0)
293 _E("fail to killing - %d\n", pid);
294 __real_send(clifd, ret);
296 case APP_TERM_REQ_BY_PID:
297 ret = _term_req_app(pid, clifd);
299 case APP_TERM_BY_PID_ASYNC:
300 if ((ret = __app_send_raw_with_noreply(pid, cmd, (unsigned char *)&dummy, sizeof(int))) < 0) {
301 _D("terminate req packet send error");
303 __real_send(clifd, ret);
305 case APP_PAUSE_BY_PID:
306 ret = _pause_app(pid, clifd);
315 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
316 static void __set_effective_appid(bundle *kb)
318 const struct appinfo *ai;
319 const struct appinfo *effective_ai;
321 const char *effective_appid;
323 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
325 ai = appinfo_find(_raf, appid);
329 effective_appid = appinfo_get_value(ai, AIT_EFFECTIVE_APPID);
330 if (effective_appid) {
332 const char *effective_pkgid;
333 effective_ai = appinfo_find(_raf, effective_appid);
334 if (effective_ai == NULL)
337 pkgid = appinfo_get_value(ai, AIT_PKGID);
338 effective_pkgid = appinfo_get_value(effective_ai, AIT_PKGID);
339 if (pkgid && effective_pkgid && strcmp(pkgid, effective_pkgid) == 0) {
340 _D("use effective appid instead of the real appid");
341 bundle_del(kb, AUL_K_PKG_NAME);
342 bundle_add(kb, AUL_K_PKG_NAME, effective_appid);
349 static gboolean __add_history_handler(gpointer user_data)
355 char *app_path = NULL;
356 char *stat_caller = NULL;
357 char *stat_tag = NULL;
360 app_pkt_t *pkt = (app_pkt_t *)user_data;
364 kb = bundle_decode(pkt->data, pkt->len);
366 if (!app_group_is_group_app(kb)) {
368 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
369 __set_effective_appid(kb);
371 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
373 ai = (struct appinfo *)appinfo_find(_raf, appid);
374 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
376 memset((void *)&rec, 0, sizeof(rec));
378 rec.pkg_name = appid;
379 rec.app_path = app_path;
382 rec.arg = (char *)pkt->data;
385 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
387 ret = rua_add_history(&rec);
389 _D("rua add history error");
392 stat_caller = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_CALLER);
393 stat_tag = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_TAG);
395 if (stat_caller != NULL && stat_tag != NULL) {
396 SECURE_LOGD("rua_stat_caller: %s, rua_stat_tag: %s", stat_caller, stat_tag);
397 rua_stat_update(stat_caller, stat_tag);
407 static int __get_pid_cb(void *user_data, const char *group, pid_t pid)
411 _D("%s: %d : %d", *sz, pid);
412 *sz = 1; /* 1 is enough */
414 return -1; /* stop the iteration */
417 int _release_srv(const char *appid)
420 const struct appinfo *ai;
422 ai = (struct appinfo *)appinfo_find(_raf, appid);
424 _E("appid not found");
425 SECURE_LOGE("release service: '%s' not found", appid);
429 r = appinfo_get_boolean(ai, AIT_RESTART);
432 SECURE_LOGD("Auto restart set: '%s'", appid);
433 return _start_srv(ai);
439 static Eina_Bool __restart_timeout_handler(void *data)
441 struct restart_info *ri = (struct restart_info *)data;
444 SECURE_LOGD("appid (%s)", ri->appid);
446 g_hash_table_remove(restart_tbl, ri->appid);
450 return ECORE_CALLBACK_CANCEL;
453 static bool __check_restart(const char *appid)
455 struct restart_info *ri = NULL;
456 //struct appinfo *ai = NULL;
458 ri = g_hash_table_lookup(restart_tbl, appid);
461 ri = calloc(1, sizeof(*ri));
463 _E("create restart info: %s", strerror(errno));
466 memset(ri, 0, sizeof(struct restart_info));
467 ri->appid = strdup(appid);
469 g_hash_table_insert(restart_tbl, ri->appid, ri);
472 SECURE_LOGD("appid (%s)", appid);
474 ri->timer = ecore_timer_add(10, __restart_timeout_handler, ri);
477 _D("count (%d)", ri->count);
479 /*ai = appinfo_find(_raf, appid);
481 appinfo_set_value(ai, AIT_STATUS, "norestart");
483 ecore_timer_del(ri->timer);
490 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
491 static inline int __send_home_launch_signal(int pid)
493 DBusMessage *message;
498 message = dbus_message_new_signal(AUL_DBUS_PATH,
499 AUL_DBUS_SIGNAL_INTERFACE,
500 AUL_DBUS_HOMELAUNCH_SIGNAL);
502 if (dbus_message_append_args(message,
503 DBUS_TYPE_UINT32, &pid,
504 DBUS_TYPE_INVALID) == FALSE) {
505 _E("Failed to load data error");
509 if (dbus_connection_send(bus, message, NULL) == FALSE) {
510 _E("dbus send error");
514 dbus_connection_flush(bus);
515 dbus_message_unref(message);
517 _W("send a home launch signal");
523 static inline int __send_app_termination_signal(int dead_pid)
525 DBusMessage *message;
530 message = dbus_message_new_signal(AUL_DBUS_PATH,
531 AUL_DBUS_SIGNAL_INTERFACE,
532 AUL_DBUS_APPDEAD_SIGNAL);
534 if (dbus_message_append_args(message,
535 DBUS_TYPE_UINT32, &dead_pid,
536 DBUS_TYPE_INVALID) == FALSE) {
537 _E("Failed to load data error");
541 if (dbus_connection_send(bus, message, NULL) == FALSE) {
542 _E("dbus send error");
546 dbus_connection_flush(bus);
547 dbus_message_unref(message);
549 _W("send dead signal done");
554 int _send_set_process_group_signal_signal(int owner_pid, int child_pid)
556 DBusMessage *message;
561 message = dbus_message_new_signal(RESOURCED_PROC_OBJECT,
562 RESOURCED_PROC_INTERFACE,
563 RESOURCED_PROC_GROUP_SIGNAL);
565 if (dbus_message_append_args(message,
566 DBUS_TYPE_INT32, &owner_pid,
567 DBUS_TYPE_INT32, &child_pid,
568 DBUS_TYPE_INVALID) == FALSE) {
569 _E("Failed to load data error");
573 if (dbus_connection_send(bus, message, NULL) == FALSE) {
574 _E("dbus send error");
578 dbus_connection_flush(bus);
579 dbus_message_unref(message);
581 _W("send set_process_group signal done");
586 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
587 static char* __get_metadata_value(const char *appid, const char *metadata_key)
590 pkgmgrinfo_appinfo_h handle;
591 char *metadata_value = NULL;
592 char *multi_appid = NULL;
594 ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
595 if (ret != PMINFO_R_OK)
598 ret = pkgmgrinfo_appinfo_get_metadata_value(handle, metadata_key, &metadata_value);
599 if (ret != PMINFO_R_OK) {
600 pkgmgrinfo_appinfo_destroy_appinfo(handle);
604 multi_appid = strdup(metadata_value);
606 pkgmgrinfo_appinfo_destroy_appinfo(handle);
611 static const char* __check_target_appid(const struct appinfo* ai, const char *appid, const char *multi_appid)
613 const char* target = NULL;
615 // Both apps are running
616 if (_status_app_is_running(appid) != -1 && _status_app_is_running(multi_appid) != -1) {
617 const char* toggle = appinfo_get_value(ai, AIT_TOGGLE_ORDER);
618 int order = atoi(toggle);
620 _D("launch a multi-instance app with toggle mode: %d", order);
623 target = multi_appid;
624 appinfo_set_value((struct appinfo *)ai,
625 AIT_TOGGLE_ORDER, "1");
630 appinfo_set_value((struct appinfo *)ai,
631 AIT_TOGGLE_ORDER, "0");
638 // Main app is running
639 if (_status_app_is_running(appid) != -1) {
640 SECURE_LOGD("Send a request to the running main appid: %s", appid);
642 // Sub app is running
643 } else if (_status_app_is_running(multi_appid) != -1) {
644 SECURE_LOGD("Send a request to the running sub appid: %s", multi_appid);
645 target = multi_appid;
647 SECURE_LOGD("Both apps are not running, launch a main app - %s", appid);
656 static void __dispatch_app_group_get_window(int clifd, const app_pkt_t *pkt)
663 b = bundle_decode(pkt->data, pkt->len);
664 bundle_get_str(b, AUL_K_PID, &buf);
667 wid = app_group_get_window(pid);
668 __real_send(clifd, wid);
671 static void __dispatch_app_group_set_window(int clifd, const app_pkt_t *pkt, int pid)
678 b = bundle_decode(pkt->data, pkt->len);
679 bundle_get_str(b, AUL_K_WID, &buf);
682 ret = app_group_set_window(pid, wid);
683 __real_send(clifd, ret);
686 static void __dispatch_app_group_get_fg_flag(int clifd, const app_pkt_t *pkt)
693 b = bundle_decode(pkt->data, pkt->len);
694 bundle_get_str(b, AUL_K_PID, &buf);
697 fg = app_group_get_fg_flag(pid);
698 __real_send(clifd, fg);
701 static void __dispatch_app_group_clear_top(int clifd, int pid)
703 app_group_clear_top(pid);
704 __real_send(clifd, 0);
707 static void __dispatch_app_group_get_leader_pid(int clifd,
708 const app_pkt_t *pkt)
715 b = bundle_decode(pkt->data, pkt->len);
716 bundle_get_str(b, AUL_K_PID, &buf);
719 lpid = app_group_get_leader_pid(pid);
720 __real_send(clifd, lpid);
723 static void __dispatch_app_group_get_leader_pids(int clifd,
724 const app_pkt_t *pkt)
728 unsigned char empty[1] = { 0 };
730 app_group_get_leader_pids(&cnt, &pids);
732 if (pids == NULL || cnt == 0) {
733 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS, empty, 0);
735 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS,
736 (unsigned char *)pids, cnt * sizeof(int));
742 static void __dispatch_app_group_get_idle_pids(int clifd,
743 const app_pkt_t *pkt)
747 unsigned char empty[1] = { 0 };
749 app_group_get_idle_pids(&cnt, &pids);
751 if (pids == NULL || cnt == 0) {
752 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS, empty, 0);
754 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS,
755 (unsigned char *)pids, cnt * sizeof(int));
761 static void __dispatch_app_group_get_group_pids(int clifd, const app_pkt_t *pkt)
768 unsigned char empty[1] = { 0 };
770 b = bundle_decode(pkt->data, pkt->len);
771 bundle_get_str(b, AUL_K_LEADER_PID, &buf);
772 leader_pid = atoi(buf);
775 app_group_get_group_pids(leader_pid, &cnt, &pids);
776 if (pids == NULL || cnt == 0) {
777 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS, empty, 0);
779 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS,
780 (unsigned char *)pids, cnt * sizeof(int));
786 static void __dispatch_app_group_lower(int clifd, int pid)
790 app_group_lower(pid, &ret);
791 __real_send(clifd, ret);
794 static void __check_host_pid(bundle *kb, struct ucred *cr)
797 SECURE_LOGD("check host pid");
801 bundle_get_str(kb, AUL_K_HOST_PID, &spid);
803 cr->pid = atoi(spid);
804 SECURE_LOGD("caller pid was changed by host pid %s", spid);
809 static gboolean __request_handler(gpointer data)
811 GPollFD *gpollfd = (GPollFD *) data;
812 int fd = gpollfd->fd;
820 char *term_pid = NULL;
823 const struct appinfo *ai;
827 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:REQ_HANDLER");
828 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
830 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
834 kb = bundle_decode(pkt->data, pkt->len);
835 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
836 __set_effective_appid(kb);
838 _D("__request_handler: %d", pkt->cmd);
845 case APP_START_ASYNC:
846 ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
847 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
848 _E("launch request has been denied by smack");
849 ret = -EILLEGALACCESS;
850 __real_send(clifd, ret);
852 __check_host_pid(kb, &cr);
853 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
855 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
856 // Check the multi-instance app
857 ai = appinfo_find(_raf, appid);
860 __real_send(clifd, -ENOAPP);
862 const char* multi = appinfo_get_value(ai, AIT_MULTI_INSTANCE);
863 if( multi && strncmp(multi, "true", strlen("true")) == 0 ) {
865 char* multi_appid =__get_metadata_value(appid, METADATA_MULTI_INSTANCE);
866 if (multi_appid != NULL)
868 SECURE_LOGD("Multi-instance main: %s, sub: %s", appid, multi_appid);
869 const char* target_appid = __check_target_appid(ai, appid, multi_appid);
871 SECURE_LOGD("launch a target appid: - %s", target_appid);
872 ret = _start_app(target_appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
874 SECURE_LOGD("No multi-instance app information, launch a main appid: - %s", appid);
875 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
882 SECURE_LOGD("launch a single-instance appid: %s", appid);
883 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
887 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
890 if (ret > 0 && bundle_get_type(kb, AUL_K_PRELAUCHING) == BUNDLE_TYPE_NONE) {
891 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
892 if (!app_group_is_group_app(kb)) {
893 item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
895 strncpy(item->appid, appid, 511);
896 __add_item_running_list(item);
900 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
901 if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
902 __send_home_launch_signal(ret);
904 g_timeout_add(1500, __add_history_handler, pkt);
909 bundle_free(kb), kb = NULL;
912 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
913 case APP_START_MULTI_INSTANCE:
914 ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
915 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
916 _E("launch request has been denied by smack");
917 ret = -EILLEGALACCESS;
918 __real_send(clifd, ret);
920 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
922 SECURE_LOGD("launch a multi-instance appid: %s", appid);
923 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
927 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
928 if (!app_group_is_group_app(kb)) {
929 item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
931 strncpy(item->appid, appid, 511);
932 __add_item_running_list(item);
936 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
937 if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
938 __send_home_launch_signal(ret);
940 g_timeout_add(1500, __add_history_handler, pkt);
945 bundle_free(kb), kb = NULL;
951 ret = __foward_cmd(pkt->cmd, kb, cr.pid);
955 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
956 ret = _status_app_is_running_v2_cached(appid);
958 _pause_app(ret, clifd);
960 _E("%s is not running", appid);
964 case APP_RESUME_BY_PID:
965 case APP_PAUSE_BY_PID:
966 case APP_TERM_REQ_BY_PID:
967 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
968 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
970 case APP_TERM_BY_PID_WITHOUT_RESTART:
971 case APP_TERM_BY_PID_ASYNC:
972 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
973 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
974 _E("terminate request has been denied by smack");
975 ret = -EILLEGALACCESS;
976 __real_send(clifd, ret);
978 term_pid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
979 appid = _status_app_get_appid_bypid(atoi(term_pid));
980 ai = appinfo_find(_raf, appid);
982 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
983 ret = __app_process_by_pid(pkt->cmd, term_pid, &cr, clifd);
986 __send_result_to_client(clifd, ret);
990 case APP_TERM_BY_PID:
991 case APP_KILL_BY_PID:
992 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
993 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
994 _E("terminate request has been denied by smack");
995 ret = -EILLEGALACCESS;
996 __real_send(clifd, ret);
998 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
999 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
1002 case APP_TERM_BGAPP_BY_PID:
1003 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminatebgapp", "x");
1004 if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1005 _E("terminate request has been denied by smack");
1006 ret = -EILLEGALACCESS;
1007 __real_send(clifd, ret);
1009 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
1010 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
1013 case APP_RUNNING_INFO:
1014 _status_send_running_appinfo_v2(clifd);
1016 case APP_RUNNING_INFO_MEMORY:
1017 _status_send_running_appinfo(clifd);
1019 case APP_IS_RUNNING:
1020 appid = malloc(MAX_PACKAGE_STR_SIZE);
1021 if (appid == NULL) {
1022 _E("Failed to allocate memory");
1023 __send_result_to_client(clifd, -1);
1026 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
1027 ret = _status_app_is_running_v2_cached(appid);
1028 SECURE_LOGD("APP_IS_RUNNING : %s : %d",appid, ret);
1029 if (ret > 0 && _status_get_app_info_status(ret) == STATUS_DYING) {
1030 SECURE_LOGD("APP_IS_RUNNING: %d is dying", ret);
1033 __send_result_to_client(clifd, ret);
1036 case APP_GET_APPID_BYPID:
1037 memcpy(&pid, pkt->data, sizeof(int));
1038 ret = _status_get_appid_bypid(clifd, pid);
1039 _D("APP_GET_APPID_BYPID : %d : %d", pid, ret);
1041 case APP_GET_PKGID_BYPID:
1042 memcpy(&pid, pkt->data, sizeof(int));
1043 ret = _status_get_pkgid_bypid(clifd, pid);
1044 _D("APP_GET_PKGID_BYPID : %d : %d", pid, ret);
1046 case APP_KEY_RESERVE:
1047 ret = _register_key_event(cr.pid);
1048 __send_result_to_client(clifd, ret);
1050 case APP_KEY_RELEASE:
1051 ret = _unregister_key_event(cr.pid);
1052 __send_result_to_client(clifd, ret);
1054 case APP_STATUS_UPDATE:
1055 status = (int *)pkt->data;
1056 _W("app status : %d", *status);
1057 if(*status == STATUS_NORESTART) {
1058 appid = _status_app_get_appid_bypid(cr.pid);
1059 ai = appinfo_find(_raf, appid);
1060 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
1062 ret = _status_update_app_info_list(cr.pid, *status, FALSE);
1066 case APP_GET_STATUS:
1067 memcpy(&pid, pkt->data, sizeof(int));
1068 ret = _status_get_app_info_status(pid);
1069 __send_result_to_client(clifd, ret);
1072 case APP_RUNNING_LIST_UPDATE:
1073 /*appid = (char *)bundle_get_val(kb, AUL_K_APPID);
1074 app_path = (char *)bundle_get_val(kb, AUL_K_EXEC);
1075 tmp_pid = (char *)bundle_get_val(kb, AUL_K_PID);
1076 pid = atoi(tmp_pid);
1077 ret = _status_add_app_info_list(appid, app_path, pid);*/
1079 __send_result_to_client(clifd, ret);
1082 case APP_GROUP_GET_WINDOW:
1083 __dispatch_app_group_get_window(clifd, pkt);
1086 case APP_GROUP_SET_WINDOW:
1087 __dispatch_app_group_set_window(clifd, pkt, cr.pid);
1090 case APP_GROUP_GET_FG:
1091 __dispatch_app_group_get_fg_flag(clifd, pkt);
1094 case APP_GROUP_GET_LEADER_PIDS:
1095 __dispatch_app_group_get_leader_pids(clifd, pkt);
1098 case APP_GROUP_GET_GROUP_PIDS:
1099 __dispatch_app_group_get_group_pids(clifd, pkt);
1102 case APP_GROUP_CLEAR_TOP:
1103 __dispatch_app_group_clear_top(clifd, cr.pid);
1106 case APP_GROUP_GET_LEADER_PID:
1107 __dispatch_app_group_get_leader_pid(clifd, pkt);
1110 case APP_GROUP_LOWER:
1111 __dispatch_app_group_lower(clifd, cr.pid);
1114 case APP_GROUP_GET_IDLE_PIDS:
1115 __dispatch_app_group_get_idle_pids(clifd, pkt);
1118 case APP_GET_CMDLINE:
1119 memcpy(&pid, pkt->data, sizeof(int));
1120 ret = _status_get_cmdline(clifd, pid);
1121 _D("APP_GET_CMDLINE : %d : %d", pid, ret);
1125 appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1126 if (appid == NULL) {
1127 _E("failed to allocate appid");
1128 __send_result_to_client(clifd, -1);
1131 strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1132 ret = _status_app_is_running_v2_cached(appid);
1133 SECURE_LOGD("APP_GET_PID: %s : %d", appid, ret);
1134 __send_result_to_client(clifd, ret);
1138 case APP_GET_PID_CACHE:
1139 appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1140 if (appid == NULL) {
1141 _E("failed to allocate appid");
1142 __send_result_to_client(clifd, -1);
1145 strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1146 ret = _status_app_is_running_from_cache(appid);
1147 SECURE_LOGD("APP_GET_PID_CACHE: %s : %d", appid, ret);
1148 __send_result_to_client(clifd, ret);
1152 case APP_GET_LAST_CALLER_PID:
1153 memcpy(&pid, pkt->data, sizeof(int));
1154 ret = _status_get_app_info_last_caller_pid(pid);
1155 SECURE_LOGD("APP_GET_LAST_CALLER_PID: %d : %d", pid, ret);
1156 __send_result_to_client(clifd, ret);
1158 case APP_SET_PROCESS_GROUP:
1159 owner_pid = atoi(bundle_get_val(kb, AUL_K_OWNER_PID));
1160 child_pid = atoi(bundle_get_val(kb, AUL_K_CHILD_PID));
1161 ret = _send_set_process_group_signal_signal(owner_pid, child_pid);
1163 bundle_free(kb), kb = NULL;
1164 __send_result_to_client(clifd, ret);
1166 case APP_GET_GROUP_INFO:
1167 _status_send_group_info(clifd);
1170 _E("no support packet");
1178 bundle_free(kb), kb = NULL;
1180 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1184 static gboolean __au_glib_check(GSource *src)
1189 fd_list = src->poll_fds;
1191 tmp = (GPollFD *) fd_list->data;
1192 if ((tmp->revents & (POLLIN | POLLPRI)))
1194 fd_list = fd_list->next;
1200 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
1207 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
1212 static GSourceFuncs funcs = {
1213 .prepare = __au_glib_prepare,
1214 .check = __au_glib_check,
1215 .dispatch = __au_glib_dispatch,
1219 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1220 static void __home_appid_vconf_cb(keynode_t *key, void *data)
1224 tmpstr = vconf_keynode_get_str(key);
1225 if (tmpstr == NULL) {
1232 home_appid = strdup(tmpstr);
1236 static int __signal_get_sigchld_fd(sigset_t mask)
1240 sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
1243 _E("failed to create signalfd for SIGCHLD");
1250 static void __release_app(int pid)
1252 const char *pkg_status;
1253 const char *appid = NULL;
1254 const struct appinfo *ai = NULL;
1256 appid = _status_app_get_appid_bypid(pid);
1257 ai = appinfo_find(_raf, appid);
1258 pkg_status = appinfo_get_value(ai, AIT_STATUS);
1259 SECURE_LOGI("appid: %s", appid);
1260 if (ai && pkg_status && strncmp(pkg_status, "blocking", 8) == 0) {
1261 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "restart");
1262 } else if (ai && pkg_status && strncmp(pkg_status, "norestart", 9) == 0) {
1263 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "installed");
1265 if (appid != NULL && __check_restart(appid)) {
1266 _release_srv(appid);
1270 __send_app_termination_signal(pid);
1273 static gboolean __sigchld_handler(gpointer data)
1276 struct signalfd_siginfo si;
1279 int nr = read(fd, &si, sizeof(struct signalfd_siginfo));
1281 if (nr != sizeof(struct signalfd_siginfo))
1285 pid_t pid = waitpid(-1, &status, WNOHANG);
1289 _D("Sig child %d", pid);
1298 int _signal_block_sigchld(void)
1303 sigaddset(&mask, SIGCHLD);
1305 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1) {
1306 _E("failed to sigprocmask");
1310 return __signal_get_sigchld_fd(mask);
1313 int _signal_unblock_sigchld(void)
1315 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
1316 _E("SIG_SETMASK error");
1320 _D("SIGCHLD unblocked");
1324 int _request_init(struct amdmgr *amd, int fd_sig)
1329 GPollFD *gpollfd_sig;
1334 fd = __create_server_sock(AUL_UTIL_PID);
1336 _E("fail to create server sock");
1339 src = g_source_new(&funcs, sizeof(GSource));
1341 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
1342 if (gpollfd == NULL) {
1343 g_source_unref(src);
1348 gpollfd->events = POLLIN;
1351 g_source_add_poll(src, gpollfd);
1352 g_source_set_callback(src, (GSourceFunc) __request_handler,
1353 (gpointer) gpollfd, NULL);
1354 g_source_set_priority(src, G_PRIORITY_DEFAULT);
1356 r = g_source_attach(src, NULL);
1358 _E("fail to attach the source : %d", r);
1362 src_sig = g_source_new(&funcs, sizeof(GSource));
1364 gpollfd_sig = (GPollFD *) g_malloc(sizeof(GPollFD));
1365 if (gpollfd_sig == NULL) {
1366 g_source_unref(src_sig);
1371 gpollfd_sig->events = G_IO_IN;
1372 gpollfd_sig->fd = fd_sig;
1374 g_source_add_poll(src_sig, gpollfd_sig);
1375 g_source_set_callback(src_sig, (GSourceFunc) __sigchld_handler,
1376 (gpointer) fd_sig, NULL);
1377 g_source_set_priority(src_sig, G_PRIORITY_DEFAULT);
1378 r = g_source_attach(src_sig, NULL);
1380 _E("fail to attach the source : %d", r);
1387 r = rua_clear_history();
1389 _D("rua_clear_history : %d", r);
1391 dbus_error_init(&error);
1392 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1394 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1395 home_appid = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
1396 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, __home_appid_vconf_cb, NULL) != 0) {
1397 _E("Unable to register callback for VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME\n");
1401 restart_tbl = g_hash_table_new(g_str_hash, g_str_equal);
1405 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1406 const char* _get_home_appid(void)