]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm service stop: Code cleanup - Prepare for other service commands.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 20 Oct 2016 10:10:56 +0000 (13:10 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 20 Oct 2016 20:04:40 +0000 (23:04 +0300)
src/doveadm/doveadm-master.c

index 9d644cc78b9b07901d71757ed276c0ff5cdb2183..ba3a1565e3e22a8f88193a82f09cc022391cfb5d 100644 (file)
@@ -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 = {