changeset 22507:633b90217c62

doveadm director: Improve logging unexpected disconnections from director socket Previously it just logged "failed", which wasn't very understandable.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 14 Sep 2017 12:42:13 +0300
parents 3afbfedcdd31
children d89f498fc00c
files src/doveadm/doveadm-director.c
diffstat 1 files changed, 35 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/doveadm-director.c	Tue Aug 22 16:32:32 2017 +0300
+++ b/src/doveadm/doveadm-director.c	Thu Sep 14 12:42:13 2017 +0300
@@ -136,6 +136,17 @@
 	return ctx;
 }
 
+static void director_disconnected(struct director_context *ctx)
+{
+	i_assert(ctx->input->eof);
+	if (ctx->input->stream_errno != 0) {
+		i_error("read(%s) failed: %s", ctx->socket_path,
+			i_stream_get_error(ctx->input));
+	} else {
+		i_error("%s unexpectedly disconnected", ctx->socket_path);
+	}
+	doveadm_exit_code = EX_TEMPFAIL;
+}
 
 static void
 cmd_director_status_user(struct director_context *ctx)
@@ -147,8 +158,7 @@
 					   ctx->tag != NULL ? ctx->tag : ""));
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL) {
-		i_error("Lookup failed");
-		doveadm_exit_code = EX_TEMPFAIL;
+		director_disconnected(ctx);
 		return;
 	}
 
@@ -228,10 +238,8 @@
 			}
 		} T_END;
 	}
-	if (line == NULL) {
-		i_error("Director disconnected unexpectedly");
-		doveadm_exit_code = EX_TEMPFAIL;
-	}
+	if (line == NULL)
+		director_disconnected(ctx);
 	director_disconnect(ctx);
 }
 
@@ -417,10 +425,8 @@
 			}
 		} T_END;
 	}
-	if (line == NULL) {
-		i_error("Director disconnected unexpectedly");
-		doveadm_exit_code = EX_TEMPFAIL;
-	}
+	if (line == NULL)
+		director_disconnected(ctx);
 deinit:
 	director_disconnect(ctx);
 	hash_table_destroy(&users);
@@ -484,9 +490,10 @@
 	}
 	for (i = 0; i < ips_count; i++) {
 		line = i_stream_read_next_line(ctx->input);
-		if (line == NULL || strcmp(line, "OK") != 0) {
+		if (line == NULL)
+			director_disconnected(ctx);
+		else if (strcmp(line, "OK") != 0) {
 			i_error("%s: %s\n", net_ip2addr(&ips[i]),
-				line == NULL ? "failed" :
 				strcmp(line, "NOTFOUND") == 0 ?
 				"doesn't exist" : line);
 			doveadm_exit_code = EX_TEMPFAIL;
@@ -538,9 +545,10 @@
 				net_ip2addr(&ips[i]));
 			if (doveadm_exit_code == 0)
 				doveadm_exit_code = DOVEADM_EX_NOTFOUND;
-		} else if (line == NULL || strcmp(line, "OK") != 0) {
-			i_error("%s: %s\n", net_ip2addr(&ips[i]),
-				line == NULL ? "failed" : line);
+		} else if (line == NULL) {
+			director_disconnected(ctx);
+		} else if (strcmp(line, "OK") != 0) {
+			i_error("%s: %s\n", net_ip2addr(&ips[i]), line);
 			doveadm_exit_code = EX_TEMPFAIL;
 		} else if (doveadm_verbose) {
 			i_info("%s: %s\n", net_ip2addr(&ips[i]), success_result);
@@ -588,8 +596,7 @@
 		"USER-MOVE\t%u\t%s\n", user_hash, ip_str));
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL) {
-		i_error("failed");
-		doveadm_exit_code = EX_TEMPFAIL;
+		director_disconnected(ctx);
 	} else if (strcmp(line, "OK") == 0) {
 		if (doveadm_verbose)
 			i_info("User hash %u moved to %s\n", user_hash, ip_str);
@@ -634,8 +641,7 @@
 
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL) {
-		i_error("failed");
-		doveadm_exit_code = EX_TEMPFAIL;
+		director_disconnected(ctx);
 	} else if (strcmp(line, "OK") == 0) {
 		if (doveadm_verbose)
 			i_info("User %s kicked", ctx->user);
@@ -662,8 +668,7 @@
 
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL) {
-		i_error("failed");
-		doveadm_exit_code = EX_TEMPFAIL;
+		director_disconnected(ctx);
 	} else if (strcmp(line, "OK") != 0) {
 		i_error("failed: %s", line);
 		doveadm_exit_code = EX_TEMPFAIL;
@@ -722,9 +727,10 @@
 				net_ip2addr(&ips[i]));
 			if (doveadm_exit_code == 0)
 				doveadm_exit_code = DOVEADM_EX_NOTFOUND;
-		} else if (line == NULL || strcmp(line, "OK") != 0) {
-			i_warning("%s: %s", net_ip2addr(&ips[i]),
-				line == NULL ? "failed" : line);
+		} else if (line == NULL) {
+			director_disconnected(ctx);
+		} else if (strcmp(line, "OK") != 0) {
+			i_warning("%s: %s", net_ip2addr(&ips[i]), line);
 			doveadm_exit_code = EX_TEMPFAIL;
 		} else if (doveadm_verbose) {
 			i_info("%s: flushed", net_ip2addr(&ips[i]));
@@ -782,10 +788,8 @@
 		doveadm_print(line);
 		doveadm_print("");
 	}
-	if (line == NULL) {
-		i_error("Director disconnected unexpectedly");
-		doveadm_exit_code = EX_TEMPFAIL;
-	}
+	if (line == NULL)
+		director_disconnected(ctx);
 	director_disconnect(ctx);
 }
 
@@ -796,8 +800,7 @@
 
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL) {
-		i_error("Director disconnected unexpectedly");
-		doveadm_exit_code = EX_TEMPFAIL;
+		director_disconnected(ctx);
 	} else if (strcmp(line, "NOTFOUND") == 0) {
 		i_error("Not found");
 		doveadm_exit_code = DOVEADM_EX_NOTFOUND;
@@ -889,10 +892,8 @@
 			}
 		} T_END;
 	}
-	if (line == NULL) {
-		i_error("Director disconnected unexpectedly");
-		doveadm_exit_code = EX_TEMPFAIL;
-	}
+	if (line == NULL)
+		director_disconnected(ctx);
 	director_disconnect(ctx);
 }