diff mbox series

[1/4] ipc_notify_receive: add timeout_ms argument

Message ID 20211122031619.2202449-1-dominique.martinet@atmark-techno.com
State Changes Requested
Headers show
Series [1/4] ipc_notify_receive: add timeout_ms argument | expand

Commit Message

Dominique Martinet Nov. 22, 2021, 3:16 a.m. UTC
allow checking if we have a notify message pending with a timeout.
Since we want timeout=0 to mean "check without waiting", also make
other timeout_ms in this file logic match for coherence:
 - timeout < 0 = wait forever
 - timeout >= 0 = pass timeout to select()

Since we now have 3 places in the code that basically do the same
optional select, move this to its own __ipc_select_timeout helper

Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
---
The uniformisation is somewhat arbitrary, but the following patch
requires a select with immediate timeout as we'll call it in between
sending image in file sending loop

 include/network_ipc.h         |  4 +-
 ipc/network_ipc.c             | 86 ++++++++++++++++-------------------
 mongoose/mongoose_interface.c |  2 +-
 3 files changed, 43 insertions(+), 49 deletions(-)
diff mbox series

Patch

diff --git a/include/network_ipc.h b/include/network_ipc.h
index 6dc2521d827f..356931bc4e5c 100644
--- a/include/network_ipc.h
+++ b/include/network_ipc.h
@@ -129,9 +129,9 @@  int ipc_inst_start_ext(void *priv, ssize_t size);
 int ipc_send_data(int connfd, char *buf, int size);
 void ipc_end(int connfd);
 int ipc_get_status(ipc_message *msg);
-int ipc_get_status_timeout(ipc_message *msg, unsigned int timeout_ms);
+int ipc_get_status_timeout(ipc_message *msg, int timeout_ms);
 int ipc_notify_connect(void);
-int ipc_notify_receive(int *connfd, ipc_message *msg);
+int ipc_notify_receive(int *connfd, ipc_message *msg, int timeout_ms);
 int ipc_postupdate(ipc_message *msg);
 int ipc_send_cmd(ipc_message *msg);
 
diff --git a/ipc/network_ipc.c b/ipc/network_ipc.c
index 5ccc18b6c6d4..39ca695ec75f 100644
--- a/ipc/network_ipc.c
+++ b/ipc/network_ipc.c
@@ -92,11 +92,32 @@  int ipc_postupdate(ipc_message *msg) {
 	return -result;
 }
 
-static int __ipc_get_status(int connfd, ipc_message *msg, unsigned int timeout_ms)
-{
+static int __ipc_select_timeout(int connfd, ipc_message *msg, int timeout_ms) {
 	fd_set fds;
 	struct timeval tv;
 
+	if (timeout_ms < 0)
+		return 0;
+	FD_ZERO(&fds);
+	FD_SET(connfd, &fds);
+
+	/*
+	 * Invalid the message
+	 * Caller should check it
+	 */
+	msg->magic = 0;
+
+	tv.tv_sec = 0;
+	tv.tv_usec = timeout_ms * 1000;
+	if ((select(connfd + 1, &fds, NULL, NULL, &tv) <= 0) ||
+			!FD_ISSET(connfd, &fds))
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+static int __ipc_get_status(int connfd, ipc_message *msg, int timeout_ms)
+{
 	memset(msg, 0, sizeof(*msg));
 	msg->magic = IPC_MAGIC;
 	msg->type = GET_STATUS;
@@ -104,22 +125,8 @@  static int __ipc_get_status(int connfd, ipc_message *msg, unsigned int timeout_m
 	if (write(connfd, msg, sizeof(*msg)) != sizeof(*msg))
 		return -1;
 
-	if (timeout_ms) {
-		FD_ZERO(&fds);
-		FD_SET(connfd, &fds);
-
-		/*
-		 * Invalid the message
-		 * Caller should check it
-		 */
-		msg->magic = 0;
-
-		tv.tv_sec = 0;
-		tv.tv_usec = timeout_ms * 1000;
-		if ((select(connfd + 1, &fds, NULL, NULL, &tv) <= 0) ||
-			!FD_ISSET(connfd, &fds))
-			return -ETIMEDOUT;
-	}
+	if (__ipc_select_timeout(connfd, msg, timeout_ms))
+		return -ETIMEDOUT;
 
 	return -(read(connfd, msg, sizeof(*msg)) != sizeof(*msg));
 }
@@ -133,7 +140,7 @@  int ipc_get_status(ipc_message *msg)
 	if (connfd < 0)
 		return -1;
 
-	ret = __ipc_get_status(connfd, msg, 0);
+	ret = __ipc_get_status(connfd, msg, -1);
 	close(connfd);
 
 	return ret;
@@ -144,7 +151,7 @@  int ipc_get_status(ipc_message *msg)
  *           -1 : error
  *           else data read
  */
-int ipc_get_status_timeout(ipc_message *msg, unsigned int timeout_ms)
+int ipc_get_status_timeout(ipc_message *msg, int timeout_ms)
 {
 	int ret;
 	int connfd;
@@ -163,11 +170,8 @@  int ipc_get_status_timeout(ipc_message *msg, unsigned int timeout_ms)
 	return ret == 0 ? sizeof(*msg) : -1;
 }
 
-static int __ipc_start_notify(int connfd, ipc_message *msg, unsigned int timeout_ms)
+static int __ipc_start_notify(int connfd, ipc_message *msg, int timeout_ms)
 {
-	fd_set fds;
-	struct timeval tv;
-
 	memset(msg, 0, sizeof(*msg));
 	msg->magic = IPC_MAGIC;
 	msg->type = NOTIFY_STREAM;
@@ -175,22 +179,8 @@  static int __ipc_start_notify(int connfd, ipc_message *msg, unsigned int timeout
 	if (write(connfd, msg, sizeof(*msg)) != sizeof(*msg))
 		return -1;
 
-	if (timeout_ms) {
-		FD_ZERO(&fds);
-		FD_SET(connfd, &fds);
-
-		/*
-		 * Invalid the message
-		 * Caller should check it
-		 */
-		msg->magic = 0;
-
-		tv.tv_sec = 0;
-		tv.tv_usec = timeout_ms * 1000;
-		if ((select(connfd + 1, &fds, NULL, NULL, &tv) <= 0) ||
-		!FD_ISSET(connfd, &fds))
-			return -ETIMEDOUT;
-	}
+	if (__ipc_select_timeout(connfd, msg, timeout_ms))
+		return -ETIMEDOUT;
 
 	return -(read(connfd, msg, sizeof(*msg)) != sizeof(*msg));
 }
@@ -208,7 +198,7 @@  int ipc_notify_connect(void)
 	/*
 	 * Initialize the notify stream
 	 */
-	ret = __ipc_start_notify(connfd, &msg, 0);
+	ret = __ipc_start_notify(connfd, &msg, -1);
 	if (ret || msg.type != ACK) {
 		fprintf(stdout, "Notify connection handshake failed..\n");
 		close(connfd);
@@ -218,10 +208,14 @@  int ipc_notify_connect(void)
 	return connfd;
 }
 
-int ipc_notify_receive(int *connfd, ipc_message *msg)
+int ipc_notify_receive(int *connfd, ipc_message *msg, int timeout_ms)
 {
-	int ret = read(*connfd, msg, sizeof(*msg));
+	int ret;
 
+	if (__ipc_select_timeout(*connfd, msg, timeout_ms))
+		return -ETIMEDOUT;
+
+	ret = read(*connfd, msg, sizeof(*msg));
 	if (ret == -1 && (errno == EAGAIN || errno == EINTR))
 		return 0;
 
@@ -229,14 +223,14 @@  int ipc_notify_receive(int *connfd, ipc_message *msg)
 		fprintf(stdout, "Connection closing..\n");
 		close(*connfd);
 		*connfd = -1;
-		return -1;
+		return -EIO;
 	}
 
 	if (msg->magic != IPC_MAGIC) {
 		fprintf(stdout, "Connection closing, invalid magic...\n");
 		close(*connfd);
 		*connfd = -1;
-		return -1;
+		return -EIO;
 	}
 
 	return ret;
@@ -330,7 +324,7 @@  int ipc_wait_for_complete(getstatus callback)
 		fd = prepare_ipc();
 		if (fd < 0)
 			break;
-		ret = __ipc_get_status(fd, &message, 0);
+		ret = __ipc_get_status(fd, &message, -1);
 		close(fd);
 
 		if (ret < 0) {
diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c
index 31548cccb254..16c7b3aaf828 100644
--- a/mongoose/mongoose_interface.c
+++ b/mongoose/mongoose_interface.c
@@ -173,7 +173,7 @@  static void *broadcast_message_thread(void *data)
 			continue;
 		}
 
-		ret = ipc_notify_receive(&fd, &msg);
+		ret = ipc_notify_receive(&fd, &msg, -1);
 		if (ret != sizeof(msg))
 			return NULL;