about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-01-13 19:06:14 -1200
committerEric Wong <e@80x24.org>2021-01-14 23:14:08 +0000
commit7dd5b28cb9bdcfa262ddad47d7f033f600675dc3 (patch)
treec55a8afc5163ad33b37911fabbfd699b1b953143 /lib/PublicInbox
parentb0898d18efbc8f646b736088f9600b87be88f91e (diff)
downloadpublic-inbox-7dd5b28cb9bdcfa262ddad47d7f033f600675dc3.tar.gz
We'll ensure our {send,recv}_cmd4 implementations are
consistent w.r.t. non-blocking and interrupted sockets.

We'll also support receiving messages without FDs associated
so we don't have to send dummy FDs to keep receivers from
reporting EOF.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/CmdIPC4.pm6
-rw-r--r--lib/PublicInbox/Spawn.pm13
2 files changed, 11 insertions, 8 deletions
diff --git a/lib/PublicInbox/CmdIPC4.pm b/lib/PublicInbox/CmdIPC4.pm
index c4fcb0d6..c244f6a1 100644
--- a/lib/PublicInbox/CmdIPC4.pm
+++ b/lib/PublicInbox/CmdIPC4.pm
@@ -18,17 +18,17 @@ no warnings 'once';
         my $mh = Socket::MsgHdr->new(buf => $_[2]);
         $mh->cmsghdr(SOL_SOCKET, SCM_RIGHTS,
                         pack('i' x scalar(@$fds), @$fds));
-        Socket::MsgHdr::sendmsg($sock, $mh, $flags) or die "sendmsg: $!";
+        Socket::MsgHdr::sendmsg($sock, $mh, $flags);
 };
 
 *recv_cmd4 = sub ($$$) {
         my ($s, undef, $len) = @_; # $_[1] = destination buffer
         my $mh = Socket::MsgHdr->new(buflen => $len, controllen => 256);
-        my $r = Socket::MsgHdr::recvmsg($s, $mh, 0) // die "recvmsg: $!";
+        my $r = Socket::MsgHdr::recvmsg($s, $mh, 0) // return ($_[1] = undef);
         $_[1] = $mh->buf;
         return () if $r == 0;
         my (undef, undef, $data) = $mh->cmsghdr;
-        unpack('i' x (length($data) / 4), $data);
+        defined($data) ? unpack('i' x (length($data) / 4), $data) : ();
 };
 
 } } # /eval /BEGIN
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index ef822e1b..e5c0b1e9 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -216,12 +216,13 @@ union my_cmsg {
         char pad[sizeof(struct cmsghdr) + 16 + SEND_FD_SPACE];
 };
 
-int send_cmd4(PerlIO *s, SV *svfds, SV *data, int flags)
+SV *send_cmd4(PerlIO *s, SV *svfds, SV *data, int flags)
 {
         struct msghdr msg = { 0 };
         union my_cmsg cmsg = { 0 };
         STRLEN dlen = 0;
         struct iovec iov;
+        ssize_t sent;
         AV *fds = (AV *)SvRV(svfds);
         I32 i, nfds = av_len(fds) + 1;
         int *fdp;
@@ -252,7 +253,8 @@ int send_cmd4(PerlIO *s, SV *svfds, SV *data, int flags)
                         *fdp++ = SvIV(*fd);
                 }
         }
-        return sendmsg(PerlIO_fileno(s), &msg, flags) >= 0;
+        sent = sendmsg(PerlIO_fileno(s), &msg, flags);
+        return sent >= 0 ? newSViv(sent) : &PL_sv_undef;
 }
 
 void recv_cmd4(PerlIO *s, SV *buf, STRLEN n)
@@ -260,7 +262,7 @@ void recv_cmd4(PerlIO *s, SV *buf, STRLEN n)
         union my_cmsg cmsg = { 0 };
         struct msghdr msg = { 0 };
         struct iovec iov;
-        size_t i;
+        ssize_t i;
         Inline_Stack_Vars;
         Inline_Stack_Reset;
 
@@ -275,8 +277,9 @@ void recv_cmd4(PerlIO *s, SV *buf, STRLEN n)
 
         i = recvmsg(PerlIO_fileno(s), &msg, 0);
         if (i < 0)
-                croak("recvmsg: %s", strerror(errno));
-        SvCUR_set(buf, i);
+                Inline_Stack_Push(&PL_sv_undef);
+        else
+                SvCUR_set(buf, i);
         if (i > 0 && cmsg.hdr.cmsg_level == SOL_SOCKET &&
                         cmsg.hdr.cmsg_type == SCM_RIGHTS) {
                 size_t len = cmsg.hdr.cmsg_len;