File Coverage

blib/lib/Daemon/Simple.pm
Criterion Covered Total %
statement 36 68 52.9
branch 2 20 10.0
condition n/a
subroutine 10 11 90.9
pod 0 6 0.0
total 48 105 45.7


line stmt bran cond sub pod time code
1             package Daemon::Simple;
2              
3 1     1   30683 use 5.008001;
  1         5  
  1         41  
4 1     1   6 use strict;
  1         2  
  1         36  
5 1     1   5 use warnings;
  1         6  
  1         56  
6              
7             our $VERSION = '0.1';
8              
9             # Preloaded methods go here.
10 1     1   1139 use Proc::ProcessTable;
  1         21512  
  1         109  
11 1     1   9 use File::Spec;
  1         3  
  1         1175  
12             # Daemon init
13             sub init
14             {
15 0     0 0 0 my ($command,$homedir,$pidfile) = @_;
16 0 0       0 unless($command)
17             {
18 0         0 print "$0 {start|stop}\n";
19 0         0 exit;
20             }
21 0 0       0 unless($homedir)
22             {
23 0         0 $homedir = File::Spec->curdir();
24             }
25 0 0       0 unless($pidfile)
26             {
27 0         0 $pidfile = $homedir."/$0.pid";
28             }
29 0         0 my $pid = get_pidfile($pidfile);
30 0         0 my $is_running = is_running($pid);
31 0 0       0 if( $command eq 'start' )
    0          
32             {
33 0 0       0 if( $is_running )
34             {
35 0         0 print "$0 is Already running.\n";
36 0         0 exit; # stop here
37             }
38             else
39             {
40             # run
41 0         0 print "$0 is Starting.\n";
42             }
43             }
44             elsif( $command eq 'stop' )
45             {
46 0 0       0 if( $is_running )
47             {
48 0         0 print "Sending stop-signal to $0 (PID:$pid).\n";
49 0         0 kill_process($pid);
50 0         0 while( is_running($pid) )
51             {
52 0         0 sleep(1);
53             }
54 0         0 print "$0 (PID:$pid) is stopped.\n";
55 0         0 destroy_pidfile($pidfile);
56             }
57             else
58             {
59 0         0 print "$0 (PID:$pid) is Already stopped.\n";
60             }
61 0         0 exit; # stop here
62             }
63            
64 0         0 my $cpid = fork();
65 0 0       0 if( $cpid ){
66 0         0 exit;
67             }
68 0         0 chdir($homedir);
69 0         0 close(STDIN);
70 0         0 close(STDOUT);
71 0         0 close(STDERR);
72 0         0 create_pidfile($pidfile);
73             }
74              
75             sub is_running
76             {
77 2     2 0 22 my $pid = shift;
78 2         82 my $table = Proc::ProcessTable->new()->table;
79 2         10022 my %processes = map { $_->pid => $_ } @$table;
  21         364  
80 2         143 return exists $processes{$pid};
81             }
82              
83             sub get_pidfile
84             {
85 1     1 0 790 my $pidfile = shift;
86             #get pid from file
87 1 50       18 return 0 unless( -e$pidfile );
88 1         28 open(FILE, "$pidfile");
89 1         16 my $pid = ;
90 1 50       8 die "Unexpected PID in $pidfile" unless $pid =~ /^(\d+)$/;
91 1         3 $pid = $1;
92 1         11 close(FILE);
93 1         11 return $pid;
94             }
95              
96             sub create_pidfile
97             {
98 1     1 0 9 my $pidfile = shift;
99             #write pid to file
100 1         123 open(FILE,">$pidfile");
101 1         21 print FILE $$;
102 1         60 close(FILE);
103             }
104              
105             sub kill_process
106             {
107 1     1 0 9 my $pid = shift;
108             #kill process
109 1         41 kill(9,$pid);
110             }
111             sub destroy_pidfile
112             {
113 1     1 0 3 my $pidfile = shift;
114             #delete pid file
115 1         107 unlink($pidfile);
116             }
117              
118             =cut
119             1;
120             __END__