2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
24 #include <bundle_internal.h>
29 #include <iniparser.h>
30 #include <pkgmgr-info.h>
36 #include "aul_svc_db.h"
38 #include "aul_svc_priv_key.h"
41 #define MAX_CHECKSUM_BUF 2048
43 /* callback handling */
44 typedef struct _aul_svc_cb_info_t {
45 aul_svc_res_fn cb_func;
50 typedef struct _aul_svc_resolve_info_t {
64 } aul_svc_resolve_info_t;
66 typedef struct _aul_svc_transient_cb_info_t {
67 aul_svc_host_res_fn cb_func;
69 } aul_svc_transient_cb_info_t;
71 pthread_mutex_t iniparser_lock = PTHREAD_MUTEX_INITIALIZER;
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);
84 static char *white_list[] = {
90 static bool __is_special_app(const char *appid)
98 while ((id = white_list[i]) != NULL) {
99 if (strcmp(id, appid) == 0)
106 static aul_svc_cb_info_t *__create_rescb(int request_code,
107 aul_svc_res_fn cbfunc, void *data)
109 aul_svc_cb_info_t* info;
111 info = (aul_svc_cb_info_t*)calloc(1, sizeof(aul_svc_cb_info_t));
115 info->request_code = request_code;
116 info->cb_func = cbfunc;
122 static void __remove_rescb(aul_svc_cb_info_t *info)
124 if (info) free(info);
127 static int __set_bundle(bundle *b, const char *key, const char *value)
129 const char *val = NULL;
131 val = bundle_get_val(b, key);
133 if (bundle_del(b, key) != 0)
134 return AUL_SVC_RET_ERROR;
138 return AUL_SVC_RET_EINVAL;
140 if (bundle_add(b, key, value) != 0)
141 return AUL_SVC_RET_ERROR;
145 return AUL_SVC_RET_OK;
148 static int __set_bundle_array(bundle *b, const char *key,
149 const char **value, int len)
153 type = aul_svc_data_is_array(b, key);
156 if (bundle_del(b, key) != 0)
157 return AUL_SVC_RET_ERROR;
161 return AUL_SVC_RET_EINVAL;
163 if (bundle_add_str_array(b, key, value, len) != 0)
164 return AUL_SVC_RET_ERROR;
166 _D("__set_bundle_array");
168 return AUL_SVC_RET_OK;
171 static void __aul_cb(bundle *b, int is_cancel, void *data)
173 const char *val = NULL;
174 aul_svc_cb_info_t* cb_info;
178 res = AUL_SVC_RES_CANCEL;
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);
185 /* remove result_code from bundle */
186 bundle_del(b, AUL_SVC_K_RES_VAL);
188 /* find corresponding callback */
189 cb_info = (aul_svc_cb_info_t*)data;
191 cb_info->cb_func(b, cb_info->request_code,
192 (aul_svc_result_val)res, cb_info->data);
193 __remove_rescb(cb_info);
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)
201 aul_svc_cb_info_t *cb_info = NULL;
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;
209 if (bundle_get_val(b, AUL_K_FORCE_LAUNCH_APP_SELECTOR))
210 pkgname = APP_SELECTOR;
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");
222 SECURE_LOGD("pkg_name : %s - with result", pkgname);
224 cb_info = __create_rescb(request_code, cbfunc, data);
225 ret = aul_launch_app_with_result_for_uid(pkgname, b, __aul_cb,
228 SECURE_LOGD("pkg_name : %s - no result", pkgname);
230 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
231 const char* data = bundle_get_val(b, AUL_SVC_K_MULTI_INSTANCE);
233 SECURE_LOGD("multi_instance value = %s", data);
235 if (data && strncmp(data, "TRUE", strlen("TRUE")) == 0)
236 ret = aul_launch_app_for_multi_instance(pkgname, b);
238 ret = aul_launch_app(pkgname, b);
240 ret = aul_launch_app_for_uid(pkgname, b, uid);
247 ret = AUL_SVC_RET_EILLACC;
250 ret = AUL_SVC_RET_EINVAL;
252 case AUL_R_ETERMINATING:
253 ret = AUL_SVC_RET_ETERMINATING;
255 case AUL_R_EREJECTED:
256 ret = AUL_SVC_RET_EREJECTED;
259 ret = AUL_SVC_RET_ENOMATCH;
262 ret = AUL_SVC_RET_ELAUNCH;
269 static int __get_resolve_info(bundle *b, aul_svc_resolve_info_t *info)
272 char *saveptr = NULL;
273 char *strtok_buf = NULL;
276 info->op = (char *)aul_svc_get_operation(b);
277 info->uri = (char *)aul_svc_get_uri(b);
279 if ((info->uri) && (strcmp(info->uri, "") == 0)) {
281 return AUL_SVC_RET_EINVAL;
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);
289 SECURE_LOGD("getting resolve info for: operation - %s / uri - %s / mime - %s\n",
290 info->op, info->uri, info->mime);
293 if (strncmp(info->uri, "/", 1) == 0) {
295 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
296 if (info->mime == NULL) {
298 return AUL_SVC_RET_ERROR;
301 ret = aul_get_mime_from_file(info->uri, info->mime, MAX_MIME_STR_SIZE);
305 } else if (strncmp(info->uri, "file:///", 8) == 0) {
307 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
308 if (info->mime == NULL) {
310 return AUL_SVC_RET_ERROR;
313 ret = aul_get_mime_from_file(&info->uri[7], info->mime, MAX_MIME_STR_SIZE);
316 } else if (strncmp(info->uri, "file:/", 6) == 0) {
318 info->origin_mime = info->mime = malloc(MAX_MIME_STR_SIZE);
319 if (info->mime == NULL) {
321 return AUL_SVC_RET_ERROR;
324 ret = aul_get_mime_from_file(&info->uri[5], info->mime, MAX_MIME_STR_SIZE);
329 if (info->mime_set == 1 && ret < 0) {
330 _E("aul_get_mime_from_file : %d", ret);
332 info->origin_mime = info->mime = NULL;
339 GMatchInfo *match_info;
340 GError *error = NULL;
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;
348 info->scheme = g_match_info_fetch(match_info, 2);
349 info->host = g_match_info_fetch(match_info, 4);
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) {
355 g_match_info_free(match_info);
356 g_regex_unref(regex);
357 return AUL_SVC_RET_ERROR;
360 snprintf(info->uri_r_info, MAX_SCHEME_STR_SIZE + MAX_HOST_STR_SIZE + 1,
361 "%s://%s", info->scheme, info->host);
364 g_match_info_free(match_info);
365 g_regex_unref(regex);
368 info->scheme = strdup("NULL");
372 info->mime = strdup("NULL");
376 info->m_type = calloc(1, MAX_LOCAL_BUFSZ);
377 if (info->m_type == NULL) {
379 return AUL_SVC_RET_ERROR;
382 info->s_type = calloc(1, MAX_LOCAL_BUFSZ);
383 if (info->s_type == NULL) {
386 return AUL_SVC_RET_ERROR;
389 tmp = strdup(info->mime);
394 return AUL_SVC_RET_ERROR;
397 strtok_buf = strtok_r(tmp, "/", &saveptr);
399 strncpy(info->m_type, strtok_buf, MAX_LOCAL_BUFSZ - 1);
400 strtok_buf = strtok_r(NULL, "/", &saveptr);
402 strncpy(info->s_type, strtok_buf, MAX_LOCAL_BUFSZ - 1);
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);
410 info->mime = malloc(MAX_MIME_STR_SIZE);
411 if (info->mime == NULL) {
415 return AUL_SVC_RET_ERROR;
418 snprintf(info->mime, MAX_MIME_STR_SIZE - 1,
419 "%s/%s", info->m_type, info->s_type);
424 static int __free_resolve_info_data(aul_svc_resolve_info_t *info)
436 if (info->uri_r_info)
437 free(info->uri_r_info);
439 free(info->origin_mime);
444 static char* __get_alias_appid(char *appid)
446 char *alias_id = NULL;
448 char key_string[MAX_PACKAGE_STR_SIZE + 5];
451 dic = iniparser_load("/usr/share/appsvc/alias.ini");
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);
461 SECURE_LOGD("alias_id : %s", val);
464 alias_id = malloc(MAX_PACKAGE_STR_SIZE);
465 if (alias_id == NULL) {
467 iniparser_freedict(dic);
471 strncpy(alias_id, val, MAX_PACKAGE_STR_SIZE - 1);
474 iniparser_freedict(dic);
479 static char* __make_query(char *query, char *op, char *uri,
480 char *mime, char *m_type, char *s_type)
482 char tmp[MAX_MIME_STR_SIZE] = { 0, };
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);
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);
498 static char* __make_query_with_collation(char *op, char *uri, char *mime, char *m_type, char *s_type)
500 char tmp[MAX_MIME_STR_SIZE];
503 query = _svc_db_query_builder_add(query, op, uri, mime, true);
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);
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);
514 query = _svc_db_query_builder_in("ac.app_control collate appsvc_collation ", query);
520 static int __app_list_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
523 GSList **app_list = (GSList **)user_data;
527 pkgmgrinfo_appinfo_get_appid(handle, &str);
528 _D("Matching application is %s", str);
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) {
533 *app_list = g_slist_append(*app_list, (void *)appid);
534 _D("%s is added", appid);
541 static int __get_list_with_category(char *category, GSList **pkg_list, uid_t uid)
544 pkgmgrinfo_appinfo_filter_h handle;
545 GSList *app_list = NULL;
547 char *list_item = NULL;
549 ret = pkgmgrinfo_appinfo_filter_create(&handle);
550 ret = pkgmgrinfo_appinfo_filter_add_string(handle,
551 PMINFO_APPINFO_PROP_APP_CATEGORY, category);
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);
560 pkgmgrinfo_appinfo_filter_destroy(handle);
562 for (iter = *pkg_list; iter != NULL; iter = g_slist_next(iter)) {
563 list_item = (char *)iter->data;
566 g_slist_free(*pkg_list);
568 *pkg_list = app_list;
573 static int __check_mainapp_mode(char *operation)
578 static int __get_list_with_submode(char *operation, char *win_id,
579 GSList **pkg_list, uid_t uid)
582 int mainapp_mode = 0;
584 mainapp_mode = __check_mainapp_mode(operation);
586 SECURE_LOGD("mainapp_mode : %d", mainapp_mode);
588 ret = _svc_db_adjust_list_with_submode(mainapp_mode, win_id, pkg_list, uid);
591 _E("error on get_list_with_submode :%d", ret);
598 static void __free_pkg_list(GSList *list)
606 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
607 list_item = (char *)iter->data;
613 static gchar *__make_checksum(const char *op, const char *uri, const char *mime)
615 char buf[MAX_CHECKSUM_BUF];
618 snprintf(buf, sizeof(buf), "%s:%s:%s", op, uri, mime);
619 checksum = g_compute_checksum_for_string(G_CHECKSUM_MD5, buf, -1);
624 static char *__get_cache(const char *checksum, uid_t uid)
626 app_pkt_t *pkt = NULL;
630 char buf[MAX_PID_STR_BUFSZ];
639 snprintf(buf, sizeof(buf), "%d", uid);
640 bundle_add(b, AUL_K_TARGET_UID, buf);
641 bundle_add(b, AUL_K_CHECKSUM, checksum);
643 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_APPID_FROM_CACHE,
649 ret = aul_sock_recv_reply_pkt(fd, &pkt);
653 if (pkt->cmd == APP_GET_APPID_FROM_CACHE) {
654 if (pkt->data[0] == 0) {
658 appid = strdup((const char *)(pkt->data));
667 static void __put_cache(const char *checksum, const char *appid, uid_t uid)
671 char buf[MAX_PID_STR_BUFSZ];
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);
684 ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_SET_CACHE, b);
687 _E("Failed to set cache : %d", ret);
692 static void __put_cache_with_info(const char *checksum, const char *info, uid_t uid)
696 snprintf(buf, sizeof(buf), "@APP_SELECTOR %s", info);
697 __put_cache(checksum, buf, uid);
700 static void __invalidate_cache(uid_t uid)
704 char buf[MAX_PID_STR_BUFSZ];
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);
717 _E("Failed to invalidate cache : %d", ret);
722 API int aul_svc_set_operation(bundle *b, const char *operation)
725 _E("bundle for aul_svc_set_operation is NULL");
726 return AUL_SVC_RET_EINVAL;
729 return __set_bundle(b, AUL_SVC_K_OPERATION, operation);
732 API int aul_svc_set_uri(bundle *b, const char *uri)
735 _E("bundle for aul_svc_set_uri is NULL");
736 return AUL_SVC_RET_EINVAL;
739 return __set_bundle(b, AUL_SVC_K_URI, uri);
742 API int aul_svc_set_mime(bundle *b, const char *mime)
745 _E("bundle for aul_svc_set_mime is NULL");
746 return AUL_SVC_RET_EINVAL;
749 return __set_bundle(b, AUL_SVC_K_MIME, mime);
752 API int aul_svc_add_data(bundle *b, const char *key, const char *val)
754 if (b == NULL || key == NULL)
755 return AUL_SVC_RET_EINVAL;
757 /* check key for data */
760 return __set_bundle(b, key, val);
763 API int aul_svc_add_data_array(bundle *b, const char *key,
764 const char **val_array, int len)
766 if (b == NULL || key == NULL)
767 return AUL_SVC_RET_EINVAL;
769 /* check key for data */
772 return __set_bundle_array(b, key, val_array, len);
775 API int aul_svc_set_pkgname(bundle *b, const char *pkg_name)
778 _E("bundle for aul_svc_set_pkgname is NULL");
779 return AUL_SVC_RET_EINVAL;
782 return __set_bundle(b, AUL_SVC_K_PKG_NAME, pkg_name);
785 API int aul_svc_set_appid(bundle *b, const char *appid)
787 char *alias_id = NULL;
790 if (b == NULL || appid == NULL) {
791 _E("bundle for aul_svc_set_appid is NULL");
792 return AUL_SVC_RET_EINVAL;
795 alias_id = __get_alias_appid((char *)appid);
796 if (alias_id == NULL) {
797 ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, appid);
799 ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, alias_id);
807 API int aul_svc_set_category(bundle *b, const char *category)
810 _E("bundle for aul_svc_set_category is NULL");
811 return AUL_SVC_RET_EINVAL;
814 return __set_bundle(b, AUL_SVC_K_CATEGORY, category);
817 API int aul_svc_set_launch_mode(bundle *b, const char *mode)
820 _E("bundle for aul_svc_set_launch_mode is NULL");
821 return AUL_SVC_RET_EINVAL;
824 return __set_bundle(b, AUL_SVC_K_LAUNCH_MODE, mode);
827 API int aul_svc_run_service(bundle *b, int request_code,
828 aul_svc_res_fn cbfunc, void *data)
830 return aul_svc_run_service_for_uid(b, request_code, cbfunc, data, getuid());
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)
836 aul_svc_resolve_info_t info;
844 GSList *pkg_list = NULL;
849 _E("bundle for aul_svc_set_appid is NULL");
850 return AUL_SVC_RET_EINVAL;
853 pkgname = (char *)aul_svc_get_pkgname(b);
854 operation = (char *)aul_svc_get_operation(b);
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,
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,
874 memset(&info, 0, sizeof(aul_svc_resolve_info_t));
875 ret = __get_resolve_info(b, &info);
877 __free_resolve_info_data(&info);
881 SECURE_LOGD("op - %s / mime - %s / scheme - %s\n",
882 info.op, info.origin_mime, info.scheme);
884 checksum = __make_checksum(info.op, info.uri, info.origin_mime);
885 appid = __get_cache(checksum, uid);
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,
894 } else if (!strcmp(appid, "^")) {
895 ret = AUL_SVC_RET_ENOMATCH;
897 ret = __run_svc_with_pkgname(appid, b, request_code,
902 __free_resolve_info_data(&info);
906 ret = _svc_db_check_perm(uid, true);
908 _E("permission error : %d", ret);
909 ret = AUL_SVC_RET_EILLACC;
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,
923 query = __make_query_with_collation(info.op, info.uri,
924 info.mime, info.m_type, info.s_type);
926 query = _svc_db_query_builder_build(query);
927 _svc_db_exec_query(query, &pkg_list, uid);
933 pkg_count = g_slist_length(pkg_list);
935 __free_pkg_list(pkg_list);
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);
942 query = __make_query(query, info.op, info.scheme,
943 info.mime, info.m_type, info.s_type);
945 query = __make_query(query, info.op, "*",
946 info.mime, info.m_type, info.s_type);
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);
954 query = _svc_db_query_builder_build(query);
955 _svc_db_exec_query(query, &pkg_list, uid);
962 __get_list_with_category(info.category, &pkg_list, uid);
964 __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
966 pkg_count = g_slist_length(pkg_list);
967 _D("pkg_count : %d", pkg_count);
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,
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,
984 __free_pkg_list(pkg_list);
989 if (info.uri_r_info) {
990 pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri_r_info, uid);
992 if (pkgname != NULL) {
993 __put_cache(checksum, pkgname, uid);
994 ret = __run_svc_with_pkgname(pkgname, b, request_code,
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);
1009 pkg_count = g_slist_length(pkg_list);
1010 if (pkg_count > 0) {
1011 __free_pkg_list(pkg_list);
1013 query = __make_query(query, info.op, "*",
1014 info.mime, info.m_type, info.s_type);
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);
1022 query = _svc_db_query_builder_build(query);
1023 _svc_db_exec_query(query, &pkg_list, uid);
1030 __get_list_with_category(info.category, &pkg_list, uid);
1032 __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1034 pkg_count = g_slist_length(pkg_list);
1035 _D("pkg_count : %d", pkg_count);
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,
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,
1053 __free_pkg_list(pkg_list);
1059 pkgname = _svc_db_get_app(info.op, info.origin_mime, info.scheme, uid);
1061 if (pkgname != NULL) {
1062 __put_cache(checksum, pkgname, uid);
1063 ret = __run_svc_with_pkgname(pkgname, b, request_code,
1069 query = __make_query(query, info.op, info.scheme,
1070 info.mime, info.m_type, info.s_type);
1072 query = __make_query(query, info.op, "*",
1073 info.mime, info.m_type, info.s_type);
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);
1081 query = _svc_db_query_builder_build(query);
1082 _svc_db_exec_query(query, &pkg_list, uid);
1090 __get_list_with_category(info.category, &pkg_list, uid);
1092 __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1094 pkg_count = g_slist_length(pkg_list);
1095 _D("pkg_count : %d", pkg_count);
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,
1104 } else if (pkg_count < 1) {
1105 __free_resolve_info_data(&info);
1106 __put_cache(checksum, "^", uid);
1108 return AUL_SVC_RET_ENOMATCH;
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,
1117 __free_pkg_list(pkg_list);
1118 __free_resolve_info_data(&info);
1124 API int aul_svc_get_list(bundle *b, aul_svc_info_iter_fn iter_fn,
1127 return aul_svc_get_list_for_uid(b, iter_fn, data, getuid());
1130 API int aul_svc_get_list_for_uid(bundle *b, aul_svc_info_iter_fn iter_fn,
1131 void *data, uid_t uid)
1133 aul_svc_resolve_info_t info;
1134 char *pkgname = NULL;
1138 GSList *pkg_list = NULL;
1139 GSList *iter = NULL;
1141 char *query2 = NULL;
1144 _E("bundle for aul_svc_run_service is NULL");
1145 return AUL_SVC_RET_EINVAL;
1148 if (iter_fn == NULL) {
1149 _E("iter_fn for aul_svc_run_service is NULL");
1150 return AUL_SVC_RET_EINVAL;
1155 memset(&info, 0, sizeof(aul_svc_resolve_info_t));
1156 ret = __get_resolve_info(b, &info);
1158 __free_resolve_info_data(&info);
1162 _D("operation - %s / shceme - %s / mime - %s\n", info.op, info.scheme,
1165 query2 = __make_query_with_collation(info.op, info.uri,
1166 info.mime, info.m_type, info.s_type);
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);
1173 query = __make_query(query, info.op, info.scheme,
1174 info.mime, info.m_type, info.s_type);
1176 query = __make_query(query, info.op, "*",
1177 info.mime, info.m_type, info.s_type);
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);
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);
1194 __get_list_with_category(info.category, &pkg_list, uid);
1196 __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
1198 pkg_count = g_slist_length(pkg_list);
1199 if (pkg_count == 0) {
1200 _E("Cannot find associated application");
1202 __free_resolve_info_data(&info);
1203 return AUL_SVC_RET_ENOMATCH;
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)
1214 g_slist_free(pkg_list);
1215 __free_resolve_info_data(&info);
1217 return AUL_SVC_RET_OK;
1220 API int aul_svc_get_all_defapps(aul_svc_info_iter_fn iter_fn, void *data)
1222 return aul_svc_get_all_defapps_for_uid(iter_fn, data, getuid());
1225 API int aul_svc_get_all_defapps_for_uid(aul_svc_info_iter_fn iter_fn,
1226 void *data, uid_t uid)
1228 char *pkgname = NULL;
1231 GSList *pkg_list = NULL;
1232 GSList *iter = NULL;
1235 ret = _svc_db_check_perm(uid, true);
1237 _E("permission error : %d", ret);
1238 return AUL_SVC_RET_EILLACC;
1241 ret = _svc_db_get_list_with_all_defapps(&pkg_list, uid);
1245 for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
1246 pkgname = iter->data;
1247 if (iter_fn(pkgname, data) != 0)
1252 g_slist_free(pkg_list);
1254 return AUL_SVC_RET_OK;
1257 API const char *aul_svc_get_operation(bundle *b)
1259 return bundle_get_val(b, AUL_SVC_K_OPERATION);
1262 API const char *aul_svc_get_uri(bundle *b)
1264 return bundle_get_val(b, AUL_SVC_K_URI);
1267 API const char *aul_svc_get_mime(bundle *b)
1269 return bundle_get_val(b, AUL_SVC_K_MIME);
1272 API const char *aul_svc_get_data(bundle *b, const char *key)
1274 return bundle_get_val(b, key);
1277 API const char **aul_svc_get_data_array(bundle *b, const char *key, int *len)
1279 return bundle_get_str_array(b, key, len);
1282 API const char *aul_svc_get_pkgname(bundle *b)
1284 return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
1287 API const char *aul_svc_get_appid(bundle *b)
1289 return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
1292 API const char *aul_svc_get_category(bundle *b)
1294 return bundle_get_val(b, AUL_SVC_K_CATEGORY);
1297 API const char *aul_svc_get_launch_mode(bundle *b)
1299 return bundle_get_val(b, AUL_SVC_K_LAUNCH_MODE);
1302 API int aul_svc_create_result_bundle(bundle *inb, bundle **outb)
1306 if (inb == NULL || outb == NULL) {
1307 _E("bundle is NULL");
1308 return AUL_SVC_RET_EINVAL;
1311 ret = aul_create_result_bundle(inb, outb);
1313 /* add additional bundle */
1314 /* bundle_add(outb, " ", " "); */
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;
1321 ret = AUL_SVC_RET_ERROR;
1326 API int aul_svc_send_result(bundle *b, aul_svc_result_val result)
1329 char tmp[MAX_LOCAL_BUFSZ];
1332 _E("aul_svc_send_result is NULL");
1333 return AUL_SVC_RET_EINVAL;
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);
1340 return AUL_SVC_RET_ERROR;
1342 if (result == AUL_SVC_RES_CANCEL)
1343 ret = aul_send_result(b, 1);
1345 ret = aul_send_result(b, 0);
1347 /* remove result_code from bundle */
1348 bundle_del(b, AUL_SVC_K_RES_VAL);
1353 API int aul_svc_set_defapp(const char *op, const char *mime_type,
1354 const char *uri, const char *defapp)
1356 return aul_svc_set_defapp_for_uid(op, mime_type, uri, defapp, getuid());
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)
1364 if (op == NULL || defapp == NULL)
1365 return AUL_SVC_RET_EINVAL;
1367 ret = _svc_db_check_perm(uid, false);
1369 _E("permission error : %d", ret);
1370 return AUL_SVC_RET_EILLACC;
1373 ret = _svc_db_add_app(op, mime_type, uri, defapp, uid);
1375 return AUL_SVC_RET_ERROR;
1377 return AUL_SVC_RET_OK;
1380 API int aul_svc_unset_defapp(const char *defapp)
1382 return aul_svc_unset_defapp_for_uid(defapp, getuid());
1385 API int aul_svc_unset_defapp_for_uid(const char *defapp, uid_t uid)
1390 return AUL_SVC_RET_EINVAL;
1392 ret = _svc_db_check_perm(uid, false);
1394 _E("permission error : %d", ret);
1395 return AUL_SVC_RET_EILLACC;
1398 ret = _svc_db_delete_with_pkgname(defapp, uid);
1401 return AUL_SVC_RET_ERROR;
1403 return AUL_SVC_RET_OK;
1406 API int aul_svc_unset_all_defapps()
1408 return aul_svc_unset_all_defapps_for_uid(getuid());
1411 API int aul_svc_unset_all_defapps_for_uid(uid_t uid)
1415 ret = _svc_db_check_perm(uid, false);
1417 _E("permission error : %d", ret);
1418 return AUL_SVC_RET_EILLACC;
1421 ret = _svc_db_delete_all(uid);
1424 return AUL_SVC_RET_ERROR;
1426 __invalidate_cache(uid);
1427 return AUL_SVC_RET_OK;
1430 API int aul_svc_is_defapp(const char *pkg_name)
1432 return aul_svc_is_defapp_for_uid(pkg_name, getuid());
1435 API int aul_svc_is_defapp_for_uid(const char *pkg_name, uid_t uid)
1439 ret = _svc_db_check_perm(uid, true);
1441 _E("permission error : %d", ret);
1442 return AUL_SVC_RET_EILLACC;
1445 return _svc_db_is_defapp(pkg_name, uid);
1448 API int aul_svc_data_is_array(bundle *b, const char *key)
1451 type = bundle_get_type(b, key);
1456 if (type & BUNDLE_TYPE_ARRAY)
1461 API int aul_svc_allow_transient_app(bundle *b, int wid)
1463 char win_id[MAX_LOCAL_BUFSZ];
1465 snprintf(win_id, MAX_LOCAL_BUFSZ, "%d", wid);
1468 _E("bundle for aul_svc_allow_transient_app is NULL");
1469 return AUL_SVC_RET_EINVAL;
1472 return __set_bundle(b, AUL_SVC_K_WIN_ID, win_id);
1475 API int aul_svc_request_transient_app(bundle *b, int callee_wid,
1476 aul_svc_host_res_fn cbfunc, void *data)
1481 API int aul_svc_subapp_terminate_request_pid(int pid)
1483 int cpid = getpid();
1488 aul_app_group_get_leader_pids(&lcnt, &lpids);
1489 for (i = 0; i < lcnt; i++) {
1490 if (lpids[i] == cpid) {
1494 aul_app_group_get_group_pids(cpid, &cnt, &pids);
1501 return aul_subapp_terminate_request_pid(pid);
1513 return aul_app_group_clear_top();
1516 API int aul_send_service_result(bundle *b)
1518 return aul_send_result(b, 0);
1521 API int aul_svc_subscribe_launch_result(bundle *b, const char *result)
1524 _E("bundle for aul_svc_subscribe_launch_result is NULL");
1525 return AUL_SVC_RET_EINVAL;
1528 return __set_bundle(b, result, "1");
1531 API int aul_svc_set_loader_id(bundle *b, int loader_id)
1533 char tmp[MAX_LOCAL_BUFSZ];
1536 _E("bundle for aul_svc_set_loader_id is NULL");
1537 return AUL_SVC_RET_EINVAL;
1540 if (loader_id <= 0) {
1541 _E("invalid loader id");
1542 return AUL_SVC_RET_EINVAL;
1545 snprintf(tmp, sizeof(tmp), "%d", loader_id);
1546 return __set_bundle(b, AUL_K_LOADER_ID, tmp);
1549 API int aul_svc_set_loader_name(bundle *b, const char *loader_name)
1552 _E("bundle for aul_svc_set_loader_name is NULL");
1553 return AUL_SVC_RET_EINVAL;
1557 _E("invalid loader name");
1558 return AUL_SVC_RET_EINVAL;
1561 return __set_bundle(b, AUL_K_LOADER_NAME, loader_name);
1564 API int aul_svc_set_background_launch(bundle *b, int enabled)
1567 return AUL_R_EINVAL;
1569 if (bundle_get_type(b, AUL_SVC_K_BG_LAUNCH) != BUNDLE_TYPE_NONE)
1570 bundle_del(b, AUL_SVC_K_BG_LAUNCH);
1573 bundle_add_str(b, AUL_SVC_K_BG_LAUNCH, "enable");
1578 API int aul_svc_set_alias_appid(const char *alias_appid, const char *appid)
1580 return aul_svc_set_alias_appid_for_uid(alias_appid, appid, getuid());
1583 API int aul_svc_set_alias_appid_for_uid(const char *alias_appid,
1584 const char *appid, uid_t uid)
1588 ret = _svc_db_check_perm(uid, false);
1590 _E("Permission error: %d", ret);
1591 return AUL_SVC_RET_EILLACC;
1594 ret = _svc_db_add_alias_appid(alias_appid, appid, uid);
1596 return AUL_SVC_RET_ERROR;
1598 return AUL_SVC_RET_OK;
1601 API int aul_svc_unset_alias_appid(const char *alias_appid)
1603 return aul_svc_unset_alias_appid_for_uid(alias_appid, getuid());
1606 API int aul_svc_unset_alias_appid_for_uid(const char *alias_appid, uid_t uid)
1610 ret = _svc_db_check_perm(uid, false);
1612 _E("Permission error: %d", ret);
1613 return AUL_SVC_RET_EILLACC;
1616 ret = _svc_db_delete_alias_appid(alias_appid, uid);
1618 return AUL_SVC_RET_ERROR;
1620 return AUL_SVC_RET_OK;
1623 API int aul_svc_foreach_alias_info(void (*callback)(const char *alias_appid,
1624 const char *appid, void *data), void *user_data)
1626 return aul_svc_foreach_alias_info_for_uid(callback, getuid(),
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)
1636 if (callback == NULL) {
1637 _E("Invalid parameter");
1638 return AUL_SVC_RET_EINVAL;
1641 ret = _svc_db_check_perm(uid, true);
1643 _E("Permission error: %d", ret);
1644 return AUL_SVC_RET_EILLACC;
1647 ret = _svc_db_foreach_alias_info(callback, uid, user_data);
1649 return AUL_SVC_RET_ERROR;
1651 return AUL_SVC_RET_OK;
1654 API int aul_svc_enable_alias_info(const char *appid)
1656 return aul_svc_enable_alias_info_for_uid(appid, getuid());
1659 API int aul_svc_enable_alias_info_for_uid(const char *appid, uid_t uid)
1663 ret = _svc_db_check_perm(uid, false);
1665 _E("Permission error: %d", ret);
1666 return AUL_SVC_RET_EILLACC;
1669 ret = _svc_db_enable_alias_info(appid, uid);
1671 return AUL_SVC_RET_ERROR;
1673 return AUL_SVC_RET_OK;
1676 API int aul_svc_disable_alias_info(const char *appid)
1678 return aul_svc_disable_alias_info_for_uid(appid, getuid());
1681 API int aul_svc_disable_alias_info_for_uid(const char *appid, uid_t uid)
1685 ret = _svc_db_check_perm(uid, false);
1687 _E("Permission error: %d", ret);
1688 return AUL_SVC_RET_EILLACC;
1691 ret = _svc_db_disable_alias_info(appid, uid);
1693 return AUL_SVC_RET_ERROR;
1695 return AUL_SVC_RET_OK;
1698 API int aul_svc_get_appid_by_alias_appid(const char *alias_appid, char **appid)
1700 return aul_svc_get_appid_by_alias_appid_for_uid(alias_appid,
1704 API int aul_svc_get_appid_by_alias_appid_for_uid(const char *alias_appid,
1705 char **appid, uid_t uid)
1709 ret = _svc_db_check_perm(uid, true);
1711 _E("Permission error: %d", ret);
1712 return AUL_SVC_RET_EILLACC;
1715 ret = _svc_db_get_appid_from_alias_info(alias_appid, appid, uid);
1717 return AUL_SVC_RET_ERROR;
1719 return AUL_SVC_RET_OK;
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)
1726 return aul_svc_foreach_alias_info_by_appid_for_uid(callback, appid,
1727 getuid(), user_data);
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)
1736 if (callback == NULL || appid == NULL) {
1737 _E("Invalid parameter");
1738 return AUL_SVC_RET_EINVAL;
1741 ret = _svc_db_check_perm(uid, true);
1743 _E("Permission error: %d", ret);
1744 return AUL_SVC_RET_EILLACC;
1747 ret = _svc_db_foreach_alias_info_by_appid(callback, appid,
1750 return AUL_SVC_RET_ERROR;
1752 return AUL_SVC_RET_OK;
1755 API int aul_svc_foreach_allowed_info(int (*callback)(const char *appid,
1756 const char *allowed_appid, void *data), void *user_data)
1758 return aul_svc_foreach_allowed_info_for_uid(callback,
1759 getuid(), user_data);
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)
1768 if (callback == NULL) {
1769 _E("Invalid parameter");
1770 return AUL_SVC_RET_EINVAL;
1773 ret = _svc_db_check_perm(uid, true);
1775 _E("Permission error: %d", ret);
1776 return AUL_SVC_RET_EILLACC;
1779 ret = _svc_db_foreach_allowed_info(callback, uid, user_data);
1781 return AUL_SVC_RET_ERROR;
1783 return AUL_SVC_RET_OK;
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)
1790 return aul_svc_foreach_allowed_info_by_appid_for_uid(callback,
1791 appid, getuid(), user_data);
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)
1800 if (callback == NULL || appid == NULL) {
1801 _E("Invalid parameter");
1802 return AUL_SVC_RET_EINVAL;
1805 ret = _svc_db_check_perm(uid, true);
1807 _E("Permission error: %d", ret);
1808 return AUL_SVC_RET_EILLACC;
1811 ret = _svc_db_foreach_allowed_info_by_appid(callback, appid,
1814 return AUL_SVC_RET_ERROR;
1816 return AUL_SVC_RET_OK;
1819 API const char *aul_svc_get_instance_id(bundle *b)
1821 return bundle_get_val(b, AUL_K_INSTANCE_ID);
1824 API int aul_svc_set_instance_id(bundle *b, const char *instance_id)
1826 if (b == NULL || instance_id == NULL) {
1827 _E("Invalid parameter");
1828 return AUL_SVC_RET_EINVAL;
1831 return __set_bundle(b, AUL_K_INSTANCE_ID, instance_id);