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

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


details:   http://hg.dovecot.org/dovecot-1.0/rev/325438d789fb
changeset: 5286:325438d789fb
user:      Timo Sirainen <tss at iki.fi>
date:      Wed May 23 23:15:16 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 da07b096e1b5 -r 325438d789fb src/master/master-settings.c
--- a/src/master/master-settings.c	Tue May 22 20:30:01 2007 +0300
+++ b/src/master/master-settings.c	Wed May 23 23:15:16 2007 +0300
@@ -14,6 +14,7 @@
 
 #include <stdio.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -821,6 +822,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)
 {
@@ -1296,6 +1337,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