[OpenWrt-Devel] [PATCH resend] [ubus 2/2] cli: add timeout support for listen command

Zhao, Gang gang.zhao.42 at gmail.com
Fri Feb 26 04:18:39 EST 2016


Currently 'ubus listen' command does not respect the timeout parameter,
i.e., whether timeout parameter is provided, 'ubus listen' will always
run infinitely. This patch let 'ubus listen' command support the timeout
parameter. For example, 'ubus listen -t 60' will cause the command to
exit after 60 seconds.

Signed-off-by: Zhao, Gang <gang.zhao.42 at gmail.com>
---

If timeout parameter is not provided, listen command will exit after
30 seconds. This is not compatable with previous version, that is,
listen will blcok infinitely if timeout parameter is not
provided. Since it seems no user is using 'ubus listen' command in
openwrt code base, I think this may be acceptable, if not, I can
change the code so listen command will only exit if timeout
parameter is provided.

 cli.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/cli.c b/cli.c
index c476f35..24b5d22 100644
--- a/cli.c
+++ b/cli.c
@@ -125,15 +125,29 @@ static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
 	return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
 }
 
+struct cli_listen_data {
+	struct uloop_timeout timeout;
+	struct ubus_event_handler ev;
+	bool timed_out;
+};
+
+static void listen_timeout(struct uloop_timeout *timeout)
+{
+	struct cli_listen_data *data = container_of(timeout, struct cli_listen_data, timeout);
+	data->timed_out = true;
+	uloop_end();
+}
+
 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
 {
-	struct ubus_event_handler listener;
+	struct cli_listen_data data = {
+		.timeout.cb = listen_timeout,
+		.ev.cb = receive_event,
+		.timed_out = false,
+	};
 	const char *event;
 	int ret = 0;
 
-	memset(&listener, 0, sizeof(listener));
-	listener.cb = receive_event;
-
 	if (argc > 0) {
 		event = argv[0];
 	} else {
@@ -142,7 +156,7 @@ static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
 	}
 
 	do {
-		ret = ubus_register_event_handler(ctx, &listener, event);
+		ret = ubus_register_event_handler(ctx, &data.ev, event);
 		if (ret)
 			break;
 
@@ -163,6 +177,7 @@ static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
 
 	uloop_init();
 	ubus_add_uloop(ctx);
+	uloop_timeout_set(&data.timeout, timeout * 1000);
 	uloop_run();
 	uloop_done();
 
-- 
2.7.2
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list