Skip to contentSkip to conversation

alfred: Show error message for invalid batadv interface

PATCH New Sven Eckelmann 1 participant 4 messages Feb 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 alfred: Show error message for invalid batadv interface

The alfred server process always stopped without any informational message when the provided batman-adv was not "none" and was not accessible. This made it extremely hard to debug the reason why alfred directly stopped after launching it.

---
 server.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
server.c +4 -1
index fc27246..efac5ad 100644
@@ -385,8 +385,11 @@ int alfred_server(struct globals *globals)
 	}
 
 	if (strcmp(globals->mesh_iface, "none") != 0 &&
-	    batadv_interface_check(globals->mesh_iface) < 0)
+	    batadv_interface_check(globals->mesh_iface) < 0) {
+		fprintf(stderr, "Can't start server: batman-adv interface %s not found\n",
+			globals->mesh_iface);
 		return -1;
+	}
 
 	num_socks = netsock_open_all(globals);
 	if (num_socks <= 0) {
Signature · 1 line
2.30.0
#1 lore raw

PATCH 2/4 alfred: Allow exactly one interface for secondary mode

A primary alfred daemon allows syncing over more than one interface. But the secondary alfred daemon needs exactly one interface. But the check for this property was insufficient because it allowed paramters like "-i wlan0,asd" when wlan0 is valid and asd is not valid.

The better solution is to really use the number of interfaces given to alfred instead of the number of interfaces evaluated as "valid".

Fixes 67ae5f57eedd ("alfred: Add support for multiple interfaces per master")
---
 alfred.h  |  1 +
 netsock.c | 11 +++++++++++
 server.c  |  4 +++-
 3 files changed, 15 insertions(+), 1 deletion(-)
alfred.h +1 -0
index 1e2c058..7d6b0b3 100644
@@ -182,6 +182,7 @@ int unix_sock_req_data_finish(struct globals *globals,
 int vis_update_data(struct globals *globals);
 /* netsock.c */
 int netsock_open_all(struct globals *globals);
+size_t netsocket_count_interfaces(struct globals *globals);
 void netsock_close_all(struct globals *globals);
 int netsock_set_interfaces(struct globals *globals, char *interfaces);
 struct interface *netsock_first_interface(struct globals *globals);
netsock.c +11 -0
index 367b207..84b0ec3 100644
@@ -471,6 +471,17 @@ int netsock_open_all(struct globals *globals)
 	return num_socks;
 }
 
+size_t netsocket_count_interfaces(struct globals *globals)
+{
+	struct interface *interface;
+	size_t count = 0;
+
+	list_for_each_entry(interface, &globals->interfaces, list)
+		count++;
+
+	return count;
+}
+
 void netsock_reopen(struct globals *globals)
 {
 	struct interface *interface;
server.c +3 -1
index efac5ad..eb2bc8a 100644
@@ -371,6 +371,7 @@ int alfred_server(struct globals *globals)
 	int maxsock, ret, recvs;
 	struct timespec last_check, now, tv;
 	fd_set fds, errfds;
+	size_t num_interfaces;
 	int num_socks;
 
 	if (create_hashes(globals))
@@ -397,7 +398,8 @@ int alfred_server(struct globals *globals)
 		return -1;
 	}
 
-	if (num_socks > 1 && globals->opmode == OPMODE_SECONDARY) {
+	num_interfaces = netsocket_count_interfaces(globals);
+	if (num_interfaces > 1 && globals->opmode == OPMODE_SECONDARY) {
 		fprintf(stderr, "More than one interface specified in secondary mode\n");
 		return -1;
 	}
Signature · 1 line
2.30.0
#2 lore raw

PATCH 3/4 alfred: Save global mode flags in bitfield

In reply to Sven Eckelmann

The verbose and ipv4mode entries in the globals structure is only used to save a boolean information. So just use a bit in a bitfield to store this information instead of a full int.

---
 alfred.h | 4 ++--
 main.c   | 9 +++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
alfred.h +2 -2
index 7d6b0b3..c64ff17 100644
@@ -115,8 +115,8 @@ struct globals {
 	enum clientmode clientmode;
 	int clientmode_arg;
 	int clientmode_version;
-	int verbose;
-	int ipv4mode;
+	uint8_t verbose:1;
+	uint8_t ipv4mode:1;
 
 	int unix_sock;
 	const char *unix_path;
main.c +5 -4
index 7b866cc..f25b6cc 100644
@@ -9,6 +9,7 @@
 #include <arpa/inet.h>
 #include <getopt.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -181,8 +182,8 @@ static struct globals *alfred_init(int argc, char *argv[])
 	globals->clientmode_version = 0;
 	globals->mesh_iface = "bat0";
 	globals->unix_path = ALFRED_SOCK_PATH_DEFAULT;
-	globals->verbose = 0;
-	globals->ipv4mode = 0;
+	globals->verbose = false;
+	globals->ipv4mode = false;
 	globals->update_command = NULL;
 	globals->sync_period.tv_sec = ALFRED_INTERVAL;
 	globals->sync_period.tv_nsec = 0;
@@ -252,7 +253,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 			globals->unix_path = optarg;
 			break;
 		case 'd':
-			globals->verbose++;
+			globals->verbose = true;
 			break;
 		case 'c':
 			globals->update_command = optarg;
@@ -268,7 +269,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 			printf(" ** Setting sync interval to: %.9f seconds (%ld.%09ld)\n", sync_period, globals->sync_period.tv_sec, globals->sync_period.tv_nsec);
 			break;
 		case '4':
-			globals->ipv4mode = 1;
+			globals->ipv4mode = true;
 			inet_pton(AF_INET, optarg, &alfred_mcast.ipv4);
 			printf(" ** IPv4 Multicast Mode: %x\n", alfred_mcast.ipv4.s_addr);
 			break;
Signature · 1 line
2.30.0
#3 lore raw

PATCH 4/4 alfred: Allow start of server without valid interface

In reply to Sven Eckelmann

The alfred server always needs interfaces to operate on. But these interfaces might not exist at the moment when the daemon process is started. This caused an error and stopped the process.

But alfred is able to deal with interfaces which disappeared at runtime but existed at startup. To force a similar behavior for the alfred startup, the parameter "--force" or "-f" is introduced.

---
 alfred.h     | 1 +
 main.c       | 7 ++++++-
 man/alfred.8 | 3 +++
 server.c     | 5 +++--
 4 files changed, 13 insertions(+), 3 deletions(-)
alfred.h +1 -0
index c64ff17..ac08253 100644
@@ -117,6 +117,7 @@ struct globals {
 	int clientmode_version;
 	uint8_t verbose:1;
 	uint8_t ipv4mode:1;
+	uint8_t force:1;
 
 	int unix_sock;
 	const char *unix_path;
main.c +6 -1
index f25b6cc..e190d42 100644
@@ -164,6 +164,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 		{"version",		no_argument,		NULL,	'v'},
 		{"verbose",		no_argument,		NULL,	'd'},
 		{"sync-period",		required_argument,	NULL,	'p'},
+		{"force",		no_argument,		NULL,	'f'},
 		{NULL,			0,			NULL,	0},
 	};
 
@@ -184,6 +185,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 	globals->unix_path = ALFRED_SOCK_PATH_DEFAULT;
 	globals->verbose = false;
 	globals->ipv4mode = false;
+	globals->force = false;
 	globals->update_command = NULL;
 	globals->sync_period.tv_sec = ALFRED_INTERVAL;
 	globals->sync_period.tv_nsec = 0;
@@ -191,7 +193,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 
 	time_random_seed();
 
-	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:u:dc:p:4:", long_options,
+	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:u:dc:p:4:f", long_options,
 				  &opt_ind)) != -1) {
 		switch (opt) {
 		case 'r':
@@ -273,6 +275,9 @@ static struct globals *alfred_init(int argc, char *argv[])
 			inet_pton(AF_INET, optarg, &alfred_mcast.ipv4);
 			printf(" ** IPv4 Multicast Mode: %x\n", alfred_mcast.ipv4.s_addr);
 			break;
+		case 'f':
+			globals->force = true;
+			break;
 		case 'h':
 		default:
 			alfred_usage();
man/alfred.8 +3 -0
index 25591be..e965db8 100644
@@ -72,6 +72,9 @@ Collect data from the network and prints it on the network
 \fB\-d\fP, \fB\-\-verbose\fP
 Show extra information in the data output
 .TP
+\fB\-d\fP, \fB\-\-force\fP
+Start server even when batman-adv or interface(s) are not yet available.
+.TP
 \fB\-V\fP, \fB\-\-req\-version\fP \fIversion\fP
 Specify the data version set for \fB\-s\fP
 
server.c +3 -2
index eb2bc8a..b4925e7 100644
@@ -386,14 +386,15 @@ int alfred_server(struct globals *globals)
 	}
 
 	if (strcmp(globals->mesh_iface, "none") != 0 &&
-	    batadv_interface_check(globals->mesh_iface) < 0) {
+	    batadv_interface_check(globals->mesh_iface) < 0 &&
+	    !globals->force) {
 		fprintf(stderr, "Can't start server: batman-adv interface %s not found\n",
 			globals->mesh_iface);
 		return -1;
 	}
 
 	num_socks = netsock_open_all(globals);
-	if (num_socks <= 0) {
+	if (num_socks <= 0 && !globals->force) {
 		fprintf(stderr, "Failed to open interfaces\n");
 		return -1;
 	}
Signature · 1 line
2.30.0