From f70ce21b6dbb1c714e92360742530c1b8aa42950 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Wed, 17 Sep 2008 09:01:08 -0700 Subject: [PATCH] * Split out a new checkpid function * If getbuglocation in getbugcomponent doesn't return a correct location, return undef --- Debbugs/Common.pm | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/Debbugs/Common.pm b/Debbugs/Common.pm index a4d7cf1..f1b6a14 100644 --- a/Debbugs/Common.pm +++ b/Debbugs/Common.pm @@ -43,7 +43,7 @@ BEGIN{ qw(getmaintainers_reverse), qw(getpseudodesc), ], - misc => [qw(make_list globify_scalar english_join)], + misc => [qw(make_list globify_scalar english_join checkpid)], date => [qw(secs_to_english)], quit => [qw(quit)], lock => [qw(filelock unfilelock lockpid)], @@ -92,6 +92,8 @@ sub getbugcomponent { # getbuglocation() directly first. return undef if defined $location and ($location ne 'db' and $location ne 'db-h'); + # if there is no location, the bug doesn't exist + return undef if not defined $location; } my $dir = getlocationpath($location); return undef if not defined $dir; @@ -417,15 +419,9 @@ Returns 1 on success, false on failure; dies on unusual errors. sub lockpid { my ($pidfile) = @_; if (-e $pidfile) { - my $pidfh = IO::File->new($pidfile, 'r') or - die "Unable to open pidfile $pidfile: $!"; - local $/; - my $pid = <$pidfh>; - ($pid) = $pid =~ /(\d+)/; - if (defined $pid and kill(0,$pid)) { - return 0; - } - close $pidfh; + my $pid = checkpid($pidfile); + die "Unable to read pidfile $pidfile: $!" if not defined $pid; + return 0 if $pid != 0; unlink $pidfile or die "Unable to unlink stale pidfile $pidfile $!"; } @@ -436,6 +432,35 @@ sub lockpid { return 1; } +=head2 checkpid + + checkpid('/path/to/pidfile'); + +Checks a pid file and determines if the process listed in the pidfile +is still running. Returns the pid if it is, 0 if it isn't running, and +undef if the pidfile doesn't exist or cannot be read. + +=cut + +sub checkpid{ + my ($pidfile) = @_; + if (-e $pidfile) { + my $pidfh = IO::File->new($pidfile, 'r') or + return undef; + local $/; + my $pid = <$pidfh>; + close $pidfh; + ($pid) = $pid =~ /(\d+)/; + if (defined $pid and kill(0,$pid)) { + return $pid; + } + return 0; + } + else { + return undef; + } +} + =head1 QUIT -- 2.39.2