blib/lib/Daemon/Mplayer.pm

filter: Statementstmt 31.6% 19 / 60 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 6 / 9 Podpod 100% 2 / 2 totaltotal 22.1% 27 / 122 CC 23 SCAR 47.6
CC
23
Cov
18%
_mplayer_daemonize
179.1
mplayer_stop
110.0
mplayer_play
2.0
CRAP 116.6
↑ Summary
1 package Daemon::Mplayer;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 BEGIN {
5 2 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶ use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶ $VERSION = q[0.012];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 @ISA = q[Exporter];
10 2 @EXPORT_OK = qw(mplayer_play mplayer_stop);
11 }
12  
13 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub mplayer_play {
16 0 ▶ _mplayer_daemonize(@_);
Subroutine mplayer_play: not called CC=1 SCAR=6.9
Pod documented
17 }
18  
19  
20 sub _mplayer_daemonize {
21 0 ▶ my $mplayer = shift;
Subroutine _mplayer_daemonize: not called CC=14 SCAR=51.9
22  
23 0 ▶ my $pidfile = $mplayer->{pidfile} || q[/tmp/mplayer_daemon.pid];
Condition0%0/2
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
l!l
00
MC/DC0%0/1
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
$mplayer->{'pidfile'}
Truth table0%0/2
A
A= $mplayer->{'pidfile'}
Aresult
11
00
24 0 ▶ my $logfile = $mplayer->{logfile} || q[/dev/null];
Condition0%0/2
$mplayer->{'logfile'} || '/dev/null'
l!l
00
MC/DC0%0/1
$mplayer->{'logfile'} || '/dev/null'
$mplayer->{'logfile'}
Truth table0%0/2
A
A= $mplayer->{'logfile'}
Aresult
11
00
25 0 ▶ my $mp_path = $mplayer->{path} || q[/usr/bin/mplayer];
Condition0%0/2
$mplayer->{'path'} || '/usr/bin/mplayer'
l!l
00
MC/DC0%0/1
$mplayer->{'path'} || '/usr/bin/mplayer'
$mplayer->{'path'}
Truth table0%0/2
A
A= $mplayer->{'path'}
Aresult
11
00
26  
27  
28 2 ▶ use POSIX qw(setsid);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 my $PID = fork();
30  
31 0 ▶ exit 0 if $PID; # parent
Branch: 1 branch
BranchTrueFalseTotal
if $PID 0 0 0
32 0 ▶ exit 1 if !defined($PID); # out of resources
Branch: 1 branch
BranchTrueFalseTotal
unless defined $PID 0 0 0
33  
34 0 setsid();
35 0 $PID = fork();
36 0 ▶ exit 1 if !defined($PID);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $PID 0 0 0
37  
38 0 ▶ if($PID) { # parent
Branch: 2 branches
BranchTrueFalseTotal
if ($PID) { } 0 0 0
elsif ($PID == 0) { } 0 0 0
39 0 ▶ open(my $fh, '>', $pidfile) or croak($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pidfile or croak($!) 0 0 0
40 0 print $fh $$;
41 0 close($fh);
42  
43 0 waitpid($PID, 0);
44 0 exit 0;
45 }
46  
47 elsif($PID == 0) { # child
48 0 ▶ open(my $fh, '>', $pidfile)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pidfile or croak("Can not open pidfile '${pidfile}': $!") 0 0 0
49 or croak(qq[Can not open pidfile '$pidfile': $!]);
50  
51 0 print $fh $$;
52 0 close($fh);
53  
54 0 ▶ open(STDOUT, '>>', $logfile) unless $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DEBUG"} 0 0 0
55 0 ▶ open(STDERR, '>', '/dev/null') unless $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DEBUG"} 0 0 0
56 0 ▶ open(STDIN, '<', '/dev/null') unless $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DEBUG"} 0 0 0
57  
58 0 exec($mp_path, @{ $mplayer->{args} });
59 }
60 0 return 0;
61 }
62  
63 sub mplayer_stop {
64 0 ▶ my $mplayer = shift;
Subroutine mplayer_stop: not called CC=10 SCAR=47.0
Pod documented
65  
66 0 ▶ croak(qq[Not a hashref: '$mplayer']) if ref($mplayer) ne q[HASH];
Branch: 1 branch
BranchTrueFalseTotal
if ref $mplayer ne "HASH" 0 0 0
67  
68 0 ▶ my $pidfile = $mplayer->{pidfile} || q[/tmp/mplayer_daemon.pid];
Condition0%0/2
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
l!l
00
MC/DC0%0/1
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
$mplayer->{'pidfile'}
Truth table0%0/2
A
A= $mplayer->{'pidfile'}
Aresult
11
00
69  
70 0 ▶ if( (!-f $pidfile) and ($pidfile =~ m/^\d+$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $pidfile and $pidfile =~ /^\d+$/) 0 0 0
Condition0%0/3
not -f $pidfile and $pidfile =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
not -f $pidfile and $pidfile =~ /^\d+$/
not -f $pidfile $pidfile =~ /^\d+$/
Truth table0%0/3
not(A) and B
A= not -f $pidfileB= $pidfile =~ /^\d+$/
ABresult
0X0
100
111
71 0 ▶ return 1 if kill(9, $pidfile);
Branch: 1 branch
BranchTrueFalseTotal
if kill 9, $pidfile 0 0 0
72 }
73 0 ▶ open(my $fh, '<', $pidfile)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pidfile or croak("Can not open pidfile '${pidfile}': $!") 0 0 0
74 or croak(qq[Can not open pidfile '$pidfile': $!]);
75  
76 0 chomp(my $pid = <$fh>);
77 0 close($fh);
78  
79 0 ▶ if($pid !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid =~ /^\d+$/) 0 0 0
80 0 croak(qq[PID '$pid' is not a valid PID]);
81 }
82  
83 0 ▶ if(kill(9, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 9, $pid) { } 0 0 0
84 0 ▶ unlink($pidfile)
Branch: 1 branch
BranchTrueFalseTotal
unlink $pidfile or croak("Can not delete pidfile '${pidfile}': $!") 0 0 0
85 or croak(qq[Can not delete pidfile '$pidfile': $!]);
86 0 return 1;
87 }
88 else {
89 0 croak(qq[Can not kill PID '$pid': $!]);
90 }
91 0 return;
92 }
93  
94  
95 2 1;
96  
97  
98 __END__
↑ Summary