#include <gio/gio.h>
#include <glib-unix.h>
#include <glib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
#include <stdexcept>
#include <utility>
}
}
- 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)