From 53aca1fdc3f7683efd90472ed7be78f793eb78bb Mon Sep 17 00:00:00 2001 From: Scott Ullrich Date: Fri, 22 Feb 2008 00:02:36 +0000 Subject: [PATCH] Add process name and id checking functions --- etc/inc/util.inc | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/etc/inc/util.inc b/etc/inc/util.inc index 33cb2b191d..c003c7f575 100644 --- a/etc/inc/util.inc +++ b/etc/inc/util.inc @@ -34,19 +34,44 @@ function killbypid($pidfile) { sigkillbypid($pidfile, "TERM"); } +function isvalidpid($pid) { + $running = `ps -p $pid | wc -l`; + if(intval($running) > 1) + return true; + else + return false; +} + +function isvalidproc($proc) { + $running = `ps awux | grep $proc | grep -v grep | wc -l`; + if(intval($running) > 1) + return true; + else + return false; +} + /* sigkill a process by pid file */ +/* return 1 for success and 0 for a failure */ function sigkillbypid($pidfile, $sig) { - if (file_exists($pidfile)) { + if (is_file($pidfile)) { $pid = trim(file_get_contents($pidfile)); - mwexec("/bin/kill -s $sig {$pid}"); - } else { - mwexec("/bin/kill -s $sig {$pidfile}"); + if(isvalidpid($pid)) + return mwexec("/bin/kill -s $sig {$pid}"); } + + return 0; +} + +/* kill a process by name */ +function sigkillbyname($procname, $sig) { + if(isvalidproc($procname)) + return mwexec("/usr/bin/killall -{$sig} " . escapeshellarg($procname)); } /* kill a process by name */ function killbyname($procname) { - mwexec("/usr/bin/killall " . escapeshellarg($procname)); + if(isvalidproc($procname)) + mwexec("/usr/bin/killall " . escapeshellarg($procname)); } /* return the subnet address given a host address and a subnet bit count */