lib/App/teai/Exec.pm

filter: Statementstmt 15.6% 51 / 327 Branchbran 0.6% 1 / 158 Conditioncond 0.0% 0 / 101 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 19.7% 13 / 66 Podpod 100% 1 / 1 totaltotal 9.2% 66 / 712 CC 135 SCAR 43.5
CC
135
Cov
9%
_reply
272.0
_run
182.0
__ANON__
182.0
CRAP 77.4
« lib/App/teai/Round.pm ↑ Summary lib/App/teai/Dash/Rounds.pm »
1 package App::teai::Exec;
2 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Fcntl qw(F_DUPFD F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use App::teai::Err;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use App::teai::Bus ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use App::teai::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use App::teai::Wire ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use App::teai::Presence ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.08';
15  
16 2 my $TAIL = 4096;
17 2 my $CHUNK = 65536;
18 2 my $MAX_FD = 65536;
19 2 my $REPLY_PERSIST = 60;
20 2 my $TIMEOUT = 3600;
21 2 my $KILL_GRACE = 5;
22 2 my $PIPE_GRACE = 2;
23 2 my $OUT_KEEP = App::teai::Wire::MAX_PAYLOAD + 4;
24 2 my $BAD_UTF8 = 'input was not valid UTF-8; invalid bytes replaced';
25 2 my %SIGNAL_EXIT = (INT => [130, 'interrupted'], TERM => [143, 'terminated'], HUP => [129, 'hangup']);
26 2 my $ACK_TIMEOUT_MS = 5000;
27 2 my $PR_SET_PDEATHSIG = 1;
28 2 my $PRCTL = _prctl($Config::Config{archname});
29  
30 0 ▶sub _knob ($name, $default) {
Subroutine _knob: not called CC=4 SCAR=30.0
31 0 ▶ my $v = $ENV{$name} // '';
Condition0%0/2
$ENV{$name} // ""
l!l
00
MC/DC0%0/1
$ENV{$name} // ""
$ENV{$name}
Truth table0%0/2
A
A= $ENV{$name}
Aresult
11
00
32 0 ▶ return $v =~ /\A\d+(?:\.\d+)?\z/ && $v > 0 ? $v : $default;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A\d+(?:\.\d+)?\z/u && $v > 0 ? : 0 0 0
Condition0%0/3
$v =~ /\A\d+(?:\.\d+)?\z/u and $v > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$v =~ /\A\d+(?:\.\d+)?\z/u and $v > 0
$v =~ /\A\d+(?:\.\d+)?\z/u $v > 0
Truth table0%0/3
A and B
A= $v =~ /\A\d+(?:\.\d+)?\z/uB= $v > 0
ABresult
0X0
100
111
33 }
34  
35 0 ▶sub _msg ($e) {
Subroutine _msg: not called CC=3 SCAR=24.8
36 0 ▶ return App::teai::Err::msg($e) if ref $e && eval { $e->isa('App::teai::Err') };
Branch: 1 branch
BranchTrueFalseTotal
if ref $e and eval { do { $e->isa("App::teai::Err") } } 0 0 0
Condition0%0/3
ref $e and eval { do { $e->isa("App::teai::Err") } }
!ll&&!rl&&r
000
MC/DC0%0/2
ref $e and eval { do { $e->isa("App::teai::Err") } }
ref $e eval { do { $e->isa("App::teai::Err") } }
Truth table0%0/3
A and B
A= ref $eB= eval { do { $e->isa("App::teai::Err") } }
ABresult
0X0
100
111
37 0 return $e =~ s/\s+at \S+ line \d+\.?\s*\z//r =~ s/\s+\z//r;
38 }
39  
40 2 ▶sub _prctl ($arch) {
Subroutine _prctl: called CC=4 SCAR=25.0
41 2 ▶ return 157 if $arch =~ /\Ax86_64-/;
Branch: 1 branch
BranchTrueFalseTotal
if $arch =~ /\Ax86_64-/u 2 0 2
42 0 ▶ return 167 if $arch =~ /\A(?:aarch64|riscv64)-/;
Branch: 1 branch
BranchTrueFalseTotal
if $arch =~ /\A(?:aarch64|riscv64)-/u 0 0 0
43 0 ▶ return 172 if $arch =~ /\A(?:i[3-6]86|arm)/;
Branch: 1 branch
BranchTrueFalseTotal
if $arch =~ /\A(?:i[3-6]86|arm)/u 0 0 0
44 0 return undef;
45 }
46  
47 0 ▶sub run (%a) {
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
48 0 ▶ my $presence = App::teai::Presence->new(kind => "exec", session => $a{session}, bus => sub { $a{bus} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my $r = eval { _run(%a) };
50 0 my $e = $@;
51 0 $presence->stop;
52 0 ▶ die $e if ref $e || length $e;
Branch: 1 branch
BranchTrueFalseTotal
if ref $e or length $e 0 0 0
Condition0%0/3
ref $e or length $e
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $e or length $e
ref $e length $e
Truth table0%0/3
A or B
A= ref $eB= length $e
ABresult
1X1
011
000
53 0 return $r;
54 }
55  
56 0 ▶sub _run (%a) {
Subroutine _run: not called CC=13 SCAR=52.0
57 0 local $SIG{PIPE} = 'IGNORE';
58 0 my ($bus, $session, $cmd, $seq) = @a{qw(bus session cmd seq)};
59 0 ▶ my @watch = grep { $_ ne 'HUP' || ($SIG{HUP} // '') ne 'IGNORE' } sort keys %SIGNAL_EXIT;
Condition0%0/5
$_ ne 'HUP' || ($SIG{'HUP'} // '') ne 'IGNORE'
l!l&&r!l&&!r
000
$SIG{"HUP"} // ""
l!l
00
MC/DC0%0/2
$_ ne 'HUP' || ($SIG{'HUP'} // '') ne 'IGNORE'
$_ ne 'HUP' $SIG{"HUP"}
Truth table0%0/3
A || B
A= $_ ne 'HUP'B= $SIG{"HUP"}
ABresult
1X1
011
000
60 0 ▶ my $deadline = App::teai::Bus::_now() + ($a{wait} // 600);
Condition0%0/2
$a{"wait"} // 600
l!l
00
MC/DC0%0/1
$a{"wait"} // 600
$a{"wait"}
Truth table0%0/2
A
A= $a{"wait"}
Aresult
11
00
61 0 ▶ _brief_for($bus, $session, $seq, $deadline) if defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq 0 0 0
62 0 ▶ my $lock = _lock($bus, $session, $deadline)
Branch: 1 branch
BranchTrueFalseTotal
my $lock = _lock($bus, $session, $deadline) or App::teai::Err::fail(4, "session $session is being read by another process") 0 0 0
63 or App::teai::Err::fail(4, "session $session is being read by another process");
64 0 ▶ my $rec = _take($bus, $session, $deadline, $seq)
Branch: 1 branch
BranchTrueFalseTotal
my $rec = _take($bus, $session, $deadline, $seq) or App::teai::Err::fail(1, "no message for $session") 0 0 0
65 or App::teai::Err::fail(1, "no message for $session");
66 0 my ($caught, $stop, %sig);
67 0 for my $name (@watch) {
68 0 ▶ $sig{$name} = EV::signal $name, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
69 0 ▶ $caught //= $name;
Condition0%0/3
$caught //= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$caught //= $name
$caught $name
Truth table0%0/3
A //= B
A= $caughtB= $name
ABresult
1X1
011
000
70 0 ▶ $stop->($SIGNAL_EXIT{$name}[1]) if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 0 0 0
71 0 };
72 }
73 0 _intake($bus, $rec);
74 0 ▶ _test_pause($bus, $ENV{TEAI_TEST_PAUSE_SPAWN}, \$caught) if defined $ENV{TEAI_TEST_PAUSE_SPAWN};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TEAI_TEST_PAUSE_SPAWN"} 0 0 0
75 0 my $reply;
76 0 ▶ if ($caught) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caught) { } 0 0 0
77 0 $reply = _answer($session, $rec, '', { stopped => $SIGNAL_EXIT{$caught}[1] });
78 }
79 else {
80 0 ▶ my $input = Encode::encode('UTF-8', $rec->{body} // '');
Condition0%0/2
$rec->{"body"} // ""
l!l
00
MC/DC0%0/1
$rec->{"body"} // ""
$rec->{"body"}
Truth table0%0/2
A
A= $rec->{"body"}
Aresult
11
00
81 0 ▶ my $res = $bus->await(sub ($done) { $stop = _spawn($cmd, $input, $a{timeout} // $TIMEOUT, \@watch, $done) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$a{"timeout"} // $TIMEOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$a{"timeout"} // $TIMEOUT
$a{"timeout"} $TIMEOUT
Truth table0%0/3
A // B
A= $a{"timeout"}B= $TIMEOUT
ABresult
1X1
011
000
82 0 undef $stop;
83 0 $reply = _reply($session, $rec, $res);
84 }
85 0 my $to = App::teai::Wire::session_subject($rec->{from}, $session);
86 0 $bus->persist($REPLY_PERSIST);
87 0 ▶ my $sent = eval { $bus->await(sub ($done) { $bus->publish($to, $reply, $done) }); 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 my $why = $@;
89 0 %sig = ();
90 0 ▶ App::teai::Err::fail(3, "reply for #$rec->{seq} not sent: " . _msg($why) . '; outcome unknown to the asker')
Branch: 1 branch
BranchTrueFalseTotal
unless $sent 0 0 0
91 unless $sent;
92 0 ▶ App::teai::Err::fail(@{ $SIGNAL_EXIT{$caught} }) if $caught;
Branch: 1 branch
BranchTrueFalseTotal
if $caught 0 0 0
93 0 return 0;
94 }
95  
96 0 ▶sub _lock ($bus, $session, $deadline) {
Subroutine _lock: not called CC=1 SCAR=6.9
97 0 my $poll;
98 0 ▶ return $bus->await(sub ($done) { $poll = $bus->lock_wait($session, $deadline, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 }
100  
101 0 ▶sub _brief_for ($bus, $session, $seq, $deadline) {
Subroutine _brief_for: not called CC=4 SCAR=30.0
102 0 ▶ my $m = $bus->await(sub ($done) { $bus->lookup($seq, $deadline, $done) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 App::teai::Err::fail(2, "#$seq is not a brief for $session")
104 0 ▶ unless ($m->{kind} eq 'ask' || $m->{kind} eq 'msg') && $m->{to} eq $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"kind"} eq "ask" || $m->{"kind"} eq "msg" and $m->{"to"} eq $session 0 0 0
Condition0%0/6
$m->{"kind"} eq "ask" || $m->{"kind"} eq "msg" and $m->{"to"} eq $session
!ll&&!rl&&r
000
$m->{"kind"} eq "ask" or $m->{"kind"} eq "msg"
l!l&&r!l&&!r
000
MC/DC0%0/3
$m->{"kind"} eq "ask" || $m->{"kind"} eq "msg" and $m->{"to"} eq $session
$m->{"kind"} eq "ask" $m->{"kind"} eq "msg" $m->{"to"} eq $session
Truth table0%0/5
A or B and C
A= $m->{"kind"} eq "ask"B= $m->{"kind"} eq "msg"C= $m->{"to"} eq $session
ABCresult
00X0
1X00
0100
1X11
0111
105 }
106  
107 0 ▶sub _test_pause ($bus, $path, $caught) {
Subroutine _test_pause: not called CC=2 SCAR=17.9
108 0 ▶ if (open my $fh, '>', "$path.ready") { close $fh }
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, ">", "$path.ready") 0 0 0
109 0 my $poll;
110 0 ▶ $bus->await(sub ($done) { $poll = EV::timer 0, 0.05, sub { $done->(1) if $$caught || -e "$path.go" } });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $$caught or -e "$path.go" 0 0 0
Condition0%0/3
$$caught or -e "$path.go"
l!l&&r!l&&!r
000
MC/DC0%0/2
$$caught or -e "$path.go"
$$caught -e "$path.go"
Truth table0%0/3
A or B
A= $$caughtB= -e "$path.go"
ABresult
1X1
011
000
111 }
112  
113 0 ▶sub _take ($bus, $session, $deadline, $seq = undef) {
Subroutine _take: not called CC=2 SCAR=17.9
114 0 ▶ return $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 my ($next, $up);
116 0 ▶ my $end = sub (@r) { undef $next; undef $up; $done->(@r) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 ▶ my $take = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 my $o = App::teai::Bus::slice($deadline);
119 0 ▶ $bus->fetch($session, $o, sub ($r, $e = undef) {
Subroutine __ANON__: not called CC=13 SCAR=52.0
120 0 ▶ return App::teai::Bus::lost($e) ? $next->() : $end->(undef, $e) if defined $e;
Branch: 2 branches
BranchTrueFalseTotal
if defined $e 0 0 0
App::teai::Bus::lost($e) ? : 0 0 0
121 0 ▶ return $o->{no_wait} ? $end->(undef) : $next->() unless $r;
Branch: 2 branches
BranchTrueFalseTotal
unless $r 0 0 0
$o->{'no_wait'} ? : 0 0 0
122 0 ▶ if ($r->{kind} eq 'ask' || $r->{kind} eq 'msg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"kind"} eq "ask" or $r->{"kind"} eq "msg") 0 0 0
Condition0%0/3
$r->{"kind"} eq "ask" or $r->{"kind"} eq "msg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"kind"} eq "ask" or $r->{"kind"} eq "msg"
$r->{"kind"} eq "ask" $r->{"kind"} eq "msg"
Truth table0%0/3
A or B
A= $r->{"kind"} eq "ask"B= $r->{"kind"} eq "msg"
ABresult
1X1
011
000
123 0 ▶ return $end->($r) if !defined $seq || $r->{seq} == $seq;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $seq or $r->{"seq"} == $seq 0 0 0
Condition0%0/3
not defined $seq or $r->{"seq"} == $seq
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $seq or $r->{"seq"} == $seq
not defined $seq $r->{"seq"} == $seq
Truth table0%0/3
not(A) or B
A= not defined $seqB= $r->{"seq"} == $seq
ABresult
1X1
011
000
124 0 ▶ if ($r->{seq} > $seq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"seq"} > $seq) 0 0 0
125 0 ▶ return _confirm($bus, $deadline, 'nak', $r, sub ($ok, $e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
126 0 ▶ $end->(undef, $ok
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
127 ? App::teai::Bus::_err(1, "brief #$seq is not in ${session}'s inbox (next is #$r->{seq})")
128 : App::teai::Bus::_err(3, "nak for #$r->{seq} not confirmed: " . _msg($e)));
129 0 });
130 }
131 0 ▶ return _confirm($bus, $deadline, 'ack', $r, sub ($ok, $e = undef) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
132 0 ▶ return $end->(undef, App::teai::Bus::_err(3, "ack for #$r->{seq} not confirmed: " . _msg($e)))
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
133 unless $ok;
134 0 print STDERR "teai: skipped stale brief #$r->{seq}\n";
135 0 $next->();
136 0 });
137 }
138 0 ▶ if (!print STDERR Encode::encode('UTF-8', App::teai::Wire::format_human($r))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print STDERR &Encode::encode("UTF-8", scalar App::teai::Wire::format_human($r))) 0 0 0
139 0 $bus->nak($r);
140 0 ▶ return $bus->flush(sub (@) { $end->(undef, 'output failed') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 }
142 0 ▶ $bus->ack($r) or return $end->(undef, $@);
Branch: 1 branch
BranchTrueFalseTotal
$bus->ack($r) or return &$end(undef, $@) 0 0 0
143 0 $next->();
144 0 });
145 0 };
146 0 ▶ $next = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
147 0 ▶ my $t = $bus->when_up($deadline, $take, sub ($e) { $end->(undef, $e) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 ▶ $up = $t if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 0 0
149 0 };
150 0 $next->();
151 0 });
152 }
153  
154 0 ▶sub _confirm ($bus, $deadline, $verb, $rec, $cb) {
Subroutine _confirm: not called CC=1 SCAR=6.9
155 0 my ($try, $up);
156 0 ▶ my $end = sub (@r) { undef $try; undef $up; $cb->(@r) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 ▶ $try = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
158 0 ▶ $bus->$verb($rec) or return $end->(undef, $@);
Branch: 1 branch
BranchTrueFalseTotal
$bus->$verb($rec) or return &$end(undef, $@) 0 0 0
159 0 ▶ $bus->flush(sub ($ok, $e = undef) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
160 0 ▶ return $end->(1) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
161 0 ▶ return $end->(undef, $e) unless App::teai::Bus::lost($e);
Branch: 1 branch
BranchTrueFalseTotal
unless App::teai::Bus::lost($e) 0 0 0
162 0 ▶ my $t = $bus->when_up($deadline, $try, sub ($f) { $end->(undef, $f) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ $up = $t if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 0 0
164 0 });
165 0 };
166 0 $try->();
167 }
168  
169 0 ▶sub _intake ($bus, $rec) {
Subroutine _intake: not called CC=2 SCAR=17.9
170 0 my $ok = eval {
171 0 ▶ $bus->await(sub ($done) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 ▶ $bus->nats->request($rec->{reply}, '+ACK', sub ($body, $e = undef, $head = undef, @) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
173 0 ▶ return $done->(undef, $e) if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
174 0 ▶ my ($status) = ($head // '') =~ m{\ANATS/1\.0[ \t]+([45]\d\d[^\r\n]*)};
Condition0%0/2
$head // ""
l!l
00
MC/DC0%0/1
$head // ""
$head
Truth table0%0/2
A
A= $head
Aresult
11
00
175 0 ▶ return $done->(undef, $status) if defined $status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $status 0 0 0
176 0 ▶ $body //= '';
Condition0%0/2
$body //= ""
l!l
00
MC/DC0%0/1
$body //= ""
$body
Truth table0%0/2
A
A= $body
Aresult
11
00
177 0 ▶ return $done->(1) if $body eq '' || $body eq '+OK';
Branch: 1 branch
BranchTrueFalseTotal
if $body eq "" or $body eq "+OK" 0 0 0
Condition0%0/3
$body eq "" or $body eq "+OK"
l!l&&r!l&&!r
000
MC/DC0%0/2
$body eq "" or $body eq "+OK"
$body eq "" $body eq "+OK"
Truth table0%0/3
A or B
A= $body eq ""B= $body eq "+OK"
ABresult
1X1
011
000
178 0 $done->(undef, 'unexpected reply: ' . substr($body, 0, 80));
179 0 }, _knob('TEAI_TEST_ACK_TIMEOUT_MS', $ACK_TIMEOUT_MS));
180 0 });
181 0 1;
182 };
183 0 ▶ App::teai::Err::fail(3, "intake ack for #$rec->{seq} not confirmed: " . _msg($@)) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
184 }
185  
186 0 ▶sub _nonblock ($fh) {
Subroutine _nonblock: not called CC=2 SCAR=17.9
187 0 my $flags = fcntl $fh, F_GETFL, 0;
188 0 ▶ fcntl $fh, F_SETFL, $flags | O_NONBLOCK if defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flags 0 0 0
189 }
190  
191 0 ▶sub _pdeathsig () { defined $PRCTL ? syscall($PRCTL, $PR_SET_PDEATHSIG, POSIX::SIGKILL()) : 0 }
Subroutine _pdeathsig: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $PRCTL ? : 0 0 0
192  
193 0 ▶sub _child ($cmd, $mask, $reset, $parent, @std) {
Subroutine _child: not called CC=12 SCAR=48.5
194 0 eval {
195 0 my $pdeath = _pdeathsig();
196 0 my $why = "$!";
197 0 ▶ POSIX::_exit(1) if getppid() != $parent;
Branch: 1 branch
BranchTrueFalseTotal
if getppid != $parent 0 0 0
198 0 $SIG{$_} = 'DEFAULT' for 'PIPE', @$reset;
199 0 POSIX::setpgid(0, 0);
200 0 $mask->delset($_) for POSIX::SIGINT(), POSIX::SIGTERM(), POSIX::SIGHUP(), POSIX::SIGCHLD();
201 0 POSIX::sigprocmask(POSIX::SIG_SETMASK(), $mask);
202 0 delete @ENV{ grep { /\ATEAI_/ } keys %ENV };
203 0 ▶ my @fd = map { fcntl($_, F_DUPFD, 3) // die } @std;
Condition0%0/2
fcntl($_, 0, 3) // die()
l!l
00
MC/DC0%0/1
fcntl($_, 0, 3) // die()
fcntl($_, 0, 3)
Truth table0%0/2
A
A= fcntl($_, 0, 3)
Aresult
11
00
204 0 ▶ POSIX::dup2($fd[$_], $_) // die for 0 .. 2;
Condition0%0/2
POSIX::dup2($fd[$_], $_) // die()
l!l
00
MC/DC0%0/1
POSIX::dup2($fd[$_], $_) // die()
POSIX::dup2($fd[$_], $_)
Truth table0%0/2
A
A= POSIX::dup2($fd[$_], $_)
Aresult
11
00
205 0 ▶ my $max = POSIX::sysconf(POSIX::_SC_OPEN_MAX()) // $MAX_FD;
Condition0%0/3
POSIX::sysconf(4) // $MAX_FD
l!l&&r!l&&!r
000
MC/DC0%0/2
POSIX::sysconf(4) // $MAX_FD
POSIX::sysconf(4) $MAX_FD
Truth table0%0/3
A // B
A= POSIX::sysconf(4)B= $MAX_FD
ABresult
1X1
011
000
206 0 ▶ $max = $MAX_FD if $max > $MAX_FD;
Branch: 1 branch
BranchTrueFalseTotal
if $max > $MAX_FD 0 0 0
207 0 POSIX::close($_) for 3 .. $max - 1;
208 0 ▶ if ($pdeath == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdeath == -1) { } 0 0 0
209 0 print STDERR "teai: cannot set the parent death signal: $why\n";
210 }
211 else {
212 0 ▶ { no warnings 'exec'; exec { $cmd->[0] } @$cmd; }
Subroutine BEGIN: called CC=1 SCAR=1.2
213 0 my $errno = $!;
214 0 print STDERR "teai: cannot run $cmd->[0]: $errno\n";
215 0 ▶ POSIX::_exit($errno == POSIX::EACCES() ? 126 : 127);
Branch: 1 branch
BranchTrueFalseTotal
$errno == 13 ? : 0 0 0
216 }
217 };
218 0 POSIX::_exit(127);
219 }
220  
221 0 ▶sub _zombie ($pid) {
Subroutine _zombie: not called CC=4 SCAR=30.0
222 0 ▶ open my $f, "<", "/proc/$pid/stat" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $f, "<", "/proc/$pid/stat" or return 0 0 0 0
223 0 ▶ return ((<$f> // "") =~ /.*\)\s+Z\s/s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(readline $f // '') =~ /.*\)\s+Z\s/su ? : 0 0 0
Condition0%0/2
readline $f // ""
l!l
00
MC/DC0%0/1
readline $f // ""
readline $f
Truth table0%0/2
A
A= readline $f
Aresult
11
00
224 }
225  
226 0 ▶sub _spawn ($cmd, $input, $timeout, $reset, $done) {
Subroutine _spawn: not called CC=10 SCAR=47.0
227 0 ▶ my $fail = sub ($e) { $done->({ error => $e }); return };
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 0 my ($in_r, $in_w, $out_r, $out_w, $err_r, $err_w);
229 0 ▶ pipe($in_r, $in_w) && pipe($out_r, $out_w) && pipe($err_r, $err_w)
Branch: 1 branch
BranchTrueFalseTotal
pipe $in_r, $in_w and pipe $out_r, $out_w and pipe $err_r, $err_w or return &$fail("pipe: $!") 0 0 0
Condition0%0/6
pipe $in_r, $in_w and pipe $out_r, $out_w and pipe $err_r, $err_w
!ll&&!rl&&r
000
pipe $in_r, $in_w and pipe $out_r, $out_w
!ll&&!rl&&r
000
MC/DC0%0/3
pipe $in_r, $in_w and pipe $out_r, $out_w and pipe $err_r, $err_w
pipe $in_r, $in_w pipe $out_r, $out_w pipe $err_r, $err_w
Truth table0%0/4
A and B and C
A= pipe $in_r, $in_wB= pipe $out_r, $out_wC= pipe $err_r, $err_w
ABCresult
0XX0
10X0
1100
1111
230 or return $fail->("pipe: $!");
231 0 my $parent = $$;
232 0 my $mask = POSIX::SigSet->new;
233 0 POSIX::sigprocmask(POSIX::SIG_BLOCK(), POSIX::SigSet->new(POSIX::SIGINT(), POSIX::SIGTERM(), POSIX::SIGHUP()), $mask);
234 0 ▶ my $pid = fork // do {
Condition0%0/3
fork // do { my $e = "fork: $!"; POSIX::sigprocmask(2, $mask); return &$fail($e) }
l!l&&r!l&&!r
000
MC/DC0%0/2
fork // do { my $e = "fork: $!"; POSIX::sigprocmask(2, $mask); return &$fail($e) }
fork do { my $e = "fork: $!"; POSIX::sigprocmask(2, $mask); return &$fail($e) }
Truth table0%0/3
A // B
A= forkB= do { my $e = "fork: $!"; POSIX::sigprocmask(2, $mask); return &$fail($e) }
ABresult
1X1
011
000
235 0 my $e = "fork: $!";
236 0 POSIX::sigprocmask(POSIX::SIG_SETMASK(), $mask);
237 0 return $fail->($e);
238 };
239 0 ▶ _child($cmd, $mask, $reset, $parent, $in_r, $out_w, $err_w) unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
240 0 POSIX::setpgid($pid, $pid);
241 0 POSIX::sigprocmask(POSIX::SIG_SETMASK(), $mask);
242 0 close $_ for $in_r, $out_w, $err_w;
243 0 _nonblock($_) for $in_w, $out_r, $err_r;
244  
245 0 my %res = (out => '', err => '', bytes => 0);
246 0 my %fh = (out => $out_r, err => $err_r);
247 0 my ($exited, $group, $off, %w) = (0, 0, 0);
248 0 ▶ my $close_in = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
249 0 delete $w{in};
250 0 ▶ close $in_w if $in_w;
Branch: 1 branch
BranchTrueFalseTotal
if $in_w 0 0 0
251 0 undef $in_w;
252 0 };
253 0 ▶ my $finish = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
254 0 ▶ return unless $exited && !%fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $exited and not %fh 0 0 0
Condition0%0/3
$exited and not %fh
!ll&&!rl&&r
000
MC/DC0%0/2
$exited and not %fh
$exited not %fh
Truth table0%0/3
A and not(B)
A= $exitedB= not %fh
ABresult
0X0
100
111
255 0 $close_in->();
256 0 %w = ();
257 0 ▶ kill 'KILL', -$pid if $group && kill 0, -$pid;
Branch: 1 branch
BranchTrueFalseTotal
if $group and kill 0, -$pid 0 0 0
Condition0%0/3
$group and kill 0, -$pid
!ll&&!rl&&r
000
MC/DC0%0/2
$group and kill 0, -$pid
$group kill 0, -$pid
Truth table0%0/3
A and B
A= $groupB= kill 0, -$pid
ABresult
0X0
100
111
258 0 $done->(\%res);
259 0 };
260 0 ▶ my $shut = sub ($key) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 delete $w{$key};
262 0 close delete $fh{$key};
263 0 };
264 0 ▶ my $stop = sub ($why) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
265 0 ▶ return if $exited || defined $res{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $exited or defined $res{"stopped"} 0 0 0
Condition0%0/3
$exited or defined $res{"stopped"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$exited or defined $res{"stopped"}
$exited defined $res{"stopped"}
Truth table0%0/3
A or B
A= $exitedB= defined $res{"stopped"}
ABresult
1X1
011
000
266 0 ▶ return if _zombie($pid);
Branch: 1 branch
BranchTrueFalseTotal
if _zombie($pid) 0 0 0
267 0 $res{stopped} = $why;
268 0 kill $_, -$pid for 'TERM', 'CONT';
269 0 ▶ $w{kill} = EV::timer _knob('TEAI_TEST_KILL_GRACE', $KILL_GRACE), 0, sub { kill 'KILL', -$pid };
Subroutine __ANON__: not called CC=1 SCAR=6.9
270 0 };
271  
272 0 ▶ $w{timeout} = EV::timer $timeout, 0, sub { $stop->('timeout') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
273 0 ▶ $w{child} = EV::child $pid, 0, sub ($cw, @) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
274 0 $cw->stop;
275 0 $res{status} = $cw->rstatus;
276 0 $exited = 1;
277 0 $group = kill 0, -$pid;
278 0 delete $w{timeout};
279 0 ▶ if (defined $res{stopped}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res{"stopped"}) 0 0 0
280 0 ▶ kill 'KILL', -$pid if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
281 0 delete $w{kill};
282 }
283 0 ▶ if (%fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (%fh) 0 0 0
284 0 ▶ $w{grace} = EV::timer $PIPE_GRACE, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
285 0 $shut->($_) for keys %fh;
286 0 $finish->();
287 0 };
288 }
289 0 $finish->();
290 0 };
291 0 for my $key (keys %fh) {
292 0 my $fh = $fh{$key};
293 0 ▶ $w{$key} = EV::io $fh, EV::READ, sub (@) {
Subroutine __ANON__: not called CC=11 SCAR=48.8
294 0 my $buf;
295 0 my $n = sysread $fh, $buf, $CHUNK;
296 0 ▶ return if !defined $n && ($!{EAGAIN} || $!{EINTR});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $!{"EAGAIN"} || $!{"EINTR"} 0 0 0
Condition0%0/6
not defined $n and $!{"EAGAIN"} || $!{"EINTR"}
!ll&&!rl&&r
000
$!{"EAGAIN"} or $!{"EINTR"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $n and $!{"EAGAIN"} || $!{"EINTR"}
not defined $n $!{"EAGAIN"} $!{"EINTR"}
Truth table0%0/4
not(A) and B or C
A= not defined $nB= $!{"EAGAIN"}C= $!{"EINTR"}
ABCresult
0XX0
1000
11X1
1011
297 0 ▶ if (!defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
298 0 ▶ $res{error} //= ($key eq "out" ? "stdout" : "stderr") . " read failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'out' ? : 0 0 0
Condition0%0/2
$res{"error"} //= ($key eq "out" ? "stdout" : "stderr") . " read failed: $!"
l!l
00
MC/DC0%0/1
$res{"error"} //= ($key eq "out" ? "stdout" : "stderr") . " read failed: $!"
$res{"error"}
Truth table0%0/2
A
A= $res{"error"}
Aresult
11
00
299 0 $shut->($key);
300 0 return $finish->();
301 }
302 0 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 0 0
303 0 $shut->($key);
304 0 return $finish->();
305 }
306 0 ▶ if ($key eq 'out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "out") 0 0 0
307 0 $res{bytes} += $n;
308 0 my $room = $OUT_KEEP - length $res{out};
309 0 ▶ $res{out} .= substr $buf, 0, $room if $room > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $room > 0 0 0 0
310 0 return;
311 }
312 0 $res{err} .= $buf;
313 0 ▶ if (length $res{err} > 2 * $TAIL) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $res{"err"} > 2 * $TAIL) 0 0 0
314 0 substr $res{err}, 0, length($res{err}) - $TAIL, '';
315 0 $res{cut} = 1;
316 }
317 0 };
318 }
319 0 ▶ if (length $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input) { } 0 0 0
320 0 ▶ $w{in} = EV::io $in_w, EV::WRITE, sub (@) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
321 0 my $n = syswrite $in_w, $input, $CHUNK, $off;
322 0 ▶ if (defined $n) { $off += $n; return if $off < length $input }
Branch: 3 branches
BranchTrueFalseTotal
if (defined $n) { } 0 0 0
elsif ($!{'EAGAIN'} or $!{'EINTR'}) { } 0 0 0
if $off < length $input 0 0 0
323 0 ▶ elsif ($!{EAGAIN} || $!{EINTR}) { return }
Condition0%0/3
$!{"EAGAIN"} or $!{"EINTR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EINTR"}
$!{"EAGAIN"} $!{"EINTR"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EINTR"}
ABresult
1X1
011
000
324 0 $close_in->();
325 0 };
326 }
327 else {
328 0 $close_in->();
329 }
330 0 return $stop;
331 }
332  
333 0 ▶sub _answer ($session, $rec, $body, $status) {
Subroutine _answer: not called CC=1 SCAR=6.9
334 return {
335 0 kind => 'reply', re => $rec->{seq}, thread => $rec->{thread}, body => $body,
336 agent => App::teai::Config::agent_of($session), status => $status,
337 };
338 }
339  
340 0 ▶sub _reply ($session, $rec, $res) {
Subroutine _reply: not called CC=16 SCAR=56.1
341 0 ▶ my $out = $res->{out} // '';
Condition0%0/2
$res->{"out"} // ""
l!l
00
MC/DC0%0/1
$res->{"out"} // ""
$res->{"out"}
Truth table0%0/2
A
A= $res->{"out"}
Aresult
11
00
342 0 ▶ my $bytes = $res->{bytes} // 0;
Condition0%0/2
$res->{"bytes"} // 0
l!l
00
MC/DC0%0/1
$res->{"bytes"} // 0
$res->{"bytes"}
Truth table0%0/2
A
A= $res->{"bytes"}
Aresult
11
00
343 0 my $over = $bytes > length $out;
344 0 ▶ $out =~ s/(?:[\xC0-\xDF]|[\xE0-\xEF][\x80-\xBF]?|[\xF0-\xF7][\x80-\xBF]{0,2})\z// if $over;
Branch: 1 branch
BranchTrueFalseTotal
if $over 0 0 0
345 0 my ($body, $bad) = App::teai::Wire::text_in($out);
346 0 ▶ my $err = $res->{err} // '';
Condition0%0/2
$res->{"err"} // ""
l!l
00
MC/DC0%0/1
$res->{"err"} // ""
$res->{"err"}
Truth table0%0/2
A
A= $res->{"err"}
Aresult
11
00
347 0 ▶ my $cut = $res->{cut} || length $err > $TAIL;
Condition0%0/3
$res->{'cut'} || length $err > $TAIL
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->{'cut'} || length $err > $TAIL
$res->{'cut'} length $err > $TAIL
Truth table0%0/3
A || B
A= $res->{'cut'}B= length $err > $TAIL
ABresult
1X1
011
000
348 0 ▶ $err = substr $err, -$TAIL if length $err > $TAIL;
Branch: 1 branch
BranchTrueFalseTotal
if length $err > $TAIL 0 0 0
349 0 ▶ $err =~ s/\A[\x80-\xBF]{1,3}// if $cut;
Branch: 1 branch
BranchTrueFalseTotal
if $cut 0 0 0
350 0 my $st = $res->{status};
351 my %status = (
352 exit => defined $st && POSIX::WIFEXITED($st) ? POSIX::WEXITSTATUS($st) : undef,
353 signal => defined $st && POSIX::WIFSIGNALED($st) ? POSIX::WTERMSIG($st) : undef,
354 stderr_tail => (App::teai::Wire::text_in($err))[0],
355 (defined $res->{error} ? (error => $res->{error}) : ()),
356 0 ▶ (defined $res->{stopped} ? (stopped => $res->{stopped}) : ()),
Branch: 5 branches
BranchTrueFalseTotal
defined $st && POSIX::WIFEXITED($st) ? : 0 0 0
defined $st && POSIX::WIFSIGNALED($st) ? : 0 0 0
defined $res->{'error'} ? : 0 0 0
defined $res->{'stopped'} ? : 0 0 0
$over ? : 0 0 0
Condition0%0/6
defined $st and POSIX::WIFEXITED($st)
!ll&&!rl&&r
000
defined $st and POSIX::WIFSIGNALED($st)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $st and POSIX::WIFEXITED($st)
defined $st POSIX::WIFEXITED($st)
MC/DC0%0/2
defined $st and POSIX::WIFSIGNALED($st)
defined $st POSIX::WIFSIGNALED($st)
Truth table0%0/3
A and B
A= defined $stB= POSIX::WIFEXITED($st)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $stB= POSIX::WIFSIGNALED($st)
ABresult
0X0
100
111
357 ($over ? (error => 'size', bytes => $bytes) : ()),
358 );
359 0 ▶ if ($bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 0 0 0
360 0 $status{warning} = 'stdout was not valid UTF-8; invalid bytes replaced';
361 0 print STDERR "teai: $BAD_UTF8\n";
362 }
363 0 my $reply = _answer($session, $rec, $body, \%status);
364 0 _fit($reply, $bytes);
365 0 return $reply;
366 }
367  
368 0 ▶sub _fits ($p) { eval { App::teai::Wire::encode_payload($p); 1 } }
Subroutine _fits: not called CC=1 SCAR=6.9
369  
370 0 ▶sub _fit ($reply, $bytes) {
Subroutine _fit: not called CC=5 SCAR=34.0
371 0 ▶ return if _fits($reply);
Branch: 1 branch
BranchTrueFalseTotal
if _fits($reply) 0 0 0
372 0 @{ $reply->{status} }{qw(error bytes)} = ('size', $bytes);
373 0 my $text = $reply->{body};
374 0 my ($lo, $hi) = (0, length $text);
375 0 ▶ $hi = App::teai::Wire::MAX_PAYLOAD + 1 if $hi > App::teai::Wire::MAX_PAYLOAD + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $hi > 1047553 0 0 0
376 0 ▶ while ($hi - $lo > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi - $lo > 1) 0 0 0
377 0 my $mid = ($lo + $hi) >> 1;
378 0 $reply->{body} = substr $text, 0, $mid;
379 0 ▶ _fits($reply) ? ($lo = $mid) : ($hi = $mid);
Branch: 1 branch
BranchTrueFalseTotal
_fits($reply) ? : 0 0 0
380 }
381 0 $reply->{body} = substr $text, 0, $lo;
382 }
383  
384 2 1;
385 __END__
« lib/App/teai/Round.pm ↑ Summary lib/App/teai/Dash/Rounds.pm »