1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # callers should use PublicInbox::CmdIPC4->can('send_cmd4') (or recv_cmd4)
5 # first choice for script/lei front-end and 2nd choice for lei backend
6 # libsocket-msghdr-perl is in Debian but not many other distros as of 2021.
7 package PublicInbox::CmdIPC4;
9 use Socket qw(SOL_SOCKET SCM_RIGHTS);
11 require Socket::MsgHdr; # XS
14 # any number of FDs per-sendmsg(2) + buffer
15 *send_cmd4 = sub ($$$$) { # (sock, fds, buf, flags) = @_;
16 my ($sock, $fds, undef, $flags) = @_;
17 my $mh = Socket::MsgHdr->new(buf => $_[2]);
18 $mh->cmsghdr(SOL_SOCKET, SCM_RIGHTS, pack('i' x scalar(@$fds), @$fds));
22 $s = Socket::MsgHdr::sendmsg($sock, $mh, $flags);
23 } while (!defined($s) &&
24 ($!{ENOBUFS} || $!{ENOMEM} || $!{ETOOMANYREFS}) &&
26 warn "sleeping on sendmsg: $! (#$try)\n" &&
27 select(undef, undef, undef, 0.1) == 0);
31 *recv_cmd4 = sub ($$$) {
32 my ($s, undef, $len) = @_; # $_[1] = destination buffer
33 my $mh = Socket::MsgHdr->new(buflen => $len, controllen => 256);
34 my $r = Socket::MsgHdr::recvmsg($s, $mh, 0) // return (undef);
37 my (undef, undef, $data) = $mh->cmsghdr;
38 defined($data) ? unpack('i' x (length($data) / 4), $data) : ();