function lx_core_lock($file = null) { global $argv; $prog = basename($argv[0]); // This is a hack.. If we can't get the arg, then that means we are in the cgi mode, and that means our process name is display.php. if (!$prog) { $prog = "display.php"; } lxfile_mkdir("../pid"); if (!$file) { $file = "{$prog}.pid"; } else { $file = basename($file); } $pidfile = "__path_program_root/pid/{$file}"; $pid = null; if (lxfile_exists($pidfile)) { $pid = lfile_get_contents($pidfile); } dprint($pid . "\n"); if (!$pid) { dprint("{$prog}:{$file} No pid file {$pidfile} detected..\n"); lfile_put_contents($pidfile, os_getpid()); return false; } $pid = trim($pid); $name = os_get_commandname($pid); if ($name) { $name = basename($name); } if (!$name || $name !== $prog) { if (!$name) { dprint("{$prog}:{$file} Stale Lock file {$pidfile} detected..., removing\n"); } else { dprint("{$prog}:{$file} Stale lock file... Another program {$name} is running on it..\n"); } lxfile_rm($pidfile); lfile_put_contents($pidfile, os_getpid()); return false; } return true; }
function save_pid($name) { lfile_put_contents("__path_program_root/pid/{$name}.pid", os_getpid()); }