Add a new function for loader termination 09/298809/5
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Sep 2023 23:44:15 +0000 (08:44 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 14 Sep 2023 00:43:53 +0000 (00:43 +0000)
The launchpad_loader_dispose() function is added to dispose the loader iself.
The function sends a disposal request to the launchpad-process-pool.
When the launchpad-process-pool gets the request, it sends SIGKILL signal to
the running loader process.

Change-Id: I74ed0b2b5fa42d47220b335e84fb1776b7b748f5
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/launchpad.cc
src/launchpad-process-pool/launchpad.hh
src/launchpad-process-pool/loader_manager.hh
src/lib/launchpad-common/types.hh
src/lib/launchpad/inc/launchpad.h
src/lib/launchpad/launchpad_loader.cc
src/lib/launchpad/launchpad_loader.hh

index c1f0f9b..fdde825 100644 (file)
@@ -197,6 +197,9 @@ Launchpad::Launchpad(int argc, char** argv)
     { PadCmd::RestartLoader,
       std::bind(&Launchpad::HandleRestartLoaderRequest, this,
           std::placeholders::_1) },
+    { PadCmd::DisposeLoader,
+      std::bind(&Launchpad::HandleDisposeLoaderRequest, this,
+          std::placeholders::_1) },
   };
 
   CPUBoostController::Level level;
@@ -295,7 +298,7 @@ void Launchpad::OnTerminate() {
 void Launchpad::HandleVisibilityRequest(std::shared_ptr<Request> request) {
   LoaderManager::GetInst().ActivateLoaderContexts(LoaderMethod::Visibility);
   request->SendResult(0);
-  _D("[PAD_CMD_VISIBILITY]");
+  _W("[PAD_CMD_VISIBILITY]");
 }
 
 void Launchpad::HandleAddLoaderRequest(std::shared_ptr<Request> request) {
@@ -308,7 +311,7 @@ void Launchpad::HandleAddLoaderRequest(std::shared_ptr<Request> request) {
   }
 
   request->SendResult(context->GetLoaderId());
-  _D("[PAD_CMD_ADD_LOADER]");
+  _W("[PAD_CMD_ADD_LOADER]");
 }
 
 void Launchpad::HandleRemoveLoaderRequest(std::shared_ptr<Request> request) {
@@ -323,13 +326,13 @@ void Launchpad::HandleRemoveLoaderRequest(std::shared_ptr<Request> request) {
   LoaderManager::GetInst().RemoveLoaderContext(LoaderType::Dynamic,
       std::stoi(loader_id_str));
   request->SendResult(0);
-  _D("[PAD_CMD_REMOVE_LOADER]");
+  _W("[PAD_CMD_REMOVE_LOADER]");
 }
 
 void Launchpad::HandleMakeDefaultSlotsRequest(
   std::shared_ptr<Request> request) {
   LoaderManager::GetInst().AddDefaultLoaderContexts();
-  _D("[PAD_CMD_MAKE_DEFAULT_SLOTS]");
+  _W("[PAD_CMD_MAKE_DEFAULT_SLOTS]");
 }
 
 void Launchpad::HandlePrepareAppDefinedLoaderRequest(
@@ -350,18 +353,18 @@ void Launchpad::HandlePrepareAppDefinedLoaderRequest(
   }
 
   request->SendResult(context->GetLoaderId());
-  _D("[PAD_CMD_PREPARE_APP_DEFINED_LOADER] result: %d", context->GetLoaderId());
+  _W("[PAD_CMD_PREPARE_APP_DEFINED_LOADER] result: %d", context->GetLoaderId());
 }
 
 void Launchpad::HandleDemandRequest(std::shared_ptr<Request> request) {
   LoaderManager::GetInst().ActivateLoaderContexts(LoaderMethod::Demand);
   request->SendResult(0);
-  _D("[PAD_CMD_DEMAND]");
+  _W("[PAD_CMD_DEMAND]");
 }
 
 void Launchpad::HandlePingRequest(std::shared_ptr<Request> request) {
   request->SendResult(getpid());
-  _D("[PAD_CMD_PING]");
+  _W("[PAD_CMD_PING]");
 }
 
 void Launchpad::HandleUpdateAppTypeRequest(std::shared_ptr<Request> request) {
@@ -376,13 +379,13 @@ void Launchpad::HandleUpdateAppTypeRequest(std::shared_ptr<Request> request) {
   _I("type: %s, installed: %s", app_type.c_str(), installed.c_str());
   bool app_installed = (installed == "true") ? true : false;
   LoaderManager::GetInst().UpdateAppInstallationStatus(app_type, app_installed);
-  _D("[PAD_CMD_UPDATE_APP_TYPE]");
+  _W("[PAD_CMD_UPDATE_APP_TYPE]");
 }
 
 void Launchpad::HandleConnectRequest(std::shared_ptr<Request> request) {
   client_socket_.reset(
       new ClientSocket(request->GetClientSocket()->RemoveFd()));
-  _D("[PAD_CMD_CONNECT] client fd: %d", client_socket_->GetFd());
+  _W("[PAD_CMD_CONNECT] client fd: %d", client_socket_->GetFd());
 }
 
 bool Launchpad::CanUseLoaderContext(
@@ -511,6 +514,7 @@ Launchpad::LaunchResult Launchpad::LaunchRequestPend(
     CPUBoostController::DoBoost(loader_context->GetPid(),
         CPUBoostController::Level::Strong, 10000);
     _W("Send result: %d", loader_context->GetPid());
+    request->SetPid(loader_context->GetPid());
     request->SendResult(loader_context->GetPid());
   }
 
@@ -571,7 +575,7 @@ void Launchpad::HandleLaunchRequest(std::shared_ptr<Request> request) {
     return;
 
   LaunchRequestComplete(request);
-  _D("[PAD_CMD_LAUNCH] appid: %s, result: %d",
+  _W("[PAD_CMD_LAUNCH] appid: %s, result: %d",
       request->GetAppInfo()->GetAppId().c_str(), request->GetPid());
 }
 
@@ -588,7 +592,7 @@ void Launchpad::HandleKillLoaderRequest(std::shared_ptr<Request> request) {
   if (loader_context->RefCount() == 0)
     loader_context->Dispose();
 
-  _D("[PAD_CMD_KILL_LOADER] loader_name: %s", loader_name.c_str());
+  _W("[PAD_CMD_KILL_LOADER] loader_name: %s", loader_name.c_str());
 }
 
 void Launchpad::HandleRestartLoaderRequest(std::shared_ptr<Request> request) {
@@ -607,10 +611,24 @@ void Launchpad::HandleRestartLoaderRequest(std::shared_ptr<Request> request) {
   if (loader_context->GetPid() < 1)
     loader_context->Prepare();
 
-  _D("[PAD_CMD_RESTART_LOADER] loader_name: %s, pid: %d",
+  _W("[PAD_CMD_RESTART_LOADER] loader_name: %s, pid: %d",
       loader_name.c_str(), loader_context->GetPid());
 }
 
+void Launchpad::HandleDisposeLoaderRequest(std::shared_ptr<Request> request) {
+  pid_t caller_pid = request->GetCallerPid();
+  auto loader_context =
+      LoaderManager::GetInst().FindLoaderContextFromPid(caller_pid);
+  if (loader_context == nullptr) {
+    _E("Failed to find loader context. pid(%d)", caller_pid);
+    return;
+  }
+
+  loader_context->Dispose();
+  _W("[PAD_CMD_DISPOSE_LOADER] loader_name: %s, pid: %d",
+     loader_context->GetLoaderName().c_str(), caller_pid);
+}
+
 void Launchpad::OnIOEventReceived(int fd, int condition) {
   auto client_socket = socket_->Accept();
   if (!client_socket) {
@@ -655,10 +673,10 @@ void Launchpad::OnSigchldReceived(pid_t pid) {
   while (iter != pending_requests_.end()) {
     auto request = *iter;
     auto context = request->GetAvailableLoaderContext();
-    if (context != nullptr && context->GetPid() == pid) {
+    if (context != nullptr && request->GetPid() == pid) {
       context->Unref();
       auto* app_info = request->GetAppInfo();
-      launchpad::DBus::SendAppLaunchSignal(context->GetPid(),
+      launchpad::DBus::SendAppLaunchSignal(request->GetPid(),
         app_info->GetAppId().c_str());
       pending_requests_.erase(iter);
       break;
index acf7670..4e6cb01 100644 (file)
@@ -78,6 +78,7 @@ class Launchpad : public IOChannel::IEvent,
   void HandleLaunchRequest(std::shared_ptr<Request> request);
   void HandleKillLoaderRequest(std::shared_ptr<Request> request);
   void HandleRestartLoaderRequest(std::shared_ptr<Request> request);
+  void HandleDisposeLoaderRequest(std::shared_ptr<Request> request);
 
   bool CanUseLoaderContext(const std::shared_ptr<LoaderContext>& context);
   LaunchResult ForkProcessing(std::shared_ptr<Request> request);
index 76825ad..7f3f078 100644 (file)
@@ -78,13 +78,13 @@ class LoaderManager : public AppDefinedLoaderInfoManager::IEvent,
   void RemoveLoaderContext(LoaderType type, int loader_id);
   std::shared_ptr<LoaderContext> FindLoaderContextFromName(
       const std::string& loader_name);
+  std::shared_ptr<LoaderContext> FindLoaderContextFromPid(pid_t pid);
 
  private:
   LoaderManager();
   ~LoaderManager();
 
   void Init();
-  std::shared_ptr<LoaderContext> FindLoaderContextFromPid(pid_t pid);
   std::shared_ptr<HydraLoaderContext> FindHydraLoaderContextFromPid(pid_t pid);
   std::shared_ptr<LoaderContext> FindLoaderContextFromLoaderId(int loader_id);
   std::shared_ptr<LoaderContext> FindLoaderContextFromType(LoaderType type);
index 7483b36..5b7eada 100644 (file)
@@ -59,6 +59,7 @@ enum PadCmd {
   Connect = 18,
   KillLoader = 19,
   RestartLoader = 20,
+  DisposeLoader = 21,
 };
 
 enum PadLoaderId {
index b7d6c21..ba5867c 100644 (file)
@@ -88,7 +88,7 @@ bundle *launchpad_loader_get_bundle(void);
  * @return @c on success,
  *         otherwise a negative error value
  *
- * @see #launchpad_loader_unblock_threads()
+ * @see launchpad_loader_unblock_threads()
  */
 int launchpad_loader_block_threads(void);
 
@@ -98,10 +98,20 @@ int launchpad_loader_block_threads(void);
  *
  * @return @c on success,
  *         otherwise a negative error value
- * @see #launchpad_loader_block_threads()
+ * @see launchpad_loader_block_threads()
  */
 int launchpad_loader_unblock_threads(void);
 
+/**
+ * @brief Disposes the loader.
+ * @details This function sends a disposal request to the launchpad.
+ *          The launchpad will dispose the loader process using kill().
+ * @since_tizen 7.0
+ *
+ * @see launchpad_loader_main();
+ */
+void launchpad_loader_dispose(void);
+
 #ifdef __cplusplus
 }
 #endif
index d6815ef..766eb22 100644 (file)
@@ -17,6 +17,7 @@
 #include "launchpad/launchpad_loader.hh"
 
 #include <aul.h>
+#include <aul_sock.h>
 #include <bundle_cpp.h>
 #include <malloc.h>
 #include <sys/types.h>
@@ -43,6 +44,8 @@ namespace {
 
 const uint32_t kMaxRetryingCount = 600;
 constexpr const char kLaunchpadLoaderSocketName[] = ".launchpad-type";
+constexpr const char kLaunchpadProcessPoolSock[] =
+    ".launchpad-process-pool-sock";
 
 tizen_base::Bundle loader_bundle;
 LaunchpadLoader* context = nullptr;
@@ -123,6 +126,21 @@ const tizen_base::Bundle& LaunchpadLoader::GetBundle() const {
   return app_info_.GetBundle();
 }
 
+void LaunchpadLoader::SendDisposalRequest() {
+  int fd = aul_sock_create_launchpad_client(kLaunchpadProcessPoolSock,
+      getuid());
+  if (fd < 0) {
+    _E("Failed to create launchpad client socket. error(%d)", fd);
+    return;
+  }
+
+  tizen_base::Bundle b;
+  int ret = aul_sock_send_bundle_with_fd(fd,
+      static_cast<int>(PadCmd::DisposeLoader), b.GetHandle(), AUL_SOCK_NOREPLY);
+  if (ret != AUL_R_OK)
+    _E("Failed to send disposal request. error(%d)", ret);
+}
+
 void LaunchpadLoader::ResetArgs() {
   memset(argv_[LoaderArg::Type], 0, strlen(argv_[LoaderArg::Type]));
   memset(argv_[LoaderArg::Id], 0, strlen(argv_[LoaderArg::Id]));
@@ -385,3 +403,13 @@ extern "C" EXPORT_API int launchpad_loader_block_threads(void) {
 extern "C" EXPORT_API int launchpad_loader_unblock_threads(void) {
   return ThreadControl::GetInst().UnblockThreads();
 }
+
+extern "C" EXPORT_API void launchpad_loader_dispose(void) {
+  if (!::context) {
+    _E("Invalid context");
+    return;
+  }
+
+  _W("Dispose");
+  ::context->SendDisposalRequest();
+}
index 8f44252..c089ab6 100644 (file)
@@ -41,6 +41,7 @@ class LaunchpadLoader {
       void* user_data);
   void Quit();
   const tizen_base::Bundle& GetBundle() const;
+  void SendDisposalRequest();
 
  private:
   void WaitForThreads(int threads);