diff options
author | Martin Pitt <martin.pitt@ubuntu.com> | 2016-05-08 21:09:35 +0200 |
---|---|---|
committer | Martin Pitt <martin.pitt@ubuntu.com> | 2016-05-08 21:09:35 +0200 |
commit | d75103d4c68f13ecf3d09234c1506d58e8fae80e (patch) | |
tree | d194cc46b63dc07129ee3bb20e9a87b7df93161a /src/basic | |
parent | 977f2beaf2d9c60c69d9dc5d86685bb2960a6a7d (diff) | |
parent | 60d9771c593e0702a892a4372443e63b38cdbcba (diff) |
Merge pull request #3202 from poettering/socket-fixes
don't reopen socket fds when reloading the daemon
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/io-util.c | 5 | ||||
-rw-r--r-- | src/basic/socket-util.c | 40 | ||||
-rw-r--r-- | src/basic/socket-util.h | 2 |
3 files changed, 47 insertions, 0 deletions
diff --git a/src/basic/io-util.c b/src/basic/io-util.c index 0037a37f2a..cc6dfa8c1b 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -33,6 +33,11 @@ int flush_fd(int fd) { .events = POLLIN, }; + /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything + * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read + * (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used + * was set to non-blocking too. */ + for (;;) { char buf[LINE_MAX]; ssize_t l; diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 0f38f9a0f3..c634f1d564 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -23,6 +23,7 @@ #include <net/if.h> #include <netdb.h> #include <netinet/ip.h> +#include <poll.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> @@ -970,3 +971,42 @@ fallback: return (ssize_t) k; } + +int flush_accept(int fd) { + + struct pollfd pollfd = { + .fd = fd, + .events = POLLIN, + }; + int r; + + + /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */ + + for (;;) { + int cfd; + + r = poll(&pollfd, 1, 0); + if (r < 0) { + if (errno == EINTR) + continue; + + return -errno; + + } else if (r == 0) + return 0; + + cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); + if (cfd < 0) { + if (errno == EINTR) + continue; + + if (errno == EAGAIN) + return 0; + + return -errno; + } + + close(cfd); + } +} diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index daa4b24a37..160f7c484b 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -135,6 +135,8 @@ int receive_one_fd(int transport_fd, int flags); ssize_t next_datagram_size_fd(int fd); +int flush_accept(int fd); + #define CMSG_FOREACH(cmsg, mh) \ for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg))) |