diff mbox series

swupdate-ipc: added monitor command

Message ID 20220517100350.219554-1-michael.adler@siemens.com
State Changes Requested
Headers show
Series swupdate-ipc: added monitor command | expand

Commit Message

Michael Adler May 17, 2022, 10:03 a.m. UTC
The monitor command prints all IPC messages to stdout, thus making it
suitable for debugging or scripting purposes.

Signed-off-by: Michael Adler <michael.adler@siemens.com>
Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 tools/swupdate-ipc.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

Comments

Stefano Babic May 17, 2022, 1:54 p.m. UTC | #1
On 17.05.22 12:03, Michael Adler wrote:
> The monitor command prints all IPC messages to stdout, thus making it
> suitable for debugging or scripting purposes.
> 
> Signed-off-by: Michael Adler <michael.adler@siemens.com>
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>   tools/swupdate-ipc.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 81 insertions(+)
> 
> diff --git a/tools/swupdate-ipc.c b/tools/swupdate-ipc.c
> index 789649f..651fb62 100644
> --- a/tools/swupdate-ipc.c
> +++ b/tools/swupdate-ipc.c
> @@ -83,6 +83,14 @@ static void usage_hawkbitcfg(const char *program) {
>   		);
>   }
>   
> +static void usage_monitor(const char *program) {
> +	fprintf(stdout,"\t %s \n", program);
> +	fprintf(stdout,
> +		"\t\t-s, --socket <path>     : path to progress IPC socket\n"
> +		"\t\t-h, --help              : print this help and exit\n"
> +		);
> +}
> +
>   /*
>    * Utility functions called by subcommands
>    */
> @@ -589,6 +597,78 @@ static int sysrestart(cmd_t __attribute__((__unused__)) *cmd,
>   }
>   #endif
>   
> +static struct option monitor_options[] = {
> +	{"help", no_argument, NULL, 'h'},
> +	{"socket", required_argument, NULL, 's'},
> +	{NULL, 0, NULL, 0}
> +};
> +
> +static int monitor(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *argv[]) {
> +	char *socket_path = NULL;
> +
> +	/* Process options with getopt */
> +	int c;
> +	while ((c = getopt_long(argc, argv, "hs:", monitor_options, NULL)) != EOF) {
> +		switch (c) {
> +		case 's':
> +			socket_path = strdup(optarg);
> +			break;
> +		case 'h':
> +			usage_monitor(argv[0]);
> +			exit(0);
> +			break;
> +		default:
> +			usage_monitor(argv[0]);
> +			exit(1);
> +			break;
> +		}
> +	}
> +
> +	int connfd = -1;
> +	struct progress_msg msg;
> +	while (1) {
> +		if (connfd < 0) {
> +			if (!socket_path)
> +				connfd = progress_ipc_connect(true);
> +			else
> +				connfd = progress_ipc_connect_with_path(socket_path, true);
> +		}
> +
> +		/*
> +		 * if still fails, try later
> +		 */
> +		if (connfd < 0) {
> +			sleep(1);
> +			continue;
> +		}
> +
> +		if (progress_ipc_receive(&connfd, &msg) <= 0) {
> +			continue;
> +		}
> +
> +		if (msg.infolen > 0) {
> +			/*
> +			 * check that msg is NULL terminated
> +			 */
> +			if (msg.infolen > sizeof(msg.info) - 1) {
> +				msg.infolen = sizeof(msg.info) - 1;
> +			}
> +			msg.info[msg.infolen] = '\0';
> +		}
> +
> +		fprintf(stdout, "[{ \"magic\": %d, \"status\": %u, \"dwl_percent\": %u, \"dwl_bytes\": %llu"
> +				", \"nsteps\": %u, \"cur_step\": %u, \"cur_percent\": %u, \"cur_image\": \"%s\""
> +				", \"hnd_name\": \"%s\", \"source\": %u, \"infolen\": %u }",
> +				msg.magic, msg.status, msg.dwl_percent,
> +				msg.dwl_bytes, msg.nsteps, msg.cur_step,
> +				msg.cur_percent, msg.cur_image, msg.hnd_name,
> +				msg.source, msg.infolen);
> +                if (msg.infolen > 0) fprintf(stdout, ", %s]\n", msg.info); else fprintf(stdout, "]\n");
> +
> +		fflush(stdout);
> +	}
> +	return 0;
> +}
>   
>   /*
>    * List of implemented commands
> @@ -600,6 +680,7 @@ cmd_t commands[] = {
>   	{"hawkbitcfg", hawkbitcfg, usage_hawkbitcfg},
>   	{"gethawkbit", gethawkbitstatus, usage_gethawkbitstatus},
>   	{"sysrestart", sysrestart, usage_sysrestart},
> +	{"monitor", monitor, usage_monitor},
>   	{NULL, NULL, NULL}
>   };
>   

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/tools/swupdate-ipc.c b/tools/swupdate-ipc.c
index 789649f..651fb62 100644
--- a/tools/swupdate-ipc.c
+++ b/tools/swupdate-ipc.c
@@ -83,6 +83,14 @@  static void usage_hawkbitcfg(const char *program) {
 		);
 }
 
+static void usage_monitor(const char *program) {
+	fprintf(stdout,"\t %s \n", program);
+	fprintf(stdout,
+		"\t\t-s, --socket <path>     : path to progress IPC socket\n"
+		"\t\t-h, --help              : print this help and exit\n"
+		);
+}
+
 /*
  * Utility functions called by subcommands
  */
@@ -589,6 +597,78 @@  static int sysrestart(cmd_t __attribute__((__unused__)) *cmd,
 }
 #endif
 
+static struct option monitor_options[] = {
+	{"help", no_argument, NULL, 'h'},
+	{"socket", required_argument, NULL, 's'},
+	{NULL, 0, NULL, 0}
+};
+
+static int monitor(cmd_t  __attribute__((__unused__)) *cmd, int argc, char *argv[]) {
+	char *socket_path = NULL;
+
+	/* Process options with getopt */
+	int c;
+	while ((c = getopt_long(argc, argv, "hs:", monitor_options, NULL)) != EOF) {
+		switch (c) {
+		case 's':
+			socket_path = strdup(optarg);
+			break;
+		case 'h':
+			usage_monitor(argv[0]);
+			exit(0);
+			break;
+		default:
+			usage_monitor(argv[0]);
+			exit(1);
+			break;
+		}
+	}
+
+	int connfd = -1;
+	struct progress_msg msg;
+	while (1) {
+		if (connfd < 0) {
+			if (!socket_path)
+				connfd = progress_ipc_connect(true);
+			else
+				connfd = progress_ipc_connect_with_path(socket_path, true);
+		}
+
+		/*
+		 * if still fails, try later
+		 */
+		if (connfd < 0) {
+			sleep(1);
+			continue;
+		}
+
+		if (progress_ipc_receive(&connfd, &msg) <= 0) {
+			continue;
+		}
+
+		if (msg.infolen > 0) {
+			/*
+			 * check that msg is NULL terminated
+			 */
+			if (msg.infolen > sizeof(msg.info) - 1) {
+				msg.infolen = sizeof(msg.info) - 1;
+			}
+			msg.info[msg.infolen] = '\0';
+		}
+
+		fprintf(stdout, "[{ \"magic\": %d, \"status\": %u, \"dwl_percent\": %u, \"dwl_bytes\": %llu"
+				", \"nsteps\": %u, \"cur_step\": %u, \"cur_percent\": %u, \"cur_image\": \"%s\""
+				", \"hnd_name\": \"%s\", \"source\": %u, \"infolen\": %u }",
+				msg.magic, msg.status, msg.dwl_percent,
+				msg.dwl_bytes, msg.nsteps, msg.cur_step,
+				msg.cur_percent, msg.cur_image, msg.hnd_name,
+				msg.source, msg.infolen);
+                if (msg.infolen > 0) fprintf(stdout, ", %s]\n", msg.info); else fprintf(stdout, "]\n");
+
+		fflush(stdout);
+	}
+	return 0;
+}
 
 /*
  * List of implemented commands
@@ -600,6 +680,7 @@  cmd_t commands[] = {
 	{"hawkbitcfg", hawkbitcfg, usage_hawkbitcfg},
 	{"gethawkbit", gethawkbitstatus, usage_gethawkbitstatus},
 	{"sysrestart", sysrestart, usage_sysrestart},
+	{"monitor", monitor, usage_monitor},
 	{NULL, NULL, NULL}
 };