[PATCH] Perlbal writes pid to file

Brett G. Durrett brett at imvu.com
Wed Sep 12 22:13:04 UTC 2007


Attached is a patch for 1.59 that will allow you to specify a pidfile in 
the configuration... it has been lightly tested with both console and 
daemon mode and, well, the pid seems to match the running process in 
each case.

If no 'pidfile' option is provided perlbal functionality does not 
change.  If you want a pidfile, specify an option like so:

server pidfile = /var/run/perlbal.pid







-------------- next part --------------
Index: doc/config-guide.txt
===================================================================
--- doc/config-guide.txt	(revision 697)
+++ doc/config-guide.txt	(working copy)
@@ -52,6 +52,7 @@
                              For purely reverse proxy or only reproxying URLs, none is fine.
    aio_threads             : number of child threads doing disk IO.  (use between 2-50)
    track_obj               : developer option to track objects
+   pidfile                 : filename to write pidfile (no pidfile if not specified)
 
 
 Diagnostic commands:
Index: lib/Perlbal.pm
===================================================================
--- lib/Perlbal.pm	(revision 697)
+++ lib/Perlbal.pm	(working copy)
@@ -94,6 +94,8 @@
 our $track_obj = 0;  # default to not track creation locations
 our $reqs = 0; # total number of requests we've done
 our $starttime = time(); # time we started
+our $pidfile = '';  # full path, default to not writing pidfile
+our $run_started = 0;  # used by pidfile (only makes sense before run started) 
 our ($lastutime, $laststime, $lastreqs) = (0, 0, 0); # for deltas
 
 our %PluginCase = ();   # lowercase plugin name -> as file is named
@@ -893,6 +895,13 @@
         return $mc->ok;
     }
 
+    if ($key eq "pidfile") {
+        return $mc->err("pidfile must be configured at startup, before Perlbal::run is called") if  $run_started;
+        return $mc->err("Expected full pathname to pidfile") unless $val;
+        $pidfile = $val;
+        return $mc->ok;
+    }
+
     return $mc->err("unknown server option '$val'");
 }
 
@@ -1142,9 +1151,13 @@
 }
 
 sub run {
+    $run_started = 1;
+
     # setup for logging
     Sys::Syslog::openlog('perlbal', 'pid', 'daemon') if $Perlbal::SYSLOG_AVAILABLE;
     Perlbal::log('info', 'beginning run');
+    my $pidfile_written = 0;
+    $pidfile_written = _write_pidfile( $pidfile ) if $pidfile;
 
     # number of AIO threads.  the number of outstanding requests isn't
     # affected by this
@@ -1181,6 +1194,9 @@
         Perlbal::log('crit', "crash log: $_") foreach split(/\r?\n/, $@);
         $clean_exit = 0;
     }
+
+    _remove_pidfile( $pidfile ) if $pidfile_written;
+
     Perlbal::log('info', 'ending run');
     Sys::Syslog::closelog() if $Perlbal::SYSLOG_AVAILABLE;
 
@@ -1199,6 +1215,32 @@
     }
 }
 
+
+sub _write_pidfile {
+    my $file = shift;
+
+    my $fh;
+    unless (open($fh, ">$file")) {
+        Perlbal::log('info', "couldn't create pidfile '$file': $!" );
+        return 0;
+    }
+    unless ((print $fh "$$\n") && close($fh)) {
+        Perlbal::log('info', "couldn't write into pidfile '$file': $!" );
+        remove_pidfile($file);
+        return 0;
+    }
+    return 1;
+}
+
+
+sub _remove_pidfile {
+    my $file = shift;
+    
+    unlink $file;
+    return 1;
+}
+
+
 # Local Variables:
 # mode: perl
 # c-basic-indent: 4


More information about the perlbal mailing list