--- pidproxy-old.py	2010-12-15 21:45:54.563840470 -0500
+++ pidproxy-new.py	2010-12-15 21:43:15.953824211 -0500
@@ -14,12 +14,15 @@
 ##############################################################################
 
 """ An executable which proxies for a subprocess; upon a signal, it sends that
-signal to the process identified by a pidfile. """
+signal to the process identified by a pidfile. Or if 'multi' is provided for
+the pidfile, then all child process are found dynamically and the signal is
+sent to each."""
 
 import os
 import sys
 import signal
 import time
+import re
 
 class PidProxy:
     pid = None
@@ -45,7 +48,7 @@
                 break
 
     def usage(self):
-        print "pidproxy.py <pidfile name> <command> [<cmdarg1> ...]"
+        print "pidproxy.py (<pidfile name>|multi) <command> [<cmdarg1> ...]"
 
     def setsignals(self):
         signal.signal(signal.SIGTERM, self.passtochild)
@@ -60,15 +63,36 @@
         pass
 
     def passtochild(self, sig, frame):
-        try:
-            pid = int(open(self.pidfile, 'r').read().strip())
-        except:
-            pid = None
-            print "Can't read child pidfile %s!" % self.pidfile
-            return
-        os.kill(pid, sig)
+        for pid in self.get_child_pids():
+            os.kill(pid, sig)  # signal/kill children
+        os.kill(self.pid, sig) # signal/kill parent
         if sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT]:
-            sys.exit(0)
+            sys.exit(0) # kill self (pidproxy)
+            
+    def get_child_pids(self):
+        pids = []
+        if self.pidfile == 'multi':
+            for pid in os.listdir("/proc/"):
+                if not re.match("\d+", pid):
+                    continue
+                try:
+                    f = open('/proc/%s/status' % pid)
+                    for line in f:
+                        if not line.startswith('PPid:'):
+                            continue
+                        ppid = line.split()[1].strip()
+                        if int(ppid) == self.pid:
+                            pids.append(int(pid))
+                        break
+                    f.close()
+                except IOError:
+                    pass # we can ignore race conditions
+        else:
+            try:
+                pids = [int(open(self.pidfile, 'r').read().strip())]
+            except:
+                print "Can't read child pidfile %s!" % self.pidfile
+        return pids
 
 def main():
     pp = PidProxy(sys.argv)
@@ -79,4 +103,3 @@
     
     
     
-
