| 1 | # | ||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||
| 3 | # | ||||||||||
| 4 | |||||||||||
| 5 | package Rex::Shared::Var::Common; | ||||||||||
| 6 | |||||||||||
| 7 | 63 | ▶ | use v5.14.4; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 63 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 63 | require Exporter; | |||||||||
| 11 | 63 | ▶ | use base qw/Exporter/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 63 | our @EXPORT_OK = qw/__lock __store __retrieve/; | |||||||||
| 13 | |||||||||||
| 14 | 63 | our $VERSION = '1.16.1'; # VERSION | |||||||||
| 15 | |||||||||||
| 16 | 63 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 63 | ▶ | use Storable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 63 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | # $PARENT_PID gets set when Rex starts. This value remains the same after the | ||||||||||
| 21 | # process forks. So $PARENT_PID is always the pid of the parent process. $$ | ||||||||||
| 22 | # however is always the pid of the current process. | ||||||||||
| 23 | 63 | our $PARENT_PID = $$; | |||||||||
| 24 | 63 | our $FILE = File::Spec->catfile( File::Spec->tmpdir(), "vars.db.$PARENT_PID" ); | |||||||||
| 25 | 63 | our $LOCK_FILE = | |||||||||
| 26 | File::Spec->catfile( File::Spec->tmpdir(), "vars.db.lock.$PARENT_PID" ); | ||||||||||
| 27 | |||||||||||
| 28 | 1120 | ▶ | sub __lock { | ||||||||
|
Subroutine
__lock: called CC=3 SCAR=11.3
| |||||||||||
| 29 | 1120 | ▶ | sysopen( my $dblock, $LOCK_FILE, O_RDWR | O_CREAT ) or die($!); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1120 | ▶ | flock( $dblock, LOCK_EX ) or die($!); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 1120 | my $ret = $_[0]->(); | |||||||||
| 33 | |||||||||||
| 34 | 1120 | close($dblock); | |||||||||
| 35 | |||||||||||
| 36 | 1120 | return $ret; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 292 | ▶ | sub __store { | ||||||||
|
Subroutine
__store: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 292 | my $ref = shift; | |||||||||
| 41 | 292 | store( $ref, $FILE ); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 1120 | ▶ | sub __retrieve { | ||||||||
|
Subroutine
__retrieve: called CC=2 SCAR=6.9
| |||||||||||
| 45 | 1120 | ▶ | return {} unless -f $FILE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1102 | return retrieve($FILE); | |||||||||
| 47 | |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 63 | ▶ | sub END { | ||||||||
|
Subroutine
END: called CC=4 SCAR=13.9
| |||||||||||
| 51 | |||||||||||
| 52 | # return if we exiting a child process | ||||||||||
| 53 | 63 | ▶ | return unless $$ eq $PARENT_PID; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | # we are exiting the master process | ||||||||||
| 56 | 28 | ▶ | unlink $FILE if -f $FILE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 28 | ▶ | unlink $LOCK_FILE if -f $LOCK_FILE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 63 | 1; | |||||||||