Delete port path 09/309109/1
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 5 Apr 2024 00:03:07 +0000 (09:03 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 5 Apr 2024 00:03:07 +0000 (09:03 +0900)
While calling a destructor of Stub, the port path should be deleted
if the caller is a daemon or service.

Change-Id: I806d5f0aeafbcf8740dbd2d4ef52b0fa5408d4b1
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/stub-internal.cc
src/stub-internal.hh

index df135a1..400e2d3 100644 (file)
@@ -108,6 +108,11 @@ Stub::~Stub() {
 
   listener_ = nullptr;
   server_.reset();
+  if (port_path_.empty()) {
+    _W("Delete port path=%s", port_path_.c_str());
+    unlink(port_path_.c_str());
+  }
+
   freed_stubs_.insert(this);
 }
 
@@ -188,11 +193,11 @@ int Stub::CreatePort() {
   if (getenv("AUL_APPID") == nullptr) {
     std::string name = Aul::GetName(getpid());
     if (!name.empty()) {
-      std::string endpoint = Aul::GetPortPath(name, GetPortName(), getuid());
-      int fd = GetFdFromSystemd(endpoint);
+      port_path_ = Aul::GetPortPath(name, GetPortName(), getuid());
+      int fd = GetFdFromSystemd();
       if (fd > -1) return fd;
 
-      fd = CreateServerSocket(endpoint);
+      fd = CreateServerSocket();
       if (fd > -1) return fd;
     }
   }
@@ -209,10 +214,10 @@ int Stub::CreatePort() {
   return fd;
 }
 
-int Stub::GetFdFromSystemd(const std::string& endpoint) {
+int Stub::GetFdFromSystemd() {
   int fds = sd_listen_fds(0);
   for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fds; ++fd) {
-    if (sd_is_socket_unix(fd, SOCK_STREAM, 1, endpoint.c_str(), 0) > 0)
+    if (sd_is_socket_unix(fd, SOCK_STREAM, 1, port_path_.c_str(), 0) > 0)
       return fd;
   }
 
@@ -220,10 +225,10 @@ int Stub::GetFdFromSystemd(const std::string& endpoint) {
   return -1;
 }
 
-int Stub::CreateServerSocket(const std::string& endpoint) {
+int Stub::CreateServerSocket() {
   try {
     ServerSocket socket;
-    socket.Bind(endpoint);
+    socket.Bind(port_path_);
     socket.Listen(128);
     return socket.RemoveFd();
   } catch (const Exception& e) {
index 71aaefc..b1ac248 100644 (file)
@@ -107,13 +107,14 @@ class Stub {
       const std::string& instance, const std::string& port_type, int fd);
   void RemoveAcceptedPorts(std::string instance);
   std::recursive_mutex& GetMutex() const;
-  int GetFdFromSystemd(const std::string& endpoint);
-  int CreateServerSocket(const std::string& endpoint);
+  int GetFdFromSystemd();
+  int CreateServerSocket();
 
  private:
   std::shared_ptr<AccessController> access_controller_ =
       std::make_shared<AccessController>();
   std::string port_name_;
+  std::string port_path_;
   std::list<std::shared_ptr<AcceptedPort>> ports_;
   IEventListener* listener_ = nullptr;
   std::unique_ptr<Server> server_;