Initial support for systemd (untested)
authorRick van Rein <rick@openfortress.nl>
Sat, 12 Mar 2016 09:01:15 +0000 (09:01 +0000)
committerRick van Rein <rick@openfortress.nl>
Sat, 12 Mar 2016 09:01:15 +0000 (09:01 +0000)
etc/systemd/tlspool.service [new file with mode: 0644]
etc/systemd/tlspool.socket [new file with mode: 0644]
include/tlspool/internal.h
src/Makefile
src/config.c
src/daemon.c

diff --git a/etc/systemd/tlspool.service b/etc/systemd/tlspool.service
new file mode 100644 (file)
index 0000000..dfa56ec
--- /dev/null
@@ -0,0 +1,2 @@
+[Service]
+ExecStart=/usr/local/bin/tlspool -c /usr/local/etc/tlspool.conf
diff --git a/etc/systemd/tlspool.socket b/etc/systemd/tlspool.socket
new file mode 100644 (file)
index 0000000..8398ffd
--- /dev/null
@@ -0,0 +1,8 @@
+[Socket]
+ListenStream=/var/run/tlspool.sock
+SocketUser=tlspool
+SocketGroup=tlspool
+SocketMode=0666
+
+[Install]
+WantedBy=sockets.target
index b0c608f..4cc415a 100644 (file)
@@ -5,6 +5,7 @@
 #define TLSPOOL_INTERNAL_H
 
 #include <stdint.h>
+#include <stdbool.h>
 
 #include <tlspool/commands.h>
 
index 82c27ef..88a3b17 100644 (file)
@@ -10,6 +10,11 @@ LDFLAGS =
 
 LIBS = $(GNUTLS_LIBS) $(P11KIT_LIBS) $(BDB_LIBS) $(TASN1_LIBS) -lpthread
 
+ifdef WITH_SYSTEMD
+CFLAGS += -DHAVE_SYSTEMD
+LIBS   += -lsystemd-daemon
+endif
+
 GNUTLS_CFLAGS = $(shell pkg-config --cflags gnutls)
 GNUTLS_LIBS   = $(shell pkg-config --libs   gnutls)
 P11KIT_CFLAGS = $(shell pkg-config --cflags p11-kit-1)
index 40db2e0..c3b27db 100644 (file)
 
 //NOTYET// #include <ldap.h>
 
+#ifdef HAVE_SYSTEMD
+#  include <systemd/sd-daemon.h>
+#endif
+
 #include <tlspool/internal.h>
 
 //NOTYET// #include <libmemcached/memcached.h>
@@ -371,6 +375,7 @@ void cfg_socketname (char *item, int itemno, char *value) {
        struct sockaddr_un sun;
        int sox;
 #ifndef CONFIG_PARSE_ONLY
+#ifndef HAVE_SYSTEMD
        uid_t me = getuid ();
        gid_t my = getgid ();
        if (strlen (value) + 1 > sizeof (sun.sun_path)) {
@@ -428,6 +433,13 @@ void cfg_socketname (char *item, int itemno, char *value) {
                        exit (1);
                }
        }
+#else  /* HAVE_SYSTEMD */
+       if (sd_listen_fds (0) != 1) {
+               fprintf (stderr, "TLS Pool should have received one socket\n");
+               exit (1);
+       }
+       sox = SD_LISTEN_FDS_START + 0;
+#endif /* HAVE_SYSTEMD */
        register_server_socket (sox);
 #endif
 }
index 11b9ba7..25cb1d9 100644 (file)
@@ -61,7 +61,11 @@ int main (int argc, char *argv []) {
        //TODO// setup syslogging
 
        //UNDO// sigset_t sigblockmask;
+#ifdef HAVE_SYSTEMD
+       int pid = -2;   /* Skip setsid() but otherwise proceed */
+#else
        int pid = fork ();
+#endif
        switch (pid) {
        case -1:
                perror ("Failed to fork daemon");
@@ -69,6 +73,7 @@ int main (int argc, char *argv []) {
        case 0:
                // Detach from the startup session
                setsid ();
+       case -2:
                //TODO// close the common fd's 0/1/2
                // Setup a SIGHUP handler to gracefully stop service
                if (sigaction (SIGHUP, &hupaction, NULL) != 0) {