| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package IPC::Pidfile; | ||||||||||||||||||||||||||||
| 4 | 1 | $IPC::Pidfile::VERSION = '0.02'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: run only one instance of a program at a time | ||||||||||||||||||||||||||||
| 6 | 1 | our $DEBUG = $ENV{IPC_PIDFILE_DEBUG}; | |||||||||||||||||||||||||||
| 7 | 1 | our $PID = pid(); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # signal handler | ||||||||||||||||||||||||||||
| 10 | 0 | ▶ | $SIG{TERM} = $SIG{INT} = sub { exit(0) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | pidfile_lock(); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | END { pidfile_clear() } | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | sub pidfile_name { "$0.pid" } | ||||||||||||||||||||||||||
|
Subroutine
pidfile_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub pidfile_lock | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pidfile_lock: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | print "$PID pidfile_lock\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | die "$0 is already running\n" if (-e pidfile_name()) && pidfile_is_fresh(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e pidfile_name() and pidfile_is_fresh()
MC/DC0%0/2
-e pidfile_name() and pidfile_is_fresh()
-e pidfile_name()
pidfile_is_fresh()
Truth table33%1/3
A and B
A= -e pidfile_name()B= pidfile_is_fresh()
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | open my $pidfile, '>', pidfile_name() or die 'Unable to open pidfile ' . pidfile_name(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 1 | select((select($pidfile), $| = 1)[0]); | |||||||||||||||||||||||||||
| 25 | 1 | print $pidfile pid(); | |||||||||||||||||||||||||||
| 26 | 1 | close $pidfile; | |||||||||||||||||||||||||||
| 27 | 1 | return 1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub pidfile_is_fresh | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pidfile_is_fresh: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | print "$PID pidfile_is_stale\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | # kill isnt portable | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | if ($^O ne 'MSWin32') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 1 | my $pid = pidfile_read(); | |||||||||||||||||||||||||||
| 37 | 1 | return kill 0, $pid; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | return 0; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub pid | ||||||||||||||||||||||||||||
| 43 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 4 | my $pid = $$; | |||||||||||||||||||||||||||
| 45 | # Win32 pids have a trailing newline?! | ||||||||||||||||||||||||||||
| 46 | 4 | chomp $pid; | |||||||||||||||||||||||||||
| 47 | 4 | return $pid; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub pidfile_read | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pidfile_read: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | print "$PID pidfile_read\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | open my $pidfile, '<', pidfile_name() or die 'Unable to open pidfile ' . pidfile_name(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 3 | my $pid = do { local($/);<$pidfile> }; | |||||||||||||||||||||||||||
| 55 | 3 | close $pidfile; | |||||||||||||||||||||||||||
| 56 | 3 | chomp $pid; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # avoid negative pid numbers and garbage | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | die "pidfile appears corrupted: $pid\n" unless $pid =~ /^[0-9]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | return $pid; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub pidfile_clear | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pidfile_clear: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | print "$PID pidfile_clear\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if (pid() eq pidfile_read()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | unlink pidfile_name() or die "Unable to delete pidfile " . pidfile_name(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 1 | 1; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||