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 ##
|
||||||
|
|
||||||
|
|
|
@ -1,306 +1,353 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ejabberd extauth script for the integration with friendica
|
* ejabberd extauth script for the integration with friendica
|
||||||
*
|
*
|
||||||
* Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
|
* Originally written for joomla by Dalibor Karlovic <dado@krizevci.info>
|
||||||
* modified for Friendica by Michael Vogel <icarus@dabo.de>
|
* modified for Friendica by Michael Vogel <icarus@dabo.de>
|
||||||
* published under GPL
|
* published under GPL
|
||||||
*
|
*
|
||||||
* Latest version of the original script for joomla is available at:
|
* Latest version of the original script for joomla is available at:
|
||||||
* http://87.230.15.86/~dado/ejabberd/joomla-login
|
* http://87.230.15.86/~dado/ejabberd/joomla-login
|
||||||
*
|
*
|
||||||
* Installation:
|
* Installation:
|
||||||
*
|
*
|
||||||
* - Change it's owner to whichever user is running the server, ie. ejabberd
|
* - Change it's owner to whichever user is running the server, ie. ejabberd
|
||||||
* $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
|
* $ chown ejabberd:ejabberd /path/to/friendica/scripts/auth_ejabberd.php
|
||||||
*
|
*
|
||||||
* - Change the access mode so it is readable only to the user ejabberd and has exec
|
* - Change the access mode so it is readable only to the user ejabberd and has exec
|
||||||
* $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
|
* $ chmod 700 /path/to/friendica/scripts/auth_ejabberd.php
|
||||||
*
|
*
|
||||||
* - Edit your ejabberd.cfg file, comment out your auth_method and add:
|
* - Edit your ejabberd.cfg file, comment out your auth_method and add:
|
||||||
* {auth_method, external}.
|
* {auth_method, external}.
|
||||||
* {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
|
* {extauth_program, "/path/to/friendica/script/auth_ejabberd.php"}.
|
||||||
*
|
*
|
||||||
* - Restart your ejabberd service, you should be able to login with your friendica auth info
|
* - Restart your ejabberd service, you should be able to login with your friendica auth info
|
||||||
*
|
*
|
||||||
* Other hints:
|
* Other hints:
|
||||||
* - if your users have a space or a @ in their nickname, they'll run into trouble
|
* - if your users have a space or a @ in their nickname, they'll run into trouble
|
||||||
* registering with any client so they should be instructed to replace these chars
|
* registering with any client so they should be instructed to replace these chars
|
||||||
* " " (space) is replaced with "%20"
|
* " " (space) is replaced with "%20"
|
||||||
* "@" is replaced with "(a)"
|
* "@" is replaced with "(a)"
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Friendica\Util;
|
namespace Friendica\Util;
|
||||||
|
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
use Friendica\Core\PConfig;
|
use Friendica\Core\PConfig;
|
||||||
use Friendica\Database\DBM;
|
use Friendica\Database\DBM;
|
||||||
use Friendica\Model\User;
|
use Friendica\Model\User;
|
||||||
use dba;
|
use dba;
|
||||||
|
|
||||||
require_once 'include/dba.php';
|
require_once 'include/dba.php';
|
||||||
|
|
||||||
class ExAuth
|
class ExAuth
|
||||||
{
|
{
|
||||||
private $bDebug;
|
private $bDebug;
|
||||||
|
private $host;
|
||||||
/**
|
private $pidfile;
|
||||||
* @brief Create the class
|
|
||||||
*
|
/**
|
||||||
* @param boolean $bDebug Debug mode
|
* @brief Create the class
|
||||||
*/
|
*
|
||||||
public function __construct()
|
* @param boolean $bDebug Debug mode
|
||||||
{
|
*/
|
||||||
$this->bDebug = (int) Config::get('jabber', 'debug');
|
public function __construct()
|
||||||
|
{
|
||||||
openlog('auth_ejabberd', LOG_PID, LOG_USER);
|
$this->bDebug = (int) Config::get('jabber', 'debug');
|
||||||
|
|
||||||
$this->writeLog(LOG_NOTICE, 'start');
|
openlog('auth_ejabberd', LOG_PID, LOG_USER);
|
||||||
}
|
|
||||||
|
$this->writeLog(LOG_NOTICE, 'start');
|
||||||
/**
|
}
|
||||||
* @brief Standard input reading function, executes the auth with the provided
|
|
||||||
* parameters
|
/**
|
||||||
*
|
* @brief Standard input reading function, executes the auth with the provided
|
||||||
* @return null
|
* parameters
|
||||||
*/
|
*
|
||||||
public function readStdin()
|
* @return null
|
||||||
{
|
*/
|
||||||
while (!feof(STDIN)) {
|
public function readStdin()
|
||||||
// Quit if the database connection went down
|
{
|
||||||
if (!dba::connected()) {
|
while (!feof(STDIN)) {
|
||||||
$this->writeLog(LOG_ERR, 'the database connection went down');
|
// Quit if the database connection went down
|
||||||
return;
|
if (!dba::connected()) {
|
||||||
}
|
$this->writeLog(LOG_ERR, 'the database connection went down');
|
||||||
|
return;
|
||||||
$iHeader = fgets(STDIN, 3);
|
}
|
||||||
$aLength = unpack('n', $iHeader);
|
|
||||||
$iLength = $aLength['1'];
|
$iHeader = fgets(STDIN, 3);
|
||||||
|
$aLength = unpack('n', $iHeader);
|
||||||
// No data? Then quit
|
$iLength = $aLength['1'];
|
||||||
if ($iLength == 0) {
|
|
||||||
$this->writeLog(LOG_ERR, 'we got no data, quitting');
|
// No data? Then quit
|
||||||
return;
|
if ($iLength == 0) {
|
||||||
}
|
$this->writeLog(LOG_ERR, 'we got no data, quitting');
|
||||||
|
return;
|
||||||
// Fetching the data
|
}
|
||||||
$sData = fgets(STDIN, $iLength + 1);
|
|
||||||
$this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
|
// Fetching the data
|
||||||
$aCommand = explode(':', $sData);
|
$sData = fgets(STDIN, $iLength + 1);
|
||||||
if (is_array($aCommand)) {
|
$this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
|
||||||
switch ($aCommand[0]) {
|
$aCommand = explode(':', $sData);
|
||||||
case 'isuser':
|
if (is_array($aCommand)) {
|
||||||
// Check the existance of a given username
|
switch ($aCommand[0]) {
|
||||||
$this->isUser($aCommand);
|
case 'isuser':
|
||||||
break;
|
// Check the existance of a given username
|
||||||
case 'auth':
|
$this->isUser($aCommand);
|
||||||
// Check if the givven password is correct
|
break;
|
||||||
$this->auth($aCommand);
|
case 'auth':
|
||||||
break;
|
// Check if the givven password is correct
|
||||||
case 'setpass':
|
$this->auth($aCommand);
|
||||||
// We don't accept the setting of passwords here
|
break;
|
||||||
$this->writeLog(LOG_NOTICE, 'setpass command disabled');
|
case 'setpass':
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
// We don't accept the setting of passwords here
|
||||||
break;
|
$this->writeLog(LOG_NOTICE, 'setpass command disabled');
|
||||||
default:
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
// We don't know the given command
|
break;
|
||||||
$this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
|
default:
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
// We don't know the given command
|
||||||
break;
|
$this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
} else {
|
break;
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
|
}
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
} else {
|
||||||
}
|
$this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* @brief Check if the given username exists
|
|
||||||
*
|
/**
|
||||||
* @param array $aCommand The command array
|
* @brief Check if the given username exists
|
||||||
*/
|
*
|
||||||
private function isUser(array $aCommand)
|
* @param array $aCommand The command array
|
||||||
{
|
*/
|
||||||
$a = get_app();
|
private function isUser(array $aCommand)
|
||||||
|
{
|
||||||
// Check if there is a username
|
$a = get_app();
|
||||||
if (!isset($aCommand[1])) {
|
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
|
// Check if there is a username
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
if (!isset($aCommand[1])) {
|
||||||
return;
|
$this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
|
return;
|
||||||
// Now we check if the given user is valid
|
}
|
||||||
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
|
||||||
|
// We only allow one process per hostname. So we set a lock file
|
||||||
// Does the hostname match? So we try directly
|
// Problem: We get the firstname after the first auth - not before
|
||||||
if ($a->get_hostname() == $aCommand[2]) {
|
$this->setHost($aCommand[2]);
|
||||||
$this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
|
|
||||||
$found = dba::exists('user', ['nickname' => $sUser]);
|
// Now we check if the given user is valid
|
||||||
} else {
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
$found = false;
|
|
||||||
}
|
// Does the hostname match? So we try directly
|
||||||
|
if ($a->get_hostname() == $aCommand[2]) {
|
||||||
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
$this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
|
||||||
if (!$found) {
|
$found = dba::exists('user', ['nickname' => $sUser]);
|
||||||
$found = $this->checkUser($aCommand[2], $aCommand[1], true);
|
} else {
|
||||||
}
|
$found = false;
|
||||||
|
}
|
||||||
if ($found) {
|
|
||||||
// The user is okay
|
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
||||||
$this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
|
if (!$found) {
|
||||||
fwrite(STDOUT, pack('nn', 2, 1));
|
$found = $this->checkUser($aCommand[2], $aCommand[1], true);
|
||||||
} else {
|
}
|
||||||
// The user isn't okay
|
|
||||||
$this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
|
if ($found) {
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
// The user is okay
|
||||||
}
|
$this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 1));
|
||||||
|
} else {
|
||||||
/**
|
// The user isn't okay
|
||||||
* @brief Check remote user existance via HTTP(S)
|
$this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
|
||||||
*
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
* @param string $host The hostname
|
}
|
||||||
* @param string $user Username
|
}
|
||||||
* @param boolean $ssl Should the check be done via SSL?
|
|
||||||
*
|
/**
|
||||||
* @return boolean Was the user found?
|
* @brief Check remote user existance via HTTP(S)
|
||||||
*/
|
*
|
||||||
private function checkUser($host, $user, $ssl)
|
* @param string $host The hostname
|
||||||
{
|
* @param string $user Username
|
||||||
$this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
|
* @param boolean $ssl Should the check be done via SSL?
|
||||||
|
*
|
||||||
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
|
* @return boolean Was the user found?
|
||||||
|
*/
|
||||||
$data = z_fetch_url($url);
|
private function checkUser($host, $user, $ssl)
|
||||||
|
{
|
||||||
if (!is_array($data)) {
|
$this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
|
||||||
return false;
|
|
||||||
}
|
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
|
||||||
|
|
||||||
if ($data['return_code'] != '200') {
|
$data = z_fetch_url($url);
|
||||||
return false;
|
|
||||||
}
|
if (!is_array($data)) {
|
||||||
|
return false;
|
||||||
$json = @json_decode($data['body']);
|
}
|
||||||
if (!is_object($json)) {
|
|
||||||
return false;
|
if ($data['return_code'] != '200') {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
return $json->nick == $user;
|
|
||||||
}
|
$json = @json_decode($data['body']);
|
||||||
|
if (!is_object($json)) {
|
||||||
/**
|
return false;
|
||||||
* @brief Authenticate the given user and password
|
}
|
||||||
*
|
|
||||||
* @param array $aCommand The command array
|
return $json->nick == $user;
|
||||||
*/
|
}
|
||||||
private function auth(array $aCommand)
|
|
||||||
{
|
/**
|
||||||
$a = get_app();
|
* @brief Authenticate the given user and password
|
||||||
|
*
|
||||||
// check user authentication
|
* @param array $aCommand The command array
|
||||||
if (sizeof($aCommand) != 4) {
|
*/
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
|
private function auth(array $aCommand)
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
{
|
||||||
return;
|
$a = get_app();
|
||||||
}
|
|
||||||
|
// check user authentication
|
||||||
// We now check if the password match
|
if (sizeof($aCommand) != 4) {
|
||||||
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
$this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
|
||||||
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
// Does the hostname match? So we try directly
|
return;
|
||||||
if ($a->get_hostname() == $aCommand[2]) {
|
}
|
||||||
$this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
|
|
||||||
|
// We only allow one process per hostname. So we set a lock file
|
||||||
$aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
|
// Problem: We get the firstname after the first auth - not before
|
||||||
if (DBM::is_result($aUser)) {
|
$this->setHost($aCommand[2]);
|
||||||
$uid = User::authenticate($aUser, $aCommand[3]);
|
|
||||||
$Error = $uid === false;
|
// We now check if the password match
|
||||||
} else {
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
$this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
|
|
||||||
$Error = true;
|
// Does the hostname match? So we try directly
|
||||||
$uid = -1;
|
if ($a->get_hostname() == $aCommand[2]) {
|
||||||
}
|
$this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
|
||||||
if ($Error) {
|
|
||||||
$this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
|
$aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
|
||||||
$sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
|
if (DBM::is_result($aUser)) {
|
||||||
$Error = ($aCommand[3] != $sPassword);
|
$uid = User::authenticate($aUser, $aCommand[3]);
|
||||||
}
|
$Error = $uid === false;
|
||||||
} else {
|
} else {
|
||||||
$Error = true;
|
$this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
|
||||||
}
|
$Error = true;
|
||||||
|
$uid = -1;
|
||||||
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
}
|
||||||
if ($Error) {
|
if ($Error) {
|
||||||
$Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
|
$this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
|
||||||
}
|
$sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
|
||||||
|
$Error = ($aCommand[3] != $sPassword);
|
||||||
if ($Error) {
|
}
|
||||||
$this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
|
} else {
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$Error = true;
|
||||||
} else {
|
}
|
||||||
$this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
|
|
||||||
fwrite(STDOUT, pack('nn', 2, 1));
|
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
||||||
}
|
if ($Error) {
|
||||||
}
|
$Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @brief Check remote credentials via HTTP(S)
|
if ($Error) {
|
||||||
*
|
$this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
|
||||||
* @param string $host The hostname
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
* @param string $user Username
|
} else {
|
||||||
* @param string $password Password
|
$this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
|
||||||
* @param boolean $ssl Should the check be done via SSL?
|
fwrite(STDOUT, pack('nn', 2, 1));
|
||||||
*
|
}
|
||||||
* @return boolean Are the credentials okay?
|
}
|
||||||
*/
|
|
||||||
private function checkCredentials($host, $user, $password, $ssl)
|
/**
|
||||||
{
|
* @brief Check remote credentials via HTTP(S)
|
||||||
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';
|
*
|
||||||
|
* @param string $host The hostname
|
||||||
$ch = curl_init();
|
* @param string $user Username
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
* @param string $password Password
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
* @param boolean $ssl Should the check be done via SSL?
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
*
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
* @return boolean Are the credentials okay?
|
||||||
curl_setopt($ch, CURLOPT_NOBODY, true);
|
*/
|
||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
private function checkCredentials($host, $user, $password, $ssl)
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
|
{
|
||||||
|
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json';
|
||||||
curl_exec($ch);
|
|
||||||
$curl_info = @curl_getinfo($ch);
|
$ch = curl_init();
|
||||||
$http_code = $curl_info['http_code'];
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_close($ch);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||||||
$this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
||||||
return $http_code == 200;
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
}
|
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
|
||||||
|
|
||||||
/**
|
curl_exec($ch);
|
||||||
* @brief write data to the syslog
|
$curl_info = @curl_getinfo($ch);
|
||||||
*
|
$http_code = $curl_info['http_code'];
|
||||||
* @param integer $loglevel The syslog loglevel
|
curl_close($ch);
|
||||||
* @param string $sMessage The syslog message
|
|
||||||
*/
|
$this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
|
||||||
private function writeLog($loglevel, $sMessage)
|
|
||||||
{
|
return $http_code == 200;
|
||||||
if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
|
}
|
||||||
return;
|
|
||||||
}
|
/**
|
||||||
syslog($loglevel, $sMessage);
|
* @brief Set the hostname for this process
|
||||||
}
|
*
|
||||||
|
* @param string $host The hostname
|
||||||
/**
|
*/
|
||||||
* @brief destroy the class, close the syslog connection.
|
private function setHost($host)
|
||||||
*/
|
{
|
||||||
public function __destruct()
|
if (!empty($this->host)) {
|
||||||
{
|
return;
|
||||||
$this->writeLog(LOG_NOTICE, 'stop');
|
}
|
||||||
closelog();
|
|
||||||
}
|
$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
|
||||||
|
*
|
||||||
|
* @param integer $loglevel The syslog loglevel
|
||||||
|
* @param string $sMessage The syslog message
|
||||||
|
*/
|
||||||
|
private function writeLog($loglevel, $sMessage)
|
||||||
|
{
|
||||||
|
if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
syslog($loglevel, $sMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief destroy the class, close the syslog connection.
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
$this->writeLog(LOG_NOTICE, 'stop');
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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