user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/4] lei: support /dev/fd/[0-2] inputs and outputs in daemon
Date: Fri, 26 Mar 2021 09:51:25 +0000	[thread overview]
Message-ID: <20210326095126.8184-4-e@80x24.org> (raw)
In-Reply-To: <20210326095126.8184-1-e@80x24.org>

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 ++++++++++-------------
 lib/PublicInbox/LeiConvert.pm  |  3 ++-
 lib/PublicInbox/LeiInput.pm    |  5 ++++-
 lib/PublicInbox/LeiOverview.pm | 13 +++++++------
 lib/PublicInbox/LeiP2q.pm      |  7 +++++--
 lib/PublicInbox/LeiToMail.pm   | 12 ++++++++----
 t/lei-convert.t                |  2 +-
 t/lei-import.t                 |  2 +-
 8 files changed, 38 insertions(+), 29 deletions(-)

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
diff --git a/lib/PublicInbox/LeiConvert.pm b/lib/PublicInbox/LeiConvert.pm
index 0cc65108..083ecc33 100644
--- a/lib/PublicInbox/LeiConvert.pm
+++ b/lib/PublicInbox/LeiConvert.pm
@@ -50,7 +50,8 @@ sub lei_convert { # the main "lei convert" method
 	my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
 	$lei->{l2m} or return
 		$lei->fail("output not specified or is not a mail destination");
-	$lei->{opt}->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
+	my $devfd = $lei->path_to_fd($ovv->{dst}) // return;
+	$lei->{opt}->{augment} = 1 if $devfd < 0;
 	$self->prepare_inputs($lei, \@inputs) or return;
 	my $op = $lei->workers_start($self, 'lei_convert', 1);
 	$lei->{cnv} = $self;
diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm
index b059ecda..eed0eed7 100644
--- a/lib/PublicInbox/LeiInput.pm
+++ b/lib/PublicInbox/LeiInput.pm
@@ -67,7 +67,10 @@ sub input_path_url {
 		return;
 	}
 	$input =~ s!\A([a-z0-9]+):!!i and $ifmt = lc($1);
-	if (-f $input) {
+	my $devfd = $lei->path_to_fd($input) // return;
+	if ($devfd >= 0) {
+		$self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
+	} elsif (-f $input) {
 		my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
 				PublicInbox::MboxLock->defaults);
 		my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
diff --git a/lib/PublicInbox/LeiOverview.pm b/lib/PublicInbox/LeiOverview.pm
index 96bfff24..8e26cba4 100644
--- a/lib/PublicInbox/LeiOverview.pm
+++ b/lib/PublicInbox/LeiOverview.pm
@@ -71,8 +71,9 @@ sub new {
 --$ofmt_key=$fmt and --output=$ofmt conflict
 
 	}
-	$fmt //= 'json' if $dst eq '/dev/stdout';
-	$fmt //= detect_fmt($lei, $dst) or return;
+
+	my $devfd = $lei->path_to_fd($dst) // return;
+	$fmt //= $devfd >= 0 ? 'json' : (detect_fmt($lei, $dst) or return);
 
 	if (index($dst, '://') < 0) { # not a URL, so assume path
 		 $dst = File::Spec->canonpath($dst);
@@ -84,11 +85,11 @@ sub new {
 	if ($fmt =~ /\A($JSONL|(?:concat)?json)\z/) {
 		$json = $self->{json} = ref(PublicInbox::Config->json);
 	}
-	if ($dst eq '/dev/stdout') {
-		my $isatty = $lei->{need_pager} = -t $lei->{1};
+	if ($devfd >= 0) {
+		my $isatty = $lei->{need_pager} = -t $lei->{$devfd};
 		$opt->{pretty} //= $isatty;
 		if (!$isatty && -f _) {
-			my $fl = fcntl($lei->{1}, F_GETFL, 0) //
+			my $fl = fcntl($lei->{$devfd}, F_GETFL, 0) //
 				return $lei->fail("fcntl(stdout): $!");
 			ovv_out_lk_init($self) unless ($fl & O_APPEND);
 		} else {
@@ -101,7 +102,7 @@ sub new {
 		$lei->{dedupe} //= PublicInbox::LeiDedupe->new($lei);
 	} else {
 		# default to the cheapest sort since MUA usually resorts
-		$opt->{'sort'} //= 'docid' if $dst ne '/dev/stdout';
+		$opt->{'sort'} //= 'docid' if $devfd < 0;
 		$lei->{l2m} = eval { PublicInbox::LeiToMail->new($lei) };
 		return $lei->fail($@) if $@;
 		if ($opt->{mua} && $lei->{l2m}->lock_free) {
diff --git a/lib/PublicInbox/LeiP2q.pm b/lib/PublicInbox/LeiP2q.pm
index fda055fe..25f63a10 100644
--- a/lib/PublicInbox/LeiP2q.pm
+++ b/lib/PublicInbox/LeiP2q.pm
@@ -107,8 +107,11 @@ sub do_p2q { # via wq_do
 	my $in = $self->{0};
 	unless ($in) {
 		my $input = $self->{input};
-		if (-e $input) {
-			$in = $lei->fopen('<', $input) or
+		my $devfd = $lei->path_to_fd($input) // return;
+		if ($devfd >= 0) {
+			$in = $lei->{$devfd};
+		} elsif (-e $input) {
+			open($in, '<', $input) or
 				return $lei->fail("open < $input: $!");
 		} else {
 			my @cmd = (qw(git format-patch --stdout -1), $input);
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index f71f74cc..88468c34 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -503,10 +503,12 @@ sub _do_augment_imap {
 sub _pre_augment_mbox {
 	my ($self, $lei) = @_;
 	my $dst = $lei->{ovv}->{dst};
-	my $out = $lei->{1};
-	if ($dst ne '/dev/stdout') {
+	my $out;
+	my $devfd = $lei->path_to_fd($dst) // die "bad $dst";
+	if ($devfd >= 0) {
+		$out = $lei->{$devfd};
+	} else { # normal-looking path
 		if (-p $dst) {
-			$out = undef;
 			open $out, '>', $dst or die "open($dst): $!";
 		} elsif (-f _ || !-e _) {
 			require PublicInbox::MboxLock;
@@ -514,12 +516,14 @@ sub _pre_augment_mbox {
 					PublicInbox::MboxLock->defaults;
 			$self->{mbl} = PublicInbox::MboxLock->acq($dst, 1, $m);
 			$out = $self->{mbl}->{fh};
+		} else {
+			die "$dst is not a file or FIFO\n";
 		}
 		$lei->{old_1} = $lei->{1}; # keep for spawning MUA
 	}
 	# Perl does SEEK_END even with O_APPEND :<
 	$self->{seekable} = seek($out, 0, SEEK_SET);
-	if (!$self->{seekable} && $! != ESPIPE && $dst ne '/dev/stdout') {
+	if (!$self->{seekable} && $! != ESPIPE && !defined($devfd)) {
 		die "seek($dst): $!\n";
 	}
 	if (!$self->{seekable}) {
diff --git a/t/lei-convert.t b/t/lei-convert.t
index e147715d..9b430d8e 100644
--- a/t/lei-convert.t
+++ b/t/lei-convert.t
@@ -87,7 +87,7 @@ test_lei({ tmpdir => $tmpdir }, sub {
 	my $exp = do { local $/; <$fh> };
 	is($out, $exp, 'stdin => stdout');
 
-	lei_ok qw(convert -F eml -o mboxcl2:/dev/stdout t/plack-qp.eml);
+	lei_ok qw(convert -F eml -o mboxcl2:/dev/fd/1 t/plack-qp.eml);
 	open $fh, '<', \$lei_out or BAIL_OUT;
 	@bar = ();
 	PublicInbox::MboxReader->mboxcl2($fh, sub {
diff --git a/t/lei-import.t b/t/lei-import.t
index a697d756..fa40ad01 100644
--- a/t/lei-import.t
+++ b/t/lei-import.t
@@ -69,7 +69,7 @@ is($res->[0]->{kw}, undef, 'no keywords set');
 
 $eml->header_set('Message-ID', '<k@y>');
 $in = 'From k@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
-lei_ok([qw(import -F mboxrd -)], undef, { %$lei_opt, 0 => \$in },
+lei_ok([qw(import -F mboxrd /dev/fd/0)], undef, { %$lei_opt, 0 => \$in },
 	\'import single file with --kw (default) from stdin');
 lei(qw(q m:k@y));
 $res = json_utf8->decode($lei_out);

  parent reply	other threads:[~2021-03-26  9:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-26  9:51 [PATCH 0/4] lei minor things Eric Wong
2021-03-26  9:51 ` [PATCH 1/4] lei q: skip lei/store->write_prepare for JSON outputs Eric Wong
2021-03-26  9:51 ` [PATCH 2/4] lei: do not blindly commit to lei/store on close Eric Wong
2021-03-26  9:51 ` Eric Wong [this message]
2021-03-26 23:07   ` [SQUASH] account for /dev/* FDs in prepare_inputs, too Eric Wong
2021-03-26  9:51 ` [PATCH 4/4] lei mark: disallow '!' in labels Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210326095126.8184-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 3/4] lei: support /dev/fd/[0-2] inputs and outputs in daemon' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.mail.public-inbox.meta
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.mail.public-inbox.meta
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git