user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 1/4] daemon: provide TCP_DEFER_ACCEPT for Perl <5.14
Date: Sun, 26 Jan 2020 10:29:21 +0000
Message-ID: <20200126102924.3593-2-e@yhbt.net> (raw)
In-Reply-To: <20200126102924.3593-1-e@yhbt.net>

Socket::TCP_DEFER_ACCEPT() did not appear in the Socket module
distributed with Perl until 5.14, despite it being available
since Linux 2.4.
---
 lib/PublicInbox/Daemon.pm | 5 +++--
 t/httpd-corner.t          | 9 +++++----
 t/httpd-https.t           | 2 +-
 t/httpd.t                 | 2 +-
 t/nntpd-tls.t             | 2 +-
 5 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 278c80f5..15d8bd31 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -566,11 +566,12 @@ sub defer_accept ($$) {
 	my ($s, $af_name) = @_;
 	return unless defined $af_name;
 	if ($^O eq 'linux') {
-		my $x = getsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT());
+		my $TCP_DEFER_ACCEPT = 9; # Socket::TCP_DEFER_ACCEPT is in 5.14+
+		my $x = getsockopt($s, IPPROTO_TCP, $TCP_DEFER_ACCEPT);
 		return unless defined $x; # may be Unix socket
 		my $sec = unpack('i', $x);
 		return if $sec > 0; # systemd users may set a higher value
-		setsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT(), 1);
+		setsockopt($s, IPPROTO_TCP, $TCP_DEFER_ACCEPT, 1);
 	} elsif ($^O eq 'freebsd') {
 		my $x = getsockopt($s, SOL_SOCKET, SO_ACCEPTFILTER);
 		return if defined $x; # don't change if set
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 4ed34934..1f2bb53f 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -28,10 +28,11 @@ open(STDIN, '<', '/dev/null') or die 'no /dev/null: $!';
 
 # Make sure we don't clobber socket options set by systemd or similar
 # using socket activation:
-my ($defer_accept_val, $accf_arg);
+my ($defer_accept_val, $accf_arg, $TCP_DEFER_ACCEPT);
 if ($^O eq 'linux') {
-	setsockopt($sock, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT(), 5) or die;
-	my $x = getsockopt($sock, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT());
+	$TCP_DEFER_ACCEPT = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
+	setsockopt($sock, IPPROTO_TCP, $TCP_DEFER_ACCEPT, 5) or die;
+	my $x = getsockopt($sock, IPPROTO_TCP, $TCP_DEFER_ACCEPT);
 	defined $x or die "getsockopt: $!";
 	$defer_accept_val = unpack('i', $x);
 	if ($defer_accept_val <= 0) {
@@ -526,7 +527,7 @@ SKIP: {
 
 SKIP: {
 	skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
-	my $var = Socket::TCP_DEFER_ACCEPT();
+	my $var = $TCP_DEFER_ACCEPT;
 	defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
 	is(unpack('i', $x), $defer_accept_val,
 		'TCP_DEFER_ACCEPT unchanged if previously set');
diff --git a/t/httpd-https.t b/t/httpd-https.t
index 9ce060c8..ee5ced0f 100644
--- a/t/httpd-https.t
+++ b/t/httpd-https.t
@@ -87,7 +87,7 @@ for my $args (
 
 	SKIP: {
 		skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
-		my $var = Socket::TCP_DEFER_ACCEPT();
+		my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
 		defined(my $x = getsockopt($https, IPPROTO_TCP, $var)) or die;
 		ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on https');
 	};
diff --git a/t/httpd.t b/t/httpd.t
index 880c69e6..2972afb2 100644
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -81,7 +81,7 @@ EOF
 
 SKIP: {
 	skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
-	my $var = Socket::TCP_DEFER_ACCEPT();
+	my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
 	defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
 	ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
 };
diff --git a/t/nntpd-tls.t b/t/nntpd-tls.t
index edc1fab4..c97d179d 100644
--- a/t/nntpd-tls.t
+++ b/t/nntpd-tls.t
@@ -174,7 +174,7 @@ for my $args (
 
 	SKIP: {
 		skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
-		my $var = Socket::TCP_DEFER_ACCEPT();
+		my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
 		defined(my $x = getsockopt($nntps, IPPROTO_TCP, $var)) or die;
 		ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on NNTPS');
 		defined($x = getsockopt($starttls, IPPROTO_TCP, $var)) or die;

  reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-26 10:29 [PATCH 0/4] Perl 5.10.1 compatibility fixes Eric Wong
2020-01-26 10:29 ` Eric Wong [this message]
2020-01-26 10:29 ` [PATCH 2/4] avoid relying on IO::Handle/IO::File autoload Eric Wong
2020-01-26 10:29 ` [PATCH 3/4] t/hl_mod: document IO::Handle for autoflush Eric Wong
2020-01-26 10:29 ` [PATCH 4/4] t/v2reindex.t: 5.10.1 glob compatibility 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: http://public-inbox.org/README

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

  git send-email \
    --in-reply-to=20200126102924.3593-2-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.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

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git