]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: port users over to use new ERRNO_IS_ACCEPT_AGAIN() call 12271/head
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Apr 2019 17:50:53 +0000 (19:50 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 10 Apr 2019 20:11:18 +0000 (22:11 +0200)
src/activate/activate.c
src/basic/socket-util.c
src/core/dbus.c
src/core/socket.c
src/journal-remote/journal-remote.c
src/journal/journald-stream.c
src/resolve/resolved-dns-stub.c
src/resolve/resolved-llmnr.c
src/socket-proxy/socket-proxyd.c
src/udev/udev-ctrl.c

index 1f1a9802ed72482c6f3b57d09eb8193174696c22..473366fb2022824f96291945a6875c4b9f4c24e2 100644 (file)
@@ -271,8 +271,12 @@ static int do_accept(const char* name, char **argv, char **envp, int fd) {
         _cleanup_close_ int fd_accepted = -1;
 
         fd_accepted = accept4(fd, NULL, NULL, 0);
         _cleanup_close_ int fd_accepted = -1;
 
         fd_accepted = accept4(fd, NULL, NULL, 0);
-        if (fd_accepted < 0)
+        if (fd_accepted < 0) {
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
+                        return 0;
+
                 return log_error_errno(errno, "Failed to accept connection on fd:%d: %m", fd);
                 return log_error_errno(errno, "Failed to accept connection on fd:%d: %m", fd);
+        }
 
         (void) getsockname_pretty(fd_accepted, &local);
         (void) getpeername_pretty(fd_accepted, true, &peer);
 
         (void) getsockname_pretty(fd_accepted, &local);
         (void) getpeername_pretty(fd_accepted, true, &peer);
index 3d929f5418dcda72f1391f605473bb8756a9d942..904bafb76f92362d29e0343d791cbfe8df6760b1 100644 (file)
@@ -15,6 +15,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "errno-util.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -1237,22 +1238,22 @@ int flush_accept(int fd) {
                                 continue;
 
                         return -errno;
                                 continue;
 
                         return -errno;
-
-                } else if (r == 0)
+                }
+                if (r == 0)
                         return 0;
 
                 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
                 if (cfd < 0) {
                         return 0;
 
                 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
                 if (cfd < 0) {
-                        if (errno == EINTR)
-                                continue;
-
                         if (errno == EAGAIN)
                                 return 0;
 
                         if (errno == EAGAIN)
                                 return 0;
 
+                        if (ERRNO_IS_ACCEPT_AGAIN(errno))
+                                continue;
+
                         return -errno;
                 }
 
                         return -errno;
                 }
 
-                close(cfd);
+                safe_close(cfd);
         }
 }
 
         }
 }
 
index 74b1c51c1d00222b54c11a923e9da2880c81767f..8ae5d173f00b05ec28171c544abe1cb8b7e68046 100644 (file)
@@ -622,6 +622,9 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void
 
         nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (nfd < 0) {
 
         nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (nfd < 0) {
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
+                        return 0;
+
                 log_warning_errno(errno, "Failed to accept private connection, ignoring: %m");
                 return 0;
         }
                 log_warning_errno(errno, "Failed to accept private connection, ignoring: %m");
                 return 0;
         }
index 95976ed44d706f68b665e6cfa38f5db034a7ac77..836e513d49d9b09aed899e81aa26fb22ba88d3ff 100644 (file)
@@ -2856,17 +2856,10 @@ static int socket_accept_do(Socket *s, int fd) {
         assert(s);
         assert(fd >= 0);
 
         assert(s);
         assert(fd >= 0);
 
-        for (;;) {
-                cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
-                if (cfd < 0) {
-                        if (errno == EINTR)
-                                continue;
-
-                        return -errno;
-                }
-
-                break;
-        }
+        cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
+        if (cfd < 0)
+                /* Convert transient network errors into clean and well-defined EAGAIN */
+                return ERRNO_IS_ACCEPT_AGAIN(errno) ? -EAGAIN : -errno;
 
         return cfd;
 }
 
         return cfd;
 }
@@ -2904,6 +2897,8 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
                 pair[0] = safe_close(pair[0]);
 
                 cfd = socket_accept_do(s, fd);
                 pair[0] = safe_close(pair[0]);
 
                 cfd = socket_accept_do(s, fd);
+                if (cfd == -EAGAIN) /* spurious accept() */
+                        _exit(EXIT_SUCCESS);
                 if (cfd < 0) {
                         log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
                         _exit(EXIT_FAILURE);
                 if (cfd < 0) {
                         log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
                         _exit(EXIT_FAILURE);
@@ -2928,6 +2923,10 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
                 return r;
         }
 
                 return r;
         }
 
+        /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
+         * this is a spurious accept(), let's convert that back to EAGAIN here. */
+        if (cfd == -EIO)
+                return -EAGAIN;
         if (cfd < 0)
                 return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m");
 
         if (cfd < 0)
                 return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m");
 
@@ -2935,6 +2934,8 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
 
 shortcut:
         cfd = socket_accept_do(s, fd);
 
 shortcut:
         cfd = socket_accept_do(s, fd);
+        if (cfd == -EAGAIN) /* spurious accept(), skip it silently */
+                return -EAGAIN;
         if (cfd < 0)
                 return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
 
         if (cfd < 0)
                 return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
 
@@ -2954,7 +2955,6 @@ static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
         log_unit_debug(UNIT(p->socket), "Incoming traffic");
 
         if (revents != EPOLLIN) {
         log_unit_debug(UNIT(p->socket), "Incoming traffic");
 
         if (revents != EPOLLIN) {
-
                 if (revents & EPOLLHUP)
                         log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.");
                 else
                 if (revents & EPOLLHUP)
                         log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.");
                 else
@@ -2967,6 +2967,8 @@ static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
             socket_address_can_accept(&p->address)) {
 
                 cfd = socket_accept_in_cgroup(p->socket, p, fd);
             socket_address_can_accept(&p->address)) {
 
                 cfd = socket_accept_in_cgroup(p->socket, p, fd);
+                if (cfd == -EAGAIN) /* Spurious accept() */
+                        return 0;
                 if (cfd < 0)
                         goto fail;
 
                 if (cfd < 0)
                         goto fail;
 
index 1da32c5f85615300eb380e7ce18190e743510111..04c66a29ce802e34fb1cb5cf2ed40c96570e6c7a 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "alloc-util.h"
 #include "def.h"
 
 #include "alloc-util.h"
 #include "def.h"
+#include "errno-util.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "journal-file.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "journal-file.h"
@@ -466,14 +467,23 @@ static int dispatch_blocking_source_event(sd_event_source *event,
         return journal_remote_handle_raw_source(event, source->importer.fd, EPOLLIN, journal_remote_server_global);
 }
 
         return journal_remote_handle_raw_source(event, source->importer.fd, EPOLLIN, journal_remote_server_global);
 }
 
-static int accept_connection(const char* type, int fd,
-                             SocketAddress *addr, char **hostname) {
-        int fd2, r;
+static int accept_connection(
+                const char* type,
+                int fd,
+                SocketAddress *addr,
+                char **hostname) {
+
+        _cleanup_close_ int fd2 = -1;
+        int r;
 
         log_debug("Accepting new %s connection on fd:%d", type, fd);
         fd2 = accept4(fd, &addr->sockaddr.sa, &addr->size, SOCK_NONBLOCK|SOCK_CLOEXEC);
 
         log_debug("Accepting new %s connection on fd:%d", type, fd);
         fd2 = accept4(fd, &addr->sockaddr.sa, &addr->size, SOCK_NONBLOCK|SOCK_CLOEXEC);
-        if (fd2 < 0)
+        if (fd2 < 0) {
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
+                        return -EAGAIN;
+
                 return log_error_errno(errno, "accept() on fd:%d failed: %m", fd);
                 return log_error_errno(errno, "accept() on fd:%d failed: %m", fd);
+        }
 
         switch(socket_address_family(addr)) {
         case AF_INET:
 
         switch(socket_address_family(addr)) {
         case AF_INET:
@@ -482,18 +492,12 @@ static int accept_connection(const char* type, int fd,
                 char *b;
 
                 r = socket_address_print(addr, &a);
                 char *b;
 
                 r = socket_address_print(addr, &a);
-                if (r < 0) {
-                        log_error_errno(r, "socket_address_print(): %m");
-                        close(fd2);
-                        return r;
-                }
+                if (r < 0)
+                        return log_error_errno(r, "socket_address_print(): %m");
 
                 r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b);
 
                 r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b);
-                if (r < 0) {
-                        log_error_errno(r, "Resolving hostname failed: %m");
-                        close(fd2);
-                        return r;
-                }
+                if (r < 0)
+                        return log_error_errno(r, "Resolving hostname failed: %m");
 
                 log_debug("Accepted %s %s connection from %s",
                           type,
 
                 log_debug("Accepted %s %s connection from %s",
                           type,
@@ -501,22 +505,22 @@ static int accept_connection(const char* type, int fd,
                           a);
 
                 *hostname = b;
                           a);
 
                 *hostname = b;
+                return TAKE_FD(fd2);
+        }
 
 
-                return fd2;
-        };
         default:
         default:
-                log_error("Rejected %s connection with unsupported family %d",
-                          type, socket_address_family(addr));
-                close(fd2);
-
-                return -EINVAL;
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Rejected %s connection with unsupported family %d",
+                                       type, socket_address_family(addr));
         }
 }
 
         }
 }
 
-static int dispatch_raw_connection_event(sd_event_source *event,
-                                         int fd,
-                                         uint32_t revents,
-                                         void *userdata) {
+static int dispatch_raw_connection_event(
+                sd_event_source *event,
+                int fd,
+                uint32_t revents,
+                void *userdata) {
+
         RemoteServer *s = userdata;
         int fd2;
         SocketAddress addr = {
         RemoteServer *s = userdata;
         int fd2;
         SocketAddress addr = {
@@ -526,6 +530,8 @@ static int dispatch_raw_connection_event(sd_event_source *event,
         char *hostname = NULL;
 
         fd2 = accept_connection("raw", fd, &addr, &hostname);
         char *hostname = NULL;
 
         fd2 = accept_connection("raw", fd, &addr, &hostname);
+        if (fd2 == -EAGAIN)
+                return 0;
         if (fd2 < 0)
                 return fd2;
 
         if (fd2 < 0)
                 return fd2;
 
index aa2db6863475396a0cedde96b139722999a84af0..24d4ac30b2d8ab19deda01cc1cda2d41933c6d9c 100644 (file)
@@ -13,6 +13,7 @@
 #include "alloc-util.h"
 #include "dirent-util.h"
 #include "env-file.h"
 #include "alloc-util.h"
 #include "dirent-util.h"
 #include "env-file.h"
+#include "errno-util.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -605,7 +606,7 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
 
         fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (fd < 0) {
 
         fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (fd < 0) {
-                if (errno == EAGAIN)
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
                         return 0;
 
                 return log_error_errno(errno, "Failed to accept stdout connection: %m");
                         return 0;
 
                 return log_error_errno(errno, "Failed to accept stdout connection: %m");
index 65c809490f27fabf9e12d69659d99d43462ed682..ce994a7ee0b213c0fa525d1547cc141c85e7e69a 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include "errno-util.h"
 #include "fd-util.h"
 #include "missing_network.h"
 #include "resolved-dns-stub.h"
 #include "fd-util.h"
 #include "missing_network.h"
 #include "resolved-dns-stub.h"
@@ -462,7 +463,7 @@ static int on_dns_stub_stream(sd_event_source *s, int fd, uint32_t revents, void
 
         cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (cfd < 0) {
 
         cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (cfd < 0) {
-                if (IN_SET(errno, EAGAIN, EINTR))
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
                         return 0;
 
                 return -errno;
                         return 0;
 
                 return -errno;
index b7c37f1132aa1be2c901a14a2060d3f005be6ed5..61e50343e5c74d63912f1506c9d19fcc8e1ca507 100644 (file)
@@ -3,6 +3,7 @@
 #include <netinet/in.h>
 #include <resolv.h>
 
 #include <netinet/in.h>
 #include <resolv.h>
 
+#include "errno-util.h"
 #include "fd-util.h"
 #include "resolved-llmnr.h"
 #include "resolved-manager.h"
 #include "fd-util.h"
 #include "resolved-llmnr.h"
 #include "resolved-manager.h"
@@ -289,7 +290,7 @@ static int on_llmnr_stream(sd_event_source *s, int fd, uint32_t revents, void *u
 
         cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (cfd < 0) {
 
         cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (cfd < 0) {
-                if (IN_SET(errno, EAGAIN, EINTR))
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
                         return 0;
 
                 return -errno;
                         return 0;
 
                 return -errno;
index ee0c6c0be6cee3d7edc1be2ed0f4ba4b28f48369..abbbc9f2d61852b75708b32c679a4297da5398b0 100644 (file)
@@ -467,7 +467,7 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat
 
         nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (nfd < 0) {
 
         nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
         if (nfd < 0) {
-                if (errno != -EAGAIN)
+                if (!ERRNO_IS_ACCEPT_AGAIN(errno))
                         log_warning_errno(errno, "Failed to accept() socket: %m");
         } else {
                 (void) getpeername_pretty(nfd, true, &peer);
                         log_warning_errno(errno, "Failed to accept() socket: %m");
         } else {
                 (void) getpeername_pretty(nfd, true, &peer);
index 59ce5c1f80098c42c21022cdaac802cb175bc53c..c3dc213aa8c7c84bb339f3f379d9d1c196fd6260 100644 (file)
@@ -20,6 +20,7 @@
 #include "sd-event.h"
 
 #include "alloc-util.h"
 #include "sd-event.h"
 
 #include "alloc-util.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "format-util.h"
 #include "io-util.h"
 #include "fd-util.h"
 #include "format-util.h"
 #include "io-util.h"
@@ -261,9 +262,10 @@ static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents,
 
         sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
         if (sock < 0) {
 
         sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
         if (sock < 0) {
-                if (errno != EINTR)
-                        log_error_errno(errno, "Failed to accept ctrl connection: %m");
-                return 0;
+                if (ERRNO_IS_ACCEPT_AGAIN(errno))
+                        return 0;
+
+                return log_error_errno(errno, "Failed to accept ctrl connection: %m");
         }
 
         /* check peer credential of connection */
         }
 
         /* check peer credential of connection */