2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 use Socket qw(AF_UNIX SOCK_STREAM MSG_EOR);
9 pipe(my ($r, $w)) or BAIL_OUT;
11 require_ok 'PublicInbox::Spawn';
12 my $SOCK_SEQPACKET = eval { Socket::SOCK_SEQPACKET() } // undef;
14 my $do_test = sub { SKIP: {
15 my ($type, $flag, $desc) = @_;
16 defined $type or skip 'SOCK_SEQPACKET missing', 7;
18 my $src = 'some payload' x 40;
19 socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
20 my $sfds = [ fileno($r), fileno($w), fileno($s1) ];
21 $send->($s1, $sfds, $src, $flag);
22 my (@fds) = $recv->($s2, my $buf, length($src) + 1);
23 is($buf, $src, 'got buffer payload '.$desc);
26 ok(open($r1, '<&=', $fds[0]), 'opened received $r');
27 ok(open($w1, '>&=', $fds[1]), 'opened received $w');
28 ok(open($s1a, '+>&=', $fds[2]), 'opened received $s1');
33 is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$r dev/ino matches');
36 is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$w dev/ino matches');
39 is("$exp[0]\0$exp[1]", "$cur[0]\0$cur[1]", '$s1 dev/ino matches');
40 if (defined($SOCK_SEQPACKET) && $type == $SOCK_SEQPACKET) {
41 $r1 = $w1 = $s1a = undef;
42 $src = (',' x 1023) . '-' .('.' x 1024);
43 $send->($s1, $sfds, $src, $flag);
44 (@fds) = $recv->($s2, $buf, 1024);
45 is($buf, (',' x 1023) . '-', 'silently truncated buf');
47 $r1 = $w1 = $s1a = undef;
50 @fds = $recv->($s2, $buf, length($src) + 1);
51 ok($!{EAGAIN}, "EAGAIN set by ($desc)");
52 is_deeply(\@fds, [ undef ], "EAGAIN $desc");
55 if ($ENV{TEST_ALRM}) {
57 local $SIG{ALRM} = sub { $alrm++ };
59 my $pid = fork // xbail "fork: $!";
61 # need to loop since Perl signals are racy
62 # (the interpreter doesn't self-pipe)
67 @fds = $recv->($s2, $buf, length($src) + 1);
68 ok($!{EINTR}, "EINTR set by ($desc)");
71 is_deeply(\@fds, [ undef ], "EINTR $desc");
72 ok($alrm, 'SIGALRM hit');
76 @fds = $recv->($s2, $buf, length($src) + 1);
77 is_deeply(\@fds, [], "no FDs on EOF $desc");
78 is($buf, '', "buffer cleared on EOF ($desc)");
80 socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
83 while (defined(my $n = $send->($s1, $sfds, $src, $flag))) {
85 fail "sent 0 bytes" if $n == 0;
87 ok($!{EAGAIN} || $!{ETOOMANYREFS},
88 "hit EAGAIN || ETOOMANYREFS on send $desc") or
89 diag "send failed with: $!";
90 ok($nsent > 0, 'sent some bytes');
92 socketpair($s1, $s2, AF_UNIX, $type, 0) or BAIL_OUT $!;
93 is($send->($s1, [], $src, $flag), length($src), 'sent w/o FDs');
95 @fds = $recv->($s2, $buf, length($src));
96 is(scalar(@fds), 0, 'no FDs received');
97 is($buf, $src, 'recv w/o FDs');
99 my $nr = 2 * 1024 * 1024;
101 vec(my $vec = '', $nr * 8 - 1, 1) = 1;
102 my $n = $send->($s1, [], $vec, $flag);
104 $n == length($vec) or
105 fail "short send: $n != ".length($vec);
106 diag "sent $nr, retrying with more";
107 $nr += 2 * 1024 * 1024;
109 ok($!{EMSGSIZE} || $!{ENOBUFS},
110 'got EMSGSIZE or ENOBUFS') or
111 diag "$nr bytes fails with: $!";
118 my $send_ic = PublicInbox::Spawn->can('send_cmd4');
119 my $recv_ic = PublicInbox::Spawn->can('recv_cmd4');
121 ($send_ic && $recv_ic) or skip 'Inline::C not installed/enabled', 12;
124 $do_test->(SOCK_STREAM, 0, 'Inline::C stream');
125 $do_test->($SOCK_SEQPACKET, MSG_EOR, 'Inline::C seqpacket');
129 require_mods('Socket::MsgHdr', 13);
130 require_ok 'PublicInbox::CmdIPC4';
131 $send = PublicInbox::CmdIPC4->can('send_cmd4');
132 $recv = PublicInbox::CmdIPC4->can('recv_cmd4');
133 $do_test->(SOCK_STREAM, 0, 'MsgHdr stream');
134 $do_test->($SOCK_SEQPACKET, MSG_EOR, 'MsgHdr seqpacket');
136 ($send_ic && $recv_ic) or
137 skip 'Inline::C not installed/enabled', 12;
139 $do_test->(SOCK_STREAM, 0, 'Inline::C -> MsgHdr stream');
140 $do_test->($SOCK_SEQPACKET, 0, 'Inline::C -> MsgHdr seqpacket');
145 skip 'not Linux', 1 if $^O ne 'linux';
146 require_ok 'PublicInbox::Syscall';
147 $send = PublicInbox::Syscall->can('send_cmd4') or
148 skip 'send_cmd4 not defined for arch';
149 $recv = PublicInbox::Syscall->can('recv_cmd4') or
150 skip 'recv_cmd4 not defined for arch';
151 $do_test->(SOCK_STREAM, 0, 'PP Linux stream');
152 $do_test->($SOCK_SEQPACKET, MSG_EOR, 'PP Linux seqpacket');