Support socket activation 45/145345/1
authorJaemin Ryu <jm77.ryu@samsung.com>
Tue, 22 Aug 2017 05:55:51 +0000 (14:55 +0900)
committerJaemin Ryu <jm77.ryu@samsung.com>
Tue, 22 Aug 2017 05:55:51 +0000 (14:55 +0900)
Change-Id: I1a27526ba58a48cc2c2eeb879d74df5de5b1695c
Signed-off-by: Jaemin Ryu <jm77.ryu@samsung.com>
include/klay/mainloop.h
include/klay/rmi/service.h
include/klay/rmi/socket.h
packaging/klay.spec
src/CMakeLists.txt
src/mainloop.cpp
src/rmi/service.cpp
src/rmi/socket.cpp

index b06d66b..3c3f55d 100644 (file)
@@ -41,7 +41,7 @@ public:
        void addEventSource(const int fd, const Event events, Callback&& callback);
        void removeEventSource(const int fd);
        bool dispatch(const int timeout);
-       void run();
+       void run(int timeout = -1);
        void stop();
 
 private:
index 83b68be..2cd75f0 100644 (file)
@@ -153,7 +153,7 @@ public:
        Service(const Service&) = delete;
        Service& operator=(const Service&) = delete;
 
-       void start();
+       void start(bool activation = false, int timeout = -1);
        void stop();
 
        void setAuditTrail(const AuditTrail& trail);
index 99d0e63..a222016 100644 (file)
@@ -53,15 +53,13 @@ public:
        void sendFileDescriptors(const int* fds, const size_t nr) const;
        void receiveFileDescriptors(int* fds, const size_t nr) const;
 
-       static Socket create(const std::string& path);
+       static Socket create(const std::string& path, bool activation = false);
        static Socket connect(const std::string& path);
 
 private:
        static int createRegularSocket(const std::string& path);
 
-#ifdef USE_SYSTEMD_SOCKET_ACTIVATION
        static int createSystemdSocket(const std::string& path);
-#endif
 
 private:
        int socketFd;
index 5ead7f0..fb8f85a 100755 (executable)
@@ -11,6 +11,7 @@ BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(libxml-2.0)
+BuildRequires: pkgconfig(libsystemd-daemon)
 BuildRequires: pkgconfig(libtzplatform-config)
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
index b43848e..f4d6828 100755 (executable)
@@ -67,6 +67,7 @@ PKG_CHECK_MODULES(KLAY_DEPS REQUIRED  gio-2.0
                                                                                libxml-2.0
                                                                                sqlite3
                                                                                dlog
+                                                                               libsystemd-daemon
 )
 
 INCLUDE_DIRECTORIES(SYSTEM     ${KLAY_INCLUDE}
index aea2f69..c1dd277 100644 (file)
@@ -84,7 +84,7 @@ void Mainloop::removeEventSource(const int fd)
        ::epoll_ctl(pollFd, EPOLL_CTL_DEL, fd, NULL);
 }
 
-bool Mainloop::dispatch(const int timeout)
+bool Mainloop::dispatch(int timeout)
 {
        int nfds;
        epoll_event event[MAX_EPOLL_EVENTS];
@@ -93,8 +93,8 @@ bool Mainloop::dispatch(const int timeout)
                nfds = ::epoll_wait(pollFd, event, MAX_EPOLL_EVENTS, timeout);
        } while ((nfds == -1) && (errno == EINTR));
 
-       if (nfds < 0) {
-               throw Exception(GetSystemErrorMessage());
+       if (nfds <= 0) {
+               return false;
        }
 
        for (int i = 0; i < nfds; i++) {
@@ -136,12 +136,14 @@ void Mainloop::prepare()
        addEventSource(wakeupSignal.getFd(), EPOLLIN, wakeupMainloop);
 }
 
-void Mainloop::run()
+void Mainloop::run(int timeout)
 {
+       bool done = false;
+
        prepare();
 
-       while (!stopped) {
-               dispatch(-1);
+       while (!stopped && !done) {
+               done = !dispatch(timeout);
        }
 }
 
index c9da900..c6b9188 100644 (file)
@@ -46,9 +46,9 @@ Service::~Service()
 {
 }
 
-void Service::start()
+void Service::start(bool activation, int timeout)
 {
-       Socket socket(Socket::create(address));
+       Socket socket(Socket::create(address, activation));
 
        auto accept = [&](int fd, runtime::Mainloop::Event event) {
                onNewConnection(std::make_shared<Connection>(socket.accept()));
@@ -58,7 +58,7 @@ void Service::start()
                                                        EPOLLIN | EPOLLHUP | EPOLLRDHUP,
                                                        accept);
 
-       mainloop.run();
+       mainloop.run(timeout);
 }
 
 void Service::stop()
index 049708b..c6dae17 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/un.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
+#include <systemd/sd-daemon.h>
 
 #include <iostream>
 
@@ -218,7 +219,6 @@ void Socket::receiveFileDescriptors(int* fds, const size_t nr) const
        }
 }
 
-#ifdef USE_SYSTEMD_SOCKET_ACTIVATION
 int Socket::createSystemdSocket(const std::string& path)
 {
        int n = ::sd_listen_fds(-1);
@@ -235,7 +235,6 @@ int Socket::createSystemdSocket(const std::string& path)
 
        return -1;
 }
-#endif
 
 int Socket::createRegularSocket(const std::string& path)
 {
@@ -279,18 +278,15 @@ int Socket::createRegularSocket(const std::string& path)
        return sockfd;
 }
 
-Socket Socket::create(const std::string& path)
+Socket Socket::create(const std::string& path, bool activation)
 {
-       int fd;
+       int fd = -1;
 
-#ifdef USE_SYSTEMD_SOCKET_ACTIVATION
-       fd = createSystemdSocket(path);
-       if (fd == -1) {
+       if (activation)
+               fd = createSystemdSocket(path);
+
+       if (fd == -1)
                fd = createRegularSocket(path);
-       }
-#else
-       fd = createRegularSocket(path);
-#endif
 
        return Socket(fd);
 }