daandemeyer / rpms / systemd

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