diff mbox series

[6/6] socket split: adapt tools

Message ID 20170906072324.15734-6-christian.storm@siemens.com
State Changes Requested
Headers show
Series None | expand

Commit Message

Storm, Christian Sept. 6, 2017, 7:23 a.m. UTC
Adapt the tool executables to use the introduced
progress_ipc{.c,.h} and to respect a --socket command
line switch to point them to the socket location in case
the configuration deviates from the default.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 tools/hawkbitcfg.c    | 32 ++++++++++++++++++++++++++---
 tools/progress.c      | 52 +++++++++--------------------------------------
 tools/sendtohawkbit.c | 56 +++++++++++++++++++++++++++++++++++----------------
 3 files changed, 77 insertions(+), 63 deletions(-)
diff mbox series

Patch

diff --git a/tools/hawkbitcfg.c b/tools/hawkbitcfg.c
index b6c2122..6f0bc54 100644
--- a/tools/hawkbitcfg.c
+++ b/tools/hawkbitcfg.c
@@ -32,12 +32,19 @@ 
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <getopt.h>
 #include "network_ipc.h"
 
 static void usage(char *program) {
-	printf("%s <polling interval 0=from server> ..\n", program);
+	printf("%s [-s <socket_path>] <polling interval 0=from server> ..\n", program);
 }
 
+static struct option long_options[] = {
+	{"help", no_argument, NULL, 'h'},
+	{"socket", required_argument, NULL, 's'},
+	{NULL, 0, NULL, 0}
+};
+
 /*
  * Simple example, it does nothing but calling the library
  */
@@ -47,7 +54,26 @@  int main(int argc, char *argv[]) {
 	size_t size;
 	char *buf;
 
-	if (argc < 2) {
+	int c;
+
+	while ((c = getopt_long(argc, argv, "hs:",
+				long_options, NULL)) != EOF) {
+		switch (c) {
+		case 's':
+			SOCKET_CTRL_PATH = strdup(optarg);
+			break;
+		case 'h':
+			usage(argv[0]);
+			exit(0);
+			break;
+		default:
+			usage(argv[0]);
+			exit(1);
+			break;
+		}
+	}
+
+	if ((argc - optind) < 1) {
 		usage(argv[0]);
 		exit(1);
 	}
@@ -67,7 +93,7 @@  int main(int argc, char *argv[]) {
 	 * case of failure
 	 */
 
-	snprintf(buf, size, "{ \"polling\" : \"%lu\"}", strtoul(argv[1], NULL, 10));
+	snprintf(buf, size, "{ \"polling\" : \"%lu\"}", strtoul(argv[optind], NULL, 10));
 
 	fprintf(stdout, "Sending: '%s'", msg.data.instmsg.buf);
 
diff --git a/tools/progress.c b/tools/progress.c
index fe9f8ef..4db0b22 100644
--- a/tools/progress.c
+++ b/tools/progress.c
@@ -36,7 +36,7 @@ 
 #include <pthread.h>
 #include <getopt.h>
 
-#include <progress.h>
+#include <progress_ipc.h>
 
 #define PSPLASH_MSG_SIZE	64
 
@@ -76,6 +76,7 @@  static struct option long_options[] = {
 	{"reboot", no_argument, NULL, 'r'},
 	{"wait", no_argument, NULL, 'w'},
 	{"color", no_argument, NULL, 'c'},
+	{"socket", required_argument, NULL, 's'},
 	{NULL, 0, NULL, 0}
 };
 
@@ -89,6 +90,7 @@  static void usage(char *programname)
 		" -r, --reboot            : reboot after a successful update\n"
 		" -w, --wait              : wait for a connection with SWUpdate\n"
 		" -p, --psplash           : send info to the psplash process\n"
+		" -s, --socket <path>     : path to progress IPC socket\n"
 		" -h, --help              : print this help and exit\n"
 		);
 }
@@ -177,45 +179,10 @@  static void psplash_progress(char *pipe, struct progress_msg *pmsg)
 	free(buf);
 }
 
-static int connect_to_swupdate(int reconnect)
-{
-	struct sockaddr_un servaddr;
-	int fd, ret;
-
-	/*
-	 * The thread read from swupdate progress thread
-	 * and forward messages to psplash
-	 */
-	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
-	bzero(&servaddr, sizeof(servaddr));
-	servaddr.sun_family = AF_LOCAL;
-	strcpy(servaddr.sun_path, SOCKET_PROGRESS_PATH);
-
-	fprintf(stdout, "Trying to connect to SWUpdate...\n");
-
-	/* Connection to SWUpdate */
-	do {
-		ret = connect(fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
-		if (ret == 0)
-			break;
-		if (!reconnect) {
-			fprintf(stderr, "no communication with swupdate\n");
-			exit(1);
-		}
-
-		usleep(10000);
-	} while (1);
-
-	fprintf(stdout, "Connected\n");
-
-	return fd;
-}
-
 int main(int argc, char **argv)
 {
 	int connfd;
 	struct progress_msg msg;
-	int ret;
 	const char *tmpdir;
 	char psplash_pipe_path[256];
 	int psplash_ok = 0;
@@ -231,7 +198,7 @@  int main(int argc, char **argv)
 	RECOVERY_STATUS	status = IDLE;		/* Update Status (Running, Failure) */
 
 	/* Process options with getopt */
-	while ((c = getopt_long(argc, argv, "cwprh",
+	while ((c = getopt_long(argc, argv, "cwprhs:",
 				long_options, NULL)) != EOF) {
 		switch (c) {
 		case 'c':
@@ -246,6 +213,9 @@  int main(int argc, char **argv)
 		case 'r':
 			opt_r = 1;
 			break;
+		case 's':
+			SOCKET_PROGRESS_PATH = strdup(optarg);
+			break;
 		case 'h':
 			usage(argv[0]);
 			exit(0);
@@ -266,14 +236,10 @@  int main(int argc, char **argv)
 	connfd = -1;
 	while (1) {
 		if (connfd < 0) {
-			connfd = connect_to_swupdate(opt_w);
+			connfd = progress_ipc_connect(opt_w);
 		}
 
-		ret = read(connfd, &msg, sizeof(msg));
-		if (ret != sizeof(msg)) {
-			fprintf(stdout, "Connection closing..\n");
-			close(connfd);
-			connfd = -1;
+		if (progress_ipc_receive(&connfd, &msg) == -1) {
 			continue;
 		}
 
diff --git a/tools/sendtohawkbit.c b/tools/sendtohawkbit.c
index c866507..2f8e34b 100644
--- a/tools/sendtohawkbit.c
+++ b/tools/sendtohawkbit.c
@@ -32,15 +32,22 @@ 
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <getopt.h>
 #include "network_ipc.h"
 
 static void usage(char *program) {
-	printf("%s <action id> <status> <finished> <execution> <detail 1> <detail 2> ..\n", program);
+	printf("%s [-s <socket_path>] <action id> <status> <finished> <execution> <detail 1> <detail 2> ..\n", program);
 }
 
 int fd;
 int verbose = 1;
 
+static struct option long_options[] = {
+	{"help", no_argument, NULL, 'h'},
+	{"socket", required_argument, NULL, 's'},
+	{NULL, 0, NULL, 0}
+};
+
 /*
  * Simple example, it does nothing but calling the library
  */
@@ -49,8 +56,26 @@  int main(int argc, char *argv[]) {
 	ipc_message msg;
 	size_t size;
 	char *buf;
+	int c;
+
+	while ((c = getopt_long(argc, argv, "hs:",
+				long_options, NULL)) != EOF) {
+		switch (c) {
+		case 's':
+			SOCKET_CTRL_PATH = strdup(optarg);
+			break;
+		case 'h':
+			usage(argv[0]);
+			exit(0);
+			break;
+		default:
+			usage(argv[0]);
+			exit(1);
+			break;
+		}
+	}
 
-	if (argc < 3) {
+	if ((argc - optind) < 2) {
 		usage(argv[0]);
 		exit(1);
 	}
@@ -69,26 +94,23 @@  int main(int argc, char *argv[]) {
 	 * An error or a NACK is returned in
 	 * case of failure
 	 */
-	for (i = 1; i < argc; i++) {
-		switch (i) {
-		case 1:
+	for (i = optind; i < argc; i++) {
+		if (i == optind) {
 			written = snprintf(buf, size, "{ \"id\" : \"%lu\"", strtoul(argv[i], NULL, 10));
-			break;
-		case 2:
+		} else
+		if (i == optind+1) {
 			written = snprintf(buf, size, ", \"status\" : \"%s\"", argv[i]);
-			break;
-		case 3:
+		} else
+		if (i == optind+2) {
 			written = snprintf(buf, size, ",\"finished\" : \"%s\"", argv[i]);
-			break;
-		case 4:
+		} else
+		if (i == optind+3) {
 			written = snprintf(buf, size, ",\"execution\" : \"%s\"", argv[i]);
-			break;
-		case 5:
+		} else
+		if (i == optind+4) {
 			written = snprintf(buf, size, ",\"details\" : [ \"%s\"", argv[i]);
-			break;
-		default:
+		} else {
 			written = snprintf(buf, size, ",\"%s\"", argv[i]);
-			break;
 		}
 
 		buf += written;
@@ -98,7 +120,7 @@  int main(int argc, char *argv[]) {
 			break;
 	}
 
-	if (i > 4)
+	if (i > optind+4)
 		written = snprintf(buf, size, "]}");
 	else
 		written = snprintf(buf, size, "}");