diff options
author | Christos Trochalakis <yatiohi@ideopolis.gr> | 2015-07-01 14:39:53 +0300 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-03-14 10:05:54 +0100 |
commit | 2054442fa4eaef0d904bcbbe1b98f57386ceb87d (patch) | |
tree | 1531c21ae6a5ab450bc80586f9ad442b7b48b361 /src/shared | |
parent | 322e286bc4da19f8dcfe991971e397e0afca067b (diff) |
socket: Set SO_REUSEPORT before bind()
bind() fails if it is called before setting SO_REUSEPORT and another
process is already binded to the same addess.
A new reuse_port option has been introduced to socket_address_listen()
to set the option as part of socket initialization.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/socket-label.c | 9 | ||||
-rw-r--r-- | src/shared/socket-util.h | 1 |
2 files changed, 9 insertions, 1 deletions
diff --git a/src/shared/socket-label.c b/src/shared/socket-label.c index cbe3ff216..144e6fd86 100644 --- a/src/shared/socket-label.c +++ b/src/shared/socket-label.c @@ -38,6 +38,7 @@ int socket_address_listen( int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, + bool reuse_port, bool free_bind, bool transparent, mode_t directory_mode, @@ -83,6 +84,12 @@ int socket_address_listen( if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0) return -errno; + if (reuse_port) { + one = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) + log_warning_errno(errno, "SO_REUSEPORT failed: %m"); + } + if (free_bind) { one = 1; if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0) @@ -146,7 +153,7 @@ int make_socket_fd(int log_level, const char* address, int flags) { } fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT, - NULL, false, false, 0755, 0644, NULL); + NULL, false, false, false, 0755, 0644, NULL); if (fd < 0 || log_get_max_level() >= log_level) { _cleanup_free_ char *p = NULL; diff --git a/src/shared/socket-util.h b/src/shared/socket-util.h index 538cf5917..6b0ce7836 100644 --- a/src/shared/socket-util.h +++ b/src/shared/socket-util.h @@ -80,6 +80,7 @@ int socket_address_listen( int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, + bool reuse_port, bool free_bind, bool transparent, mode_t directory_mode, |