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