]> git.donarmstrong.com Git - debbugs.git/commitdiff
* Split out a new checkpid function
authorDon Armstrong <don@donarmstrong.com>
Wed, 17 Sep 2008 16:01:08 +0000 (09:01 -0700)
committerDon Armstrong <don@donarmstrong.com>
Wed, 17 Sep 2008 16:01:08 +0000 (09:01 -0700)
 * If getbuglocation in getbugcomponent doesn't return a correct
   location, return undef

Debbugs/Common.pm

index a4d7cf109136c99f370b40fd9b3b1515e38c02c4..f1b6a144d8977c6cb234008bf7e2a373d51ddde1 100644 (file)
@@ -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