View Single Post
  #4  
Old 8th May 2009, 22:53
glohmann glohmann is offline
Junior Member
 
Join Date: May 2009
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ok here is another approach looking for the lock and pid file:

<?php

$ret = _checkDaemon2("/var/lock/subsys/httpd", "/var/run/httpd.pid");
printf("\n check on daemon returned '%s' \n\n", (($ret==true)?"true":"false") );

/**
* check if the named daemon is up and running
* daemon_lock: path of daemon lock file (see /var/lock/subsys/)
* daemon_pid : path of daemon pid file (see /var/run/)
*/
function _checkDaemon2 ($daemon_lock, $daemon_pid) {
$retval = file_exists($daemon_lock);
if ($retval == true) {
// subsystem is locked, but maybe daemon crashed, check pid is alive
$pid = file_get_contents($daemon_pid, 0, null, -1, 20); // it is unlikely a pid is longer then 20 characters
if($pid != false) {
$pid = intval($pid); // as we call 'exec' we want to be sure this is an integer!
// we found a pid file, now chek if process is alive
exec("ps -p $pid", $retarr, $retval);
if(count($retarr)>1) {
// we have at least two lines looking like so daemon is alive
// [0] => PID TTY TIME CMD
// [1] => 15375 ? 00:00:01 httpd
return true;
}
}
}
return false;
}

?>


We again use exec but this time the only modifiable parameter is the pid which we check before is an integer so no chance to exploit that. All other methods only doing reading attempts to the file system.
Reply With Quote