From: Hwankyu Jhun Date: Mon, 5 Jun 2023 04:30:36 +0000 (+0000) Subject: Refactor launchpad common library X-Git-Tag: accepted/tizen/unified/20230608.164330~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F293786%2F9;p=platform%2Fcore%2Fappfw%2Flaunchpad.git Refactor launchpad common library The common library is implemented using C++ language. Change-Id: I34759ccb4098af00bd484e77be6a63d67fb3c8dc Signed-off-by: Hwankyu Jhun --- diff --git a/src/launchpad-process-pool/app_executor.cc b/src/launchpad-process-pool/app_executor.cc index ff38056..cd58dde 100644 --- a/src/launchpad-process-pool/app_executor.cc +++ b/src/launchpad-process-pool/app_executor.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,8 @@ #include #include +#include +#include #include #include "launchpad-process-pool/config.hh" @@ -36,8 +39,6 @@ #include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/signal_manager.hh" #include "launchpad-process-pool/user_tracer.hh" -#include "lib/common/inc/launchpad_common.h" -#include "lib/common/inc/launchpad_types.h" namespace launchpad { namespace fs = std::filesystem; @@ -138,14 +139,12 @@ void AppExecutor::OnExecution() { SECURE_LOGD("input argument %d : %s##", i, app_argv[i]); } - auto* lib_dir = _get_libdir(app_info_->GetAppPath().c_str()); - if (lib_dir != nullptr) { - setenv("LD_LIBRARY_PATH", lib_dir, 1); - free(lib_dir); - } + auto lib_dir = Util::GetLibDirectory(app_info_); + if (!lib_dir.empty()) + setenv("LD_LIBRARY_PATH", lib_dir.c_str(), 1); - _close_all_fds(); - if (execv(app_argv[LOADER_ARG_PATH], app_argv) < 0) { + Util::CloseAllFds(); + if (execv(app_argv[LoaderArg::Path], app_argv) < 0) { char err_buf[1024]; fprintf(stderr, "Failed to exeucte a file. path: %s, errno: %d(%s)\n", app_info_->GetAppPath().c_str(), errno, @@ -177,9 +176,7 @@ int AppExecutor::StepPluginPrepareApp() { } int AppExecutor::StepEnableExternalPackage() { - return _enable_external_pkg(app_info_->GetBundle().GetHandle(), - app_info_->GetPkgId().c_str(), - app_info_->IsGlobal() ? GLOBAL_USER : getuid()); + return Util::EnableExternalPackage(app_info_); } int AppExecutor::StepEnableTrustAnchor() { @@ -195,8 +192,7 @@ int AppExecutor::StepEnableTrustAnchor() { } int AppExecutor::StepMountResDir() { - return _mount_res_dir(app_info_->GetRootPath().c_str(), - app_info_->GetBundle().GetHandle()); + return Util::MountResourceDirectories(app_info_); } int AppExecutor::StepChangeMountNamespace() { @@ -223,7 +219,7 @@ int AppExecutor::StepSecurityPrepareApp() { int AppExecutor::StepSetupStdio() { if (app_info_->GetBundle().GetType(kAulSdk) == BUNDLE_TYPE_NONE) - _setup_stdio(basename(const_cast(app_info_->GetAppPath().c_str()))); + Stdio::Setup(); return 0; } @@ -246,21 +242,21 @@ int AppExecutor::StepSetEnvironments() { } int AppExecutor::StepWaitTepMount() { - return _wait_tep_mount(app_info_->GetBundle().GetHandle()); + return Util::WaitTepMount(app_info_); } int AppExecutor::StepPrepareAppSocketAndIdFile() { if (app_info_->GetBundle().GetType(kAulSdk) != BUNDLE_TYPE_NONE) return 0; - if (_prepare_app_socket() < 0) + if (Util::PrepareAppSocket() < 0) return -1; - return _prepare_id_file(); + return Util::PrepareAppIdFile(app_info_); } int AppExecutor::StepSendStartupSignal() { - _send_cmd_to_amd(APP_STARTUP_SIGNAL); + Util::SendCmdToAmd(AmdCmd::AppStartupSignal); return 0; } @@ -308,7 +304,7 @@ std::vector AppExecutor::CreateAppArgv(const std::string& app_path, argv.insert(argv.end(), launcher_argv.begin(), launcher_argv.end()); auto exported_argv = b.Export(); - exported_argv[LOADER_ARG_PATH] = app_path; + exported_argv[LoaderArg::Path] = app_path; if (!exported_argv.empty()) argv.insert(argv.end(), exported_argv.begin(), exported_argv.end()); diff --git a/src/launchpad-process-pool/debug.cc b/src/launchpad-process-pool/debug.cc index 53f37cc..c9f43e2 100644 --- a/src/launchpad-process-pool/debug.cc +++ b/src/launchpad-process-pool/debug.cc @@ -25,10 +25,10 @@ #include #include +#include #include -#include "lib/common/inc/key.h" -#include "lib/common/inc/launchpad_common.h" +#include "launchpad-process-pool/log_private.hh" namespace fs = std::filesystem; @@ -81,7 +81,7 @@ bool Debug::Load() { } void Debug::PrepareDebugger(const tizen_base::Bundle& b) { - auto debugger = b.GetString(AUL_K_SDK); + auto debugger = b.GetString(kAulSdk); if (debugger.empty()) return; @@ -134,7 +134,7 @@ void Debug::ChangeMountNamespace() { } void Debug::CheckWebAppDebugging(const tizen_base::Bundle& b) { - if (b.GetType(AUL_K_DEBUG) != BUNDLE_TYPE_NONE) + if (b.GetType(kAulDebug) != BUNDLE_TYPE_NONE) setenv("TIZEN_DEBUGGING_PORT", "1", 1); } @@ -283,9 +283,9 @@ void Debug::ParseAndRedirectStandardFds(const tizen_base::Bundle& b) { } pid_t Debug::GetCallerPid(const tizen_base::Bundle& b) { - auto pid_str = b.GetString(AUL_K_ORG_CALLER_PID); + auto pid_str = b.GetString(kAulOrgCallerPid); if (pid_str.empty()) - pid_str = b.GetString(AUL_K_CALLER_PID); + pid_str = b.GetString(kAulCallerPid); if (pid_str.empty()) return -1; diff --git a/src/launchpad-process-pool/debugger_info.cc b/src/launchpad-process-pool/debugger_info.cc index 42cb0bd..c9618b1 100644 --- a/src/launchpad-process-pool/debugger_info.cc +++ b/src/launchpad-process-pool/debugger_info.cc @@ -29,8 +29,8 @@ #include #include +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/util.hh" -#include "lib/common/inc/launchpad_common.h" namespace launchpad { namespace { diff --git a/src/launchpad-process-pool/hydra_loader_context.cc b/src/launchpad-process-pool/hydra_loader_context.cc index 545e107..cbcb6a8 100644 --- a/src/launchpad-process-pool/hydra_loader_context.cc +++ b/src/launchpad-process-pool/hydra_loader_context.cc @@ -23,9 +23,9 @@ #include #include #include +#include #include "launchpad-process-pool/log_private.hh" -#include "lib/common/inc/launchpad_types.h" namespace fs = std::filesystem; @@ -110,7 +110,7 @@ void HydraLoaderContext::SetHydraPid(pid_t hydra_pid) { void HydraLoaderContext::PrepareCandidateProcess() { _W("Send launch request to hydra loader. fd(%d)", client_socket_->GetFd()); - HydraRequest request(LAUNCH_CANDIDATE, GetSchedPriority()); + HydraRequest request(HydraCmd::LaunchCandidate, GetSchedPriority()); tizen_base::Parcel parcel; parcel.WriteParcelable(request); diff --git a/src/launchpad-process-pool/launcher_info.cc b/src/launchpad-process-pool/launcher_info.cc index 95646ad..a94d382 100644 --- a/src/launchpad-process-pool/launcher_info.cc +++ b/src/launchpad-process-pool/launcher_info.cc @@ -23,8 +23,8 @@ #include +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/util.hh" -#include "lib/common/inc/launchpad_common.h" namespace launchpad { namespace { diff --git a/src/launchpad-process-pool/launchpad.cc b/src/launchpad-process-pool/launchpad.cc index 4e25d5b..db621b0 100644 --- a/src/launchpad-process-pool/launchpad.cc +++ b/src/launchpad-process-pool/launchpad.cc @@ -33,12 +33,9 @@ #include #include #include +#include #include -#include "lib/common/inc/launchpad_common.h" -#include "lib/common/inc/launchpad_plugin.h" -#include "lib/common/inc/launchpad_types.h" - #include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/dbus.hh" #include "launchpad-process-pool/debug.hh" @@ -47,6 +44,7 @@ #include "launchpad-process-pool/loader_manager.hh" #include "launchpad-process-pool/loader_executor.hh" #include "launchpad-process-pool/log.hh" +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/memory_monitor.hh" #include "launchpad-process-pool/signal_manager.hh" #include "launchpad-process-pool/tracer.hh" @@ -176,34 +174,34 @@ Launchpad::Launchpad(int argc, char** argv) loop_(g_main_loop_new(nullptr, FALSE)) { LaunchpadArgs::GetInst().Set(argc_, argv_); handlers_ = { - { PAD_CMD_VISIBILITY, + { PadCmd::Visibility, std::bind(&Launchpad::HandleVisibilityRequest, this, std::placeholders::_1) }, - { PAD_CMD_ADD_LOADER, + { PadCmd::AddLoader, std::bind(&Launchpad::HandleAddLoaderRequest, this, std::placeholders::_1) }, - { PAD_CMD_REMOVE_LOADER, + { PadCmd::RemoveLoader, std::bind(&Launchpad::HandleRemoveLoaderRequest, this, std::placeholders::_1) }, - { PAD_CMD_MAKE_DEFAULT_SLOTS, + { PadCmd::MakeDefaultSlots, std::bind(&Launchpad::HandleMakeDefaultSlotsRequest, this, std::placeholders::_1) }, - { PAD_CMD_PREPARE_APP_DEFINED_LOADER, + { PadCmd::PrepareAppDefinedLoader, std::bind(&Launchpad::HandlePrepareAppDefinedLoaderRequest, this, std::placeholders::_1) }, - { PAD_CMD_DEMAND, + { PadCmd::Demand, std::bind(&Launchpad::HandleDemandRequest, this, std::placeholders::_1) }, - { PAD_CMD_PING, + { PadCmd::Ping, std::bind(&Launchpad::HandlePingRequest, this, std::placeholders::_1) }, - { PAD_CMD_UPDATE_APP_TYPE, + { PadCmd::UpdateAppType, std::bind(&Launchpad::HandleUpdateAppTypeRequest, this, std::placeholders::_1) }, - { PAD_CMD_CONNECT, + { PadCmd::Connect, std::bind(&Launchpad::HandleConnectRequest, this, std::placeholders::_1) }, - { PAD_CMD_LAUNCH, + { PadCmd::Launch, std::bind(&Launchpad::HandleLaunchRequest, this, std::placeholders::_1) }, }; @@ -262,7 +260,7 @@ bool Launchpad::OnCreate() { LoaderManager::GetInst().SetEventListener(this); launchpad::Debug::GetInst().Init(); - _send_cmd_to_amd(LAUNCHPAD_LAUNCH_SIGNAL); + Util::SendCmdToAmd(AmdCmd::LaunchpadLaunchSignal); lang_config_.reset(new LanguageConfig()); region_format_config_.reset(new RegionFormatConfig()); @@ -278,7 +276,7 @@ void Launchpad::OnTerminate() { lang_config_.reset(); pid_map_.clear(); - _send_cmd_to_amd(LAUNCHPAD_DEAD_SIGNAL); + Util::SendCmdToAmd(AmdCmd::LaunchpadDeadSignal); Debug::GetInst().Dispose(); LoaderManager::GetInst().Dispose(); @@ -400,19 +398,19 @@ Launchpad::LaunchResult Launchpad::LaunchRequestPrepare( auto& comp_type = app_info->GetCompType(); if (comp_type == "svcapp") { request->SetLoaderId(GetLoaderIdFromBundle(request->GetBundle())); - if (request->GetLoaderId() > PAD_LOADER_ID_DYNAMIC_BASE) { + if (request->GetLoaderId() > PadLoaderId::DynamicBase) { auto context = loader_manager.FindLoaderContext(LoaderType::Dynamic, request->GetLoaderId()); if (context != nullptr && CanUseLoaderContext(context)) request->SetAvailableLoaderContext(std::move(context)); } else { - request->SetLoaderId(PAD_LOADER_ID_DIRECT); + request->SetLoaderId(PadLoaderId::Direct); } } else if (comp_type == "widget" && app_info->GetAppType() == "webapp") { - request->SetLoaderId(PAD_LOADER_ID_DIRECT); + request->SetLoaderId(PadLoaderId::Direct); } else { request->SetLoaderId(GetLoaderIdFromBundle(request->GetBundle())); - if (request->GetLoaderId() <= PAD_LOADER_ID_STATIC) { + if (request->GetLoaderId() <= PadLoaderId::Static) { auto context = loader_manager.FindAvailableLoaderContext( app_info->GetHwacc(), app_info->GetAppType(), app_info->GetLoaderName()); @@ -466,7 +464,7 @@ Launchpad::LaunchResult Launchpad::ForkProcessing( Launchpad::LaunchResult Launchpad::LaunchRequestDo( std::shared_ptr request) { auto loader_context = request->GetAvailableLoaderContext(); - if (request->GetLoaderId() == PAD_LOADER_ID_DIRECT || + if (request->GetLoaderId() == PadLoaderId::Direct || loader_context == nullptr || launchpad::Debug::GetInst().CheckAsanApp( request->GetAppInfo()->GetAppId())) { @@ -491,7 +489,7 @@ Launchpad::LaunchResult Launchpad::LaunchRequestDo( _W("Launch %d type process. appid: %s", static_cast(loader_context->GetType()), app_info->GetAppId().c_str()); - request->SetPid(loader_context->Deploy(app_info, request->GetAppPacket())); + request->SetPid(loader_context->Deploy(app_info)); return LaunchResult::Success; } @@ -525,42 +523,35 @@ void Launchpad::HandleLaunchRequest(std::shared_ptr request) { } void Launchpad::OnIOEventReceived(int fd, int condition) { - struct ucred cr; - int client_fd = -1; - app_pkt_t* pkt = _accept_recv_pkt_raw(fd, &client_fd, &cr); - if (pkt == nullptr) - return; - - auto pkt_auto = std::unique_ptr( - pkt, std::free); - auto* kb = bundle_decode(pkt->data, pkt->len); - if (kb == nullptr) { - close(client_fd); + auto client_socket = socket_->Accept(); + if (!client_socket) { + _E("Failed to accept the client request"); return; } - tizen_base::Bundle b(kb, false, true); - auto request = std::make_shared( - std::make_unique(client_fd), pkt_auto.release(), - std::move(b)); - _W("cmd(%d), caller(%d)", request->GetCmd(), request->GetCallerPid()); - if (request->GetCallerUid() >= kRegularUidMin) { - if (CheckCallerPermission(request->GetCallerPid()) < 0) { - _E("Permission denied. pid(%d)", request->GetCallerPid()); - request->SendResult(-EPERM); + try { + auto request = std::make_shared(std::move(client_socket)); + _W("cmd(%d), caller(%d)", request->GetCmd(), request->GetCallerPid()); + if (request->GetCallerUid() >= kRegularUidMin) { + if (CheckCallerPermission(request->GetCallerPid()) < 0) { + _E("Permission denied. pid(%d)", request->GetCallerPid()); + request->SendResult(-EPERM); + return; + } + } + + auto found = handlers_.find(request->GetCmd()); + if (found == handlers_.end()) { + _E("Unknown command: %d", request->GetCmd()); + request->SendResult(-EINVAL); return; } - } - auto found = handlers_.find(request->GetCmd()); - if (found == handlers_.end()) { - _E("Unknown command: %d", request->GetCmd()); - request->SendResult(-EINVAL); - return; + auto method_handler = found->second; + method_handler(std::move(request)); + } catch (const Exception& e) { + _E("Exception occurs. error(%s)", e.what()); } - - auto method_handler = found->second; - method_handler(std::move(request)); } void Launchpad::OnSigchldReceived(pid_t pid) { diff --git a/src/launchpad-process-pool/loader_context.cc b/src/launchpad-process-pool/loader_context.cc index 8ced6a7..9da3176 100644 --- a/src/launchpad-process-pool/loader_context.cc +++ b/src/launchpad-process-pool/loader_context.cc @@ -30,16 +30,15 @@ #include #include #include +#include #include "launchpad-process-pool/app_labels_monitor.hh" #include "launchpad-process-pool/loader_executor.hh" #include "launchpad-process-pool/log.hh" +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/memory_monitor.hh" #include "launchpad-process-pool/user_tracer.hh" #include "launchpad-process-pool/util.hh" -#include "lib/common/inc/key.h" -#include "lib/common/inc/launchpad_common.h" -#include "lib/common/inc/launchpad_types.h" namespace fs = std::filesystem; @@ -229,7 +228,7 @@ pid_t LoaderContext::Prepare() { return pid_; } -pid_t LoaderContext::Deploy(const AppInfo* app_info, app_pkt_t* app_packet) { +pid_t LoaderContext::Deploy(const AppInfo* app_info) { Util::DeleteSocketPath(pid_, getuid()); tizen_base::Parcel parcel; parcel.WriteParcelable(*app_info); diff --git a/src/launchpad-process-pool/loader_context.hh b/src/launchpad-process-pool/loader_context.hh index 1dfbc26..882a6ef 100644 --- a/src/launchpad-process-pool/loader_context.hh +++ b/src/launchpad-process-pool/loader_context.hh @@ -29,8 +29,6 @@ #include #include -#include "lib/common/inc/launchpad_common.h" - #include "launchpad-process-pool/cpu_checker.hh" #include "launchpad-process-pool/loader_info.hh" #include "launchpad-process-pool/launcher_info.hh" @@ -73,7 +71,7 @@ class LoaderContext : public std::enable_shared_from_this, virtual void Listen(); virtual void Dispose(); virtual pid_t Prepare(); - virtual pid_t Deploy(const AppInfo* app_info, app_pkt_t* request); + virtual pid_t Deploy(const AppInfo* app_info); const std::string& GetLoaderName() const; const std::string& GetLoaderPath() const; diff --git a/src/launchpad-process-pool/loader_executor.cc b/src/launchpad-process-pool/loader_executor.cc index 43f1d4c..be307ec 100644 --- a/src/launchpad-process-pool/loader_executor.cc +++ b/src/launchpad-process-pool/loader_executor.cc @@ -24,12 +24,13 @@ #include #include +#include +#include +#include #include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/signal_manager.hh" -#include "lib/common/inc/launchpad_common.h" -#include "lib/common/inc/launchpad_types.h" namespace launchpad { namespace { @@ -94,13 +95,13 @@ void LoaderExecutor::OnExecution() { } SignalManager::GetInst().UnblockSigchld(); - _close_all_fds(); - _setup_stdio(basename(loader_argv[LOADER_ARG_PATH])); + Util::CloseAllFds(); + Stdio::Setup(); - if (execv(loader_argv[LOADER_ARG_PATH], loader_argv.data()) < 0) { + if (execv(loader_argv[LoaderArg::Path], loader_argv.data()) < 0) { char err_buf[1024]; fprintf(stderr, "Failed to execute a file. path: %s, errno: %d:%s\n", - loader_argv[LOADER_ARG_PATH], errno, + loader_argv[LoaderArg::Path], errno, strerror_r(errno, err_buf, sizeof(err_buf))); exit(EXIT_FAILURE); } @@ -119,14 +120,14 @@ void LoaderExecutor::OnRequestReceived(tizen_base::Parcel* parcel) { std::vector LoaderExecutor::CreateLoaderArgv( const LoaderContext* loader_context) { - std::string dummy(LOADER_ARG_LEN - 1, ' '); - std::vector argv(LOADER_ARG_DUMMY + 1); - argv[LOADER_ARG_DUMMY] = std::move(dummy); - argv[LOADER_ARG_PATH] = loader_context->GetLoaderPath(); - argv[LOADER_ARG_TYPE] = std::to_string(loader_context->GetType()); - argv[LOADER_ARG_ID] = std::to_string(loader_context->GetLoaderId()); - argv[LOADER_ARG_HYDRA] = loader_context->IsHydraMode() ? "1" : "0"; - argv[LOADER_ARG_EXTRA] = loader_context->GetLoaderExtra(); + std::string dummy(kLoaderArgLength - 1, ' '); + std::vector argv(LoaderArg::Dummy + 1); + argv[LoaderArg::Dummy] = std::move(dummy); + argv[LoaderArg::Path] = loader_context->GetLoaderPath(); + argv[LoaderArg::Type] = std::to_string(loader_context->GetType()); + argv[LoaderArg::Id] = std::to_string(loader_context->GetLoaderId()); + argv[LoaderArg::Hydra] = loader_context->IsHydraMode() ? "1" : "0"; + argv[LoaderArg::Extra] = loader_context->GetLoaderExtra(); return argv; } diff --git a/src/launchpad-process-pool/loader_factory.cc b/src/launchpad-process-pool/loader_factory.cc index 8a86c28..c52524f 100644 --- a/src/launchpad-process-pool/loader_factory.cc +++ b/src/launchpad-process-pool/loader_factory.cc @@ -20,11 +20,11 @@ #include #include +#include -#include "lib/common/inc/launchpad_common.h" - -#include "launchpad-process-pool/loader_context.hh" #include "launchpad-process-pool/hydra_loader_context.hh" +#include "launchpad-process-pool/loader_context.hh" +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/user_tracer.hh" namespace launchpad { @@ -77,8 +77,8 @@ LoaderFactory& LoaderFactory::GetInst() { std::shared_ptr LoaderFactory::CreateLoaderContext( LoaderInfoPtr info) { auto app_types = GetAppTypeString(info); - int loader_id = (info->GetExe() == "null") ? PAD_LOADER_ID_DIRECT : - PAD_LOADER_ID_STATIC; + int loader_id = static_cast((info->GetExe() == "null") ? + PadLoaderId::Direct : PadLoaderId::Static); LoaderContext* context; try { if (info->IsHydraMode()) { diff --git a/src/launchpad-process-pool/loader_factory.hh b/src/launchpad-process-pool/loader_factory.hh index bfd8856..f44da2c 100644 --- a/src/launchpad-process-pool/loader_factory.hh +++ b/src/launchpad-process-pool/loader_factory.hh @@ -22,7 +22,7 @@ #include #include -#include "lib/common/inc/launchpad_types.h" +#include #include "launchpad-process-pool/loader_context.hh" #include "launchpad-process-pool/loader_info.hh" @@ -47,7 +47,7 @@ class LoaderFactory { int MakeDynamicLoaderId(); private: - int dynamic_loader_id_ = PAD_LOADER_ID_DYNAMIC_BASE; + int dynamic_loader_id_ = PadLoaderId::DynamicBase; }; } // namespace launchpad diff --git a/src/launchpad-process-pool/loader_info.cc b/src/launchpad-process-pool/loader_info.cc index b7e32f2..df00c5c 100644 --- a/src/launchpad-process-pool/loader_info.cc +++ b/src/launchpad-process-pool/loader_info.cc @@ -27,8 +27,8 @@ #include #include +#include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/util.hh" -#include "lib/common/inc/launchpad_common.h" namespace fs = std::filesystem; diff --git a/src/launchpad-process-pool/loader_info.hh b/src/launchpad-process-pool/loader_info.hh index 4f4e982..2ad1bc0 100644 --- a/src/launchpad-process-pool/loader_info.hh +++ b/src/launchpad-process-pool/loader_info.hh @@ -27,16 +27,12 @@ #include #include +#include + #include "launchpad-process-pool/util.hh" namespace launchpad { -enum class LoaderType : int { - None = 0, - User = 1, - Dynamic = 100, -}; - enum class LoaderMethod : int { None = 0x0000, Timeout = 0x0001, diff --git a/src/launchpad-process-pool/loader_manager.cc b/src/launchpad-process-pool/loader_manager.cc index ee3b86a..7c6ed30 100644 --- a/src/launchpad-process-pool/loader_manager.cc +++ b/src/launchpad-process-pool/loader_manager.cc @@ -19,6 +19,8 @@ #include #include +#include + #include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/loader_executor.hh" #include "launchpad-process-pool/loader_factory.hh" @@ -300,14 +302,14 @@ std::shared_ptr LoaderManager::FindAvailableLoaderContext( type = loader_info_manager_->FindSwType(app_type); } - return FindLoaderContext(type, PAD_LOADER_ID_STATIC); + return FindLoaderContext(type, PadLoaderId::Static); } std::shared_ptr LoaderManager::FindAlternativeLoaderContext( LoaderType type) { auto alternative_types = loader_info_manager_->GetAlternativeTypes(type); for (auto& alternative_type : alternative_types) { - auto context = FindLoaderContext(alternative_type, PAD_LOADER_ID_STATIC); + auto context = FindLoaderContext(alternative_type, PadLoaderId::Static); if (context == nullptr) continue; diff --git a/src/launchpad-process-pool/request.cc b/src/launchpad-process-pool/request.cc index 46d80f6..4dc6049 100644 --- a/src/launchpad-process-pool/request.cc +++ b/src/launchpad-process-pool/request.cc @@ -16,26 +16,45 @@ #include "launchpad-process-pool/request.hh" +#include #include +#include + +#include + +#include "launchpad-process-pool/log_private.hh" namespace launchpad { -Request::Request(std::unique_ptr client_socket, app_pkt_t* pkt, - tizen_base::Bundle b) +Request::Request(std::unique_ptr client_socket) : client_socket_(std::move(client_socket)), - pkt_(pkt, std::free), - b_(std::move(b)), peer_cred_(PeerCredentials::Get(client_socket_->GetFd())) { + char header[sizeof(int) + sizeof(int) + sizeof(int)] = { 0, }; + int ret = client_socket_->Receive(header, sizeof(header)); + if (ret != 0) { + _E("Failed to receive the packet header. error(%d)", ret); + THROW(ret); + } + + memcpy(&cmd_, header, sizeof(int)); + int datalen = 0; + memcpy(&datalen, header + sizeof(int), sizeof(int)); + std::vector buf(datalen); + + ret = client_socket_->Receive(buf.data(), datalen); + if (ret != 0) { + _E("Failed to receive the packet. error(%d)", ret); + THROW(ret); + } + + std::string raw(buf.begin(), buf.end()); + b_ = tizen_base::Bundle(raw); } ClientSocket* Request::GetClientSocket() const { return client_socket_.get(); } -app_pkt_t* Request::GetAppPacket() const { - return pkt_.get(); -} - const tizen_base::Bundle& Request::GetBundle() const { return b_; } @@ -49,7 +68,7 @@ const AppInfo* Request::GetAppInfo() const { } int Request::GetCmd() const { - return pkt_->cmd; + return cmd_; } pid_t Request::GetCallerPid() const { diff --git a/src/launchpad-process-pool/request.hh b/src/launchpad-process-pool/request.hh index 3079a9f..15313fe 100644 --- a/src/launchpad-process-pool/request.hh +++ b/src/launchpad-process-pool/request.hh @@ -26,19 +26,15 @@ #include #include -#include "lib/common/inc/launchpad_common.h" - #include "launchpad-process-pool/loader_context.hh" namespace launchpad { class Request { public: - Request(std::unique_ptr client_socket, app_pkt_t* pkt, - tizen_base::Bundle b); + explicit Request(std::unique_ptr client_socket); ClientSocket* GetClientSocket() const; - app_pkt_t* GetAppPacket() const; const tizen_base::Bundle& GetBundle() const; void SetAppInfo(std::unique_ptr app_info); const AppInfo* GetAppInfo() const; @@ -57,9 +53,9 @@ class Request { private: std::unique_ptr client_socket_; - std::unique_ptr pkt_; - tizen_base::Bundle b_; std::unique_ptr peer_cred_; + int cmd_ = -1; + tizen_base::Bundle b_; std::unique_ptr app_info_; std::shared_ptr context_; std::shared_ptr available_context_; diff --git a/src/launchpad-process-pool/sequencer.cc b/src/launchpad-process-pool/sequencer.cc index 2232dea..98305c1 100644 --- a/src/launchpad-process-pool/sequencer.cc +++ b/src/launchpad-process-pool/sequencer.cc @@ -20,7 +20,7 @@ #include #include "launchpad-process-pool/config.hh" -#include "lib/common/inc/launchpad_common.h" +#include "launchpad-process-pool/log_private.hh" namespace launchpad { diff --git a/src/launchpad-process-pool/signal_manager.cc b/src/launchpad-process-pool/signal_manager.cc index 5340025..8d273c2 100644 --- a/src/launchpad-process-pool/signal_manager.cc +++ b/src/launchpad-process-pool/signal_manager.cc @@ -23,8 +23,6 @@ #include #include -#include "lib/common/inc/launchpad_common.h" - #include "launchpad-process-pool/dbus.hh" #include "launchpad-process-pool/log_private.hh" @@ -39,8 +37,11 @@ class GarbageCollector : public launchpad::Worker::Job { void Do() override { _W("pid: %d", pid_); - _delete_sock_path(pid_, getuid()); try { + std::string path = "/run/aul/apps/" + std::to_string(getuid()) + "/" + + std::to_string(pid_); + DeleteSocketPath(fs::path(path)); + DeleteUnusedFiles(); SocketGarbadgeCollector(); } catch (const std::filesystem::filesystem_error& e) { @@ -49,6 +50,14 @@ class GarbageCollector : public launchpad::Worker::Job { } private: + void DeleteSocketPath(const fs::path& path) { + try { + fs::remove_all(path); + } catch (const fs::filesystem_error& e) { + _E("Exception occurs. error(%s)", e.what()); + } + } + void SocketGarbadgeCollector() { std::string path = "/run/aul/apps/" + std::to_string(getuid()); for (const auto &entry : fs::directory_iterator(path)) { @@ -57,7 +66,7 @@ class GarbageCollector : public launchpad::Worker::Job { std::string proc_path = "/proc/" + entry.path().filename().string(); if (!fs::exists(proc_path)) - _delete_sock_path(atoi(entry.path().filename().c_str()), getuid()); + DeleteSocketPath(entry.path()); } } diff --git a/src/lib/launchpad-common/aul_keys.hh b/src/lib/launchpad-common/aul_keys.hh index 8548872..82fb1db 100644 --- a/src/lib/launchpad-common/aul_keys.hh +++ b/src/lib/launchpad-common/aul_keys.hh @@ -41,7 +41,7 @@ constexpr const char kAulRootPath[] = "__AUL_ROOT_PATH__"; constexpr const char kAulApiVersion[] = "__AUL_API_VERSION__"; constexpr const char kAulLoaderName[] = "__AUL_LOADER_NAME__"; constexpr const char kAulSdk[] = "__AUL_SDK__"; -constexpr const char kAulOrgCallrPid[] = "__AUL_ORG_CALLER_PID__"; +constexpr const char kAulOrgCallerPid[] = "__AUL_ORG_CALLER_PID__"; constexpr const char kAulHighPriority[] = "__AUL_HIGHPRIORITY__"; constexpr const char kAulIsGlobal[] = "__AUL_IS_GLOBAL__"; constexpr const char kAulTepPath[] = "__AUL_TEP_PATH__"; diff --git a/src/lib/launchpad-common/server_socket.cc b/src/lib/launchpad-common/server_socket.cc index 772c196..af98bb7 100644 --- a/src/lib/launchpad-common/server_socket.cc +++ b/src/lib/launchpad-common/server_socket.cc @@ -152,4 +152,12 @@ int ServerSocket::RemoveFd() { return fd; } +void ServerSocket::SetCloseOnExec(bool do_close) { + int flags = fcntl(fd_, F_GETFD); + int ret = fcntl(fd_, F_SETFD, + do_close ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC); + if (ret != 0) + _E("fcntl() is failed. errno(%d)", errno); +} + } // namespace launchpad diff --git a/src/lib/launchpad-common/server_socket.hh b/src/lib/launchpad-common/server_socket.hh index 603d6e7..50d932b 100644 --- a/src/lib/launchpad-common/server_socket.hh +++ b/src/lib/launchpad-common/server_socket.hh @@ -47,6 +47,7 @@ class EXPORT_API ServerSocket { bool IsClosed() const; int GetFd() const; int RemoveFd(); + void SetCloseOnExec(bool do_close); private: int fd_; diff --git a/src/lib/launchpad-common/types.hh b/src/lib/launchpad-common/types.hh new file mode 100644 index 0000000..e19b5b3 --- /dev/null +++ b/src/lib/launchpad-common/types.hh @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIB_LAUNCHPAD_COMMON_TYPES_HH_ +#define LIB_LAUNCHPAD_COMMON_TYPES_HH_ + +#undef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) + +namespace launchpad { + +const int kLoaderArgLength = 1024; + +enum LoaderArg { + Path, + Type, + Id, + Hydra, + Extra, + Dummy, +}; + +enum HydraCmd { + LaunchCandidate, + LaunchCandidateWithArgs, +}; + +enum LoaderType { + Unsupported = -1, + None = 0, + User = 1, + Dynamic = 100, + Max, +}; + +enum PadCmd { + Launch = 0, + Visibility = 10, + AddLoader = 11, + RemoveLoader = 12, + MakeDefaultSlots = 13, + Demand = 14, + Ping = 15, + UpdateAppType = 16, + PrepareAppDefinedLoader = 17, + Connect = 18, +}; + +enum PadLoaderId { + Static = 0, + Direct = 1, + DynamicBase = 10, +}; + +enum AmdCmd { + LaunchpadDeadSignal = 61, + LaunchpadLaunchSignal = 83, + AppStartupSignal = 89, +}; + +} // namespace launchpad + +#endif // LIB_LAUNCHPAD_COMMON_TYPES_HH_ diff --git a/src/lib/launchpad-glib/CMakeLists.txt b/src/lib/launchpad-glib/CMakeLists.txt index 2c519fa..44fa0b5 100644 --- a/src/lib/launchpad-glib/CMakeLists.txt +++ b/src/lib/launchpad-glib/CMakeLists.txt @@ -14,8 +14,10 @@ TARGET_INCLUDE_DIRECTORIES(${TARGET_LAUNCHPAD_GLIB} PUBLIC APPLY_PKG_CONFIG(${TARGET_LAUNCHPAD_GLIB} PUBLIC BUNDLE_DEPS + DBUS_DEPS DLOG_DEPS GIO_DEPS + LIBTZPLATFORM_CONFIG_DEPS VCONF_DEPS ) diff --git a/src/lib/launchpad-glib/util.cc b/src/lib/launchpad-glib/util.cc index 2d4e5cf..c97db86 100644 --- a/src/lib/launchpad-glib/util.cc +++ b/src/lib/launchpad-glib/util.cc @@ -16,26 +16,51 @@ #include "launchpad-glib/util.hh" +#include #include +#include #include #include #include #include +#include #include #include +#include +#include #include +#include +#include #include +#include +#include +#include +#include +#include #include "launchpad-glib/log_private.hh" +#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) + namespace fs = std::filesystem; namespace launchpad { namespace { constexpr const char kDefaultLocale[] = "en_US.UTF-8"; +constexpr const char kTepBusName[] = "org.tizen.system.deviced"; +constexpr const char kTepObjectPath[] = "/Org/Tizen/System/DeviceD/Tzip"; +constexpr const char kTepInterfaceName[] = "org.tizen.system.deviced.Tzip"; +constexpr const char kTepIsMountedMethod[] = "IsMounted"; +const int kMaxTepIsMountRetryCount = 100; +constexpr const char kApp2sdBusName[] = "org.tizen.app2sd"; +constexpr const char kApp2sdObjectPath[] = "/org/tizen/app2sd"; +constexpr const char kApp2sdInterfaceName[] = "org.tizen.app2sd"; +constexpr const char kApp2sdOndemandSetupInitMethod[] = "OndemandSetupInit"; +const int kApp2sdRetryMax = 5; +const int kApp2sdWaitUsec = 1000000 / 2; // 0.5 sec void SetLanguageEnvironments() { const char* lang = getenv("LANG"); @@ -79,6 +104,227 @@ static void SetExecutionDomain() { } #endif // TIZEN_FEATURE_SET_PERSONALITY_32 +class DBus { + public: + DBus() { + DBusError error; + dbus_error_init(&error); + conn_ = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); + if (conn_ == nullptr) { + _E("Failed to connect to D-Bus Daemon"); + if (dbus_error_is_set(&error)) { + _E("D-Bus error: %s", error.message); + dbus_error_free(&error); + } + THROW(-ENOTCONN); + } + } + + virtual ~DBus() { + if (conn_ != nullptr) + dbus_connection_close(conn_); + } + + void SendMessage(DBusMessage* message, int timeout, int* result) { + if (message == nullptr) { + _E("Invalid parameter"); + THROW(-EINVAL); + } + + DBusPendingCall* pending = nullptr; + dbus_bool_t ret = dbus_connection_send_with_reply(conn_, message, &pending, + timeout); + if (!ret || pending == nullptr) { + _E("Failed to send message"); + THROW(-EIO); + } + + dbus_connection_flush(conn_); + dbus_pending_call_block(pending); + auto* reply = dbus_pending_call_steal_reply(pending); + dbus_pending_call_unref(pending); + if (reply == nullptr) { + _E("Failed to get reply message"); + THROW(-EIO); + } + + auto reply_auto = + std::unique_ptr( + reply, dbus_message_unref); + DBusMessageIter iter; + if (!dbus_message_iter_init(reply, &iter)) { + _E("Message ha no argument"); + THROW(-EIO); + } + + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32) { + _E("Argument is not interger. type(%d)", + dbus_message_iter_get_arg_type(&iter)); + THROW(-EIO); + } + + dbus_int32_t res; + dbus_message_iter_get_basic(&iter, &res); + *result = static_cast(res); + _D("Result: %d", *result); + } + + private: + DBusConnection* conn_ = nullptr; +}; + +class TepMountChecker : public DBus { + public: + explicit TepMountChecker(std::vector paths) + : paths_(std::move(paths)) {} + + bool IsMounted() { + for (auto& path : paths_) { + if (CheckTepMount(path) != 0) + return false; + } + + return true; + } + + private: + DBusMessage* CreateTepMountMessage(const std::string& path) { + DBusMessage* message = dbus_message_new_method_call(kTepBusName, + kTepObjectPath, kTepInterfaceName, kTepIsMountedMethod); + if (message == nullptr) { + _E("dbus_message_new_method_call() is failed"); + return nullptr; + } + + DBusMessageIter iter; + dbus_message_iter_init_append(message, &iter); + auto* tep_path = path.c_str(); + auto ret = dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, + &tep_path); + if (!ret) { + _E("dbus_message_iter_append_basic() is failed"); + dbus_message_unref(message); + return nullptr; + } + + return message; + } + + int IsTepMountDone(const std::string& path) { + int result = -1; + auto message = std::unique_ptr( + CreateTepMountMessage(path), dbus_message_unref); + SendMessage(message.get(), 500, &result); + return result; + } + + int CheckTepMount(const std::string& path) { + if (path.empty()) + return 0; + + int count = 0; + while (count < kMaxTepIsMountRetryCount) { + if (IsTepMountDone(path) == 1) + return 0; + + usleep(50 * 1000); + count++; + } + + _E("Not able to mount within 5 seconds. path(%s", path.c_str()); + return -1; + } + + private: + std::vector paths_; +}; + +void MountDirectories(const std::vector& srcs, + const std::string& dest) { + std::string opt = "lowerdir=" + dest; + for (auto& src : srcs) + opt += ":" + src; + + _D("mount opt: %s", opt.c_str()); + int ret = mount(nullptr, dest.c_str(), "overlay", MS_RDONLY, opt.c_str()); + if (ret != 0) + _E("mount() is failed. dest(%s), errno(%d)", dest.c_str(), errno); +} + +class ExternalPackage : public DBus { + public: + ExternalPackage(std::string package, uid_t uid) + : package_(std::move(package)), uid_(uid) { + _D("package(%s), uid(%u)", package_.c_str(), uid_); + } + + void Enable() { + int result = -1; + int retry_count = 0; + auto message = std::unique_ptr( + CreateApp2sdMessage(), dbus_message_unref); + while (retry_count <= kApp2sdRetryMax) { + try { + SendMessage(message.get(), 500, &result); + } catch (const Exception& e) { + _E("Exception occurs. error(%s)", e.what()); + retry_count++; + continue; + } + + break; + } + + _D("Result: %d", result); + if (result < 0) + THROW(-EIO); + } + + private: + DBusMessage* CreateApp2sdMessage() { + DBusMessage* message = dbus_message_new_method_call(kApp2sdBusName, + kApp2sdObjectPath, kApp2sdInterfaceName, + kApp2sdOndemandSetupInitMethod); + if (message == nullptr) { + _E("dbus_message_new_method_call() is failed"); + return nullptr; + } + + DBusMessageIter iter; + dbus_message_iter_init_append(message, &iter); + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, + package_.c_str())) { + _E("dbus_message_iter_append_basic() is failed"); + dbus_message_unref(message); + return nullptr; + } + + return message; + } + + private: + std::string package_; + uid_t uid_; +}; + +class AmdPacket : public tizen_base::Parcelable { + public: + explicit AmdPacket(int cmd) : cmd_(cmd) {} + + void WriteToParcel(tizen_base::Parcel* parcel) const { + parcel->WriteInt32(cmd_); + parcel->WriteInt32(0); + parcel->WriteInt32(0); + } + + void ReadFromParcel(tizen_base::Parcel* parcel) { + parcel->ReadInt32(&cmd_); + } + + private: + int cmd_; +}; + } // namespace void Util::SetEnvironments(const AppInfo* app_info) { @@ -143,4 +389,156 @@ void Util::DeleteSocketPath(pid_t pid, uid_t uid) { } } +int Util::EnableExternalPackage(const AppInfo* app_info) { + auto installed_storage = app_info->GetBundle().GetString( + kAulInstalledStorage); + if (installed_storage != "external") + return 0; + + try { + ExternalPackage external_package(app_info->GetPkgId(), + app_info->IsGlobal() ? GLOBAL_USER : getuid()); + external_package.Enable(); + } catch (const Exception& e) { + _E("Exception occurs. error(%s)", e.what()); + return -1; + } + + return 0; +} + +int Util::MountResourceDirectories(const AppInfo* app_info) { + auto& root_path = app_info->GetRootPath(); + auto& b = app_info->GetBundle(); + auto global_res_dir = b.GetStringArray(kAulMountGlobalResDir); + if (!global_res_dir.empty()) + MountDirectories(global_res_dir, root_path + "/res/mount/global"); + + auto allowed_res_dir = b.GetStringArray(kAulMountAllowedResDir); + if (!allowed_res_dir.empty()) + MountDirectories(allowed_res_dir, root_path + "/res/mount/allowed"); + + auto res_pkgids = b.GetStringArray(kAulMountResPkgIds); + if (!res_pkgids.empty()) { + std::string pkgids; + for (auto& pkgid : res_pkgids) { + if (!pkgids.empty()) + pkgids += ":"; + + pkgids += pkgid; + } + + setenv("RES_PKGIDS", pkgids.c_str(), 1); + } + + return 0; +} + +int Util::WaitTepMount(const AppInfo* app_info) { + if (app_info->GetBundle().GetType(kAulTepPath) == BUNDLE_TYPE_NONE) + return 0; + + try { + auto paths = app_info->GetBundle().GetStringArray(kAulTepPath); + TepMountChecker checker(std::move(paths)); + if (!checker.IsMounted()) + return -1; + } catch (const Exception& e) { + _E("Exception occurs. error(%s)", e.what()); + return e.GetErrorCode(); + } + + _I("TEP Mount has been done"); + return 0; +} + +std::string Util::GetLibDirectory(const AppInfo* app_info) { + std::filesystem::path path(app_info->GetAppPath()); + auto lib_dir = path.relative_path().string() + "/../lib/"; + if (std::filesystem::exists(lib_dir)) + return lib_dir; + + return ""; +} + +void Util::CloseAllFds() { + int aul_fd = -1; + const char* aul_listen_fd = getenv("AUL_LISTEN_FD"); + if (aul_listen_fd != nullptr) + aul_fd = atoi(aul_listen_fd); + + try { + fs::path proc_path("/proc/self/fd"); + for (const auto& entry : fs::directory_iterator(proc_path)) { + if (!isdigit(entry.path().filename().string()[0])) + continue; + + int fd = std::stoi(entry.path().filename().string()); + if (fd < 3 || fd == aul_fd) + continue; + + close(fd); + } + } catch (const fs::filesystem_error& e) { + _E("Execption occurs. error(%s)", e.what()); + } +} + +int Util::PrepareAppSocket() { + try { + std::string path = "/run/aul/apps/" + std::to_string(getuid()) + "/" + + std::to_string(getpid()); + ServerSocket socket; + if (!fs::exists(path)) + fs::create_directory(path); + + path += "/.app-sock"; + socket.Bind(path); + socket.Listen(128); + socket.SetReceiveBufferSize(Socket::kSocketMaxBufferSize); + socket.SetSendBufferSize(Socket::kSocketMaxBufferSize); + socket.SetCloseOnExec(false); + int fd = socket.RemoveFd(); + setenv("AUL_LISTEN_FD", std::to_string(fd).c_str(), 1); + } catch (const Exception& e) { + _E("Exception occurs. error(%s)", e.what()); + return e.GetErrorCode(); + } + + return 0; +} + +int Util::PrepareAppIdFile(const AppInfo* app_info) { + std::string path = "/run/aul/apps/" + std::to_string(getuid()) + "/" + + std::to_string(getpid()) + "/" + app_info->GetAppId(); + std::ofstream stream(path); + stream.close(); + return 0; +} + +int Util::SendCmdToAmd(enum AmdCmd cmd) { + try { + std::string endpoint = "/run/aul/daemons/.amd-sock"; + ClientSocket socket; + socket.Connect(endpoint); + socket.SetReceiveBufferSize(Socket::kSocketMaxBufferSize); + socket.SetReceiveTimeout(5000); + + AmdPacket packet(static_cast(cmd)); + tizen_base::Parcel parcel; + parcel.WriteParcelable(packet); + + int ret = socket.Send(parcel.GetData(), parcel.GetDataSize()); + if (ret != 0) { + _E("Failed to send cmd(%d), error(%d)", static_cast(cmd), ret); + return -ECOMM; + } + } catch (const Exception& e) { + _E("Exception occrus. error(%s)", e.what()); + return e.GetErrorCode(); + } + + return 0; +} + } // namespace launchpad diff --git a/src/lib/launchpad-glib/util.hh b/src/lib/launchpad-glib/util.hh index 0589202..d1646c0 100644 --- a/src/lib/launchpad-glib/util.hh +++ b/src/lib/launchpad-glib/util.hh @@ -17,17 +17,34 @@ #ifndef LIB_LAUNCHPAD_GLIB_UTIL_HH_ #define LIB_LAUNCHPAD_GLIB_UTIL_HH_ +#include +#include + +#include + #include +#include #undef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) +#undef GLOBAL_USER +#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) + namespace launchpad { class EXPORT_API Util { public: static void SetEnvironments(const AppInfo* app_info); static void DeleteSocketPath(pid_t pid, uid_t uid); + static int EnableExternalPackage(const AppInfo* app_info); + static int MountResourceDirectories(const AppInfo* app_info); + static int WaitTepMount(const AppInfo* app_info); + static std::string GetLibDirectory(const AppInfo* app_info); + static void CloseAllFds(); + static int PrepareAppSocket(); + static int PrepareAppIdFile(const AppInfo* app_info); + static int SendCmdToAmd(enum AmdCmd cmd); }; } // namespace launchpad diff --git a/src/lib/launchpad-hydra/launchpad_hydra.cc b/src/lib/launchpad-hydra/launchpad_hydra.cc index c08106b..c2648ab 100644 --- a/src/lib/launchpad-hydra/launchpad_hydra.cc +++ b/src/lib/launchpad-hydra/launchpad_hydra.cc @@ -27,9 +27,7 @@ #include #include #include - -#include "common/inc/launchpad_common.h" -#include "common/inc/launchpad_types.h" +#include #include "launchpad-hydra/log_private.hh" #include "launchpad-hydra/event_loop.hh" @@ -55,21 +53,21 @@ LaunchpadHydra::LaunchpadHydra(int argc, char** argv) : argc_(argc) { THROW(-EINVAL); } - type_ = argv[LOADER_ARG_TYPE][0] - '0'; - if (type_ < 0 || type_ >= LAUNCHPAD_LOADER_TYPE_MAX) { + type_ = argv[LoaderArg::Type][0] - '0'; + if (type_ < 0 || type_ >= LoaderType::Max) { _E("Invalid argument. type(%d)", type_); THROW(-EINVAL); } - int hydra_mode = argv[LOADER_ARG_HYDRA][0] - '0'; + int hydra_mode = argv[LoaderArg::Hydra][0] - '0'; _D("Hydra mode: %d", hydra_mode); if (!hydra_mode) { _W("Run in non hydra mode"); THROW(-EINVAL); } - argv[LOADER_ARG_HYDRA] = const_cast("0"); - loader_id_ = atoi(argv[LOADER_ARG_ID]); + argv[LoaderArg::Hydra] = const_cast("0"); + loader_id_ = atoi(argv[LoaderArg::Id]); HydraArgs::GetInst().Set(argc, argv); std::string env = std::to_string( @@ -195,10 +193,10 @@ int LaunchpadHydra::GetLoaderId() const { void LaunchpadHydra::HandleLaunchEvent(const HydraRequest& request) { pid_t pid = -1; - if (request.GetCommand() == LAUNCH_CANDIDATE) { + if (request.GetCommand() == HydraCmd::LaunchCandidate) { _W("LAUNCH_CANDIDATE"); pid = executor_->Execute(argc_, argv_, request.GetPriority()); - } else if (request.GetCommand() == LAUNCH_CANDIDATE_WITH_ARGS) { + } else if (request.GetCommand() == HydraCmd::LaunchCandidateWithArgs) { _W("LAUNCH_CANDIDATE_WITH_ARGS"); pid = executor_->Execute(request.GetArgc(), request.GetArgv(), request.GetPriority()); diff --git a/src/lib/launchpad/launchpad_loader.cc b/src/lib/launchpad/launchpad_loader.cc index 6ee82f1..3cd5f2a 100644 --- a/src/lib/launchpad/launchpad_loader.cc +++ b/src/lib/launchpad/launchpad_loader.cc @@ -31,9 +31,8 @@ #include #include #include +#include -#include "common/inc/launchpad_common.h" -#include "common/inc/launchpad_types.h" #include "launchpad/log_private.hh" #include "launchpad/step_prepare_execution.hh" #include "launchpad/thread_control.hh" @@ -61,19 +60,19 @@ LaunchpadLoader::LaunchpadLoader(int argc, char** argv) THROW(-EINVAL); } - int is_hydra = argv_[LOADER_ARG_HYDRA][0] - '0'; + int is_hydra = argv_[LoaderArg::Hydra][0] - '0'; if (is_hydra) { _E("Cannot run in hydra mode"); THROW(-EINVAL); } - loader_type_ = atoi(argv_[LOADER_ARG_TYPE]); - if (loader_type_ < 0 || loader_type_ >= LAUNCHPAD_TYPE_MAX) { + loader_type_ = atoi(argv_[LoaderArg::Type]); + if (loader_type_ < 0 || loader_type_ >= LoaderType::Max) { _E("Invalid argument. type: %d", loader_type_); THROW(-EINVAL); } - loader_id_ = atoi(argv_[LOADER_ARG_ID]); + loader_id_ = atoi(argv_[LoaderArg::Id]); _W("loader type: %d, loader id: %d", loader_type_, loader_id_); context = this; } @@ -125,9 +124,9 @@ const tizen_base::Bundle& LaunchpadLoader::GetBundle() const { } void LaunchpadLoader::ResetArgs() { - memset(argv_[LOADER_ARG_TYPE], 0, strlen(argv_[LOADER_ARG_TYPE])); - memset(argv_[LOADER_ARG_ID], 0, strlen(argv_[LOADER_ARG_ID])); - memset(argv_[LOADER_ARG_EXTRA], 0, strlen(argv_[LOADER_ARG_EXTRA])); + memset(argv_[LoaderArg::Type], 0, strlen(argv_[LoaderArg::Type])); + memset(argv_[LoaderArg::Id], 0, strlen(argv_[LoaderArg::Id])); + memset(argv_[LoaderArg::Extra], 0, strlen(argv_[LoaderArg::Extra])); } void LaunchpadLoader::WaitForThreads(int threads) { @@ -172,7 +171,7 @@ int LaunchpadLoader::ConnectToLaunchpad() { bool LaunchpadLoader::OnCreate() { setsid(); - tizen_base::Bundle extra(argv_[LOADER_ARG_EXTRA]); + tizen_base::Bundle extra(argv_[LoaderArg::Extra]); ResetArgs(); if (callback_.create == nullptr) { @@ -239,7 +238,7 @@ void LaunchpadLoader::ChangeArgs(int argc, char** argv) { } memset(argv_[0], '\0', strlen(argv_[0])); - snprintf(argv_[0], LOADER_ARG_LEN, "%s", argv[0]); + snprintf(argv_[0], kLoaderArgLength, "%s", argv[0]); } int LaunchpadLoader::OnLaunch(int argc, char** argv, AppInfo* app_info) { diff --git a/src/lib/launchpad/step_prepare_execution.cc b/src/lib/launchpad/step_prepare_execution.cc index ad8481a..88d2fa0 100644 --- a/src/lib/launchpad/step_prepare_execution.cc +++ b/src/lib/launchpad/step_prepare_execution.cc @@ -29,9 +29,9 @@ #include #include +#include +#include -#include "common/inc/launchpad_common.h" -#include "common/inc/launchpad_types.h" #include "launchpad/log_private.hh" #include "launchpad/thread_control.hh" @@ -76,9 +76,7 @@ int StepPrepareExecution::Prepare(AppInfo* app_info) { } int StepPrepareExecution::EnableExternalPackage(AppInfo* app_info) { - int ret = _enable_external_pkg(app_info->GetBundle().GetHandle(), - app_info->GetPkgId().c_str(), - app_info->IsGlobal() ? GLOBAL_USER : getuid()); + int ret = Util::EnableExternalPackage(app_info); if (ret < 0) { _E("Failed to enable external package. error: %d", ret); return -1; @@ -100,8 +98,7 @@ int StepPrepareExecution::TrustAnchorLaunch(AppInfo* app_info) { } int StepPrepareExecution::MountResourceDirectories(AppInfo* app_info) { - int ret = _mount_res_dir(app_info->GetRootPath().c_str(), - app_info->GetBundle().GetHandle()); + int ret = Util::MountResourceDirectories(app_info); if (ret < 0) { _E("Failed to mount resource direstories. error: %d", ret); return -1; @@ -129,7 +126,7 @@ int StepPrepareExecution::SecurityManagerPrepareApp(AppInfo* app_info) { } int StepPrepareExecution::SetupStdio(AppInfo* app_info) { - _setup_stdio(basename(app_info->GetAppPath().c_str())); + Stdio::Setup(); return 0; } @@ -157,15 +154,15 @@ int StepPrepareExecution::SetDumpable(AppInfo* app_info) { } int StepPrepareExecution::SetProcessName(AppInfo* app_info) { + char* name = basename(const_cast(app_info->GetAppPath().c_str())); char process_name[16] = { 0, }; - snprintf(process_name, sizeof(process_name), "%s", - basename(app_info->GetAppPath().c_str())); + snprintf(process_name, sizeof(process_name), "%s", name); prctl(PR_SET_NAME, process_name); return 0; } int StepPrepareExecution::WaitTepMount(AppInfo* app_info) { - int ret = _wait_tep_mount(app_info->GetBundle().GetHandle()); + int ret = Util::WaitTepMount(app_info); if (ret < 0) { _E("Failed to wait tep mount. error: %d", ret); return -1; @@ -175,7 +172,7 @@ int StepPrepareExecution::WaitTepMount(AppInfo* app_info) { } int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) { - int ret = _prepare_app_socket(); + int ret = Util::PrepareAppSocket(); if (ret < 0) { _E("Failed to prepare app socket. error: %d", ret); return -1; @@ -185,7 +182,7 @@ int StepPrepareExecution::PrepareAppSocket(AppInfo* app_info) { } int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) { - int ret = _prepare_id_file(); + int ret = Util::PrepareAppIdFile(app_info); if (ret < 0) { _E("Failed to prepare id file. error: %d", ret); return -1; @@ -195,7 +192,7 @@ int StepPrepareExecution::PrepareIdFile(AppInfo* app_info) { } int StepPrepareExecution::SendStartupSignal(AppInfo* app_info) { - if (_send_cmd_to_amd(APP_STARTUP_SIGNAL) != 0) + if (Util::SendCmdToAmd(AmdCmd::AppStartupSignal) != 0) _W("Failed to send startup signal"); return 0;