Skip to contentSkip to conversation

batctl: tpmeter bugfixes

PATCH New Sven Eckelmann 1 participant 6 messages Jul 7

lore
Share & tools
lore
#0 lore raw

cover batctl: tpmeter bugfixes

I have now splitted the bugfixes for batctl in topic branches to make it a little bit easier to review. Similar to the "random bugfixes 2026-06-21" patchset, they are just various (mostly minor) problems which should be tackled at some point.

---
Sven Eckelmann (5):
batctl: tpmeter: don't use negative errno as exit status
batctl: tpmeter: abort result wait on receive errors
batctl: tpmeter: reject invalid test duration argument
batctl: tpmeter: report kernel errors via strerror
batctl: tpmeter: don't cancel test from the signal handler

 throughputmeter.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 51 insertions(+), 7 deletions(-)

---
base-commit: eb9597d4ca6db17c579f5ae9443c51b013e2fe65
change-id: 20260704-bugfixes-tpmeter-a5de357b56e1

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>
#1 lore raw

PATCH 1/5 batctl: tpmeter: don't use negative errno as exit status

The netlink error codes from tp_meter_start() and tp_recv_result() are returned unmodified as command result, which main() passes to exit(). A failure like -EOPNOTSUPP therefore turns into the meaningless exit status instead of EXIT_FAILURE.

Report EXIT_FAILURE after printing the error.

Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 throughputmeter.c | 2 ++
 1 file changed, 2 insertions(+)
throughputmeter.c +2 -0
index 98a9490..10f4f24 100644
@@ -383,6 +383,7 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	ret = tp_meter_start(state, dst_mac, time, &cookie);
 	if (ret < 0) {
 		printf("Failed to send tp_meter request to kernel: %d\n", ret);
+		ret = EXIT_FAILURE;
 		goto out;
 	}
 
@@ -390,6 +391,7 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	ret = tp_recv_result(listen_sock, &result);
 	if (ret < 0) {
 		printf("Failed to recv tp_meter result from kernel: %d\n", ret);
+		ret = EXIT_FAILURE;
 		goto out;
 	}
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/5 batctl: tpmeter: abort result wait on receive errors

In reply to Sven Eckelmann

tp_recv_result() loops on nl_recvmsgs() until the tp_meter result notification arrives, but never checks its return value. When the receive fails (receive queue overrun and drops notification, socket failed completely, ...), the loop just calls nl_recvmsgs() again and blocks forever in recvmsg() waiting for a message that will never arrive.

Leave the receive loop on errors instead; the existing checks below then report the failure to the caller.

Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 throughputmeter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
throughputmeter.c +6 -2
index 10f4f24..fd4165c 100644
@@ -198,6 +198,7 @@ static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 {
 	struct nl_cb *cb;
 	int err = 0;
+	int ret;
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
 	nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
@@ -205,8 +206,11 @@ static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 		  result);
 	nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, result);
 
-	while (result->error == 0 && !result->found)
-		nl_recvmsgs(sock, cb);
+	while (result->error == 0 && !result->found) {
+		ret = nl_recvmsgs(sock, cb);
+		if (ret < 0)
+			break;
+	}
 
 	nl_cb_put(cb);
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/5 batctl: tpmeter: reject invalid test duration argument

In reply to Sven Eckelmann

The -t option parses its argument with strtoul and
assigns the result straight into the uint32_t test length without any validation.

Use the same strto* validation as the rest of batctl and print a user readable error in case of an parsing error.

Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 throughputmeter.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
throughputmeter.c +12 -1
index fd4165c..7d41c10 100644
@@ -329,15 +329,26 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	};
 	struct bat_host *bat_host;
 	int ret = EXIT_FAILURE;
+	unsigned long time_arg;
 	uint64_t throughput;
 	uint32_t time = 0;
 	char *dst_string;
+	char *endptr;
 	int optchar;
 
 	while ((optchar = getopt(argc, argv, "t:n")) != -1) {
 		switch (optchar) {
 		case 't':
-			time = strtoul(optarg, NULL, 10);
+			time_arg = strtoul(optarg, &endptr, 10);
+			if (!endptr || *endptr != '\0' || endptr == optarg ||
+			    time_arg > UINT32_MAX) {
+				fprintf(stderr,
+					"Error - the supplied test duration is invalid: %s\n",
+					optarg);
+				tp_meter_usage();
+				return EXIT_FAILURE;
+			}
+			time = time_arg;
 			break;
 		case 'n':
 			read_opt &= ~USE_BAT_HOSTS;
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/5 batctl: tpmeter: report kernel errors via strerror

In reply to Sven Eckelmann

tp_meter_start() and tp_recv_result() return a negative system errno captured from the kernel's netlink error reply. Both failure paths in throughputmeter() printed that value with "%d", so the user saw a raw negative number instead of a readable message. In batctl, this is usually formatted with strerror() to get a human readable version.

---
 throughputmeter.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
throughputmeter.c +4 -2
index 7d41c10..6799c93 100644
@@ -397,7 +397,8 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 
 	ret = tp_meter_start(state, dst_mac, time, &cookie);
 	if (ret < 0) {
-		printf("Failed to send tp_meter request to kernel: %d\n", ret);
+		printf("Failed to send tp_meter request to kernel: %s\n",
+		       strerror(-ret));
 		ret = EXIT_FAILURE;
 		goto out;
 	}
@@ -405,7 +406,8 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	result.cookie = cookie.cookie;
 	ret = tp_recv_result(listen_sock, &result);
 	if (ret < 0) {
-		printf("Failed to recv tp_meter result from kernel: %d\n", ret);
+		printf("Failed to recv tp_meter result from kernel: %s\n",
+		       strerror(-ret));
 		ret = EXIT_FAILURE;
 		goto out;
 	}
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/5 batctl: tpmeter: don't cancel test from the signal handler

In reply to Sven Eckelmann

tp_sig_handler() calls fflush() and tp_meter_stop() directly from signal context. tp_meter_stop() allocates a netlink message with nlmsg_alloc() and sends it via nl_send_auto_complete() - none of these functions (nor fflush) are async-signal-safe.

Let the signal handler only set a flag. tp_recv_result() now waits for the result notification with poll(), which is not restarted when a signal arrives, and sends the CANCEL request from the main flow before continuing to wait for the (then canceled) test result.

Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 throughputmeter.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
throughputmeter.c +27 -2
index 6799c93..dbb88f6 100644
@@ -17,6 +17,7 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <net/if.h>
+#include <poll.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,6 +35,7 @@
 
 static struct ether_addr *dst_mac;
 static struct state *tp_state;
+static volatile sig_atomic_t tp_aborted;
 
 struct tp_result {
 	int error;
@@ -194,8 +196,15 @@ static int no_seq_check(struct nl_msg *msg __maybe_unused,
 	return NL_OK;
 }
 
+static int tp_meter_stop(struct state *state, struct ether_addr *dst_mac);
+
 static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 {
+	struct pollfd pfd = {
+		.fd = nl_socket_get_fd(sock),
+		.events = POLLIN,
+	};
+	bool cancel_sent = false;
 	struct nl_cb *cb;
 	int err = 0;
 	int ret;
@@ -207,6 +216,23 @@ static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 	nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, result);
 
 	while (result->error == 0 && !result->found) {
+		if (tp_aborted && !cancel_sent) {
+			cancel_sent = true;
+			tp_meter_stop(tp_state, dst_mac);
+		}
+
+		/* wake up regularly to notice an abort even when the signal
+		 * arrived outside of poll()
+		 */
+		ret = poll(&pfd, 1, 1000);
+		if (ret < 0) {
+			if (errno == EINTR)
+				continue;
+			break;
+		}
+		if (ret == 0)
+			continue;
+
 		ret = nl_recvmsgs(sock, cb);
 		if (ret < 0)
 			break;
@@ -295,8 +321,7 @@ void tp_sig_handler(int sig)
 	switch (sig) {
 	case SIGINT:
 	case SIGTERM:
-		fflush(stdout);
-		tp_meter_stop(tp_state, dst_mac);
+		tp_aborted = 1;
 		break;
 	default:
 		break;
Signature · 1 line
2.47.3