Skip to contentSkip to conversation

batctl: ping: Add subsecond precision to ping interval

PATCH Superseded Noah Peterson 2 participants 2 messages Dec 25, 2024 – Dec 31, 2024

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 batctl: ping: Add subsecond precision to ping interval

Modify the batctl ping utility to accept both integer and floating-point values for the interval between sending pings. This enhancement allows specifying intervals with subsecond precision.

For example:
sudo batctl ping aa:bb:cc:dd:ee:ff -i 0.5

---
v2: Fixing use-after-free, adding a missing header file, noted by Sven <[email protected]>

---
 ping.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
ping.c +18 -9
index 52bce4d..a69b6ac 100644
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <sys/select.h>
 #include <sys/time.h>
+#include <time.h>
 #include <netinet/if_ether.h>
 
 #include "batadv_packet_compat.h"
@@ -65,14 +66,15 @@ static int ping(struct state *state, int argc, char **argv)
 	struct bat_host *bat_host, *rr_host;
 	ssize_t read_len;
 	int ret = EXIT_FAILURE, res, optchar, found_args = 1;
-	int loop_count = -1, loop_interval = 0, timeout = 1, rr = 0, i;
+	int loop_count = -1, timeout = 1, rr = 0, i;
 	unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss;
-	char *dst_string, *mac_string, *rr_string;
-	double time_delta;
+	char *dst_string, *mac_string, *rr_string, *end = NULL;
+	double time_delta, ping_interval, integral_part, fractional_part = 0.0;
 	float min = 0.0, max = 0.0, avg = 0.0, mdev = 0.0;
 	uint8_t last_rr_cur = 0, last_rr[BATADV_RR_LEN][ETH_ALEN];
 	size_t packet_len;
 	int disable_translate_mac = 0;
+	struct timespec loop_interval = {0, 0};
 
 	while ((optchar = getopt(argc, argv, "hc:i:t:RT")) != -1) {
 		switch (optchar) {
@@ -86,9 +88,17 @@ static int ping(struct state *state, int argc, char **argv)
 			ping_usage();
 			return EXIT_SUCCESS;
 		case 'i':
-			loop_interval = strtol(optarg, NULL , 10);
-			if (loop_interval < 1)
-				loop_interval = 1;
+			errno = 0;
+			ping_interval = strtod(optarg, &end);
+			if (errno) {
+				fprintf(stderr, "Error - invalid ping interval '%s'\n", optarg);
+				goto out;
+			} else {
+				ping_interval = fmax(ping_interval, 0.001);
+			}
+			fractional_part = modf(ping_interval, &integral_part);
+			loop_interval.tv_sec = (time_t)integral_part;
+			loop_interval.tv_nsec = (long)(fractional_part * 1000000000l);
 			found_args += ((*((char*)(optarg - 1)) == optchar ) ? 1 : 2);
 			break;
 		case 't':
@@ -285,9 +295,8 @@ static int ping(struct state *state, int argc, char **argv)
 		/* skip last sleep in case no more packets will be sent out */
 		if (loop_count == 0)
 			continue;
-
-		if (loop_interval > 0)
-			sleep(loop_interval);
+		if (loop_interval.tv_sec > 0 || loop_interval.tv_nsec > 0)
+			nanosleep(&loop_interval, NULL);
 		else if ((tv.tv_sec != 0) || (tv.tv_usec != 0))
 			select(0, NULL, NULL, NULL, &tv);
 	}
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 11 lines · On Wednesday, 25 December 2024 04:27:24 GMT+1 Noah Peterson wrote:

Modify the batctl ping utility to accept both integer and floating-point values for the interval between sending pings. This enhancement allows specifying intervals with subsecond precision.

For example:
sudo batctl ping aa:bb:cc:dd:ee:ff -i 0.5

---
v2: Fixing use-after-free, adding a missing header file, noted by Sven <[email protected]>

This patch doesn't apply:

$ git rev-parse HEAD
60ff59118fd7f007adae1cc77ce9b268e3c977ce
$ b4 shazam -s [email protected]
Grabbing thread from lore.kernel.org/all/[email protected]/t.mbox.gz
Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 1 messages in the thread
Looking for additional code-review trailers on lore.kernel.org
Analyzing 0 code-review messages
Checking attestation on all messages, may take a moment...
---
  ✓ [PATCH v2] batctl: ping: Add subsecond precision to ping interval
    + Signed-off-by: Sven Eckelmann <[email protected]>
  ---
  ✓ Signed: DKIM/gmail.com
---
Total patches: 1
---
Applying: batctl: ping: Add subsecond precision to ping interval
Patch failed at 0001 batctl: ping: Add subsecond precision to ping interval
error: patch failed: ping.c:65
error: ping.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
$ patch -p1 -i .git/rebase-apply/patch                                           
patching file ping.c
Hunk #1 succeeded at 19 (offset -2 lines).
Hunk #2 FAILED at 66.
Hunk #3 FAILED at 87.
Hunk #4 succeeded at 302 (offset 16 lines).
2 out of 4 hunks FAILED -- saving rejects to file ping.c.rej
$ cat ping.c.rej
--- ping.c
+++ ping.c
@@ -66,14 +67,15 @@ static int ping(struct state *state, int argc, char **argv)
        struct bat_host *bat_host, *rr_host;
        ssize_t read_len;
        int ret = EXIT_FAILURE, res, optchar, found_args = 1;
-       int loop_count = -1, loop_interval = 0, timeout = 1, rr = 0, i;
+       int loop_count = -1, timeout = 1, rr = 0, i;
        unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss;
-       char *dst_string, *mac_string, *rr_string;
-       double time_delta;
+       char *dst_string, *mac_string, *rr_string, *end = NULL;
+       double time_delta, ping_interval, integral_part, fractional_part = 0.0;
        float min = 0.0, max = 0.0, avg = 0.0, mdev = 0.0;
        uint8_t last_rr_cur = 0, last_rr[BATADV_RR_LEN][ETH_ALEN];
        size_t packet_len;
        int disable_translate_mac = 0;
+       struct timespec loop_interval = {0, 0};
        while ((optchar = getopt(argc, argv, "hc:i:t:RT")) != -1) {
                switch (optchar) {
@@ -87,9 +89,17 @@ static int ping(struct state *state, int argc, char **argv)
                        ping_usage();
                        return EXIT_SUCCESS;
                case 'i':
-                       loop_interval = strtol(optarg, NULL , 10);
-                       if (loop_interval < 1)
-                               loop_interval = 1;
+                       errno = 0;
+                       ping_interval = strtod(optarg, &end);
+                       if (errno) {

+ fprintf(stderr, "Error - invalid ping interval '%s'\n", optarg);
+ goto out;
+ } else {
+ ping_interval = fmax(ping_interval, 0.001);

+                       }
+                       fractional_part = modf(ping_interval, &integral_part);
+                       loop_interval.tv_sec = (time_t)integral_part;
+                       loop_interval.tv_nsec = (long)(fractional_part * 1000000000l);
                        found_args += ((*((char*)(optarg - 1)) == optchar ) ? 1 : 2);
                        break;
                case 't'

It looks like you're changes were based on a version of batctl which was from 2024-10-19 or older.

Quoted 22 lines
---
 ping.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
ping.c +2 -1
index 52bce4d..a69b6ac 100644
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <sys/select.h>
 #include <sys/time.h>
+#include <time.h>
 #include <netinet/if_ether.h>
 
 #include "batadv_packet_compat.h"
@@ -65,14 +66,15 @@ static int ping(struct state *state, int argc, char **argv)
 	struct bat_host *bat_host, *rr_host;
 	ssize_t read_len;
 	int ret = EXIT_FAILURE, res, optchar, found_args = 1;
-	int loop_count = -1, loop_interval = 0, timeout = 1, rr = 0, i;
+	int loop_count = -1, timeout = 1, rr = 0, i;

ping.c static int ping(struct state *state, int argc, char **argv) · line 77

Quoted 10 lines
ping.c +3 -2
 	unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss;
-	char *dst_string, *mac_string, *rr_string;
-	double time_delta;
+	char *dst_string, *mac_string, *rr_string, *end = NULL;
+	double time_delta, ping_interval, integral_part, fractional_part = 0.0;
 	float min = 0.0, max = 0.0, avg = 0.0, mdev = 0.0;
 	uint8_t last_rr_cur = 0, last_rr[BATADV_RR_LEN][ETH_ALEN];
 	size_t packet_len;
 	int disable_translate_mac = 0;
+	struct timespec loop_interval = {0, 0};

This is one of the reasons why your patch doesn't apply. When you rebase it on the newest version, please try to use the reverse X-mas tree order and avoid multiple declarations per line.

Kind regards,
Sven