| 1 | #+############################################################################## | ||||||||||||||||||||||||||||
| 2 | # # | ||||||||||||||||||||||||||||
| 3 | # File: No/Worries/PidFile.pm # | ||||||||||||||||||||||||||||
| 4 | # # | ||||||||||||||||||||||||||||
| 5 | # Description: pid file handling without worries # | ||||||||||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # module definition | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package No::Worries::PidFile; | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = "1.7"; | |||||||||||||||||||||||||||
| 17 | 2 | our $REVISION = sprintf("%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # used modules | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Fcntl qw(:DEFAULT :flock :seek); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use No::Worries qw($_IntegerRegexp $_NumberRegexp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use No::Worries::Die qw(dief); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use No::Worries::Export qw(export_control); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use No::Worries::Proc qw(proc_terminate); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use No::Worries::Stat qw(ST_MTIME); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use Params::Validate qw(validate :types); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use POSIX qw(:errno_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use Time::HiRes qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # safely read something from an open file | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub _read ($$;$) { | ||||||||||||||||||||||||||
|
Subroutine
_read: called CC=8 SCAR=22.7
| |||||||||||||||||||||||||||||
| 38 | 2 | my($path, $fh, $noclose) = @_; | |||||||||||||||||||||||||||
| 39 | 2 | my($data, $done); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | flock($fh, LOCK_EX) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | or dief("cannot flock(%s, LOCK_EX): %s", $path, $!); | ||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sysseek($fh, 0, SEEK_SET) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | or dief("cannot sysseek(%s, 0, SEEK_SET): %s", $path, $!); | ||||||||||||||||||||||||||||
| 45 | 2 | $data = ""; | |||||||||||||||||||||||||||
| 46 | 2 | $done = -1; | |||||||||||||||||||||||||||
| 47 | 2 | ▶ | while ($done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 4 | $done = sysread($fh, $data, 16, length($data)); | |||||||||||||||||||||||||||
| 49 | 4 | ▶ | dief("cannot sysread(%s, %d): %s", $path, 16, $!) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | unless defined($done); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if ($noclose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | flock($fh, LOCK_UN) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or dief("cannot flock(%s, LOCK_UN): %s", $path, $!); | ||||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 2 | ▶ | close($fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | or dief("cannot close(%s): %s", $path, $!); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 2 | return($data); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | # safely write something to an open file | ||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub _write ($$$) { | ||||||||||||||||||||||||||
|
Subroutine
_write: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||
| 67 | 1 | my($path, $fh, $data) = @_; | |||||||||||||||||||||||||||
| 68 | 1 | my($length, $offset, $done); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | flock($fh, LOCK_EX) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | or dief("cannot flock(%s, LOCK_EX): %s", $path, $!); | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sysseek($fh, 0, SEEK_SET) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | or dief("cannot sysseek(%s, 0, SEEK_SET): %s", $path, $!); | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | truncate($fh, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or dief("cannot truncate(%s, 0): %s", $path, $!); | ||||||||||||||||||||||||||||
| 76 | 1 | $length = length($data); | |||||||||||||||||||||||||||
| 77 | 1 | $offset = 0; | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | while ($length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | $done = syswrite($fh, $data, $length, $offset); | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | dief("cannot syswrite(%s, %d): %s", $path, $length, $!) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | unless defined($done); | ||||||||||||||||||||||||||||
| 82 | 1 | $length -= $done; | |||||||||||||||||||||||||||
| 83 | 1 | $offset += $done; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | close($fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | or dief("cannot close(%s): %s", $path, $!); | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # check if a process is alive by killing it ;-) | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub _alive ($) { | ||||||||||||||||||||||||||
|
Subroutine
_alive: called CC=3 SCAR=16.5
| |||||||||||||||||||||||||||||
| 94 | 1 | my($pid) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | return(1) if kill(0, $pid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return(0) if $! == ESRCH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | dief("cannot kill(0, %d): %s", $pid, $!); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # | ||||||||||||||||||||||||||||
| 102 | # kill a process | ||||||||||||||||||||||||||||
| 103 | # | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub _kill ($$$%) { | ||||||||||||||||||||||||||
|
Subroutine
_kill: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 106 | 0 | my($path, $fh, $pid, %option) = @_; | |||||||||||||||||||||||||||
| 107 | 0 | my($maxtime); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # gently | ||||||||||||||||||||||||||||
| 110 | 0 | $option{callback}->("(pid $pid) is being told to quit..."); | |||||||||||||||||||||||||||
| 111 | 0 | _write($path, $fh, "$pid\nquit\n"); | |||||||||||||||||||||||||||
| 112 | 0 | $maxtime = Time::HiRes::time() + $option{linger}; | |||||||||||||||||||||||||||
| 113 | 0 | while (1) { | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | last unless _alive($pid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | last if Time::HiRes::time() > $maxtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | Time::HiRes::sleep(0.1); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (_alive($pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | # forcedly | ||||||||||||||||||||||||||||
| 120 | 0 | $option{callback}->("(pid $pid) is still running, killing it now..."); | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($option{kill}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | proc_terminate($pid, kill => $option{kill}); | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 0 | proc_terminate($pid); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | $option{callback}->("(pid $pid) has been successfully killed"); | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | $option{callback}->("does not seem to be running anymore"); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||
| 133 | # check a process | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub _status ($%) { | ||||||||||||||||||||||||||
|
Subroutine
_status: called CC=9 SCAR=26.0
| |||||||||||||||||||||||||||||
| 137 | 2 | my($path, %option) = @_; | |||||||||||||||||||||||||||
| 138 | 2 | my($fh, @stat, $data, $pid, $status, $message, $lsb); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | $status = 0; | |||||||||||||||||||||||||||
| 141 | 2 | ▶ | unless (sysopen($fh, $path, O_RDWR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | if ($! == ENOENT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1 | ($message, $lsb) = | |||||||||||||||||||||||||||
| 144 | ("does not seem to be running (no pid file)", 3); | ||||||||||||||||||||||||||||
| 145 | 1 | goto done; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | dief("cannot sysopen(%s, O_RDWR): %s", $path, $!); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 1 | ▶ | @stat = stat($fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | or dief("cannot stat(%s): %s", $path, $!); | ||||||||||||||||||||||||||||
| 151 | 1 | $data = _read($path, $fh); | |||||||||||||||||||||||||||
| 152 | 1 | ▶ | if ($data eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | # this can happen in pf_set(), between open() and lock() | ||||||||||||||||||||||||||||
| 154 | 0 | ($message, $lsb) = | |||||||||||||||||||||||||||
| 155 | ("does not seem to be running yet (empty pid file)", 4); | ||||||||||||||||||||||||||||
| 156 | 0 | goto done; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | if ($data =~ /^(\d+)(\s+([a-z]+))?\s*$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | $pid = $1; | |||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||
| 161 | 0 | dief("unexpected pid file contents in %s: %s", $path, $data); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | unless (_alive($pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ($message, $lsb) = | |||||||||||||||||||||||||||
| 165 | ("(pid $pid) does not seem to be running anymore", 1); | ||||||||||||||||||||||||||||
| 166 | 0 | goto done; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 1 | $data = localtime($stat[ST_MTIME]); | |||||||||||||||||||||||||||
| 169 | 1 | ▶ | if ($option{freshness} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$option{"freshness"} and $stat[9] < &Time::HiRes::time() - $option{"freshness"}
MC/DC0%0/2
$option{"freshness"} and $stat[9] < &Time::HiRes::time() - $option{"freshness"}
$option{"freshness"}
$stat[9] < &Time::HiRes::time() - $option{"freshness"}
Truth table33%1/3
A and B
A= $option{"freshness"}B= $stat[9] < &Time::HiRes::time() - $option{"freshness"}
| |||||||||||||||||||||||||||||
| 170 | $stat[ST_MTIME] < Time::HiRes::time() - $option{freshness}) { | ||||||||||||||||||||||||||||
| 171 | 0 | ($message, $lsb) = | |||||||||||||||||||||||||||
| 172 | ("(pid $pid) does not seem to be running anymore since $data", 4); | ||||||||||||||||||||||||||||
| 173 | 0 | goto done; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | # so far so good ;-) | ||||||||||||||||||||||||||||
| 176 | 1 | ($status, $message, $lsb) = (1, "(pid $pid) was active on $data", 0); | |||||||||||||||||||||||||||
| 177 | 2 | done: | |||||||||||||||||||||||||||
| 178 | return($status, $message, $lsb); | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||
| 182 | # set the pid file | ||||||||||||||||||||||||||||
| 183 | # | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | my %pf_set_options = ( | |||||||||||||||||||||||||||
| 186 | pid => { optional => 1, type => SCALAR, regex => $_IntegerRegexp }, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 2 | ▶ | sub pf_set ($@) { | ||||||||||||||||||||||||||
|
Subroutine
pf_set: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 2 | my($path, %option, $fh); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | $path = shift(@_); | |||||||||||||||||||||||||||
| 193 | 2 | ▶ | %option = validate(@_, \%pf_set_options) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 2 | ▶ | $option{pid} ||= $$; | ||||||||||||||||||||||||||
|
Condition33%1/3
$option{"pid"} ||= $$
MC/DC0%0/2
$option{"pid"} ||= $$
$option{"pid"}
$$
Truth table33%1/3
A ||= B
A= $option{"pid"}B= $$
| |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | sysopen($fh, $path, O_WRONLY|O_CREAT|O_EXCL) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | or dief("cannot sysopen(%s, O_WRONLY|O_CREAT|O_EXCL): %s", $path, $!); | ||||||||||||||||||||||||||||
| 197 | 1 | _write($path, $fh, "$option{pid}\n"); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # check the pid file | ||||||||||||||||||||||||||||
| 202 | # | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 2 | my %pf_check_options = ( | |||||||||||||||||||||||||||
| 205 | pid => { optional => 1, type => SCALAR, regex => $_IntegerRegexp }, | ||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | ▶ | sub pf_check ($@) { | ||||||||||||||||||||||||||
|
Subroutine
pf_check: called CC=7 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||
| 209 | 2 | my($path, %option, $fh, $data, $pid, $action); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 2 | $path = shift(@_); | |||||||||||||||||||||||||||
| 212 | 2 | ▶ | %option = validate(@_, \%pf_check_options) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 2 | ▶ | $option{pid} ||= $$; | ||||||||||||||||||||||||||
|
Condition33%1/3
$option{"pid"} ||= $$
MC/DC0%0/2
$option{"pid"} ||= $$
$option{"pid"}
$$
Truth table33%1/3
A ||= B
A= $option{"pid"}B= $$
| |||||||||||||||||||||||||||||
| 214 | 2 | ▶ | sysopen($fh, $path, O_RDWR) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | or dief("cannot sysopen(%s, O_RDWR): %s", $path, $!); | ||||||||||||||||||||||||||||
| 216 | 1 | $data = _read($path, $fh); | |||||||||||||||||||||||||||
| 217 | 1 | ▶ | if ($data =~ /^(\d+)\s*$/s) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 218 | 1 | ($pid, $action) = ($1, ""); | |||||||||||||||||||||||||||
| 219 | } elsif ($data =~ /^(\d+)\s+([a-z]+)\s*$/s) { | ||||||||||||||||||||||||||||
| 220 | 0 | ($pid, $action) = ($1, $2); | |||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||
| 222 | 0 | dief("unexpected pid file contents in %s: %s", $path, $data) | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | dief("pid file %s has been taken over by pid %d!", $path, $pid) | ||||||||||||||||||||||||||||
| 225 | 1 | ▶ | unless $pid == $option{pid}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 1 | return($action); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # | ||||||||||||||||||||||||||||
| 230 | # touch the pid file | ||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 2 | ▶ | sub pf_touch ($) { | ||||||||||||||||||||||||||
|
Subroutine
pf_touch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 2 | my($path) = @_; | |||||||||||||||||||||||||||
| 235 | 2 | my($now); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 2 | $now = time(); | |||||||||||||||||||||||||||
| 238 | 2 | ▶ | utime($now, $now, $path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | or dief("cannot utime(%d, %d, %s): %s", $now, $now, $path, $!); | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # | ||||||||||||||||||||||||||||
| 243 | # unset the pid file | ||||||||||||||||||||||||||||
| 244 | # | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 2 | ▶ | sub pf_unset ($) { | ||||||||||||||||||||||||||
|
Subroutine
pf_unset: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | 2 | my($path) = @_; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 2 | ▶ | unless (unlink($path)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 1 | ▶ | return if $! == ENOENT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | dief("cannot unlink(%s): %s", $path, $!); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | # | ||||||||||||||||||||||||||||
| 256 | # use the pid file to find out the program status | ||||||||||||||||||||||||||||
| 257 | # | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 2 | my %pf_status_options = ( | |||||||||||||||||||||||||||
| 260 | freshness => { optional => 1, type => SCALAR, regex => $_NumberRegexp }, | ||||||||||||||||||||||||||||
| 261 | timeout => { optional => 1, type => SCALAR, regex => $_NumberRegexp }, | ||||||||||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 2 | ▶ | sub pf_status ($@) { | ||||||||||||||||||||||||||
|
Subroutine
pf_status: called CC=6 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||
| 265 | 2 | my($path, %option, $maxtime, $status, $message, $lsb); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 2 | $path = shift(@_); | |||||||||||||||||||||||||||
| 268 | 2 | ▶ | %option = validate(@_, \%pf_status_options) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 2 | ▶ | if ($option{timeout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | # check multiple times until success or timeout | ||||||||||||||||||||||||||||
| 271 | 0 | $maxtime = Time::HiRes::time() + $option{timeout}; | |||||||||||||||||||||||||||
| 272 | 0 | while (1) { | |||||||||||||||||||||||||||
| 273 | 0 | ($status, $message, $lsb) = _status($path, %option); | |||||||||||||||||||||||||||
| 274 | 0 | ▶ | last if $status or Time::HiRes::time() > $maxtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$status or &Time::HiRes::time() > $maxtime
MC/DC0%0/2
$status or &Time::HiRes::time() > $maxtime
$status
&Time::HiRes::time() > $maxtime
Truth table0%0/3
A or B
A= $statusB= &Time::HiRes::time() > $maxtime
| |||||||||||||||||||||||||||||
| 275 | 0 | Time::HiRes::sleep(0.1); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | } else { | ||||||||||||||||||||||||||||
| 278 | # check only once | ||||||||||||||||||||||||||||
| 279 | 2 | ($status, $message, $lsb) = _status($path, %option); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 2 | ▶ | return($status, $message, $lsb) if wantarray(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 2 | return($status); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # | ||||||||||||||||||||||||||||
| 286 | # use the pid file to make the program quit | ||||||||||||||||||||||||||||
| 287 | # | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 2 | my %pf_quit_options = ( | |||||||||||||||||||||||||||
| 290 | callback => { optional => 1, type => CODEREF }, | ||||||||||||||||||||||||||||
| 291 | linger => { optional => 1, type => SCALAR, regex => $_NumberRegexp }, | ||||||||||||||||||||||||||||
| 292 | kill => { optional => 1, type => SCALAR }, | ||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 1 | ▶ | sub pf_quit ($@) { | ||||||||||||||||||||||||||
|
Subroutine
pf_quit: called CC=15 SCAR=47.4
Pod
documented
| |||||||||||||||||||||||||||||
| 296 | 1 | my($path, %option, $fh, $data, $pid); | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 1 | $path = shift(@_); | |||||||||||||||||||||||||||
| 299 | 1 | ▶ | %option = validate(@_, \%pf_quit_options) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $option{callback} ||= sub { printf("%s\n", $_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$option{"callback"} ||= sub {
printf "%s\n", $_[0];
}
MC/DC0%0/2
$option{"callback"} ||= sub {
printf "%s\n", $_[0];
}
$option{"callback"}
sub {
printf "%s\n", $_[0];
}
Truth table33%1/3
A ||= B
A= $option{"callback"}B= sub {
printf "%s\n", $_[0];
}
| |||||||||||||||||||||||||||||
| 301 | 1 | ▶ | $option{linger} ||= 5; | ||||||||||||||||||||||||||
|
Condition50%1/2
$option{"linger"} ||= 5
MC/DC0%0/1
$option{"linger"} ||= 5
$option{"linger"}
Truth table50%1/2
A
A= $option{"linger"}
| |||||||||||||||||||||||||||||
| 302 | 1 | ▶ | unless (sysopen($fh, $path, O_RDWR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 1 | ▶ | if ($! == ENOENT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 1 | $option{callback}->("does not seem to be running (no pid file)"); | |||||||||||||||||||||||||||
| 305 | 1 | return; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | 0 | dief("cannot sysopen(%s, O_RDWR): %s", $path, $!); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | 0 | $data = _read($path, $fh, 1); | |||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ($data eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | # this can happen while setting the pid file, between open and lock in pf_set() | ||||||||||||||||||||||||||||
| 312 | # but what can we do? we wait a bit, try again and complain if itis still empty | ||||||||||||||||||||||||||||
| 313 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 314 | 0 | $data = _read($path, $fh, 1); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ($data =~ /^(\d+)(\s+([a-z]+))?\s*$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | $pid = $1; | |||||||||||||||||||||||||||
| 318 | } else { | ||||||||||||||||||||||||||||
| 319 | 0 | dief("unexpected pid file contents in %s: %s", $path, $data); | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 0 | _kill($path, $fh, $pid, %option); | |||||||||||||||||||||||||||
| 322 | # in any case, we make sure that _this_ pid file does not exist anymore | ||||||||||||||||||||||||||||
| 323 | # we have to be extra careful to make sure it is the same pid file | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | unless (sysopen($fh, $path, O_RDWR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | return if $! == ENOENT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 0 | dief("cannot sysopen(%s, O_RDWR): %s", $path, $!); | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | 0 | $data = _read($path, $fh); | |||||||||||||||||||||||||||
| 329 | 0 | ▶ | return if $data eq ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if ($data =~ /^(\d+)(\s+([a-z]+))?\s*$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 0 | ▶ | return unless $1 == $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | } else { | ||||||||||||||||||||||||||||
| 333 | 0 | dief("unexpected pid file contents in %s: %s", $path, $data); | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | # same pid so assume same pid file... remove it | ||||||||||||||||||||||||||||
| 336 | 0 | $option{callback}->("removing stale pid file: $path"); | |||||||||||||||||||||||||||
| 337 | 0 | ▶ | unless (unlink($path)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | # take into account a potential race condition... | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | dief("cannot unlink(%s): %s", $path, $!) unless $! == ENOENT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # | ||||||||||||||||||||||||||||
| 344 | # sleep for some time, taking into account an optional pid file | ||||||||||||||||||||||||||||
| 345 | # | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 2 | my %pf_sleep_options = ( | |||||||||||||||||||||||||||
| 348 | time => { optional => 1, type => SCALAR, regex => $_NumberRegexp }, | ||||||||||||||||||||||||||||
| 349 | ); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub pf_sleep ($@) { | ||||||||||||||||||||||||||
|
Subroutine
pf_sleep: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 352 | 0 | my($path, %option, $end, $sleep); | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | $path = shift(@_); | |||||||||||||||||||||||||||
| 355 | 0 | ▶ | %option = validate(@_, \%pf_sleep_options) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 0 | ▶ | $option{time} = 1 unless defined($option{time}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if ($path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | $end = Time::HiRes::time() + $option{time} if $option{time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | while (1) { | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | return(0) if pf_check($path) eq "quit"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | pf_touch($path); | |||||||||||||||||||||||||||
| 362 | 0 | ▶ | last unless $option{time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 0 | $sleep = $end - Time::HiRes::time(); | |||||||||||||||||||||||||||
| 364 | 0 | ▶ | last if $sleep <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | $sleep = 1 if $sleep > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | Time::HiRes::sleep($sleep); | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | } else { | ||||||||||||||||||||||||||||
| 369 | 0 | ▶ | Time::HiRes::sleep($option{time}) if $option{time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | return(1); | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # | ||||||||||||||||||||||||||||
| 375 | # export control | ||||||||||||||||||||||||||||
| 376 | # | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 1 | ▶ | sub import : method { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 379 | 1 | my($pkg, %exported); | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 1 | $pkg = shift(@_); | |||||||||||||||||||||||||||
| 382 | 1 | grep($exported{$_}++, map("pf_$_", | |||||||||||||||||||||||||||
| 383 | qw(set check touch unset status quit sleep))); | ||||||||||||||||||||||||||||
| 384 | 1 | export_control(scalar(caller()), $pkg, \%exported, @_); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 2 | 1; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | __DATA__ | ||||||||||||||||||||||||||||