4f092c902df9042cc54a36cf429facbff131fd39
[platform/core/appfw/aul-1.git] / src / launch.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <dirent.h>
26 #include <glib.h>
27 #include <gio/gio.h>
28 #include <ttrace.h>
29
30 #include <bundle_internal.h>
31
32 #include "app_signal.h"
33 #include "aul.h"
34 #include "aul_api.h"
35 #include "aul_sock.h"
36 #include "perf.h"
37 #include "aul_util.h"
38 #include "launch.h"
39 #include "key.h"
40 #include "aul_app_com.h"
41 #include "aul_error.h"
42
43 #define TEP_ISMOUNT_MAX_RETRY_CNT 20
44
45 static int aul_initialized = 0;
46 static int aul_fd;
47 static void *__window_object = NULL;
48 static void *__bg_object = NULL;
49 static void *__conformant_object = NULL;
50
51 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
52 static void *_aul_data;
53
54 static int app_resume();
55 static int app_terminate();
56 static void __clear_internal_key(bundle *kb);
57 static inline void __set_stime(bundle *kb);
58 static int __app_start_internal(gpointer data);
59 static int __app_launch_local(bundle *b);
60 static int __send_result_to_launchpad(int fd, int res);
61
62 static data_control_provider_handler_fn __dc_handler = NULL;
63 extern  int aul_launch_fini();
64
65 int aul_is_initialized()
66 {
67         return aul_initialized;
68 }
69
70 int __call_aul_handler(aul_type type, bundle *kb)
71 {
72         if (_aul_handler)
73                 _aul_handler(type, kb, _aul_data);
74         return 0;
75 }
76
77 int app_start(bundle *kb)
78 {
79         const char *str = NULL;
80
81         _app_start_res_prepare(kb);
82         __call_aul_handler(AUL_START, kb);
83         /* Handle the DataControl callback */
84         str = bundle_get_val(kb, AUL_K_DATA_CONTROL_TYPE);
85         if (str != NULL && strcmp(str, "CORE") == 0) {
86                 if (__dc_handler != NULL)
87                         __dc_handler(kb, 0, NULL); /* bundle, request_id, data */
88         }
89
90         return 0;
91 }
92
93 static int app_resume()
94 {
95         __call_aul_handler(AUL_RESUME, NULL);
96         return 0;
97 }
98
99 static int app_terminate()
100 {
101         __call_aul_handler(AUL_TERMINATE, NULL);
102         return 0;
103 }
104
105 static int bgapp_terminate(void)
106 {
107         __call_aul_handler(AUL_TERMINATE_BGAPP, NULL);
108         return 0;
109 }
110
111 static int app_pause(void)
112 {
113         __call_aul_handler(AUL_PAUSE, NULL);
114         return 0;
115 }
116
117 static int app_prepare_to_suspend(bundle *kb)
118 {
119         _D("[__SUSPEND__]");
120         __call_aul_handler(AUL_SUSPEND, kb);
121         return 0;
122 }
123
124 static int app_prepare_to_wake(bundle *kb)
125 {
126         _D("[__WAKE__]");
127         __call_aul_handler(AUL_WAKE, kb);
128         return 0;
129 }
130
131 static void app_update_requested(void)
132 {
133         _D("[UPDATE REQUESTED]");
134         __call_aul_handler(AUL_UPDATE_REQUESTED, NULL);
135 }
136
137 static int __send_cmd_for_uid_opt(int pid, uid_t uid, int cmd, bundle *kb, int opt)
138 {
139         int res;
140
141         res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
142         if (res < 0)
143                 res = aul_error_convert(res);
144
145         return res;
146 }
147
148 static int __send_cmd_async_for_uid_opt(int pid, uid_t uid,
149                                         int cmd, bundle *kb, int opt)
150 {
151         int res;
152
153         res = aul_sock_send_bundle(pid, uid, cmd, kb, AUL_SOCK_NOREPLY);
154         if (res < 0)
155                 res = aul_error_convert(res);
156
157         return res;
158 }
159
160 /**
161  * @brief       encode kb and send it to 'pid'
162  * @param[in]   pid             receiver's pid
163  * @param[in]   cmd             message's status (APP_START | APP_RESULT)
164  * @param[in]   kb              data
165  */
166 API int app_send_cmd(int pid, int cmd, bundle *kb)
167 {
168         return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
169 }
170
171 API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
172 {
173         return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_NONE);
174 }
175
176 API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
177 {
178         return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
179 }
180
181 API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
182                                         int cmd, bundle *kb)
183 {
184         return __send_cmd_async_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
185 }
186
187 API int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
188 {
189         return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
190 }
191
192 API int app_send_cmd_to_launchpad(const char *pad_type, uid_t uid, int cmd, bundle *kb)
193 {
194         int fd;
195         int len;
196         int res;
197
198         fd = aul_sock_create_launchpad_client(pad_type, uid);
199         if (fd < 0)
200                 return -1;
201
202         res = aul_sock_send_bundle_with_fd(fd, cmd,
203                         kb, AUL_SOCK_ASYNC);
204         if (res < 0) {
205                 close(fd);
206                 return res;
207         }
208
209 retry_recv:
210         len = recv(fd, &res, sizeof(int), 0);
211         if (len == -1) {
212                 if (errno == EAGAIN) {
213                         _E("recv timeout: %s", strerror(errno));
214                         res = -EAGAIN;
215                 } else if (errno == EINTR) {
216                         _D("recv: %s", strerror(errno));
217                         goto retry_recv;
218                 } else {
219                         _E("recv error: %s", strerror(errno));
220                         res = -ECOMM;
221                 }
222         }
223
224         close(fd);
225
226         return res;
227 }
228
229 static void __clear_internal_key(bundle *kb)
230 {
231         bundle_del(kb, AUL_K_CALLER_PID);
232         bundle_del(kb, AUL_K_APPID);
233         bundle_del(kb, AUL_K_WAIT_RESULT);
234         bundle_del(kb, AUL_K_SEND_RESULT);
235         bundle_del(kb, AUL_K_ARGV0);
236 }
237
238 static inline void __set_stime(bundle *kb)
239 {
240         struct timeval tv;
241         char tmp[MAX_LOCAL_BUFSZ];
242
243         gettimeofday(&tv, NULL);
244         snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
245         bundle_add(kb, AUL_K_STARTTIME, tmp);
246 }
247
248 static int __app_start_internal(gpointer data)
249 {
250         bundle *kb;
251
252         kb = (bundle *) data;
253         app_start(kb);
254         bundle_free(kb);
255
256         return 0;
257 }
258
259 static int __app_launch_local(bundle *b)
260 {
261         if (!aul_is_initialized())
262                 return AUL_R_ENOINIT;
263
264         if (b == NULL)
265                 _E("bundle for APP_START is NULL");
266
267         if (g_idle_add(__app_start_internal, b) > 0)
268                 return AUL_R_OK;
269         else
270                 return AUL_R_ERROR;
271 }
272
273 static int __app_resume_local()
274 {
275         if (!aul_is_initialized())
276                 return AUL_R_ENOINIT;
277
278         app_resume();
279
280         return 0;
281 }
282
283 /**
284  * @brief       start caller with kb
285  * @return      callee's pid
286  */
287 int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
288 {
289         return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
290 }
291
292 int app_request_to_launchpad_for_uid(int cmd, const char *appid, bundle *kb, uid_t uid)
293 {
294         int must_free = 0;
295         int ret = 0;
296         bundle *b;
297         char buf[MAX_PID_STR_BUFSZ];
298
299         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:REQ_TO_PAD");
300         _W("request cmd(%d) : appid(%s), target_uid(%d)", cmd, appid, uid);
301         if (kb == NULL) {
302                 kb = bundle_create();
303                 must_free = 1;
304         } else {
305                 __clear_internal_key(kb);
306         }
307
308         bundle_del(kb, AUL_K_APPID);
309         bundle_add(kb, AUL_K_APPID, appid);
310         __set_stime(kb);
311         snprintf(buf, sizeof(buf), "%d", uid);
312         bundle_del(kb, AUL_K_TARGET_UID);
313         bundle_add(kb, AUL_K_TARGET_UID, buf);
314
315         switch (cmd) {
316         case APP_PAUSE:
317         case APP_PAUSE_BY_PID:
318                 ret = app_send_cmd_with_queue_noreply_for_uid(AUL_UTIL_PID,
319                                 uid, cmd, kb);
320                 break;
321         default:
322                 ret = app_send_cmd_with_queue_for_uid(AUL_UTIL_PID, uid, cmd,
323                                 kb);
324                 break;
325         }
326
327         _W("request cmd(%d) result : %d", cmd, ret);
328         if (ret == AUL_R_LOCAL) {
329                 _E("app_request_to_launchpad : Same Process Send Local");
330
331                 switch (cmd) {
332                 case APP_START:
333                 case APP_START_RES:
334                 case APP_START_ASYNC:
335                 case WIDGET_UPDATE:
336                         b = bundle_dup(kb);
337                         ret = __app_launch_local(b);
338                         break;
339                 case APP_OPEN:
340                 case APP_RESUME:
341                 case APP_RESUME_BY_PID:
342                 case APP_RESUME_BY_PID_ASYNC:
343                         ret = __app_resume_local();
344                         break;
345                 default:
346                         _E("no support packet");
347                 }
348
349         }
350
351         /* cleanup */
352         if (must_free)
353                 bundle_free(kb);
354
355         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
356
357         return ret;
358 }
359
360 static int __send_result_to_launchpad(int fd, int res)
361 {
362         if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
363                 if (errno == EPIPE) {
364                         _E("send failed due to EPIPE.\n");
365                         close(fd);
366                         return -1;
367                 }
368                 _E("send fail to client");
369         }
370         close(fd);
371         return 0;
372 }
373
374 static int widget_get_content(int clifd, bundle *kb)
375 {
376         int ret;
377         int fd = 0;
378         char *widget_id = NULL;
379         char *instance_id = NULL;
380         char *content_info = NULL;
381
382         bundle_get_str(kb, AUL_K_WIDGET_ID, &widget_id);
383         bundle_get_str(kb, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
384
385         ret = aul_sock_recv_reply_sock_fd(clifd, &fd, 1);
386         if (ret < 0) {
387                 _E("failed to recv sock fd");
388                 return ret;
389         }
390
391         if (!widget_id || !instance_id) {
392                 aul_sock_send_raw_with_fd(fd, -EINVAL, 0, 0, AUL_SOCK_NOREPLY);
393                 return 0;
394         }
395
396         __call_aul_handler(AUL_WIDGET_CONTENT, kb);
397
398         bundle_get_str(kb, AUL_K_WIDGET_CONTENT_INFO, &content_info);
399         if (content_info) {
400                 ret = aul_sock_send_raw_with_fd(fd, 0,
401                                 (unsigned char *)content_info,
402                                 strlen(content_info) + 1, AUL_SOCK_NOREPLY);
403         } else {
404                 ret = aul_sock_send_raw_with_fd(fd, -ENOENT,
405                                 NULL, 0, AUL_SOCK_NOREPLY);
406         }
407
408         if (ret < 0)
409                 _E("failed to send content %d (%d)", fd, ret);
410
411         return ret;
412 }
413
414 /**
415  * @brief       caller & callee's sock handler
416  */
417 int aul_sock_handler(int fd)
418 {
419         app_pkt_t *pkt;
420         bundle *kbundle = NULL;
421         int clifd;
422         struct ucred cr;
423
424         const char *pid_str;
425         int pid = -1;
426         int ret;
427
428         if ((pkt = aul_sock_recv_pkt(fd, &clifd, &cr)) == NULL) {
429                 _E("recv error");
430                 return -1;
431         }
432
433         if (pkt->opt & AUL_SOCK_NOREPLY) {
434                 close(clifd);
435         } else if (pkt->cmd != WIDGET_GET_CONTENT) {
436                 ret = __send_result_to_launchpad(clifd, 0);
437                 if (ret < 0) {
438                         free(pkt);
439                         return -1;
440                 }
441         }
442
443         if (pkt->opt & AUL_SOCK_BUNDLE) {
444                 kbundle = bundle_decode(pkt->data, pkt->len);
445                 if (kbundle == NULL)
446                         goto err;
447         }
448
449         switch (pkt->cmd) {
450         case APP_START: /* run in callee */
451         case APP_START_RES:
452         case APP_START_ASYNC:
453                 app_start(kbundle);
454                 break;
455
456         case APP_OPEN:  /* run in callee */
457         case APP_RESUME:
458         case APP_RESUME_BY_PID:
459                 app_resume();
460                 break;
461
462         case APP_TERM_BY_PID:   /* run in callee */
463         case APP_TERM_BY_PID_ASYNC:
464         case APP_TERM_BY_PID_SYNC:
465                 app_terminate();
466                 break;
467
468         case APP_TERM_BGAPP_BY_PID:
469                 bgapp_terminate();
470                 break;
471
472         case APP_TERM_REQ_BY_PID:       /* run in callee */
473                 app_subapp_terminate_request();
474                 break;
475
476         case APP_RESULT:        /* run in caller */
477         case APP_CANCEL:
478                 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
479                 if (pid_str)
480                         pid = atoi(pid_str);
481
482                 app_result(pkt->cmd, kbundle, pid);
483                 break;
484
485         case APP_KEY_EVENT:     /* run in caller */
486                 app_key_event(kbundle);
487                 break;
488
489         case APP_PAUSE_BY_PID:
490                 app_pause();
491                 break;
492         case APP_COM_MESSAGE:
493                 app_com_recv(kbundle);
494                 break;
495         case APP_WAKE:
496                 app_prepare_to_wake(kbundle);
497                 break;
498         case APP_SUSPEND:
499                 app_prepare_to_suspend(kbundle);
500                 break;
501         case APP_STATUS_NOTIFICATION:
502                 app_status_event(kbundle);
503                 break;
504         case WIDGET_GET_CONTENT:
505                 widget_get_content(clifd, kbundle);
506                 break;
507         case UPDATE_APP_SCREEN:
508                 app_update_requested();
509                 break;
510         default:
511                 _E("no support packet");
512         }
513
514         if (kbundle)
515                 bundle_free(kbundle);
516
517         free(pkt);
518         return 0;
519
520 err:
521         free(pkt);
522         return -1;
523 }
524
525 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
526 {
527         int ac = 1;
528
529         char *buf = NULL;
530
531         *kb = bundle_create();
532         if (*kb == NULL)
533                 return AUL_R_ERROR;
534
535         if (argv == NULL)
536                 return AUL_R_OK;
537
538         if ((argv != NULL) && (argv[0] != NULL)) {
539                 buf = strdup(argv[0]);
540                 if (NULL == buf) {
541                         _E("Malloc failed");
542                         return AUL_R_ERROR;
543                 }
544
545                 bundle_add(*kb, AUL_K_ARGV0, buf);
546         }
547         if (buf) {              /*Prevent FIX: ID 38717 */
548                 free(buf);
549                 buf = NULL;
550         }
551
552         while (ac < argc) {
553                 if (ac + 1 == argc) {
554                         if (bundle_add(*kb, argv[ac], "") < 0) {
555                                 _E("bundle add error pos - %d", ac);
556                                 return AUL_R_ECANCELED;
557                         }
558                 } else {
559                         if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
560                                 _E("bundle add error pos - %d", ac);
561                                 return AUL_R_ECANCELED;
562                         }
563                 }
564                 ac = ac + 2;
565         }
566
567         return AUL_R_OK;
568 }
569
570 int aul_register_init_callback(
571         int (*aul_handler) (aul_type type, bundle *, void *), void *data)
572 {
573         /* Save start handler function in static var */
574         _aul_handler = aul_handler;
575         _aul_data = data;
576         return 0;
577 }
578
579 int aul_initialize()
580 {
581         if (aul_initialized)
582                 return AUL_R_ECANCELED;
583
584
585         aul_fd = aul_sock_create_server(getpid(), getuid());
586         if (aul_fd < 0) {
587                 _E("aul_init create sock failed");
588                 return AUL_R_ECOMM;
589         }
590
591         aul_initialized = 1;
592
593         return aul_fd;
594 }
595
596 API void aul_finalize()
597 {
598         aul_launch_fini();
599
600         if (aul_initialized)
601                 close(aul_fd);
602
603         return;
604 }
605
606 API int aul_request_data_control_socket_pair(bundle *kb, int *fd)
607 {
608         bundle *b = kb;
609         int ret;
610         int fds[1] = {0};
611
612         if (!fd)
613                 return AUL_R_EINVAL;
614
615         if (b) {
616                 __clear_internal_key(b);
617         } else {
618                 b = bundle_create();
619                 if (!b)
620                         return AUL_R_ERROR;
621         }
622
623         ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_GET_DC_SOCKET_PAIR, b, AUL_SOCK_ASYNC);
624         if (ret > 0) {
625                 ret = aul_sock_recv_reply_sock_fd(ret, fds, 1);
626                 if (ret == 0)
627                         fd[0] = fds[0];
628         }
629
630         if (kb == NULL)
631                 bundle_free(b);
632
633         return ret;
634 }
635
636 API int aul_request_message_port_socket_pair(int *fd)
637 {
638         int ret;
639         int fds[2] = {0,};
640
641         if (!fd)
642                 return AUL_R_EINVAL;
643
644         ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
645                         APP_GET_MP_SOCKET_PAIR, NULL, 0, AUL_SOCK_ASYNC);
646         if (ret > 0) {
647                 ret = aul_sock_recv_reply_sock_fd(ret, fds, 2);
648                 if (ret == 0) {
649                         fd[0] = fds[0];
650                         fd[1] = fds[1];
651                 }
652         }
653
654         return ret;
655 }
656
657 API int aul_launch_app(const char *appid, bundle *kb)
658 {
659         return aul_launch_app_for_uid(appid, kb, getuid());
660 }
661
662 API int aul_launch_app_for_uid(const char *appid, bundle *kb, uid_t uid)
663 {
664         int ret;
665
666         if (appid == NULL)
667                 return AUL_R_EINVAL;
668
669         ret = app_request_to_launchpad_for_uid(APP_START, appid, kb, uid);
670         return ret;
671 }
672
673 API int aul_open_app(const char *appid)
674 {
675         return aul_open_app_for_uid(appid, getuid());
676 }
677
678 API int aul_open_app_for_uid(const char *appid, uid_t uid)
679 {
680         int ret;
681
682         if (appid == NULL)
683                 return AUL_R_EINVAL;
684
685         ret = app_request_to_launchpad_for_uid(APP_OPEN, appid, NULL, uid);
686         return ret;
687 }
688
689 API int aul_resume_app(const char *appid)
690 {
691         return aul_resume_app_for_uid(appid, getuid());
692 }
693
694 API int aul_resume_app_for_uid(const char *appid, uid_t uid)
695 {
696         int ret;
697
698         if (appid == NULL)
699                 return AUL_R_EINVAL;
700
701         ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, NULL, uid);
702         return ret;
703 }
704
705 API int aul_resume_pid(int pid)
706 {
707         return aul_resume_pid_for_uid(pid, getuid());
708 }
709
710 API int aul_resume_pid_for_uid(int pid, uid_t uid)
711 {
712         char pid_str[MAX_PID_STR_BUFSZ];
713         int ret;
714
715         if (pid <= 0)
716                 return AUL_R_EINVAL;
717
718         snprintf(pid_str, sizeof(pid_str), "%d", pid);
719         ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID,
720                         pid_str, NULL, uid);
721         return ret;
722 }
723
724 API int aul_terminate_pid(int pid)
725 {
726         return aul_terminate_pid_for_uid(pid, getuid());
727 }
728
729 API int aul_terminate_pid_for_uid(int pid, uid_t uid)
730 {
731         char pid_str[MAX_PID_STR_BUFSZ];
732         int ret;
733
734         if (pid <= 0)
735                 return AUL_R_EINVAL;
736
737         snprintf(pid_str, sizeof(pid_str), "%d", pid);
738         ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID,
739                         pid_str, NULL, uid);
740         if (ret == pid)
741                 ret = AUL_R_OK;
742
743         return ret;
744 }
745
746 API int aul_terminate_bgapp_pid(int pid)
747 {
748         char pid_str[MAX_PID_STR_BUFSZ];
749         int ret;
750
751         if (pid <= 0)
752                 return AUL_R_EINVAL;
753
754         snprintf(pid_str, sizeof(pid_str), "%d", pid);
755         ret = app_request_to_launchpad(APP_TERM_BGAPP_BY_PID, pid_str, NULL);
756         if (ret == pid)
757                 ret = AUL_R_OK;
758
759         return ret;
760 }
761
762 API int aul_terminate_pid_without_restart(int pid)
763 {
764         char pid_str[MAX_PID_STR_BUFSZ];
765         int ret;
766
767         if (pid <= 0)
768                 return AUL_R_EINVAL;
769
770         snprintf(pid_str, sizeof(pid_str), "%d", pid);
771         ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART,
772                         pid_str, NULL);
773         return ret;
774 }
775
776 API int aul_terminate_pid_sync_without_restart(int pid)
777 {
778         return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
779 }
780
781 API int aul_terminate_pid_sync_without_restart_for_uid(int pid, uid_t uid)
782 {
783         char pid_str[MAX_PID_STR_BUFSZ];
784         int ret;
785
786         if (pid <= 0)
787                 return AUL_R_EINVAL;
788
789         snprintf(pid_str, sizeof(pid_str), "%d", pid);
790         ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART,
791                         pid_str, NULL, uid);
792         return ret;
793 }
794
795 API int aul_terminate_pid_async(int pid)
796 {
797         return aul_terminate_pid_async_for_uid(pid, getuid());
798 }
799
800 API int aul_terminate_pid_async_for_uid(int pid, uid_t uid)
801 {
802         char pid_str[MAX_PID_STR_BUFSZ];
803         int ret;
804
805         if (pid <= 0)
806                 return AUL_R_EINVAL;
807
808         snprintf(pid_str, sizeof(pid_str), "%d", pid);
809         ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_ASYNC, pid_str,
810                         NULL, uid);
811         return ret;
812 }
813
814 API int aul_kill_pid(int pid)
815 {
816         char pid_str[MAX_PID_STR_BUFSZ];
817         int ret;
818
819         if (pid <= 0)
820                 return AUL_R_EINVAL;
821
822         snprintf(pid_str, sizeof(pid_str), "%d", pid);
823         ret = app_request_to_launchpad(APP_KILL_BY_PID, pid_str, NULL);
824         return ret;
825 }
826
827 API int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler)
828 {
829         __dc_handler = handler;
830         return 0;
831 }
832
833 API int aul_unset_data_control_provider_cb(void)
834 {
835         __dc_handler = NULL;
836         return 0;
837 }
838
839 API void aul_set_preinit_window(void *evas_object)
840 {
841         __window_object = evas_object;
842 }
843
844 API void* aul_get_preinit_window(const char *win_name)
845 {
846         return __window_object;
847 }
848
849 API void aul_set_preinit_background(void *evas_object)
850 {
851         __bg_object = evas_object;
852 }
853
854 API void* aul_get_preinit_background(void)
855 {
856         return __bg_object;
857 }
858
859 API void aul_set_preinit_conformant(void *evas_object)
860 {
861         __conformant_object = evas_object;
862 }
863
864 API void* aul_get_preinit_conformant(void)
865 {
866         return __conformant_object;
867 }
868
869 API int aul_pause_app(const char *appid)
870 {
871         return aul_pause_app_for_uid(appid, getuid());
872 }
873
874 API int aul_pause_app_for_uid(const char *appid, uid_t uid)
875 {
876         int ret;
877
878         if (appid == NULL)
879                 return AUL_R_EINVAL;
880
881         ret = app_request_to_launchpad_for_uid(APP_PAUSE, appid, NULL, uid);
882         return ret;
883 }
884
885 API int aul_pause_pid(int pid)
886 {
887         return aul_pause_pid_for_uid(pid, getuid());
888 }
889
890 API int aul_pause_pid_for_uid(int pid, uid_t uid)
891 {
892         char pid_str[MAX_PID_STR_BUFSZ];
893         int ret;
894
895         if (pid <= 0)
896                 return AUL_R_EINVAL;
897
898         snprintf(pid_str, sizeof(pid_str), "%d", pid);
899         ret = app_request_to_launchpad_for_uid(APP_PAUSE_BY_PID,
900                         pid_str, NULL, uid);
901         return ret;
902 }
903
904 API int aul_reload_appinfo(void)
905 {
906         char pid_str[MAX_PID_STR_BUFSZ];
907
908         snprintf(pid_str, sizeof(pid_str), "%d", getpid());
909
910         return app_request_to_launchpad(AMD_RELOAD_APPINFO, pid_str, NULL);
911 }
912
913 API int aul_is_tep_mount_dbus_done(const char *tep_string)
914 {
915         GError *err = NULL;
916         GDBusConnection *conn;
917         GDBusMessage *msg = NULL;
918         GDBusMessage *reply = NULL;
919         GVariant *body;
920         int ret = AUL_R_ERROR;
921
922         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
923         if (conn == NULL) {
924                 _E("g_bus_get_sync() is failed. %s", err->message);
925                 g_error_free(err);
926                 return AUL_R_ERROR;
927         }
928
929         msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
930                                         TEP_OBJECT_PATH,
931                                         TEP_INTERFACE_NAME,
932                                         TEP_IS_MOUNTED_METHOD);
933         if (msg == NULL) {
934                 _E("g_dbus_message_new_method_call() is failed. %s",
935                                 err->message);
936                 goto end;
937         }
938         g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
939
940         reply = g_dbus_connection_send_message_with_reply_sync(conn,
941                                         msg,
942                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE,
943                                         500,
944                                         NULL,
945                                         NULL,
946                                         &err);
947         if (reply == NULL) {
948                 _E("g_dbus_connection_send_message_with_reply_sync() "
949                                         "is failed. %s", err->message);
950                 goto end;
951         }
952
953         body = g_dbus_message_get_body(reply);
954         if (body == NULL) {
955                 _E("g_dbus_message_get_body() is failed.");
956                 goto end;
957         }
958
959         g_variant_get(body, "(i)", &ret);
960
961 end:
962         if (msg)
963                 g_object_unref(msg);
964         if (reply)
965                 g_object_unref(reply);
966         if (conn)
967                 g_object_unref(conn);
968
969         g_clear_error(&err);
970
971         return ret;
972 }
973
974 API int aul_check_tep_mount(const char *tep_path)
975 {
976         if (tep_path) {
977                 int rv = -1;
978                 int cnt = 0;
979                 while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
980                         rv = aul_is_tep_mount_dbus_done(tep_path);
981                         if (rv == 1)
982                                 break;
983                         usleep(50 * 1000);
984                         cnt++;
985                 }
986                 /* incase after trying 1 sec, not getting mounted then quit */
987                 if (rv != 1) {
988                         _E("Not able to mount within 1 sec");
989                         return -1;
990                 }
991         }
992         return 0;
993 }
994
995 API int aul_add_loader(const char *loader_path, bundle *kb)
996 {
997         return aul_add_loader_for_uid(loader_path, kb, getuid());
998 }
999
1000 API int aul_add_loader_for_uid(const char *loader_path, bundle *kb, uid_t uid)
1001 {
1002         int ret;
1003         bundle *b;
1004         bundle_raw *kb_raw = NULL;
1005         int len;
1006         char buf[MAX_PID_STR_BUFSZ];
1007
1008         if (loader_path == NULL)
1009                 return AUL_R_EINVAL;
1010
1011         b = bundle_create();
1012         if (b == NULL)
1013                 return AUL_R_ERROR;
1014
1015         snprintf(buf, sizeof(buf), "%d", uid);
1016         bundle_add_str(b, AUL_K_TARGET_UID, buf);
1017         bundle_add_str(b, AUL_K_LOADER_PATH, loader_path);
1018
1019         if (kb) {
1020                 ret = bundle_encode(kb, &kb_raw, &len);
1021                 if (ret != BUNDLE_ERROR_NONE) {
1022                         bundle_free(b);
1023                         return AUL_R_EINVAL;
1024                 }
1025
1026                 bundle_add_str(b, AUL_K_LOADER_EXTRA, (const char *)kb_raw);
1027         }
1028
1029         ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_ADD_LOADER, b);
1030         bundle_free(b);
1031         if (kb_raw)
1032                 free(kb_raw);
1033
1034         return ret;
1035 }
1036
1037 API int aul_remove_loader(int loader_id)
1038 {
1039         return aul_remove_loader_for_uid(loader_id, getuid());
1040 }
1041
1042 API int aul_remove_loader_for_uid(int loader_id, uid_t uid)
1043 {
1044         char buf[MAX_PID_STR_BUFSZ];
1045         int ret;
1046         bundle *b;
1047
1048         if (loader_id <= 0)
1049                 return AUL_R_EINVAL;
1050
1051         b = bundle_create();
1052         if (b == NULL) {
1053                 _E("out of memory");
1054                 return AUL_R_ERROR;
1055         }
1056
1057         snprintf(buf, sizeof(buf), "%d", loader_id);
1058         bundle_add_str(b, AUL_K_LOADER_ID, buf);
1059         snprintf(buf, sizeof(buf), "%d", uid);
1060         bundle_add_str(b, AUL_K_TARGET_UID, buf);
1061
1062         ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_REMOVE_LOADER, b);
1063         bundle_free(b);
1064
1065         return ret;
1066 }
1067
1068 API int aul_app_register_pid(const char *appid, int pid)
1069 {
1070         char pid_str[MAX_PID_STR_BUFSZ];
1071         int ret;
1072         bundle *b;
1073
1074         if (!appid || pid <= 0)
1075                 return AUL_R_EINVAL;
1076
1077         b = bundle_create();
1078         if (b == NULL) {
1079                 _E("out of memory");
1080                 return AUL_R_ERROR;
1081         }
1082
1083         bundle_add_str(b, AUL_K_APPID, appid);
1084         snprintf(pid_str, sizeof(pid_str), "%d", pid);
1085         bundle_add_str(b, AUL_K_PID, pid_str);
1086
1087         ret = app_send_cmd_with_noreply(AUL_UTIL_PID, APP_REGISTER_PID, b);
1088         bundle_free(b);
1089
1090         return ret;
1091 }
1092
1093 API int aul_launch_app_async(const char *appid, bundle *kb)
1094 {
1095         return aul_launch_app_async_for_uid(appid, kb, getuid());
1096 }
1097
1098 API int aul_launch_app_async_for_uid(const char *appid, bundle *kb, uid_t uid)
1099 {
1100         int ret;
1101
1102         if (appid == NULL)
1103                 return AUL_R_EINVAL;
1104
1105         ret = app_request_to_launchpad_for_uid(APP_START_ASYNC, appid, kb, uid);
1106         return ret;
1107 }
1108
1109 API int aul_prepare_candidate_process(void)
1110 {
1111         unsigned char dummy[1] = { 0 };
1112
1113         return aul_sock_send_raw(AUL_UTIL_PID, getuid(),
1114                         APP_PREPARE_CANDIDATE_PROCESS, dummy, 0, AUL_SOCK_NONE);
1115 }
1116
1117 API int aul_terminate_pid_sync(int pid)
1118 {
1119         return aul_terminate_pid_sync_for_uid(pid, getuid());
1120 }
1121
1122 API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid)
1123 {
1124         char pid_str[MAX_PID_STR_BUFSZ];
1125         int ret;
1126
1127         if (pid <= 0)
1128                 return AUL_R_EINVAL;
1129
1130         snprintf(pid_str, sizeof(pid_str), "%d", pid);
1131         ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC, pid_str,
1132                         NULL, uid);
1133         return ret;
1134 }
1135
1136 API int aul_resume_pid_async(int pid)
1137 {
1138         return aul_resume_pid_async_for_uid(pid, getuid());
1139 }
1140
1141 API int aul_resume_pid_async_for_uid(int pid, uid_t uid)
1142 {
1143         char pid_str[MAX_PID_STR_BUFSZ];
1144         int ret;
1145
1146         if (pid <= 0)
1147                 return AUL_R_EINVAL;
1148
1149         snprintf(pid_str, sizeof(pid_str), "%d", pid);
1150         ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID_ASYNC,
1151                         pid_str, NULL, uid);
1152         return ret;
1153 }
1154
1155 API int aul_resume_app_by_instance_id(const char *appid,
1156                 const char *instance_id)
1157 {
1158         return aul_resume_app_by_instance_id_for_uid(appid,
1159                         instance_id, getuid());
1160 }
1161
1162 API int aul_resume_app_by_instance_id_for_uid(const char *appid,
1163                 const char *instance_id, uid_t uid)
1164 {
1165         int ret;
1166         bundle *b;
1167
1168         if (appid == NULL || instance_id == NULL) {
1169                 _E("Invalid parameter");
1170                 return AUL_R_EINVAL;
1171         }
1172
1173         b = bundle_create();
1174         if (b == NULL) {
1175                 _E("Out of memory");
1176                 return AUL_R_EINVAL;
1177         }
1178
1179         ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
1180         if (ret != BUNDLE_ERROR_NONE) {
1181                 _E("Failed to add instance id(%s)", instance_id);
1182                 bundle_free(b);
1183                 return AUL_R_ERROR;
1184         }
1185
1186         ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, b, uid);
1187         bundle_free(b);
1188
1189         return ret;
1190 }