[v2] Add "reconnect" cmdline argument to hostapd_cli/wpa_cli
diff mbox series

Message ID 1567675860-19823-1-git-send-email-bilalhp@gmail.com
State New
Headers show
Series
  • [v2] Add "reconnect" cmdline argument to hostapd_cli/wpa_cli
Related show

Commit Message

bilalhp@gmail.com Sept. 5, 2019, 9:31 a.m. UTC
From: Bilal Hatipoglu <bilal.hatipoglu@airties.com>

When newly added "-r" parameter is used, both clis will try to reconnect
forever on connection lost until signalled (ctrl+c) or terminated.
This is useful only when used with -a to take action to retrieve events or
get status and the cli process stays even if hostapd/wpa_supplicant daemons
restart for some reason (e.g. configuration change).

Signed-off-by: Veli Demirel <veli.demirel@airties.com>
Signed-off-by: Bilal Hatipoglu <bilal.hatipoglu@airties.com>
---
 hostapd/hostapd_cli.c    | 25 +++++++++++++++++++------
 wpa_supplicant/wpa_cli.c | 40 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 55 insertions(+), 10 deletions(-)

Patch
diff mbox series

diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c
index 0460243..83ce6f1 100644
--- a/hostapd/hostapd_cli.c
+++ b/hostapd/hostapd_cli.c
@@ -54,7 +54,7 @@  static void usage(void)
 	fprintf(stderr, "%s\n", hostapd_cli_version);
 	fprintf(stderr,
 		"\n"
-		"usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvB] "
+		"usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvBr] "
 		"[-a<path>] \\\n"
 		"                   [-P<pid file>] [-G<ping interval>] [command..]\n"
 		"\n"
@@ -68,6 +68,9 @@  static void usage(void)
 		"   -a<file>     run in daemon mode executing the action file "
 		"based on events\n"
 		"                from hostapd\n"
+		"   -r           try to reconnect when client socket is "
+		"disconnected.\n"
+		"                this is useful only when used with -a\n"
 		"   -B           run a daemon in the background\n"
 		"   -i<ifname>   Interface to listen on (default: first "
 		"interface found in the\n"
@@ -2011,12 +2014,13 @@  int main(int argc, char *argv[])
 	int warning_displayed = 0;
 	int c;
 	int daemonize = 0;
+	int reconnect = 0;
 
 	if (os_program_init())
 		return -1;
 
 	for (;;) {
-		c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
+		c = getopt(argc, argv, "a:BhG:i:p:P:s:vr");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -2048,6 +2052,9 @@  int main(int argc, char *argv[])
 		case 's':
 			client_socket_dir = optarg;
 			break;
+		case 'r':
+			reconnect = 1;
+			break;
 		default:
 			usage();
 			return -1;
@@ -2087,8 +2094,7 @@  int main(int argc, char *argv[])
 				printf("Connection established.\n");
 			break;
 		}
-
-		if (!interactive) {
+		if (!interactive && !reconnect) {
 			perror("Failed to connect to hostapd - "
 			       "wpa_ctrl_open");
 			return -1;
@@ -2106,8 +2112,15 @@  int main(int argc, char *argv[])
 		return -1;
 	if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
 		return -1;
-
-	if (interactive)
+	if (reconnect && action_file && ctrl_ifname != NULL) {
+		do {
+			if (ctrl_conn) {
+				hostapd_cli_action(ctrl_conn);
+			}
+			os_sleep(1, 0);
+			hostapd_cli_reconnect(ctrl_ifname);
+		} while(!hostapd_cli_quit);
+	} else if (interactive)
 		hostapd_cli_interactive();
 	else if (action_file)
 		hostapd_cli_action(ctrl_conn);
diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c
index 43ac427..7e1e59b 100644
--- a/wpa_supplicant/wpa_cli.c
+++ b/wpa_supplicant/wpa_cli.c
@@ -52,6 +52,7 @@  static char *ctrl_ifname = NULL;
 static const char *global = NULL;
 static const char *pid_file = NULL;
 static const char *action_file = NULL;
+static int reconnect = 0;
 static int ping_interval = 5;
 static int interactive = 0;
 static char *ifname_prefix = NULL;
@@ -80,7 +81,7 @@  static void update_ifnames(struct wpa_ctrl *ctrl);
 
 static void usage(void)
 {
-	printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
+	printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvBr] "
 	       "[-a<action file>] \\\n"
 	       "        [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
 	       "\\\n"
@@ -91,6 +92,8 @@  static void usage(void)
 	       "  -a = run in daemon mode executing the action file based on "
 	       "events from\n"
 	       "       wpa_supplicant\n"
+	       "  -r = try to reconnect when client socket is disconnected.\n"
+	       "       this is useful only when used with -a\n"
 	       "  -B = run a daemon in the background\n"
 	       "  default path: " CONFIG_CTRL_IFACE_DIR "\n"
 	       "  default interface: first interface found in socket path\n");
@@ -4039,7 +4042,9 @@  static void wpa_cli_action_process(const char *msg)
 		wpa_cli_exec(action_file, ifname, pos);
 	} else if (str_starts(pos, WPA_EVENT_TERMINATING)) {
 		printf("wpa_supplicant is terminating - stop monitoring\n");
-		wpa_cli_quit = 1;
+		if (!reconnect) {
+			wpa_cli_quit = 1;
+		}
 	}
 }
 
@@ -4231,6 +4236,10 @@  static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
 	if (wpa_ctrl_pending(ctrl) < 0) {
 		printf("Connection to wpa_supplicant lost - trying to "
 		       "reconnect\n");
+		if (reconnect) {
+			eloop_terminate();
+			return;
+		}
 		wpa_cli_reconnect();
 	}
 }
@@ -4578,6 +4587,9 @@  static void wpa_cli_cleanup(void)
 static void wpa_cli_terminate(int sig, void *ctx)
 {
 	eloop_terminate();
+	if (reconnect) {
+		wpa_cli_quit = 1;
+	}
 }
 
 
@@ -4655,7 +4667,7 @@  int main(int argc, char *argv[])
 		return -1;
 
 	for (;;) {
-		c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:v");
+		c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:vr");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -4690,6 +4702,9 @@  int main(int argc, char *argv[])
 		case 's':
 			client_socket_dir = optarg;
 			break;
+		case 'r':
+			reconnect = 1;
+			break;
 		default:
 			usage();
 			return -1;
@@ -4712,7 +4727,24 @@  int main(int argc, char *argv[])
 	if (ctrl_ifname == NULL)
 		ctrl_ifname = wpa_cli_get_default_ifname();
 
-	if (interactive) {
+	if (reconnect && action_file && ctrl_ifname) {
+		do {
+			if (ctrl_conn) {
+				wpa_cli_action(ctrl_conn);
+			} else {
+				os_sleep(1, 0);
+			}
+			wpa_cli_close_connection();
+			wpa_cli_open_connection(ctrl_ifname, 0);
+			if (ctrl_conn) {
+				if (wpa_ctrl_attach(ctrl_conn) != 0) {
+					wpa_cli_close_connection();
+				} else {
+					wpa_cli_attached = 1;
+				}
+			}
+		} while (!wpa_cli_quit);
+	} else if (interactive) {
 		wpa_cli_interactive();
 	} else {
 		if (!global &&