ryantimwilson / rpms / systemd

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