2 * Copyright (c) 2000 - 2021 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.
17 #include <bundle_internal.h>
22 #include <glib-unix.h>
29 #include <sys/types.h>
35 #include "app_request.h"
36 #include "app_signal.h"
39 #include "aul_app_com.h"
40 #include "aul_error.h"
45 using namespace aul::internal;
49 constexpr const int TEP_ISMOUNT_MAX_RETRY_CNT = 20;
53 ResultInfo(int fd, aul_result_cb callback, void* user_data)
54 : fd_(fd), callback_(callback), user_data_(user_data) {}
56 ResultInfo(const ResultInfo&) = delete;
57 ResultInfo& operator = (const ResultInfo&) = delete;
61 g_source_remove(source_);
68 source_ = g_unix_fd_add(fd_,
69 static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR),
72 _E("g_unix_fd_add() is failed");
80 void ProcessReadEvent() {
81 int res = aul_sock_recv_result_with_fd(fd_);
83 res = aul_error_convert(res);
87 callback_(res, user_data_);
90 void ProcessErrorEvent() {
91 callback_(AUL_R_ERROR, user_data_);
94 static gboolean FdSourceFunc(int fd, GIOCondition condition,
96 auto* info = static_cast<ResultInfo*>(user_data);
97 if (condition & G_IO_IN)
98 info->ProcessReadEvent();
100 info->ProcessErrorEvent();
104 return G_SOURCE_REMOVE;
109 aul_result_cb callback_;
114 int aul_initialized = 0;
116 void* window_object = nullptr;
117 void* bg_object = nullptr;
118 void* conformant_object = nullptr;
120 int SendCmdForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
121 int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
123 res = aul_error_convert(res);
128 int SendCmdNoReplyForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
129 int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_NOREPLY);
131 res = aul_error_convert(res);
137 const char* env_str = getenv("AUL_PID");
138 if (env_str && isdigit(*env_str)) {
139 int env_pid = atoi(env_str);
140 if (env_pid != getpid()) {
141 _W("pid(%d) is not equal to AUL_PID(%d)",
153 if (CheckEnvPid() != 0)
156 const char* listen_fd = getenv("AUL_LISTEN_FD");
158 if (isdigit(*listen_fd))
159 fd = atoi(listen_fd);
160 setenv("AUL_LISTEN_FD", "-1", 1);
168 extern "C" int aul_is_initialized() {
169 return aul_initialized;
172 extern "C" API int app_send_cmd(int pid, int cmd, bundle* kb) {
173 return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
176 extern "C" API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd,
178 return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_NONE);
181 extern "C" API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd,
183 return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
186 extern "C" API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
187 int cmd, bundle* kb) {
188 return SendCmdNoReplyForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
191 extern "C" API int app_send_cmd_with_noreply(int pid, int cmd, bundle* kb) {
192 return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
195 extern "C" API int app_send_cmd_to_launchpad(const char* pad_type, uid_t uid,
196 int cmd, bundle* kb) {
197 int fd = aul_sock_create_launchpad_client(pad_type, uid);
201 int res = aul_sock_send_bundle_with_fd(fd, cmd, kb, AUL_SOCK_ASYNC);
208 int len = recv(fd, &res, sizeof(int), 0);
210 if (errno == EAGAIN) {
212 _E("recv timeout: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
214 } else if (errno == EINTR) {
216 _D("recv: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
220 _E("recv error: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
229 extern "C" int app_request_local(int cmd, bundle* kb) {
230 _E("app_request_to_launchpad : Same Process Send Local");
234 case APP_START_ASYNC:
236 case APP_START_RES_ASYNC:
237 case APP_SEND_LAUNCH_REQUEST:
238 return aul_launch_local(bundle_dup(kb));
241 case APP_RESUME_BY_PID:
242 case APP_RESUME_BY_PID_ASYNC:
243 case APP_SEND_RESUME_REQUEST:
244 return aul_resume_local();
246 _E("no support packet");
251 extern "C" int app_request_to_launchpad_for_uid(int cmd, const char* appid,
252 bundle* kb, uid_t uid) {
253 return AppRequest(cmd, uid)
259 extern "C" int aul_initialize() {
261 return AUL_R_ECANCELED;
263 aul_fd = GetPreInitFd();
264 if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
265 int flag = fcntl(aul_fd, F_GETFD);
267 (void)fcntl(aul_fd, F_SETFD, flag);
269 _W("Failed to get preinit fd");
270 aul_fd = aul_sock_create_server(getpid(), getuid());
272 _E("aul_init create sock failed");
282 extern "C" API void aul_finalize() {
285 if (aul_initialized) {
286 aul_sock_destroy_server(aul_fd);
291 extern "C" API int aul_request_data_control_socket_pair(bundle* kb, int* fd) {
295 int clifd = AppRequest(APP_GET_DC_SOCKET_PAIR)
297 .SendSimply(AUL_SOCK_ASYNC);
301 int ret = aul_sock_recv_result_with_fd(clifd);
304 if (ret == -EILLEGALACCESS) {
305 _E("Illegal access in datacontrol socket pair request");
306 return AUL_R_EILLACC;
312 ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
319 extern "C" API int aul_request_message_port_socket_pair(int* fd) {
323 int ret = AppRequest(APP_GET_MP_SOCKET_PAIR)
324 .SendCmdOnly(AUL_SOCK_ASYNC);
327 ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
337 extern "C" API int aul_launch_app(const char* appid, bundle* kb) {
338 return aul_launch_app_for_uid(appid, kb, getuid());
341 extern "C" API int aul_launch_app_for_uid(const char* appid, bundle* kb,
343 if (appid == nullptr)
346 return AppRequest(APP_START, uid)
352 extern "C" API int aul_open_app(const char* appid) {
353 return aul_open_app_for_uid(appid, getuid());
356 extern "C" API int aul_open_app_for_uid(const char* appid, uid_t uid) {
357 if (appid == nullptr)
360 return AppRequest(APP_OPEN, uid)
365 extern "C" API int aul_resume_app(const char* appid) {
366 return aul_resume_app_for_uid(appid, getuid());
369 extern "C" API int aul_resume_app_for_uid(const char* appid, uid_t uid) {
370 if (appid == nullptr)
373 return AppRequest(APP_RESUME, uid)
378 extern "C" API int aul_resume_pid(int pid) {
379 return aul_resume_pid_for_uid(pid, getuid());
382 extern "C" API int aul_resume_pid_for_uid(int pid, uid_t uid) {
386 return AppRequest(APP_RESUME_BY_PID, uid)
391 extern "C" API int aul_terminate_pid(int pid) {
392 return aul_terminate_pid_for_uid(pid, getuid());
395 extern "C" API int aul_terminate_pid_for_uid(int pid, uid_t uid) {
399 int ret = AppRequest(APP_TERM_BY_PID, uid)
408 extern "C" API int aul_terminate_bgapp_pid(int pid) {
412 int ret = AppRequest(APP_TERM_BGAPP_BY_PID)
421 extern "C" API int aul_terminate_pid_without_restart(int pid) {
425 return AppRequest(APP_TERM_BY_PID_WITHOUT_RESTART)
430 extern "C" API int aul_terminate_pid_sync_without_restart(int pid) {
431 return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
434 extern "C" API int aul_terminate_pid_sync_without_restart_for_uid(int pid,
439 return AppRequest(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART, uid)
444 extern "C" API int aul_terminate_pid_async(int pid) {
445 return aul_terminate_pid_async_for_uid(pid, getuid());
448 extern "C" API int aul_terminate_pid_async_for_uid(int pid, uid_t uid) {
452 return AppRequest(APP_TERM_BY_PID_ASYNC, uid)
457 extern "C" API int aul_kill_pid(int pid) {
461 return AppRequest(APP_KILL_BY_PID)
466 extern "C" API void aul_set_preinit_window(void* evas_object) {
467 window_object = evas_object;
470 extern "C" API void* aul_get_preinit_window(const char* win_name) {
471 return window_object;
474 extern "C" API void aul_set_preinit_background(void* evas_object) {
475 bg_object = evas_object;
478 extern "C" API void* aul_get_preinit_background(void) {
482 extern "C" API void aul_set_preinit_conformant(void* evas_object) {
483 conformant_object = evas_object;
486 extern "C" API void* aul_get_preinit_conformant(void) {
487 return conformant_object;
490 extern "C" API int aul_pause_app(const char* appid) {
491 return aul_pause_app_for_uid(appid, getuid());
494 extern "C" API int aul_pause_app_for_uid(const char* appid, uid_t uid) {
495 if (appid == nullptr)
498 return AppRequest(APP_PAUSE, uid)
500 .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
503 extern "C" API int aul_pause_pid(int pid) {
504 return aul_pause_pid_for_uid(pid, getuid());
507 extern "C" API int aul_pause_pid_for_uid(int pid, uid_t uid) {
511 return AppRequest(APP_PAUSE_BY_PID, uid)
513 .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
516 extern "C" API int aul_reload_appinfo(void) {
517 return AppRequest(AMD_RELOAD_APPINFO)
518 .SetAppIdAsPid(getpid())
522 extern "C" API int aul_is_tep_mount_dbus_done(const char* tep_string) {
523 GError* err = nullptr;
524 GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err);
525 if (conn == nullptr) {
526 _E("g_bus_get_sync() is failed. error(%s)", err ? err->message : "Unknown");
531 std::unique_ptr<GDBusConnection, decltype(g_object_unref)*> conn_ptr(
532 conn, g_object_unref);
534 GDBusMessage* msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
537 TEP_IS_MOUNTED_METHOD);
538 if (msg == nullptr) {
539 _E("g_dbus_message_new_method_call() is failed. error(%s)",
540 err ? err->message : "Unknown");
544 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_ptr(
545 msg, g_object_unref);
546 g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
548 GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(conn,
550 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
555 if (reply == nullptr || err != nullptr) {
556 _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
557 err ? err->message : "Unknown");
562 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_ptr(
563 reply, g_object_unref);
565 GVariant* body = g_dbus_message_get_body(reply);
566 if (body == nullptr) {
567 _E("g_dbus_message_get_body() is failed.");
571 int ret = AUL_R_ERROR;
572 g_variant_get(body, "(i)", &ret);
576 extern "C" API int aul_check_tep_mount(const char* tep_path) {
580 while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
581 ret = aul_is_tep_mount_dbus_done(tep_path);
587 /* incase after trying 1 sec, not getting mounted then quit */
589 _E("Not able to mount within 1 sec");
596 extern "C" API int aul_add_loader(const char* loader_path, bundle* kb) {
597 return aul_add_loader_for_uid(loader_path, kb, getuid());
600 extern "C" API int aul_add_loader_for_uid(const char* loader_path, bundle* kb,
602 if (loader_path == nullptr)
605 tizen_base::Bundle b = { {AUL_K_LOADER_PATH, loader_path} };
609 tizen_base::Bundle extra(kb, false, false);
610 auto raw = extra.ToRaw();
611 b.Add(AUL_K_LOADER_EXTRA, (const char*)(raw.first.get()));
612 } catch (const std::bad_alloc&) {
617 return AppRequest(APP_ADD_LOADER, uid)
622 extern "C" API int aul_remove_loader(int loader_id) {
623 return aul_remove_loader_for_uid(loader_id, getuid());
626 extern "C" API int aul_remove_loader_for_uid(int loader_id, uid_t uid) {
630 return AppRequest(APP_REMOVE_LOADER, uid)
631 .With({{AUL_K_LOADER_ID, std::to_string(loader_id)}})
635 extern "C" API int aul_app_register_pid(const char* appid, int pid) {
636 if (appid == nullptr || pid <= 0)
639 return AppRequest(APP_REGISTER_PID)
642 .SendSimply(AUL_SOCK_NOREPLY);
645 extern "C" API int aul_launch_app_async(const char* appid, bundle* kb) {
646 return aul_launch_app_async_for_uid(appid, kb, getuid());
649 extern "C" API int aul_launch_app_async_for_uid(const char* appid, bundle* kb,
651 if (appid == nullptr)
654 return AppRequest(APP_START_ASYNC, uid)
660 extern "C" API int aul_prepare_candidate_process(void) {
661 return AppRequest(APP_PREPARE_CANDIDATE_PROCESS)
665 extern "C" API int aul_terminate_pid_sync(int pid) {
666 return aul_terminate_pid_sync_for_uid(pid, getuid());
669 extern "C" API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid) {
673 return AppRequest(APP_TERM_BY_PID_SYNC, uid)
678 extern "C" API int aul_resume_pid_async(int pid) {
679 return aul_resume_pid_async_for_uid(pid, getuid());
682 extern "C" API int aul_resume_pid_async_for_uid(int pid, uid_t uid) {
686 return AppRequest(APP_RESUME_BY_PID_ASYNC, uid)
691 extern "C" API int aul_resume_app_by_instance_id(const char* appid,
692 const char *instance_id) {
693 return aul_resume_app_by_instance_id_for_uid(appid, instance_id, getuid());
696 extern "C" API int aul_resume_app_by_instance_id_for_uid(const char* appid,
697 const char* instance_id, uid_t uid) {
698 if (appid == nullptr || instance_id == nullptr) {
699 _E("Invalid parameter");
703 return AppRequest(APP_RESUME, uid)
705 .SetInstId(instance_id)
709 extern "C" API int aul_terminate_instance_async(const char* instance_id,
711 return aul_terminate_instance_async_for_uid(instance_id, pid, getuid());
714 extern "C" API int aul_terminate_instance_async_for_uid(const char* instance_id,
715 int pid, uid_t uid) {
716 if (instance_id == nullptr) {
717 _E("Invalid parameter");
721 return AppRequest(APP_TERM_INSTANCE_ASYNC, uid)
723 .SetInstId(instance_id)
727 extern "C" API int aul_terminate_app_with_instance_id(const char* appid,
728 const char* instance_id) {
729 return aul_terminate_app_with_instance_id_for_uid(appid, instance_id,
733 extern "C" API int aul_terminate_app_with_instance_id_for_uid(const char* appid,
734 const char* instance_id, uid_t uid) {
735 if (appid == nullptr || instance_id == nullptr) {
736 _E("Invalid parameter");
740 return AppRequest(APP_TERMINATE, uid)
742 .SetInstId(instance_id)
746 extern "C" API int aul_terminate_app(const char* appid) {
747 return aul_terminate_app_for_uid(appid, getuid());
750 extern "C" API int aul_terminate_app_for_uid(const char* appid, uid_t uid) {
751 if (appid == nullptr) {
752 _E("Invalid parameter");
756 return AppRequest(APP_TERMINATE, uid)
761 extern "C" API int aul_prepare_app_defined_loader(const char* loader_name) {
762 return aul_prepare_app_defined_loader_for_uid(loader_name, getuid());
765 extern "C" API int aul_prepare_app_defined_loader_for_uid(
766 const char* loader_name, uid_t uid) {
767 if (loader_name == nullptr) {
768 _E("Invalid parameter");
772 int ret = AppRequest(APP_PREPARE_APP_DEFINED_LOADER, uid)
773 .With({{AUL_K_LOADER_NAME, loader_name}})
776 _E("Failed to prepare app-defined loader. error(%d)", ret);
780 _I("loader id(%d)", ret);
784 extern "C" API int aul_terminate_pid_async_v2(pid_t pid,
785 aul_result_cb callback, void *user_data) {
786 if (pid < 1 || callback == nullptr) {
787 _E("Invalid parameter");
791 int fd = AppRequest(APP_TERM_BY_PID, getuid())
793 .SendSimply(AUL_SOCK_ASYNC);
795 _E("Failed to send request. error(%d)", fd);
800 auto* info = new ResultInfo(fd, callback, user_data);
802 } catch (const std::exception& e) {
803 _E("Exception occurs. error: %s", e.what());
811 extern "C" API int aul_terminate_app_async(const char* appid,
812 aul_result_cb callback, void *user_data) {
813 if (appid == nullptr || callback == nullptr) {
814 _E("Invalid parameter");
818 int fd = AppRequest(APP_TERMINATE, getuid())
820 .SendSimply(AUL_SOCK_ASYNC);
822 _E("Failed to send request. error(%d)", fd);
827 auto* info = new ResultInfo(fd, callback, user_data);
829 } catch (const std::exception& e) {
830 _E("Exception occurs. error: %s", e.what());
838 extern "C" API int aul_kill_pid_async(pid_t pid,
839 aul_result_cb callback, void *user_data) {
840 if (pid < 1 || callback == nullptr) {
841 _E("Invalid parameter");
845 int fd = AppRequest(APP_KILL_BY_PID, getuid())
847 .SendSimply(AUL_SOCK_ASYNC);
849 _E("Failed to send request. error(%d)", fd);
854 auto* info = new ResultInfo(fd, callback, user_data);
856 } catch (const std::exception& e) {
857 _E("Exception occurs. error: %s", e.what());