| 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.010]; | ||||||||||||||||||||||||||
|
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'
MC/DC0%0/1
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
$mplayer->{'pidfile'}
Truth table0%0/2
A
A= $mplayer->{'pidfile'}
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my $logfile = $mplayer->{logfile} || q[/dev/null]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$mplayer->{'logfile'} || '/dev/null'
MC/DC0%0/1
$mplayer->{'logfile'} || '/dev/null'
$mplayer->{'logfile'}
Truth table0%0/2
A
A= $mplayer->{'logfile'}
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $mp_path = $mplayer->{path} || q[/usr/bin/mplayer]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$mplayer->{'path'} || '/usr/bin/mplayer'
MC/DC0%0/1
$mplayer->{'path'} || '/usr/bin/mplayer'
$mplayer->{'path'}
Truth table0%0/2
A
A= $mplayer->{'path'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | exit 1 if !defined($PID); # out of resources | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | setsid(); | |||||||||||||||||||||||||||
| 35 | 0 | $PID = fork(); | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | exit 1 if !defined($PID); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if($PID) { # parent | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | open(my $fh, '>', $pidfile) or croak($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | open(STDERR, '>', '/dev/null') unless $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | open(STDIN, '<', '/dev/null') unless $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 hashre: '$mplayer']) if ref($mplayer) ne q[HASH]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $pidfile = $mplayer->{pidfile} || q[/tmp/mplayer_daemon.pid]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
MC/DC0%0/1
$mplayer->{'pidfile'} || '/tmp/mplayer_daemon.pid'
$mplayer->{'pidfile'}
Truth table0%0/2
A
A= $mplayer->{'pidfile'}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if( (!-f $pidfile) and ($pidfile =~ m/^\d+$/) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not -f $pidfile and $pidfile =~ /^\d+$/
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+$/
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return 1 if kill(9, $pidfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | open(my $fh, '<', $pidfile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | 0 | croak(qq[PID '$pid' is not a valid PID]); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if(kill(9, $pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | unlink($pidfile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||