From patchwork Thu Dec 9 01:06:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1565558 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20210112 header.b=ZKsaP2w5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::538; helo=mail-pg1-x538.google.com; envelope-from=swupdate+bncbcwivbv7sugrbpfnywgqmgqeuuudkea@googlegroups.com; receiver=) Received: from mail-pg1-x538.google.com (mail-pg1-x538.google.com [IPv6:2607:f8b0:4864:20::538]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J8bV84VjVz9t54 for ; Thu, 9 Dec 2021 12:07:11 +1100 (AEDT) Received: by mail-pg1-x538.google.com with SMTP id j3-20020a634a43000000b00325af3ab5f0sf2266384pgl.11 for ; Wed, 08 Dec 2021 17:07:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1639012029; cv=pass; d=google.com; s=arc-20160816; b=a73eVfjjx11029ddq5aP1cFs6/uh55HgWbYH9PfpYuNfXFnLHJ7wvxn4Rdf6PyW0Ha bkz7Yri612rLa7ffpoer+fPw8Qw/LH8LbvL7nsErvv2tYWXz5rdjThpAJeIVJ4OEoVZT 6QYFrgTOrknK/0ORXP4hrwKScY0e/gQlUia72peYVXHnboV+4746I56wvM0WSu/HoqVy QnPpyuD8dtdNLeW1kHngPY2cCOA6XWnNOceIbWbRV5RrWIde53BdHQRhOGj/aRYuPURZ blDxvzTjY/ivTO0GBSwKUkIXKxzvTAxGZngBQDzdE9g+Gai4T1/BKdFDAcN4b2pMzAsI NHdg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=1IIuqjwfP4FH9nQrHAdFVrsEKdR+BFhNFJMxTR5gc3U=; b=mMfspyAanCNbI9T/2/THNml8IkpGlrLVjpIG2BtbFveSvl5Wy1tNM3QjaA3sTLf8Mm kwpyaZloLqGTjwUX7Rx7B5uKKG2p0G4u9FTL7MQE+F2bPXkm+cTBUDMnEJ9lpZ+Bjh08 0q98mN3i6sRWMEOHtBqP6+6ZhgzXvCkvC21oTCp3GerBe4PhmpKOWqTq5Yek4ALktMob hrOZlPy7vOvHaQ9ARwj+w898SBwkr+nFOHi6EvfHXP5LT1A2NiKp3aS7txak+BbUwt3r ise83ghkms8otRyl6bznZ/ERGdmOJVZaxe10tLt+nRU25i5NeGuXMEn0h1U4n7anNfBS ZL1w== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=1IIuqjwfP4FH9nQrHAdFVrsEKdR+BFhNFJMxTR5gc3U=; b=ZKsaP2w51WaIakV1yXOBW3Ba0ZAAwJv8KXNhyd0YZZjjfqg7E0BpdnqOfdOaKLLnQp 0rN3FxN5F/qTM+0v/17jepHcy0zLmSJO5L5p4o1fhTLM5i2RxRfyn92m8W3ppfErvD0C kgFVlkHdQKgLVofE6xaF4PhgYzkUyiMoqT8qjb09topoVfgPNzIRnfDPppCMRYd2SwzY FtGrt2MuA+DTPhMggm5R34VwjkPp7gFOB1Crd0GVpvLKLADwWpBy4BYfdjNfAEnj1dx+ NbCwXdRjUoLO/PWGQPxSaC72Jtsq6wR7l16752BYB0XxTntuKF+2rIrI/nFup3YYZPPQ P11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=1IIuqjwfP4FH9nQrHAdFVrsEKdR+BFhNFJMxTR5gc3U=; b=iWTJNIOL6qFrikzVqa9RmVi1fyS0+rb6Nd+EPpKgP4Y0mQZF7PbQgZYBeXuUapFXET yrMtchjhOwtfwbdqKIe0Gzd1+7pEmEK5pDzHORrxV+34dYuaIKjwNvx9RYvO8IQKjiyX 0oFej0KHyD0I7ENv7I6pt2nOmfH9FFApqaEgSrbpeOdrhY2IuCwz8BPagwmAbWdje67u u9fmG0/1gHvSicGZoeECMnRBBHRpTrp8lEayNrIlpg3QWtW3Z6irNyayy4l22jeh+31I 9vKX75s++HZVe3qHbao1GRHoZXNscs+JVGMZHf0fKCs6kkxg8wPi/GoNuw1RZ2Mo7+RG 5siw== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532bff5bu7MX/fO7GmtGbdA7WauP3Yy+eGvlquqINJwcaNpAkvES TvSNpUpcazOPmtVpCjq8T6Q= X-Google-Smtp-Source: ABdhPJzVYzpYGCyR3YtzPpUAtf3k5j9l6AQ+fmSCVOCaE/sGo8yetbl9W6ACcn9dDU00W1PDRUYFXw== X-Received: by 2002:a17:90b:1643:: with SMTP id il3mr11485182pjb.182.1639012029180; Wed, 08 Dec 2021 17:07:09 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a17:902:e749:: with SMTP id p9ls2089686plf.6.gmail; Wed, 08 Dec 2021 17:07:08 -0800 (PST) X-Received: by 2002:a17:903:1103:b0:143:a593:dc41 with SMTP id n3-20020a170903110300b00143a593dc41mr64653548plh.5.1639012028415; Wed, 08 Dec 2021 17:07:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639012028; cv=none; d=google.com; s=arc-20160816; b=v46XAbPRx+JG5rxhCJ4Ey4bS4Ekubfcl3+YJNVxGGFE2TR18HsDcs0RWmdvtU9eBcd asSarzFCgaOcKqTj/iqeWVUw/S3LSXp/NnGJqu2O2PJKCviv8zRLdv9zD6C6KmoNSfrk FGGQfk8sbh9eKn/Dqn6Fx6FV29ZFlYUsri06oWfzBW8ISITU58rmtE7y9bIjVzxqCgs7 /+3dV5Dhvroi7WMvAMiK9phiCD443eBnvjBxdPGn9IQKamXKSqHX19LYcaYmvLAbZiRY vt3k/t0Wh7Aodw32YUvaPvI5Rb92IRVodejpRayA9E8yWj+kvrxnd3M8O9gGuyMl7mCt SNcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from; bh=rgX8DPnIovz5UGGJF0kh3oJnGtmiS3d0eegvtewqQhc=; b=1C4/VCe69/MkbfFpsE0FnfyW7nNVK9b4OvFsdvn5AMWxAjz4bQ5C1+0AY9HYvkz8qf 9u/zV5Ux9Pnsz0bxHNVSvFg1DGSiDxOYHRSCZMyc0Gz/EjVeaD9y2cLhyXBC+GbnSH3V JEqr0AObiiJjZkBsOhR3HzfwWfTNnv6Yf1Nj9RA9Zag/IAquQD0BZjOCm8nw9K2DgDAV kKruiojIHjdx8WbFKr4nWafziznZe4DA2OSejB0ySN60Ay8ZsY/UGi6U8tP6db7pOAOx LZyxaK1tHG1QlR6wiFAwJimIWJQNhFp/dzdzMMDBd19ORA1XW49TpqIZ83g5OXvWkFPi oiYQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw2.atmark-techno.com (gw2.atmark-techno.com. [35.74.137.57]) by gmr-mx.google.com with ESMTP id w4si502607pjr.3.2021.12.08.17.07.08 for ; Wed, 08 Dec 2021 17:07:08 -0800 (PST) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) client-ip=35.74.137.57; Received: from gw2.atmark-techno.com (localhost [127.0.0.1]) by gw2.atmark-techno.com (Postfix) with ESMTP id 45E3320D3E for ; Thu, 9 Dec 2021 10:07:07 +0900 (JST) Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 3113420D3E for ; Thu, 9 Dec 2021 10:07:07 +0900 (JST) Received: by mail-pf1-f198.google.com with SMTP id y5-20020a62b505000000b004a804839861so2578793pfe.7 for ; Wed, 08 Dec 2021 17:07:07 -0800 (PST) X-Received: by 2002:a17:903:41cb:b0:142:62a:4d86 with SMTP id u11-20020a17090341cb00b00142062a4d86mr63278234ple.43.1639012026115; Wed, 08 Dec 2021 17:07:06 -0800 (PST) X-Received: by 2002:a17:903:41cb:b0:142:62a:4d86 with SMTP id u11-20020a17090341cb00b00142062a4d86mr63278213ple.43.1639012025873; Wed, 08 Dec 2021 17:07:05 -0800 (PST) Received: from pc-0115 (103.131.189.35.bc.googleusercontent.com. [35.189.131.103]) by smtp.gmail.com with ESMTPSA id o134sm4517701pfg.1.2021.12.08.17.07.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Dec 2021 17:07:05 -0800 (PST) Received: from martinet by pc-0115 with local (Exim 4.95) (envelope-from ) id 1mv7tn-0032Uf-UL; Thu, 09 Dec 2021 10:07:03 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH v2 1/4] ipc_notify_receive: add timeout_ms argument Date: Thu, 9 Dec 2021 10:06:56 +0900 Message-Id: <20211209010659.724523-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , 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 --- v2: trivial rebase on master include/network_ipc.h | 4 +- ipc/network_ipc.c | 86 ++++++++++++++++------------------- mongoose/mongoose_interface.c | 2 +- 3 files changed, 43 insertions(+), 49 deletions(-) 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 851a17bfbaf8..930cbf393a12 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;