All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [PATCH 5/8] alfred: vis: report failures while reading the answer
Date: Tue, 28 Jul 2026 18:01:34 +0200	[thread overview]
Message-ID: <20260728-bugfixes-vis-v1-5-2540447c8a67@narfation.org> (raw)
In-Reply-To: <20260728-bugfixes-vis-v1-0-2540447c8a67@narfation.org>

vis_read_answer() returns NULL for a:

* closed connection
* read error
* malformed packet
* unexpected answer

All of them were indistinguishable from the latter regular end of the
answer. Even on errors batadv-vis printed an empty (well formed document).
An error could not directly be detected by the return code.

Report the error state of the packet parsing separately and use it for the
exit code.

Fixes: 2e3e312f7757 ("alfred: add vis")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 vis/vis.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/vis/vis.c b/vis/vis.c
index 33fa692..451d79c 100644
--- a/vis/vis.c
+++ b/vis/vis.c
@@ -718,7 +718,8 @@ static ssize_t read_full(int fd, void *buf, size_t count)
 	return read_len;
 }
 
-static struct vis_v1 *vis_receive_answer_packet(int sock, uint16_t *len)
+static struct vis_v1 *vis_receive_answer_packet(int sock, uint16_t *len,
+						int *error)
 {
 	struct alfred_push_data_v0 *push;
 	static uint8_t buf[65536];
@@ -727,15 +728,29 @@ static struct vis_v1 *vis_receive_answer_packet(int sock, uint16_t *len)
 	int ret;
 	int l;
 
+	*error = -1;
+
 	ret = read_full(sock, buf, sizeof(*tlv));
-	if (ret < 0)
+	if (ret < 0) {
+		perror("read from unix socket failed");
 		return NULL;
+	}
+
+	/* end of the answer */
+	if (ret == 0) {
+		*error = 0;
+		return NULL;
+	}
 
 	if (ret < (int)sizeof(*tlv))
 		return NULL;
 
 	tlv = (struct alfred_tlv *)buf;
-	/* TODO: might return an ALFRED_STATUS_ERROR too, handle it */
+	if (tlv->type == ALFRED_STATUS_ERROR) {
+		fprintf(stderr, "Request of vis data failed\n");
+		return NULL;
+	}
+
 	if (tlv->type != ALFRED_PUSH_DATA)
 		return NULL;
 
@@ -763,6 +778,8 @@ static struct vis_v1 *vis_receive_answer_packet(int sock, uint16_t *len)
 	if (data->header.version != VIS_PACKETVERSION)
 		return NULL;
 
+	*error = 0;
+
 	return (struct vis_v1 *) data->data;
 }
 
@@ -995,6 +1012,7 @@ static int vis_read_answer(struct globals *globals)
 	struct vis_iface *ifaces;
 	struct vis_v1 *vis_data;
 	uint16_t len;
+	int ret = 0;
 
 	switch (globals->vis_format) {
 	case FORMAT_DOT:
@@ -1013,7 +1031,7 @@ static int vis_read_answer(struct globals *globals)
 	ops->preamble();
 
 	while ((vis_data =
-		vis_receive_answer_packet(globals->unix_sock, &len)) != NULL) {
+		vis_receive_answer_packet(globals->unix_sock, &len, &ret)) != NULL) {
 		if (len < sizeof(*vis_data))
 			return -1;
 
@@ -1037,19 +1055,21 @@ static int vis_read_answer(struct globals *globals)
 	}
 	ops->postamble();
 
-	return 0;
+	return ret;
 }
 
 static int vis_get_data(struct globals *globals)
 {
+	int ret;
+
 	globals->unix_sock = vis_request_data(globals);
 	if (globals->unix_sock < 0)
 		return -1;
 
-	vis_read_answer(globals);
+	ret = vis_read_answer(globals);
 	close(globals->unix_sock);
 
-	return 0;
+	return ret;
 }
 
 static void vis_usage(void)
@@ -1176,8 +1196,10 @@ int main(int argc, char *argv[])
 		return vis_server(globals);
 		break;
 	case OPMODE_CLIENT:
-		return vis_get_data(globals);
-		break;
+		if (vis_get_data(globals) < 0)
+			return 1;
+
+		return 0;
 	}
 
 	return 0;

-- 
2.47.3


  parent reply	other threads:[~2026-07-28 17:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 16:01 [PATCH 0/8] alfred: vis: random fixes Sven Eckelmann
2026-07-28 16:01 ` [PATCH 1/8] alfred: vis: reject too long unix socket paths Sven Eckelmann
2026-07-28 16:01 ` [PATCH 2/8] alfred: vis: keep interface names always in heap Sven Eckelmann
2026-07-28 16:01 ` [PATCH 3/8] alfred: vis: return query_rtnl_link error on failed send Sven Eckelmann
2026-07-28 16:01 ` [PATCH 4/8] alfred: vis: handle short reads on the unix socket Sven Eckelmann
2026-07-28 16:01 ` Sven Eckelmann [this message]
2026-07-28 16:01 ` [PATCH 6/8] alfred: vis: don't drop the rest of the answer for a bogus dataset Sven Eckelmann
2026-07-28 16:01 ` [PATCH 7/8] alfred: vis: fix jsondoc output for nodes without vis entries Sven Eckelmann
2026-07-28 16:01 ` [PATCH 8/8] alfred: vis: skip originators with unresolvable hard interface Sven Eckelmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260728-bugfixes-vis-v1-5-2540447c8a67@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.