Prevent the running of multiple xmpp auth daemons at a time
This commit is contained in:
parent
0ef2425a3e
commit
8db5b121ff
3 changed files with 385 additions and 323 deletions
|
@ -19,6 +19,7 @@ Example: To set the directory value please add this line to your .htconfig.php:
|
||||||
|
|
||||||
## jabber ##
|
## jabber ##
|
||||||
* **debug** (Boolean) - Enable debug level for the jabber account synchronisation.
|
* **debug** (Boolean) - Enable debug level for the jabber account synchronisation.
|
||||||
|
* **lockpath** - Must be writable by the ejabberd process. if set then it will prevent the running of multiple processes.
|
||||||
|
|
||||||
## system ##
|
## system ##
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,8 @@ require_once 'include/dba.php';
|
||||||
class ExAuth
|
class ExAuth
|
||||||
{
|
{
|
||||||
private $bDebug;
|
private $bDebug;
|
||||||
|
private $host;
|
||||||
|
private $pidfile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create the class
|
* @brief Create the class
|
||||||
|
@ -133,6 +135,10 @@ class ExAuth
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We only allow one process per hostname. So we set a lock file
|
||||||
|
// Problem: We get the firstname after the first auth - not before
|
||||||
|
$this->setHost($aCommand[2]);
|
||||||
|
|
||||||
// Now we check if the given user is valid
|
// Now we check if the given user is valid
|
||||||
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
|
|
||||||
|
@ -209,6 +215,10 @@ class ExAuth
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We only allow one process per hostname. So we set a lock file
|
||||||
|
// Problem: We get the firstname after the first auth - not before
|
||||||
|
$this->setHost($aCommand[2]);
|
||||||
|
|
||||||
// We now check if the password match
|
// We now check if the password match
|
||||||
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
|
|
||||||
|
@ -281,6 +291,43 @@ class ExAuth
|
||||||
return $http_code == 200;
|
return $http_code == 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the hostname for this process
|
||||||
|
*
|
||||||
|
* @param string $host The hostname
|
||||||
|
*/
|
||||||
|
private function setHost($host)
|
||||||
|
{
|
||||||
|
if (!empty($this->host)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writeLog(LOG_INFO, 'Hostname for process ' . getmypid() . ' is ' . $host);
|
||||||
|
|
||||||
|
$this->host = $host;
|
||||||
|
|
||||||
|
$lockpath = Config::get('jabber', 'lockpath');
|
||||||
|
if (is_null($lockpath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->pidfile = new Pidfile($lockpath, $host);
|
||||||
|
if ($this->pidfile->isRunning()) {
|
||||||
|
$oldpid = $this->pidfile->pid();
|
||||||
|
$this->writeLog(LOG_INFO, 'Process ' . $oldpid . ' was running for ' . $this->pidfile->runningTime() . ' seconds and will now be killed');
|
||||||
|
$this->pidfile->kill();
|
||||||
|
|
||||||
|
// Wait until the other process is hopefully killed
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
$this->pidfile = new Pidfile($lockpath, $host);
|
||||||
|
if ($oldpid == $this->pidfile->pid()) {
|
||||||
|
$this->writeLog(LOG_ERR, 'Process ' . $oldpid . "wasn't killed in time. We now quit our process.");
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief write data to the syslog
|
* @brief write data to the syslog
|
||||||
*
|
*
|
||||||
|
|
|
@ -11,6 +11,7 @@ class Pidfile
|
||||||
{
|
{
|
||||||
private $file;
|
private $file;
|
||||||
private $running;
|
private $running;
|
||||||
|
private $pid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $dir path
|
* @param string $dir path
|
||||||
|
@ -19,18 +20,19 @@ class Pidfile
|
||||||
*/
|
*/
|
||||||
public function __construct($dir, $name)
|
public function __construct($dir, $name)
|
||||||
{
|
{
|
||||||
$this->_file = "$dir/$name.pid";
|
$this->file = "$dir/$name";
|
||||||
|
$this->running = false;
|
||||||
|
|
||||||
if (file_exists($this->_file)) {
|
if (file_exists($this->file)) {
|
||||||
$pid = trim(@file_get_contents($this->file));
|
$this->pid = trim(@file_get_contents($this->file));
|
||||||
if (($pid != "") && posix_kill($pid, 0)) {
|
if (($this->pid != "") && posix_kill($this->pid, 0)) {
|
||||||
$this->running = true;
|
$this->running = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->running) {
|
if (!$this->running) {
|
||||||
$pid = getmypid();
|
$this->pid = getmypid();
|
||||||
file_put_contents($this->file, $pid);
|
file_put_contents($this->file, $this->pid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,34 +41,46 @@ class Pidfile
|
||||||
*/
|
*/
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
if ((! $this->running) && file_exists($this->file)) {
|
if (!$this->running && file_exists($this->file)) {
|
||||||
@unlink($this->file);
|
@unlink($this->file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* @brief Check if a process with this pid file is already running
|
||||||
|
* @return boolean Is it running?
|
||||||
*/
|
*/
|
||||||
public static function isRunning()
|
public function isRunning()
|
||||||
{
|
{
|
||||||
return self::$running;
|
return $this->running;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return object
|
* @brief Return the pid of the process
|
||||||
|
* @return boolean process id
|
||||||
*/
|
*/
|
||||||
public static function runningTime()
|
public function pid()
|
||||||
{
|
{
|
||||||
return time() - @filectime(self::$file);
|
return $this->pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @brief Returns the seconds that the old process was running
|
||||||
|
* @return integer run time of the old process
|
||||||
|
*/
|
||||||
|
public function runningTime()
|
||||||
|
{
|
||||||
|
return time() - @filectime($this->file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Kills the old process
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public static function kill()
|
public function kill()
|
||||||
{
|
{
|
||||||
if (file_exists(self::$file)) {
|
if (!empty($this->pid)) {
|
||||||
return posix_kill(file_get_contents(self::$file), SIGTERM);
|
return posix_kill($this->pid, SIGTERM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue