Fix the implementation of IOChannel class 61/316961/3
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 2 Sep 2024 03:40:46 +0000 (12:40 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 2 Sep 2024 04:17:50 +0000 (13:17 +0900)
- Use g_unix_fd_add() instead of g_io_channel_unix_fd().
- When calling the UnixFdFunc function, the function checks whether
the file descriptor is equal to the file descriptor of the IOChannel instance.
It's not equal, the function will remove the GSource.

Change-Id: I19ce6d18d11d175e33f250748de1f4a129cf7b07
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/app_labels_monitor.cc
src/launchpad-process-pool/file_monitor.cc
src/launchpad-process-pool/hydra_loader_context.cc
src/launchpad-process-pool/hydra_sigchld_event.cc
src/launchpad-process-pool/launchpad.cc
src/launchpad-process-pool/loader_context.cc
src/launchpad-process-pool/sigchld_event.cc
src/lib/launchpad-common/client_socket.cc
src/lib/launchpad-glib/io_channel.cc
src/lib/launchpad-glib/io_channel.hh

index 6d1e24119b9834853eeaf58dd0effb6e6435d56c..7382376e7a69b203f4428b1d27440a156c1ffa6f 100644 (file)
@@ -63,7 +63,6 @@ void AppLabelsMonitor::Init() {
 
   channel_ = std::make_unique<IOChannel>(fd, IOChannel::IOCondition::IO_IN,
       this);
-  channel_->SetCloseOnDestroy(false);
   handle_ = monitor_auto.release();
   disposed_ = false;
 }
index 62aa3e84a6994a04ae700a9ac71c1c2adc2b5771..ee2d16704b29e60ee8a821dec6a8ee0eedef991e 100644 (file)
@@ -84,7 +84,6 @@ FileMonitor::FileMonitor(const std::string_view path,
   }
 
   channel_ = std::make_unique<IOChannel>(fd, G_IO_IN, this);
-  channel_->SetCloseOnDestroy(false);
   wd_ = wd;
   fd_ = fd;
 }
index 88bbf6c96ed421bded602d0bd7eba9e5decb3775..46bd449dc04f376ffa735398853cc411c35923da 100644 (file)
@@ -65,7 +65,6 @@ void HydraLoaderContext::Listen() {
     hydra_channel_.reset(
         new IOChannel(
             hydra_socket_->GetFd(), IOChannel::IOCondition::IO_IN, this));
-    hydra_channel_->SetCloseOnDestroy(false);
   } catch (const Exception& e) {
     _E("Exception occurs. error: %s", e.what());
     THROW(e.GetErrorCode());
@@ -149,7 +148,6 @@ void HydraLoaderContext::HandleHydraLoaderEvent() {
           new IOChannel(client_socket_->GetFd(),
             IOChannel::IOCondition::IO_IN | IOChannel::IOCondition::IO_HUP,
             this));
-      client_channel_->SetCloseOnDestroy(false);
       hydra_prepared_ = true;
       SECURE_LOGI("Type %d hydra loader was connected. pid: %d",
           GetType(), GetHydraPid());
index 4ae62e755d9f0fc233911d96596036392dedb2bd..dc987f8455552567e893ac37c07a122ccf1f55ae 100644 (file)
@@ -58,7 +58,6 @@ HydraSigchldEvent::HydraSigchldEvent(IEvent* listener)
 
   channel_.reset(new IOChannel(socket_->GetFd(), IOChannel::IOCondition::IO_IN,
       this));
-  channel_->SetCloseOnDestroy(false);
 }
 
 HydraSigchldEvent::~HydraSigchldEvent() {
index e91c60df026be91e957b0df213146eb1c1c6f9e3..c2b6f84510b6f5e5407230e2b76177eecc43d684 100644 (file)
@@ -257,7 +257,6 @@ bool Launchpad::OnCreate() {
     socket_.reset(GetLaunchpadSocket());
     channel_.reset(
         new IOChannel(socket_->GetFd(), IOChannel::IOCondition::IO_IN, this));
-    channel_->SetCloseOnDestroy(false);
   } catch (const Exception& e) {
     _E("Exception occurs. error: %s", e.what());
     return false;
index de99fa16bf107f8dd7fd56e730c15db9d5c163ab..9187988560d0d6ec628bbc93a5701f251f75c0e5 100644 (file)
@@ -182,7 +182,6 @@ void LoaderContext::Listen() {
 
     server_channel_.reset(new IOChannel(server_socket_->GetFd(),
         IOChannel::IOCondition::IO_IN, this));
-    server_channel_->SetCloseOnDestroy(false);
   } catch (const Exception& e) {
     _E("Exception occurs. error: %s", e.what());
     THROW(e.GetErrorCode());
@@ -643,7 +642,6 @@ void LoaderContext::HandleLoaderEvent() {
               client_socket_->GetFd(),
               IOChannel::IOCondition::IO_IN | IOChannel::IOCondition::IO_HUP,
               this));
-      client_channel_->SetCloseOnDestroy(false);
 
       if (IsHydraMode())
         SetPid(peer_cred->GetPid());
@@ -673,13 +671,10 @@ void LoaderContext::HandleLoaderClientEvent(int condition) {
     auto peer_cred = PeerCredentials::Get(client_socket_->GetFd());
     if (!peer_cred) return;
 
-    if (peer_cred->GetPid() == pid_) {
-      SECURE_LOGE("Type %d loader was disconnected. pid: %d", GetType(), pid_);
-      Dispose();
-      Prepare();
-    } else {
-      SECURE_LOGE("pid(%d) was disconnected", peer_cred->GetPid());
-    }
+    SECURE_LOGE("Type %d loader was disconnected. pid: %d, peer_cred: %d",
+        GetType(), pid_, peer_cred->GetPid());
+    Dispose();
+    Prepare();
   }
 }
 
index 052398152811468b4e7acc31e4dab160d05e0bc5..ac496b8e838e5ea4662b98634a511f5f8499f53d 100644 (file)
@@ -30,7 +30,6 @@ SigchldEvent::SigchldEvent(int sfd, IEvent* listener)
       channel_(new IOChannel(sfd, IOChannel::IOCondition::IO_IN, this)),
       listener_(listener) {
   _W("SigchldEvent() ctor. sfd: %d", sfd);
-  channel_->SetCloseOnDestroy(false);
 }
 
 SigchldEvent::~SigchldEvent() {
index ca5bbb05929f8352784edb58176a59e28a8a24c5..21c0f70f1197db28764ca2575b0bbe0cae369be8 100644 (file)
@@ -40,9 +40,12 @@ ClientSocket::ClientSocket() {
     _E("socket() is failed. errno(%d)", errno);
     THROW(error);
   }
+  _W("fd(%d)", fd_);
 }
 
-ClientSocket::ClientSocket(int fd) : fd_(fd) {}
+ClientSocket::ClientSocket(int fd) : fd_(fd) {
+  _W("fd(%d)", fd_);
+}
 
 ClientSocket::~ClientSocket() {
   Close();
@@ -50,6 +53,7 @@ ClientSocket::~ClientSocket() {
 
 void ClientSocket::Close() {
   if (fd_ > -1) {
+    _W("fd(%d)", fd_);
     close(fd_);
     fd_ = -1;
   }
index 8ac0531b98733affba6cd959f1f068a5b3f669df..9ff02f00718aef5f8e883f39405d67b0c1539e50 100644 (file)
@@ -26,40 +26,27 @@ namespace launchpad {
 
 IOChannel::IOChannel(int fd, int condition, IOChannel::IEvent* listener)
     : fd_(fd), listener_(listener) {
-  auto channel = g_io_channel_unix_new(fd_);
-  if (channel == nullptr) {
-    _E("g_io_channel_unix_new() is failed");
-    THROW(-ENOMEM);
-  }
-  auto channel_auto =
-      std::unique_ptr<GIOChannel, decltype(g_io_channel_unref)*>(
-          channel, g_io_channel_unref);
-
-  source_id_ = g_io_add_watch(channel, static_cast<GIOCondition>(condition),
-      IOEventCb, this);
+  source_id_ = g_unix_fd_add(fd, static_cast<GIOCondition>(condition),
+      UnixFdFunc, this);
   if (source_id_ == 0) {
-    _E("g_io_add_watch() is failed");
+    _E("g_unix_fd_add() is failed");
     THROW(-ENOMEM);
   }
-
-  channel_ = channel_auto.release();
 }
 
 IOChannel::~IOChannel() {
   if (source_id_ != 0)
     g_source_remove(source_id_);
-
-  if (channel_ != nullptr)
-    g_io_channel_unref(channel_);
 }
 
-void IOChannel::SetCloseOnDestroy(bool do_close) {
-  g_io_channel_set_close_on_unref(channel_, do_close);
-}
-
-gboolean IOChannel::IOEventCb(GIOChannel* channel, GIOCondition condition,
+gboolean IOChannel::UnixFdFunc(gint fd, GIOCondition condition,
     gpointer user_data) {
   auto* io_channel = static_cast<IOChannel*>(user_data);
+  if (io_channel->fd_ != fd) {
+    _E("fd(%d) is not equal to fd(%d) of io channel", fd, io_channel->fd_);
+    return G_SOURCE_REMOVE;
+  }
+
   auto* listener = io_channel->listener_;
   if (listener != nullptr)
     listener->OnIOEventReceived(io_channel->fd_, static_cast<int>(condition));
index 0936754140ec1f0575a975d36ea0b03be39d7ad9..0d891d6a3a19f1c90d3cad8c8063ca72b78cec6c 100644 (file)
@@ -17,7 +17,7 @@
 #ifndef LIB_LAUNCHPAD_GLIB_IO_CHANNEL_HH_
 #define LIB_LAUNCHPAD_GLIB_IO_CHANNEL_HH_
 
-#include <gio/gio.h>
+#include <glib-unix.h>
 
 #include <exception.hh>
 
@@ -49,16 +49,13 @@ class EXPORT_API IOChannel {
   IOChannel(const IOChannel&) = delete;
   IOChannel& operator = (const IOChannel&) = delete;
 
-  void SetCloseOnDestroy(bool do_close);
-
  private:
-  static gboolean IOEventCb(GIOChannel* channel, GIOCondition condition,
+  static gboolean UnixFdFunc(gint fd, GIOCondition condition,
       gpointer user_data);
 
  private:
   int fd_;
   IEvent* listener_ = nullptr;
-  GIOChannel* channel_ = nullptr;
   guint source_id_ = 0;
 };