From 42a65a32623158c5bdce234b1b431b9f5093da70 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 11 Nov 2015 03:38:47 +0000 Subject: set TCP listener options on inherited sockets systemd users may not set the correct TCP socket options for us, so be sure to set TCP_NODELAY, SO_KEEPALIVE, and use a sufficiently large listen backlog to avoid hurting performance for users who bind sockets outside of cmogstored. --- bind_listen.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/bind_listen.c b/bind_listen.c index 5535b92..c705576 100644 --- a/bind_listen.c +++ b/bind_listen.c @@ -15,15 +15,17 @@ * http://labs.apnic.net/blabs/?p=57 */ -static int set_tcp_opts(int fd) +static int set_tcp_opts(int fd, bool inherited) { int val; socklen_t len = sizeof(int); int rc; - val = 1; - rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, len); - if (rc < 0) return rc; + if (!inherited) { + val = 1; + rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, len); + if (rc < 0) return rc; + } val = 1; rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, len); @@ -40,8 +42,11 @@ int mog_bind_listen(struct addrinfo *r) { /* see if we inherited the socket, first */ int fd = mog_inherit_get(r->ai_addr, r->ai_addrlen); + const int backlog = 1024; - if (fd >= 0) + if (fd >= 0 && + set_tcp_opts(fd, true) == 0 && + listen(fd, backlog) == 0) return fd; for (; r; r = r->ai_next) { @@ -56,9 +61,9 @@ int mog_bind_listen(struct addrinfo *r) * everywhere yet (in 2012). */ if (mog_set_cloexec(fd, true) == 0 && - set_tcp_opts(fd) == 0 && + set_tcp_opts(fd, false) == 0 && bind(fd, r->ai_addr, r->ai_addrlen) == 0 && - listen(fd, 1024) == 0) + listen(fd, backlog) == 0) break; PRESERVE_ERRNO( mog_close(fd) ); -- cgit v1.2.3-24-ge0c7