From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6811B1F59D for ; Mon, 8 Aug 2022 23:53:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1660002791; bh=QXVCZVZ/W3YSDAPiftel949tq2bhyngOFZLxi64SpbQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Ekl2XRcU53F1+uNzikw+2ML6YM6pl8sBIwlMLY9krP8sCWClIAXNAW+/fJ5Td7bXB 1kH1foJLL0DN3spgU5s8WpI4IZa/A+PLd+pI6QfI4DBLRBZhqcpCXfpoNkFHjI+Uvt q+VSZQ2rGMNUa1a5D7QUPNRChZZuiPB2D5npahyA= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/5] daemon: use default address + well-known ports for scheme Date: Mon, 8 Aug 2022 23:53:07 +0000 Message-Id: <20220808235311.2006279-2-e@80x24.org> In-Reply-To: <20220808235311.2006279-1-e@80x24.org> References: <20220808235311.2006279-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This ensures the "bound $URL" diagnostic message at startup always shows the URL scheme handled if not relying on socket inheritance. This also avoids duplicate/unused data structures when binding sockets ourselves, as bound socket names can expand from short names to longer names (e.g. "0:119" => "0.0.0.0:119"). --- lib/PublicInbox/Daemon.pm | 45 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index b6f4f9ed..0043d21e 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -192,20 +192,23 @@ EOF foreach my $l (@cfg_listen) { my $orig = $l; - my $scheme = ''; - my $port; - if ($l =~ s!\A([^:]+)://!!) { $scheme = $1 } + my ($scheme, $port, $opt); + + $l =~ s!\A([a-z0-9]+)://!! and $scheme = $1; + (!$scheme && ($default_listen // '') =~ m!\A([^:]+)://!) and + $scheme = $1; if ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9]+)/) { $port = $1 + 0; - my $s = $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port}; - $scheme //= $s if defined $s; - } elsif (index($l, '/') != 0) { # unix socket - $port //= $SCHEME2PORT{$scheme} if $scheme; - $port // die "no port in listen=$l\n"; + $scheme //= $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port}; + } + $scheme or die "unable to determine URL scheme of $orig\n"; + if (!defined($port) && index($l, '/') != 0) { # unix socket + $port = $SCHEME2PORT{$scheme} // + die "no port in listen=$orig\n"; $l =~ s!\A([^/]+)!$1:$port! or die "unable to add port=$port to $l\n"; } - my $opt; # non-TLS options + $l =~ s!/\z!!; # chop one trailing slash if ($l =~ s!/?\?(.+)\z!!) { $opt = listener_opt($1); $tls_opt{"$scheme://$l"} = accept_tls_opt($opt); @@ -214,10 +217,10 @@ EOF } elsif ($scheme =~ /\A(?:https|imaps|nntps|pop3s)\z/) { die "$orig specified w/o cert=\n"; } - $scheme =~ /\A(?:http|imap|nntp|pop3)/ and + if ($listener_names->{$l}) { # already inherited $xnetd->{$l} = load_mod($scheme, $opt, $l); - - next if $listener_names->{$l}; # already inherited + next; + } my (%o, $sock_pkg); if (index($l, '/') == 0) { $sock_pkg = 'IO::Socket::UNIX'; @@ -244,16 +247,16 @@ EOF } $o{Listen} = 1024; my $prev = umask 0000; - my $s = eval { $sock_pkg->new(%o) }; - warn "error binding $l: $! ($@)\n" unless $s; + my $s = eval { $sock_pkg->new(%o) } or + warn "error binding $l: $! ($@)\n"; umask $prev; - if ($s) { - $s->blocking(0); - my $k = sockname($s); - warn "# bound $scheme://$k\n"; - $listener_names->{$k} = $s; - push @listeners, $s; - } + $s // next; + $s->blocking(0); + my $sockname = sockname($s); + warn "# bound $scheme://$sockname\n"; + $xnetd->{$sockname} //= load_mod($scheme); + $listener_names->{$sockname} = $s; + push @listeners, $s; } # cert/key options in @cfg_listen takes precedence when inheriting,