Commit Diff


commit - 24232204eb43c6b12bdf7dfad526276bd914fadf
commit + d040746a37a2af87b1b4ffe746e2f3b9231576f1
blob - f9e9dbe6239e6560d0aea5aa3d366b2ef5a9513a
blob + ebac30e06a4b9df017c6d9c1e2e7d6dc47cae63c
--- server.c
+++ server.c
@@ -65,14 +65,9 @@ static void	 client_close_ev(int, short, void *);
 
 static void	 do_accept(int, short, void*);
 
-static void	 handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
 static void	 handle_dispatch_imsg(int, short, void *);
 static void	 handle_siginfo(int, short, void*);
 
-static imsg_handlerfn *handlers[] = {
-	[IMSG_QUIT] = handle_imsg_quit,
-};
-
 static uint32_t server_client_id;
 
 struct client_tree_id clients;
@@ -1318,29 +1313,49 @@ client_by_id(int id)
 }
 
 static void
-handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
+handle_dispatch_imsg(int fd, short ev, void *d)
 {
-	/*
-	 * don't call event_loopbreak since we want to finish to
-	 * handle the ongoing connections.
-	 */
+	struct imsgbuf	*ibuf = d;
+	struct imsg	 imsg;
+	ssize_t		 n;
 
-	shutting_down = 1;
+	if ((n = imsg_read(ibuf)) == -1) {
+		if (errno == EAGAIN || errno == EWOULDBLOCK)
+			return;
+		fatal("imsg_read");
+	}
 
-	event_del(&e4);
-	if (has_ipv6)
-		event_del(&e6);
-	if (has_siginfo)
-		signal_del(&siginfo);
-	event_del(&imsgev);
-	signal_del(&sigusr2);
-}
+	if (n == 0)
+		fatal("connection closed.");	/* XXX: fatalx */
 
-static void
-handle_dispatch_imsg(int fd, short ev, void *d)
-{
-	struct imsgbuf *ibuf = d;
-	dispatch_imsg(ibuf, handlers, sizeof(handlers));
+	for (;;) {
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
+			fatal("imsg_get");
+		if (n == 0)
+			return;
+
+		switch (imsg.hdr.type) {
+		case IMSG_QUIT:
+			/*
+			 * Don't call event_loopbreak since we want to
+			 * finish handling the ongoing connections.
+			 */
+			shutting_down = 1;
+
+			event_del(&e4);
+			if (has_ipv6)
+				event_del(&e6);
+			if (has_siginfo)
+				signal_del(&siginfo);
+			event_del(&imsgev);
+			signal_del(&sigusr2);
+			break;
+		default:
+			/* XXX: fatalx */
+			fatal("Unknown message %d", imsg.hdr.type);
+		}
+		imsg_free(&imsg);
+	}
 }
 
 static void