[PATCH] wpa-cli: Support monitor mode without action file.

greearb at candelatech.com greearb at candelatech.com
Wed Jun 14 11:29:22 PDT 2017


From: Ben Greear <greearb at candelatech.com>

I wanted a way to start wpa_cli, have it do the keep-alive ping,
and print events as they are received.  I do not want an action file,
and I do not need interactive mode, and I want to run
this in a way where stdin may not be open.

So, introduce '-m' monitor mode.

wpa_cli -m -g /var/run/wpa_supplicant_if_wiphy0

Signed-off-by: Ben Greear <greearb at candelatech.com>
---
 wpa_supplicant/wpa_cli.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c
index 510f385..7edacac 100644
--- a/wpa_supplicant/wpa_cli.c
+++ b/wpa_supplicant/wpa_cli.c
@@ -54,6 +54,7 @@ static const char *pid_file = NULL;
 static const char *action_file = NULL;
 static int ping_interval = 5;
 static int interactive = 0;
+static int do_monitor = 0;
 static char *ifname_prefix = NULL;
 
 static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
@@ -89,6 +90,7 @@ static void usage(void)
 	       "events from\n"
 	       "       wpa_supplicant\n"
 	       "  -B = run a daemon in the background\n"
+	       "  -m = Run in monitor mode, even if we don't have an action file.\n"
 	       "  default path: " CONFIG_CTRL_IFACE_DIR "\n"
 	       "  default interface: first interface found in socket path\n");
 	print_help(NULL);
@@ -123,7 +125,7 @@ static int wpa_cli_open_connection(const char *ifname, int attach)
 	if (ctrl_conn == NULL)
 		return -1;
 
-	if (attach && interactive)
+	if (attach && (interactive || do_monitor))
 		mon_conn = wpa_ctrl_open(ifname);
 	else
 		mon_conn = NULL;
@@ -168,7 +170,7 @@ static int wpa_cli_open_connection(const char *ifname, int attach)
 		return -1;
 	}
 
-	if (attach && interactive)
+	if (attach && (interactive || do_monitor))
 		mon_conn = wpa_ctrl_open2(cfile, client_socket_dir);
 	else
 		mon_conn = NULL;
@@ -178,7 +180,7 @@ static int wpa_cli_open_connection(const char *ifname, int attach)
 	if (mon_conn) {
 		if (wpa_ctrl_attach(mon_conn) == 0) {
 			wpa_cli_attached = 1;
-			if (interactive)
+			if (interactive || do_monitor)
 				eloop_register_read_sock(
 					wpa_ctrl_get_fd(mon_conn),
 					wpa_cli_mon_receive, NULL, NULL);
@@ -200,7 +202,7 @@ static void wpa_cli_close_connection(void)
 		return;
 
 	if (wpa_cli_attached) {
-		wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn);
+		wpa_ctrl_detach((interactive || do_monitor) ? mon_conn : ctrl_conn);
 		wpa_cli_attached = 0;
 	}
 	wpa_ctrl_close(ctrl_conn);
@@ -3682,6 +3684,9 @@ static void wpa_cli_action_process(const char *msg)
 	if (eloop_terminated())
 		return;
 
+	if (!action_file)
+		return;
+
 	pos = msg;
 	if (os_strncmp(pos, "IFNAME=", 7) == 0) {
 		const char *end;
@@ -3818,8 +3823,9 @@ static void wpa_cli_reconnect(void)
 			return;
 		}
 
-		if (interactive) {
-			update_ifnames(ctrl_conn);
+		if (interactive || do_monitor) {
+			if (interactive)
+				update_ifnames(ctrl_conn);
 			mon_conn = wpa_ctrl_open(global);
 			if (mon_conn) {
 				if (wpa_ctrl_attach(mon_conn) == 0) {
@@ -4269,6 +4275,7 @@ static void wpa_cli_action(struct wpa_ctrl *ctrl)
 	int fd;
 
 	fd = wpa_ctrl_get_fd(ctrl);
+
 	eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
 			       ctrl, NULL);
 	eloop_register_read_sock(fd, wpa_cli_action_receive, ctrl, NULL);
@@ -4369,7 +4376,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:mp:P:s:v");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -4391,6 +4398,9 @@ int main(int argc, char *argv[])
 		case 'v':
 			printf("%s\n", wpa_cli_version);
 			return 0;
+		case 'm':
+			do_monitor = 1;
+			break;
 		case 'i':
 			os_free(ctrl_ifname);
 			ctrl_ifname = os_strdup(optarg);
@@ -4415,7 +4425,7 @@ int main(int argc, char *argv[])
 	 */
 	setbuf(stdout, NULL);
 
-	interactive = (argc == optind) && (action_file == NULL);
+	interactive = (argc == optind) && (action_file == NULL) && !do_monitor;
 
 	if (interactive)
 		printf("%s\n\n%s\n\n", wpa_cli_version, cli_license);
@@ -4436,8 +4446,9 @@ int main(int argc, char *argv[])
 			return -1;
 		}
 
-		if (interactive) {
-			update_ifnames(ctrl_conn);
+		if (interactive || do_monitor) {
+			if (interactive)
+				update_ifnames(ctrl_conn);
 			mon_conn = wpa_ctrl_open(global);
 			if (mon_conn) {
 				if (wpa_ctrl_attach(mon_conn) == 0) {
@@ -4458,7 +4469,7 @@ int main(int argc, char *argv[])
 
 	eloop_register_signal_terminate(wpa_cli_terminate, NULL);
 
-	if (ctrl_ifname == NULL)
+	if (ctrl_ifname == NULL && !do_monitor)
 		ctrl_ifname = wpa_cli_get_default_ifname();
 
 	if (interactive) {
@@ -4473,7 +4484,7 @@ int main(int argc, char *argv[])
 			return -1;
 		}
 
-		if (action_file) {
+		if (action_file || do_monitor) {
 			if (wpa_ctrl_attach(ctrl_conn) == 0) {
 				wpa_cli_attached = 1;
 			} else {
@@ -4486,7 +4497,7 @@ int main(int argc, char *argv[])
 		if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
 			return -1;
 
-		if (action_file)
+		if (action_file || do_monitor)
 			wpa_cli_action(ctrl_conn);
 		else
 			ret = wpa_request(ctrl_conn, argc - optind,
-- 
2.7.5




More information about the Hostap mailing list