Blame SOURCES/0382-util-introduce-CMSG_FOREACH-macro-and-make-use-of-it.patch

17b0f1
From 603edc22d0516044b72b09ed94a696edd2de7f37 Mon Sep 17 00:00:00 2001
17b0f1
From: Lennart Poettering <lennart@poettering.net>
17b0f1
Date: Wed, 10 Jun 2015 19:10:47 +0200
17b0f1
Subject: [PATCH] util: introduce CMSG_FOREACH() macro and make use of it
17b0f1
 everywhere
17b0f1
17b0f1
It's only marginally shorter then the usual for() loop, but certainly
17b0f1
more readable.
17b0f1
17b0f1
Cherry-picked from: 2a1288ff89322a2f49c79f6d1832c8164c14a05c
17b0f1
Related: #1318994
17b0f1
---
17b0f1
 src/core/manager.c                      |  2 +-
17b0f1
 src/core/namespace.c                    |  3 +--
17b0f1
 src/import/importd.c                    |  8 ++------
17b0f1
 src/journal/journald-server.c           |  2 +-
17b0f1
 src/libsystemd-network/sd-dhcp-client.c |  2 +-
17b0f1
 src/libsystemd-network/sd-dhcp-server.c |  2 +-
17b0f1
 src/libsystemd/sd-bus/bus-container.c   |  2 +-
17b0f1
 src/libsystemd/sd-bus/bus-socket.c      | 16 ++++++++++------
17b0f1
 src/libsystemd/sd-rtnl/rtnl-message.c   |  2 +-
17b0f1
 src/resolve/resolved-dns-stream.c       |  3 ++-
17b0f1
 src/resolve/resolved-manager.c          |  2 +-
17b0f1
 src/shared/macro.h                      |  3 +++
17b0f1
 src/shared/util.c                       | 12 +++++++++++-
17b0f1
 src/shared/util.h                       |  2 ++
17b0f1
 src/timesync/timesyncd-manager.c        |  2 +-
17b0f1
 15 files changed, 39 insertions(+), 24 deletions(-)
17b0f1
17b0f1
diff --git a/src/core/manager.c b/src/core/manager.c
17b0f1
index c5021993e5..71dd70c94c 100644
17b0f1
--- a/src/core/manager.c
17b0f1
+++ b/src/core/manager.c
17b0f1
@@ -1679,7 +1679,7 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
17b0f1
                 return -errno;
17b0f1
         }
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &msghdr) {
17b0f1
                 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
 
17b0f1
                         fd_array = (int*) CMSG_DATA(cmsg);
17b0f1
diff --git a/src/core/namespace.c b/src/core/namespace.c
17b0f1
index ebd5fb3347..00495c1446 100644
17b0f1
--- a/src/core/namespace.c
17b0f1
+++ b/src/core/namespace.c
17b0f1
@@ -658,12 +658,11 @@ int setup_netns(int netns_storage_socket[2]) {
17b0f1
         } else {
17b0f1
                 /* Yay, found something, so let's join the namespace */
17b0f1
 
17b0f1
-                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
17b0f1
+                CMSG_FOREACH(cmsg, &mh)
17b0f1
                         if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
                                 assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
17b0f1
                                 netns = *(int*) CMSG_DATA(cmsg);
17b0f1
                         }
17b0f1
-                }
17b0f1
 
17b0f1
                 if (setns(netns, CLONE_NEWNET) < 0) {
17b0f1
                         r = -errno;
17b0f1
diff --git a/src/import/importd.c b/src/import/importd.c
17b0f1
index 9aaf991f83..a29630b12b 100644
17b0f1
--- a/src/import/importd.c
17b0f1
+++ b/src/import/importd.c
17b0f1
@@ -507,12 +507,8 @@ static int manager_on_notify(sd_event_source *s, int fd, uint32_t revents, void
17b0f1
                 return -errno;
17b0f1
         }
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
17b0f1
-                if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
-                        close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
17b0f1
-                        log_warning("Somebody sent us unexpected fds, ignoring.");
17b0f1
-                        return 0;
17b0f1
-                } else if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
+        CMSG_FOREACH(cmsg, &msghdr) {
17b0f1
+                if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
                            cmsg->cmsg_type == SCM_CREDENTIALS &&
17b0f1
                            cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
17b0f1
 
17b0f1
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
17b0f1
index 6a35ebbde0..1eb1394d10 100644
17b0f1
--- a/src/journal/journald-server.c
17b0f1
+++ b/src/journal/journald-server.c
17b0f1
@@ -1176,7 +1176,7 @@ int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void
17b0f1
                         return -errno;
17b0f1
                 }
17b0f1
 
17b0f1
-                for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
17b0f1
+                CMSG_FOREACH(cmsg, &msghdr) {
17b0f1
 
17b0f1
                         if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
                             cmsg->cmsg_type == SCM_CREDENTIALS &&
17b0f1
diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c
17b0f1
index 5f90617b9e..870850ed36 100644
17b0f1
--- a/src/libsystemd-network/sd-dhcp-client.c
17b0f1
+++ b/src/libsystemd-network/sd-dhcp-client.c
17b0f1
@@ -1590,7 +1590,7 @@ static int client_receive_message_raw(sd_event_source *s, int fd,
17b0f1
         } else if ((size_t)len < sizeof(DHCPPacket))
17b0f1
                 return 0;
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &msg) {
17b0f1
                 if (cmsg->cmsg_level == SOL_PACKET &&
17b0f1
                     cmsg->cmsg_type == PACKET_AUXDATA &&
17b0f1
                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct tpacket_auxdata))) {
17b0f1
diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
17b0f1
index 0f284eb6a1..c9d0ace728 100644
17b0f1
--- a/src/libsystemd-network/sd-dhcp-server.c
17b0f1
+++ b/src/libsystemd-network/sd-dhcp-server.c
17b0f1
@@ -903,7 +903,7 @@ static int server_receive_message(sd_event_source *s, int fd,
17b0f1
         else if ((size_t)len < sizeof(DHCPMessage))
17b0f1
                 return 0;
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &msg) {
17b0f1
                 if (cmsg->cmsg_level == IPPROTO_IP &&
17b0f1
                     cmsg->cmsg_type == IP_PKTINFO &&
17b0f1
                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct in_pktinfo))) {
17b0f1
diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
17b0f1
index d29b98a269..10ab714312 100644
17b0f1
--- a/src/libsystemd/sd-bus/bus-container.c
17b0f1
+++ b/src/libsystemd/sd-bus/bus-container.c
17b0f1
@@ -222,7 +222,7 @@ int bus_container_connect_kernel(sd_bus *b) {
17b0f1
         if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
17b0f1
                 return -errno;
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
17b0f1
+        CMSG_FOREACH(cmsg, &mh)
17b0f1
                 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
                         int *fds;
17b0f1
                         unsigned n_fds;
17b0f1
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
17b0f1
index a3c3a45b46..ab56ef4f33 100644
17b0f1
--- a/src/libsystemd/sd-bus/bus-socket.c
17b0f1
+++ b/src/libsystemd/sd-bus/bus-socket.c
17b0f1
@@ -503,7 +503,6 @@ static int bus_socket_read_auth(sd_bus *b) {
17b0f1
                 struct cmsghdr cmsghdr;
17b0f1
                 uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
17b0f1
         } control;
17b0f1
-        struct cmsghdr *cmsg;
17b0f1
         bool handle_cmsg = false;
17b0f1
 
17b0f1
         assert(b);
17b0f1
@@ -554,8 +553,10 @@ static int bus_socket_read_auth(sd_bus *b) {
17b0f1
 
17b0f1
         b->rbuffer_size += k;
17b0f1
 
17b0f1
-        if (handle_cmsg)
17b0f1
-                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
17b0f1
+        if (handle_cmsg) {
17b0f1
+                struct cmsghdr *cmsg;
17b0f1
+
17b0f1
+                CMSG_FOREACH(cmsg, &mh)
17b0f1
                         if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
                             cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
                                 int j;
17b0f1
@@ -569,6 +570,7 @@ static int bus_socket_read_auth(sd_bus *b) {
17b0f1
                         } else
17b0f1
                                 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
17b0f1
                                           cmsg->cmsg_level, cmsg->cmsg_type);
17b0f1
+        }
17b0f1
 
17b0f1
         r = bus_socket_auth_verify(b);
17b0f1
         if (r != 0)
17b0f1
@@ -930,7 +932,6 @@ int bus_socket_read_message(sd_bus *bus) {
17b0f1
                 struct cmsghdr cmsghdr;
17b0f1
                 uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
17b0f1
         } control;
17b0f1
-        struct cmsghdr *cmsg;
17b0f1
         bool handle_cmsg = false;
17b0f1
 
17b0f1
         assert(bus);
17b0f1
@@ -976,8 +977,10 @@ int bus_socket_read_message(sd_bus *bus) {
17b0f1
 
17b0f1
         bus->rbuffer_size += k;
17b0f1
 
17b0f1
-        if (handle_cmsg)
17b0f1
-                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
17b0f1
+        if (handle_cmsg) {
17b0f1
+                struct cmsghdr *cmsg;
17b0f1
+
17b0f1
+                CMSG_FOREACH(cmsg, &mh)
17b0f1
                         if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
                             cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
                                 int n, *f;
17b0f1
@@ -1005,6 +1008,7 @@ int bus_socket_read_message(sd_bus *bus) {
17b0f1
                         } else
17b0f1
                                 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
17b0f1
                                           cmsg->cmsg_level, cmsg->cmsg_type);
17b0f1
+        }
17b0f1
 
17b0f1
         r = bus_socket_read_message_need(bus, &need);
17b0f1
         if (r < 0)
17b0f1
diff --git a/src/libsystemd/sd-rtnl/rtnl-message.c b/src/libsystemd/sd-rtnl/rtnl-message.c
17b0f1
index 9276bbdebc..cc84253f1d 100644
17b0f1
--- a/src/libsystemd/sd-rtnl/rtnl-message.c
17b0f1
+++ b/src/libsystemd/sd-rtnl/rtnl-message.c
17b0f1
@@ -1444,7 +1444,7 @@ static int socket_recv_message(int fd, struct iovec *iov, uint32_t *_group, bool
17b0f1
                 return (errno == EAGAIN || errno == EINTR) ? 0 : -errno;
17b0f1
         }
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &msg) {
17b0f1
                 if (cmsg->cmsg_level == SOL_SOCKET &&
17b0f1
                     cmsg->cmsg_type == SCM_CREDENTIALS &&
17b0f1
                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
17b0f1
diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c
17b0f1
index 4c0b557bad..7f47e7223a 100644
17b0f1
--- a/src/resolve/resolved-dns-stream.c
17b0f1
+++ b/src/resolve/resolved-dns-stream.c
17b0f1
@@ -113,7 +113,8 @@ static int dns_stream_identify(DnsStream *s) {
17b0f1
 
17b0f1
         mh.msg_control = &control;
17b0f1
         mh.msg_controllen = sl;
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
17b0f1
+
17b0f1
+        CMSG_FOREACH(cmsg, &mh) {
17b0f1
 
17b0f1
                 if (cmsg->cmsg_level == IPPROTO_IPV6) {
17b0f1
                         assert(s->peer.sa.sa_family == AF_INET6);
17b0f1
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
17b0f1
index 7c253aa13f..173ab8a148 100644
17b0f1
--- a/src/resolve/resolved-manager.c
17b0f1
+++ b/src/resolve/resolved-manager.c
17b0f1
@@ -920,7 +920,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
17b0f1
         } else
17b0f1
                 return -EAFNOSUPPORT;
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &mh) {
17b0f1
 
17b0f1
                 if (cmsg->cmsg_level == IPPROTO_IPV6) {
17b0f1
                         assert(p->family == AF_INET6);
17b0f1
diff --git a/src/shared/macro.h b/src/shared/macro.h
17b0f1
index 9d857dc8d7..7a57f4e5b1 100644
17b0f1
--- a/src/shared/macro.h
17b0f1
+++ b/src/shared/macro.h
17b0f1
@@ -471,4 +471,7 @@ static inline bool GID_IS_INVALID(gid_t gid) {
17b0f1
         }                                                       \
17b0f1
         struct __useless_struct_to_allow_trailing_semicolon__
17b0f1
 
17b0f1
+#define CMSG_FOREACH(cmsg, mh)                                          \
17b0f1
+        for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
17b0f1
+
17b0f1
 #include "log.h"
17b0f1
diff --git a/src/shared/util.c b/src/shared/util.c
17b0f1
index 4c441a5448..357fbfe7dc 100644
17b0f1
--- a/src/shared/util.c
17b0f1
+++ b/src/shared/util.c
17b0f1
@@ -7887,7 +7887,7 @@ int openpt_in_namespace(pid_t pid, int flags) {
17b0f1
         if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
17b0f1
                 return -errno;
17b0f1
 
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
17b0f1
+        CMSG_FOREACH(cmsg, &mh)
17b0f1
                 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
17b0f1
                         int *fds;
17b0f1
                         unsigned n_fds;
17b0f1
@@ -8375,6 +8375,16 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
17b0f1
         return -1;
17b0f1
 }
17b0f1
 
17b0f1
+void cmsg_close_all(struct msghdr *mh) {
17b0f1
+        struct cmsghdr *cmsg;
17b0f1
+
17b0f1
+        assert(mh);
17b0f1
+
17b0f1
+        CMSG_FOREACH(cmsg, mh)
17b0f1
+                if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
17b0f1
+                        close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
17b0f1
+}
17b0f1
+
17b0f1
 char *shell_maybe_quote(const char *s) {
17b0f1
         const char *p;
17b0f1
         char *r, *t;
17b0f1
diff --git a/src/shared/util.h b/src/shared/util.h
17b0f1
index be04524cc9..12afcc3429 100644
17b0f1
--- a/src/shared/util.h
17b0f1
+++ b/src/shared/util.h
17b0f1
@@ -1082,6 +1082,8 @@ void sigkill_wait(pid_t *pid);
17b0f1
 
17b0f1
 int syslog_parse_priority(const char **p, int *priority, bool with_facility);
17b0f1
 
17b0f1
+void cmsg_close_all(struct msghdr *mh);
17b0f1
+
17b0f1
 char *shell_maybe_quote(const char *s);
17b0f1
 
17b0f1
 typedef enum ExtractFlags {
17b0f1
diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c
17b0f1
index 73ac7eecbf..5cc1968204 100644
17b0f1
--- a/src/timesync/timesyncd-manager.c
17b0f1
+++ b/src/timesync/timesyncd-manager.c
17b0f1
@@ -539,7 +539,7 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
17b0f1
         }
17b0f1
 
17b0f1
         recv_time = NULL;
17b0f1
-        for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
17b0f1
+        CMSG_FOREACH(cmsg, &msghdr) {
17b0f1
                 if (cmsg->cmsg_level != SOL_SOCKET)
17b0f1
                         continue;
17b0f1