user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] script/lei: background ourselves on MUA/pager exec
@ 2021-03-31 23:29 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-03-31 23:29 UTC (permalink / raw)
  To: meta

This ought to gives the MUA or pager exclusive access to the
controlling terminal.  The downside is we can only exec the
pager or MUA once per invocation, but I can't imagine a valid
case for running those things multiple times, either.

Note: I'm no expert when it comes to terminal control matters,
but this allows Ctrl-Z-ed mutt instance to come back and is
a nice code reduction, as well.
---
 script/lei | 37 +++++++++++++++----------------------
 1 file changed, 15 insertions(+), 22 deletions(-)

diff --git a/script/lei b/script/lei
index cb605e2e..bea8dcde 100755
--- a/script/lei
+++ b/script/lei
@@ -14,36 +14,31 @@ my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
 	PublicInbox::Spawn->can('send_cmd4');
 };
 
-my %pids;
-my $sigchld = sub {
-	my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
-	for my $pid (keys %pids) {
-		delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
-	}
-};
-
+my @orig_pid;
 my $exec_cmd = sub {
 	my ($fds, $argc, @argv) = @_;
+	die "BUG: already exec-ed\n" if @orig_pid;
+	@orig_pid = ($$);
+	require POSIX; # WNOHANG
 	my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
 	my @rdr;
 	for my $fd (@$fds) {
 		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, $tmpfh) = splice(@rdr, 0, 2)) {
-			open $old_io, '+<&', $tmpfh or die "open +<&=: $!";
-		}
-		%ENV = (%ENV, %env);
-		exec(@argv);
-		warn "exec: @argv: $!\n";
-		POSIX::_exit(1);
+		POSIX::setsid() > 0 or die "setsid: $!";
+		return; # continue $recv_cmd in background
+	}
+	my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
+	while (my ($old_io, $tmpfh) = splice(@rdr, 0, 2)) {
+		open $old_io, '+<&', $tmpfh or die "open +<&=: $!";
 	}
-	$pids{$pid} = 1;
+	@ENV{keys %env} = values %env;
+	exec(@argv);
+	warn "exec: @argv: $!\n";
+	POSIX::_exit(1);
 };
 
 if ($send_cmd && eval {
@@ -100,18 +95,16 @@ Falling back to (slow) one-shot mode
 		if ($buf =~ /\Aexec (.+)\z/) {
 			$exec_cmd->(\@fds, split(/\0/, $1));
 		} elsif ($buf eq '-WINCH') {
-			kill($buf, $$); # for MUA
+			kill($buf, @orig_pid); # for MUA
 		} elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
 			$x_it_code = $1 + 0;
 			last;
 		} elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
 			$x_it_code = $1 + 0;
 		} else {
-			$sigchld->();
 			die $buf;
 		}
 	}
-	$sigchld->();
 	if (my $sig = ($x_it_code & 127)) {
 		kill $sig, $$;
 		sleep(1) while 1;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-31 23:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31 23:29 [PATCH] script/lei: background ourselves on MUA/pager exec Eric Wong

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://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.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