Set the cache even though app-selector is resolved
[platform/core/appfw/aul-1.git] / src / service.c
1 /*
2  * Copyright (c) 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 <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <bundle.h>
24 #include <bundle_internal.h>
25 #include <glib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <dlfcn.h>
29 #include <iniparser.h>
30 #include <pkgmgr-info.h>
31
32 #include "aul.h"
33 #include "aul_api.h"
34 #include "aul_svc.h"
35 #include "aul_sock.h"
36 #include "aul_svc_db.h"
37 #include "aul_util.h"
38 #include "aul_svc_priv_key.h"
39 #include "launch.h"
40
41 #define MAX_CHECKSUM_BUF        2048
42
43 /* callback handling */
44 typedef struct _aul_svc_cb_info_t {
45         aul_svc_res_fn cb_func;
46         int request_code;
47         void *data;
48 } aul_svc_cb_info_t;
49
50 typedef struct _aul_svc_resolve_info_t {
51         char *pkgname;
52         char *op;
53         char *uri;
54         char *scheme;
55         char *host;
56         char *uri_r_info;
57         char *origin_mime;
58         char *mime;
59         char *m_type;
60         char *s_type;
61         char *category;
62         char *win_id;
63         int mime_set;
64 } aul_svc_resolve_info_t;
65
66 typedef struct _aul_svc_transient_cb_info_t {
67         aul_svc_host_res_fn cb_func;
68         void *data;
69 } aul_svc_transient_cb_info_t;
70
71 pthread_mutex_t iniparser_lock = PTHREAD_MUTEX_INITIALIZER;
72 GSList *tmp_list;
73
74 static aul_svc_cb_info_t *__create_rescb(int request_code,
75                                         aul_svc_res_fn cbfunc, void *data);
76 static void __remove_rescb(aul_svc_cb_info_t *info);
77 static int __set_bundle(bundle *b, const char *key, const char *value);
78 static void __aul_cb(bundle *b, int is_cancel, void *data);
79 static int __run_svc_with_pkgname(char *pkgname, bundle *b, int request_code,
80                                 aul_svc_res_fn cbfunc, void *data, uid_t uid);
81 static int __get_resolve_info(bundle *b, aul_svc_resolve_info_t *info);
82 static int __free_resolve_info_data(aul_svc_resolve_info_t *info);
83
84 static char *white_list[] = {
85         APP_SELECTOR,
86         SHARE_PANEL,
87         NULL
88 };
89
90 static bool __is_special_app(const char *appid)
91 {
92         const char *id;
93         int i = 0;
94
95         if (appid == NULL)
96                 return false;
97
98         while ((id = white_list[i]) != NULL) {
99                 if (strcmp(id, appid) == 0)
100                         return true;
101                 i++;
102         }
103         return false;
104 }
105
106 static aul_svc_cb_info_t *__create_rescb(int request_code,
107                                         aul_svc_res_fn cbfunc, void *data)
108 {
109         aul_svc_cb_info_t* info;
110
111         info = (aul_svc_cb_info_t*)calloc(1, sizeof(aul_svc_cb_info_t));
112         if (info == NULL)
113                 return NULL;
114
115         info->request_code = request_code;
116         info->cb_func = cbfunc;
117         info->data = data;
118
119         return info;
120 }
121
122 static void __remove_rescb(aul_svc_cb_info_t *info)
123 {
124         if (info) free(info);
125 }
126
127 static int __set_bundle(bundle *b, const char *key, const char *value)
128 {
129         const char *val = NULL;
130
131         val = bundle_get_val(b, key);
132         if (val) {
133                 if (bundle_del(b, key) != 0)
134                         return AUL_SVC_RET_ERROR;
135         }
136
137         if (!value)
138                 return AUL_SVC_RET_EINVAL;
139
140         if (bundle_add(b, key, value) != 0)
141                 return AUL_SVC_RET_ERROR;
142
143         _D("__set_bundle");
144
145         return AUL_SVC_RET_OK;
146 }
147
148 static int __set_bundle_array(bundle *b, const char *key,
149                                 const char **value, int len)
150 {
151
152         int type;
153         type = aul_svc_data_is_array(b, key);
154
155         if (type == 1) {
156                 if (bundle_del(b, key) != 0)
157                         return AUL_SVC_RET_ERROR;
158         }
159
160         if (!value)
161                 return AUL_SVC_RET_EINVAL;
162
163         if (bundle_add_str_array(b, key, value, len) != 0)
164                 return AUL_SVC_RET_ERROR;
165
166         _D("__set_bundle_array");
167
168         return AUL_SVC_RET_OK;
169 }
170
171 static void __aul_cb(bundle *b, int is_cancel, void *data)
172 {
173         const char *val = NULL;
174         aul_svc_cb_info_t*  cb_info;
175         int res;
176
177         if (is_cancel)
178                 res = AUL_SVC_RES_CANCEL;
179         else {
180                 /* get result_code from bundle */
181                 val = bundle_get_val(b, AUL_SVC_K_RES_VAL);
182                 res = (val == NULL) ? AUL_SVC_RES_NOT_OK : atoi(val);
183         }
184
185         /* remove result_code from bundle */
186         bundle_del(b, AUL_SVC_K_RES_VAL);
187
188         /* find corresponding callback */
189         cb_info = (aul_svc_cb_info_t*)data;
190
191         cb_info->cb_func(b, cb_info->request_code,
192                         (aul_svc_result_val)res, cb_info->data);
193         __remove_rescb(cb_info);
194
195         return;
196 }
197
198 static int __run_svc_with_pkgname(char *pkgname, bundle *b, int request_code,
199                                 aul_svc_res_fn cbfunc, void *data, uid_t uid)
200 {
201         aul_svc_cb_info_t *cb_info = NULL;
202         int ret = -1;
203
204         if (bundle_get_type(b, AUL_SVC_K_SELECTOR_EXTRA_LIST) != BUNDLE_TYPE_NONE) {
205                 if (!aul_svc_get_pkgname(b))
206                         pkgname = APP_SELECTOR;
207         }
208
209         if (bundle_get_val(b, AUL_K_FORCE_LAUNCH_APP_SELECTOR))
210                 pkgname = APP_SELECTOR;
211
212         if (__is_special_app(pkgname)) {
213                 bundle_del(b, AUL_SVC_K_CAN_BE_LEADER);
214                 bundle_add_str(b, AUL_SVC_K_CAN_BE_LEADER, "true");
215                 bundle_del(b, AUL_SVC_K_REROUTE);
216                 bundle_add_str(b, AUL_SVC_K_REROUTE, "true");
217                 bundle_del(b, AUL_SVC_K_RECYCLE);
218                 bundle_add_str(b, AUL_SVC_K_RECYCLE, "true");
219         }
220
221         if (cbfunc) {
222                 SECURE_LOGD("pkg_name : %s - with result", pkgname);
223
224                 cb_info = __create_rescb(request_code, cbfunc, data);
225                 ret = aul_launch_app_with_result_for_uid(pkgname, b, __aul_cb,
226                                 cb_info, uid);
227         } else {
228                 SECURE_LOGD("pkg_name : %s - no result", pkgname);
229
230 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
231                 const char* data = bundle_get_val(b, AUL_SVC_K_MULTI_INSTANCE);
232                 if (data)
233                         SECURE_LOGD("multi_instance value = %s", data);
234
235                 if (data && strncmp(data, "TRUE", strlen("TRUE")) == 0)
236                         ret = aul_launch_app_for_multi_instance(pkgname, b);
237                 else
238                         ret = aul_launch_app(pkgname, b);
239 #else
240                 ret = aul_launch_app_for_uid(pkgname, b, uid);
241 #endif
242         }
243
244         if (ret < 0) {
245                 switch (ret) {
246                 case AUL_R_EILLACC:
247                         ret = AUL_SVC_RET_EILLACC;
248                         break;
249                 case AUL_R_EINVAL:
250                         ret = AUL_SVC_RET_EINVAL;
251                         break;
252                 case AUL_R_ETERMINATING:
253                         ret = AUL_SVC_RET_ETERMINATING;
254                         break;
255                 case AUL_R_EREJECTED:
256                         ret = AUL_SVC_RET_EREJECTED;
257                         break;
258                 case AUL_R_ENOAPP:
259                         ret = AUL_SVC_RET_ENOMATCH;
260                         break;
261                 default:
262                         ret = AUL_SVC_RET_ELAUNCH;
263                 }
264         }
265
266         return ret;
267 }
268
269 static int __get_resolve_info(bundle *b, aul_svc_resolve_info_t *info)
270 {
271         char *tmp = NULL;
272         char *saveptr = NULL;
273         char *strtok_buf = NULL;
274         int ret = -1;
275
276         info->op = (char *)aul_svc_get_operation(b);
277         info->uri = (char *)aul_svc_get_uri(b);
278
279         if ((info->uri) && (strcmp(info->uri, "") == 0)) {
280                 _E("Uri is empty");
281                 return AUL_SVC_RET_EINVAL;
282         }
283
284         info->origin_mime = info->mime = (char *)aul_svc_get_mime(b);
285         info->pkgname = (char *)aul_svc_get_pkgname(b);
286         info->category = (char *)aul_svc_get_category(b);
287         info->win_id = (char *)bundle_get_val(b, AUL_SVC_K_WIN_ID);
288
289         SECURE_LOGD("getting resolve info for: operation - %s / uri - %s / mime - %s\n",
290                         info->op, info->uri, info->mime);
291
292         if (info->uri) {
293                 if (strncmp(info->uri, "/", 1) == 0) {
294                         if (!info->mime) {
295                                 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
296                                 if (info->mime == NULL) {
297                                         _E("out of memory");
298                                         return AUL_SVC_RET_ERROR;
299                                 }
300
301                                 ret = aul_get_mime_from_file(info->uri, info->mime, MAX_MIME_STR_SIZE);
302                                 info->mime_set = 1;
303                         }
304                         info->uri = NULL;
305                 } else if (strncmp(info->uri, "file:///", 8) == 0) {
306                         if (!info->mime) {
307                                 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
308                                 if (info->mime == NULL) {
309                                         _E("out of memory");
310                                         return AUL_SVC_RET_ERROR;
311                                 }
312
313                                 ret = aul_get_mime_from_file(&info->uri[7], info->mime, MAX_MIME_STR_SIZE);
314                                 info->mime_set = 1;
315                         }
316                 } else if (strncmp(info->uri, "file:/", 6) == 0) {
317                         if (!info->mime) {
318                                 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
319                                 if (info->mime == NULL) {
320                                         _E("out of memory");
321                                         return AUL_SVC_RET_ERROR;
322                                 }
323
324                                 ret = aul_get_mime_from_file(&info->uri[5], info->mime, MAX_MIME_STR_SIZE);
325                                 info->mime_set = 1;
326                         }
327                 }
328
329                 if (info->mime_set == 1 && ret < 0) {
330                         _E("aul_get_mime_from_file : %d", ret);
331                         free(info->mime);
332                         info->origin_mime = info->mime = NULL;
333                         info->mime_set = 0;
334                 }
335         }
336
337         if (info->uri) {
338                 GRegex *regex;
339                 GMatchInfo *match_info;
340                 GError *error = NULL;
341
342                 regex = g_regex_new("^(([^:/?#]+):)?(//([^/?#]*))?", 0, 0, &error);
343                 if (g_regex_match(regex, info->uri, 0, &match_info) == FALSE) {
344                         g_regex_unref(regex);
345                         return AUL_SVC_RET_EINVAL;
346                 }
347
348                 info->scheme = g_match_info_fetch(match_info, 2);
349                 info->host = g_match_info_fetch(match_info, 4);
350
351                 if (info->scheme && info->host) {
352                         info->uri_r_info = malloc(MAX_SCHEME_STR_SIZE + MAX_HOST_STR_SIZE + 2);
353                         if (info->uri_r_info == NULL) {
354                                 _E("out of memory");
355                                 g_match_info_free(match_info);
356                                 g_regex_unref(regex);
357                                 return AUL_SVC_RET_ERROR;
358                         }
359
360                         snprintf(info->uri_r_info, MAX_SCHEME_STR_SIZE + MAX_HOST_STR_SIZE + 1,
361                                                 "%s://%s", info->scheme, info->host);
362                 }
363
364                 g_match_info_free(match_info);
365                 g_regex_unref(regex);
366
367         } else {
368                 info->scheme = strdup("NULL");
369         }
370
371         if (!info->mime) {
372                 info->mime = strdup("NULL");
373                 return 0;
374         }
375
376         info->m_type = calloc(1, MAX_LOCAL_BUFSZ);
377         if (info->m_type == NULL) {
378                 _E("ouf of memory");
379                 return AUL_SVC_RET_ERROR;
380         }
381
382         info->s_type = calloc(1, MAX_LOCAL_BUFSZ);
383         if (info->s_type == NULL) {
384                 _E("out of memory");
385                 free(info->m_type);
386                 return AUL_SVC_RET_ERROR;
387         }
388
389         tmp = strdup(info->mime);
390         if (tmp == NULL) {
391                 _E("out of memory");
392                 free(info->s_type);
393                 free(info->m_type);
394                 return AUL_SVC_RET_ERROR;
395         }
396
397         strtok_buf = strtok_r(tmp, "/", &saveptr);
398         if (strtok_buf)
399                 strncpy(info->m_type, strtok_buf, MAX_LOCAL_BUFSZ - 1);
400         strtok_buf = strtok_r(NULL, "/", &saveptr);
401         if (strtok_buf)
402                 strncpy(info->s_type, strtok_buf, MAX_LOCAL_BUFSZ - 1);
403         free(tmp);
404
405         if (strncmp(info->m_type, "*", 1) == 0)
406                 strncpy(info->m_type, "%", MAX_LOCAL_BUFSZ - 1);
407         if (strncmp(info->s_type, "*", 1) == 0)
408                 strncpy(info->s_type, "%", MAX_LOCAL_BUFSZ - 1);
409
410         info->mime = malloc(MAX_MIME_STR_SIZE);
411         if (info->mime == NULL) {
412                 _E("out of memory");
413                 free(info->s_type);
414                 free(info->m_type);
415                 return AUL_SVC_RET_ERROR;
416         }
417
418         snprintf(info->mime, MAX_MIME_STR_SIZE - 1,
419                         "%s/%s", info->m_type, info->s_type);
420
421         return 0;
422 }
423
424 static int __free_resolve_info_data(aul_svc_resolve_info_t *info)
425 {
426         if (info->mime)
427                 free(info->mime);
428         if (info->scheme)
429                 free(info->scheme);
430         if (info->host)
431                 free(info->host);
432         if (info->m_type)
433                 free(info->m_type);
434         if (info->s_type)
435                 free(info->s_type);
436         if (info->uri_r_info)
437                 free(info->uri_r_info);
438         if (info->mime_set)
439                 free(info->origin_mime);
440
441         return 0;
442 }
443
444 static char* __get_alias_appid(char *appid)
445 {
446         char *alias_id = NULL;
447         char *val = NULL;
448         char key_string[MAX_PACKAGE_STR_SIZE + 5];
449         dictionary *dic;
450
451         dic = iniparser_load("/usr/share/appsvc/alias.ini");
452
453         if (dic == NULL)
454                 return NULL;
455
456         snprintf(key_string, sizeof(key_string), "Alias:%s", appid);
457         pthread_mutex_lock(&iniparser_lock);
458         val = iniparser_getstring(dic, key_string, NULL);
459         pthread_mutex_unlock(&iniparser_lock);
460
461         SECURE_LOGD("alias_id : %s", val);
462
463         if (val != NULL) {
464                 alias_id = malloc(MAX_PACKAGE_STR_SIZE);
465                 if (alias_id == NULL) {
466                         _E("out of memory");
467                         iniparser_freedict(dic);
468                         return NULL;
469                 }
470
471                 strncpy(alias_id, val, MAX_PACKAGE_STR_SIZE - 1);
472         }
473
474         iniparser_freedict(dic);
475
476         return alias_id;
477 }
478
479 static char* __make_query(char *query, char *op, char *uri,
480                         char *mime, char *m_type, char *s_type)
481 {
482         char tmp[MAX_MIME_STR_SIZE] = { 0, };
483
484         query = _svc_db_query_builder_add(query, op, uri, mime, false);
485         if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(s_type, "%", 1) != 0)) {
486                 snprintf(tmp, MAX_MIME_STR_SIZE - 1, "%s/*", m_type);
487                 query = _svc_db_query_builder_add(query, op, uri, tmp, false);
488         }
489
490         if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(m_type, "%", 1) != 0)) {
491                 snprintf(tmp, MAX_MIME_STR_SIZE - 1, "*/*");
492                 query = _svc_db_query_builder_add(query, op, uri, tmp, false);
493         }
494
495         return query;
496 }
497
498 static char* __make_query_with_collation(char *op, char *uri, char *mime, char *m_type, char *s_type)
499 {
500         char tmp[MAX_MIME_STR_SIZE];
501         char *query = NULL;
502
503         query = _svc_db_query_builder_add(query, op, uri, mime, true);
504
505         if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(s_type, "%", 1) != 0)) {
506                 snprintf(tmp, MAX_MIME_STR_SIZE - 1, "%s/*", m_type);
507                 query = _svc_db_query_builder_add(query, op, uri, tmp, true);
508         }
509         if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(m_type, "%", 1) != 0)) {
510                 snprintf(tmp, MAX_MIME_STR_SIZE - 1, "*/*");
511                 query = _svc_db_query_builder_add(query, op, uri, tmp, true);
512         }
513
514         query = _svc_db_query_builder_in("ac.app_control collate appsvc_collation ", query);
515
516         return query;
517 }
518
519
520 static int __app_list_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
521 {
522         char *appid = NULL;
523         GSList **app_list = (GSList **)user_data;
524         char *str = NULL;
525         GSList *iter = NULL;
526
527         pkgmgrinfo_appinfo_get_appid(handle, &str);
528         _D("Matching application is %s", str);
529
530         for (iter = tmp_list; iter != NULL; iter = g_slist_next(iter)) {
531                 if (strncmp(str, (char *)iter->data, MAX_PACKAGE_STR_SIZE - 1) == 0) {
532                         appid = strdup(str);
533                         *app_list = g_slist_append(*app_list, (void *)appid);
534                         _D("%s is added", appid);
535                 }
536         }
537
538         return 0;
539 }
540
541 static int __get_list_with_category(char *category, GSList **pkg_list, uid_t uid)
542 {
543         int ret;
544         pkgmgrinfo_appinfo_filter_h handle;
545         GSList *app_list = NULL;
546         GSList *iter = NULL;
547         char *list_item = NULL;
548
549         ret = pkgmgrinfo_appinfo_filter_create(&handle);
550         ret = pkgmgrinfo_appinfo_filter_add_string(handle,
551                                 PMINFO_APPINFO_PROP_APP_CATEGORY, category);
552
553         tmp_list = *pkg_list;
554         ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle,
555                                 __app_list_cb, &app_list, uid);
556         if (ret != PMINFO_R_OK) {
557                 pkgmgrinfo_appinfo_filter_destroy(handle);
558                 return -1;
559         }
560         pkgmgrinfo_appinfo_filter_destroy(handle);
561
562         for (iter = *pkg_list; iter != NULL; iter = g_slist_next(iter)) {
563                 list_item = (char *)iter->data;
564                 g_free(list_item);
565         }
566         g_slist_free(*pkg_list);
567
568         *pkg_list = app_list;
569
570         return 0;
571 }
572
573 static int __check_mainapp_mode(char *operation)
574 {
575         return 0;
576 }
577
578 static int __get_list_with_submode(char *operation, char *win_id,
579                                 GSList **pkg_list, uid_t uid)
580 {
581         int ret = 0;
582         int mainapp_mode = 0;
583
584         mainapp_mode = __check_mainapp_mode(operation);
585
586         SECURE_LOGD("mainapp_mode : %d", mainapp_mode);
587
588         ret = _svc_db_adjust_list_with_submode(mainapp_mode, win_id, pkg_list, uid);
589
590         if (ret < 0) {
591                 _E("error on get_list_with_submode :%d", ret);
592                 return -1;
593         }
594
595         return 0;
596 }
597
598 static void __free_pkg_list(GSList *list)
599 {
600         char *list_item;
601         GSList *iter = NULL;
602
603         if (list == NULL)
604                 return;
605
606         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
607                 list_item = (char *)iter->data;
608                 g_free(list_item);
609         }
610         g_slist_free(list);
611 }
612
613 static gchar *__make_checksum(const char *op, const char *uri, const char *mime)
614 {
615         char buf[MAX_CHECKSUM_BUF];
616         gchar *checksum;
617
618         snprintf(buf, sizeof(buf), "%s:%s:%s", op, uri, mime);
619         checksum = g_compute_checksum_for_string(G_CHECKSUM_MD5, buf, -1);
620
621         return checksum;
622 }
623
624 static char *__get_cache(const char *checksum, uid_t uid)
625 {
626         app_pkt_t *pkt = NULL;
627         int fd;
628         int ret;
629         bundle *b;
630         char buf[MAX_PID_STR_BUFSZ];
631         char *appid;
632
633         b = bundle_create();
634         if (b == NULL) {
635                 _E("out of memory");
636                 return NULL;
637         }
638
639         snprintf(buf, sizeof(buf), "%d", uid);
640         bundle_add(b, AUL_K_TARGET_UID, buf);
641         bundle_add(b, AUL_K_CHECKSUM, checksum);
642
643         fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_APPID_FROM_CACHE,
644                         b, AUL_SOCK_ASYNC);
645         bundle_free(b);
646         if (fd <= 0)
647                 return NULL;
648
649         ret = aul_sock_recv_reply_pkt(fd, &pkt);
650         if (ret < 0)
651                 return NULL;
652
653         if (pkt->cmd == APP_GET_APPID_FROM_CACHE) {
654                 if (pkt->data[0] == 0) {
655                         free(pkt);
656                         return NULL;
657                 }
658                 appid = strdup((const char *)(pkt->data));
659                 free(pkt);
660                 return appid;
661         }
662         free(pkt);
663
664         return NULL;
665 }
666
667 static void __put_cache(const char *checksum, const char *appid, uid_t uid)
668 {
669         int ret;
670         bundle *b;
671         char buf[MAX_PID_STR_BUFSZ];
672
673         b = bundle_create();
674         if (!b) {
675                 _E("out of memory");
676                 return;
677         }
678
679         snprintf(buf, sizeof(buf), "%d", uid);
680         bundle_add(b, AUL_K_TARGET_UID, buf);
681         bundle_add(b, AUL_K_CHECKSUM, checksum);
682         bundle_add(b, AUL_K_APPID, appid);
683
684         ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_SET_CACHE, b);
685
686         if (ret < 0)
687                 _E("Failed to set cache : %d", ret);
688
689         bundle_free(b);
690 }
691
692 static void __put_cache_with_info(const char *checksum, const char *info, uid_t uid)
693 {
694         char buf[PATH_MAX];
695
696         snprintf(buf, sizeof(buf), "@APP_SELECTOR %s", info);
697         __put_cache(checksum, buf, uid);
698 }
699
700 static void __invalidate_cache(uid_t uid)
701 {
702         int ret;
703         bundle *b;
704         char buf[MAX_PID_STR_BUFSZ];
705
706         b = bundle_create();
707         if (!b) {
708                 _E("out of memory");
709                 return;
710         }
711
712         snprintf(buf, sizeof(buf), "%d", uid);
713         bundle_add(b, AUL_K_TARGET_UID, buf);
714         ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_INVALIDATE_CACHE, b);
715
716         if (ret < 0)
717                 _E("Failed to invalidate cache : %d", ret);
718
719         bundle_free(b);
720 }
721
722 API int aul_svc_set_operation(bundle *b, const char *operation)
723 {
724         if (b == NULL) {
725                 _E("bundle for aul_svc_set_operation is NULL");
726                 return AUL_SVC_RET_EINVAL;
727         }
728
729         return __set_bundle(b, AUL_SVC_K_OPERATION, operation);
730 }
731
732 API int aul_svc_set_uri(bundle *b, const char *uri)
733 {
734         if (b == NULL) {
735                 _E("bundle for aul_svc_set_uri is NULL");
736                 return AUL_SVC_RET_EINVAL;
737         }
738
739         return __set_bundle(b, AUL_SVC_K_URI, uri);
740 }
741
742 API int aul_svc_set_mime(bundle *b, const char *mime)
743 {
744         if (b == NULL) {
745                 _E("bundle for aul_svc_set_mime is NULL");
746                 return AUL_SVC_RET_EINVAL;
747         }
748
749         return __set_bundle(b, AUL_SVC_K_MIME, mime);
750 }
751
752 API int aul_svc_add_data(bundle *b, const char *key, const char *val)
753 {
754         if (b == NULL || key == NULL)
755                 return AUL_SVC_RET_EINVAL;
756
757         /* check key for data */
758         /******************/
759
760         return __set_bundle(b, key, val);
761 }
762
763 API int aul_svc_add_data_array(bundle *b, const char *key,
764                                 const char **val_array, int len)
765 {
766         if (b == NULL || key == NULL)
767                 return AUL_SVC_RET_EINVAL;
768
769         /* check key for data */
770         /******************/
771
772         return __set_bundle_array(b, key, val_array, len);
773 }
774
775 API int aul_svc_set_pkgname(bundle *b, const char *pkg_name)
776 {
777         if (b == NULL) {
778                 _E("bundle for aul_svc_set_pkgname is NULL");
779                 return AUL_SVC_RET_EINVAL;
780         }
781
782         return __set_bundle(b, AUL_SVC_K_PKG_NAME, pkg_name);
783 }
784
785 API int aul_svc_set_appid(bundle *b, const char *appid)
786 {
787         char *alias_id = NULL;
788         int ret;
789
790         if (b == NULL || appid == NULL) {
791                 _E("bundle for aul_svc_set_appid is NULL");
792                 return AUL_SVC_RET_EINVAL;
793         }
794
795         alias_id = __get_alias_appid((char *)appid);
796         if (alias_id == NULL) {
797                 ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, appid);
798         } else {
799                 ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, alias_id);
800                 free(alias_id);
801                 alias_id = NULL;
802         }
803
804         return ret;
805 }
806
807 API int aul_svc_set_category(bundle *b, const char *category)
808 {
809         if (b == NULL) {
810                 _E("bundle for aul_svc_set_category is NULL");
811                 return AUL_SVC_RET_EINVAL;
812         }
813
814         return __set_bundle(b, AUL_SVC_K_CATEGORY, category);
815 }
816
817 API int aul_svc_set_launch_mode(bundle *b, const char *mode)
818 {
819         if (b == NULL) {
820                 _E("bundle for aul_svc_set_launch_mode is NULL");
821                 return AUL_SVC_RET_EINVAL;
822         }
823
824         return __set_bundle(b, AUL_SVC_K_LAUNCH_MODE, mode);
825 }
826
827 API int aul_svc_run_service(bundle *b, int request_code,
828                                         aul_svc_res_fn cbfunc, void *data)
829 {
830         return aul_svc_run_service_for_uid(b, request_code, cbfunc, data, getuid());
831 }
832
833 API int aul_svc_run_service_for_uid(bundle *b, int request_code,
834                                 aul_svc_res_fn cbfunc, void *data, uid_t uid)
835 {
836         aul_svc_resolve_info_t info;
837         char *pkgname;
838         char *operation;
839         int pkg_count = 0;
840         int ret = -1;
841         char *appid;
842         int l;
843
844         GSList *pkg_list = NULL;
845         char *query = NULL;
846         gchar *checksum;
847
848         if (b == NULL) {
849                 _E("bundle for aul_svc_set_appid is NULL");
850                 return AUL_SVC_RET_EINVAL;
851         }
852
853         pkgname = (char *)aul_svc_get_pkgname(b);
854         operation = (char *)aul_svc_get_operation(b);
855
856         /* explict*/
857         if (pkgname) {
858                 if (operation == NULL)
859                         aul_svc_set_operation(b, AUL_SVC_OPERATION_DEFAULT);
860                 ret = __run_svc_with_pkgname(pkgname, b, request_code, cbfunc,
861                                 data, uid);
862                 return ret;
863         }
864
865         /* share panel */
866         if (operation && (strcmp(operation, AUL_SVC_OPERATION_SHARE) == 0
867                 || strcmp(operation, AUL_SVC_OPERATION_MULTI_SHARE) == 0
868                 || strcmp(operation, AUL_SVC_OPERATION_SHARE_TEXT) == 0)) {
869                 ret = __run_svc_with_pkgname(SHARE_PANEL, b, request_code,
870                                 cbfunc, data, uid);
871                 return ret;
872         }
873
874         memset(&info, 0, sizeof(aul_svc_resolve_info_t));
875         ret = __get_resolve_info(b, &info);
876         if (ret < 0) {
877                 __free_resolve_info_data(&info);
878                 return ret;
879         }
880
881         SECURE_LOGD("op - %s / mime - %s / scheme - %s\n",
882                                         info.op, info.origin_mime, info.scheme);
883
884         checksum = __make_checksum(info.op, info.uri, info.origin_mime);
885         appid = __get_cache(checksum, uid);
886
887         if (appid) {
888                 _D("Hit! %s / %s", checksum, appid);
889                 l = strlen("@APP_SELECTOR ");
890                 if (!strncmp("@APP_SELECTOR ", appid, l)) {
891                         bundle_add(b, AUL_SVC_K_URI_R_INFO, &appid[l]);
892                         ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
893                                 cbfunc, data, uid);
894                 } else if (!strcmp(appid, "^")) {
895                         ret = AUL_SVC_RET_ENOMATCH;
896                 } else {
897                         ret = __run_svc_with_pkgname(appid, b, request_code,
898                                 cbfunc, data, uid);
899                 }
900                 free(appid);
901                 g_free(checksum);
902                 __free_resolve_info_data(&info);
903                 return ret;
904         }
905
906         ret = _svc_db_check_perm(uid, true);
907         if (ret < 0) {
908                 _E("permission error : %d", ret);
909                 ret = AUL_SVC_RET_EILLACC;
910                 goto end;
911         }
912
913         /*uri*/
914         pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri, uid);
915         if (pkgname != NULL) {
916                 __put_cache(checksum, pkgname, uid);
917                 ret = __run_svc_with_pkgname(pkgname, b, request_code,
918                         cbfunc, data, uid);
919                 free(pkgname);
920                 goto end;
921         }
922
923         query = __make_query_with_collation(info.op, info.uri,
924                         info.mime, info.m_type, info.s_type);
925
926         query = _svc_db_query_builder_build(query);
927         _svc_db_exec_query(query, &pkg_list, uid);
928         if (query) {
929                 free(query);
930                 query = NULL;
931         }
932
933         pkg_count = g_slist_length(pkg_list);
934         if (pkg_count > 0) {
935                 __free_pkg_list(pkg_list);
936                 pkg_list = NULL;
937                 if (info.uri_r_info) {
938                         query = __make_query(query, info.op, info.uri_r_info,
939                                 info.mime, info.m_type, info.s_type);
940                 }
941
942                 query = __make_query(query, info.op, info.scheme,
943                         info.mime, info.m_type, info.s_type);
944
945                 query = __make_query(query, info.op, "*",
946                         info.mime, info.m_type, info.s_type);
947
948                 if (info.scheme && (strcmp(info.scheme, "file") == 0)
949                         && info.mime && (strcmp(info.mime, "NULL") != 0)) {
950                         query = __make_query(query, info.op, "NULL",
951                                 info.mime, info.m_type, info.s_type);
952                 }
953
954                 query = _svc_db_query_builder_build(query);
955                 _svc_db_exec_query(query, &pkg_list, uid);
956                 if (query) {
957                         free(query);
958                         query = NULL;
959                 }
960
961                 if (info.category)
962                         __get_list_with_category(info.category, &pkg_list, uid);
963
964                 __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
965
966                 pkg_count = g_slist_length(pkg_list);
967                 _D("pkg_count : %d", pkg_count);
968
969                 if (pkg_count == 1) {
970                         pkgname = (char *)pkg_list->data;
971                         if (pkgname != NULL) {
972                                 __put_cache(checksum, pkgname, uid);
973                                 ret = __run_svc_with_pkgname(pkgname, b, request_code,
974                                                 cbfunc, data, uid);
975                                 goto end;
976                         }
977                 } else if (pkg_count > 1) {
978                         bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri);
979                         __put_cache_with_info(checksum, info.uri, uid);
980                         ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
981                                         cbfunc, data, uid);
982                         goto end;
983                 }
984                 __free_pkg_list(pkg_list);
985                 pkg_list = NULL;
986         }
987
988         /*scheme & host*/
989         if (info.uri_r_info) {
990                 pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri_r_info, uid);
991
992                 if (pkgname != NULL) {
993                         __put_cache(checksum, pkgname, uid);
994                         ret = __run_svc_with_pkgname(pkgname, b, request_code,
995                                         cbfunc, data, uid);
996                         free(pkgname);
997                         goto end;
998                 }
999
1000                 query = __make_query(query, info.op, info.uri_r_info,
1001                         info.mime, info.m_type, info.s_type);
1002                 query = _svc_db_query_builder_build(query);
1003                 _svc_db_exec_query(query, &pkg_list, uid);
1004                 if (query) {
1005                         free(query);
1006                         query = NULL;
1007                 }
1008
1009                 pkg_count = g_slist_length(pkg_list);
1010                 if (pkg_count > 0) {
1011                         __free_pkg_list(pkg_list);
1012                         pkg_list = NULL;
1013                         query = __make_query(query, info.op, "*",
1014                         info.mime, info.m_type, info.s_type);
1015
1016                         if (info.scheme && (strcmp(info.scheme, "file") == 0)
1017                                 && info.mime && (strcmp(info.mime, "NULL") != 0)) {
1018                                 query = __make_query(query, info.op, "NULL",
1019                                         info.mime, info.m_type, info.s_type);
1020                         }
1021
1022                         query = _svc_db_query_builder_build(query);
1023                         _svc_db_exec_query(query, &pkg_list, uid);
1024                         if (query) {
1025                                 free(query);
1026                                 query = NULL;
1027                         }
1028
1029                         if (info.category)
1030                                 __get_list_with_category(info.category, &pkg_list, uid);
1031
1032                         __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1033
1034                         pkg_count = g_slist_length(pkg_list);
1035                         _D("pkg_count : %d", pkg_count);
1036
1037                         if (pkg_count == 1) {
1038                                 pkgname = (char *)pkg_list->data;
1039                                 if (pkgname != NULL) {
1040                                         __put_cache(checksum, pkgname, uid);
1041                                         ret = __run_svc_with_pkgname(pkgname, b, request_code,
1042                                                         cbfunc, data, uid);
1043                                         goto end;
1044                                 }
1045                         } else if (pkg_count > 1) {
1046                                 bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri_r_info);
1047                                 __put_cache_with_info(checksum, info.uri_r_info, uid);
1048                                 ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
1049                                         cbfunc, data, uid);
1050                                 goto end;
1051                         }
1052
1053                         __free_pkg_list(pkg_list);
1054                         pkg_list = NULL;
1055                 }
1056         }
1057
1058         /*scheme*/
1059         pkgname = _svc_db_get_app(info.op, info.origin_mime, info.scheme, uid);
1060
1061         if (pkgname != NULL) {
1062                 __put_cache(checksum, pkgname, uid);
1063                 ret = __run_svc_with_pkgname(pkgname, b, request_code,
1064                         cbfunc, data, uid);
1065                 free(pkgname);
1066                 goto end;
1067         }
1068
1069         query = __make_query(query, info.op, info.scheme,
1070                 info.mime, info.m_type, info.s_type);
1071
1072         query = __make_query(query, info.op, "*",
1073                 info.mime, info.m_type, info.s_type);
1074
1075         if (info.scheme && (strcmp(info.scheme, "file") == 0)
1076                 && info.mime && (strcmp(info.mime, "NULL") != 0)) {
1077                 query = __make_query(query, info.op, "NULL",
1078                         info.mime, info.m_type, info.s_type);
1079         }
1080
1081         query = _svc_db_query_builder_build(query);
1082         _svc_db_exec_query(query, &pkg_list, uid);
1083
1084         if (query) {
1085                 free(query);
1086                 query = NULL;
1087         }
1088
1089         if (info.category)
1090                 __get_list_with_category(info.category, &pkg_list, uid);
1091
1092         __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1093
1094         pkg_count = g_slist_length(pkg_list);
1095         _D("pkg_count : %d", pkg_count);
1096
1097         if (pkg_count == 1) {
1098                 pkgname = (char *)pkg_list->data;
1099                 if (pkgname != NULL) {
1100                         __put_cache(checksum, pkgname, uid);
1101                         ret = __run_svc_with_pkgname(pkgname, b, request_code,
1102                                         cbfunc, data, uid);
1103                 }
1104         } else if (pkg_count < 1) {
1105                 __free_resolve_info_data(&info);
1106                 __put_cache(checksum, "^", uid);
1107                 g_free(checksum);
1108                 return AUL_SVC_RET_ENOMATCH;
1109         } else {
1110                 bundle_add(b, AUL_SVC_K_URI_R_INFO, info.scheme);
1111                 __put_cache_with_info(checksum, info.scheme, uid);
1112                 ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
1113                                 cbfunc, data, uid);
1114         }
1115
1116 end:
1117         __free_pkg_list(pkg_list);
1118         __free_resolve_info_data(&info);
1119         g_free(checksum);
1120
1121         return ret;
1122 }
1123
1124 API int aul_svc_get_list(bundle *b, aul_svc_info_iter_fn iter_fn,
1125                 void *data)
1126 {
1127         return aul_svc_get_list_for_uid(b, iter_fn, data, getuid());
1128 }
1129
1130 API int aul_svc_get_list_for_uid(bundle *b, aul_svc_info_iter_fn iter_fn,
1131                 void *data, uid_t uid)
1132 {
1133         aul_svc_resolve_info_t info;
1134         char *pkgname = NULL;
1135         int pkg_count;
1136         int ret = -1;
1137
1138         GSList *pkg_list = NULL;
1139         GSList *iter = NULL;
1140         char *query = NULL;
1141         char *query2 = NULL;
1142
1143         if (b == NULL) {
1144                 _E("bundle for aul_svc_run_service is NULL");
1145                 return AUL_SVC_RET_EINVAL;
1146         }
1147
1148         if (iter_fn == NULL) {
1149                 _E("iter_fn for aul_svc_run_service is NULL");
1150                 return AUL_SVC_RET_EINVAL;
1151         }
1152
1153
1154         /* parse bundle */
1155         memset(&info, 0, sizeof(aul_svc_resolve_info_t));
1156         ret = __get_resolve_info(b, &info);
1157         if (ret < 0) {
1158                 __free_resolve_info_data(&info);
1159                 return ret;
1160         }
1161
1162         _D("operation - %s / shceme - %s / mime - %s\n", info.op, info.scheme,
1163            info.mime);
1164
1165         query2 = __make_query_with_collation(info.op, info.uri,
1166                         info.mime, info.m_type, info.s_type);
1167
1168         if (info.uri_r_info) {
1169                 query = __make_query(query, info.op, info.uri_r_info,
1170                         info.mime, info.m_type, info.s_type);
1171         }
1172
1173         query = __make_query(query, info.op, info.scheme,
1174                 info.mime, info.m_type, info.s_type);
1175
1176         query = __make_query(query, info.op, "*",
1177                 info.mime, info.m_type, info.s_type);
1178
1179         if (info.scheme && (strcmp(info.scheme, "file") == 0)
1180                 && info.mime && (strcmp(info.mime, "NULL") != 0)) {
1181                 query = __make_query(query, info.op, "NULL",
1182                         info.mime, info.m_type, info.s_type);
1183         }
1184
1185         query = _svc_db_query_builder_or(query2, query);
1186         query = _svc_db_query_builder_build(query);
1187         _svc_db_exec_query(query, &pkg_list, uid);
1188         if (query) {
1189                 free(query);
1190                 query = NULL;
1191         }
1192
1193         if (info.category)
1194                 __get_list_with_category(info.category, &pkg_list, uid);
1195
1196         __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1197
1198         pkg_count = g_slist_length(pkg_list);
1199         if (pkg_count == 0) {
1200                 _E("Cannot find associated application");
1201
1202                 __free_resolve_info_data(&info);
1203                 return AUL_SVC_RET_ENOMATCH;
1204         }
1205
1206         for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
1207                 pkgname = iter->data;
1208                 SECURE_LOGD("PKGNAME : %s\n", pkgname);
1209                 if (iter_fn(pkgname, data) != 0)
1210                         break;
1211                 g_free(pkgname);
1212         }
1213
1214         g_slist_free(pkg_list);
1215         __free_resolve_info_data(&info);
1216
1217         return AUL_SVC_RET_OK;
1218 }
1219
1220 API int aul_svc_get_all_defapps(aul_svc_info_iter_fn iter_fn, void *data)
1221 {
1222         return aul_svc_get_all_defapps_for_uid(iter_fn, data, getuid());
1223 }
1224
1225 API int aul_svc_get_all_defapps_for_uid(aul_svc_info_iter_fn iter_fn,
1226                 void *data, uid_t uid)
1227 {
1228         char *pkgname = NULL;
1229         int ret = -1;
1230
1231         GSList *pkg_list = NULL;
1232         GSList *iter = NULL;
1233
1234
1235         ret = _svc_db_check_perm(uid, true);
1236         if (ret < 0) {
1237                 _E("permission error : %d", ret);
1238                 return AUL_SVC_RET_EILLACC;
1239         }
1240
1241         ret = _svc_db_get_list_with_all_defapps(&pkg_list, uid);
1242         if (ret < 0)
1243                 return ret;
1244
1245         for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
1246                 pkgname = iter->data;
1247                 if (iter_fn(pkgname, data) != 0)
1248                         break;
1249                 g_free(pkgname);
1250         }
1251
1252         g_slist_free(pkg_list);
1253
1254         return AUL_SVC_RET_OK;
1255 }
1256
1257 API const char *aul_svc_get_operation(bundle *b)
1258 {
1259         return bundle_get_val(b, AUL_SVC_K_OPERATION);
1260 }
1261
1262 API const char *aul_svc_get_uri(bundle *b)
1263 {
1264         return bundle_get_val(b, AUL_SVC_K_URI);
1265 }
1266
1267 API const char *aul_svc_get_mime(bundle *b)
1268 {
1269         return bundle_get_val(b, AUL_SVC_K_MIME);
1270 }
1271
1272 API const char *aul_svc_get_data(bundle *b, const char *key)
1273 {
1274         return bundle_get_val(b, key);
1275 }
1276
1277 API const char **aul_svc_get_data_array(bundle *b, const char *key, int *len)
1278 {
1279         return bundle_get_str_array(b, key, len);
1280 }
1281
1282 API const char *aul_svc_get_pkgname(bundle *b)
1283 {
1284         return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
1285 }
1286
1287 API const char *aul_svc_get_appid(bundle *b)
1288 {
1289         return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
1290 }
1291
1292 API const char *aul_svc_get_category(bundle *b)
1293 {
1294         return bundle_get_val(b, AUL_SVC_K_CATEGORY);
1295 }
1296
1297 API const char *aul_svc_get_launch_mode(bundle *b)
1298 {
1299         return bundle_get_val(b, AUL_SVC_K_LAUNCH_MODE);
1300 }
1301
1302 API int aul_svc_create_result_bundle(bundle *inb, bundle **outb)
1303 {
1304         int ret = -1;
1305
1306         if (inb == NULL || outb == NULL) {
1307                 _E("bundle is NULL");
1308                 return AUL_SVC_RET_EINVAL;
1309         }
1310
1311         ret = aul_create_result_bundle(inb, outb);
1312
1313         /* add additional bundle */
1314         /*  bundle_add(outb, " ", " ");  */
1315
1316         if (ret == AUL_R_OK)
1317                 ret = AUL_SVC_RET_OK;
1318         else if (ret == AUL_R_EINVAL)
1319                 ret = AUL_SVC_RET_EINVAL;
1320         else
1321                 ret = AUL_SVC_RET_ERROR;
1322
1323         return ret;
1324 }
1325
1326 API int aul_svc_send_result(bundle *b, aul_svc_result_val result)
1327 {
1328         int ret;
1329         char tmp[MAX_LOCAL_BUFSZ];
1330
1331         if (b == NULL) {
1332                 _E("aul_svc_send_result is NULL");
1333                 return AUL_SVC_RET_EINVAL;
1334         }
1335
1336         /* add result_code to bundle */
1337         snprintf(tmp, MAX_LOCAL_BUFSZ, "%d", (int)result);
1338         ret = __set_bundle(b, AUL_SVC_K_RES_VAL, tmp);
1339         if (ret < 0)
1340                 return AUL_SVC_RET_ERROR;
1341
1342         if (result == AUL_SVC_RES_CANCEL)
1343                 ret = aul_send_result(b, 1);
1344         else
1345                 ret = aul_send_result(b, 0);
1346
1347         /* remove result_code from bundle */
1348         bundle_del(b, AUL_SVC_K_RES_VAL);
1349
1350         return ret;
1351 }
1352
1353 API int aul_svc_set_defapp(const char *op, const char *mime_type,
1354                                 const char *uri, const char *defapp)
1355 {
1356         return aul_svc_set_defapp_for_uid(op, mime_type, uri, defapp, getuid());
1357 }
1358
1359 API int aul_svc_set_defapp_for_uid(const char *op, const char *mime_type,
1360                                 const char *uri, const char *defapp, uid_t uid)
1361 {
1362         int ret;
1363
1364         if (op == NULL || defapp == NULL)
1365                 return AUL_SVC_RET_EINVAL;
1366
1367         ret = _svc_db_check_perm(uid, false);
1368         if (ret < 0) {
1369                 _E("permission error : %d", ret);
1370                 return AUL_SVC_RET_EILLACC;
1371         }
1372
1373         ret = _svc_db_add_app(op, mime_type, uri, defapp, uid);
1374         if (ret < 0)
1375                 return AUL_SVC_RET_ERROR;
1376
1377         return AUL_SVC_RET_OK;
1378 }
1379
1380 API int aul_svc_unset_defapp(const char *defapp)
1381 {
1382         return aul_svc_unset_defapp_for_uid(defapp, getuid());
1383 }
1384
1385 API int aul_svc_unset_defapp_for_uid(const char *defapp, uid_t uid)
1386 {
1387         int ret;
1388
1389         if (defapp == NULL)
1390                 return AUL_SVC_RET_EINVAL;
1391
1392         ret = _svc_db_check_perm(uid, false);
1393         if (ret < 0) {
1394                 _E("permission error : %d", ret);
1395                 return AUL_SVC_RET_EILLACC;
1396         }
1397
1398         ret = _svc_db_delete_with_pkgname(defapp, uid);
1399
1400         if (ret < 0)
1401                 return AUL_SVC_RET_ERROR;
1402
1403         return AUL_SVC_RET_OK;
1404 }
1405
1406 API int aul_svc_unset_all_defapps()
1407 {
1408         return aul_svc_unset_all_defapps_for_uid(getuid());
1409 }
1410
1411 API int aul_svc_unset_all_defapps_for_uid(uid_t uid)
1412 {
1413         int ret;
1414
1415         ret = _svc_db_check_perm(uid, false);
1416         if (ret < 0) {
1417                 _E("permission error : %d", ret);
1418                 return AUL_SVC_RET_EILLACC;
1419         }
1420
1421         ret = _svc_db_delete_all(uid);
1422
1423         if (ret < 0)
1424                 return AUL_SVC_RET_ERROR;
1425
1426         __invalidate_cache(uid);
1427         return AUL_SVC_RET_OK;
1428 }
1429
1430 API int aul_svc_is_defapp(const char *pkg_name)
1431 {
1432         return aul_svc_is_defapp_for_uid(pkg_name, getuid());
1433 }
1434
1435 API int aul_svc_is_defapp_for_uid(const char *pkg_name, uid_t uid)
1436 {
1437         int ret;
1438
1439         ret = _svc_db_check_perm(uid, true);
1440         if (ret < 0) {
1441                 _E("permission error : %d", ret);
1442                 return AUL_SVC_RET_EILLACC;
1443         }
1444
1445         return _svc_db_is_defapp(pkg_name, uid);
1446 }
1447
1448 API int aul_svc_data_is_array(bundle *b, const char *key)
1449 {
1450         int type;
1451         type = bundle_get_type(b, key);
1452
1453         if (type <= 0)
1454                 return 0;
1455
1456         if (type & BUNDLE_TYPE_ARRAY)
1457                 return 1;
1458         return 0;
1459 }
1460
1461 API int aul_svc_allow_transient_app(bundle *b, int wid)
1462 {
1463         char win_id[MAX_LOCAL_BUFSZ];
1464
1465         snprintf(win_id, MAX_LOCAL_BUFSZ, "%d", wid);
1466
1467         if (b == NULL) {
1468                 _E("bundle for aul_svc_allow_transient_app is NULL");
1469                 return AUL_SVC_RET_EINVAL;
1470         }
1471
1472         return __set_bundle(b, AUL_SVC_K_WIN_ID, win_id);
1473 }
1474
1475 API int aul_svc_request_transient_app(bundle *b, int callee_wid,
1476                                 aul_svc_host_res_fn cbfunc, void *data)
1477 {
1478         return 0;
1479 }
1480
1481 API int aul_svc_subapp_terminate_request_pid(int pid)
1482 {
1483         int cpid = getpid();
1484         int lcnt;
1485         int *lpids = NULL;
1486         int i;
1487
1488         aul_app_group_get_leader_pids(&lcnt, &lpids);
1489         for (i = 0; i < lcnt; i++) {
1490                 if (lpids[i] == cpid) {
1491                         int cnt;
1492                         int *pids = NULL;
1493
1494                         aul_app_group_get_group_pids(cpid, &cnt, &pids);
1495
1496                         if (cnt == 0) {
1497                                 free(lpids);
1498                                 if (pids)
1499                                         free(pids);
1500
1501                                 return aul_subapp_terminate_request_pid(pid);
1502                         }
1503
1504                         if (pids != NULL)
1505                                 free(pids);
1506                         break;
1507                 }
1508         }
1509
1510         if (lpids != NULL)
1511                 free(lpids);
1512
1513         return aul_app_group_clear_top();
1514 }
1515
1516 API int aul_send_service_result(bundle *b)
1517 {
1518         return aul_send_result(b, 0);
1519 }
1520
1521 API int aul_svc_subscribe_launch_result(bundle *b, const char *result)
1522 {
1523         if (b == NULL) {
1524                 _E("bundle for aul_svc_subscribe_launch_result is NULL");
1525                 return AUL_SVC_RET_EINVAL;
1526         }
1527
1528         return __set_bundle(b, result, "1");
1529 }
1530
1531 API int aul_svc_set_loader_id(bundle *b, int loader_id)
1532 {
1533         char tmp[MAX_LOCAL_BUFSZ];
1534
1535         if (b == NULL) {
1536                 _E("bundle for aul_svc_set_loader_id is NULL");
1537                 return AUL_SVC_RET_EINVAL;
1538         }
1539
1540         if (loader_id <= 0) {
1541                 _E("invalid loader id");
1542                 return AUL_SVC_RET_EINVAL;
1543         }
1544
1545         snprintf(tmp, sizeof(tmp), "%d", loader_id);
1546         return __set_bundle(b, AUL_K_LOADER_ID, tmp);
1547 }
1548
1549 API int aul_svc_set_loader_name(bundle *b, const char *loader_name)
1550 {
1551         if (b == NULL) {
1552                 _E("bundle for aul_svc_set_loader_name is NULL");
1553                 return AUL_SVC_RET_EINVAL;
1554         }
1555
1556         if (!loader_name) {
1557                 _E("invalid loader name");
1558                 return AUL_SVC_RET_EINVAL;
1559         }
1560
1561         return __set_bundle(b, AUL_K_LOADER_NAME, loader_name);
1562 }
1563
1564 API int aul_svc_set_background_launch(bundle *b, int enabled)
1565 {
1566         if (b == NULL)
1567                 return AUL_R_EINVAL;
1568
1569         if (bundle_get_type(b, AUL_SVC_K_BG_LAUNCH) != BUNDLE_TYPE_NONE)
1570                 bundle_del(b, AUL_SVC_K_BG_LAUNCH);
1571
1572         if (enabled)
1573                 bundle_add_str(b, AUL_SVC_K_BG_LAUNCH, "enable");
1574
1575         return AUL_R_OK;
1576 }
1577
1578 API int aul_svc_set_alias_appid(const char *alias_appid, const char *appid)
1579 {
1580         return aul_svc_set_alias_appid_for_uid(alias_appid, appid, getuid());
1581 }
1582
1583 API int aul_svc_set_alias_appid_for_uid(const char *alias_appid,
1584                 const char *appid, uid_t uid)
1585 {
1586         int ret;
1587
1588         ret = _svc_db_check_perm(uid, false);
1589         if (ret < 0) {
1590                 _E("Permission error: %d", ret);
1591                 return AUL_SVC_RET_EILLACC;
1592         }
1593
1594         ret = _svc_db_add_alias_appid(alias_appid, appid, uid);
1595         if (ret < 0)
1596                 return AUL_SVC_RET_ERROR;
1597
1598         return AUL_SVC_RET_OK;
1599 }
1600
1601 API int aul_svc_unset_alias_appid(const char *alias_appid)
1602 {
1603         return aul_svc_unset_alias_appid_for_uid(alias_appid, getuid());
1604 }
1605
1606 API int aul_svc_unset_alias_appid_for_uid(const char *alias_appid, uid_t uid)
1607 {
1608         int ret;
1609
1610         ret = _svc_db_check_perm(uid, false);
1611         if (ret < 0) {
1612                 _E("Permission error: %d", ret);
1613                 return AUL_SVC_RET_EILLACC;
1614         }
1615
1616         ret = _svc_db_delete_alias_appid(alias_appid, uid);
1617         if (ret < 0)
1618                 return AUL_SVC_RET_ERROR;
1619
1620         return AUL_SVC_RET_OK;
1621 }
1622
1623 API int aul_svc_foreach_alias_info(void (*callback)(const char *alias_appid,
1624                         const char *appid, void *data), void *user_data)
1625 {
1626         return aul_svc_foreach_alias_info_for_uid(callback, getuid(),
1627                         user_data);
1628 }
1629
1630 API int aul_svc_foreach_alias_info_for_uid(void (*callback)(
1631                         const char *alias_appid, const char *appid,
1632                         void *data), uid_t uid, void *user_data)
1633 {
1634         int ret;
1635
1636         if (callback == NULL) {
1637                 _E("Invalid parameter");
1638                 return AUL_SVC_RET_EINVAL;
1639         }
1640
1641         ret = _svc_db_check_perm(uid, true);
1642         if (ret < 0) {
1643                 _E("Permission error: %d", ret);
1644                 return AUL_SVC_RET_EILLACC;
1645         }
1646
1647         ret = _svc_db_foreach_alias_info(callback, uid, user_data);
1648         if (ret < 0)
1649                 return AUL_SVC_RET_ERROR;
1650
1651         return AUL_SVC_RET_OK;
1652 }
1653
1654 API int aul_svc_enable_alias_info(const char *appid)
1655 {
1656         return aul_svc_enable_alias_info_for_uid(appid, getuid());
1657 }
1658
1659 API int aul_svc_enable_alias_info_for_uid(const char *appid, uid_t uid)
1660 {
1661         int ret;
1662
1663         ret = _svc_db_check_perm(uid, false);
1664         if (ret < 0) {
1665                 _E("Permission error: %d", ret);
1666                 return AUL_SVC_RET_EILLACC;
1667         }
1668
1669         ret = _svc_db_enable_alias_info(appid, uid);
1670         if (ret < 0)
1671                 return AUL_SVC_RET_ERROR;
1672
1673         return AUL_SVC_RET_OK;
1674 }
1675
1676 API int aul_svc_disable_alias_info(const char *appid)
1677 {
1678         return aul_svc_disable_alias_info_for_uid(appid, getuid());
1679 }
1680
1681 API int aul_svc_disable_alias_info_for_uid(const char *appid, uid_t uid)
1682 {
1683         int ret;
1684
1685         ret = _svc_db_check_perm(uid, false);
1686         if (ret < 0) {
1687                 _E("Permission error: %d", ret);
1688                 return AUL_SVC_RET_EILLACC;
1689         }
1690
1691         ret = _svc_db_disable_alias_info(appid, uid);
1692         if (ret < 0)
1693                 return AUL_SVC_RET_ERROR;
1694
1695         return AUL_SVC_RET_OK;
1696 }
1697
1698 API int aul_svc_get_appid_by_alias_appid(const char *alias_appid, char **appid)
1699 {
1700         return aul_svc_get_appid_by_alias_appid_for_uid(alias_appid,
1701                         appid, getuid());
1702 }
1703
1704 API int aul_svc_get_appid_by_alias_appid_for_uid(const char *alias_appid,
1705                 char **appid, uid_t uid)
1706 {
1707         int ret;
1708
1709         ret = _svc_db_check_perm(uid, true);
1710         if (ret < 0) {
1711                 _E("Permission error: %d", ret);
1712                 return AUL_SVC_RET_EILLACC;
1713         }
1714
1715         ret = _svc_db_get_appid_from_alias_info(alias_appid, appid, uid);
1716         if (ret < 0)
1717                 return AUL_SVC_RET_ERROR;
1718
1719         return AUL_SVC_RET_OK;
1720 }
1721
1722 API int aul_svc_foreach_alias_info_by_appid(int (*callback)(
1723                         const char *alias_appid, const char *appid, void *data),
1724                 const char *appid, void *user_data)
1725 {
1726         return aul_svc_foreach_alias_info_by_appid_for_uid(callback, appid,
1727                         getuid(), user_data);
1728 }
1729
1730 API int aul_svc_foreach_alias_info_by_appid_for_uid(int (*callback)(
1731                         const char *alias_appid, const char *appid, void *data),
1732                 const char *appid, uid_t uid, void *user_data)
1733 {
1734         int ret;
1735
1736         if (callback == NULL || appid == NULL) {
1737                 _E("Invalid parameter");
1738                 return AUL_SVC_RET_EINVAL;
1739         }
1740
1741         ret = _svc_db_check_perm(uid, true);
1742         if (ret < 0) {
1743                 _E("Permission error: %d", ret);
1744                 return AUL_SVC_RET_EILLACC;
1745         }
1746
1747         ret = _svc_db_foreach_alias_info_by_appid(callback, appid,
1748                         uid, user_data);
1749         if (ret < 0)
1750                 return AUL_SVC_RET_ERROR;
1751
1752         return AUL_SVC_RET_OK;
1753 }
1754
1755 API int aul_svc_foreach_allowed_info(int (*callback)(const char *appid,
1756                         const char *allowed_appid, void *data), void *user_data)
1757 {
1758         return aul_svc_foreach_allowed_info_for_uid(callback,
1759                         getuid(), user_data);
1760 }
1761
1762 API int aul_svc_foreach_allowed_info_for_uid(int (*callback)(const char *appid,
1763                         const char *allowed_appid, void *data),
1764                 uid_t uid, void *user_data)
1765 {
1766         int ret;
1767
1768         if (callback == NULL) {
1769                 _E("Invalid parameter");
1770                 return AUL_SVC_RET_EINVAL;
1771         }
1772
1773         ret = _svc_db_check_perm(uid, true);
1774         if (ret < 0) {
1775                 _E("Permission error: %d", ret);
1776                 return AUL_SVC_RET_EILLACC;
1777         }
1778
1779         ret = _svc_db_foreach_allowed_info(callback, uid, user_data);
1780         if (ret < 0)
1781                 return AUL_SVC_RET_ERROR;
1782
1783         return AUL_SVC_RET_OK;
1784 }
1785
1786 API int aul_svc_foreach_allowed_info_by_appid(int (*callback)(
1787                         const char *appid, const char *allowed_appid, void *data),
1788                 const char *appid, void *user_data)
1789 {
1790         return aul_svc_foreach_allowed_info_by_appid_for_uid(callback,
1791                         appid, getuid(), user_data);
1792 }
1793
1794 API int aul_svc_foreach_allowed_info_by_appid_for_uid(int (*callback)(
1795                         const char *appid, const char *allowed_appid, void *data),
1796                 const char *appid, uid_t uid, void *user_data)
1797 {
1798         int ret;
1799
1800         if (callback == NULL || appid == NULL) {
1801                 _E("Invalid parameter");
1802                 return AUL_SVC_RET_EINVAL;
1803         }
1804
1805         ret = _svc_db_check_perm(uid, true);
1806         if (ret < 0) {
1807                 _E("Permission error: %d", ret);
1808                 return AUL_SVC_RET_EILLACC;
1809         }
1810
1811         ret = _svc_db_foreach_allowed_info_by_appid(callback, appid,
1812                         uid, user_data);
1813         if (ret < 0)
1814                 return AUL_SVC_RET_ERROR;
1815
1816         return AUL_SVC_RET_OK;
1817 }
1818
1819 API const char *aul_svc_get_instance_id(bundle *b)
1820 {
1821         return bundle_get_val(b, AUL_K_INSTANCE_ID);
1822 }
1823
1824 API int aul_svc_set_instance_id(bundle *b, const char *instance_id)
1825 {
1826         if (b == NULL || instance_id == NULL) {
1827                 _E("Invalid parameter");
1828                 return AUL_SVC_RET_EINVAL;
1829         }
1830
1831         return __set_bundle(b, AUL_K_INSTANCE_ID, instance_id);
1832 }