| 1 | package Parse::Daemontools::Service; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use 5.008_005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use bigint; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 6 | my($class, $args) = @_; | |||||||||||||||||||||||||||
| 14 | 6 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args ||= {}
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | my $self = bless { | |||||||||||||||||||||||||||
| 17 | base_dir => '/service', | ||||||||||||||||||||||||||||
| 18 | %$args, | ||||||||||||||||||||||||||||
| 19 | }, $class; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | return $self; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | sub base_dir { | ||||||||||||||||||||||||||
|
Subroutine
base_dir: called CC=3 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 5 | my($self, $base_dir) = @_; | |||||||||||||||||||||||||||
| 26 | 5 | ▶ | if ($base_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if (! -d $base_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | Carp::carp("No such directory: $base_dir"); | |||||||||||||||||||||||||||
| 29 | 0 | return; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | $self->{base_dir} = $base_dir; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 5 | return $self->{base_dir}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=31 SCAR=38.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 5 | my($self, $service, $param) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | if (! $service) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | Carp::carp("Missing mandatory args: service"); | |||||||||||||||||||||||||||
| 41 | 0 | return; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | my $service_dir = join '/', $self->base_dir, $service; | |||||||||||||||||||||||||||
| 45 | 5 | ▶ | if (! -d $service_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | Carp::carp("No such directory: $service_dir"); | |||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ### down | ||||||||||||||||||||||||||||
| 51 | 5 | my $normallyup = 0; | |||||||||||||||||||||||||||
| 52 | 5 | ▶ | $normallyup = 1 if ! -e "$service_dir/down"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ### supervise/status | ||||||||||||||||||||||||||||
| 55 | 5 | my $status_file = join '/', $service_dir, 'supervise', 'status'; | |||||||||||||||||||||||||||
| 56 | 5 | ▶ | if (! -f $status_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | Carp::carp("No such status file: $status_file"); | |||||||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | ▶ | open my $fh, '<', $status_file or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | Carp::carp("Failed to open status file: $status_file: $!"); | |||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 5 | my $status = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 66 | 5 | close $fh; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | my($tai_h, $tai_l, $nanosec, $pid, $paused, $want) = unpack "NNLVCa", $status; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | $pid = undef if $pid == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | my $when = ($tai_h << 32) + $tai_l; | |||||||||||||||||||||||||||
| 73 | 5 | my $now = tai64_now(); | |||||||||||||||||||||||||||
| 74 | 5 | ▶ | if ($now < $when) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $when = $now; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 5 | my $elapse = $now - $when; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | my @info; | |||||||||||||||||||||||||||
| 80 | 5 | ▶ | push @info, "normally down" if ($pid && !$normallyup); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid and not $normallyup
MC/DC0%0/2
$pid and not $normallyup
$pid
not $normallyup
Truth table66%2/3
A and not(B)
A= $pidB= not $normallyup
| |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | push @info, "normally up" if (!$pid && $normallyup); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $pid and $normallyup
MC/DC0%0/2
not $pid and $normallyup
not $pid
$normallyup
Truth table33%1/3
not(A) and B
A= not $pidB= $normallyup
| |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | push @info, "paused" if ($pid && $paused); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid and $paused
MC/DC0%0/2
$pid and $paused
$pid
$paused
Truth table66%2/3
A and B
A= $pidB= $paused
| |||||||||||||||||||||||||||||
| 83 | 5 | ▶ | push @info, "want up" if (!$pid && $want eq 'u'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $pid and $want eq "u"
MC/DC0%0/2
not $pid and $want eq "u"
not $pid
$want eq "u"
Truth table66%2/3
not(A) and B
A= not $pidB= $want eq "u"
| |||||||||||||||||||||||||||||
| 84 | 5 | ▶ | push @info, "want down" if ($pid && $want eq 'd'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid and $want eq "d"
MC/DC0%0/2
$pid and $want eq "d"
$pid
$want eq "d"
Truth table66%2/3
A and B
A= $pidB= $want eq "d"
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | ### env/ | ||||||||||||||||||||||||||||
| 87 | 5 | my $env = {}; | |||||||||||||||||||||||||||
| 88 | 5 | my @env_dir; | |||||||||||||||||||||||||||
| 89 | 5 | ▶ | if ($param->{env_dir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | if (ref $param->{env_dir} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | @env_dir = @{ $param->{env_dir} }; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 1 | @env_dir = ($param->{env_dir}); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 2 | @env_dir = ("$service_dir/env"); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 5 | for my $ed (@env_dir) { | |||||||||||||||||||||||||||
| 99 | 6 | ▶ | next unless -d $ed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | if (opendir my $envdir, $ed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 5 | ▶ | while (my $k = readdir $envdir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 20 | ▶ | next if $k =~ /^\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 10 | ▶ | open my $fh, '<', "$ed/$k" or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 10 | my $v = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 106 | 10 | close $fh; | |||||||||||||||||||||||||||
| 107 | 10 | chomp $v; | |||||||||||||||||||||||||||
| 108 | 10 | $env->{$k} = $v; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | Carp::carp("Failed to open env dir: $ed: $!"); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | my $start_at = tai642unix($when); | |||||||||||||||||||||||||||
| 116 | 5 | ▶ | $start_at = $start_at->numify if ref($start_at) && $start_at->can('numify'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $start_at and $start_at->can("numify")
MC/DC0%0/2
ref $start_at and $start_at->can("numify")
ref $start_at
$start_at->can("numify")
Truth table33%1/3
A and B
A= ref $start_atB= $start_at->can("numify")
| |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | $elapse = $elapse->numify if ref($elapse) && $elapse->can('numify'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $elapse and $elapse->can("numify")
MC/DC0%0/2
ref $elapse and $elapse->can("numify")
ref $elapse
$elapse->can("numify")
Truth table33%1/3
A and B
A= ref $elapseB= $elapse->can("numify")
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | return { | ||||||||||||||||||||||||||||
| 120 | 5 | ▶ | service => $service_dir, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | status => defined $pid ? 'up' : 'down', | ||||||||||||||||||||||||||||
| 122 | pid => $pid, | ||||||||||||||||||||||||||||
| 123 | seconds => $elapse, | ||||||||||||||||||||||||||||
| 124 | start_at => $start_at, | ||||||||||||||||||||||||||||
| 125 | info => join(", ", @info), | ||||||||||||||||||||||||||||
| 126 | env => $env, | ||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # http://cr.yp.to/libtai/tai64.html | ||||||||||||||||||||||||||||
| 131 | # http://cr.yp.to/proto/tai64.txt | ||||||||||||||||||||||||||||
| 132 | 5 | ▶ | sub unix2tai64 { | ||||||||||||||||||||||||||
|
Subroutine
unix2tai64: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 5 | my $u = shift; | |||||||||||||||||||||||||||
| 134 | 5 | return 4611686018427387914 + $u; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | ▶ | sub tai642unix { | ||||||||||||||||||||||||||
|
Subroutine
tai642unix: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 5 | my $t = shift; | |||||||||||||||||||||||||||
| 139 | 5 | return $t - 4611686018427387914; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | ▶ | sub tai64_now { | ||||||||||||||||||||||||||
|
Subroutine
tai64_now: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 5 | return unix2tai64(time()); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 3 | 1; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||