[dovecot-cvs] dovecot: If Dovecot is already running, complain about it instea...

dovecot at dovecot.org dovecot at dovecot.org
Wed May 23 23:15:25 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/b118198fbfa3
changeset: 5659:b118198fbfa3
user:      Timo Sirainen <tss at iki.fi>
date:      Wed May 23 23:15:17 2007 +0300
description:
If Dovecot is already running, complain about it instead of wiping out
login_dir and causing the already running Dovecot to break.

diffstat:

1 file changed, 51 insertions(+)
src/master/master-settings.c |   51 ++++++++++++++++++++++++++++++++++++++++++

diffs (75 lines):

diff -r 115d13bb1a47 -r b118198fbfa3 src/master/master-settings.c
--- a/src/master/master-settings.c	Wed May 23 19:07:12 2007 +0300
+++ b/src/master/master-settings.c	Wed May 23 23:15:17 2007 +0300
@@ -14,6 +14,7 @@
 
 #include <stdio.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -824,6 +825,46 @@ static bool settings_fix(struct settings
 	return nofixes ? TRUE : settings_do_fixes(set);
 }
 
+static int pid_file_is_running(const char *path)
+{
+	char buf[32];
+	int fd;
+	ssize_t ret;
+
+	fd = open(path, O_RDONLY);
+	if (fd == -1) {
+		if (errno == ENOENT)
+			return 0;
+		i_error("open(%s) failed: %m", path);
+		return -1;
+	}
+
+	ret = read(fd, buf, sizeof(buf));
+	if (ret <= 0) {
+		if (ret == 0)
+			i_error("Empty PID file in %s, overriding", path);
+		else
+			i_error("read(%s) failed: %m", path);
+	} else {
+		pid_t pid;
+
+		if (buf[ret-1] == '\n')
+			ret--;
+		buf[ret] = '\0';
+		pid = atoi(buf);
+		if (pid == getpid() || (kill(pid, 0) < 0 && errno == ESRCH)) {
+			/* doesn't exist */
+			ret = 0;
+		} else {
+			i_error("Dovecot is already running with PID %s "
+				"(read from %s)", buf, path);
+			ret = 1;
+		}
+	}
+	(void)close(fd);
+	return ret;
+}
+
 static struct auth_settings *
 auth_settings_new(struct server_settings *server, const char *name)
 {
@@ -1295,6 +1336,16 @@ bool master_settings_read(const char *pa
 	if (ctx.root->next != NULL)
 		ctx.root = ctx.root->next;
 
+	if (!nochecks && !nofixes) {
+		ctx.root->defaults = settings_is_active(ctx.root->imap) ?
+			ctx.root->imap : ctx.root->pop3;
+
+		path = t_strconcat(ctx.root->defaults->base_dir,
+				   "/master.pid", NULL);
+		if (pid_file_is_running(path) != 0)
+			return FALSE;
+	}
+
 	prev = NULL;
 	for (server = ctx.root; server != NULL; server = server->next) {
 		if ((*server->imap->protocols == '\0' ||


More information about the dovecot-cvs mailing list