From 65ec1f78464ce8cf4f6379aca7bbae38743c50f0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 26 Mar 2021 09:51:25 +0000 Subject: lei: support /dev/fd/[0-2] inputs and outputs in daemon Since lei-daemon won't have the same FDs as the client, we need to special-case thse mappings and won't be able to open arbitrary, non-standard FDs. We also won't attempt to support /proc/self/fd/[0-2] since that's a Linux-ism. /dev/fd/[0-2] and /dev/std{in,out,err} are portable to FreeBSD, at least. mawk(1) also supports /dev/std{out,err}, as does gawk(1) (which supports everything we can support, and arbitrary /dev/fd/$FD). --- lib/PublicInbox/LEI.pm | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'lib/PublicInbox/LEI.pm') diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 59715633..eb3ad9e2 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -464,7 +464,6 @@ sub _lei_atfork_child { } } else { # worker, Net::NNTP (Net::Cmd) uses STDERR directly open STDERR, '+>&='.fileno($self->{2}) or warn "open $!"; - delete $self->{0}; } for (delete @$self{qw(3 old_1 au_done)}) { close($_) if defined($_); @@ -929,19 +928,17 @@ sub poke_mua { # forces terminal MUAs to wake up and hopefully notice new mail } my %path_to_fd = ('/dev/stdin' => 0, '/dev/stdout' => 1, '/dev/stderr' => 2); -$path_to_fd{"/dev/fd/$_"} = $path_to_fd{"/proc/self/fd/$_"} for (0..2); -sub fopen { - my ($self, $mode, $path) = @_; - rel2abs($self, $path); +$path_to_fd{"/dev/fd/$_"} = $_ for (0..2); + +# this also normalizes the path +sub path_to_fd { + my ($self, $path) = @_; + $path = rel2abs($self, $path); $path =~ tr!/!/!s; - if (defined(my $fd = $path_to_fd{$path})) { - return $self->{$fd}; - } - if ($path =~ m!\A/(?:dev|proc/self)/fd/[0-9]+\z!) { - return fail($self, "cannot open $path from daemon"); - } - open my $fh, $mode, $path or return; - $fh; + $path_to_fd{$path} // ( + ($path =~ m!\A/(?:dev|proc/self)/fd/[0-9]+\z!) ? + fail($self, "cannot open $path from daemon") : -1 + ); } # caller needs to "-t $self->{1}" to check if tty -- cgit v1.2.3-24-ge0c7