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-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 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 DAC8C1FA13 for ; Sun, 24 Jan 2021 11:46:55 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 3/9] ipc: get rid of wq_set_recv_modes Date: Sun, 24 Jan 2021 04:46:49 -0700 Message-Id: <20210124114655.12815-4-e@80x24.org> In-Reply-To: <20210124114655.12815-1-e@80x24.org> References: <20210124114655.12815-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Just open every FD as read/write. Perl (or any non-broken runtime) won't care and won't attempt to use F_SETFL to alter file description flags; as attempting to change those would lead to unpleasant side effects if the file description is shared with another process. --- lib/PublicInbox/IPC.pm | 11 ++--------- lib/PublicInbox/LEI.pm | 7 +++---- lib/PublicInbox/LeiToMail.pm | 7 ++----- lib/PublicInbox/LeiXSearch.pm | 5 ++--- script/lei | 11 +++++------ t/ipc.t | 1 - 6 files changed, 14 insertions(+), 28 deletions(-) diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm index 592efd21..c52441f7 100644 --- a/lib/PublicInbox/IPC.pm +++ b/lib/PublicInbox/IPC.pm @@ -46,11 +46,6 @@ my $send_cmd = PublicInbox::Spawn->can('send_cmd4') // do { PublicInbox::CmdIPC4->can('send_cmd4'); }; -sub wq_set_recv_modes { - my ($self, @modes) = @_; - $self->{-wq_recv_modes} = \@modes; -} - sub _get_rec ($) { my ($r) = @_; defined(my $len = <$r>) or return; @@ -251,15 +246,13 @@ sub _recv_and_run { my ($self, $s2, $len, $full_stream) = @_; my @fds = $recv_cmd->($s2, my $buf, $len); my $n = length($buf // '') or return; - my @m = @{$self->{-wq_recv_modes} // [qw( +<&= >&= >&= )]}; my $nfd = 0; for my $fd (@fds) { - my $mode = shift(@m); - if (open(my $cmdfh, $mode, $fd)) { + if (open(my $cmdfh, '+<&=', $fd)) { $self->{$nfd++} = $cmdfh; $cmdfh->autoflush(1); } else { - die "$$ open($mode$fd) (FD:$nfd): $!"; + die "$$ open(+<&=$fd) (FD:$nfd): $!"; } } while ($full_stream && $n < $len) { diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index a9123c6e..473a28a9 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -765,11 +765,10 @@ sub accept_dispatch { # Listener {post_accept} callback return send($sock, 'timed out waiting to recv FDs', MSG_EOR); my @fds = $recv_cmd->($sock, my $buf, 4096 * 33); # >MAX_ARG_STRLEN if (scalar(@fds) == 4) { - my $i = 0; - for my $rdr (qw(<&= >&= >&= <&=)) { + for my $i (0..3) { my $fd = shift(@fds); - open($self->{$i++}, $rdr, $fd) and next; - send($sock, "open($rdr$fd) (FD=$i): $!", MSG_EOR); + open($self->{$i}, '+<&=', $fd) and next; + send($sock, "open(+<&=$fd) (FD=$i): $!", MSG_EOR); } } else { return send($sock, "recv_cmd failed: $!", MSG_EOR); diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm index 5f38add1..08a1570d 100644 --- a/lib/PublicInbox/LeiToMail.pm +++ b/lib/PublicInbox/LeiToMail.pm @@ -227,9 +227,7 @@ sub decompress_src ($$$) { sub dup_src ($) { my ($in) = @_; - # fileno needed because wq_set_recv_modes only used ">&=" for {1} - # and Perl blindly trusts that to reject the '+' (readability flag) - open my $dup, '+>>&=', fileno($in) or die "dup: $!"; + open my $dup, '+>>&', $in or die "dup: $!"; $dup; } @@ -475,8 +473,7 @@ sub write_mail { # via ->wq_do sub ipc_atfork_prepare { my ($self) = @_; - # (done_wr, stdout|mbox, stderr, 3: sock, 4: each_smsg_done_wr) - $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= >&=]); + # FDs: (done_wr, stdout|mbox, stderr, 3: sock, 4: each_smsg_done_wr) $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC } diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm index 1c093a94..c396c597 100644 --- a/lib/PublicInbox/LeiXSearch.pm +++ b/lib/PublicInbox/LeiXSearch.pm @@ -389,9 +389,8 @@ sub ipc_atfork_prepare { require PublicInbox::MboxReader; require IO::Uncompress::Gunzip; } - # (0: done_wr, 1: stdout|mbox, 2: stderr, - # 3: sock, 4: $l2m->{-wq_s1}, 5: $startq) - $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&= <&=]); + # FDS: (0: done_wr, 1: stdout|mbox, 2: stderr, + # 3: sock, 4: $l2m->{-wq_s1}, 5: $startq) $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC } diff --git a/script/lei b/script/lei index 8c40bf12..006c1180 100755 --- a/script/lei +++ b/script/lei @@ -23,20 +23,19 @@ sub sigchld { sub exec_cmd { my ($fds, $argc, @argv) = @_; - my @m = (*STDIN{IO}, '<&=', *STDOUT{IO}, '>&=', *STDERR{IO}, '>&='); + my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO}); my @rdr; for my $fd (@$fds) { - my ($old_io, $mode) = splice(@m, 0, 2); - open(my $tmpfh, $mode, $fd) or die "open $mode$fd: $!"; - push @rdr, $old_io, $mode, $tmpfh; + open(my $tmpfh, '+<&=', $fd) or die "open +<&=$fd: $!"; + push @rdr, shift(@old), $tmpfh; } require POSIX; # WNOHANG $SIG{CHLD} = \&sigchld; my $pid = fork // die "fork: $!"; if ($pid == 0) { my %env = map { split(/=/, $_, 2) } splice(@argv, $argc); - while (my ($old_io, $mode, $tmpfh) = splice(@rdr, 0, 3)) { - open $old_io, $mode, $tmpfh or die "open $mode: $!"; + while (my ($old_io, $tmpfh) = splice(@rdr, 0, 2)) { + open $old_io, '+<&', $tmpfh or die "open +<&=: $!"; } %ENV = (%ENV, %env); exec(@argv); diff --git a/t/ipc.t b/t/ipc.t index f25f2491..5801c760 100644 --- a/t/ipc.t +++ b/t/ipc.t @@ -119,7 +119,6 @@ $test->('local'); $ipc->ipc_worker_stop; # idempotent # work queues -$ipc->wq_set_recv_modes(qw( +>&= >&= >&= )); pipe(my ($ra, $wa)) or BAIL_OUT $!; pipe(my ($rb, $wb)) or BAIL_OUT $!; pipe(my ($rc, $wc)) or BAIL_OUT $!;