keyd: keyd.o $(CORE_OBJS) keydb_$(DBTYPE).o
$(CC) $(LDFLAGS) $(PROGS_LDFLAGS_EXTRA) \
- -o keyd keyd.o $(CORE_OBJS) keydb_$(DBTYPE).o $(LIBS)
+ -o keyd keyd.o $(CORE_OBJS) keydb_$(DBTYPE).o $(LIBS) \
+ @SYSTEMD_LIBS@
keydctl: keydctl.o onak-conf.o ll.o log.o
$(CC) $(LDFLAGS) $(PROGS_LDFLAGS_EXTRA) \
m4_include([m4/ax_lib_nettle.m4])
AX_LIB_NETTLE(auto)
+AC_CHECK_HEADER([systemd/sd-daemon.h], [
+ AC_CHECK_LIB([systemd-daemon], [sd_listen_fds],
+ [AC_DEFINE([HAVE_SYSTEMD], [1], [systemd-daemon is available])
+ AC_SUBST([SYSTEMD_LIBS], [-lsystemd-daemon])
+ ])
+])
dnl We should always have these backends available.
backends="file fs keyd"
Priority: optional
Maintainer: Jonathan McDowell <noodles@earth.li>
Uploaders: Ross Burton <ross@debian.org>
-Build-Depends: debhelper (>= 9), dh-autoreconf, libdb-dev, nettle-dev, libcurl4-gnutls-dev | libcurl-dev
+Build-Depends: debhelper (>= 9), dh-autoreconf, libdb-dev, nettle-dev, libcurl4-gnutls-dev | libcurl-dev, libsystemd-daemon-dev
Standards-Version: 3.9.4.0
Homepage: http://www.earth.li/projectpurple/progs/onak.html
Vcs-Browser: http://the.earth.li/gitweb/?p=onak.git;a=summary
#include <time.h>
#include <unistd.h>
+#include "config.h"
+
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
+
#include "charfuncs.h"
#include "cleanup.h"
#include "keyd.h"
/* Maximum number of clients we're prepared to accept at once */
#define MAX_CLIENTS 16
+#ifdef HAVE_SYSTEMD
+static bool using_socket_activation = false;
+#endif
+
static struct keyd_stats *stats;
static void daemonize(void)
struct sockaddr_un sock;
int fd = -1;
int ret = -1;
+#ifdef HAVE_SYSTEMD
+ int n;
- fd = socket(PF_UNIX, SOCK_STREAM, 0);
- if (fd != -1) {
- ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
- }
+ n = sd_listen_fds(0);
+ if (n > 1) {
+ logthing(LOGTHING_ERROR,
+ "Too many file descriptors received from systemd.");
+ } else if (n == 1) {
+ fd = SD_LISTEN_FDS_START + 0;
+ if (sd_is_socket_unix(fd, SOCK_STREAM, 1, NULL, 0) <= 0) {
+ logthing(LOGTHING_ERROR,
+ "systemd passed an invalid socket.");
+ fd = -1;
+ }
+ using_socket_activation = true;
+ } else {
+#endif
+ fd = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (fd != -1) {
+ ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ }
- if (ret != -1) {
- sock.sun_family = AF_UNIX;
- strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
- unlink(sockname);
- ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
- }
+ if (ret != -1) {
+ sock.sun_family = AF_UNIX;
+ strncpy(sock.sun_path, sockname,
+ sizeof(sock.sun_path) - 1);
+ unlink(sockname);
+ ret = bind(fd, (struct sockaddr *) &sock,
+ sizeof(sock));
+ }
- if (ret != -1) {
- ret = listen(fd, 5);
- if (ret == -1) {
- close(fd);
- fd = -1;
+ if (ret != -1) {
+ ret = listen(fd, 5);
+ if (ret == -1) {
+ close(fd);
+ fd = -1;
+ }
}
+#ifdef HAVE_SYSTEMD
}
+#endif
return fd;
}
}
}
dbctx->cleanupdb(dbctx);
- sock_close(fd);
- unlink(sockname);
+#ifdef HAVE_SYSTEMD
+ if (!using_socket_activation) {
+#endif
+ sock_close(fd);
+ unlink(sockname);
+#ifdef HAVE_SYSTEMD
+ }
+#endif
}
free(stats);