2 * Copyright (c) 2000 - 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.
21 #include <bundle_internal.h>
25 #include "menu_db_util.h"
29 #include "aul_error.h"
31 typedef struct _internal_param_t {
32 aul_app_info_iter_fn iter_fn;
36 static const char *__appid;
37 static const char *__pkgid;
38 static const char *__root_path;
40 API int aul_app_get_pid(const char *appid)
42 return aul_app_get_pid_for_uid(appid, getuid());
45 API int aul_app_get_pid_for_uid(const char *appid, uid_t uid)
48 char buf[MAX_PID_STR_BUFSZ];
60 snprintf(buf, sizeof(buf), "%d", uid);
61 bundle_add(b, AUL_K_APPID, appid);
62 bundle_add(b, AUL_K_TARGET_UID, buf);
64 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_PID,
71 API int aul_app_is_running(const char *appid)
73 return aul_app_is_running_for_uid(appid, getuid());
76 API int aul_app_is_running_for_uid(const char *appid, uid_t uid)
79 char buf[MAX_PID_STR_BUFSZ];
91 snprintf(buf, sizeof(buf), "%d", uid);
92 bundle_add(b, AUL_K_APPID, appid);
93 bundle_add(b, AUL_K_TARGET_UID, buf);
95 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_IS_RUNNING,
104 static void __running_app_info_cb(app_pkt_t *pkt, void *user_data)
106 internal_param_t *param = (internal_param_t *)user_data;
111 if (pkt == NULL || param == NULL) {
112 _E("Invalid parameter");
116 if (pkt->cmd == APP_GET_INFO_ERROR) {
117 _E("Failed to get app info");
121 if (pkt->opt & AUL_SOCK_BUNDLE)
122 b = bundle_decode(pkt->data, pkt->len);
127 val = bundle_get_val(b, AUL_K_PID);
132 info.pid = atoi(val);
134 info.appid = (char *)bundle_get_val(b, AUL_K_APPID);
135 info.app_path = (char *)bundle_get_val(b, AUL_K_EXEC);
136 info.pkgid = (char *)bundle_get_val(b, AUL_K_PKGID);
137 info.instance_id = (char *)bundle_get_val(b, AUL_K_INSTANCE_ID);
139 val = bundle_get_val(b, AUL_K_STATUS);
144 info.status = atoi(val);
146 val = bundle_get_val(b, AUL_K_IS_SUBAPP);
151 info.is_sub_app = atoi(val);
153 info.pkg_name = info.appid;
154 param->iter_fn(&info, param->user_data);
158 static int __get_running_app_info(int cmd, aul_app_info_iter_fn iter_fn,
159 void *user_data, uid_t uid)
164 char buf[MAX_PID_STR_BUFSZ];
165 internal_param_t param = {iter_fn, user_data};
176 snprintf(buf, sizeof(buf), "%d", uid);
177 bundle_add(b, AUL_K_TARGET_UID, buf);
179 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, cmd, b, AUL_SOCK_ASYNC);
182 return aul_error_convert(fd);
184 ret = aul_sock_recv_pkt_with_cb(fd, __running_app_info_cb, ¶m);
186 return aul_error_convert(ret);
191 API int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
194 return aul_app_get_running_app_info_for_uid(iter_fn,
195 user_data, getuid());
198 API int aul_app_get_running_app_info_for_uid(aul_app_info_iter_fn iter_fn,
199 void *user_data, uid_t uid)
201 return __get_running_app_info(APP_RUNNING_INFO, iter_fn,
205 API int aul_app_get_all_running_app_info(aul_app_info_iter_fn iter_fn,
208 return aul_app_get_all_running_app_info_for_uid(iter_fn,
209 user_data, getuid());
212 API int aul_app_get_all_running_app_info_for_uid(aul_app_info_iter_fn iter_fn,
213 void *user_data, uid_t uid)
215 return __get_running_app_info(APP_ALL_RUNNING_INFO, iter_fn,
219 API int aul_app_get_running_app_instance_info(aul_app_info_iter_fn iter_fn,
222 return aul_app_get_running_app_instance_info_for_uid(iter_fn,
223 user_data, getuid());
226 API int aul_app_get_running_app_instance_info_for_uid(
227 aul_app_info_iter_fn iter_fn, void *user_data, uid_t uid)
229 return __get_running_app_info(APP_RUNNING_INSTANCE_INFO, iter_fn,
233 API void aul_set_preinit_appid(const char *appid)
238 const char *__get_preinit_appid(void)
241 __appid = getenv("AUL_APPID");
246 API void aul_set_preinit_pkgid(const char *pkgid)
251 const char *__get_preinit_pkgid(void)
254 __pkgid = getenv("AUL_PKGID");
259 API void aul_set_preinit_root_path(const char *root_path)
261 __root_path = root_path;
264 API const char *aul_get_preinit_root_path(void)
267 __root_path = getenv("AUL_ROOT_PATH");
272 API int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
274 return aul_app_get_appid_bypid(pid, pkgname, len);
277 API int aul_app_get_appid_bypid_for_uid(int pid, char *appid, int len,
280 app_pkt_t *pkt = NULL;
283 const char *preinit_appid;
285 char buf[MAX_PID_STR_BUFSZ];
287 if (pid <= 0 || appid == NULL) {
288 _E("Invalid parameter");
292 if (getpid() == pid) {
293 preinit_appid = __get_preinit_appid();
295 snprintf(appid, len, "%s", preinit_appid);
306 snprintf(buf, sizeof(buf), "%d", pid);
307 bundle_add(b, AUL_K_PID, buf);
308 snprintf(buf, sizeof(buf), "%d", uid);
309 bundle_add(b, AUL_K_TARGET_UID, buf);
311 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_APPID_BYPID,
317 ret = aul_sock_recv_reply_pkt(fd, &pkt);
318 if (ret < 0 || pkt == NULL)
321 if (pkt->cmd == APP_GET_INFO_OK) {
322 snprintf(appid, len, "%s", pkt->data);
331 API int aul_app_get_appid_bypid(int pid, char *appid, int len)
333 return aul_app_get_appid_bypid_for_uid(pid, appid, len, getuid());
336 static int __get_pkginfo(int pid, char *buf, int len, uid_t uid)
339 app_info_from_db *menu_info;
341 appid = __get_preinit_appid();
343 _E("Failed to get preinit appid - %d", pid);
347 menu_info = _get_app_info_from_db_by_appid_user(appid, uid);
348 if (menu_info == NULL) {
349 _E("Failed to get app info - %s", appid);
353 snprintf(buf, len, "%s", _get_pkgid(menu_info));
354 _free_app_info_from_db(menu_info);
359 API int aul_app_get_pkgid_bypid_for_uid(int pid, char *pkgid, int len,
362 app_pkt_t *pkt = NULL;
365 const char *preinit_pkgid;
367 char buf[MAX_PID_STR_BUFSZ];
369 if (pid <= 0 || pkgid == NULL) {
370 _E("Invalid parameter");
374 if (getpid() == pid) {
375 preinit_pkgid = __get_preinit_pkgid();
377 snprintf(pkgid, len, "%s", preinit_pkgid);
380 /* fallback (for debugging) */
381 ret = __get_pkginfo(pid, pkgid, len, uid);
393 snprintf(buf, sizeof(buf), "%d", pid);
394 bundle_add(b, AUL_K_PID, buf);
395 snprintf(buf, sizeof(buf), "%d", uid);
396 bundle_add(b, AUL_K_TARGET_UID, buf);
398 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_PKGID_BYPID,
404 ret = aul_sock_recv_reply_pkt(fd, &pkt);
405 if (ret < 0 || pkt == NULL)
408 if (pkt->cmd == APP_GET_INFO_OK) {
409 snprintf(pkgid, len, "%s", pkt->data);
418 API int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len)
420 return aul_app_get_pkgid_bypid_for_uid(pid, pkgid, len, getuid());
423 API int aul_update_rua_stat_for_uid(bundle *b, uid_t uid)
426 char buf[MAX_PID_STR_BUFSZ];
429 _E("Invalid parameter");
433 snprintf(buf, sizeof(buf), "%d", uid);
434 bundle_del(b, AUL_K_TARGET_UID);
435 bundle_add(b, AUL_K_TARGET_UID, buf);
437 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid,
438 APP_UPDATE_RUA_STAT, b, AUL_SOCK_NONE);
442 API int aul_add_rua_history_for_uid(bundle *b, uid_t uid)
445 char buf[MAX_PID_STR_BUFSZ];
448 SECURE_LOGE("invalid param");
452 snprintf(buf, sizeof(buf), "%d", uid);
453 bundle_del(b, AUL_K_TARGET_UID);
454 bundle_add(b, AUL_K_TARGET_UID, buf);
456 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid,
457 APP_ADD_HISTORY, b, AUL_SOCK_NONE);
461 API int aul_delete_rua_history_for_uid(bundle *b, uid_t uid)
464 char buf[MAX_PID_STR_BUFSZ];
468 kb = bundle_create();
477 snprintf(buf, sizeof(buf), "%d", uid);
478 bundle_del(b, AUL_K_TARGET_UID);
479 bundle_add(b, AUL_K_TARGET_UID, buf);
481 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_REMOVE_HISTORY,
489 API int aul_set_default_app_by_operation(bundle *b)
496 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
497 APP_SET_APP_CONTROL_DEFAULT_APP, b, AUL_SOCK_NONE);
499 if (ret == -EILLEGALACCESS)
500 return AUL_R_EILLACC;
508 API int aul_unset_default_app_by_operation(const char *app_id)
515 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(), APP_UNSET_APP_CONTROL_DEFAULT_APP,
516 (unsigned char *)app_id, strlen(app_id), AUL_SOCK_NONE);
518 if (ret == -EILLEGALACCESS)
519 return AUL_R_EILLACC;
527 API int aul_app_get_last_caller_pid(int pid)
529 return aul_app_get_last_caller_pid_for_uid(pid, getuid());
532 API int aul_app_get_last_caller_pid_for_uid(int pid, uid_t uid)
535 char buf[MAX_PID_STR_BUFSZ];
539 _E("Invalid parameter");
545 _E("Failed to create bundle");
549 snprintf(buf, sizeof(buf), "%d", pid);
550 bundle_add(b, AUL_K_PID, buf);
551 snprintf(buf, sizeof(buf), "%d", uid);
552 bundle_add(b, AUL_K_TARGET_UID, buf);
554 ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_LAST_CALLER_PID,
558 return aul_error_convert(ret);
563 API int aul_set_alias_appid(const char *alias_appid, const char *appid)
568 if (alias_appid == NULL || appid == NULL) {
569 _E("Invalid parameters");
578 bundle_add(b, AUL_K_ALIAS_APPID, alias_appid);
579 bundle_add(b, AUL_K_APPID, appid);
581 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
582 APP_SET_ALIAS_APPID, b, AUL_SOCK_NONE);
585 return aul_error_convert(ret);
590 API int aul_unset_alias_appid(const char *alias_appid)
595 if (alias_appid == NULL) {
596 _E("Invalid parameter");
605 bundle_add(b, AUL_K_ALIAS_APPID, alias_appid);
607 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
608 APP_UNSET_ALIAS_APPID, b, AUL_SOCK_NONE);
611 return aul_error_convert(ret);
616 API int aul_enable_alias_info(const char *appid)
622 _E("Invalid parameter");
631 bundle_add(b, AUL_K_APPID, appid);
633 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
634 APP_ENABLE_ALIAS_INFO, b, AUL_SOCK_NONE);
637 return aul_error_convert(ret);
642 API int aul_disable_alias_info(const char *appid)
648 _E("Invalid parameter");
657 bundle_add(b, AUL_K_APPID, appid);
659 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
660 APP_DISABLE_ALIAS_INFO, b, AUL_SOCK_NONE);
663 return aul_error_convert(ret);
668 API int aul_app_get_instance_id_bypid_for_uid(int pid, char *instance_id,
671 app_pkt_t *pkt = NULL;
675 char buf[MAX_PID_STR_BUFSZ];
677 if (pid <= 0 || instance_id == NULL) {
678 _E("Invalid parameter");
688 snprintf(buf, sizeof(buf), "%d", pid);
689 bundle_add(b, AUL_K_PID, buf);
690 snprintf(buf, sizeof(buf), "%d", uid);
691 bundle_add(b, AUL_K_TARGET_UID, buf);
693 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid,
694 APP_GET_INSTANCE_ID_BYPID, b,
700 ret = aul_sock_recv_reply_pkt(fd, &pkt);
701 if (ret < 0 || pkt == NULL)
704 if (pkt->cmd == APP_GET_INFO_OK) {
705 snprintf(instance_id, len, "%s", pkt->data);
715 API int aul_app_get_instance_id_bypid(int pid, char *instance_id, int len)
717 return aul_app_get_instance_id_bypid_for_uid(pid,
718 instance_id, len, getuid());
721 API int aul_app_is_running_with_instance_id(const char *appid,
722 const char *instance_id, bool *running)
727 if (!appid || !instance_id) {
728 _E("Invalid parameter");
738 bundle_add(b, AUL_K_APPID, appid);
739 bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
741 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_IS_RUNNING,
745 return aul_error_convert(ret);
747 *running = (bool)ret;