Skip to content

Commit

Permalink
doveadm service stop: Code cleanup - Prepare for other service commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Oct 20, 2016
1 parent 7fa3afc commit ae1884e
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/doveadm/doveadm-master.c
Expand Up @@ -87,46 +87,54 @@ static void cmd_reload(int argc ATTR_UNUSED, char *argv[] ATTR_UNUSED)
doveadm_master_send_signal(SIGHUP);
}

static void cmd_service_stop(int argc, char *argv[])
static struct istream *master_service_send_cmd(const char *cmd)
{
const char *path, *line;
int fd;

if (argc == 1)
help_ver2(&doveadm_cmd_service_stop_ver2);
const char *path;

path = t_strconcat(doveadm_settings->base_dir, "/master", NULL);
fd = net_connect_unix(path);
int fd = net_connect_unix(path);
if (fd == -1)
i_fatal("net_connect_unix(%s) failed: %m", path);
net_set_nonblock(fd, FALSE);

const char *str =
t_strdup_printf("VERSION\tmaster-client\t1\t0\n%s\n", cmd);
if (write_full(fd, str, strlen(str)) < 0)
i_error("write(%s) failed: %m", path);
return i_stream_create_fd_autoclose(&fd, IO_BLOCK_SIZE);
}

static void cmd_service_stop(int argc, char *argv[])
{
const char *line;

if (argc == 1)
help_ver2(&doveadm_cmd_service_stop_ver2);

string_t *cmd = t_str_new(128);
str_append(cmd, "VERSION\tmaster-client\t1\t0\nSTOP");
str_append(cmd, "STOP");
for (int i = 1; i < argc; i++) {
str_append_c(cmd, '\t');
str_append(cmd, argv[i]);
}
str_append_c(cmd, '\n');
if (write_full(fd, str_data(cmd), str_len(cmd)) < 0)
i_error("write(%s) failed: %m", path);
struct istream *input = master_service_send_cmd(str_c(cmd));

alarm(5);
struct istream *input = i_stream_create_fd(fd, IO_BLOCK_SIZE, FALSE);
if (i_stream_read_next_line(input) == NULL ||
(line = i_stream_read_next_line(input)) == NULL) {
i_error("read(%s) failed: %s", path, i_stream_get_error(input));
i_error("read(%s) failed: %s", i_stream_get_name(input),
i_stream_get_error(input));
doveadm_exit_code = EX_TEMPFAIL;
} else if (line[0] == '-') {
doveadm_exit_code = DOVEADM_EX_NOTFOUND;
i_error("%s", line+1);
} else if (line[0] != '+') {
i_error("Unexpected input from %s: %s", path, line);
i_error("Unexpected input from %s: %s",
i_stream_get_name(input), line);
doveadm_exit_code = EX_TEMPFAIL;
}
alarm(0);
i_stream_destroy(&input);
i_close_fd(&fd);
}

struct doveadm_cmd_ver2 doveadm_cmd_stop_ver2 = {
Expand Down

0 comments on commit ae1884e

Please sign in to comment.