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