summaryrefslogtreecommitdiff
path: root/src/launch.c
blob: ccfc88371c1806ef2fcdc59dfe3a147754ff1add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
/*
 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <glib.h>
#include <gio/gio.h>
#include <ttrace.h>

#include <bundle_internal.h>

#include "app_signal.h"
#include "aul.h"
#include "aul_api.h"
#include "aul_sock.h"
#include "aul_util.h"
#include "launch.h"
#include "key.h"
#include "aul_app_com.h"
#include "aul_error.h"

#define TEP_ISMOUNT_MAX_RETRY_CNT 20

static int aul_initialized = 0;
static int aul_fd;
static void *__window_object = NULL;
static void *__bg_object = NULL;
static void *__conformant_object = NULL;

static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
static void *_aul_data;

static int app_resume();
static int app_terminate();
static void __clear_internal_key(bundle *kb);
static inline void __set_stime(bundle *kb);
static int __app_start_internal(gpointer data);
static int __app_launch_local(bundle *b);
static int __send_result_to_launchpad(int fd, int res);

static data_control_provider_handler_fn __dc_handler = NULL;
extern  int aul_launch_fini();

int aul_is_initialized()
{
	return aul_initialized;
}

int __call_aul_handler(aul_type type, bundle *kb)
{
	if (_aul_handler)
		_aul_handler(type, kb, _aul_data);
	return 0;
}

int app_start(bundle *kb)
{
	const char *str = NULL;

	_app_start_res_prepare(kb);
	__call_aul_handler(AUL_START, kb);
	/* Handle the DataControl callback */
	str = bundle_get_val(kb, AUL_K_DATA_CONTROL_TYPE);
	if (str != NULL && strcmp(str, "CORE") == 0) {
		if (__dc_handler != NULL)
			__dc_handler(kb, 0, NULL); /* bundle, request_id, data */
	}

	return 0;
}

static int app_resume()
{
	__call_aul_handler(AUL_RESUME, NULL);
	return 0;
}

static int app_terminate()
{
	__call_aul_handler(AUL_TERMINATE, NULL);
	return 0;
}

static int bgapp_terminate(void)
{
	__call_aul_handler(AUL_TERMINATE_BGAPP, NULL);
	return 0;
}

static int app_pause(void)
{
	__call_aul_handler(AUL_PAUSE, NULL);
	return 0;
}

static int app_prepare_to_suspend(bundle *kb)
{
	_D("[__SUSPEND__]");
	__call_aul_handler(AUL_SUSPEND, kb);
	return 0;
}

static int app_prepare_to_wake(bundle *kb)
{
	_D("[__WAKE__]");
	__call_aul_handler(AUL_WAKE, kb);
	return 0;
}

static void app_update_requested(void)
{
	_D("[UPDATE REQUESTED]");
	__call_aul_handler(AUL_UPDATE_REQUESTED, NULL);
}

static int __send_cmd_for_uid_opt(int pid, uid_t uid, int cmd, bundle *kb, int opt)
{
	int res;

	res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
	if (res < 0)
		res = aul_error_convert(res);

	return res;
}

static int __send_cmd_async_for_uid_opt(int pid, uid_t uid,
					int cmd, bundle *kb, int opt)
{
	int res;

	res = aul_sock_send_bundle(pid, uid, cmd, kb, AUL_SOCK_NOREPLY);
	if (res < 0)
		res = aul_error_convert(res);

	return res;
}

/**
 * @brief	encode kb and send it to 'pid'
 * @param[in]	pid		receiver's pid
 * @param[in]	cmd		message's status (APP_START | APP_RESULT)
 * @param[in]	kb		data
 */
API int app_send_cmd(int pid, int cmd, bundle *kb)
{
	return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
}

API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
{
	return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_NONE);
}

API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
{
	return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
}

API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
					int cmd, bundle *kb)
{
	return __send_cmd_async_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
}

API int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
{
	return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
}

API int app_send_cmd_to_launchpad(const char *pad_type, uid_t uid, int cmd, bundle *kb)
{
	int fd;
	int len;
	int res;
	char buf[1024];

	fd = aul_sock_create_launchpad_client(pad_type, uid);
	if (fd < 0)
		return -1;

	res = aul_sock_send_bundle_with_fd(fd, cmd,
			kb, AUL_SOCK_ASYNC);
	if (res < 0) {
		close(fd);
		return res;
	}

retry_recv:
	len = recv(fd, &res, sizeof(int), 0);
	if (len == -1) {
		if (errno == EAGAIN) {
			_E("recv timeout: %d(%s)",
					errno,
					strerror_r(errno, buf, sizeof(buf)));
			res = -EAGAIN;
		} else if (errno == EINTR) {
			_D("recv: %d(%s)",
					errno,
					strerror_r(errno, buf, sizeof(buf)));
			goto retry_recv;
		} else {
			_E("recv error: %d(%s)",
					errno,
					strerror_r(errno, buf, sizeof(buf)));
			res = -ECOMM;
		}
	}

	close(fd);

	return res;
}

static void __clear_internal_key(bundle *kb)
{
	bundle_del(kb, AUL_K_CALLER_PID);
	bundle_del(kb, AUL_K_APPID);
	bundle_del(kb, AUL_K_WAIT_RESULT);
	bundle_del(kb, AUL_K_SEND_RESULT);
	bundle_del(kb, AUL_K_ARGV0);
}

static inline void __set_stime(bundle *kb)
{
	struct timeval tv;
	char tmp[MAX_LOCAL_BUFSZ];

	gettimeofday(&tv, NULL);
	snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
	bundle_del(kb, AUL_K_STARTTIME);
	bundle_add(kb, AUL_K_STARTTIME, tmp);
}

static int __app_start_internal(gpointer data)
{
	bundle *kb;

	kb = (bundle *) data;
	app_start(kb);
	bundle_free(kb);

	return 0;
}

static int __app_launch_local(bundle *b)
{
	if (!aul_is_initialized())
		return AUL_R_ENOINIT;

	if (b == NULL)
		_E("bundle for APP_START is NULL");

	if (g_idle_add(__app_start_internal, b) > 0)
		return AUL_R_OK;
	else
		return AUL_R_ERROR;
}

static int __app_resume_local()
{
	if (!aul_is_initialized())
		return AUL_R_ENOINIT;

	app_resume();

	return 0;
}

/**
 * @brief	start caller with kb
 * @return	callee's pid
 */
int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
{
	return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
}

int app_request_to_launchpad_for_uid(int cmd, const char *appid, bundle *kb, uid_t uid)
{
	int must_free = 0;
	int ret = 0;
	bundle *b;
	char buf[MAX_PID_STR_BUFSZ];

	traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:REQ_TO_PAD");
	_W("request cmd(%d) : appid(%s), target_uid(%d)", cmd, appid, uid);
	if (kb == NULL) {
		kb = bundle_create();
		must_free = 1;
	} else {
		__clear_internal_key(kb);
	}

	bundle_del(kb, AUL_K_APPID);
	bundle_add(kb, AUL_K_APPID, appid);
	__set_stime(kb);
	snprintf(buf, sizeof(buf), "%d", uid);
	bundle_del(kb, AUL_K_TARGET_UID);
	bundle_add(kb, AUL_K_TARGET_UID, buf);

	switch (cmd) {
	case APP_PAUSE:
	case APP_PAUSE_BY_PID:
		ret = app_send_cmd_with_queue_noreply_for_uid(AUL_UTIL_PID,
				uid, cmd, kb);
		break;
	default:
		ret = app_send_cmd_with_queue_for_uid(AUL_UTIL_PID, uid, cmd,
				kb);
		break;
	}

	_W("request cmd(%d) result : %d", cmd, ret);
	if (ret == AUL_R_LOCAL) {
		_E("app_request_to_launchpad : Same Process Send Local");

		switch (cmd) {
		case APP_START:
		case APP_START_RES:
		case APP_START_ASYNC:
		case WIDGET_UPDATE:
		case APP_START_RES_ASYNC:
			b = bundle_dup(kb);
			ret = __app_launch_local(b);
			break;
		case APP_OPEN:
		case APP_RESUME:
		case APP_RESUME_BY_PID:
		case APP_RESUME_BY_PID_ASYNC:
			ret = __app_resume_local();
			break;
		default:
			_E("no support packet");
		}

	}

	/* cleanup */
	if (must_free)
		bundle_free(kb);

	traceEnd(TTRACE_TAG_APPLICATION_MANAGER);

	return ret;
}

static int __send_result_to_launchpad(int fd, int res)
{
	if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
		if (errno == EPIPE) {
			_E("send failed due to EPIPE.");
			close(fd);
			return -1;
		}
		_E("send fail to client");
	}
	close(fd);
	return 0;
}

static int widget_get_content(int clifd, bundle *kb)
{
	int ret;
	int fd[2] = { 0, };
	char *widget_id = NULL;
	char *instance_id = NULL;
	char *content_info = NULL;

	bundle_get_str(kb, AUL_K_WIDGET_ID, &widget_id);
	bundle_get_str(kb, AUL_K_WIDGET_INSTANCE_ID, &instance_id);

	ret = aul_sock_recv_reply_sock_fd(clifd, &fd, 1);
	if (ret < 0) {
		_E("failed to recv sock fd");
		return ret;
	}

	if (!widget_id || !instance_id) {
		aul_sock_send_raw_with_fd(fd[0], -EINVAL, 0, 0, AUL_SOCK_NOREPLY);
		return 0;
	}

	__call_aul_handler(AUL_WIDGET_CONTENT, kb);

	bundle_get_str(kb, AUL_K_WIDGET_CONTENT_INFO, &content_info);
	if (content_info) {
		ret = aul_sock_send_raw_with_fd(fd[0], 0,
				(unsigned char *)content_info,
				strlen(content_info) + 1, AUL_SOCK_NOREPLY);
	} else {
		ret = aul_sock_send_raw_with_fd(fd[0], -ENOENT,
				NULL, 0, AUL_SOCK_NOREPLY);
	}

	if (ret < 0)
		_E("failed to send content %d (%d)", fd[0], ret);

	return ret;
}

/**
 * @brief	caller & callee's sock handler
 */
int aul_sock_handler(int fd)
{
	app_pkt_t *pkt;
	bundle *kbundle = NULL;
	int clifd;
	struct ucred cr;

	const char *pid_str;
	int pid = -1;
	int ret;

	if ((pkt = aul_sock_recv_pkt(fd, &clifd, &cr)) == NULL) {
		_E("recv error");
		return -1;
	}

	if (pkt->cmd != WIDGET_GET_CONTENT) {
		if (pkt->opt & AUL_SOCK_NOREPLY) {
			close(clifd);
		} else {
			ret = __send_result_to_launchpad(clifd, 0);
			if (ret < 0) {
				free(pkt);
				return -1;
			}
		}
	}

	if (pkt->opt & AUL_SOCK_BUNDLE) {
		kbundle = bundle_decode(pkt->data, pkt->len);
		if (kbundle == NULL)
			goto err;
	}

	switch (pkt->cmd) {
	case APP_START:	/* run in callee */
	case APP_START_RES:
	case APP_START_ASYNC:
	case APP_START_RES_ASYNC:
		app_start(kbundle);
		break;

	case APP_OPEN:	/* run in callee */
	case APP_RESUME:
	case APP_RESUME_BY_PID:
		app_resume();
		break;

	case APP_TERM_BY_PID:	/* run in callee */
	case APP_TERM_BY_PID_ASYNC:
	case APP_TERM_BY_PID_SYNC:
		app_terminate();
		break;

	case APP_TERM_BGAPP_BY_PID:
		bgapp_terminate();
		break;

	case APP_TERM_REQ_BY_PID:	/* run in callee */
		app_subapp_terminate_request();
		break;

	case APP_RESULT:	/* run in caller */
	case APP_CANCEL:
		pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
		if (pid_str)
			pid = atoi(pid_str);

		app_result(pkt->cmd, kbundle, pid);
		break;

	case APP_KEY_EVENT:	/* run in caller */
		app_key_event(kbundle);
		break;

	case APP_PAUSE_BY_PID:
		app_pause();
		break;
	case APP_COM_MESSAGE:
		app_com_recv(kbundle);
		break;
	case APP_WAKE:
		app_prepare_to_wake(kbundle);
		break;
	case APP_SUSPEND:
		app_prepare_to_suspend(kbundle);
		break;
	case WIDGET_GET_CONTENT:
		widget_get_content(clifd, kbundle);
		break;
	case APP_UPDATE_REQUESTED:
		app_update_requested();
		break;
	default:
		_E("no support packet");
	}

	if (kbundle)
		bundle_free(kbundle);

	free(pkt);
	return 0;

err:
	free(pkt);
	return -1;
}

int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
{
	int ac = 1;

	char *buf = NULL;

	*kb = bundle_create();
	if (*kb == NULL)
		return AUL_R_ERROR;

	if (argv == NULL)
		return AUL_R_OK;

	if ((argv != NULL) && (argv[0] != NULL)) {
		buf = strdup(argv[0]);
		if (NULL == buf) {
			_E("Malloc failed");
			return AUL_R_ERROR;
		}

		bundle_add(*kb, AUL_K_ARGV0, buf);
	}
	if (buf) {		/*Prevent FIX: ID 38717 */
		free(buf);
		buf = NULL;
	}

	while (ac < argc) {
		if (ac + 1 == argc) {
			if (bundle_add(*kb, argv[ac], "") < 0) {
				_E("bundle add error pos - %d", ac);
				return AUL_R_ECANCELED;
			}
		} else {
			if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
				_E("bundle add error pos - %d", ac);
				return AUL_R_ECANCELED;
			}
		}
		ac = ac + 2;
	}

	return AUL_R_OK;
}

int aul_register_init_callback(
	int (*aul_handler) (aul_type type, bundle *, void *), void *data)
{
	/* Save start handler function in static var */
	_aul_handler = aul_handler;
	_aul_data = data;
	return 0;
}

static int __get_preinit_fd(void)
{
	int fd = -1;
	const char *listen_fd;

	listen_fd = getenv("AUL_LISTEN_FD");
	if (listen_fd) {
		if (isdigit(*listen_fd))
			fd = atoi(listen_fd);
		unsetenv("AUL_LISTEN_FD");
	}

	return fd;
}

int aul_initialize()
{
	int flag;

	if (aul_initialized)
		return AUL_R_ECANCELED;

	aul_fd = __get_preinit_fd();
	if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
		flag = fcntl(aul_fd, F_GETFD);
		flag |= FD_CLOEXEC;
		(void)fcntl(aul_fd, F_SETFD, flag);
	} else {
		_W("Failed to get preinit fd");
		aul_fd = aul_sock_create_server(getpid(), getuid());
		if (aul_fd < 0) {
			_E("aul_init create sock failed");
			return AUL_R_ECOMM;
		}
	}
	aul_notify_start();

	aul_initialized = 1;

	return aul_fd;
}

API void aul_finalize()
{
	aul_launch_fini();

	if (aul_initialized) {
		aul_sock_destroy_server(aul_fd);
		aul_fd = -1;
	}

	return;
}

API int aul_request_data_control_socket_pair(bundle *kb, int *fd)
{
	bundle *b = kb;
	int ret;
	int clifd;
	int fds[2] = { 0, };

	if (!fd)
		return AUL_R_EINVAL;

	if (b) {
		__clear_internal_key(b);
	} else {
		b = bundle_create();
		if (!b)
			return AUL_R_ERROR;
	}

	clifd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_GET_DC_SOCKET_PAIR, b, AUL_SOCK_ASYNC);
	if (kb == NULL)
		bundle_free(b);

	if (clifd > 0) {
		ret = aul_sock_recv_result_with_fd(clifd);
		if (ret < 0) {
			close(clifd);
			if (ret == -EILLEGALACCESS) {
				_E("Illegal access in datacontrol socket pair request");
				return AUL_R_EILLACC;
			}
			return ret;
		}

		ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
		if (ret == 0)
			fd[0] = fds[0];
	} else {
		return AUL_R_ERROR;
	}

	return ret;
}

API int aul_request_message_port_socket_pair(int *fd)
{
	int ret;
	int fds[2] = {0,};

	if (!fd)
		return AUL_R_EINVAL;

	ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
			APP_GET_MP_SOCKET_PAIR, NULL, 0, AUL_SOCK_ASYNC);
	if (ret > 0) {
		ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
		if (ret == 0) {
			fd[0] = fds[0];
			fd[1] = fds[1];
		}
	}

	return ret;
}

API int aul_launch_app(const char *appid, bundle *kb)
{
	return aul_launch_app_for_uid(appid, kb, getuid());
}

API int aul_launch_app_for_uid(const char *appid, bundle *kb, uid_t uid)
{
	int ret;

	if (appid == NULL)
		return AUL_R_EINVAL;

	ret = app_request_to_launchpad_for_uid(APP_START, appid, kb, uid);
	return ret;
}

API int aul_open_app(const char *appid)
{
	return aul_open_app_for_uid(appid, getuid());
}

API int aul_open_app_for_uid(const char *appid, uid_t uid)
{
	int ret;

	if (appid == NULL)
		return AUL_R_EINVAL;

	ret = app_request_to_launchpad_for_uid(APP_OPEN, appid, NULL, uid);
	return ret;
}

API int aul_resume_app(const char *appid)
{
	return aul_resume_app_for_uid(appid, getuid());
}

API int aul_resume_app_for_uid(const char *appid, uid_t uid)
{
	int ret;

	if (appid == NULL)
		return AUL_R_EINVAL;

	ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, NULL, uid);
	return ret;
}

API int aul_resume_pid(int pid)
{
	return aul_resume_pid_for_uid(pid, getuid());
}

API int aul_resume_pid_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID,
			pid_str, NULL, uid);
	return ret;
}

API int aul_terminate_pid(int pid)
{
	return aul_terminate_pid_for_uid(pid, getuid());
}

API int aul_terminate_pid_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID,
			pid_str, NULL, uid);
	if (ret == pid)
		ret = AUL_R_OK;

	return ret;
}

API int aul_terminate_bgapp_pid(int pid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad(APP_TERM_BGAPP_BY_PID, pid_str, NULL);
	if (ret == pid)
		ret = AUL_R_OK;

	return ret;
}

API int aul_terminate_pid_without_restart(int pid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART,
			pid_str, NULL);
	return ret;
}

API int aul_terminate_pid_sync_without_restart(int pid)
{
	return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
}

API int aul_terminate_pid_sync_without_restart_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART,
			pid_str, NULL, uid);
	return ret;
}

API int aul_terminate_pid_async(int pid)
{
	return aul_terminate_pid_async_for_uid(pid, getuid());
}

API int aul_terminate_pid_async_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_ASYNC, pid_str,
			NULL, uid);
	return ret;
}

API int aul_kill_pid(int pid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad(APP_KILL_BY_PID, pid_str, NULL);
	return ret;
}

API int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler)
{
	__dc_handler = handler;
	return 0;
}

API int aul_unset_data_control_provider_cb(void)
{
	__dc_handler = NULL;
	return 0;
}

API void aul_set_preinit_window(void *evas_object)
{
	__window_object = evas_object;
}

API void* aul_get_preinit_window(const char *win_name)
{
	return __window_object;
}

API void aul_set_preinit_background(void *evas_object)
{
	__bg_object = evas_object;
}

API void* aul_get_preinit_background(void)
{
	return __bg_object;
}

API void aul_set_preinit_conformant(void *evas_object)
{
	__conformant_object = evas_object;
}

API void* aul_get_preinit_conformant(void)
{
	return __conformant_object;
}

API int aul_pause_app(const char *appid)
{
	return aul_pause_app_for_uid(appid, getuid());
}

API int aul_pause_app_for_uid(const char *appid, uid_t uid)
{
	int ret;

	if (appid == NULL)
		return AUL_R_EINVAL;

	ret = app_request_to_launchpad_for_uid(APP_PAUSE, appid, NULL, uid);
	return ret;
}

API int aul_pause_pid(int pid)
{
	return aul_pause_pid_for_uid(pid, getuid());
}

API int aul_pause_pid_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_PAUSE_BY_PID,
			pid_str, NULL, uid);
	return ret;
}

API int aul_reload_appinfo(void)
{
	char pid_str[MAX_PID_STR_BUFSZ];

	snprintf(pid_str, sizeof(pid_str), "%d", getpid());

	return app_request_to_launchpad(AMD_RELOAD_APPINFO, pid_str, NULL);
}

API int aul_is_tep_mount_dbus_done(const char *tep_string)
{
	GError *err = NULL;
	GDBusConnection *conn;
	GDBusMessage *msg = NULL;
	GDBusMessage *reply = NULL;
	GVariant *body;
	int ret = AUL_R_ERROR;

	conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
	if (conn == NULL) {
		_E("g_bus_get_sync() is failed. %s", err->message);
		g_error_free(err);
		return AUL_R_ERROR;
	}

	msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
					TEP_OBJECT_PATH,
					TEP_INTERFACE_NAME,
					TEP_IS_MOUNTED_METHOD);
	if (msg == NULL) {
		_E("g_dbus_message_new_method_call() is failed. %s",
				err->message);
		goto end;
	}
	g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));

	reply = g_dbus_connection_send_message_with_reply_sync(conn,
					msg,
					G_DBUS_SEND_MESSAGE_FLAGS_NONE,
					500,
					NULL,
					NULL,
					&err);
	if (reply == NULL) {
		_E("g_dbus_connection_send_message_with_reply_sync() "
					"is failed. %s", err->message);
		goto end;
	}

	body = g_dbus_message_get_body(reply);
	if (body == NULL) {
		_E("g_dbus_message_get_body() is failed.");
		goto end;
	}

	g_variant_get(body, "(i)", &ret);

end:
	if (msg)
		g_object_unref(msg);
	if (reply)
		g_object_unref(reply);
	if (conn)
		g_object_unref(conn);

	g_clear_error(&err);

	return ret;
}

API int aul_check_tep_mount(const char *tep_path)
{
	if (tep_path) {
		int rv = -1;
		int cnt = 0;
		while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
			rv = aul_is_tep_mount_dbus_done(tep_path);
			if (rv == 1)
				break;
			usleep(50 * 1000);
			cnt++;
		}
		/* incase after trying 1 sec, not getting mounted then quit */
		if (rv != 1) {
			_E("Not able to mount within 1 sec");
			return -1;
		}
	}
	return 0;
}

API int aul_add_loader(const char *loader_path, bundle *kb)
{
	return aul_add_loader_for_uid(loader_path, kb, getuid());
}

API int aul_add_loader_for_uid(const char *loader_path, bundle *kb, uid_t uid)
{
	int ret;
	bundle *b;
	bundle_raw *kb_raw = NULL;
	int len;
	char buf[MAX_PID_STR_BUFSZ];

	if (loader_path == NULL)
		return AUL_R_EINVAL;

	b = bundle_create();
	if (b == NULL)
		return AUL_R_ERROR;

	snprintf(buf, sizeof(buf), "%d", uid);
	bundle_add_str(b, AUL_K_TARGET_UID, buf);
	bundle_add_str(b, AUL_K_LOADER_PATH, loader_path);

	if (kb) {
		ret = bundle_encode(kb, &kb_raw, &len);
		if (ret != BUNDLE_ERROR_NONE) {
			bundle_free(b);
			return AUL_R_EINVAL;
		}

		bundle_add_str(b, AUL_K_LOADER_EXTRA, (const char *)kb_raw);
	}

	ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_ADD_LOADER, b);
	bundle_free(b);
	if (kb_raw)
		free(kb_raw);

	return ret;
}

API int aul_remove_loader(int loader_id)
{
	return aul_remove_loader_for_uid(loader_id, getuid());
}

API int aul_remove_loader_for_uid(int loader_id, uid_t uid)
{
	char buf[MAX_PID_STR_BUFSZ];
	int ret;
	bundle *b;

	if (loader_id <= 0)
		return AUL_R_EINVAL;

	b = bundle_create();
	if (b == NULL) {
		_E("out of memory");
		return AUL_R_ERROR;
	}

	snprintf(buf, sizeof(buf), "%d", loader_id);
	bundle_add_str(b, AUL_K_LOADER_ID, buf);
	snprintf(buf, sizeof(buf), "%d", uid);
	bundle_add_str(b, AUL_K_TARGET_UID, buf);

	ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_REMOVE_LOADER, b);
	bundle_free(b);

	return ret;
}

API int aul_app_register_pid(const char *appid, int pid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;
	bundle *b;

	if (!appid || pid <= 0)
		return AUL_R_EINVAL;

	b = bundle_create();
	if (b == NULL) {
		_E("out of memory");
		return AUL_R_ERROR;
	}

	bundle_add_str(b, AUL_K_APPID, appid);
	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	bundle_add_str(b, AUL_K_PID, pid_str);

	ret = app_send_cmd_with_noreply(AUL_UTIL_PID, APP_REGISTER_PID, b);
	bundle_free(b);

	return ret;
}

API int aul_launch_app_async(const char *appid, bundle *kb)
{
	return aul_launch_app_async_for_uid(appid, kb, getuid());
}

API int aul_launch_app_async_for_uid(const char *appid, bundle *kb, uid_t uid)
{
	int ret;

	if (appid == NULL)
		return AUL_R_EINVAL;

	ret = app_request_to_launchpad_for_uid(APP_START_ASYNC, appid, kb, uid);
	return ret;
}

API int aul_prepare_candidate_process(void)
{
	unsigned char dummy[1] = { 0 };

	return aul_sock_send_raw(AUL_UTIL_PID, getuid(),
			APP_PREPARE_CANDIDATE_PROCESS, dummy, 0, AUL_SOCK_NONE);
}

API int aul_terminate_pid_sync(int pid)
{
	return aul_terminate_pid_sync_for_uid(pid, getuid());
}

API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC, pid_str,
			NULL, uid);
	return ret;
}

API int aul_resume_pid_async(int pid)
{
	return aul_resume_pid_async_for_uid(pid, getuid());
}

API int aul_resume_pid_async_for_uid(int pid, uid_t uid)
{
	char pid_str[MAX_PID_STR_BUFSZ];
	int ret;

	if (pid <= 0)
		return AUL_R_EINVAL;

	snprintf(pid_str, sizeof(pid_str), "%d", pid);
	ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID_ASYNC,
			pid_str, NULL, uid);
	return ret;
}

API int aul_resume_app_by_instance_id(const char *appid,
		const char *instance_id)
{
	return aul_resume_app_by_instance_id_for_uid(appid,
			instance_id, getuid());
}

API int aul_resume_app_by_instance_id_for_uid(const char *appid,
		const char *instance_id, uid_t uid)
{
	int ret;
	bundle *b;

	if (appid == NULL || instance_id == NULL) {
		_E("Invalid parameter");
		return AUL_R_EINVAL;
	}

	b = bundle_create();
	if (b == NULL) {
		_E("Out of memory");
		return AUL_R_EINVAL;
	}

	ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
	if (ret != BUNDLE_ERROR_NONE) {
		_E("Failed to add instance id(%s)", instance_id);
		bundle_free(b);
		return AUL_R_ERROR;
	}

	ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, b, uid);
	bundle_free(b);

	return ret;
}