]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LEI.pm
lei: fork + FD cleanup
[public-inbox.git] / lib / PublicInbox / LEI.pm
index 24f5930bdab58297d0e87263d68e2ca7a7d3f0ff..1ef0cbecc2c3793a4a71f6df0d0dbaabb4912e79 100644 (file)
@@ -33,6 +33,7 @@ my $GLP_PASS = Getopt::Long::Parser->new;
 $GLP_PASS->configure(qw(gnu_getopt no_ignore_case auto_abbrev pass_through));
 
 our %PATH2CFG; # persistent for socket daemon
+our @TO_CLOSE_ATFORK_CHILD;
 
 # TBD: this is a documentation mechanism to show a subcommand
 # (may) pass options through to another command:
@@ -240,15 +241,12 @@ my %CONFIG_KEYS = (
 sub x_it ($$) { # pronounced "exit"
        my ($self, $code) = @_;
        $self->{1}->autoflush(1); # make sure client sees stdout before exit
-       if (my $sig = ($code & 127)) {
-               kill($sig, $self->{pid} // $$);
-       } else {
-               $code >>= 8;
-               if (my $sock = $self->{sock}) {
-                       say $sock "exit=$code";
-               } else { # for oneshot
-                       $quit->($code);
-               }
+       my $sig = ($code & 127);
+       $code >>= 8 unless $sig;
+       if (my $sock = $self->{sock}) {
+               say $sock "exit=$code";
+       } else { # for oneshot
+               $quit->($code);
        }
 }
 
@@ -269,6 +267,44 @@ sub fail ($$;$) {
        undef;
 }
 
+sub atfork_prepare_wq {
+       my ($self, $wq) = @_;
+       push @{$wq->{-ipc_atfork_child_close}}, @TO_CLOSE_ATFORK_CHILD,
+                               grep { defined } @$self{qw(0 1 2 sock)}
+}
+
+# usage: local %SIG = (%SIG, $lei->atfork_child_wq($wq));
+sub atfork_child_wq {
+       my ($self, $wq) = @_;
+       $self->{sock} //= $wq->{0};
+       $self->{$_} //= $wq->{$_} for (0..2);
+       my $oldpipe = $SIG{PIPE};
+       %PATH2CFG = ();
+       @TO_CLOSE_ATFORK_CHILD = ();
+       (
+               __WARN__ => sub { err($self, @_) },
+               PIPE => sub {
+                       $self->x_it(141);
+                       $oldpipe->() if ref($oldpipe) eq 'CODE';
+               }
+       );
+}
+
+# usage: ($lei, @io) = $lei->atfork_parent_wq($wq);
+sub atfork_parent_wq {
+       my ($self, $wq) = @_;
+       if ($wq->wq_workers) {
+               my $env = delete $self->{env}; # env is inherited at fork
+               my $ret = bless { %$self }, ref($self);
+               $self->{env} = $env;
+               delete @$ret{qw(-lei_store cfg)};
+               my $in = delete $ret->{0};
+               ($ret, delete($ret->{sock}) // $in, delete @$ret{1, 2});
+       } else {
+               ($self, ($self->{sock} // $self->{0}), @$self{1, 2});
+       }
+}
+
 sub _help ($;$) {
        my ($self, $errmsg) = @_;
        my $cmd = $self->{cmd} // 'COMMAND';
@@ -608,8 +644,8 @@ sub start_pager {
        $self->{1} = $wpager;
        $self->{2} = $wpager if -t $self->{2};
        my $pid = spawn([$pager], $env, $rdr);
-       dwaitpid($pid, undef, $self->{sock});
        $env->{GIT_PAGER_IN_USE} = 'true'; # we may spawn git
+       [ $pid, @$rdr{1, 2} ];
 }
 
 sub accept_dispatch { # Listener {post_accept} callback
@@ -648,13 +684,12 @@ sub accept_dispatch { # Listener {post_accept} callback
                say $sock "request command truncated";
                return;
        }
-       my ($client_pid, $argc, @argv) = split(/\0/, $buf, -1);
+       my ($argc, @argv) = split(/\0/, $buf, -1);
        undef $buf;
        my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
        if (chdir($env{PWD})) {
                local %ENV = %env;
                $self->{env} = \%env;
-               $self->{pid} = $client_pid + 0;
                eval { dispatch($self, @argv) };
                say $sock $@ if $@;
        } else {
@@ -675,6 +710,8 @@ sub event_step {
 
 sub noop {}
 
+our $oldset; sub oldset { $oldset }
+
 # lei(1) calls this when it can't connect
 sub lazy_start {
        my ($path, $errno, $nfd) = @_;
@@ -691,7 +728,7 @@ sub lazy_start {
        my @st = stat($path) or die "stat($path): $!";
        my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
        pipe(my ($eof_r, $eof_w)) or die "pipe: $!";
-       my $oldset = PublicInbox::DS::block_signals();
+       local $oldset = PublicInbox::DS::block_signals();
        if ($nfd == 1) {
                require PublicInbox::CmdIPC1;
                $recv_cmd = PublicInbox::CmdIPC1->can('recv_cmd1');
@@ -713,6 +750,7 @@ sub lazy_start {
        return if $pid;
        $0 = "lei-daemon $path";
        local %PATH2CFG;
+       local @TO_CLOSE_ATFORK_CHILD = ($l, $eof_r, $eof_w);
        $_->blocking(0) for ($l, $eof_r, $eof_w);
        $l = PublicInbox::Listener->new($l, \&accept_dispatch, $l);
        my $exit_code;
@@ -737,7 +775,9 @@ sub lazy_start {
        };
        my $sigfd = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
        local %SIG = (%SIG, %$sig) if !$sigfd;
+       local $SIG{PIPE} = 'IGNORE';
        if ($sigfd) { # TODO: use inotify/kqueue to detect unlinked sockets
+               push @TO_CLOSE_ATFORK_CHILD, $sigfd->{sock};
                PublicInbox::DS->SetLoopTimeout(5000);
        } else {
                # wake up every second to accept signals if we don't
@@ -795,9 +835,9 @@ sub oneshot {
        local %PATH2CFG;
        umask(077) // die("umask(077): $!");
        dispatch((bless {
-               0 => *STDIN{IO},
-               1 => *STDOUT{IO},
-               2 => *STDERR{IO},
+               0 => *STDIN{GLOB},
+               1 => *STDOUT{GLOB},
+               2 => *STDERR{GLOB},
                env => \%ENV
        }, __PACKAGE__), @ARGV);
 }