68b73f692505a3c455bd145f3b5a47134982e2cb
[framework/appfw/aul-1.git] / am_daemon / amd_status.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <glib.h>
29 #include <aul.h>
30 #include <string.h>
31 #include <Ecore.h>
32 #include <proc_stat.h>
33 #include <pkgmgr-info.h>
34 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
35 #include <vconf/vconf.h>
36 #endif
37 #include <bundle_internal.h>
38
39 #include "launch.h"
40 #include "amd_config.h"
41 #include "amd_status.h"
42 #include "amd_appinfo.h"
43 #include "amd_launch.h"
44 #include "aul_util.h"
45 #include "simple_util.h"
46 #include "app_sock.h"
47 #include "menu_db_util.h"
48 #include "amd_app_group.h"
49
50 #define WINDOW_READY    "/tmp/.wm_ready"
51
52 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
53 int cooldown_status = 0;
54 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
55
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;
60
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"
65
66 static int log_index = 0;
67 static int log_fd = 0;
68 #endif
69
70 static app_status_info_t* __get_app_status_info(int pid)
71 {
72         GSList *iter = NULL;
73         app_status_info_t *info_t = NULL;
74
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) {
78                         return info_t;
79                 }
80         }
81
82         return NULL;
83 }
84
85 static void __add_running_cache(const char *appid, int pid)
86 {
87         if (!appid || pid < 1)
88                 return;
89
90         if (!app_running_cache)
91                 app_running_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
92
93         GSList *pid_list = NULL;
94
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);
99         } else {
100                 if (!g_slist_find(pid_list, GINT_TO_POINTER(pid)))
101                         pid_list = g_slist_append(pid_list, GINT_TO_POINTER(pid));
102         }
103 }
104
105 static void __remove_running_cache(const char *appid, int pid)
106 {
107         if (!appid || pid < 1)
108                 return;
109
110         if (!app_running_cache)
111                 return;
112
113         GSList *pid_list = NULL;
114         GSList *remain_list = NULL;
115
116         pid_list = (GSList *)g_hash_table_lookup(app_running_cache, appid);
117         if (pid_list == NULL)
118                 return;
119
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);
123         }
124
125         if (!remain_list) {
126                 g_hash_table_remove(app_running_cache, appid);
127         }
128 }
129
130 static void __add_pkg_info(const char *pkgid, app_status_info_t *appinfo)
131 {
132         pkg_status_info_t *pkginfo = NULL;
133
134         if (pkgid == NULL || appinfo == NULL) {
135                 _E("empty arguments: %s", pkgid == NULL ? (appinfo == NULL ? "appinfo, pkgid" : "pkgid") : "appinfo");
136                 return;
137         }
138
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);
141
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");
147                         return;
148                 }
149                 memset(pkginfo, 0, sizeof(pkg_status_info_t));
150                 pkginfo->pkgid = strdup(pkgid);
151                 if (pkginfo->pkgid == NULL) {
152                         free(pkginfo);
153                         _E("failed to allocate memory");
154                         return;
155                 }
156                 g_hash_table_insert(pkg_status_info_table, pkginfo->pkgid, pkginfo);
157         }
158
159         pkginfo->status = appinfo->status;
160         appinfo->pkginfo = pkginfo;
161
162         if (appinfo->status == STATUS_SERVICE) {
163                 pkginfo->svc_list = g_slist_append(pkginfo->svc_list, appinfo);
164         }
165         else {
166                 pkginfo->ui_list = g_slist_append(pkginfo->ui_list, appinfo);
167         }
168 }
169
170 static int __get_ui_app_status_pkg_info(pkg_status_info_t *pkginfo)
171 {
172         app_status_info_t *appinfo = NULL;
173         GSList *iter = NULL;
174         int status = STATUS_BG;
175
176         if (pkginfo == NULL)
177                 return -1;
178
179         iter = pkginfo->ui_list;
180         while (iter) {
181                 appinfo = (app_status_info_t *)iter->data;
182                 if (appinfo->status != STATUS_BG) {
183                         status = appinfo->status;
184                 }
185
186                 iter = g_slist_next(iter);
187         }
188
189         return status;
190 }
191
192 static int __update_pkg_info(const char *pkgid, app_status_info_t *appinfo)
193 {
194         pkg_status_info_t *pkginfo = NULL;
195         int ret = 0;
196
197         if (pkgid == NULL || appinfo == NULL)
198                 return -1;
199
200         if (pkg_status_info_table == NULL) {
201                 return -1;
202         }
203
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");
207                 return -1;
208         }
209
210         if (pkginfo->ui_list) {
211                 ret = __get_ui_app_status_pkg_info(pkginfo);
212                 if (ret > -1)
213                         pkginfo->status = ret;
214         }
215         else
216                 pkginfo->status = STATUS_SERVICE;
217
218         return 0;
219 }
220
221
222 static void __remove_pkg_info(const char *pkgid, app_status_info_t *appinfo)
223 {
224         pkg_status_info_t *pkginfo = NULL;
225         const struct appinfo *ai = NULL;
226         const char *component_type = NULL;
227
228         if (pkgid == NULL || appinfo == NULL) {
229                 _E("empty arguments: %s", pkgid == NULL ? (appinfo == NULL ? "appinfo, pkgid" : "pkgid") : "appinfo");
230                 return;
231         }
232
233         ai = appinfo_find(_saf, appinfo->appid);
234         component_type = appinfo_get_value(ai, AIT_COMPTYPE);
235
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");
239                 return;
240         }
241
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);
246                 }
247         } else {
248                 if (pkginfo->ui_list) {
249                         pkginfo->ui_list = g_slist_remove(pkginfo->ui_list, appinfo);
250                         _D("~STATUS_SERVICE : appid(%s)", appinfo->appid);
251                 }
252         }
253
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;
259                 }
260                 free(pkginfo);
261         }
262 }
263
264 static void __remove_all_shared_info(app_status_info_t *info_t)
265 {
266         if (info_t && info_t->shared_info_list) {
267                 GList *list = info_t->shared_info_list;
268
269                 while (list) {
270                         shared_info_t *sit = (shared_info_t*)list->data;
271
272                         if (sit) {
273                                 if (sit->owner_exec_label)
274                                         free(sit->owner_exec_label);
275                                 if (sit->paths) {
276                                         int i = 0;
277                                         while (1) {
278                                                 if (sit->paths[i] == NULL) {
279                                                         free(sit->paths);
280                                                         break;
281                                                 }
282
283                                                 free(sit->paths[i]);
284                                                 i++;
285                                         }
286                                 }
287                                 free(sit);
288                         }
289                         list = g_list_next(list);
290                 }
291
292                 g_list_free(info_t->shared_info_list);
293                 info_t->shared_info_list = NULL;
294         }
295 }
296
297 static void __destroy_app_status_info(app_status_info_t *info_t)
298 {
299         if (info_t == NULL)
300                 return;
301
302         if (info_t->appid) {
303                 free(info_t->appid);
304                 info_t->appid = NULL;
305         }
306
307         if (info_t->app_path) {
308                 free(info_t->app_path);
309                 info_t->app_path = NULL;
310         }
311
312         if (info_t->caller) {
313                 free(info_t->caller);
314                 info_t->caller = NULL;
315         }
316
317         if (info_t->pkgid) {
318                 free(info_t->pkgid);
319                 info_t->pkgid = NULL;
320         }
321
322         if (info_t->exec_label) {
323                 free(info_t->exec_label);
324                 info_t->exec_label = NULL;
325         }
326
327         __remove_all_shared_info(info_t);
328         free(info_t);
329 }
330
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)
333 {
334         GSList *iter = NULL;
335         app_status_info_t *info_t = NULL;
336         const struct appinfo *ai;
337         const char *component_type = NULL;
338         const char *pkgid = NULL;
339
340         if (!appid || !app_path)
341                 return -1;
342
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)) {
347                                 return 0;
348                         } else {
349                                 __remove_running_cache(info_t->appid, info_t->pid);
350                                 app_status_info_list = g_slist_remove(app_status_info_list, info_t);
351
352                                 __remove_pkg_info(info_t->pkgid, info_t);
353
354                                 __destroy_app_status_info(info_t);
355                                 break;
356                         }
357                 }
358         }
359
360         ai = appinfo_find(_saf, appid);
361
362         info_t = malloc(sizeof(app_status_info_t));
363         if (info_t == NULL) {
364                 _E("out of memory");
365                 return -1;
366         }
367
368         memset(info_t, 0, sizeof(app_status_info_t));
369
370         info_t->appid = strdup(appid);
371         if (info_t->appid == NULL)
372                 goto error;
373
374         info_t->app_path = strdup(app_path);
375         if (info_t->app_path == NULL)
376                 goto error;
377
378         if (caller) {
379                 info_t->caller = strdup(caller);
380                 if (info_t->caller == NULL)
381                         goto error;
382         }
383
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;
387         } else {
388                 info_t->status = STATUS_LAUNCHING;
389         }
390
391         pkgid = appinfo_get_value(ai, AIT_PKGID);
392         if (pkgid == NULL)
393                 goto error;
394
395         info_t->pid = pid;
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;
400
401         info_t->pkgid = strdup(pkgid);
402         if (info_t->pkgid == NULL)
403                 goto error;
404
405         app_status_info_list = g_slist_append(app_status_info_list, info_t);
406
407         __add_pkg_info(pkgid, info_t);
408
409         __add_running_cache(info_t->appid, pid);
410
411         _D("pid(%d) appid(%s) pkgid(%s) comp(%s)", pid, appid, pkgid, component_type);
412
413         return 0;
414
415 error:
416         __destroy_app_status_info(info_t);
417
418         return -1;
419 }
420
421 static Eina_Bool __app_terminate_timer_cb(void *data)
422 {
423         int pid = (int)data;
424         int ret = 0;
425
426         _D("pid(%d)", pid);
427
428         ret = kill(pid, SIGKILL);
429         if (ret == -1)
430                 _W("send SIGKILL: %s", strerror(errno));
431
432         return ECORE_CALLBACK_CANCEL;
433 }
434
435 int _status_update_app_info_list(int pid, int status)
436 {
437         GSList *iter = NULL;
438         app_status_info_t *info_t = NULL;
439
440         _D("pid(%d) status(%d)", pid, status);
441         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
442         {
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);
449                         }
450                         __update_pkg_info(info_t->pkgid, info_t);
451
452                         _D("pid(%d) appid(%s) pkgid(%s) status(%d)", pid, info_t->appid, info_t->pkgid, info_t->status);
453                         break;
454                 }
455         }
456
457         app_group_set_status(pid, status);
458
459         return 0;
460 }
461
462 int _status_remove_app_info_list(int pid)
463 {
464         GSList *iter = NULL;
465         app_status_info_t *info_t = NULL;
466
467         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
468         {
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);
473
474                         __remove_pkg_info(info_t->pkgid, info_t);
475
476                         __destroy_app_status_info(info_t);
477                         break;
478                 }
479         }
480
481         return 0;
482 }
483
484 int _status_get_app_info_status(int pid)
485 {
486         GSList *iter = NULL;
487         app_status_info_t *info_t = NULL;
488
489         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
490         {
491                 info_t = (app_status_info_t *)iter->data;
492                 if(pid == info_t->pid) {
493                         return info_t->status;
494                 }
495         }
496
497         return app_group_get_status(pid);
498 }
499
500 static gint __find_app_bypid(gconstpointer app_data, gconstpointer pid_data)
501 {
502         int pid = GPOINTER_TO_INT(pid_data);
503         app_status_info_t *appinfo = (app_status_info_t *)app_data;
504
505         if (appinfo && pid && appinfo->pid == pid)
506                 return 0;
507
508         return -1;
509 }
510
511 void _status_find_service_apps(int pid, enum app_status status, void (*send_event_to_svc_core) (int))
512 {
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;
517
518         app_list = g_slist_find_custom(app_status_info_list, GINT_TO_POINTER(pid), __find_app_bypid);
519
520         if (!app_list) {
521                 _E("unable to find app by pid:%d", pid);
522                 return;
523         }
524
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;
528
529         while (svc_list) {
530                 svc_info_t = (app_status_info_t *)svc_list->data;
531                 if (svc_info_t)
532                         send_event_to_svc_core(svc_info_t->pid);
533
534                 svc_list = g_slist_next(svc_list);
535         }
536 }
537
538 void _status_check_service_only(int pid, void (*send_event_to_svc_core) (int))
539 {
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;
544         int ui_cnt = 0;
545
546         app_list = g_slist_find_custom(app_status_info_list, GINT_TO_POINTER(pid), __find_app_bypid);
547
548         if (!app_list) {
549                 _E("unable to find app by pid:%d", pid);
550                 return;
551         }
552
553         info_t = (app_status_info_t *)app_list->data;
554         ui_list = info_t->pkginfo->ui_list;
555         while (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) {
558                         ui_cnt++;
559                 }
560                 ui_list = g_slist_next(ui_list);
561         }
562
563         if (ui_cnt == 0)
564                 send_event_to_svc_core(pid);
565 }
566
567 int _status_update_app_info_caller_pid(int pid, int caller_pid)
568 {
569         GSList *iter = NULL;
570         app_status_info_t *info_t = NULL;
571
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;
576                         return 0;
577                 }
578         }
579         return -1;
580 }
581
582 int _status_set_exec_label(int pid, const char *exec_label)
583 {
584         app_status_info_t *info_t = __get_app_status_info(pid);
585
586         if (info_t) {
587                 if (info_t->exec_label)
588                         free(info_t->exec_label);
589                 info_t->exec_label = strdup(exec_label);
590                 return 0;
591         }
592
593         return -1;
594 }
595
596 const char* _status_get_exec_label(int pid)
597 {
598         app_status_info_t *info_t = __get_app_status_info(pid);
599
600         if (info_t)
601                 return info_t->exec_label;
602
603         return NULL;
604 }
605
606 int _status_add_shared_info(int pid, const char *exec_label, char **paths)
607 {
608         shared_info_t *sit = (shared_info_t*)malloc(sizeof(shared_info_t));
609
610         if (!sit)
611                 return -1;
612
613         sit->owner_exec_label = strdup(exec_label);
614         sit->paths = paths;
615
616         app_status_info_t* info_t = __get_app_status_info(pid);
617
618         if (info_t) {
619                 info_t->shared_info_list = g_list_append(info_t->shared_info_list, sit);
620                 return 0;
621         }
622
623         if (sit->owner_exec_label)
624                 free(sit->owner_exec_label);
625         free(sit);
626         return -1;
627 }
628
629 int _status_clear_shared_info_list(int pid)
630 {
631         app_status_info_t* info_t = __get_app_status_info(pid);
632
633         if (info_t) {
634                 __remove_all_shared_info(info_t);
635                 return 0;
636         }
637
638         return -1;
639 }
640
641 GList* _status_get_shared_info_list(int pid)
642 {
643         app_status_info_t *info_t = __get_app_status_info(pid);
644
645         if (info_t)
646                 return info_t->shared_info_list;
647
648         return NULL;
649 }
650
651 int _status_get_app_info_last_caller_pid(int pid)
652 {
653         GSList *iter = NULL;
654         app_status_info_t *info_t = NULL;
655
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;
660                 }
661         }
662         return -1;
663 }
664
665 int _status_app_is_running(const char *appid)
666 {
667         GSList *iter = NULL;
668         app_status_info_t *info_t = NULL;
669
670         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
671         {
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) {
674                         return info_t->pid;
675                 }
676         }
677         return -1;
678 }
679
680 char* _status_app_get_appid_bypid(int pid)
681 {
682         GSList *iter = NULL;
683         app_status_info_t *info_t = NULL;
684
685         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
686         {
687                 info_t = (app_status_info_t *)iter->data;
688                 if( pid == info_t->pid ) {
689                         return info_t->appid;
690                 }
691         }
692         return NULL;
693 }
694
695 char* _status_get_caller_by_appid(const char *appid)
696 {
697         GSList *iter = NULL;
698         app_status_info_t *info_t = NULL;
699
700         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
701         {
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;
705                 }
706         }
707
708         return NULL;
709 }
710
711
712 int _status_send_running_appinfo(int fd)
713 {
714         GSList *iter = NULL;
715         app_status_info_t *info_t = NULL;
716         app_pkt_t *pkt = NULL;
717         char tmp_pid[MAX_PID_STR_BUFSZ];
718
719         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
720         if(!pkt) {
721                 _E("malloc fail");
722                 return 0;
723         }
724
725         memset(pkt, 0, AUL_SOCK_MAXBUFF);
726
727         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
728         {
729                 info_t = (app_status_info_t *)iter->data;
730                 if (app_group_is_sub_app(info_t->pid))
731                         continue;
732
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);
740         }
741
742         pkt->cmd = APP_RUNNING_INFO_RESULT;
743         pkt->len = strlen((char *)pkt->data) + 1;
744
745         if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
746                 if (errno == EPIPE)
747                         _E("send failed due to EPIPE.\n");
748                 _E("send fail to client");
749         }
750
751         if(pkt)
752                 free(pkt);
753
754         close(fd);
755
756         return 0;
757 }
758
759 int _status_app_is_running_v2(const char *appid)
760 {
761         char *apppath = NULL;
762         int ret = 0;
763         int i = 0;
764         const char *ae = NULL;
765         const struct appinfo *ai;
766
767         if(appid == NULL)
768                 return -1;
769
770         ai = appinfo_find(_saf, appid);
771
772         if (ai == NULL)
773                 return -1;
774
775         ae = appinfo_get_value(ai, AIT_EXEC);
776         if (ae == NULL)
777                 return -1;
778
779         apppath = strdup(ae);
780         if (apppath == NULL)
781                 return -1;
782
783         /*truncate apppath if it includes default bundles */
784         while (apppath[i] != 0) {
785                 if (apppath[i] == ' ' || apppath[i] == '\t') {
786                         apppath[i]='\0';
787                         break;
788                 }
789                 i++;
790         }
791
792         ret = __proc_iter_cmdline(NULL, apppath);
793
794         free(apppath);
795
796         return ret;
797 }
798
799 int _status_app_is_running_from_cache(const char *appid)
800 {
801         const char *path = NULL;
802         GSList *pid_list = NULL;
803         const struct appinfo *ai;
804
805         if (app_running_cache)
806                 pid_list = (GSList *)g_hash_table_lookup(app_running_cache, appid);
807
808         if (pid_list) {
809                 ai = appinfo_find(_saf, appid);
810                 if (ai == NULL)
811                         return -1;
812
813                 path = appinfo_get_value(ai, AIT_EXEC);
814                 if (path == NULL)
815                         return -1;
816
817                 while (pid_list) {
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);
821                         } else {
822                                 _E("is_running garbage, pid: %d", GPOINTER_TO_INT(pid_list->data));
823                                 __remove_running_cache(appid, GPOINTER_TO_INT(pid_list->data));
824                         }
825                         pid_list = pid_list->next;
826                 }
827         }
828
829         return 0;
830 }
831
832 int _status_app_is_running_v2_cached(const char *appid)
833 {
834         int ret = 0;
835
836         ret = _status_app_is_running_from_cache(appid);
837         if (ret > 0)
838                 return ret;
839
840         ret = _status_app_is_running_v2(appid);
841         if (ret > 0) {
842                 _E("is running missing app detected: %s (%d)", appid, ret);
843                 __add_running_cache((char *)appid, ret);
844         }
845
846         return ret;
847 }
848
849 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
850 {
851         app_info_from_db *menu_info;
852         char *r_info;
853         char *pkgname = NULL;
854         char *app_path = NULL;
855
856         r_info = (char *)priv;
857
858         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
859                 goto out;
860         else {
861                 pkgname = _get_pkgname(menu_info);
862                 if (pkgname == NULL)
863                         goto out;
864
865                 app_path = _get_app_path(menu_info);
866                 if (app_path == NULL)
867                         goto out;
868
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);
875         }
876
877  out:
878         if (menu_info != NULL)
879                 _free_app_info_from_db(menu_info);
880         return 0;
881 }
882
883 int _status_send_running_appinfo_v2(int fd)
884 {
885         app_pkt_t *pkt = NULL;
886
887         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
888         if(!pkt) {
889                 _E("malloc fail");
890                 close(fd);
891                 return 0;
892         }
893
894         memset(pkt, 0, AUL_SOCK_MAXBUFF);
895
896         __proc_iter_cmdline(__get_pkginfo, pkt->data);
897
898         pkt->cmd = APP_RUNNING_INFO_RESULT;
899         pkt->len = strlen((char *)pkt->data) + 1;
900
901         if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
902                 if (errno == EPIPE)
903                         _E("send failed due to EPIPE.\n");
904                 _E("send fail to client");
905         }
906
907         if(pkt)
908                 free(pkt);
909
910         close(fd);
911
912         return 0;
913 }
914
915 int _status_get_pkgname_bypid(int pid, char *pkgname, int len)
916 {
917         char *cmdline;
918         app_info_from_db *menu_info;
919
920         cmdline = __proc_get_cmdline_bypid(pid);
921         if (cmdline == NULL)
922                 return -1;
923
924         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
925                 free(cmdline);
926                 return -1;
927         } else {
928                 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
929         }
930
931         free(cmdline);
932         _free_app_info_from_db(menu_info);
933
934         return 0;
935 }
936
937 int _status_get_appid_bypid(int fd, int pid)
938 {
939         app_pkt_t *pkt = NULL;
940         int pgid;
941
942         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
943         if(!pkt) {
944                 _E("malloc fail");
945                 close(fd);
946                 return 0;
947         }
948
949         memset(pkt, 0, AUL_SOCK_MAXBUFF);
950
951         pkt->cmd = APP_GET_INFO_ERROR;
952
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;
956                 goto out;
957         }
958         /* support app launched by shell script*/
959         _D("second chance");
960         pgid = getpgid(pid);
961         if (pgid <= 1)
962                 goto out;
963
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;
967
968  out:
969         pkt->len = strlen((char *)pkt->data) + 1;
970
971         if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
972                 if (errno == EPIPE)
973                         _E("send failed due to EPIPE.\n");
974                 _E("send fail to client");
975         }
976
977         if(pkt)
978                 free(pkt);
979
980         close(fd);
981
982         return 0;
983 }
984
985 static int __get_pkgid_bypid(int pid, char *pkgid, int len)
986 {
987         char *cmdline;
988         app_info_from_db *menu_info;
989
990         cmdline = __proc_get_cmdline_bypid(pid);
991         if (cmdline == NULL)
992                 return -1;
993
994         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
995                 free(cmdline);
996                 return -1;
997         } else
998                 snprintf(pkgid, len, "%s", _get_pkgid(menu_info));
999
1000         free(cmdline);
1001         _free_app_info_from_db(menu_info);
1002
1003         return 0;
1004 }
1005
1006 int _status_get_pkgid_bypid(int fd, int pid)
1007 {
1008         app_pkt_t *pkt = NULL;
1009         int pgid;
1010
1011         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1012         if(!pkt) {
1013                 _E("malloc fail");
1014                 close(fd);
1015                 return 0;
1016         }
1017
1018         memset(pkt, 0, AUL_SOCK_MAXBUFF);
1019
1020         pkt->cmd = APP_GET_INFO_ERROR;
1021
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;
1025                 goto out;
1026         }
1027         /* support app launched by shell script*/
1028         _D("second chance");
1029         pgid = getpgid(pid);
1030         if (pgid <= 1)
1031                 goto out;
1032
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;
1036
1037  out:
1038         pkt->len = strlen((char *)pkt->data) + 1;
1039
1040         if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1041                 if (errno == EPIPE)
1042                         _E("send failed due to EPIPE.\n");
1043                 _E("send fail to client");
1044         }
1045
1046         if(pkt)
1047                 free(pkt);
1048
1049         close(fd);
1050
1051         return 0;
1052 }
1053
1054 int _status_get_cmdline(int fd, int pid)
1055 {
1056         app_pkt_t *pkt = NULL;
1057         int len;
1058         char *cmdline;
1059
1060         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1061         if(!pkt) {
1062                 _E("malloc fail");
1063                 close(fd);
1064                 return 0;
1065         }
1066
1067         memset(pkt, 0, AUL_SOCK_MAXBUFF);
1068
1069         pkt->cmd = APP_GET_INFO_ERROR;
1070
1071         cmdline = __proc_get_cmdline_bypid(pid);
1072         if (cmdline == NULL)
1073                 goto out;
1074         _E("cmdline : %s", cmdline);
1075
1076         strncpy((char *)pkt->data, cmdline, MAX_PACKAGE_STR_SIZE);
1077         _E("pkt->data : %s", pkt->data);
1078         pkt->cmd = APP_GET_INFO_OK;
1079
1080  out:
1081         pkt->len = strlen((char *)pkt->data) + 1;
1082
1083         if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1084                 if (errno == EPIPE)
1085                         _E("send failed due to EPIPE.\n");
1086                 _E("send fail to client");
1087         }
1088
1089         free(cmdline);
1090
1091         if(pkt)
1092                 free(pkt);
1093
1094         close(fd);
1095
1096         return 0;
1097 }
1098
1099
1100 int _status_send_group_info(int fd)
1101 {
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;
1107         char buf[2048];
1108
1109         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
1110         if(!pkt) {
1111                 _E("malloc fail");
1112                 return 0;
1113         }
1114
1115         memset(pkt, 0, AUL_SOCK_MAXBUFF);
1116
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))
1120         {
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);
1127                 }
1128         }
1129
1130         snprintf(buf, sizeof(buf), "\n");
1131         strncat((char *)pkt->data, buf, 2048);
1132
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))
1136         {
1137                 info_t = (app_status_info_t *)iter->data;
1138
1139                 _D("%s(%d)=>", info_t->appid, info_t->pid);
1140                 if(info_t->status != STATUS_VISIBLE) {
1141                                 continue;
1142                 }
1143
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);
1148
1149         }
1150
1151         snprintf(buf, sizeof(buf), "\n");
1152         strncat((char *)pkt->data, buf, 2048);
1153
1154         snprintf(buf, sizeof(buf), "=======================STATUS_BG=============================\n");
1155         strncat((char *)pkt->data, buf, 2048);
1156
1157         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
1158         {
1159                 info_t = (app_status_info_t *)iter->data;
1160
1161                 if(info_t->status != STATUS_BG)
1162                         continue;
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);
1167
1168         }
1169
1170         snprintf(buf, sizeof(buf), "\n");
1171         strncat((char *)pkt->data, buf, 2048);
1172
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))
1176         {
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))
1182                         {
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);
1186                         }
1187                         snprintf(buf, sizeof(buf), "||\n");
1188                         strncat((char *)pkt->data, buf, 2048);
1189                 }
1190         }
1191         snprintf(buf, sizeof(buf), "\n");
1192         strncat((char *)pkt->data, buf, 2048);
1193
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))
1197         {
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))
1203                         {
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);
1207                         }
1208                         snprintf(buf, sizeof(buf), "||\n");
1209                         strncat((char *)pkt->data, buf, 2048);
1210                 }
1211         }
1212         snprintf(buf, sizeof(buf), "\n");
1213         strncat((char *)pkt->data, buf, 2048);
1214
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))
1218         {
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))
1225                         {
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);
1229                         }
1230                         snprintf(buf, sizeof(buf), "||\n");
1231                         strncat((char *)pkt->data, buf, 2048);
1232                 }
1233         }
1234
1235         snprintf(buf, sizeof(buf), "************************************************************\n");
1236         strncat((char *)pkt->data, buf, 2048);
1237
1238         pkt->cmd = APP_GET_GROUP_INFO;
1239         pkt->len = strlen((char *)pkt->data) + 1;
1240
1241         if ((send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
1242                 if (errno == EPIPE)
1243                         _E("send failed due to EPIPE.\n");
1244                 _E("send fail to client");
1245         }
1246
1247         if(pkt)
1248                 free(pkt);
1249
1250         close(fd);
1251
1252         return 0;
1253 }
1254
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)
1257 {
1258         if(appinfo_get_boolean(ai, AIT_TASKMANAGE) && !appinfo_get_boolean(ai, AIT_COOLDOWN) ) {
1259                 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "blocking");
1260         }
1261 }
1262
1263 static void __app_info_iter_release_cb(void *user_data, const char *appid, const struct appinfo *ai)
1264 {
1265         if(appinfo_get_boolean(ai, AIT_TASKMANAGE) && !appinfo_get_boolean(ai, AIT_COOLDOWN) ) {
1266                 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "installed");
1267         }
1268 }
1269
1270 static int __cooldown_cb(const char* status, void *data)
1271 {
1272         GSList *iter = NULL;
1273         app_status_info_t *info_t = NULL;
1274         const struct appinfo *ai;
1275         int dummy;
1276
1277         _D("status %s", status);
1278
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))
1282                 {
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) );
1290                         }
1291                 }
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;
1296         }
1297
1298         return 0;
1299 }
1300
1301 int _status_get_cooldown_status(void)
1302 {
1303         return cooldown_status;
1304 }
1305
1306 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1307
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)
1310 {
1311         const char *component = NULL;
1312         int onboot = 0;
1313
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);
1319                         _start_srv(ai);
1320                 } else {
1321                         _E("service: %s is already running", appid);
1322                 }
1323         }
1324 }
1325
1326 static void __ups_cb(keynode_t *key, void *data)
1327 {
1328         const char *name = NULL;
1329         int ups_mode = 0;
1330
1331         name = vconf_keynode_get_name(key);
1332         if (name == NULL) {
1333                 _E("vconf key value for UPS mode is invalid");
1334                 return;
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);
1340                 }
1341                 else if (ups_mode == SETTING_PSMODE_EMERGENCY) {
1342                         _W("UPS mode is enabled");
1343                 }
1344                 else {
1345                         _W("Current Power Saving mode: %d", ups_mode);
1346                 }
1347         }
1348 }
1349 #endif //_APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1350
1351 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
1352 static int _status_log_init(void)
1353 {
1354         int offset = 0;
1355
1356         log_fd = open(AMD_LOG_FILE, O_CREAT | O_WRONLY, 0644);
1357
1358         if(log_fd < 0)
1359                 return -1;
1360
1361         offset = lseek(log_fd, 0, SEEK_END);
1362         if (offset != 0) {
1363                 log_index = (int)(offset / AMD_LOG_BUFFER_STRING_SIZE);
1364
1365                 if (log_index >= AMD_LOG_BUFFER_SIZE) {
1366                         log_index = 0;
1367                         lseek(log_fd, 0, SEEK_SET);
1368                 }
1369         }
1370
1371         return 0;
1372 }
1373
1374 int _status_log_save(const char *tag, const char *message)
1375 {
1376         int ret = 0;
1377         int offset = 0;
1378         time_t now;
1379         char time_buf[32] = {0,};
1380         char buffer[AMD_LOG_BUFFER_STRING_SIZE] = {0,};
1381
1382         if(log_fd < 0)
1383                 return -1;
1384
1385         time(&now);
1386         ctime_r(&now, time_buf);
1387         if (log_index != 0) {
1388                 offset = lseek(log_fd, 0, SEEK_CUR);
1389         } else {
1390                 offset = lseek(log_fd, 0, SEEK_SET);
1391         }
1392
1393         if (offset == -1)
1394                 _E("error in lseek: %s", strerror(errno));
1395
1396         snprintf(buffer, AMD_LOG_BUFFER_STRING_SIZE, "[%-6d] %-15s %-50s %s", log_index, tag, message, time_buf);
1397
1398         ret = write(log_fd, buffer, strlen(buffer));
1399         if (ret < 0) {
1400                 _E("Cannot write the amd log: %d", ret);
1401                 return -1;
1402         }
1403
1404         if (++log_index >= AMD_LOG_BUFFER_SIZE) {
1405                 log_index = 0;
1406         }
1407
1408         return 0;
1409 }
1410 #endif
1411
1412 int _status_init(struct amdmgr* amd)
1413 {
1414         _saf = amd->af;
1415
1416 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1417         aul_listen_cooldown_signal(__cooldown_cb, NULL);
1418 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
1419
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");
1423         }
1424 #endif
1425
1426 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
1427         _status_log_init();
1428 #endif
1429
1430         return 0;
1431 }
1432
1433 gboolean _status_check_window_ready(void)
1434 {
1435         if (access(WINDOW_READY, R_OK) == 0)
1436                 return true;
1437         else
1438                 return false;
1439 }