blib/lib/App/ptimeout.pm

filter: Statementstmt 7.7% 19 / 245 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 25.0% 6 / 24 Podpod n/a totaltotal 5.1% 25 / 483 CC 91 SCAR 47.3
CC
91
Cov
4%
_run
272.0
_run
272.0
_flush_remaining_stderr
132.0
CRAP 113.2
↑ Summary
1 package App::ptimeout;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.0.3';
12  
13 0 ▶sub _run {
Subroutine _run: not called CC=16 SCAR=56.1
14 0 ▶ my($timeout, @argv) = @_;
Subroutine _run: not called CC=16 SCAR=56.1
15  
16 0 ▶ if($timeout =~ /m$/) { $timeout *= 60 }
Branch: 4 branches
BranchTrueFalseTotal
if ($timeout =~ /m$/) { } 0 0 0
elsif ($timeout =~ /h$/) { } 0 0 0
if ($timeout =~ /m$/) { } 0 0 0
elsif ($timeout =~ /h$/) { } 0 0 0
17 0 elsif($timeout =~ /h$/) { $timeout *= 3600 }
18  
19 0 ▶ pipe(my $stderr_reader, my $stderr_writer)
Branch: 2 branches
BranchTrueFalseTotal
pipe my $stderr_reader, my $stderr_writer or die "Error creating stderr pipe\n" 0 0 0
pipe my $stderr_reader, my $stderr_writer or die "Error creating stderr pipe\n" 0 0 0
20 or die("Error creating stderr pipe\n");
21 0 _set_nonblocking($stderr_reader);
22  
23 0 my $pid = fork();
24 0 ▶ if(!defined($pid)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
25 0 die("Error forking\n")
26 } elsif(!$pid) {
27 # the child process, which runs the command
28  
29 # only the parent cares about this end of the pipe, so close the copy in this process
30 0 ▶ close $stderr_reader or die("Error closing stderr reader\n");
Branch: 2 branches
BranchTrueFalseTotal
close $stderr_reader or die "Error closing stderr reader\n" 0 0 0
close $stderr_reader or die "Error closing stderr reader\n" 0 0 0
31 # re-open STDERR as a duplicate of the pipe we inherited from the parent
32 0 ▶ open STDERR, '>&', $stderr_writer
Branch: 2 branches
BranchTrueFalseTotal
open STDERR, ">&", $stderr_writer or die "Error redirecting stderr\n" 0 0 0
open STDERR, ">&", $stderr_writer or die "Error redirecting stderr\n" 0 0 0
33 or die("Error redirecting stderr\n");
34 # now that we've duplicated it we don't need another copy
35 0 ▶ close $stderr_writer or die("Error closing stderr writer\n");
Branch: 2 branches
BranchTrueFalseTotal
close $stderr_writer or die "Error closing stderr writer\n" 0 0 0
close $stderr_writer or die "Error closing stderr writer\n" 0 0 0
36  
37 0 my $status = system @argv;
38 0 exit _normalise_status($status);
39 }
40  
41 # only the parent process, the watchdog, gets here
42  
43 # only the child cares about this end of the pipe, it now has a copy, so close ours
44 0 ▶ close $stderr_writer or die("Error closing stderr writer\n");
Branch: 2 branches
BranchTrueFalseTotal
close $stderr_writer or die "Error closing stderr writer\n" 0 0 0
close $stderr_writer or die "Error closing stderr writer\n" 0 0 0
45  
46 0 my $deadline = time + $timeout;
47 0 my $timed_out = 0;
48 0 my $child_status;
49 0 my $stderr_buffer = '';
50 0 my $held_line;
51  
52 # Loop while the process we're monitoring does its thang
53 0 while(1) {
54 # The background process is running, and its STDERR is being captured.
55 # If it's said anything on its STDERR, flush it to the *real* STDERR.
56 0 _pump_stderr(
57 $stderr_reader,
58 \$stderr_buffer,
59 \$held_line,
60 timed_out => $timed_out,
61 );
62  
63 # Has the process finished?
64 0 my $waited = waitpid($pid, WNOHANG);
65 0 ▶ if($waited == $pid) { # yes, and it exited
Branch: 2 branches
BranchTrueFalseTotal
if ($waited == $pid) 0 0 0
if ($waited == $pid) 0 0 0
66 0 $child_status = $?;
67 0 last;
68 }
69 0 ▶ if($waited == -1) { # it's all gone wrong
Branch: 2 branches
BranchTrueFalseTotal
if ($waited == -1) 0 0 0
if ($waited == -1) 0 0 0
70 0 die("Error waiting for child process\n");
71 }
72  
73 0 ▶ if(time >= $deadline) { # still running, but been running too long
Branch: 2 branches
BranchTrueFalseTotal
if (time >= $deadline) 0 0 0
if (time >= $deadline) 0 0 0
74 # looks like dpulicate code, but it avoids a race condition
75 0 $waited = waitpid($pid, WNOHANG);
76 0 ▶ if($waited == $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($waited == $pid) 0 0 0
if ($waited == $pid) 0 0 0
77 0 $child_status = $?;
78 0 last;
79 }
80 0 warn "timed out\n";
81 0 $timed_out = 1;
82 0 $child_status = _terminate_process_tree($pid);
83 0 last;
84 }
85  
86 0 select undef, undef, undef, 0.1;
87 }
88  
89 _flush_remaining_stderr(
90 0 $stderr_reader,
91 \$stderr_buffer,
92 \$held_line,
93 timed_out => $timed_out,
94 );
95 0 ▶ close $stderr_reader or die("Error closing stderr reader\n");
Branch: 2 branches
BranchTrueFalseTotal
close $stderr_reader or die "Error closing stderr reader\n" 0 0 0
close $stderr_reader or die "Error closing stderr reader\n" 0 0 0
96  
97 0 ▶ exit($timed_out ? 124 : _normalise_status($child_status));
Branch: 2 branches
BranchTrueFalseTotal
$timed_out ? : 0 0 0
$timed_out ? : 0 0 0
98 }
99  
100 0 ▶sub _normalise_status {
Subroutine _normalise_status: not called CC=2 SCAR=17.9
101 0 ▶ my($status) = @_;
Subroutine _normalise_status: not called CC=2 SCAR=17.9
102  
103 0 ▶ return 255 if($status == -1);
Branch: 2 branches
BranchTrueFalseTotal
if $status == -1 0 0 0
if $status == -1 0 0 0
104 0 return $status >> 8;
105 }
106  
107 0 ▶sub _set_nonblocking {
Subroutine _set_nonblocking: not called CC=3 SCAR=24.8
108 0 ▶ my($fh) = @_;
Subroutine _set_nonblocking: not called CC=3 SCAR=24.8
109  
110 0 my $flags = fcntl($fh, F_GETFL, 0);
111 0 ▶ defined($flags) or die("Error reading stderr flags\n");
Branch: 2 branches
BranchTrueFalseTotal
defined $flags or die "Error reading stderr flags\n" 0 0 0
defined $flags or die "Error reading stderr flags\n" 0 0 0
112 0 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 2 branches
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "Error setting stderr nonblocking\n" 0 0 0
fcntl $fh, 4, $flags | 2048 or die "Error setting stderr nonblocking\n" 0 0 0
113 or die("Error setting stderr nonblocking\n");
114 }
115  
116 0 ▶sub _set_blocking {
Subroutine _set_blocking: not called CC=3 SCAR=24.8
117 0 ▶ my($fh) = @_;
Subroutine _set_blocking: not called CC=3 SCAR=24.8
118  
119 0 my $flags = fcntl($fh, F_GETFL, 0);
120 0 ▶ defined($flags) or die("Error reading stderr flags\n");
Branch: 2 branches
BranchTrueFalseTotal
defined $flags or die "Error reading stderr flags\n" 0 0 0
defined $flags or die "Error reading stderr flags\n" 0 0 0
121 0 ▶ fcntl($fh, F_SETFL, $flags & ~O_NONBLOCK)
Branch: 2 branches
BranchTrueFalseTotal
fcntl $fh, 4, $flags & '18446744073709549567' or die "Error restoring blocking stderr\n" 0 0 0
fcntl $fh, 4, $flags & '18446744073709549567' or die "Error restoring blocking stderr\n" 0 0 0
122 or die("Error restoring blocking stderr\n");
123 }
124  
125 0 ▶sub _emit_stderr_lines {
Subroutine _emit_stderr_lines: not called CC=6 SCAR=37.4
126 0 ▶ my($buffer_ref, $held_line_ref, %options) = @_;
Subroutine _emit_stderr_lines: not called CC=6 SCAR=37.4
127  
128 0 ▶ while($$buffer_ref =~ s/\A([^\n]*\n)//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$buffer_ref =~ s/\A([^\n]*\n)//) 0 0 0
if ($$buffer_ref =~ s/\A([^\n]*\n)//) 0 0 0
129 0 my $line = $1;
130 # OpenBSD sometimes (?) spits out this unnecessary diagnostic when sh is SIGTERMed
131 0 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if ($options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/) 0 0 0
if ($options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/) 0 0 0
Condition0%0/12
$options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd"
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd"
!ll&&!rl&&r
000
MC/DC0%0/3
$options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/
$options{"timed_out"} $^O eq "openbsd" $line =~ /\ATerminated\s*\z/
MC/DC0%0/3
$options{"timed_out"} and $^O eq "openbsd" and $line =~ /\ATerminated\s*\z/
$options{"timed_out"} $^O eq "openbsd" $line =~ /\ATerminated\s*\z/
Truth table0%0/4
A and B and C
A= $options{"timed_out"}B= $^O eq "openbsd"C= $line =~ /\ATerminated\s*\z/
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $options{"timed_out"}B= $^O eq "openbsd"C= $line =~ /\ATerminated\s*\z/
ABCresult
0XX0
10X0
1100
1111
132 $options{timed_out} &&
133 $^O eq 'openbsd' &&
134 $line =~ /\ATerminated\s*\z/
135 ) {
136 0 $$held_line_ref = $line;
137 0 next;
138 }
139  
140 0 ▶ if(defined($$held_line_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$held_line_ref) 0 0 0
if (defined $$held_line_ref) 0 0 0
141 0 print STDERR $$held_line_ref;
142 0 undef $$held_line_ref;
143 }
144 0 print STDERR $line;
145 }
146 }
147  
148 0 ▶sub _pump_stderr {
Subroutine _pump_stderr: not called CC=5 SCAR=34.0
149 0 ▶ my($stderr_reader, $buffer_ref, $held_line_ref, %options) = @_;
Subroutine _pump_stderr: not called CC=5 SCAR=34.0
150  
151 0 while(1) {
152 0 my $chunk = '';
153 0 my $bytes = sysread($stderr_reader, $chunk, 4096);
154 0 ▶ if(!defined($bytes)) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $bytes) 0 0 0
unless (defined $bytes) 0 0 0
155 0 ▶ last if($!{EAGAIN} || $!{EWOULDBLOCK});
Branch: 2 branches
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 0 0 0
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} 0 0 0
Condition0%0/6
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}
ABresult
1X1
011
000
156 0 die("Error reading stderr\n");
157 }
158 0 ▶ last if($bytes == 0);
Branch: 2 branches
BranchTrueFalseTotal
if $bytes == 0 0 0 0
if $bytes == 0 0 0 0
159 0 $$buffer_ref .= $chunk;
160 0 _emit_stderr_lines($buffer_ref, $held_line_ref, %options);
161 }
162 }
163  
164 0 ▶sub _flush_remaining_stderr {
Subroutine _flush_remaining_stderr: not called CC=11 SCAR=48.8
165 0 ▶ my($stderr_reader, $buffer_ref, $held_line_ref, %options) = @_;
Subroutine _flush_remaining_stderr: not called CC=11 SCAR=48.8
166  
167 0 _set_blocking($stderr_reader);
168 0 while(1) {
169 0 my $chunk = '';
170 0 my $bytes = sysread($stderr_reader, $chunk, 4096);
171 0 ▶ die("Error reading stderr\n") if(!defined($bytes));
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bytes 0 0 0
unless defined $bytes 0 0 0
172 0 ▶ last if($bytes == 0);
Branch: 2 branches
BranchTrueFalseTotal
if $bytes == 0 0 0 0
if $bytes == 0 0 0 0
173 0 $$buffer_ref .= $chunk;
174 0 _emit_stderr_lines($buffer_ref, $held_line_ref, %options);
175 }
176  
177 0 ▶ if(length($$buffer_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $$buffer_ref) 0 0 0
if (length $$buffer_ref) 0 0 0
178 0 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'timed_out'} and $^O eq 'openbsd' and $$buffer_ref =~ /\ATerminated\s*\z/) { } 0 0 0
if ($options{'timed_out'} and $^O eq 'openbsd' and $$buffer_ref =~ /\ATerminated\s*\z/) { } 0 0 0
Condition0%0/12
$options{"timed_out"} and $^O eq "openbsd" and $$buffer_ref =~ /\ATerminated\s*\z/
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd"
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd" and $$buffer_ref =~ /\ATerminated\s*\z/
!ll&&!rl&&r
000
$options{"timed_out"} and $^O eq "openbsd"
!ll&&!rl&&r
000
MC/DC0%0/3
$options{"timed_out"} and $^O eq "openbsd" and $$buffer_ref =~ /\ATerminated\s*\z/
$options{"timed_out"} $^O eq "openbsd" $$buffer_ref =~ /\ATerminated\s*\z/
MC/DC0%0/3
$options{"timed_out"} and $^O eq "openbsd" and $$buffer_ref =~ /\ATerminated\s*\z/
$options{"timed_out"} $^O eq "openbsd" $$buffer_ref =~ /\ATerminated\s*\z/
Truth table0%0/4
A and B and C
A= $options{"timed_out"}B= $^O eq "openbsd"C= $$buffer_ref =~ /\ATerminated\s*\z/
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $options{"timed_out"}B= $^O eq "openbsd"C= $$buffer_ref =~ /\ATerminated\s*\z/
ABCresult
0XX0
10X0
1100
1111
179 $options{timed_out} &&
180 $^O eq 'openbsd' &&
181 $$buffer_ref =~ /\ATerminated\s*\z/
182 ) {
183 0 $$buffer_ref = '';
184 } else {
185 0 ▶ if(defined($$held_line_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$held_line_ref) 0 0 0
if (defined $$held_line_ref) 0 0 0
186 0 print STDERR $$held_line_ref;
187 0 undef $$held_line_ref;
188 }
189 0 print STDERR $$buffer_ref;
190 0 $$buffer_ref = '';
191 }
192 }
193  
194 0 ▶ if(defined($$held_line_ref) && !($options{timed_out} && $^O eq 'openbsd')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd") 0 0 0
if (defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd") 0 0 0
Condition0%0/12
defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd"
!ll&&!rl&&r
000
$options{'timed_out'} && $^O eq 'openbsd'
!ll&&!rl&&r
000
defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd"
!ll&&!rl&&r
000
$options{'timed_out'} && $^O eq 'openbsd'
!ll&&!rl&&r
000
MC/DC0%0/3
defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd"
defined $$held_line_ref $options{'timed_out'} $^O eq 'openbsd'
MC/DC0%0/3
defined $$held_line_ref and not $options{"timed_out"} && $^O eq "openbsd"
defined $$held_line_ref $options{'timed_out'} $^O eq 'openbsd'
Truth table0%0/4
A and not(B && C)
A= defined $$held_line_refB= $options{'timed_out'}C= $^O eq 'openbsd'
ABCresult
0XX0
1110
10X1
1101
Truth table0%0/4
A and not(B && C)
A= defined $$held_line_refB= $options{'timed_out'}C= $^O eq 'openbsd'
ABCresult
0XX0
1110
10X1
1101
195 0 print STDERR $$held_line_ref;
196 }
197 }
198  
199 0 ▶sub _terminate_process_tree {
Subroutine _terminate_process_tree: not called CC=4 SCAR=30.0
200 0 ▶ my($pid) = @_;
Subroutine _terminate_process_tree: not called CC=4 SCAR=30.0
201  
202 0 my $process_table = Proc::ProcessTable->new->table;
203 0 my @victims = _get_pids($process_table, $pid);
204 0 kill SIGTERM => @victims, $pid;
205  
206 0 my $deadline = time + 2;
207 0 ▶ while(time < $deadline) {
Branch: 2 branches
BranchTrueFalseTotal
if (time < $deadline) 0 0 0
if (time < $deadline) 0 0 0
208 0 my $waited = waitpid($pid, WNOHANG);
209 0 ▶ return $? if($waited == $pid || $waited == -1);
Branch: 2 branches
BranchTrueFalseTotal
if $waited == $pid or $waited == -1 0 0 0
if $waited == $pid or $waited == -1 0 0 0
Condition0%0/6
$waited == $pid or $waited == -1
l!l&&r!l&&!r
000
$waited == $pid or $waited == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$waited == $pid or $waited == -1
$waited == $pid $waited == -1
MC/DC0%0/2
$waited == $pid or $waited == -1
$waited == $pid $waited == -1
Truth table0%0/3
A or B
A= $waited == $pidB= $waited == -1
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $waited == $pidB= $waited == -1
ABresult
1X1
011
000
210 0 select undef, undef, undef, 0.1;
211 }
212  
213 0 $process_table = Proc::ProcessTable->new->table;
214 0 @victims = _get_pids($process_table, $pid);
215 0 kill SIGKILL => @victims, $pid;
216 0 waitpid($pid, 0);
217 0 $?;
218 }
219  
220 # Copied from Proc::Killfam::get_pids in Proc-ProcessTable-0.634 which is
221 # GPL/Artistic licenced. It's undocumented there so should be considered
222 # unstable, hence why copied.
223 0 ▶sub _get_pids {
Subroutine _get_pids: not called CC=4 SCAR=30.0
224 0 ▶ my($procs, @kids) = @_;
Subroutine _get_pids: not called CC=4 SCAR=30.0
225 0 my @pids;
226 0 foreach my $kid (@kids) {
227 0 foreach my $proc (@$procs) {
228 0 ▶ if ($proc->ppid == $kid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proc->ppid == $kid) 0 0 0
if ($proc->ppid == $kid) 0 0 0
229 0 my $pid = $proc->pid;
230 0 push @pids, $pid, _get_pids($procs, $pid);
231 }
232 }
233 }
234 0 @pids;
235 }
236  
237 =head1 NAME
238  
239 App::ptimeout - module implementing L<ptimeout>
240  
241 =head1 AUTHOR, COPYRIGHT and LICENCE
242  
243 Copyright 2026 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
244  
245 This software is free-as-in-speech software, and may be used,
246 distributed, and modified under the terms of either the GNU
247 General Public Licence version 2 or the Artistic Licence. It's
248 up to you which one you use. The full text of the licences can
249 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
250  
251 =head1 CONSPIRACY
252  
253 This is also free-as-in-mason software.
254  
255 1;
↑ Summary