From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: cmogstored-public@bogomips.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E2F7F1F4CF; Wed, 11 Nov 2015 03:38:47 +0000 (UTC) Date: Wed, 11 Nov 2015 03:38:47 +0000 From: Eric Wong To: cmogstored-public@bogomips.org Subject: [PATCH] set TCP listener options on inherited sockets Message-ID: <20151111033847.GA16666@dcvr.yhbt.net> References: <20151111024017.4786-1-e@80x24.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20151111024017.4786-1-e@80x24.org> List-Id: 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) ); -- EW