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.
18 #include <sys/types.h>
31 #include <bundle_internal.h>
33 #include "app_signal.h"
40 #include "aul_app_com.h"
41 #include "aul_error.h"
43 #define TEP_ISMOUNT_MAX_RETRY_CNT 20
45 static int aul_initialized = 0;
47 static void *__window_object = NULL;
48 static void *__bg_object = NULL;
49 static void *__conformant_object = NULL;
51 static void __clear_internal_key(bundle *kb);
52 static inline void __set_stime(bundle *kb);
54 int aul_is_initialized()
56 return aul_initialized;
59 static int __send_cmd_for_uid_opt(int pid, uid_t uid, int cmd, bundle *kb, int opt)
63 res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
65 res = aul_error_convert(res);
70 static int __send_cmd_noreply_for_uid_opt(int pid, uid_t uid,
71 int cmd, bundle *kb, int opt)
75 res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_NOREPLY);
77 res = aul_error_convert(res);
82 static int __send_cmd_async_for_uid_opt(int pid, uid_t uid,
83 int cmd, bundle *kb, int opt)
87 res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_ASYNC);
89 res = aul_error_convert(res);
95 * @brief encode kb and send it to 'pid'
96 * @param[in] pid receiver's pid
97 * @param[in] cmd message's status (APP_START | APP_RESULT)
100 API int app_send_cmd(int pid, int cmd, bundle *kb)
102 return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
105 API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
107 return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_NONE);
110 API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
112 return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
115 API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
118 return __send_cmd_noreply_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
121 API int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
123 return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
126 API int app_send_cmd_to_launchpad(const char *pad_type, uid_t uid, int cmd, bundle *kb)
133 fd = aul_sock_create_launchpad_client(pad_type, uid);
137 res = aul_sock_send_bundle_with_fd(fd, cmd,
145 len = recv(fd, &res, sizeof(int), 0);
147 if (errno == EAGAIN) {
148 _E("recv timeout: %d(%s)",
150 strerror_r(errno, buf, sizeof(buf)));
152 } else if (errno == EINTR) {
155 strerror_r(errno, buf, sizeof(buf)));
158 _E("recv error: %d(%s)",
160 strerror_r(errno, buf, sizeof(buf)));
170 static void __clear_internal_key(bundle *kb)
172 bundle_del(kb, AUL_K_CALLER_PID);
173 bundle_del(kb, AUL_K_APPID);
174 bundle_del(kb, AUL_K_WAIT_RESULT);
175 bundle_del(kb, AUL_K_SEND_RESULT);
176 bundle_del(kb, AUL_K_ARGV0);
179 static inline void __set_stime(bundle *kb)
182 char tmp[MAX_LOCAL_BUFSZ];
184 gettimeofday(&tv, NULL);
185 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
186 bundle_del(kb, AUL_K_STARTTIME);
187 bundle_add(kb, AUL_K_STARTTIME, tmp);
190 int app_request_local(int cmd, bundle *kb)
194 _E("app_request_to_launchpad : Same Process Send Local");
199 case APP_START_ASYNC:
201 case APP_START_RES_ASYNC:
202 case APP_SEND_LAUNCH_REQUEST:
204 return aul_launch_local(b);
207 case APP_RESUME_BY_PID:
208 case APP_RESUME_BY_PID_ASYNC:
209 return aul_resume_local();
211 _E("no support packet");
217 * @brief start caller with kb
218 * @return callee's pid
220 int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
222 return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
225 int app_request_to_launchpad_for_uid(int cmd, const char *appid, bundle *kb, uid_t uid)
229 char buf[MAX_PID_STR_BUFSZ];
231 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:REQ_TO_PAD");
232 _W("request cmd(%d) : appid(%s), target_uid(%d)", cmd, appid, uid);
234 kb = bundle_create();
237 __clear_internal_key(kb);
240 bundle_del(kb, AUL_K_APPID);
241 bundle_add(kb, AUL_K_APPID, appid);
243 snprintf(buf, sizeof(buf), "%d", uid);
244 bundle_del(kb, AUL_K_TARGET_UID);
245 bundle_add(kb, AUL_K_TARGET_UID, buf);
249 case APP_PAUSE_BY_PID:
250 ret = app_send_cmd_with_queue_noreply_for_uid(AUL_UTIL_PID,
253 case APP_SEND_LAUNCH_REQUEST:
254 case APP_SEND_LAUNCH_REQUEST_SYNC:
255 ret = __send_cmd_async_for_uid_opt(AUL_UTIL_PID,
256 uid, cmd, kb, AUL_SOCK_QUEUE);
259 ret = app_send_cmd_with_queue_for_uid(AUL_UTIL_PID, uid, cmd,
264 _W("request cmd(%d) result : %d", cmd, ret);
265 if (ret == AUL_R_LOCAL)
266 ret = app_request_local(cmd, kb);
272 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
277 static int __get_preinit_fd(void)
280 const char *listen_fd;
282 listen_fd = getenv("AUL_LISTEN_FD");
284 if (isdigit(*listen_fd))
285 fd = atoi(listen_fd);
286 unsetenv("AUL_LISTEN_FD");
297 return AUL_R_ECANCELED;
299 aul_fd = __get_preinit_fd();
300 if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
301 flag = fcntl(aul_fd, F_GETFD);
303 (void)fcntl(aul_fd, F_SETFD, flag);
305 _W("Failed to get preinit fd");
306 aul_fd = aul_sock_create_server(getpid(), getuid());
308 _E("aul_init create sock failed");
319 API void aul_finalize()
323 if (aul_initialized) {
324 aul_sock_destroy_server(aul_fd);
331 API int aul_request_data_control_socket_pair(bundle *kb, int *fd)
342 __clear_internal_key(b);
349 clifd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_GET_DC_SOCKET_PAIR, b, AUL_SOCK_ASYNC);
354 ret = aul_sock_recv_result_with_fd(clifd);
357 if (ret == -EILLEGALACCESS) {
358 _E("Illegal access in datacontrol socket pair request");
359 return AUL_R_EILLACC;
364 ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
374 API int aul_request_message_port_socket_pair(int *fd)
382 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
383 APP_GET_MP_SOCKET_PAIR, NULL, 0, AUL_SOCK_ASYNC);
385 ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
395 API int aul_launch_app(const char *appid, bundle *kb)
397 return aul_launch_app_for_uid(appid, kb, getuid());
400 API int aul_launch_app_for_uid(const char *appid, bundle *kb, uid_t uid)
407 ret = app_request_to_launchpad_for_uid(APP_START, appid, kb, uid);
411 API int aul_open_app(const char *appid)
413 return aul_open_app_for_uid(appid, getuid());
416 API int aul_open_app_for_uid(const char *appid, uid_t uid)
423 ret = app_request_to_launchpad_for_uid(APP_OPEN, appid, NULL, uid);
427 API int aul_resume_app(const char *appid)
429 return aul_resume_app_for_uid(appid, getuid());
432 API int aul_resume_app_for_uid(const char *appid, uid_t uid)
439 ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, NULL, uid);
443 API int aul_resume_pid(int pid)
445 return aul_resume_pid_for_uid(pid, getuid());
448 API int aul_resume_pid_for_uid(int pid, uid_t uid)
450 char pid_str[MAX_PID_STR_BUFSZ];
456 snprintf(pid_str, sizeof(pid_str), "%d", pid);
457 ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID,
462 API int aul_terminate_pid(int pid)
464 return aul_terminate_pid_for_uid(pid, getuid());
467 API int aul_terminate_pid_for_uid(int pid, uid_t uid)
469 char pid_str[MAX_PID_STR_BUFSZ];
475 snprintf(pid_str, sizeof(pid_str), "%d", pid);
476 ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID,
484 API int aul_terminate_bgapp_pid(int pid)
486 char pid_str[MAX_PID_STR_BUFSZ];
492 snprintf(pid_str, sizeof(pid_str), "%d", pid);
493 ret = app_request_to_launchpad(APP_TERM_BGAPP_BY_PID, pid_str, NULL);
500 API int aul_terminate_pid_without_restart(int pid)
502 char pid_str[MAX_PID_STR_BUFSZ];
508 snprintf(pid_str, sizeof(pid_str), "%d", pid);
509 ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART,
514 API int aul_terminate_pid_sync_without_restart(int pid)
516 return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
519 API int aul_terminate_pid_sync_without_restart_for_uid(int pid, uid_t uid)
521 char pid_str[MAX_PID_STR_BUFSZ];
527 snprintf(pid_str, sizeof(pid_str), "%d", pid);
528 ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART,
533 API int aul_terminate_pid_async(int pid)
535 return aul_terminate_pid_async_for_uid(pid, getuid());
538 API int aul_terminate_pid_async_for_uid(int pid, uid_t uid)
540 char pid_str[MAX_PID_STR_BUFSZ];
546 snprintf(pid_str, sizeof(pid_str), "%d", pid);
547 ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_ASYNC, pid_str,
552 API int aul_kill_pid(int pid)
554 char pid_str[MAX_PID_STR_BUFSZ];
560 snprintf(pid_str, sizeof(pid_str), "%d", pid);
561 ret = app_request_to_launchpad(APP_KILL_BY_PID, pid_str, NULL);
565 API void aul_set_preinit_window(void *evas_object)
567 __window_object = evas_object;
570 API void* aul_get_preinit_window(const char *win_name)
572 return __window_object;
575 API void aul_set_preinit_background(void *evas_object)
577 __bg_object = evas_object;
580 API void* aul_get_preinit_background(void)
585 API void aul_set_preinit_conformant(void *evas_object)
587 __conformant_object = evas_object;
590 API void* aul_get_preinit_conformant(void)
592 return __conformant_object;
595 API int aul_pause_app(const char *appid)
597 return aul_pause_app_for_uid(appid, getuid());
600 API int aul_pause_app_for_uid(const char *appid, uid_t uid)
607 ret = app_request_to_launchpad_for_uid(APP_PAUSE, appid, NULL, uid);
611 API int aul_pause_pid(int pid)
613 return aul_pause_pid_for_uid(pid, getuid());
616 API int aul_pause_pid_for_uid(int pid, uid_t uid)
618 char pid_str[MAX_PID_STR_BUFSZ];
624 snprintf(pid_str, sizeof(pid_str), "%d", pid);
625 ret = app_request_to_launchpad_for_uid(APP_PAUSE_BY_PID,
630 API int aul_reload_appinfo(void)
632 char pid_str[MAX_PID_STR_BUFSZ];
634 snprintf(pid_str, sizeof(pid_str), "%d", getpid());
636 return app_request_to_launchpad(AMD_RELOAD_APPINFO, pid_str, NULL);
639 API int aul_is_tep_mount_dbus_done(const char *tep_string)
642 GDBusConnection *conn;
643 GDBusMessage *msg = NULL;
644 GDBusMessage *reply = NULL;
646 int ret = AUL_R_ERROR;
648 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
650 _E("g_bus_get_sync() is failed. %s", err->message);
655 msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
658 TEP_IS_MOUNTED_METHOD);
660 _E("g_dbus_message_new_method_call() is failed. %s",
664 g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
666 reply = g_dbus_connection_send_message_with_reply_sync(conn,
668 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
674 _E("g_dbus_connection_send_message_with_reply_sync() "
675 "is failed. %s", err->message);
679 body = g_dbus_message_get_body(reply);
681 _E("g_dbus_message_get_body() is failed.");
685 g_variant_get(body, "(i)", &ret);
691 g_object_unref(reply);
693 g_object_unref(conn);
700 API int aul_check_tep_mount(const char *tep_path)
705 while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
706 rv = aul_is_tep_mount_dbus_done(tep_path);
712 /* incase after trying 1 sec, not getting mounted then quit */
714 _E("Not able to mount within 1 sec");
721 API int aul_add_loader(const char *loader_path, bundle *kb)
723 return aul_add_loader_for_uid(loader_path, kb, getuid());
726 API int aul_add_loader_for_uid(const char *loader_path, bundle *kb, uid_t uid)
730 bundle_raw *kb_raw = NULL;
732 char buf[MAX_PID_STR_BUFSZ];
734 if (loader_path == NULL)
741 snprintf(buf, sizeof(buf), "%d", uid);
742 bundle_add_str(b, AUL_K_TARGET_UID, buf);
743 bundle_add_str(b, AUL_K_LOADER_PATH, loader_path);
746 ret = bundle_encode(kb, &kb_raw, &len);
747 if (ret != BUNDLE_ERROR_NONE) {
752 bundle_add_str(b, AUL_K_LOADER_EXTRA, (const char *)kb_raw);
755 ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_ADD_LOADER, b);
763 API int aul_remove_loader(int loader_id)
765 return aul_remove_loader_for_uid(loader_id, getuid());
768 API int aul_remove_loader_for_uid(int loader_id, uid_t uid)
770 char buf[MAX_PID_STR_BUFSZ];
783 snprintf(buf, sizeof(buf), "%d", loader_id);
784 bundle_add_str(b, AUL_K_LOADER_ID, buf);
785 snprintf(buf, sizeof(buf), "%d", uid);
786 bundle_add_str(b, AUL_K_TARGET_UID, buf);
788 ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_REMOVE_LOADER, b);
794 API int aul_app_register_pid(const char *appid, int pid)
796 char pid_str[MAX_PID_STR_BUFSZ];
800 if (!appid || pid <= 0)
809 bundle_add_str(b, AUL_K_APPID, appid);
810 snprintf(pid_str, sizeof(pid_str), "%d", pid);
811 bundle_add_str(b, AUL_K_PID, pid_str);
813 ret = app_send_cmd_with_noreply(AUL_UTIL_PID, APP_REGISTER_PID, b);
819 API int aul_launch_app_async(const char *appid, bundle *kb)
821 return aul_launch_app_async_for_uid(appid, kb, getuid());
824 API int aul_launch_app_async_for_uid(const char *appid, bundle *kb, uid_t uid)
831 ret = app_request_to_launchpad_for_uid(APP_START_ASYNC, appid, kb, uid);
835 API int aul_prepare_candidate_process(void)
837 unsigned char dummy[1] = { 0 };
839 return aul_sock_send_raw(AUL_UTIL_PID, getuid(),
840 APP_PREPARE_CANDIDATE_PROCESS, dummy, 0, AUL_SOCK_NONE);
843 API int aul_terminate_pid_sync(int pid)
845 return aul_terminate_pid_sync_for_uid(pid, getuid());
848 API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid)
850 char pid_str[MAX_PID_STR_BUFSZ];
856 snprintf(pid_str, sizeof(pid_str), "%d", pid);
857 ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC, pid_str,
862 API int aul_resume_pid_async(int pid)
864 return aul_resume_pid_async_for_uid(pid, getuid());
867 API int aul_resume_pid_async_for_uid(int pid, uid_t uid)
869 char pid_str[MAX_PID_STR_BUFSZ];
875 snprintf(pid_str, sizeof(pid_str), "%d", pid);
876 ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID_ASYNC,
881 API int aul_resume_app_by_instance_id(const char *appid,
882 const char *instance_id)
884 return aul_resume_app_by_instance_id_for_uid(appid,
885 instance_id, getuid());
888 API int aul_resume_app_by_instance_id_for_uid(const char *appid,
889 const char *instance_id, uid_t uid)
894 if (appid == NULL || instance_id == NULL) {
895 _E("Invalid parameter");
905 ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
906 if (ret != BUNDLE_ERROR_NONE) {
907 _E("Failed to add instance id(%s)", instance_id);
912 ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, b, uid);