Add socket listening logic
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 14 Apr 2025 08:40:12 +0000 (17:40 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Wed, 14 May 2025 09:02:19 +0000 (18:02 +0900)
Systemd socket activation is not essential for socket activation of the service module.

Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/activation_method/fd_monitor.cc

index 84398696705f09514efd3dbf9d569ebbd526d286..01ba28509d0b93866be6ea79335d7184e2a8c4fe 100644 (file)
@@ -5,6 +5,8 @@
 #include <gio/gio.h>
 #include <glib-unix.h>
 #include <glib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include <stdexcept>
 #include <utility>
@@ -34,9 +36,28 @@ FdMonitor::FdMonitor(std::string name,
     }
   }
 
-  if (fd == -1)
-    throw std::runtime_error("Can not find socket file descriptor(" + path +
-                             ")");
+  if (fd == -1) {
+    _W("Can not find systemd socket file descriptor(%s)", path.c_str());
+    fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (fd < 0)
+      std::runtime_error("socket failed.");
+
+    struct sockaddr_un sockad {};
+    sockad.sun_family = AF_UNIX;
+    strcpy(sockad.sun_path, path.c_str());
+    if (access(path.c_str(), F_OK) == 0)
+      unlink(path.c_str());
+
+    if (bind(fd, (struct sockaddr*)&sockad, sizeof(sockad)) < 0) {
+      close(fd);
+      std::runtime_error("bind failed.");
+    }
+
+    if (listen(fd, 128) < 0) {
+      close(fd);
+      std::runtime_error("listen failed.");
+    }
+  }
 
   unix_fd_source_id_ = g_unix_fd_add(fd, G_IO_IN, UnixFdSourceFunc, this);
   if (unix_fd_source_id_ == 0)