]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ipc: get rid of wq_set_recv_modes
authorEric Wong <e@80x24.org>
Sun, 24 Jan 2021 11:46:49 +0000 (04:46 -0700)
committerEric Wong <e@80x24.org>
Sun, 24 Jan 2021 19:45:22 +0000 (15:45 -0400)
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
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiToMail.pm
lib/PublicInbox/LeiXSearch.pm
script/lei
t/ipc.t

index 592efd21f0f95fd4ddc81d53ce95344302b31b58..c52441f7bf2628d5710130d78149c049e69b9656 100644 (file)
@@ -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) {
index a9123c6e3c97603e3ee1917cac57912a1b1789ce..473a28a99ccdc5645e948408cb9d0bf308802030 100644 (file)
@@ -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);
index 5f38add1179c946e4209b73527ce656da25e219c..08a1570d21c71f20dfc8ae8c698b3b7561d6cc9e 100644 (file)
@@ -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
 }
 
index 1c093a94ca35389aadfdeabf622cfccd25af821a..c396c59734589b0b21bd4932da7a4688d9d3c750 100644 (file)
@@ -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
 }
 
index 8c40bf121602b8cbf0fd96e3054c4f27b0468f44..006c118089397ff9e670e1de52b56c6e0a59f0ee 100755 (executable)
@@ -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 f25f24917da877efed3d4f7fac5d6cfff41cfae2..5801c760cf8bcb960896138f4c05b75483eac774 100644 (file)
--- 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 $!;