netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eugene Crosser <crosser@average.org>
To: netfilter-devel@vger.kernel.org
Cc: Eugene Crosser <crosser@average.org>
Subject: [PATCH nft v2 2/2] Handle retriable errors from mnl functions
Date: Thu,  9 Dec 2021 19:26:07 +0100	[thread overview]
Message-ID: <20211209182607.18550-3-crosser@average.org> (raw)
In-Reply-To: <20211209182607.18550-1-crosser@average.org>

rc == -1 and errno == EINTR mean:

mnl_socket_recvfrom() - blindly rerun the function
mnl_cb_run()          - restart dump request from scratch

This commit introduces handling of both these conditions

Signed-off-by: Eugene Crosser <crosser@average.org>
---
 src/iface.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/iface.c b/src/iface.c
index d0e1834c..5ecc087d 100644
--- a/src/iface.c
+++ b/src/iface.c
@@ -59,14 +59,14 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
 	return MNL_CB_OK;
 }
 
-void iface_cache_update(void)
+static int __iface_cache_update(void)
 {
 	char buf[MNL_SOCKET_BUFFER_SIZE];
 	struct mnl_socket *nl;
 	struct nlmsghdr *nlh;
 	struct rtgenmsg *rt;
 	uint32_t seq, portid;
-	int ret;
+	int ret = -1;
 
 	nlh = mnl_nlmsg_put_header(buf);
 	nlh->nlmsg_type	= RTM_GETLINK;
@@ -77,28 +77,39 @@ void iface_cache_update(void)
 
 	nl = mnl_socket_open(NETLINK_ROUTE);
 	if (nl == NULL)
-		netlink_init_error();
+		return -1;
 
 	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
-		netlink_init_error();
+		goto close_and_return;
 
 	portid = mnl_socket_get_portid(nl);
 
 	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0)
-		netlink_init_error();
+		goto close_and_return;
 
-	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	while (ret > 0) {
-		ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
-		if (ret <= MNL_CB_STOP)
+	do {
+		do ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+		while (ret == -1 && errno == EINTR);
+		if (ret == -1)
 			break;
-		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	}
-	if (ret == -1)
-		netlink_init_error();
+		ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
+	} while (ret > MNL_CB_STOP);
 
+close_and_return:
 	mnl_socket_close(nl);
 
+	return ret;
+}
+
+void iface_cache_update(void)
+{
+	int ret;
+
+	do ret = __iface_cache_update();
+	while (ret == -1 && errno == EINTR);
+	if (ret == -1)
+		netlink_init_error();
+
 	iface_cache_init = true;
 }
 
-- 
2.32.0


  parent reply	other threads:[~2021-12-09 18:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 18:26 [PATCH nft v2 0/2] Improve handling of errors from mnl* functions" Eugene Crosser
2021-12-09 18:26 ` [PATCH nft v2 1/2] Use abort() in case of netlink_abi_error Eugene Crosser
2022-01-26  1:11   ` Pablo Neira Ayuso
2021-12-09 18:26 ` Eugene Crosser [this message]
2022-01-27 18:20   ` [PATCH nft v2 2/2] Handle retriable errors from mnl functions Pablo Neira Ayuso
2022-01-27 18:39     ` Eugene Crosser
2022-01-27 23:04       ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211209182607.18550-3-crosser@average.org \
    --to=crosser@average.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).