doveadm-master.c revision c7713320cd35e77543f1bdc7229988a160dae322
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2010-2016 Dovecot authors, see the included COPYING file */
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#include "lib.h"
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen#include "str.h"
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen#include "istream.h"
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen#include "write-full.h"
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#include "doveadm.h"
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#include <unistd.h>
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#include <fcntl.h>
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#include <signal.h>
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen#define MASTER_PID_FILE_NAME "master.pid"
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainenstatic bool pid_file_read(const char *path, pid_t *pid_r)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen{
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen char buf[32];
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen int fd;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen ssize_t ret;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen bool found;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen fd = open(path, O_RDONLY);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (fd == -1) {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (errno == ENOENT)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen return FALSE;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_fatal("open(%s) failed: %m", path);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
2a325b952fe47346d76221d2c07a3fe02faf8800Timo Sirainen ret = read(fd, buf, sizeof(buf)-1);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (ret <= 0) {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (ret == 0)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_error("Empty PID file in %s", path);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen else
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_fatal("read(%s) failed: %m", path);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen found = FALSE;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen } else {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (buf[ret-1] == '\n')
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen ret--;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen buf[ret] = '\0';
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (str_to_pid(buf, pid_r) < 0)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen found = FALSE;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen else {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen found = !(*pid_r == getpid() ||
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen (kill(*pid_r, 0) < 0 && errno == ESRCH));
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
4307c886579381dbb1897ea1388ae6978c96f560Timo Sirainen i_close_fd(&fd);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen return found;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen}
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
bf333c7645b8ddb6eedd6834db2fd908888793e1Timo Sirainenvoid doveadm_master_send_signal(int signo)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen{
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen const char *pidfile_path;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen unsigned int i;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen pid_t pid;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen pidfile_path = t_strconcat(doveadm_settings->base_dir,
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen "/"MASTER_PID_FILE_NAME, NULL);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (!pid_file_read(pidfile_path, &pid))
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_fatal("Dovecot is not running (read from %s)", pidfile_path);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (kill(pid, signo) < 0)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_fatal("kill(%s, %d) failed: %m", dec2str(pid), signo);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (signo == SIGTERM) {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen /* wait for a while for the process to die */
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen usleep(1000);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen for (i = 0; i < 30; i++) {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (kill(pid, 0) < 0) {
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen if (errno != ESRCH)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen i_error("kill() failed: %m");
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen break;
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen usleep(100000);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen }
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen}
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainenstatic void cmd_stop(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen{
bf333c7645b8ddb6eedd6834db2fd908888793e1Timo Sirainen doveadm_master_send_signal(SIGTERM);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen}
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainenstatic void cmd_reload(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen{
bf333c7645b8ddb6eedd6834db2fd908888793e1Timo Sirainen doveadm_master_send_signal(SIGHUP);
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen}
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainenstatic void cmd_service_stop(int argc, char *argv[])
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen{
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen const char *path, *line;
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen int fd;
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen if (argc == 1)
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen help_ver2(&doveadm_cmd_service_stop_ver2);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen path = t_strconcat(doveadm_settings->base_dir, "/master", NULL);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen fd = net_connect_unix(path);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen if (fd == -1)
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_fatal("net_connect_unix(%s) failed: %m", path);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen net_set_nonblock(fd, FALSE);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen string_t *cmd = t_str_new(128);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen str_append(cmd, "VERSION\tmaster-client\t1\t0\nSTOP");
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen for (int i = 1; i < argc; i++) {
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen str_append_c(cmd, '\t');
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen str_append(cmd, argv[i]);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen }
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen str_append_c(cmd, '\n');
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen if (write_full(fd, str_data(cmd), str_len(cmd)) < 0)
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_error("write(%s) failed: %m", path);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen alarm(5);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen struct istream *input = i_stream_create_fd(fd, IO_BLOCK_SIZE);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen if (i_stream_read_next_line(input) == NULL ||
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen (line = i_stream_read_next_line(input)) == NULL) {
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_error("read(%s) failed: %s", path, i_stream_get_error(input));
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen doveadm_exit_code = EX_TEMPFAIL;
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen } else if (line[0] == '-') {
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen doveadm_exit_code = DOVEADM_EX_NOTFOUND;
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_error("%s", line+1);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen } else if (line[0] != '+') {
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_error("Unexpected input from %s: %s", path, line);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen doveadm_exit_code = EX_TEMPFAIL;
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen }
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen alarm(0);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_stream_destroy(&input);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen i_close_fd(&fd);
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen}
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_stop_ver2 = {
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .old_cmd = cmd_stop,
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .name = "stop",
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .usage = "",
68cc278710182485b6c09e9a9ff8db90a727f343Aki TuomiDOVEADM_CMD_PARAMS_START
68cc278710182485b6c09e9a9ff8db90a727f343Aki TuomiDOVEADM_CMD_PARAMS_END
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen};
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_reload_ver2 = {
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .old_cmd = cmd_reload,
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .name = "reload",
68cc278710182485b6c09e9a9ff8db90a727f343Aki Tuomi .usage = "",
68cc278710182485b6c09e9a9ff8db90a727f343Aki TuomiDOVEADM_CMD_PARAMS_START
68cc278710182485b6c09e9a9ff8db90a727f343Aki TuomiDOVEADM_CMD_PARAMS_END
169b1488b6eea7a968021afa4f929b2e26d75d98Timo Sirainen};
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainenstruct doveadm_cmd_ver2 doveadm_cmd_service_stop_ver2 = {
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen .old_cmd = cmd_service_stop,
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen .name = "service stop",
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen .usage = "<service> [<service> [...]]",
c7713320cd35e77543f1bdc7229988a160dae322Timo SirainenDOVEADM_CMD_PARAMS_START
c7713320cd35e77543f1bdc7229988a160dae322Timo SirainenDOVEADM_CMD_PARAM('\0', "service", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
c7713320cd35e77543f1bdc7229988a160dae322Timo SirainenDOVEADM_CMD_PARAMS_END
c7713320cd35e77543f1bdc7229988a160dae322Timo Sirainen};