diff mbox series

Add "reconnect" cmdline argument to hostapd_cli/wpa_cli

Message ID 1566290223-26218-1-git-send-email-bilal.hatipoglu@airties.com
State Changes Requested
Headers show
Series Add "reconnect" cmdline argument to hostapd_cli/wpa_cli | expand

Commit Message

Bilal Hatipoglu Aug. 20, 2019, 8:37 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 for using a single instance cli to retrieve events & 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>
---
 hostapd/hostapd_cli.c    | 20 +++++++++++++++-----
 wpa_supplicant/wpa_cli.c | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 8 deletions(-)

Comments

Bilal Hatipoglu Aug. 28, 2019, 10:01 a.m. UTC | #1
Hello,

Please discard this. I will send version 2 of the patch.

Regards.


Bilal Hatipoglu









From: Hostap <hostap-bounces@lists.infradead.org> on behalf of Bilal Hatipoglu <bilal.hatipoglu@airties.com>

Sent: Tuesday, August 20, 2019 11:37 AM

To: hostap@lists.infradead.org <hostap@lists.infradead.org>

Subject: [PATCH] Add "reconnect" cmdline argument to hostapd_cli/wpa_cli

 


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 for using a single instance cli to retrieve events & 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>

---

 hostapd/hostapd_cli.c    | 20 +++++++++++++++-----

 wpa_supplicant/wpa_cli.c | 35 ++++++++++++++++++++++++++++++++---

 2 files changed, 47 insertions(+), 8 deletions(-)



diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c

index 0460243..07b6ed0 100644

--- a/hostapd/hostapd_cli.c

+++ b/hostapd/hostapd_cli.c

@@ -2011,12 +2011,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 +2049,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 +2091,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 +2109,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..1c7d885 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;

@@ -4039,7 +4040,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 +4234,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 +4585,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 +4665,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 +4700,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 +4725,23 @@ 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 &&
diff mbox series

Patch

diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c
index 0460243..07b6ed0 100644
--- a/hostapd/hostapd_cli.c
+++ b/hostapd/hostapd_cli.c
@@ -2011,12 +2011,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 +2049,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 +2091,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 +2109,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..1c7d885 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;
@@ -4039,7 +4040,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 +4234,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 +4585,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 +4665,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 +4700,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 +4725,23 @@  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 &&