dovecot-2.0: doveadm: Added stop and reload commands.
dovecot at dovecot.org
dovecot at dovecot.org
Fri May 28 15:11:22 EEST 2010
details: http://hg.dovecot.org/dovecot-2.0/rev/c740a50ab676
changeset: 11414:c740a50ab676
user: Timo Sirainen <tss at iki.fi>
date: Fri May 28 13:03:49 2010 +0100
description:
doveadm: Added stop and reload commands.
diffstat:
src/doveadm/Makefile.am | 1 +
src/doveadm/doveadm-master.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++
src/doveadm/doveadm.c | 2 +
src/doveadm/doveadm.h | 2 +
4 files changed, 98 insertions(+), 0 deletions(-)
diffs (132 lines):
diff -r b6c027d9631f -r c740a50ab676 src/doveadm/Makefile.am
--- a/src/doveadm/Makefile.am Fri May 28 12:31:22 2010 +0100
+++ b/src/doveadm/Makefile.am Fri May 28 13:03:49 2010 +0100
@@ -52,6 +52,7 @@
doveadm-dump-mailboxlog.c \
doveadm-dump-thread.c \
doveadm-kick.c \
+ doveadm-master.c \
doveadm-mail.c \
doveadm-mail-altmove.c \
doveadm-mail-expunge.c \
diff -r b6c027d9631f -r c740a50ab676 src/doveadm/doveadm-master.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-master.c Fri May 28 13:03:49 2010 +0100
@@ -0,0 +1,93 @@
+/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "doveadm.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#define MASTER_PID_FILE_NAME "master.pid"
+
+static bool pid_file_read(const char *path, pid_t *pid_r)
+{
+ char buf[32];
+ int fd;
+ ssize_t ret;
+ bool found;
+
+ fd = open(path, O_RDONLY);
+ if (fd == -1) {
+ if (errno == ENOENT)
+ return FALSE;
+ i_fatal("open(%s) failed: %m", path);
+ }
+
+ ret = read(fd, buf, sizeof(buf));
+ if (ret <= 0) {
+ if (ret == 0)
+ i_error("Empty PID file in %s", path);
+ else
+ i_fatal("read(%s) failed: %m", path);
+ found = FALSE;
+ } else {
+ if (buf[ret-1] == '\n')
+ ret--;
+ buf[ret] = '\0';
+ if (str_to_pid(buf, pid_r) < 0)
+ found = FALSE;
+ else {
+ found = !(*pid_r == getpid() ||
+ (kill(*pid_r, 0) < 0 && errno == ESRCH));
+ }
+ }
+ (void)close(fd);
+ return found;
+}
+
+static void send_master_signal(int signo)
+{
+ const char *pidfile_path;
+ unsigned int i;
+ pid_t pid;
+
+ pidfile_path = t_strconcat(doveadm_settings->base_dir,
+ "/"MASTER_PID_FILE_NAME, NULL);
+
+ if (!pid_file_read(pidfile_path, &pid))
+ i_fatal("Dovecot is not running (read from %s)", pidfile_path);
+
+ if (kill(pid, signo) < 0)
+ i_fatal("kill(%s, %d) failed: %m", dec2str(pid), signo);
+
+ if (signo == SIGTERM) {
+ /* wait for a while for the process to die */
+ usleep(1000);
+ for (i = 0; i < 30; i++) {
+ if (kill(pid, 0) < 0) {
+ if (errno != ESRCH)
+ i_error("kill() failed: %m");
+ break;
+ }
+ usleep(100000);
+ }
+ }
+}
+
+static void cmd_stop(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
+{
+ send_master_signal(SIGTERM);
+}
+
+static void cmd_reload(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
+{
+ send_master_signal(SIGHUP);
+}
+
+struct doveadm_cmd doveadm_cmd_stop = {
+ cmd_stop, "stop", "", NULL
+};
+
+struct doveadm_cmd doveadm_cmd_reload = {
+ cmd_reload, "reload", "", NULL
+};
diff -r b6c027d9631f -r c740a50ab676 src/doveadm/doveadm.c
--- a/src/doveadm/doveadm.c Fri May 28 12:31:22 2010 +0100
+++ b/src/doveadm/doveadm.c Fri May 28 13:03:49 2010 +0100
@@ -176,6 +176,8 @@
static struct doveadm_cmd *doveadm_commands[] = {
&doveadm_cmd_help,
+ &doveadm_cmd_stop,
+ &doveadm_cmd_reload,
&doveadm_cmd_auth,
&doveadm_cmd_user,
&doveadm_cmd_dump,
diff -r b6c027d9631f -r c740a50ab676 src/doveadm/doveadm.h
--- a/src/doveadm/doveadm.h Fri May 28 12:31:22 2010 +0100
+++ b/src/doveadm/doveadm.h Fri May 28 13:03:49 2010 +0100
@@ -14,6 +14,8 @@
const char *long_usage;
};
+extern struct doveadm_cmd doveadm_cmd_stop;
+extern struct doveadm_cmd doveadm_cmd_reload;
extern struct doveadm_cmd doveadm_cmd_auth;
extern struct doveadm_cmd doveadm_cmd_user;
extern struct doveadm_cmd doveadm_cmd_dump;
More information about the dovecot-cvs
mailing list