dovecot-2.0: Added support for systemd.

dovecot at dovecot.org dovecot at dovecot.org
Wed Nov 17 20:41:00 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/84eb4afebc95
changeset: 12439:84eb4afebc95
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Nov 17 18:40:52 2010 +0000
description:
Added support for systemd.
Based on patch by Christophe Fergeau

diffstat:

 Makefile.am                     |   14 +
 configure.in                    |    9 +
 dovecot.service.in              |    8 +
 dovecot.socket                  |   15 +
 src/lib-master/master-service.c |    4 +
 src/master/Makefile.am          |   10 +-
 src/master/main.c               |    4 +
 src/master/sd-daemon.c          |  436 +++++++++++++++++++++++++++++++++++++++++++
 src/master/sd-daemon.h          |  265 ++++++++++++++++++++++++++
 src/master/service-listen.c     |   51 ++++-
 10 files changed, 809 insertions(+), 7 deletions(-)

diffs (truncated from 930 to 300 lines):

diff -r c1d8fb31c1ad -r 84eb4afebc95 Makefile.am
--- a/Makefile.am	Wed Nov 17 18:21:46 2010 +0000
+++ b/Makefile.am	Wed Nov 17 18:40:52 2010 +0000
@@ -49,6 +49,17 @@
 	-e "s|^\(dovecot_moduledir\)=|\1=$(moduledir)|" \
 	> dovecot-config
 
+if HAVE_SYSTEMD
+%.service: %.service.in
+	$(AM_V_GEN)sed -e 's, at sbindir\@,$(sbindir),g' $< > $@
+
+systemdsystemunit_DATA = \
+        dovecot.socket \
+        dovecot.service
+else
+EXTRA_DIST += dovecot.socket dovecot.service.in
+endif
+
 install-exec-hook:
 	rm $(DESTDIR)$(pkglibdir)/dovecot-config && \
 	grep -v '^LIBDOVECOT_.*_INCLUDE' dovecot-config | \
@@ -62,6 +73,9 @@
 	> $(DESTDIR)$(pkglibdir)/dovecot-config
 
 CLEANFILES = $(datafiles)
+if HAVE_SYSTEMD
+CLEANFILES += $systedmsystemunit_DATA
+endif
 
 DISTCLEANFILES = $(top_builddir)/dovecot-version.h
 
diff -r c1d8fb31c1ad -r 84eb4afebc95 configure.in
--- a/configure.in	Wed Nov 17 18:21:46 2010 +0000
+++ b/configure.in	Wed Nov 17 18:40:52 2010 +0000
@@ -2629,6 +2629,15 @@
 AC_SUBST(RUN_TEST)
 AC_SUBST(abs_top_builddir)
 
+AC_ARG_WITH([systemdsystemunitdir],
+        AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
+        [], [with_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)])
+if test "x$with_systemdsystemunitdir" != xno; then
+        AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
+        AC_DEFINE(HAVE_SYSTEMD,, Define if you want to use systemd socket activation)
+fi
+AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ])
+
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([
 Makefile
diff -r c1d8fb31c1ad -r 84eb4afebc95 dovecot.service.in
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dovecot.service.in	Wed Nov 17 18:40:52 2010 +0000
@@ -0,0 +1,8 @@
+[Unit]
+Description=Dovecot IMAP/POP3 email server
+After=local-fs.target network.target
+
+[Service]
+Type=simple
+ExecStart=@sbindir@/dovecot -F
+NonBlocking=yes
diff -r c1d8fb31c1ad -r 84eb4afebc95 dovecot.socket
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dovecot.socket	Wed Nov 17 18:40:52 2010 +0000
@@ -0,0 +1,15 @@
+[Unit]
+Description=Dovecot IMAP/POP3 email server activation socket
+
+[Socket]
+#dovecot expects separate IPv4 and IPv6 sockets
+BindIPv6Only=ipv6-only
+ListenStream=0.0.0.0:143
+ListenStream=[::]:143
+ListenStream=0.0.0.0:993
+ListenStream=[::]:993
+KeepAlive=true
+
+[Install]
+WantedBy=sockets.target
+
diff -r c1d8fb31c1ad -r 84eb4afebc95 src/lib-master/master-service.c
--- a/src/lib-master/master-service.c	Wed Nov 17 18:21:46 2010 +0000
+++ b/src/lib-master/master-service.c	Wed Nov 17 18:40:52 2010 +0000
@@ -400,6 +400,10 @@
 #ifdef DEBUG
 		"GDB",
 #endif
+#ifdef HAVE_SYSTEMD
+		"LISTEN_PID",
+		"LISTEN_FDS",
+#endif
 		NULL
 	};
 	env_clean_except(preserve_envs + (preserve_home ? 0 : 1));
diff -r c1d8fb31c1ad -r 84eb4afebc95 src/master/Makefile.am
--- a/src/master/Makefile.am	Wed Nov 17 18:21:46 2010 +0000
+++ b/src/master/Makefile.am	Wed Nov 17 18:40:52 2010 +0000
@@ -2,6 +2,11 @@
 
 sbin_PROGRAMS = dovecot
 
+if HAVE_SYSTEMD
+SYSTEMD_SOURCES = sd-daemon.c
+endif
+
+
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-settings \
@@ -27,13 +32,15 @@
 	service-monitor.c \
 	service-process.c \
 	service-process-notify.c \
-	service.c
+	service.c \
+	$(SYSTEMD_SOURCES)
 
 noinst_HEADERS = \
 	capabilities.h \
 	common.h \
 	dup2-array.h \
 	master-settings.h \
+	sd-daemon.h \
 	service-anvil.h \
 	service-listen.h \
 	service-log.h \
@@ -41,3 +48,4 @@
 	service-process.h \
 	service-process-notify.h \
 	service.h
+
diff -r c1d8fb31c1ad -r 84eb4afebc95 src/master/main.c
--- a/src/master/main.c	Wed Nov 17 18:21:46 2010 +0000
+++ b/src/master/main.c	Wed Nov 17 18:40:52 2010 +0000
@@ -601,6 +601,10 @@
 	static const char *preserve_envs[] = {
 		/* AIX depends on TZ to get the timezone correctly. */
 		"TZ",
+#ifdef HAVE_SYSTEMD
+		"LISTEN_PID",
+		"LISTEN_FDS",
+#endif
 		NULL
 	};
 	struct master_settings *set;
diff -r c1d8fb31c1ad -r 84eb4afebc95 src/master/sd-daemon.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/master/sd-daemon.c	Wed Nov 17 18:40:52 2010 +0000
@@ -0,0 +1,436 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  Copyright 2010 Lennart Poettering
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation files
+  (the "Software"), to deal in the Software without restriction,
+  including without limitation the rights to use, copy, modify, merge,
+  publish, distribute, sublicense, and/or sell copies of the Software,
+  and to permit persons to whom the Software is furnished to do so,
+  subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+  SOFTWARE.
+***/
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/fcntl.h>
+#include <netinet/in.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stddef.h>
+
+#include "sd-daemon.h"
+
+int sd_listen_fds(int unset_environment) {
+
+#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
+        return 0;
+#else
+        int r, fd;
+        const char *e;
+        char *p = NULL;
+        unsigned long l;
+
+        if (!(e = getenv("LISTEN_PID"))) {
+                r = 0;
+                goto finish;
+        }
+
+        errno = 0;
+        l = strtoul(e, &p, 10);
+
+        if (errno != 0) {
+                r = -errno;
+                goto finish;
+        }
+
+        if (!p || *p || l <= 0) {
+                r = -EINVAL;
+                goto finish;
+        }
+
+        /* Is this for us? */
+        if (getpid() != (pid_t) l) {
+                r = 0;
+                goto finish;
+        }
+
+        if (!(e = getenv("LISTEN_FDS"))) {
+                r = 0;
+                goto finish;
+        }
+
+        errno = 0;
+        l = strtoul(e, &p, 10);
+
+        if (errno != 0) {
+                r = -errno;
+                goto finish;
+        }
+
+        if (!p || *p) {
+                r = -EINVAL;
+                goto finish;
+        }
+
+        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
+                int flags;
+
+                if ((flags = fcntl(fd, F_GETFD)) < 0) {
+                        r = -errno;
+                        goto finish;
+                }
+
+                if (flags & FD_CLOEXEC)
+                        continue;
+
+                if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
+                        r = -errno;
+                        goto finish;
+                }
+        }
+
+        r = (int) l;
+
+finish:
+        if (unset_environment) {
+                unsetenv("LISTEN_PID");
+                unsetenv("LISTEN_FDS");
+        }
+
+        return r;
+#endif
+}
+
+int sd_is_fifo(int fd, const char *path) {
+        struct stat st_fd;
+
+        if (fd < 0)
+                return -EINVAL;
+
+        memset(&st_fd, 0, sizeof(st_fd));
+        if (fstat(fd, &st_fd) < 0)
+                return -errno;
+
+        if (!S_ISFIFO(st_fd.st_mode))
+                return 0;
+
+        if (path) {
+                struct stat st_path;
+
+                memset(&st_path, 0, sizeof(st_path));
+                if (stat(path, &st_path) < 0) {
+
+                        if (errno == ENOENT || errno == ENOTDIR)
+                                return 0;
+
+                        return -errno;


More information about the dovecot-cvs mailing list