cmogstored dev/user discussion/issues/patches/etc
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: cmogstored-public@bogomips.org
Subject: [PATCH] set TCP listener options on inherited sockets
Date: Wed, 11 Nov 2015 03:38:47 +0000	[thread overview]
Message-ID: <20151111033847.GA16666@dcvr.yhbt.net> (raw)
In-Reply-To: <20151111024017.4786-1-e@80x24.org>

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


  reply	other threads:[~2015-11-11  3:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-11  2:40 [PATCH] support systemd-style socket activation via environment Eric Wong
2015-11-11  3:38 ` Eric Wong [this message]
2015-11-11  4:01 ` [PATCH] doc: add example systemd config files Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/cmogstored/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151111033847.GA16666@dcvr.yhbt.net \
    --to=e@80x24.org \
    --cc=cmogstored-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/cmogstored.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).