=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm 2005/03/24 19:51:50 1.4 +++ openbsd/OpenBSDTorrents/lib/OpenBSDTorrents.pm 2005/03/25 03:11:38 1.5 @@ -1,5 +1,5 @@ package OpenBSDTorrents; -#$Id: OpenBSDTorrents.pm,v 1.4 2005/03/24 19:51:50 andrew Exp $ +#$Id: OpenBSDTorrents.pm,v 1.5 2005/03/25 03:11:38 andrew Exp $ use 5.008005; use strict; use warnings; @@ -12,14 +12,17 @@ our @EXPORT = qw( $BaseDir + $HomeDir $TorrentDir $BaseName $Tracker &Name_Torrent &Get_Files_and_Dirs + &justme ); our $BaseDir = '/home/ftp/pub'; +our $HomeDir = '/home/andrew/OpenBSDTorrents'; our $TorrentDir = '/home/andrew/torrents'; our $BaseName = 'OpenBSD'; our $Tracker = 'http://OpenBSD.somedomain.net/announce.php'; @@ -79,6 +82,40 @@ } return join '-', ($year, $mon, $mday, $hour . $min); } + +# "There can be only one." --the Highlander +sub justme { + + my $myname; + + if ($0 =~ m#([^/]+$)#) { + $myname = $1; + } else { + die "Couldn't figure out myname"; + } + + my $SEMA = "$HomeDir/$myname.pid"; + if (open SEMA, "<", $SEMA) { + my $pid = ; + if (defined $pid) { + chomp $pid; + if ($pid =~ /^(\d+)$/) { + $pid = $1; + } else { + die "invalid pid read '$pid'"; + } + if (kill(0, $pid)) { + print "$0 already running (pid $pid), bailing out\n"; + exit 253; + } + } + close SEMA; + } + open (SEMA, ">", $SEMA) or die "can't write $SEMA: $!"; + print SEMA "$$\n"; + close(SEMA) or die "can't close $SEMA: $!"; +} + 1; __END__