Refactor app labels monitor 43/290643/2
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 30 Mar 2023 05:17:11 +0000 (05:17 +0000)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 30 Mar 2023 06:20:42 +0000 (06:20 +0000)
The app labels monitor is implmeneted using C++ language.

Change-Id: I329a9a80a1428487a38c2439383242d6eb036fa8
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/app_labels_monitor.cc [new file with mode: 0644]
src/launchpad-process-pool/app_labels_monitor.hh [new file with mode: 0644]
src/launchpad-process-pool/launchpad.cc

diff --git a/src/launchpad-process-pool/app_labels_monitor.cc b/src/launchpad-process-pool/app_labels_monitor.cc
new file mode 100644 (file)
index 0000000..05425a6
--- /dev/null
@@ -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.
+ */
+
+#include "launchpad-process-pool/app_labels_monitor.hh"
+
+#include <exception.hh>
+
+#include "launchpad-process-pool/log.hh"
+#include "launchpad-process-pool/log_private.hh"
+
+namespace launchpad {
+
+AppLabelsMonitor::AppLabelsMonitor() {
+  app_labels_monitor* monitor = nullptr;
+  int ret = security_manager_app_labels_monitor_init(&monitor);
+  if (ret != SECURITY_MANAGER_SUCCESS) {
+    _E("Failed to initialize app labels monitor. error: %d", ret);
+    THROW(ret);
+  }
+
+  auto monitor_auto =
+      std::unique_ptr<app_labels_monitor,
+          decltype(security_manager_app_labels_monitor_finish)*>(
+              monitor, security_manager_app_labels_monitor_finish);
+
+  ret = security_manager_app_labels_monitor_process(monitor);
+  if (ret != SECURITY_MANAGER_SUCCESS) {
+    _E("Failed to process app labels monitor. error: %d", ret);
+    THROW(ret);
+  }
+
+  int fd = -1;
+  ret = security_manager_app_labels_monitor_get_fd(monitor, &fd);
+  if (ret != SECURITY_MANAGER_SUCCESS) {
+    _E("Failed to get file descriptor. error: %d", ret);
+    THROW(ret);
+  }
+
+  channel_ = std::make_unique<IOChannel>(fd, IOChannel::IOCondition::IO_IN,
+      this);
+  channel_->SetCloseOnDestroy(false);
+  handle_ = monitor_auto.release();
+}
+
+AppLabelsMonitor::~AppLabelsMonitor() {
+  if (handle_ != nullptr)
+    security_manager_app_labels_monitor_finish(handle_);
+}
+
+void AppLabelsMonitor::SetEventListener(IEvent* listener) {
+  listener_ = listener;
+}
+
+void AppLabelsMonitor::OnIOEventReceived(int fd, int condition) {
+  _D("fd(%d), condition(%d)", fd, condition);
+  Log::Print("[APP_LABELS]", "fd(%d), condition(%d)", fd, condition);
+  security_manager_app_labels_monitor_process(handle_);
+
+  if (listener_ != nullptr)
+    listener_->OnAppLabelsChanged();
+}
+
+}  // namespace launchpad
diff --git a/src/launchpad-process-pool/app_labels_monitor.hh b/src/launchpad-process-pool/app_labels_monitor.hh
new file mode 100644 (file)
index 0000000..cf085ea
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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 LAUNCHPAD_PROCESS_POOL_APP_LABELS_MONITOR_HH_
+#define LAUNCHPAD_PROCESS_POOL_APP_LABELS_MONITOR_HH_
+
+#include <security-manager.h>
+
+#include <memory>
+
+#include <io_channel.hh>
+
+namespace launchpad {
+
+class AppLabelsMonitor : public IOChannel::IEvent {
+ public:
+  class IEvent {
+   public:
+    virtual ~IEvent() = default;
+    virtual void OnAppLabelsChanged() = 0;
+  };
+
+  AppLabelsMonitor();
+  ~AppLabelsMonitor();
+  AppLabelsMonitor(const AppLabelsMonitor&) = delete;
+  AppLabelsMonitor& operator=(const AppLabelsMonitor&) = delete;
+
+  void SetEventListener(IEvent* listener);
+
+ private:
+  void OnIOEventReceived(int fd, int condition) override;
+
+ private:
+  std::unique_ptr<IOChannel> channel_;
+  app_labels_monitor* handle_ = nullptr;
+  IEvent* listener_ = nullptr;
+};
+
+}  // namespace launchpad
+
+#endif  // LAUNCHPAD_PROCESS_POOL_APP_LABELS_MONITOR_HH_
index 336cfe2..a8ebde2 100644 (file)
@@ -41,6 +41,7 @@
 #include <algorithm>
 #include <string>
 
+#include <exception.hh>
 #include <procfs.hh>
 
 #include "launchpad-process-pool/launchpad_io_channel.h"
@@ -52,6 +53,7 @@
 #include "lib/common/inc/perf.h"
 
 #include "launchpad-process-pool/app_defined_loader_info_manager.hh"
+#include "launchpad-process-pool/app_labels_monitor.hh"
 #include "launchpad-process-pool/config.hh"
 #include "launchpad-process-pool/debug.hh"
 #include "launchpad-process-pool/dbus.hh"
@@ -208,6 +210,7 @@ static void HandleSigchld(pid_t pid);
 static candidate_process_context_t* __find_slot_from_loader_name(
     const char* loader_name);
 static int __remove_slot(int type, int loader_id);
+static void HandleAppLabelsChanged();
 
 namespace {
 
@@ -276,18 +279,24 @@ class AppDefinedLoaderInfoEventHandler
   }
 };
 
+class AppLabelsChangedEventHandler
+    : public launchpad::AppLabelsMonitor::IEvent {
+ private:
+  void OnAppLabelsChanged() override {
+    HandleAppLabelsChanged();
+  }
+};
+
 std::unique_ptr<launchpad::LoaderInfoManager> loader_info_manager;
 AppDefinedLoaderInfoEventHandler app_defined_loader_info_event_handler;
 std::unique_ptr<launchpad::AppDefinedLoaderInfoManager>
     app_defined_loader_info_manager;
 int user_slot_offset;
 GList* candidate_slot_list;
-app_labels_monitor* label_monitor;
 std::vector<launchpad::LauncherInfoPtr> launcher_info_list;
 GHashTable* __pid_table;
 sequencer __sequencer;
 io_channel_h __logger_channel;
-io_channel_h __label_monitor_channel;
 io_channel_h __launchpad_channel;
 int __client_fd = -1;
 std::unique_ptr<launchpad::Worker> cleaner;
@@ -295,7 +304,8 @@ SigchldHandler sigchld_handler;
 std::unique_ptr<launchpad::HWAccelerationConfig> hwacc_config;
 std::unique_ptr<launchpad::LanguageConfig> lang_config;
 std::unique_ptr<launchpad::RegionFormatConfig> region_format_config;
-
+AppLabelsChangedEventHandler app_labels_changed_event_handler;
+std::unique_ptr<launchpad::AppLabelsMonitor> label_monitor;
 
 }  // namespace
 
@@ -1589,22 +1599,10 @@ static void HandleSigchld(pid_t pid) {
   }
 }
 
-static bool __handle_label_monitor(int fd, io_condition_e cond, void* data) {
-  candidate_process_context_t* cpc;
+static void HandleAppLabelsChanged() {
   GList* iter = candidate_slot_list;
-
-  if (cond & (IO_ERR | IO_HUP | IO_NVAL)) {
-    _E("fd(%d), io_condition(%d)", fd, cond);
-    abort();
-    return false;
-  }
-
-  _D("fd(%d) condition(%d)", fd, cond);
-  launchpad::Log::Print("[LABEL]", "fd(%d), condition(%d)", fd, cond);
-  security_manager_app_labels_monitor_process(label_monitor);
-
   while (iter) {
-    cpc = reinterpret_cast<candidate_process_context_t*>(iter->data);
+    auto* cpc = reinterpret_cast<candidate_process_context_t*>(iter->data);
     if (cpc->is_hydra) {
       if (cpc->hydra_pid > 0) {
         __dispose_hydra_process(cpc);
@@ -1617,8 +1615,6 @@ static bool __handle_label_monitor(int fd, io_condition_e cond, void* data) {
 
     iter = g_list_next(iter);
   }
-
-  return true;
 }
 
 static float __interpolator(float input, int cpu_max, int cpu_min) {
@@ -2703,44 +2699,6 @@ static int __init_launchpad_fd(int argc, char** argv) {
   return 0;
 }
 
-static int __init_label_monitor_fd(void) {
-  int r;
-  int fd = -1;
-
-  auto err_handler = [&]() -> int {
-    if (fd > 0)
-      close(fd);
-
-    if (label_monitor) {
-      security_manager_app_labels_monitor_finish(label_monitor);
-      label_monitor = nullptr;
-    }
-    return -1;
-  };
-
-  r = security_manager_app_labels_monitor_init(&label_monitor);
-  if (r != SECURITY_MANAGER_SUCCESS)
-    return -1;
-
-  r = security_manager_app_labels_monitor_process(label_monitor);
-  if (r != SECURITY_MANAGER_SUCCESS)
-    return err_handler();
-
-  security_manager_app_labels_monitor_get_fd(label_monitor, &fd);
-  if (fd < 0) {
-    _E("failed to get fd");
-    return err_handler();
-  }
-
-  auto cond = IO_IN | IO_PRI | IO_HUP | IO_ERR | IO_NVAL;
-  __label_monitor_channel = _io_channel_create(
-      fd, static_cast<io_condition_e>(cond), __handle_label_monitor, nullptr);
-  if (!__label_monitor_channel)
-    return err_handler();
-
-  return 0;
-}
-
 static int __verify_loader_caps(const char* loader) {
   cap_t cap_d;
   cap_flag_value_t eff_state;
@@ -2960,9 +2918,12 @@ static int __before_loop(int argc, char** argv) {
     return -1;
   }
 
-  ret = __init_label_monitor_fd();
-  if (ret != 0)
-    _W("Failed to initialize label monitor");
+  try {
+    label_monitor.reset(new launchpad::AppLabelsMonitor());
+    label_monitor->SetEventListener(&app_labels_changed_event_handler);
+  } catch (const launchpad::Exception& e) {
+    _W("Failed to initialize label monitor. error: %s", e.what());
+  }
 
   MemoryManager::GetInst().Init();
   __add_default_slots();
@@ -3019,12 +2980,7 @@ static void __after_loop(void) {
   launchpad::Debug::GetInst().Dispose();
   launcher_info_list.clear();
   app_defined_loader_info_manager.reset();
-
-  if (__label_monitor_channel)
-    _io_channel_destroy(__label_monitor_channel);
-
-  if (label_monitor)
-    security_manager_app_labels_monitor_finish(label_monitor);
+  label_monitor.reset();
 
   if (__logger_channel)
     _io_channel_destroy(__logger_channel);