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.
25 #include <sys/types.h>
32 #include <proc_stat.h>
33 #include <pkgmgr-info.h>
34 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
35 #include <vconf/vconf.h>
37 #include <bundle_internal.h>
40 #include "amd_config.h"
41 #include "amd_status.h"
42 #include "amd_appinfo.h"
43 #include "amd_launch.h"
45 #include "simple_util.h"
47 #include "menu_db_util.h"
48 #include "amd_app_group.h"
50 #define WINDOW_READY "/tmp/.wm_ready"
52 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
53 int cooldown_status = 0;
54 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
56 GSList *app_status_info_list = NULL;
57 GHashTable *pkg_status_info_table = NULL;
58 GHashTable *app_running_cache = NULL;
59 struct appinfomgr *_saf = NULL;
61 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
62 #define AMD_LOG_BUFFER_SIZE 10000
63 #define AMD_LOG_BUFFER_STRING_SIZE 128
64 #define AMD_LOG_FILE "/var/log/amd.log"
66 static int log_index = 0;
67 static int log_fd = 0;
70 static app_status_info_t* __get_app_status_info(int pid)
73 app_status_info_t *info_t = NULL;
75 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter)) {
76 info_t = (app_status_info_t *)iter->data;
77 if (info_t && info_t->pid == pid) {
85 static void __add_running_cache(const char *appid, int pid)
87 if (!appid || pid < 1)
90 if (!app_running_cache)
91 app_running_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
93 GSList *pid_list = NULL;
95 pid_list = (GSList *)g_hash_table_lookup(app_running_cache, appid);
96 if (pid_list == NULL) {
97 pid_list = g_slist_append(pid_list, GINT_TO_POINTER(pid));
98 g_hash_table_insert(app_running_cache, g_strdup(appid), pid_list);
100 if (!g_slist_find(pid_list, GINT_TO_POINTER(pid)))
101 pid_list = g_slist_append(pid_list, GINT_TO_POINTER(pid));
105 static void __remove_running_cache(const char *appid, int pid)
107 if (!appid || pid < 1)
110 if (!app_running_cache)
113 GSList *pid_list = NULL;
114 GSList *remain_list = NULL;
116 pid_list = (GSList *)g_hash_table_lookup(app_running_cache, appid);
117 if (pid_list == NULL)
120 remain_list = g_slist_remove(pid_list, GINT_TO_POINTER(pid));
121 if (remain_list && remain_list != pid_list) {
122 g_hash_table_replace(app_running_cache, g_strdup(appid), remain_list);
126 g_hash_table_remove(app_running_cache, appid);
130 static void __add_pkg_info(const char *pkgid, app_status_info_t *appinfo)
132 pkg_status_info_t *pkginfo = NULL;
134 if (pkgid == NULL || appinfo == NULL) {
135 _E("empty arguments: %s", pkgid == NULL ? (appinfo == NULL ? "appinfo, pkgid" : "pkgid") : "appinfo");
139 if (pkg_status_info_table == NULL)
140 pkg_status_info_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
142 pkginfo = g_hash_table_lookup(pkg_status_info_table, pkgid);
143 if (pkginfo == NULL) {
144 pkginfo = (pkg_status_info_t *)malloc(sizeof(pkg_status_info_t));
145 if (pkginfo == NULL) {
146 _E("failed to allocate memory");
149 memset(pkginfo, 0, sizeof(pkg_status_info_t));
150 pkginfo->pkgid = strdup(pkgid);
151 if (pkginfo->pkgid == NULL) {
153 _E("failed to allocate memory");
156 g_hash_table_insert(pkg_status_info_table, pkginfo->pkgid, pkginfo);
159 pkginfo->status = appinfo->status;
160 appinfo->pkginfo = pkginfo;
162 if (appinfo->status == STATUS_SERVICE) {
163 pkginfo->svc_list = g_slist_append(pkginfo->svc_list, appinfo);
166 pkginfo->ui_list = g_slist_append(pkginfo->ui_list, appinfo);
170 static int __get_ui_app_status_pkg_info(pkg_status_info_t *pkginfo)
172 app_status_info_t *appinfo = NULL;
174 int status = STATUS_BG;
179 iter = pkginfo->ui_list;
181 appinfo = (app_status_info_t *)iter->data;
182 if (appinfo->status != STATUS_BG) {
183 status = appinfo->status;
186 iter = g_slist_next(iter);
192 static int __update_pkg_info(const char *pkgid, app_status_info_t *appinfo)
194 pkg_status_info_t *pkginfo = NULL;
197 if (pkgid == NULL || appinfo == NULL)
200 if (pkg_status_info_table == NULL) {
204 pkginfo = (pkg_status_info_t *)g_hash_table_lookup(pkg_status_info_table, pkgid);
205 if (pkginfo == NULL) {
206 _E("pkgid(%s) is not on list");
210 if (pkginfo->ui_list) {
211 ret = __get_ui_app_status_pkg_info(pkginfo);
213 pkginfo->status = ret;
216 pkginfo->status = STATUS_SERVICE;
222 static void __remove_pkg_info(const char *pkgid, app_status_info_t *appinfo)
224 pkg_status_info_t *pkginfo = NULL;
225 const struct appinfo *ai = NULL;
226 const char *component_type = NULL;
228 if (pkgid == NULL || appinfo == NULL) {
229 _E("empty arguments: %s", pkgid == NULL ? (appinfo == NULL ? "appinfo, pkgid" : "pkgid") : "appinfo");
233 ai = appinfo_find(_saf, appinfo->appid);
234 component_type = appinfo_get_value(ai, AIT_COMPTYPE);
236 pkginfo = (pkg_status_info_t *)g_hash_table_lookup(pkg_status_info_table, pkgid);
237 if (pkginfo == NULL) {
238 _E("pkgid(%s) is not on list");
242 if (component_type && strcmp(component_type, APP_TYPE_SERVICE) == 0) {
243 if (pkginfo->svc_list) {
244 pkginfo->svc_list = g_slist_remove(pkginfo->svc_list, appinfo);
245 _D("STATUS_SERVICE : appid(%s)", appinfo->appid);
248 if (pkginfo->ui_list) {
249 pkginfo->ui_list = g_slist_remove(pkginfo->ui_list, appinfo);
250 _D("~STATUS_SERVICE : appid(%s)", appinfo->appid);
254 if (!pkginfo->svc_list && !pkginfo->ui_list) {
255 g_hash_table_remove(pkg_status_info_table, pkgid);
256 if (pkginfo->pkgid) {
257 free(pkginfo->pkgid);
258 pkginfo->pkgid = NULL;
264 static void __remove_all_shared_info(app_status_info_t *info_t)
266 if (info_t && info_t->shared_info_list) {
267 GList *list = info_t->shared_info_list;
270 shared_info_t *sit = (shared_info_t*)list->data;
273 if (sit->owner_exec_label)
274 free(sit->owner_exec_label);
278 if (sit->paths[i] == NULL) {
289 list = g_list_next(list);
292 g_list_free(info_t->shared_info_list);
293 info_t->shared_info_list = NULL;
297 static void __destroy_app_status_info(app_status_info_t *info_t)
304 info_t->appid = NULL;
307 if (info_t->app_path) {
308 free(info_t->app_path);
309 info_t->app_path = NULL;
312 if (info_t->caller) {
313 free(info_t->caller);
314 info_t->caller = NULL;
319 info_t->pkgid = NULL;
322 if (info_t->exec_label) {
323 free(info_t->exec_label);
324 info_t->exec_label = NULL;
327 __remove_all_shared_info(info_t);
331 int _status_add_app_info_list(const char *appid, const char *app_path,
332 const char *caller, int pid, int pad_pid, int is_subapp)
335 app_status_info_t *info_t = NULL;
336 const struct appinfo *ai;
337 const char *component_type = NULL;
338 const char *pkgid = NULL;
340 if (!appid || !app_path)
343 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter)) {
344 info_t = (app_status_info_t *)iter->data;
345 if (pid == info_t->pid) {
346 if (!strcmp(appid, info_t->appid)) {
349 __remove_running_cache(info_t->appid, info_t->pid);
350 app_status_info_list = g_slist_remove(app_status_info_list, info_t);
352 __remove_pkg_info(info_t->pkgid, info_t);
354 __destroy_app_status_info(info_t);
360 ai = appinfo_find(_saf, appid);
362 info_t = malloc(sizeof(app_status_info_t));
363 if (info_t == NULL) {
368 memset(info_t, 0, sizeof(app_status_info_t));
370 info_t->appid = strdup(appid);
371 if (info_t->appid == NULL)
374 info_t->app_path = strdup(app_path);
375 if (info_t->app_path == NULL)
379 info_t->caller = strdup(caller);
380 if (info_t->caller == NULL)
384 component_type = appinfo_get_value(ai, AIT_COMPTYPE);
385 if (component_type && strcmp(component_type, APP_TYPE_SERVICE) == 0) {
386 info_t->status = STATUS_SERVICE;
388 info_t->status = STATUS_LAUNCHING;
391 pkgid = appinfo_get_value(ai, AIT_PKGID);
396 info_t->pad_pid = pad_pid;
397 info_t->is_subapp = is_subapp;
398 info_t->shared_info_list = NULL;
399 info_t->exec_label = NULL;
401 info_t->pkgid = strdup(pkgid);
402 if (info_t->pkgid == NULL)
405 app_status_info_list = g_slist_append(app_status_info_list, info_t);
407 __add_pkg_info(pkgid, info_t);
409 __add_running_cache(info_t->appid, pid);
411 _D("pid(%d) appid(%s) pkgid(%s) comp(%s)", pid, appid, pkgid, component_type);
416 __destroy_app_status_info(info_t);
421 static Eina_Bool __app_terminate_timer_cb(void *data)
428 ret = kill(pid, SIGKILL);
430 _W("send SIGKILL: %s", strerror(errno));
432 return ECORE_CALLBACK_CANCEL;
435 int _status_update_app_info_list(int pid, int status)
438 app_status_info_t *info_t = NULL;
440 _D("pid(%d) status(%d)", pid, status);
441 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
443 info_t = (app_status_info_t *)iter->data;
444 if(pid == info_t->pid) {
445 info_t->status = status;
446 if(status == STATUS_DYING) {
447 if(info_t->pad_pid != DEBUG_LAUNCHPAD_PID)
448 ecore_timer_add(5, __app_terminate_timer_cb, (void *)info_t->pid);
450 __update_pkg_info(info_t->pkgid, info_t);
452 _D("pid(%d) appid(%s) pkgid(%s) status(%d)", pid, info_t->appid, info_t->pkgid, info_t->status);
457 app_group_set_status(pid, status);
462 int _status_remove_app_info_list(int pid)
465 app_status_info_t *info_t = NULL;
467 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
469 info_t = (app_status_info_t *)iter->data;
470 if(pid == info_t->pid) {
471 __remove_running_cache(info_t->appid, info_t->pid);
472 app_status_info_list = g_slist_remove(app_status_info_list, info_t);
474 __remove_pkg_info(info_t->pkgid, info_t);
476 __destroy_app_status_info(info_t);
484 int _status_get_app_info_status(int pid)
487 app_status_info_t *info_t = NULL;
489 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
491 info_t = (app_status_info_t *)iter->data;
492 if(pid == info_t->pid) {
493 return info_t->status;
497 return app_group_get_status(pid);
500 static gint __find_app_bypid(gconstpointer app_data, gconstpointer pid_data)
502 int pid = GPOINTER_TO_INT(pid_data);
503 app_status_info_t *appinfo = (app_status_info_t *)app_data;
505 if (appinfo && pid && appinfo->pid == pid)
511 void _status_find_service_apps(int pid, enum app_status status, void (*send_event_to_svc_core) (int))
513 GSList *app_list = NULL;
514 GSList *svc_list = NULL;
515 app_status_info_t *info_t = NULL;
516 app_status_info_t *svc_info_t = NULL;
518 app_list = g_slist_find_custom(app_status_info_list, GINT_TO_POINTER(pid), __find_app_bypid);
521 _E("unable to find app by pid:%d", pid);
525 info_t = (app_status_info_t *)app_list->data;
526 if (info_t && info_t->pkginfo && info_t->pkginfo->status == status)
527 svc_list = info_t->pkginfo->svc_list;
530 svc_info_t = (app_status_info_t *)svc_list->data;
532 send_event_to_svc_core(svc_info_t->pid);
534 svc_list = g_slist_next(svc_list);
538 void _status_check_service_only(int pid, void (*send_event_to_svc_core) (int))
540 GSList *app_list = NULL;
541 GSList *ui_list = NULL;
542 app_status_info_t *info_t = NULL;
543 app_status_info_t *ui_info_t = NULL;
546 app_list = g_slist_find_custom(app_status_info_list, GINT_TO_POINTER(pid), __find_app_bypid);
549 _E("unable to find app by pid:%d", pid);
553 info_t = (app_status_info_t *)app_list->data;
554 ui_list = info_t->pkginfo->ui_list;
556 ui_info_t = (app_status_info_t *)ui_list->data;
557 if (ui_info_t && _status_app_is_running_v2(ui_info_t->appid) > 0) {
560 ui_list = g_slist_next(ui_list);
564 send_event_to_svc_core(pid);
567 int _status_update_app_info_caller_pid(int pid, int caller_pid)
570 app_status_info_t *info_t = NULL;
572 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter)) {
573 info_t = (app_status_info_t *)iter->data;
574 if (info_t && info_t->pid == pid) {
575 info_t->last_caller_pid = caller_pid;
582 int _status_set_exec_label(int pid, const char *exec_label)
584 app_status_info_t *info_t = __get_app_status_info(pid);
587 if (info_t->exec_label)
588 free(info_t->exec_label);
589 info_t->exec_label = strdup(exec_label);
596 const char* _status_get_exec_label(int pid)
598 app_status_info_t *info_t = __get_app_status_info(pid);
601 return info_t->exec_label;
606 int _status_add_shared_info(int pid, const char *exec_label, char **paths)
608 shared_info_t *sit = (shared_info_t*)malloc(sizeof(shared_info_t));
613 sit->owner_exec_label = strdup(exec_label);
616 app_status_info_t* info_t = __get_app_status_info(pid);
619 info_t->shared_info_list = g_list_append(info_t->shared_info_list, sit);
623 if (sit->owner_exec_label)
624 free(sit->owner_exec_label);
629 int _status_clear_shared_info_list(int pid)
631 app_status_info_t* info_t = __get_app_status_info(pid);
634 __remove_all_shared_info(info_t);
641 GList* _status_get_shared_info_list(int pid)
643 app_status_info_t *info_t = __get_app_status_info(pid);
646 return info_t->shared_info_list;
651 int _status_get_app_info_last_caller_pid(int pid)
654 app_status_info_t *info_t = NULL;
656 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter)) {
657 info_t = (app_status_info_t *)iter->data;
658 if (info_t && info_t->pid == pid) {
659 return info_t->last_caller_pid;
665 int _status_app_is_running(const char *appid)
668 app_status_info_t *info_t = NULL;
670 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
672 info_t = (app_status_info_t *)iter->data;
673 if( strncmp(appid, info_t->appid, MAX_PACKAGE_STR_SIZE) == 0 && !info_t->is_subapp) {
680 char* _status_app_get_appid_bypid(int pid)
683 app_status_info_t *info_t = NULL;
685 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
687 info_t = (app_status_info_t *)iter->data;
688 if( pid == info_t->pid ) {
689 return info_t->appid;
695 char* _status_get_caller_by_appid(const char *appid)
698 app_status_info_t *info_t = NULL;
700 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
702 info_t = (app_status_info_t *)iter->data;
703 if( strncmp(appid, info_t->appid, MAX_PACKAGE_STR_SIZE-1) == 0 && !info_t->is_subapp) {
704 return info_t->caller;
712 int _status_send_running_appinfo(int fd)
715 app_status_info_t *info_t = NULL;
716 app_pkt_t *pkt = NULL;
717 char tmp_pid[MAX_PID_STR_BUFSZ];
719 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
725 memset(pkt, 0, AUL_SOCK_MAXBUFF);
727 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
729 info_t = (app_status_info_t *)iter->data;
730 if (app_group_is_sub_app(info_t->pid))
733 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", info_t->pid);
734 strncat((char *)pkt->data, tmp_pid, MAX_PID_STR_BUFSZ);
735 strncat((char *)pkt->data, ":", 1);
736 strncat((char *)pkt->data, info_t->appid, MAX_PACKAGE_STR_SIZE);
737 strncat((char *)pkt->data, ":", 1);
738 strncat((char *)pkt->data, info_t->app_path, MAX_PACKAGE_APP_PATH_SIZE);
739 strncat((char *)pkt->data, ";", 1);
742 pkt->cmd = APP_RUNNING_INFO_RESULT;
743 pkt->len = strlen((char *)pkt->data) + 1;
745 if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
747 _E("send failed due to EPIPE.\n");
748 _E("send fail to client");
759 int _status_app_is_running_v2(const char *appid)
761 char *apppath = NULL;
764 const char *ae = NULL;
765 const struct appinfo *ai;
770 ai = appinfo_find(_saf, appid);
775 ae = appinfo_get_value(ai, AIT_EXEC);
779 apppath = strdup(ae);
783 /*truncate apppath if it includes default bundles */
784 while (apppath[i] != 0) {
785 if (apppath[i] == ' ' || apppath[i] == '\t') {
792 ret = __proc_iter_cmdline(NULL, apppath);
799 int _status_app_is_running_from_cache(const char *appid)
801 const char *path = NULL;
802 GSList *pid_list = NULL;
803 const struct appinfo *ai;
805 if (app_running_cache)
806 pid_list = (GSList *)g_hash_table_lookup(app_running_cache, appid);
809 ai = appinfo_find(_saf, appid);
813 path = appinfo_get_value(ai, AIT_EXEC);
818 if (__proc_check_app(path, GPOINTER_TO_INT(pid_list->data))) {
819 _D("is_running hit cache, return immediately");
820 return GPOINTER_TO_INT(pid_list->data);
822 _E("is_running garbage, pid: %d", GPOINTER_TO_INT(pid_list->data));
823 __remove_running_cache(appid, GPOINTER_TO_INT(pid_list->data));
825 pid_list = pid_list->next;
832 int _status_app_is_running_v2_cached(const char *appid)
836 ret = _status_app_is_running_from_cache(appid);
840 ret = _status_app_is_running_v2(appid);
842 _E("is running missing app detected: %s (%d)", appid, ret);
843 __add_running_cache((char *)appid, ret);
849 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
851 app_info_from_db *menu_info;
853 char *pkgname = NULL;
854 char *app_path = NULL;
856 r_info = (char *)priv;
858 if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
861 pkgname = _get_pkgname(menu_info);
865 app_path = _get_app_path(menu_info);
866 if (app_path == NULL)
869 strncat(r_info, dname, 8);
870 strncat(r_info, ":", 1);
871 strncat(r_info, pkgname, MAX_PACKAGE_STR_SIZE);
872 strncat(r_info, ":", 1);
873 strncat(r_info, app_path, MAX_PACKAGE_APP_PATH_SIZE);
874 strncat(r_info, ";", 1);
878 if (menu_info != NULL)
879 _free_app_info_from_db(menu_info);
883 int _status_send_running_appinfo_v2(int fd)
885 app_pkt_t *pkt = NULL;
887 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
894 memset(pkt, 0, AUL_SOCK_MAXBUFF);
896 __proc_iter_cmdline(__get_pkginfo, pkt->data);
898 pkt->cmd = APP_RUNNING_INFO_RESULT;
899 pkt->len = strlen((char *)pkt->data) + 1;
901 if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
903 _E("send failed due to EPIPE.\n");
904 _E("send fail to client");
915 int _status_get_pkgname_bypid(int pid, char *pkgname, int len)
918 app_info_from_db *menu_info;
920 cmdline = __proc_get_cmdline_bypid(pid);
924 if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
928 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
932 _free_app_info_from_db(menu_info);
937 int _status_get_appid_bypid(int fd, int pid)
939 app_pkt_t *pkt = NULL;
942 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
949 memset(pkt, 0, AUL_SOCK_MAXBUFF);
951 pkt->cmd = APP_GET_INFO_ERROR;
953 if (_status_get_pkgname_bypid(pid, (char *)pkt->data, MAX_PACKAGE_STR_SIZE) == 0) {
954 SECURE_LOGD("appid for %d is %s", pid, pkt->data);
955 pkt->cmd = APP_GET_INFO_OK;
958 /* support app launched by shell script*/
964 _D("second change pgid = %d, pid = %d", pgid, pid);
965 if (_status_get_pkgname_bypid(pgid, (char *)pkt->data, MAX_PACKAGE_STR_SIZE) == 0)
966 pkt->cmd = APP_GET_INFO_OK;
969 pkt->len = strlen((char *)pkt->data) + 1;
971 if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
973 _E("send failed due to EPIPE.\n");
974 _E("send fail to client");
985 static int __get_pkgid_bypid(int pid, char *pkgid, int len)
988 app_info_from_db *menu_info;
990 cmdline = __proc_get_cmdline_bypid(pid);
994 if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
998 snprintf(pkgid, len, "%s", _get_pkgid(menu_info));
1001 _free_app_info_from_db(menu_info);
1006 int _status_get_pkgid_bypid(int fd, int pid)
1008 app_pkt_t *pkt = NULL;
1011 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1018 memset(pkt, 0, AUL_SOCK_MAXBUFF);
1020 pkt->cmd = APP_GET_INFO_ERROR;
1022 if (__get_pkgid_bypid(pid, (char *)pkt->data, MAX_PACKAGE_STR_SIZE) == 0) {
1023 SECURE_LOGD("appid for %d is %s", pid, pkt->data);
1024 pkt->cmd = APP_GET_INFO_OK;
1027 /* support app launched by shell script*/
1028 _D("second chance");
1029 pgid = getpgid(pid);
1033 _D("second change pgid = %d, pid = %d", pgid, pid);
1034 if (__get_pkgid_bypid(pgid, (char *)pkt->data, MAX_PACKAGE_STR_SIZE) == 0)
1035 pkt->cmd = APP_GET_INFO_OK;
1038 pkt->len = strlen((char *)pkt->data) + 1;
1040 if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1042 _E("send failed due to EPIPE.\n");
1043 _E("send fail to client");
1054 int _status_get_cmdline(int fd, int pid)
1056 app_pkt_t *pkt = NULL;
1060 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1067 memset(pkt, 0, AUL_SOCK_MAXBUFF);
1069 pkt->cmd = APP_GET_INFO_ERROR;
1071 cmdline = __proc_get_cmdline_bypid(pid);
1072 if (cmdline == NULL)
1074 _E("cmdline : %s", cmdline);
1076 strncpy((char *)pkt->data, cmdline, MAX_PACKAGE_STR_SIZE);
1077 _E("pkt->data : %s", pkt->data);
1078 pkt->cmd = APP_GET_INFO_OK;
1081 pkt->len = strlen((char *)pkt->data) + 1;
1083 if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1085 _E("send failed due to EPIPE.\n");
1086 _E("send fail to client");
1100 int _status_send_group_info(int fd)
1102 GSList *iter = NULL;
1103 GSList *iter2 = NULL;
1104 app_status_info_t *info_t = NULL;
1105 app_status_info_t *info_t2 = NULL;
1106 app_pkt_t *pkt = NULL;
1109 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1115 memset(pkt, 0, AUL_SOCK_MAXBUFF);
1117 snprintf(buf, sizeof(buf), "=======================STATUS_LAUNCHING======================\n");
1118 strncat((char *)pkt->data, buf, 2048);
1119 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1121 info_t = (app_status_info_t *)iter->data;
1122 if(info_t->status == STATUS_LAUNCHING) {
1123 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t->appid, info_t->pid);
1124 strncat((char *)pkt->data, buf, 2048);
1125 snprintf(buf, sizeof(buf), "||\n");
1126 strncat((char *)pkt->data, buf, 2048);
1130 snprintf(buf, sizeof(buf), "\n");
1131 strncat((char *)pkt->data, buf, 2048);
1133 snprintf(buf, sizeof(buf), "=======================STATUS_FG=============================\n");
1134 strncat((char *)pkt->data, buf, 2048);
1135 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1137 info_t = (app_status_info_t *)iter->data;
1139 _D("%s(%d)=>", info_t->appid, info_t->pid);
1140 if(info_t->status != STATUS_VISIBLE) {
1144 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t->appid, info_t->pid);
1145 strncat((char *)pkt->data, buf, 2048);
1146 snprintf(buf, sizeof(buf), "||\n");
1147 strncat((char *)pkt->data, buf, 2048);
1151 snprintf(buf, sizeof(buf), "\n");
1152 strncat((char *)pkt->data, buf, 2048);
1154 snprintf(buf, sizeof(buf), "=======================STATUS_BG=============================\n");
1155 strncat((char *)pkt->data, buf, 2048);
1157 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1159 info_t = (app_status_info_t *)iter->data;
1161 if(info_t->status != STATUS_BG)
1163 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t->appid, info_t->pid);
1164 strncat((char *)pkt->data, buf, 2048);
1165 snprintf(buf, sizeof(buf), "||\n");
1166 strncat((char *)pkt->data, buf, 2048);
1170 snprintf(buf, sizeof(buf), "\n");
1171 strncat((char *)pkt->data, buf, 2048);
1173 snprintf(buf, sizeof(buf), "=======================STATUS_SERVICE(FG)====================\n");
1174 strncat((char *)pkt->data, buf, 2048);
1175 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1177 info_t = (app_status_info_t *)iter->data;
1178 if(info_t->status == STATUS_SERVICE && info_t->pkginfo->status == STATUS_VISIBLE) {
1179 snprintf(buf, sizeof(buf), "[pkgid(%s)]", info_t->pkginfo->pkgid);
1180 strncat((char *)pkt->data, buf, 2048);
1181 for (iter2 = info_t->pkginfo->svc_list; iter2 != NULL; iter2 = g_slist_next(iter2))
1183 info_t2 = (app_status_info_t *)iter2->data;
1184 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t2->appid, info_t2->pid);
1185 strncat((char *)pkt->data, buf, 2048);
1187 snprintf(buf, sizeof(buf), "||\n");
1188 strncat((char *)pkt->data, buf, 2048);
1191 snprintf(buf, sizeof(buf), "\n");
1192 strncat((char *)pkt->data, buf, 2048);
1194 snprintf(buf, sizeof(buf), "=======================STATUS_SERVICE(BG)====================\n");
1195 strncat((char *)pkt->data, buf, 2048);
1196 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1198 info_t = (app_status_info_t *)iter->data;
1199 if(info_t->status == STATUS_SERVICE && info_t->pkginfo->status == STATUS_BG) {
1200 snprintf(buf, sizeof(buf), "[pkgid(%s)]", info_t->pkginfo->pkgid);
1201 strncat((char *)pkt->data, buf, 2048);
1202 for (iter2 = info_t->pkginfo->svc_list; iter2 != NULL; iter2 = g_slist_next(iter2))
1204 info_t2 = (app_status_info_t *)iter2->data;
1205 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t2->appid, info_t2->pid);
1206 strncat((char *)pkt->data, buf, 2048);
1208 snprintf(buf, sizeof(buf), "||\n");
1209 strncat((char *)pkt->data, buf, 2048);
1212 snprintf(buf, sizeof(buf), "\n");
1213 strncat((char *)pkt->data, buf, 2048);
1215 snprintf(buf, sizeof(buf), "=======================STATUS_SERVICE========================\n");
1216 strncat((char *)pkt->data, buf, 2048);
1217 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1219 info_t = (app_status_info_t *)iter->data;
1220 if(info_t->status == STATUS_SERVICE && info_t->pkginfo->status == STATUS_SERVICE) {
1221 snprintf(buf, sizeof(buf), "[pkgid(%s)]", info_t->pkginfo->pkgid);
1222 _D("[pkgid(%s)]", info_t->pkginfo->pkgid);
1223 strncat((char *)pkt->data, buf, 2048);
1224 for (iter2 = info_t->pkginfo->svc_list; iter2 != NULL; iter2 = g_slist_next(iter2))
1226 info_t2 = (app_status_info_t *)iter2->data;
1227 snprintf(buf, sizeof(buf), "%s(%d)=>", info_t2->appid, info_t2->pid);
1228 strncat((char *)pkt->data, buf, 2048);
1230 snprintf(buf, sizeof(buf), "||\n");
1231 strncat((char *)pkt->data, buf, 2048);
1235 snprintf(buf, sizeof(buf), "************************************************************\n");
1236 strncat((char *)pkt->data, buf, 2048);
1238 pkt->cmd = APP_GET_GROUP_INFO;
1239 pkt->len = strlen((char *)pkt->data) + 1;
1241 if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1243 _E("send failed due to EPIPE.\n");
1244 _E("send fail to client");
1255 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1256 static void __app_info_iter_limit_cb(void *user_data, const char *appid, const struct appinfo *ai)
1258 if(appinfo_get_boolean(ai, AIT_TASKMANAGE) && !appinfo_get_boolean(ai, AIT_COOLDOWN) ) {
1259 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "blocking");
1263 static void __app_info_iter_release_cb(void *user_data, const char *appid, const struct appinfo *ai)
1265 if(appinfo_get_boolean(ai, AIT_TASKMANAGE) && !appinfo_get_boolean(ai, AIT_COOLDOWN) ) {
1266 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "installed");
1270 static int __cooldown_cb(const char* status, void *data)
1272 GSList *iter = NULL;
1273 app_status_info_t *info_t = NULL;
1274 const struct appinfo *ai;
1277 _D("status %s", status);
1279 if(strncmp(status, "LimitAction", 11) == 0) {
1280 appinfo_foreach(_saf, __app_info_iter_limit_cb, NULL);
1281 for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1283 info_t = (app_status_info_t *)iter->data;
1284 ai = appinfo_find(_saf, info_t->appid);
1285 if(appinfo_get_boolean(ai, AIT_TASKMANAGE) && !appinfo_get_boolean(ai, AIT_COOLDOWN)) {
1286 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "blocking");
1287 const char *type = appinfo_get_value(ai, AIT_COMPTYPE);
1288 aul_send_app_terminate_request_signal(info_t->pid, info_t->appid, info_t->pkgid, type);
1289 __app_send_raw_with_noreply(info_t->pid, APP_TERM_BY_PID_ASYNC, (unsigned char *)&dummy, sizeof(int) );
1292 cooldown_status = COOLDOWN_LIMIT;
1293 } else if (strncmp(status, "Release", 7) == 0 && cooldown_status != COOLDOWN_RELEASE){
1294 appinfo_foreach(_saf, __app_info_iter_release_cb, NULL);
1295 cooldown_status = COOLDOWN_RELEASE;
1301 int _status_get_cooldown_status(void)
1303 return cooldown_status;
1306 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1308 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1309 static void __app_info_iter_ups_release_cb(void *user_data, const char *appid, const struct appinfo *ai)
1311 const char *component = NULL;
1314 component = appinfo_get_value(ai, AIT_COMPTYPE);
1315 onboot = appinfo_get_boolean(ai, AIT_ONBOOT);
1316 if (onboot == 1 && strncmp(component, "svcapp", 6) == 0) {
1317 if (_status_app_is_running(appid) < 0) {
1318 _W("start service (ups release) - %s", appid);
1321 _E("service: %s is already running", appid);
1326 static void __ups_cb(keynode_t *key, void *data)
1328 const char *name = NULL;
1331 name = vconf_keynode_get_name(key);
1333 _E("vconf key value for UPS mode is invalid");
1335 } else if (strcmp(name, VCONFKEY_SETAPPL_PSMODE) == 0) {
1336 ups_mode = vconf_keynode_get_int(key);
1337 if (ups_mode == SETTING_PSMODE_NORMAL) {
1338 _W("UPS mode is disabled");
1339 appinfo_foreach(_saf, __app_info_iter_ups_release_cb, NULL);
1341 else if (ups_mode == SETTING_PSMODE_EMERGENCY) {
1342 _W("UPS mode is enabled");
1345 _W("Current Power Saving mode: %d", ups_mode);
1349 #endif //_APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1351 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
1352 static int _status_log_init(void)
1356 log_fd = open(AMD_LOG_FILE, O_CREAT | O_WRONLY, 0644);
1361 offset = lseek(log_fd, 0, SEEK_END);
1363 log_index = (int)(offset / AMD_LOG_BUFFER_STRING_SIZE);
1365 if (log_index >= AMD_LOG_BUFFER_SIZE) {
1367 lseek(log_fd, 0, SEEK_SET);
1374 int _status_log_save(const char *tag, const char *message)
1379 char time_buf[32] = {0,};
1380 char buffer[AMD_LOG_BUFFER_STRING_SIZE] = {0,};
1386 ctime_r(&now, time_buf);
1387 if (log_index != 0) {
1388 offset = lseek(log_fd, 0, SEEK_CUR);
1390 offset = lseek(log_fd, 0, SEEK_SET);
1394 _E("error in lseek: %s", strerror(errno));
1396 snprintf(buffer, AMD_LOG_BUFFER_STRING_SIZE, "[%-6d] %-15s %-50s %s", log_index, tag, message, time_buf);
1398 ret = write(log_fd, buffer, strlen(buffer));
1400 _E("Cannot write the amd log: %d", ret);
1404 if (++log_index >= AMD_LOG_BUFFER_SIZE) {
1412 int _status_init(struct amdmgr* amd)
1416 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1417 aul_listen_cooldown_signal(__cooldown_cb, NULL);
1418 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1420 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1421 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __ups_cb, NULL) != 0) {
1422 _E("Unable to register callback for VCONFKEY_SETAPPL_PSMODE");
1426 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
1433 gboolean _status_check_window_ready(void)
1435 if (access(WINDOW_READY, R_OK) == 0)