]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/CmdIPC1.pm
lei: run pager in client script
[public-inbox.git] / lib / PublicInbox / CmdIPC1.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # callers should use PublicInbox::CmdIPC1->can('send_cmd1') (or recv_cmd1)
5 # 2nd choice for lei(1) front-end and 3rd choice for lei internals
6 package PublicInbox::CmdIPC1;
7 use strict;
8 use v5.10.1;
9 BEGIN { eval {
10 require IO::FDPass; # XS, available in all major distros
11 no warnings 'once';
12
13 *send_cmd1 = sub ($$$$) { # (sock, fds, buf, flags) = @_;
14         my ($sock, $fds, undef, $flags) = @_;
15         for my $fd (@$fds) {
16                 IO::FDPass::send(fileno($sock), $fd) or
17                                         die "IO::FDPass::send: $!";
18         }
19         send($sock, $_[2], $flags) or die "send $!";
20 };
21
22 *recv_cmd1 = sub ($$$;$) {
23         my ($s, undef, $len, $nfds) = @_;
24         $nfds //= 3;
25         my @fds = map { IO::FDPass::recv(fileno($s)) } (1..$nfds);
26         recv($s, $_[1], $len, 0) // die "recv: $!";
27         length($_[1]) == 0 ? () : @fds;
28 };
29
30 } } # /eval /BEGIN
31
32 1;