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:
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);
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;
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
libxml-2.0
sqlite3
dlog
+ libsystemd-daemon
)
INCLUDE_DIRECTORIES(SYSTEM ${KLAY_INCLUDE}
::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];
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++) {
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);
}
}
{
}
-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()));
EPOLLIN | EPOLLHUP | EPOLLRDHUP,
accept);
- mainloop.run();
+ mainloop.run(timeout);
}
void Service::stop()
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/socket.h>
+#include <systemd/sd-daemon.h>
#include <iostream>
}
}
-#ifdef USE_SYSTEMD_SOCKET_ACTIVATION
int Socket::createSystemdSocket(const std::string& path)
{
int n = ::sd_listen_fds(-1);
return -1;
}
-#endif
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);
}