File Coverage

blib/lib/Daemon/Mplayer.pm
Criterion Covered Total %
statement 18 59 30.5
branch 0 34 0.0
condition 0 11 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 26 115 22.6


line stmt bran cond sub pod time code
1             package Daemon::Mplayer;
2 1     1   115656 use strict;
  1         2  
  1         35  
3              
4             BEGIN {
5 1     1   4 use Exporter;
  1         2  
  1         52  
6 1     1   7 use vars qw($VERSION @ISA @EXPORT_OK);
  1         1  
  1         93  
7              
8 1     1   3 $VERSION = q[0.012];
9 1         15 @ISA = q[Exporter];
10 1         21 @EXPORT_OK = qw(mplayer_play mplayer_stop);
11             }
12              
13 1     1   6 use Carp qw(croak);
  1         8  
  1         119  
14              
15             sub mplayer_play {
16 0     0 1   _mplayer_daemonize(@_);
17             }
18              
19              
20             sub _mplayer_daemonize {
21 0     0     my $mplayer = shift;
22              
23 0   0       my $pidfile = $mplayer->{pidfile} || q[/tmp/mplayer_daemon.pid];
24 0   0       my $logfile = $mplayer->{logfile} || q[/dev/null];
25 0   0       my $mp_path = $mplayer->{path} || q[/usr/bin/mplayer];
26              
27              
28 1     1   715 use POSIX qw(setsid);
  1         30041  
  1         7  
29 0           my $PID = fork();
30              
31 0 0         exit 0 if $PID; # parent
32 0 0         exit 1 if !defined($PID); # out of resources
33              
34 0           setsid();
35 0           $PID = fork();
36 0 0         exit 1 if !defined($PID);
37              
38 0 0         if($PID) { # parent
    0          
39 0 0         open(my $fh, '>', $pidfile) or croak($!);
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 0         open(my $fh, '>', $pidfile)
49             or croak(qq[Can not open pidfile '$pidfile': $!]);
50              
51 0           print $fh $$;
52 0           close($fh);
53              
54 0 0         open(STDOUT, '>>', $logfile) unless $ENV{DEBUG};
55 0 0         open(STDERR, '>', '/dev/null') unless $ENV{DEBUG};
56 0 0         open(STDIN, '<', '/dev/null') unless $ENV{DEBUG};
57              
58 0           exec($mp_path, @{ $mplayer->{args} });
  0            
59             }
60 0           return 0;
61             }
62              
63             sub mplayer_stop {
64 0     0 1   my $mplayer = shift;
65              
66 0 0         croak(qq[Not a hashref: '$mplayer']) if ref($mplayer) ne q[HASH];
67              
68 0   0       my $pidfile = $mplayer->{pidfile} || q[/tmp/mplayer_daemon.pid];
69              
70 0 0 0       if( (!-f $pidfile) and ($pidfile =~ m/^\d+$/) ) {
71 0 0         return 1 if kill(9, $pidfile);
72             }
73 0 0         open(my $fh, '<', $pidfile)
74             or croak(qq[Can not open pidfile '$pidfile': $!]);
75              
76 0           chomp(my $pid = <$fh>);
77 0           close($fh);
78              
79 0 0         if($pid !~ /^\d+$/) {
80 0           croak(qq[PID '$pid' is not a valid PID]);
81             }
82              
83 0 0         if(kill(9, $pid)) {
84 0 0         unlink($pidfile)
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             1;
96              
97              
98             __END__