]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/CmdIPC1.pm
cmd_ipc: send FDs with buffer payload
[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, in, out, err, buf, flags) = @_;
14         for (1..3) {
15                 IO::FDPass::send(fileno($_[0]), $_[$_]) or
16                                         die "IO::FDPass::send: $!";
17         }
18         send($_[0], $_[4], $_[5]) or die "send $!";
19 };
20
21 *recv_cmd1 = sub ($$$) {
22         my ($s, undef, $len) = @_;
23         my @fds = map { IO::FDPass::recv(fileno($s)) } (0..2);
24         recv($s, $_[1], $len, 0) // die "recv: $!";
25         length($_[1]) == 0 ? () : @fds;
26 };
27
28 } } # /eval /BEGIN
29
30 1;