| 1 | package Daemon::Easy; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008008; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub import{ | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=23.9
| |||||||||||||||||||||||||||||||||
| 12 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | my %args = ( (@_ and ref($_[0])) ? %{$_[0]} : @_ ) or (); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | $args{sleep} = 5 unless defined $args{sleep}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | $args{pidfile} ||= "$0.pid"; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"pidfile"} ||= "$0.pid"
MC/DC0%0/1
$args{"pidfile"} ||= "$0.pid"
$args{"pidfile"}
Truth table50%1/2
A
A= $args{"pidfile"}
| |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | $args{stopfile} ||= "$0.stop"; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"stopfile"} ||= "$0.stop"
MC/DC0%0/1
$args{"stopfile"} ||= "$0.stop"
$args{"stopfile"}
Truth table50%1/2
A
A= $args{"stopfile"}
| |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | $args{callback} ||= 'worker'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"callback"} ||= "worker"
MC/DC0%0/1
$args{"callback"} ||= "worker"
$args{"callback"}
Truth table50%1/2
A
A= $args{"callback"}
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 1 | $args{worker} = \&{caller()."::$args{callback}"}; | |||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | *{caller()."::run"} = sub{ | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||
| 23 | 0 | unshift @_, \%args; | |||||||||||||||||||||||||||||||
| 24 | 0 | goto &_run; | |||||||||||||||||||||||||||||||
| 25 | 1 | }; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub _run{ | ||||||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 29 | 0 | my ($args,$cmd) = @_; | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $cmd ||= $ARGV[0]; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$cmd ||= $ARGV[0]
MC/DC0%0/2
$cmd ||= $ARGV[0]
$cmd
$ARGV[0]
Truth table0%0/3
A ||= B
A= $cmdB= $ARGV[0]
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 0 | $cmd = lc $cmd; | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if( $cmd eq 'start' ){ | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if(my $status = status($args)){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 0 | print "the daemon is running with pid: $status\n"; | |||||||||||||||||||||||||||||||
| 36 | 0 | exit; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | 0 | start($args); | |||||||||||||||||||||||||||||||
| 39 | }elsif( $cmd eq 'stop' ){ | ||||||||||||||||||||||||||||||||
| 40 | 0 | stop($args); | |||||||||||||||||||||||||||||||
| 41 | }elsif( $cmd eq 'status' ){ | ||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( status($args) ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | print "the daemon is running with pid: ".status($args)."\n"; | |||||||||||||||||||||||||||||||
| 44 | }else{ | ||||||||||||||||||||||||||||||||
| 45 | 0 | print "the daemon stopped\n"; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | }elsif( $cmd eq 'restart' ){ | ||||||||||||||||||||||||||||||||
| 48 | 0 | stop($args); | |||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sleep(3) while(status($args)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 0 | start($args); | |||||||||||||||||||||||||||||||
| 51 | }else{ | ||||||||||||||||||||||||||||||||
| 52 | 0 | usage(); | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub usage{ | ||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 57 | 0 | print "usage:\n\t $0 [start stop restart status]\n" ; | |||||||||||||||||||||||||||||||
| 58 | 0 | exit; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub start{ | ||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 62 | 0 | my $args = shift; | |||||||||||||||||||||||||||||||
| 63 | 0 | my $pid = fork(); | |||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | die "cant fork, $!\n" unless defined $pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if($pid){ # parent, remember the child pid, and exit | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | open PID,">$args->{pidfile}" or die "cant open $args->{pidfile}, $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 0 | print PID $pid; | |||||||||||||||||||||||||||||||
| 69 | 0 | close PID; | |||||||||||||||||||||||||||||||
| 70 | 0 | exit(0); | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | POSIX::setsid(); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | unlink ($args->{stopfile}) if( -e $args->{stopfile} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | while(1){ | |||||||||||||||||||||||||||||||
| 78 | 0 | eval { $args->{worker}->(); }; | |||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if($@){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | print $@; | |||||||||||||||||||||||||||||||
| 81 | 0 | last; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sleep($args->{sleep}) if $args->{sleep}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if(-e $args->{stopfile} ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 0 | unlink($args->{stopfile}); | |||||||||||||||||||||||||||||||
| 86 | 0 | last; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 0 | unlink ($args->{pidfile}); | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub stop{ | ||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 94 | 0 | my $args = shift; | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | open FH, ">$args->{stopfile}" or die "cant create $args->{stopfile}, $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | close FH; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | # check status, 0 if stoped, pid if running | ||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub status{ | ||||||||||||||||||||||||||||||
|
Subroutine
status: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 101 | 0 | my $args = shift; | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if(-e $args->{pidfile} ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | open PID,$args->{pidfile} or die "cant open $args->{pidfile}, $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 0 | my $pid=<PID>; chomp $pid; | |||||||||||||||||||||||||||||||
| 105 | 0 | close PID; | |||||||||||||||||||||||||||||||
| 106 | 0 | return $pid; | |||||||||||||||||||||||||||||||
| 107 | }else{ | ||||||||||||||||||||||||||||||||
| 108 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||||||