blib/lib/No/Worries/Proc.pm

filter: Statementstmt 86.0% 302 / 351 Branchbran 58.4% 125 / 214 Conditioncond 50.0% 30 / 60 MC/DCmcdc 29.7% 11 / 37 Subroutinesub 94.5% 35 / 37 Podpod 100% 7 / 7 totaltotal 72.2% 510 / 706 CC 143 SCAR 33.0
CC
143
Cov
73%
proc_detach
156.0
_chk_cmd
63.6
proc_terminate
49.8
CRAP 27.2
« blib/lib/No/Worries/PidFile.pm ↑ Summary blib/lib/No/Worries/Syslog.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Proc.pm #
4 # #
5 # Description: process handling without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Proc;
14 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use 5.005; # need the four-argument form of substr()
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 our $VERSION = "1.7";
18 16 our $REVISION = sprintf("%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/);
19  
20 #
21 # used modules
22 #
23  
24 16 ▶use Config qw(%Config);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use IO::Select qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶use No::Worries qw($_IntegerRegexp $_NumberRegexp);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use No::Worries::Dir qw(dir_change);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 16 ▶use Params::Validate qw(validate validate_with :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use POSIX qw(:sys_wait_h :errno_h setsid);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 16 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 #
35 # global variables
36 #
37  
38 16 our(@SigName, $Transient);
39  
40 #
41 # check a command to be executed
42 #
43  
44 54 ▶sub _chk_cmd (@) {
Subroutine _chk_cmd: called CC=11 SCAR=41.5
45 54 my(@cmd) = @_;
46 54 my($path);
47  
48 54 ▶ if ($cmd[0] =~ /\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd[0] =~ m[/]) { } 54 0 54
49 54 ▶ dief("invalid command: %s", $cmd[0]) unless -f $cmd[0] and -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $cmd[0] and -x _ 0 54 54
Condition33%1/3
-f $cmd[0] and -x _
!ll&&!rl&&r
0054
MC/DC0%0/2
-f $cmd[0] and -x _
-f $cmd[0] -x _
Truth table33%1/3
A and B
A= -f $cmd[0]B= -x _
ABresult
0X0
100
111
50 } else {
51 0 ▶ $path = $ENV{PATH} || "/usr/bin:/usr/sbin:/bin:/sbin";
Condition0%0/2
$ENV{'PATH'} || '/usr/bin:/usr/sbin:/bin:/sbin'
l!l
00
MC/DC0%0/1
$ENV{'PATH'} || '/usr/bin:/usr/sbin:/bin:/sbin'
$ENV{'PATH'}
Truth table0%0/2
A
A= $ENV{'PATH'}
Aresult
11
00
52 0 foreach my $dir (split(/:/, $path)) {
53 0 ▶ next unless length($dir) and -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir and -d $dir 0 0 0
Condition0%0/3
length $dir and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
length $dir and -d $dir
length $dir -d $dir
Truth table0%0/3
A and B
A= length $dirB= -d $dir
ABresult
0X0
100
111
54 0 ▶ next unless -f "$dir/$cmd[0]" and -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$dir/$cmd[0]" and -x _ 0 0 0
Condition0%0/3
-f "$dir/$cmd[0]" and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$dir/$cmd[0]" and -x _
-f "$dir/$cmd[0]" -x _
Truth table0%0/3
A and B
A= -f "$dir/$cmd[0]"B= -x _
ABresult
0X0
100
111
55 0 $cmd[0] = "$dir/$cmd[0]";
56 0 last;
57 }
58 0 ▶ dief("command not found: %s", $cmd[0]) unless $cmd[0] =~ /\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd[0] =~ m[/] 0 0 0
59 }
60 54 return(\@cmd);
61 }
62  
63 #
64 # definition of the process structure
65 #
66  
67 16 my $nbre = "(\\d+\\.)?\\d+"; # fractional number pattern
68 16 my $ksre = "([A-Z]+\\/${nbre}\\s+)*[A-Z]+\\/${nbre}"; # kill spec. pattern
69  
70 16 my %proc_structure = (
71 # public
72 command => { optional => 0, type => ARRAYREF },
73 pid => { optional => 0, type => SCALAR, regex => $_IntegerRegexp },
74 start => { optional => 0, type => SCALAR, regex => $_NumberRegexp },
75 stop => { optional => 1, type => SCALAR, regex => $_NumberRegexp },
76 status => { optional => 1, type => SCALAR, regex => qr/^-?\d+$/ },
77 timeout => { optional => 1, type => SCALAR, regex => $_NumberRegexp },
78 # private
79 kill => { optional => 1, type => SCALAR, regex => qr/^${ksre}$/ },
80 maxtime => { optional => 1, type => SCALAR, regex => $_NumberRegexp },
81 fhin => { optional => 1, type => GLOBREF },
82 fhout => { optional => 1, type => GLOBREF },
83 fherr => { optional => 1, type => GLOBREF },
84 bufin => { optional => 1, type => SCALAR },
85 cbout => { optional => 1, type => CODEREF },
86 cberr => { optional => 1, type => CODEREF },
87 );
88  
89 53 ▶sub _chk_proc ($) {
Subroutine _chk_proc: called CC=1 SCAR=0.2
90 53 my($proc) = @_;
91  
92 validate_with(
93 params => $proc,
94 spec => \%proc_structure,
95 0 ▶ on_fail => sub { dief("invalid process structure: %s", $_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 53 );
97 53 return(); # so that validate_with() is called in void context
98 }
99  
100 #
101 # close a file handle used for IPC
102 #
103  
104 65 ▶sub _close ($$$$) {
Subroutine _close: called CC=3 SCAR=11.0
105 65 my($proc, $fh, $what, $ios) = @_;
106  
107 65 ▶ $ios->remove($fh) if $ios;
Branch: 1 branch
BranchTrueFalseTotal
if $ios 64 1 65
108 65 ▶ close($fh) or dief("cannot close(): %s", $!);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &dief("cannot close(): %s", $!) 65 0 65
109 65 delete($proc->{"fh$what"});
110 65 delete($proc->{"cb$what"});
111 }
112  
113 #
114 # try to read from a dead process in case we called _is_alive() on it
115 # before all its output pipes got emptied...
116 #
117  
118 44 ▶sub _read_zombie ($$$) {
Subroutine _read_zombie: called CC=11 SCAR=25.6
119 44 my($proc, $iosr, $iosw) = @_;
120 44 my($fh, $buf, $done);
121  
122 44 foreach my $what (qw(in)) {
123 44 ▶ next unless $proc->{"fh$what"} and $proc->{"cb$what"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$proc{"fh$what"} and $$proc{"cb$what"} 44 0 44
Condition33%1/3
$$proc{"fh$what"} and $$proc{"cb$what"}
!ll&&!rl&&r
4400
MC/DC0%0/2
$$proc{"fh$what"} and $$proc{"cb$what"}
$$proc{"fh$what"} $$proc{"cb$what"}
Truth table33%1/3
A and B
A= $$proc{"fh$what"}B= $$proc{"cb$what"}
ABresult
0X0
100
111
124 0 $fh = $proc->{"fh$what"};
125 # no write, simply close
126 0 _close($proc, $fh, $what, $iosw);
127 }
128 44 foreach my $what (qw(out err)) {
129 88 ▶ next unless $proc->{"fh$what"} and $proc->{"cb$what"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$proc{"fh$what"} and $$proc{"cb$what"} 72 16 88
Condition66%2/3
$$proc{"fh$what"} and $$proc{"cb$what"}
!ll&&!rl&&r
72016
MC/DC50%1/2
$$proc{"fh$what"} and $$proc{"cb$what"}
$$proc{"fh$what"} $$proc{"cb$what"}
Truth table66%2/3
A and B
A= $$proc{"fh$what"}B= $$proc{"cb$what"}
ABresult
0X0
100
111
130 16 $fh = $proc->{"fh$what"};
131 # read until EOF then close
132 16 $done = 1;
133 16 ▶ while ($done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done) 17 16 33
134 17 ▶ last if $iosr and not grep($fh eq $_, $iosr->can_read(1));
Branch: 1 branch
BranchTrueFalseTotal
if $iosr and not grep(($fh eq $_), $iosr->can_read(1)) 0 17 17
Condition66%2/3
$iosr and not grep(($fh eq $_), $iosr->can_read(1))
!ll&&!rl&&r
2150
MC/DC0%0/2
$iosr and not grep(($fh eq $_), $iosr->can_read(1))
$iosr not grep(($fh eq $_), $iosr->can_read(1))
Truth table66%2/3
A and not(B)
A= $iosrB= not grep(($fh eq $_), $iosr->can_read(1))
ABresult
0X0
100
111
135 17 $buf = "";
136 17 $done = sysread($fh, $buf, 8192);
137 17 ▶ dief("cannot sysread(): %s", $!) unless defined($done);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $done 0 17 17
138 17 $proc->{"cb$what"}($proc, $buf);
139 }
140 16 _close($proc, $fh, $what, $iosr);
141 }
142 }
143  
144 #
145 # check if a process is alive, record its status if not
146 #
147  
148 2969 ▶sub _is_alive ($$$) {
Subroutine _is_alive: called CC=4 SCAR=14.3
149 2969 my($proc, $iosr, $iosw) = @_;
150  
151 # check if it recently died
152 2969 ▶ if (waitpid($proc->{pid}, WNOHANG) == $proc->{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($proc->{"pid"}, 1) == $proc->{"pid"}) 44 2925 2969
153 44 $proc->{status} = $?;
154 44 $proc->{stop} = Time::HiRes::time();
155 44 delete($proc->{maxtime});
156 44 delete($proc->{kill});
157 44 _read_zombie($proc, $iosr, $iosw);
158 44 return(0); # no
159 }
160 # check if we can kill it
161 2925 ▶ if (kill(0, $proc->{pid}) or $! == EPERM) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $proc->{"pid"} or $! == 1) 2925 0 2925
Condition33%1/3
kill 0, $proc->{"pid"} or $! == 1
l!l&&r!l&&!r
292500
MC/DC0%0/2
kill 0, $proc->{"pid"} or $! == 1
kill 0, $proc->{"pid"} $! == 1
Truth table33%1/3
A or B
A= kill 0, $proc->{"pid"}B= $! == 1
ABresult
1X1
011
000
162 2925 return(1); # yes
163 }
164 # ooops
165 0 return(); # don't know
166 }
167  
168 #
169 # prepare I/O before creating a process
170 #
171  
172 54 ▶sub _prepare_stdin ($$) {
Subroutine _prepare_stdin: called CC=7 SCAR=20.3
173 54 my($proc, $stdin) = @_;
174 54 my($ref, $rdrin, $wrtin);
175  
176 54 ▶ return() unless defined($stdin);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stdin 43 11 54
177 11 $ref = ref($stdin);
178 11 ▶ if ($ref eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 6 5 11
elsif ($ref eq 'SCALAR') { } 5 0 5
179 6 ▶ if ($stdin eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdin eq '') { } 0 6 6
180 0 dief("unexpected stdin: empty string");
181 } else {
182 ## no critic 'InputOutput::RequireBriefOpen'
183 6 ▶ open($rdrin, "<", $stdin)
Branch: 1 branch
BranchTrueFalseTotal
open $rdrin, "<", $stdin or &dief("cannot open(<, %s): %s", $stdin, $!) 6 0 6
184 or dief("cannot open(<, %s): %s", $stdin, $!);
185 }
186 } elsif ($ref eq "SCALAR") {
187 5 ▶ pipe($rdrin, $wrtin)
Branch: 1 branch
BranchTrueFalseTotal
pipe $rdrin, $wrtin or &dief("cannot pipe(): %s", $!) 5 0 5
188 or dief("cannot pipe(): %s", $!);
189 5 $proc->{fhin} = $wrtin;
190 5 $proc->{bufin} = ${ $stdin };
191 } else {
192 0 dief("unexpected stdin: ref(%s)", $ref);
193 }
194 11 return($rdrin, $wrtin);
195 }
196  
197 54 ▶sub _prepare_stdout ($$) {
Subroutine _prepare_stdout: called CC=9 SCAR=23.6
198 54 my($proc, $stdout) = @_;
199 54 my($ref, $rdrout, $wrtout);
200  
201 54 ▶ return() unless defined($stdout);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stdout 2 52 54
202 52 $ref = ref($stdout);
203 52 ▶ if ($ref eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 8 44 52
elsif ($ref eq 'CODE' or $ref eq 'SCALAR') { } 44 0 44
204 8 ▶ if ($stdout eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout eq '') { } 0 8 8
205 0 dief("unexpected stdout: empty string");
206 } else {
207 ## no critic 'InputOutput::RequireBriefOpen'
208 8 ▶ open($wrtout, ">", $stdout)
Branch: 1 branch
BranchTrueFalseTotal
open $wrtout, ">", $stdout or &dief("cannot open(>, %s): %s", $stdout, $!) 8 0 8
209 or dief("cannot open(>, %s): %s", $stdout, $!);
210 }
211 ▶ } elsif ($ref eq "CODE" or $ref eq "SCALAR") {
Condition33%1/3
$ref eq "CODE" or $ref eq "SCALAR"
l!l&&r!l&&!r
0440
MC/DC0%0/2
$ref eq "CODE" or $ref eq "SCALAR"
$ref eq "CODE" $ref eq "SCALAR"
Truth table33%1/3
A or B
A= $ref eq "CODE"B= $ref eq "SCALAR"
ABresult
1X1
011
000
212 44 ▶ pipe($rdrout, $wrtout)
Branch: 1 branch
BranchTrueFalseTotal
pipe $rdrout, $wrtout or &dief("cannot pipe(): %s", $!) 44 0 44
213 or dief("cannot pipe(): %s", $!);
214 44 $proc->{fhout} = $rdrout;
215 44 ▶ if ($ref eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 0 44 44
216 0 $proc->{cbout} = $stdout;
217 } else {
218 44 ${ $stdout } = "";
219 66 ▶ $proc->{cbout} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
220 66 my($_proc, $_buf) = @_;
221 66 ${ $stdout } .= $_buf;
222 44 };
223 }
224 } else {
225 0 dief("unexpected stdout: ref(%s)", $ref);
226 }
227 52 return($rdrout, $wrtout);
228 }
229  
230 54 ▶sub _prepare_stderr ($$) {
Subroutine _prepare_stderr: called CC=9 SCAR=24.1
231 54 my($proc, $stderr) = @_;
232 54 my($ref, $rdrerr, $wrterr, $merge);
233  
234 54 ▶ return() unless defined($stderr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stderr 15 39 54
235 39 $ref = ref($stderr);
236 39 ▶ if ($ref eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 9 30 39
elsif ($ref eq 'CODE' or $ref eq 'SCALAR') { } 30 0 30
237 9 ▶ if ($stderr eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr eq '') { } 9 0 9
238 # special case: stderr will be merged with stdout
239 9 $merge = 1;
240 } else {
241 ## no critic 'InputOutput::RequireBriefOpen'
242 0 ▶ open($wrterr, ">", $stderr)
Branch: 1 branch
BranchTrueFalseTotal
open $wrterr, ">", $stderr or &dief("cannot open(>, %s): %s", $stderr, $!) 0 0 0
243 or dief("cannot open(>, %s): %s", $stderr, $!);
244 }
245 ▶ } elsif ($ref eq "CODE" or $ref eq "SCALAR") {
Condition33%1/3
$ref eq "CODE" or $ref eq "SCALAR"
l!l&&r!l&&!r
0300
MC/DC0%0/2
$ref eq "CODE" or $ref eq "SCALAR"
$ref eq "CODE" $ref eq "SCALAR"
Truth table33%1/3
A or B
A= $ref eq "CODE"B= $ref eq "SCALAR"
ABresult
1X1
011
000
246 30 ▶ pipe($rdrerr, $wrterr)
Branch: 1 branch
BranchTrueFalseTotal
pipe $rdrerr, $wrterr or &dief("cannot pipe(): %s", $!) 30 0 30
247 or dief("cannot pipe(): %s", $!);
248 30 $proc->{fherr} = $rdrerr;
249 30 ▶ if ($ref eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 0 30 30
250 0 $proc->{cberr} = $stderr;
251 } else {
252 30 ${ $stderr } = "";
253 35 ▶ $proc->{cberr} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
254 35 my($_proc, $_buf) = @_;
255 35 ${ $stderr } .= $_buf;
256 30 };
257 }
258 } else {
259 0 dief("unexpected stderr: ref(%s)", $ref);
260 }
261 39 return($rdrerr, $wrterr, $merge);
262 }
263  
264 #
265 # redirect I/O after creating a process
266 #
267  
268 9 ▶sub _redirect_io ($$$$) {
Subroutine _redirect_io: called CC=12 SCAR=25.3
269 9 my($rdrin, $wrtout, $wrterr, $merge) = @_;
270 9 my($fd);
271  
272 # handle stdin
273 9 ▶ if ($rdrin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rdrin) 2 7 9
274 2 $fd = fileno($rdrin);
275 2 ▶ if (fileno(*STDIN) != $fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno STDIN != $fd) 2 0 2
276 2 ▶ open(*STDIN, "<&=$fd")
Branch: 1 branch
BranchTrueFalseTotal
open *STDIN, "<&=$fd" or &dief("cannot redirect stdin: %s", $!) 2 0 2
277 or dief("cannot redirect stdin: %s", $!);
278 }
279 }
280 # handle stdout
281 9 ▶ if ($wrtout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrtout) 8 1 9
282 8 $fd = fileno($wrtout);
283 8 ▶ if (fileno(*STDOUT) != $fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno STDOUT != $fd) 8 0 8
284 8 ▶ open(*STDOUT, ">&=$fd")
Branch: 1 branch
BranchTrueFalseTotal
open *STDOUT, ">&=$fd" or &dief("cannot redirect stdout: %s", $!) 8 0 8
285 or dief("cannot redirect stdout: %s", $!);
286 }
287 }
288 # handle stderr
289 9 ▶ if ($wrterr or $merge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrterr or $merge) 6 3 9
Condition100%3/3
$wrterr or $merge
l!l&&r!l&&!r
513
MC/DC100%2/2
$wrterr or $merge
$wrterr $merge
Truth table100%3/3
A or B
A= $wrterrB= $merge
ABresult
1X1
011
000
290 6 ▶ $fd = $merge ? fileno(*STDOUT) : fileno($wrterr);
Branch: 1 branch
BranchTrueFalseTotal
$merge ? : 1 5 6
291 6 ▶ if (fileno(*STDERR) != $fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno STDERR != $fd) 6 0 6
292 6 ▶ open(*STDERR, ">&=$fd")
Branch: 1 branch
BranchTrueFalseTotal
open *STDERR, ">&=$fd" or &dief("cannot redirect stderr: %s", $!) 6 0 6
293 or dief("cannot redirect stderr: %s", $!);
294 }
295 }
296 }
297  
298 #
299 # fork a new process, setup its environment and exec() the command
300 #
301  
302 16 my %proc_create_options = (
303 command => { optional => 0, type => ARRAYREF },
304 cwd => { optional => 1, type => SCALAR },
305 timeout => { optional => 1, type => SCALAR, regex => $_NumberRegexp },
306 kill => { optional => 1, type => SCALAR, regex => qr/^${ksre}$/ },
307 stdin => { optional => 1, type => SCALAR | SCALARREF },
308 stdout => { optional => 1, type => SCALAR | SCALARREF | CODEREF },
309 stderr => { optional => 1, type => SCALAR | SCALARREF | CODEREF },
310 );
311  
312 54 ▶sub proc_create (@) {
Subroutine proc_create: called CC=17 SCAR=30.0
Pod documented
313 54 my(%option, %proc, $merge);
314 54 my($rdrin, $wrtin, $rdrout, $wrtout, $rdrerr, $wrterr);
315  
316 #
317 # preparation
318 #
319  
320 54 %option = validate(@_, \%proc_create_options);
321 54 $proc{command} = _chk_cmd(@{ $option{command} });
322 # check the "current working directory" option
323 54 ▶ if (defined($option{cwd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{"cwd"}) 0 54 54
324 0 ▶ dief("invalid directory: %s", $option{cwd}) unless -d $option{cwd};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $option{"cwd"} 0 0 0
325 }
326 # prepare I/O
327 54 ($rdrin, $wrtin) = _prepare_stdin(\%proc, $option{stdin});
328 54 ($rdrout, $wrtout) = _prepare_stdout(\%proc, $option{stdout});
329 54 ($rdrerr, $wrterr, $merge) = _prepare_stderr(\%proc, $option{stderr});
330 # fork
331 54 $proc{pid} = fork();
332 54 ▶ dief("cannot fork(): %s", $!) unless defined($proc{pid});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proc{"pid"} 0 54 54
333  
334 #
335 # handle the child
336 #
337  
338 54 ▶ unless ($proc{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proc{"pid"}) 9 45 54
339 # we are about to exec() or die()
340 9 $Transient = 1;
341 # handle the "current working directory"
342 9 ▶ dir_change($option{cwd}) if defined($option{cwd});
Branch: 1 branch
BranchTrueFalseTotal
if defined $option{"cwd"} 0 9 9
343 # make sure the STD* file handles are "normal"
344 9 foreach my $glob (*STDIN, *STDOUT, *STDERR) {
345 27 ▶ next unless tied($glob);
Branch: 1 branch
BranchTrueFalseTotal
unless tied $glob 27 0 27
346 16 ▶ no warnings qw(untie); ## no critic 'ProhibitNoWarnings'
Subroutine BEGIN: called CC=1 SCAR=0.0
347 0 untie($glob);
348 }
349 # handle the pipe ends to close
350 9 foreach my $fh ($wrtin, $rdrout, $rdrerr) {
351 27 ▶ next unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 14 13 27
352 13 ▶ close($fh) or dief("cannot close pipe: %s", $!);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &dief("cannot close pipe: %s", $!) 13 0 13
353 }
354 # redirect I/O
355 9 _redirect_io($rdrin, $wrtout, $wrterr, $merge);
356 # execute the command
357 9 exec({ $proc{command}[0] } @{ $proc{command} })
358 9 ▶ or dief("cannot execute %s: %s", $proc{command}[0], $!);
Branch: 1 branch
BranchTrueFalseTotal
exec {$proc{"command"}[0];} @{$proc{"command"};} or &dief("cannot execute %s: %s", $proc{"command"}[0], $!) 0 0 0
359 0 exit(-1);
360 }
361  
362 #
363 # handle the father
364 #
365  
366 # record the "start" time
367 45 $proc{start} = Time::HiRes::time();
368 # record the maximum running time
369 45 ▶ if (defined($option{timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{"timeout"}) 6 39 45
370 6 $proc{maxtime} = $proc{start} + $option{timeout};
371 }
372 # record the kill specification
373 45 ▶ $proc{kill} = $option{kill} if $option{kill};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"kill"} 0 45 45
374 # handle the pipe ends to close
375 45 foreach my $fh ($rdrin, $wrtout, $wrterr) {
376 135 ▶ next unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 57 78 135
377 78 ▶ close($fh) or dief("cannot close pipe: %s", $!);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &dief("cannot close pipe: %s", $!) 78 0 78
378 }
379 # so far so good
380 45 return(\%proc);
381 }
382  
383 #
384 # terminate a process
385 #
386  
387 16 my %proc_terminate_options = (
388 kill => { optional => 1, type => SCALAR, regex => qr/^${ksre}$/ },
389 _iosr => { optional => 1, type => UNDEF|OBJECT },
390 _iosw => { optional => 1, type => UNDEF|OBJECT },
391 );
392  
393 7 ▶sub proc_terminate ($@) {
Subroutine proc_terminate: called CC=16 SCAR=39.1
Pod documented
394 7 my($proc, %option, $pid, $sig, $grace, $maxtime);
395  
396 # setup
397 7 $proc = shift(@_);
398 7 ▶ if (ref($proc) eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $proc eq '') { } 0 7 7
elsif (ref $proc eq 'HASH') { } 7 0 7
399 0 ▶ dief("unexpected pid: %s", $proc) unless $proc =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc =~ /^\d+$/ 0 0 0
400 0 $proc = { pid => $proc };
401 } elsif (ref($proc) eq "HASH") {
402 7 _chk_proc($proc);
403 } else {
404 0 dief("unexpected process: %s", $proc);
405 }
406 7 ▶ %option = validate(@_, \%proc_terminate_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 1 7
407 7 ▶ $option{kill} ||= $proc->{kill} || "TERM/1 INT/1 QUIT/1";
Condition40%2/5
$option{"kill"} ||= $proc->{"kill"} || "TERM/1 INT/1 QUIT/1"
l!l&&r!l&&!r
070
$proc->{'kill'} || 'TERM/1 INT/1 QUIT/1'
l!l
07
MC/DC0%0/2
$option{"kill"} ||= $proc->{"kill"} || "TERM/1 INT/1 QUIT/1"
$option{"kill"} $proc->{'kill'}
Truth table0%0/3
A ||= B
A= $option{"kill"}B= $proc->{'kill'}
ABresult
1X1
011
000
408 7 $pid = $proc->{pid};
409 # gentle kill
410 7 foreach my $spec (split(/\s+/, $option{kill})) {
411 7 ▶ if ($spec =~ /^([A-Z]+)\/(${nbre})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[^([A-Z]+)/($nbre)$]) { } 7 0 7
412 7 ($sig, $grace) = ($1, $2);
413 } else {
414 0 dief("unexpected kill specification: %s", $spec);
415 }
416 7 ▶ unless (kill($sig, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill $sig, $pid) 0 7 7
417 0 ▶ dief("cannot kill(%s, %d): %s", $sig, $pid, $!) unless $! == ESRCH;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 3 0 0 0
418 }
419 7 $maxtime = Time::HiRes::time() + $grace;
420 7 ▶ while (Time::HiRes::time() < $maxtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Time::HiRes::time() < $maxtime) 42 0 42
421 42 ▶ return unless _is_alive($proc, $option{_iosr}, $option{_iosw});
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_alive($proc, $option{"_iosr"}, $option{"_iosw"}) 7 35 42
422 35 Time::HiRes::sleep(0.01);
423 }
424 0 ▶ return unless _is_alive($proc, $option{_iosr}, $option{_iosw});
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_alive($proc, $option{"_iosr"}, $option{"_iosw"}) 0 0 0
425 }
426 # hard kill
427 0 $sig = "KILL";
428 0 ▶ unless (kill($sig, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill $sig, $pid) 0 0 0
429 0 ▶ dief("cannot kill(%s, %d): %s", $sig, $pid, $!) unless $! == ESRCH;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 3 0 0 0
430 }
431 }
432  
433 #
434 # setup monitoring
435 #
436  
437 44 ▶sub _monitor_setup ($) {
Subroutine _monitor_setup: called CC=8 SCAR=20.8
438 44 my($procs) = @_;
439 44 my(%process, %map, $iosr, $iosw, $fh);
440  
441 # store the processes to monitor in a hash
442 44 foreach my $proc (@{ $procs }) {
443 46 _chk_proc($proc);
444 46 $process{$proc->{pid}} = $proc;
445 }
446 # record the file handles to monitor
447 44 $iosr = IO::Select->new();
448 44 $iosw = IO::Select->new();
449 44 foreach my $proc (values(%process)) {
450 46 foreach my $what (qw(in out err)) {
451 138 $fh = $proc->{"fh$what"};
452 138 ▶ next unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 73 65 138
453 65 ▶ if ($what eq "in") {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq 'in') { } 4 61 65
454 4 $iosw->add($fh);
455 } else {
456 61 $iosr->add($fh);
457 }
458 65 $map{"$fh"} = [ $proc->{pid}, $what ];
459 }
460 }
461 44 ▶ $iosr = undef unless $iosr->count();
Branch: 1 branch
BranchTrueFalseTotal
unless $iosr->count 8 36 44
462 44 ▶ $iosw = undef unless $iosw->count();
Branch: 1 branch
BranchTrueFalseTotal
unless $iosw->count 40 4 44
463 44 return(\%process, \%map, $iosr, $iosw);
464 }
465  
466 #
467 # monitor I/O
468 #
469  
470 2085 ▶sub _monitor_reading ($$$$$) {
Subroutine _monitor_reading: called CC=4 SCAR=13.9
471 2085 my($process, $map, $iosr, $bufsize, $timeout) = @_;
472 2085 my($buf, $done, $proc, $what);
473  
474 2085 foreach my $fh ($iosr->can_read($timeout)) {
475 84 $timeout = 0;
476 84 $buf = "";
477 84 $done = sysread($fh, $buf, $bufsize);
478 84 ▶ dief("cannot sysread(): %s", $!) unless defined($done);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $done 0 84 84
479 84 $proc = $process->{$map->{"$fh"}[0]};
480 84 $what = $map->{"$fh"}[1];
481 84 $proc->{"cb$what"}($proc, $buf);
482 84 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 45 39 84
483 45 _close($proc, $fh, $what, $iosr);
484 }
485 }
486 2085 return($timeout);
487 }
488  
489 8 ▶sub _monitor_writing ($$$$$) {
Subroutine _monitor_writing: called CC=4 SCAR=13.9
490 8 my($process, $map, $iosw, $bufsize, $timeout) = @_;
491 8 my($buf, $done, $proc, $what);
492  
493 8 foreach my $fh ($iosw->can_write($timeout)) {
494 8 $timeout = 0;
495 8 $proc = $process->{$map->{"$fh"}[0]};
496 8 $what = $map->{"$fh"}[1];
497 8 $buf = $proc->{"buf$what"};
498 8 ▶ if (length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 4 4 8
499 4 $done = syswrite($fh, $buf, length($buf));
500 4 ▶ dief("cannot syswrite(): %s", $!) unless defined($done);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $done 0 4 4
501 4 substr($proc->{"buf$what"}, 0, $done, "");
502 } else {
503 4 _close($proc, $fh, $what, $iosw);
504 }
505 }
506 8 return($timeout);
507 }
508  
509 #
510 # monitor termination (death and timeout)
511 #
512  
513 2855 ▶sub _monitor_termination ($$$$) {
Subroutine _monitor_termination: called CC=5 SCAR=16.1
514 2855 my($process, $iosr, $iosw, $timeout) = @_;
515 2855 my($now);
516  
517 # check if some processes finished
518 2855 foreach my $proc (grep(!defined($_->{status}), values(%{ $process }))) {
519 2927 ▶ next if _is_alive($proc, $iosr, $iosw);
Branch: 1 branch
BranchTrueFalseTotal
if &_is_alive($proc, $iosr, $iosw) 2890 37 2927
520 37 $timeout = 0;
521 }
522 # check if some processes timed out
523 2855 $now = Time::HiRes::time();
524 2855 foreach my $proc (grep($_->{maxtime}, values(%{ $process }))) {
525 252 ▶ next unless $now > $proc->{maxtime};
Branch: 1 branch
BranchTrueFalseTotal
unless $now > $proc->{"maxtime"} 246 6 252
526 6 $timeout = 0;
527 6 delete($proc->{maxtime});
528 6 $proc->{timeout} = $now;
529 6 proc_terminate($proc, _iosr => $iosr, _iosw => $iosw);
530 }
531 2855 return($timeout);
532 }
533  
534 #
535 # monitor one or more processes
536 #
537  
538 16 my %proc_monitor_options = (
539 timeout => { optional => 1, type => SCALAR, regex => $_NumberRegexp },
540 bufsize => { optional => 1, type => SCALAR, regex => $_IntegerRegexp },
541 deaths => { optional => 1, type => SCALAR, regex => $_IntegerRegexp },
542 );
543  
544 44 ▶sub proc_monitor ($@) {
Subroutine proc_monitor: called CC=20 SCAR=30.2
Pod documented
545 44 my($procs, %option, $process, $map, $iosr, $iosw);
546 44 my($maxtime, $timeout, $zombies);
547  
548 #
549 # preparation
550 #
551  
552 44 $procs = shift(@_);
553 44 ▶ if (ref($procs) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $procs eq 'HASH') { } 42 2 44
elsif (ref $procs ne 'ARRAY') { } 0 2 2
554 42 $procs = [ $procs ];
555 } elsif (ref($procs) ne "ARRAY") {
556 0 dief("unexpected processes: %s", $procs);
557 }
558 44 ▶ %option = validate(@_, \%proc_monitor_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 42 44
559 44 ▶ $option{bufsize} ||= 8192;
Condition50%1/2
$option{"bufsize"} ||= 8192
l!l
044
MC/DC0%0/1
$option{"bufsize"} ||= 8192
$option{"bufsize"}
Truth table50%1/2
A
A= $option{"bufsize"}
Aresult
11
00
560 44 ($process, $map, $iosr, $iosw) = _monitor_setup($procs);
561 # count the number of processes which are already dead
562 44 $zombies = grep(defined($_->{status}), values(%{ $process }));
563  
564 #
565 # work
566 #
567  
568 $maxtime = Time::HiRes::time() + $option{timeout}
569 44 ▶ if defined($option{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if defined $option{"timeout"} 1 43 44
570 44 ▶ while ($iosr or $iosw
Branch: 1 branch
BranchTrueFalseTotal
if ($iosr or $iosw or grep((!defined($_->{"status"})), values %{$process;})) 2855 43 2898
Condition83%5/6
$iosr or $iosw or grep((!defined($_->{"status"})), values %{$process;})
l!l&&r!l&&!r
208577043
$iosr or $iosw
l!l&&r!l&&!r
20850813
MC/DC66%2/3
$iosr or $iosw or grep((!defined($_->{"status"})), values %{$process;})
$iosr $iosw grep((!defined($_->{"status"})), values %{$process;})
Truth table75%3/4
A or B or C
A= $iosrB= $ioswC= grep((!defined($_->{"status"})), values %{$process;})
ABCresult
1XX1
01X1
0011
0000
571 813 or grep(!defined($_->{status}), values(%{ $process }))) {
572 2855 $timeout = 0.01;
573 # read what can be read
574 $timeout = _monitor_reading($process, $map, $iosr, $option{bufsize},
575 2855 ▶ $timeout) if $iosr;
Branch: 1 branch
BranchTrueFalseTotal
if $iosr 2085 770 2855
576 # write what can be written
577 $timeout = _monitor_writing($process, $map, $iosw, $option{bufsize},
578 2855 ▶ $timeout) if $iosw;
Branch: 1 branch
BranchTrueFalseTotal
if $iosw 8 2847 2855
579 # check if some processes finished or timed out
580 2855 $timeout = _monitor_termination($process, $iosr, $iosw, $timeout);
581 # or if we timed out
582 2855 ▶ last if $maxtime and Time::HiRes::time() > $maxtime;
Branch: 1 branch
BranchTrueFalseTotal
if $maxtime and &Time::HiRes::time() > $maxtime 0 2855 2855
Condition33%1/3
$maxtime and &Time::HiRes::time() > $maxtime
!ll&&!rl&&r
285500
MC/DC0%0/2
$maxtime and &Time::HiRes::time() > $maxtime
$maxtime &Time::HiRes::time() > $maxtime
Truth table33%1/3
A and B
A= $maxtimeB= &Time::HiRes::time() > $maxtime
ABresult
0X0
100
111
583 # or if enough processes died
584 last if $option{deaths}
585 72 and grep(defined($_->{status}), values(%{ $process }))
586 2855 ▶ >= $zombies + $option{deaths};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"deaths"} and grep(defined($_->{"status"}), values %{$process;}) >= $zombies + $option{"deaths"} 1 2854 2855
Condition100%3/3
$option{"deaths"} and grep(defined($_->{"status"}), values %{$process;}) >= $zombies + $option{"deaths"}
!ll&&!rl&&r
2783711
MC/DC100%2/2
$option{"deaths"} and grep(defined($_->{"status"}), values %{$process;}) >= $zombies + $option{"deaths"}
$option{"deaths"} grep(defined($_->{"status"}), values %{$process;}) >= $zombies + $option{"deaths"}
Truth table100%3/3
A and B
A= $option{"deaths"}B= grep(defined($_->{"status"}), values %{$process;}) >= $zombies + $option{"deaths"}
ABresult
0X0
100
111
587 # sleep a bit if needed (= if we have not worked before in the loop)
588 2854 ▶ Time::HiRes::sleep($timeout) if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 2768 86 2854
589 # update the IO::Select objects
590 2854 ▶ $iosr = undef unless $iosr and $iosr->count();
Branch: 1 branch
BranchTrueFalseTotal
unless $iosr and $iosr->count 805 2049 2854
Condition100%3/3
$iosr and $iosr->count
!ll&&!rl&&r
770352049
MC/DC100%2/2
$iosr and $iosr->count
$iosr $iosr->count
Truth table100%3/3
A and B
A= $iosrB= $iosr->count
ABresult
0X0
100
111
591 2854 ▶ $iosw = undef unless $iosw and $iosw->count();
Branch: 1 branch
BranchTrueFalseTotal
unless $iosw and $iosw->count 2850 4 2854
Condition100%3/3
$iosw and $iosw->count
!ll&&!rl&&r
284644
MC/DC100%2/2
$iosw and $iosw->count
$iosw $iosw->count
Truth table100%3/3
A and B
A= $ioswB= $iosw->count
ABresult
0X0
100
111
592 }
593 }
594  
595 #
596 # run the given command
597 #
598  
599 49 ▶sub proc_run (@) {
Subroutine proc_run: called CC=2 SCAR=6.9
Pod documented
600 49 my(@args) = @_;
601 49 my($proc);
602  
603 # create the process
604 49 $proc = proc_create(@args);
605 # monitor it until it ends
606 42 proc_monitor($proc);
607 # return what is expected
608 42 ▶ return(%{ $proc }) if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 6 36 42
609 36 return($proc->{status});
610 }
611  
612 #
613 # execute the given command, check its status and return its output
614 #
615  
616 4 ▶sub proc_output (@) {
Subroutine proc_output: called CC=2 SCAR=7.0
Pod documented
617 4 my(@command) = @_;
618 4 my($output, $status);
619  
620 4 $output = "";
621 4 $status = proc_run(command => \@command, stdout => \$output);
622 3 ▶ dief("%s failed: %d", $command[0], $status) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 3 3
623 3 return($output);
624 }
625  
626 #
627 # detach ourself and go in the background
628 #
629  
630 16 my %proc_detach_options = (
631 callback => { optional => 1, type => CODEREF },
632 );
633  
634 0 ▶sub proc_detach (@) {
Subroutine proc_detach: not called CC=12 SCAR=50.5
Pod documented
635 0 my(%option, $pid, $sid);
636  
637 0 ▶ %option = validate(@_, \%proc_detach_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
638 # change directory to a known place
639 0 dir_change("/");
640 # fork and let dad die
641 0 $pid = fork();
642 0 ▶ dief("cannot fork(): %s", $!) unless defined($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
643 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
644 # we are about to exit()
645 0 $Transient = 1;
646 0 ▶ $option{callback}->($pid) if $option{callback};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"callback"} 0 0 0
647 0 exit(0);
648 }
649 # create a new session
650 0 $sid = setsid();
651 0 ▶ dief("cannot setsid(): %s", $!) if $sid == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $sid == -1 0 0 0
652 # detach std* from anything but plain files (i.e. allow: cmd --detach > log)
653 0 ▶ unless (-f STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f STDIN) 0 0 0
654 0 ▶ open(STDIN, "<", "/dev/null")
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "/dev/null" or &dief("cannot re-open stdin: %s", $!) 0 0 0
655 or dief("cannot re-open stdin: %s", $!);
656 }
657 0 ▶ unless (-f STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f STDOUT) 0 0 0
658 0 ▶ open(STDOUT, ">", "/dev/null")
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", "/dev/null" or &dief("cannot re-open stdout: %s", $!) 0 0 0
659 or dief("cannot re-open stdout: %s", $!);
660 }
661 0 ▶ unless (-f STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f STDERR) 0 0 0
662 0 ▶ open(STDERR, ">", "/dev/null")
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", "/dev/null" or &dief("cannot re-open stderr: %s", $!) 0 0 0
663 or dief("cannot re-open stderr: %s", $!);
664 }
665 }
666  
667 #
668 # return a string representation of the process status
669 #
670  
671 6 ▶sub proc_status ($) {
Subroutine proc_status: called CC=5 SCAR=17.3
Pod documented
672 6 my($status) = @_;
673 6 my($signum, @list);
674  
675 6 ▶ return("ok") unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 3 3 6
676 3 $signum = $status & 127;
677 3 push(@list, sprintf("code=%d", $status >> 8));
678 3 ▶ push(@list, sprintf("signal=%s", $SigName[$signum] || $signum))
Branch: 1 branch
BranchTrueFalseTotal
if $signum 0 3 3
Condition0%0/3
$No::Worries::Proc::SigName[$signum] || $signum
l!l&&r!l&&!r
000
MC/DC0%0/2
$No::Worries::Proc::SigName[$signum] || $signum
$No::Worries::Proc::SigName[$signum] $signum
Truth table0%0/3
A || B
A= $No::Worries::Proc::SigName[$signum]B= $signum
ABresult
1X1
011
000
679 if $signum;
680 3 ▶ push(@list, "(core dumped)")
Branch: 1 branch
BranchTrueFalseTotal
if $status & 128 0 3 3
681 if $status & 128;
682 3 return(join(" ", @list));
683 }
684  
685 #
686 # module initialization
687 #
688  
689 16 @SigName[split(/\s+/, $Config{sig_num})] = split(/\s+/, $Config{sig_name});
690  
691 #
692 # export control
693 #
694  
695 28 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
696 28 my($pkg, %exported);
697  
698 28 $pkg = shift(@_);
699 28 grep($exported{$_}++,
700 map("proc_$_", qw(create detach monitor output run status terminate)));
701 28 export_control(scalar(caller()), $pkg, \%exported, @_);
702 }
703  
704 16 1;
705  
706 __DATA__
« blib/lib/No/Worries/PidFile.pm ↑ Summary blib/lib/No/Worries/Syslog.pm »