From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 5FAD01F4C8 for ; Sat, 9 Nov 2024 01:18:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1731115124; bh=9BejAlWnEd4R+SgspsFTUcLinAhGa1L+7uppIq0bp8k=; h=From:To:Subject:Date:In-Reply-To:References:From; b=BBUa/43z8p4JRE1+URr/iN8cXT2zg2cxHjDK8xAaFoXPKQtCi6n7BYAlIY7KNCQYg XM9+YZRxBmkHYWXv4E3R2rVQSohwQm3OQq1NeMo0jM7kWlkBAWGdqcSi3Hto0xCIdF IV0NZ2iBVkLmRh+K0vWONIhHebIkTaS1yB8tfbqE= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/2] daemon: warn on setsockopt failures Date: Sat, 9 Nov 2024 01:13:41 +0000 Message-ID: <20241109011341.90730-3-e@80x24.org> In-Reply-To: <20241109011341.90730-1-e@80x24.org> References: <20241109011341.90730-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: While actually dying is too harsh for platform-specific features, having a warning may be useful in case the system is misconfigured somehow. --- lib/PublicInbox/Daemon.pm | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index fa4314b4..8e6ddbb9 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -6,7 +6,7 @@ # and/or lossy connections. package PublicInbox::Daemon; use v5.12; -use autodie qw(chdir open pipe); +use autodie qw(chdir open pipe setsockopt); use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev); use IO::Handle; # ->autoflush use IO::Socket; @@ -644,20 +644,26 @@ sub tls_cb { sub defer_accept ($$) { my ($s, $af_name) = @_; - return unless defined $af_name; - if ($^O eq 'linux') { - 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, $TCP_DEFER_ACCEPT, 1); - } elsif ($^O =~ /\A(?:freebsd|netbsd|dragonfly)\z/) { - my $x = getsockopt($s, SOL_SOCKET, $SO_ACCEPTFILTER); - return if ($x // "\0") =~ /[^\0]/s; # don't change if set - my $accf_arg = pack('a16a240', $af_name, ''); - setsockopt($s, SOL_SOCKET, $SO_ACCEPTFILTER, $accf_arg); - } + $af_name // return; + eval { + if ($^O eq 'linux') { + # Socket::TCP_DEFER_ACCEPT is only in 5.14+ + my $TCP_DEFER_ACCEPT = 9; + my $x = getsockopt($s, IPPROTO_TCP, $TCP_DEFER_ACCEPT) + // return; # may be Unix socket + my $sec = unpack('i', $x); + return if $sec > 0; # systemd users may this higher + setsockopt $s, IPPROTO_TCP, $TCP_DEFER_ACCEPT, 1; + } elsif ($^O =~ /\A(?:freebsd|netbsd|dragonfly)\z/) { + # getsockopt can EINVAL if SO_ACCEPTFILTER is unset: + my $x = getsockopt($s, SOL_SOCKET, $SO_ACCEPTFILTER); + return if ($x // '') =~ /[^\0]/s; # don't change if set + my $accf_arg = pack('a16a240', $af_name, ''); + setsockopt $s, SOL_SOCKET, $SO_ACCEPTFILTER, $accf_arg; + } + }; + my $err = $@; # sockname() clobbers $@ + warn 'W: ', sockname($s), ' ', $err, "\n" if $err; } sub daemon_loop () {