]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
mnl: Simplify mnl_batch_talk()
authorPhil Sutter <phil@nwl.cc>
Fri, 31 May 2019 14:17:43 +0000 (16:17 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 31 May 2019 16:09:39 +0000 (18:09 +0200)
By mimicking mnl_nft_event_listener() code, mnl_batch_talk() may be
simplified quite a bit:

* Turn the conditional loop into an unconditional one.
* Call select() at loop start, which merges the two call sites.
* Check readfds content after select() returned instead of in loop
  condition - if fd is not set, break to return error state stored in
  'err' variable.
* Old code checked that select() return code is > 0, but that was
  redundant: if FD_ISSET() returns true, select return code was 1.
* Move 'nlh' helper variable definition into error handling block, it is
  not used outside of it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/mnl.c

index b32e4190b150507e82ed4c77954e03a90089342b..579210e4736a4c6ee49389b8b1da19ca69bea069 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -329,16 +329,17 @@ int mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list,
        if (ret == -1)
                return -1;
 
-       FD_ZERO(&readfds);
-       FD_SET(fd, &readfds);
-
        /* receive and digest all the acknowledgments from the kernel. */
-       ret = select(fd+1, &readfds, NULL, NULL, &tv);
-       if (ret == -1)
-               return -1;
+       while (true) {
+               FD_ZERO(&readfds);
+               FD_SET(fd, &readfds);
 
-       while (ret > 0 && FD_ISSET(fd, &readfds)) {
-               struct nlmsghdr *nlh = (struct nlmsghdr *)rcv_buf;
+               ret = select(fd + 1, &readfds, NULL, NULL, &tv);
+               if (ret == -1)
+                       return -1;
+
+               if (!FD_ISSET(fd, &readfds))
+                       break;
 
                ret = mnl_socket_recvfrom(nl, rcv_buf, sizeof(rcv_buf));
                if (ret == -1)
@@ -346,15 +347,11 @@ int mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list,
 
                ret = mnl_cb_run(rcv_buf, ret, 0, portid, &netlink_echo_callback, ctx);
                /* Continue on error, make sure we get all acknowledgments */
-               if (ret == -1)
-                       mnl_err_list_node_add(err_list, errno, nlh->nlmsg_seq);
-
-               FD_ZERO(&readfds);
-               FD_SET(fd, &readfds);
+               if (ret == -1) {
+                       struct nlmsghdr *nlh = (struct nlmsghdr *)rcv_buf;
 
-               ret = select(fd+1, &readfds, NULL, NULL, &tv);
-               if (ret == -1)
-                       return -1;
+                       mnl_err_list_node_add(err_list, errno, nlh->nlmsg_seq);
+               }
        }
        return 0;
 }