From 5b2f18bf8fdb76d77c0b7a9c0d54978a48b496a2 Mon Sep 17 00:00:00 2001
From: Jonas Wallden <jonasw@roxen.com>
Date: Wed, 26 Jan 2011 17:29:10 +0100
Subject: [PATCH] Support optional watchdog script for external filter binaries
 that don't terminate in reasonable time.

---
 lib/modules/Search.pmod/Filter.pmod/Base.pike | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/modules/Search.pmod/Filter.pmod/Base.pike b/lib/modules/Search.pmod/Filter.pmod/Base.pike
index 70da1cdb2d..fb255b379c 100644
--- a/lib/modules/Search.pmod/Filter.pmod/Base.pike
+++ b/lib/modules/Search.pmod/Filter.pmod/Base.pike
@@ -8,7 +8,9 @@ constant contenttypes = ({ });
 .Output filter(Standards.URI uri, string|Stdio.File data,
 	      string content_type, mixed ... more);
 
-string my_popen(array(string) args, string|void cwd, int|void wait_for_exit)
+
+string my_popen(array(string) args, string|void cwd, int|void wait_for_exit,
+		array(string)|void watchdog_args)
   // A smarter version of Process.popen: No need to quote arguments.
 {    
   Stdio.File pipe0 = Stdio.File();
@@ -20,11 +22,25 @@ string my_popen(array(string) args, string|void cwd, int|void wait_for_exit)
     setup["cwd"] = cwd;
   Process.create_process proc = Process.create_process(args, setup);
   pipe1->close();
+
+  //  Launch watchdog if provided (requires wait_for_exit)
+  Process.Process watchdog;
+  if (wait_for_exit && watchdog_args) {
+    //  Insert pid of running process where caller used "%p"
+    array(string) wd_args =
+      replace(watchdog_args + ({ }), "%p", (string) proc->pid());
+    watchdog = Process.spawn_pike(wd_args);
+  }
+  
   string result = pipe0->read();
   if(!result)
     error("my_popen failed with error "+pipe0->errno()+".\n");
   pipe0->close();
   if (wait_for_exit)
     proc->wait();
+  
+  if (watchdog)
+    watchdog->kill(9);
+  
   return result;
 }
-- 
GitLab