package pidfile; use 5.006; use strict; use warnings; our $VERSION = '0.0.1'; use Carp; use IO::File; use Fcntl qw(:flock); use File::Basename; use File::Spec; our %PF_Default = ( path => '/tmp', name => basename($0), mode => 0644, ); # Preloaded methods go here. sub new { my ( $class, %args ) = @_; my %pf = ( %PF_Default, %args ); croak "path => $pf{path} is not a directory" unless -d $pf{path}; croak "path => $pf{path} is not readable" unless -r _; croak "path => $pf{path} is not writeable" unless -w _; croak "name must be set" unless defined $pf{name} and length $pf{name}; return bless \%pf, $class; } sub pidfile { my $s = shift; return $s->{file} ||= File::Spec->catfile($s->{path}, $s->{name}.".pid"); } sub activate { my $s = shift; croak sprintf "%s contains a living pid %d\n", $s->pidfile, $s->getpid if ( $s->is_alive ); $s->setpid; } sub setpid { my $s = shift; my $f = IO::File->new( $s->pidfile, O_RDWR | O_CREAT, $PF_Default{mode} ); croak sprintf "can't open %s for writing: $!\n", $s->pidfile unless defined $f; croak sprintf "can't lock_ex %s: $!\n", $s->pidfile unless flock $f, LOCK_EX; $f->setpos(0); $f->truncate(0); $f->print( "$$", $/ ); $f->close; return 1; } sub getpid { my $s = shift; my $f = IO::File->new( $s->pidfile, O_RDONLY ); croak sprintf "can't open %s for reading: $!\n", $s->pidfile unless defined $f; croak sprintf "can't lock_sh %s: $!\n", $s->pidfile unless flock $f, LOCK_SH; my $l = $f->getline; $f->close; chomp $l; croak sprintf "%s contained non-numeric pid\n", $s->pidfile unless $l == $l + 0; return $l+0; } sub is_alive { my $s = shift; return 0 unless -f $s->pidfile; return kill 0, $s->getpid; } sub is_self { my $s = shift; return 0 unless -f $s->pidfile; return $$ == $s->getpid; } sub DESTROY { my $s = shift; unlink $s->pidfile if $s->is_self; } 1; __END__