Merge pull request #4044 from annando/ejabberd-lock
Prevent the running of multiple xmpp auth daemons at a time
This commit is contained in:
commit
33ccd501b9
|
@ -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,351 @@
|
||||||
<?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;
|
||||||
/**
|
|
||||||
* @brief Create the class
|
/**
|
||||||
*
|
* @brief Create the class
|
||||||
* @param boolean $bDebug Debug mode
|
*
|
||||||
*/
|
* @param boolean $bDebug Debug mode
|
||||||
public function __construct()
|
*/
|
||||||
{
|
public function __construct()
|
||||||
$this->bDebug = (int) Config::get('jabber', 'debug');
|
{
|
||||||
|
$this->bDebug = (int) Config::get('jabber', 'debug');
|
||||||
openlog('auth_ejabberd', LOG_PID, LOG_USER);
|
|
||||||
|
openlog('auth_ejabberd', LOG_PID, LOG_USER);
|
||||||
$this->writeLog(LOG_NOTICE, 'start');
|
|
||||||
}
|
$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
|
||||||
*
|
* parameters
|
||||||
* @return null
|
*
|
||||||
*/
|
* @return null
|
||||||
public function readStdin()
|
*/
|
||||||
{
|
public function readStdin()
|
||||||
while (!feof(STDIN)) {
|
{
|
||||||
// Quit if the database connection went down
|
while (!feof(STDIN)) {
|
||||||
if (!dba::connected()) {
|
// Quit if the database connection went down
|
||||||
$this->writeLog(LOG_ERR, 'the database connection went down');
|
if (!dba::connected()) {
|
||||||
return;
|
$this->writeLog(LOG_ERR, 'the database connection went down');
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
$iHeader = fgets(STDIN, 3);
|
|
||||||
$aLength = unpack('n', $iHeader);
|
$iHeader = fgets(STDIN, 3);
|
||||||
$iLength = $aLength['1'];
|
$aLength = unpack('n', $iHeader);
|
||||||
|
$iLength = $aLength['1'];
|
||||||
// No data? Then quit
|
|
||||||
if ($iLength == 0) {
|
// No data? Then quit
|
||||||
$this->writeLog(LOG_ERR, 'we got no data, quitting');
|
if ($iLength == 0) {
|
||||||
return;
|
$this->writeLog(LOG_ERR, 'we got no data, quitting');
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Fetching the data
|
|
||||||
$sData = fgets(STDIN, $iLength + 1);
|
// Fetching the data
|
||||||
$this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
|
$sData = fgets(STDIN, $iLength + 1);
|
||||||
$aCommand = explode(':', $sData);
|
$this->writeLog(LOG_DEBUG, 'received data: ' . $sData);
|
||||||
if (is_array($aCommand)) {
|
$aCommand = explode(':', $sData);
|
||||||
switch ($aCommand[0]) {
|
if (is_array($aCommand)) {
|
||||||
case 'isuser':
|
switch ($aCommand[0]) {
|
||||||
// Check the existance of a given username
|
case 'isuser':
|
||||||
$this->isUser($aCommand);
|
// Check the existance of a given username
|
||||||
break;
|
$this->isUser($aCommand);
|
||||||
case 'auth':
|
break;
|
||||||
// Check if the givven password is correct
|
case 'auth':
|
||||||
$this->auth($aCommand);
|
// Check if the givven password is correct
|
||||||
break;
|
$this->auth($aCommand);
|
||||||
case 'setpass':
|
break;
|
||||||
// We don't accept the setting of passwords here
|
case 'setpass':
|
||||||
$this->writeLog(LOG_NOTICE, 'setpass command disabled');
|
// We don't accept the setting of passwords here
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$this->writeLog(LOG_NOTICE, 'setpass command disabled');
|
||||||
break;
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
default:
|
break;
|
||||||
// We don't know the given command
|
default:
|
||||||
$this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
|
// We don't know the given command
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$this->writeLog(LOG_NOTICE, 'unknown command ' . $aCommand[0]);
|
||||||
break;
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
}
|
break;
|
||||||
} else {
|
}
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
|
} else {
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$this->writeLog(LOG_NOTICE, 'invalid command string ' . $sData);
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @brief Check if the given username exists
|
/**
|
||||||
*
|
* @brief Check if the given username exists
|
||||||
* @param array $aCommand The command array
|
*
|
||||||
*/
|
* @param array $aCommand The command array
|
||||||
private function isUser(array $aCommand)
|
*/
|
||||||
{
|
private function isUser(array $aCommand)
|
||||||
$a = get_app();
|
{
|
||||||
|
$a = get_app();
|
||||||
// Check if there is a username
|
|
||||||
if (!isset($aCommand[1])) {
|
// Check if there is a username
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
|
if (!isset($aCommand[1])) {
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$this->writeLog(LOG_NOTICE, 'invalid isuser command, no username given');
|
||||||
return;
|
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
|
||||||
|
// Problem: We get the firstname after the first auth - not before
|
||||||
// Does the hostname match? So we try directly
|
$this->setHost($aCommand[2]);
|
||||||
if ($a->get_hostname() == $aCommand[2]) {
|
|
||||||
$this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
|
// Now we check if the given user is valid
|
||||||
$found = dba::exists('user', ['nickname' => $sUser]);
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
} else {
|
|
||||||
$found = false;
|
// Does the hostname match? So we try directly
|
||||||
}
|
if ($a->get_hostname() == $aCommand[2]) {
|
||||||
|
$this->writeLog(LOG_INFO, 'internal user check for ' . $sUser . '@' . $aCommand[2]);
|
||||||
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
$found = dba::exists('user', ['nickname' => $sUser]);
|
||||||
if (!$found) {
|
} else {
|
||||||
$found = $this->checkUser($aCommand[2], $aCommand[1], true);
|
$found = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($found) {
|
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
||||||
// The user is okay
|
if (!$found) {
|
||||||
$this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
|
$found = $this->checkUser($aCommand[2], $aCommand[1], true);
|
||||||
fwrite(STDOUT, pack('nn', 2, 1));
|
}
|
||||||
} else {
|
|
||||||
// The user isn't okay
|
if ($found) {
|
||||||
$this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
|
// The user is okay
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$this->writeLog(LOG_NOTICE, 'valid user: ' . $sUser);
|
||||||
}
|
fwrite(STDOUT, pack('nn', 2, 1));
|
||||||
}
|
} else {
|
||||||
|
// The user isn't okay
|
||||||
/**
|
$this->writeLog(LOG_WARNING, 'invalid user: ' . $sUser);
|
||||||
* @brief Check remote user existance via HTTP(S)
|
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?
|
/**
|
||||||
*
|
* @brief Check remote user existance via HTTP(S)
|
||||||
* @return boolean Was the user found?
|
*
|
||||||
*/
|
* @param string $host The hostname
|
||||||
private function checkUser($host, $user, $ssl)
|
* @param string $user Username
|
||||||
{
|
* @param boolean $ssl Should the check be done via SSL?
|
||||||
$this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
|
*
|
||||||
|
* @return boolean Was the user found?
|
||||||
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
|
*/
|
||||||
|
private function checkUser($host, $user, $ssl)
|
||||||
$data = z_fetch_url($url);
|
{
|
||||||
|
$this->writeLog(LOG_INFO, 'external user check for ' . $user . '@' . $host);
|
||||||
if (!is_array($data)) {
|
|
||||||
return false;
|
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
|
||||||
}
|
|
||||||
|
$data = z_fetch_url($url);
|
||||||
if ($data['return_code'] != '200') {
|
|
||||||
return false;
|
if (!is_array($data)) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
$json = @json_decode($data['body']);
|
|
||||||
if (!is_object($json)) {
|
if ($data['return_code'] != '200') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $json->nick == $user;
|
$json = @json_decode($data['body']);
|
||||||
}
|
if (!is_object($json)) {
|
||||||
|
return false;
|
||||||
/**
|
}
|
||||||
* @brief Authenticate the given user and password
|
|
||||||
*
|
return $json->nick == $user;
|
||||||
* @param array $aCommand The command array
|
}
|
||||||
*/
|
|
||||||
private function auth(array $aCommand)
|
/**
|
||||||
{
|
* @brief Authenticate the given user and password
|
||||||
$a = get_app();
|
*
|
||||||
|
* @param array $aCommand The command array
|
||||||
// check user authentication
|
*/
|
||||||
if (sizeof($aCommand) != 4) {
|
private function auth(array $aCommand)
|
||||||
$this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
|
{
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
$a = get_app();
|
||||||
return;
|
|
||||||
}
|
// check user authentication
|
||||||
|
if (sizeof($aCommand) != 4) {
|
||||||
// We now check if the password match
|
$this->writeLog(LOG_NOTICE, 'invalid auth command, data missing');
|
||||||
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
|
return;
|
||||||
// Does the hostname match? So we try directly
|
}
|
||||||
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
|
||||||
|
// Problem: We get the firstname after the first auth - not before
|
||||||
$aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
|
$this->setHost($aCommand[2]);
|
||||||
if (DBM::is_result($aUser)) {
|
|
||||||
$uid = User::authenticate($aUser, $aCommand[3]);
|
// We now check if the password match
|
||||||
$Error = $uid === false;
|
$sUser = str_replace(array('%20', '(a)'), array(' ', '@'), $aCommand[1]);
|
||||||
} else {
|
|
||||||
$this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
|
// Does the hostname match? So we try directly
|
||||||
$Error = true;
|
if ($a->get_hostname() == $aCommand[2]) {
|
||||||
$uid = -1;
|
$this->writeLog(LOG_INFO, 'internal auth for ' . $sUser . '@' . $aCommand[2]);
|
||||||
}
|
|
||||||
if ($Error) {
|
$aUser = dba::select('user', ['uid', 'password'], ['nickname' => $sUser], ['limit' => 1]);
|
||||||
$this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
|
if (DBM::is_result($aUser)) {
|
||||||
$sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
|
$uid = User::authenticate($aUser, $aCommand[3]);
|
||||||
$Error = ($aCommand[3] != $sPassword);
|
$Error = $uid === false;
|
||||||
}
|
} else {
|
||||||
} else {
|
$this->writeLog(LOG_WARNING, 'user not found: ' . $sUser);
|
||||||
$Error = true;
|
$Error = true;
|
||||||
}
|
$uid = -1;
|
||||||
|
}
|
||||||
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
if ($Error) {
|
||||||
if ($Error) {
|
$this->writeLog(LOG_INFO, 'check against alternate password for ' . $sUser . '@' . $aCommand[2]);
|
||||||
$Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
|
$sPassword = PConfig::get($uid, 'xmpp', 'password', null, true);
|
||||||
}
|
$Error = ($aCommand[3] != $sPassword);
|
||||||
|
}
|
||||||
if ($Error) {
|
} else {
|
||||||
$this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
|
$Error = true;
|
||||||
fwrite(STDOUT, pack('nn', 2, 0));
|
}
|
||||||
} else {
|
|
||||||
$this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
|
// If the hostnames doesn't match or there is some failure, we try to check remotely
|
||||||
fwrite(STDOUT, pack('nn', 2, 1));
|
if ($Error) {
|
||||||
}
|
$Error = !$this->checkCredentials($aCommand[2], $aCommand[1], $aCommand[3], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ($Error) {
|
||||||
* @brief Check remote credentials via HTTP(S)
|
$this->writeLog(LOG_WARNING, 'authentification failed for user ' . $sUser . '@' . $aCommand[2]);
|
||||||
*
|
fwrite(STDOUT, pack('nn', 2, 0));
|
||||||
* @param string $host The hostname
|
} else {
|
||||||
* @param string $user Username
|
$this->writeLog(LOG_NOTICE, 'authentificated user ' . $sUser . '@' . $aCommand[2]);
|
||||||
* @param string $password Password
|
fwrite(STDOUT, pack('nn', 2, 1));
|
||||||
* @param boolean $ssl Should the check be done via SSL?
|
}
|
||||||
*
|
}
|
||||||
* @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
|
||||||
|
* @param string $user Username
|
||||||
$ch = curl_init();
|
* @param string $password Password
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
* @param boolean $ssl Should the check be done via SSL?
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
*
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
* @return boolean Are the credentials okay?
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
*/
|
||||||
curl_setopt($ch, CURLOPT_NOBODY, true);
|
private function checkCredentials($host, $user, $password, $ssl)
|
||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
{
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
|
$this->writeLog(LOG_INFO, 'external credential check for ' . $user . '@' . $host);
|
||||||
|
|
||||||
curl_exec($ch);
|
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/api/account/verify_credentials.json?skip_status=true';
|
||||||
$curl_info = @curl_getinfo($ch);
|
|
||||||
$http_code = $curl_info['http_code'];
|
$ch = curl_init();
|
||||||
curl_close($ch);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
$this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||||||
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
return $http_code == 200;
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
||||||
}
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
|
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
|
||||||
/**
|
|
||||||
* @brief write data to the syslog
|
curl_exec($ch);
|
||||||
*
|
$curl_info = @curl_getinfo($ch);
|
||||||
* @param integer $loglevel The syslog loglevel
|
$http_code = $curl_info['http_code'];
|
||||||
* @param string $sMessage The syslog message
|
curl_close($ch);
|
||||||
*/
|
|
||||||
private function writeLog($loglevel, $sMessage)
|
$this->writeLog(LOG_INFO, 'external auth for ' . $user . '@' . $host . ' returned ' . $http_code);
|
||||||
{
|
|
||||||
if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
|
return $http_code == 200;
|
||||||
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)) {
|
||||||
$this->writeLog(LOG_NOTICE, 'stop');
|
return;
|
||||||
closelog();
|
}
|
||||||
}
|
|
||||||
}
|
$this->writeLog(LOG_INFO, 'Hostname for process ' . getmypid() . ' is ' . $host);
|
||||||
|
|
||||||
|
$this->host = $host;
|
||||||
|
|
||||||
|
$lockpath = Config::get('jabber', 'lockpath');
|
||||||
|
if (is_null($lockpath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = $lockpath . DIRECTORY_SEPARATOR . $host;
|
||||||
|
if (Pidfile::isRunningProcess($file)) {
|
||||||
|
if (PidFile::killProcess($file)) {
|
||||||
|
$this->writeLog(LOG_INFO, 'Old process was successfully killed');
|
||||||
|
} else {
|
||||||
|
$this->writeLog(LOG_ERR, "The old Process wasn't killed in time. We now quit our process.");
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now it is safe to create the pid file
|
||||||
|
Pidfile::create($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,64 +9,100 @@ namespace Friendica\Util;
|
||||||
*/
|
*/
|
||||||
class Pidfile
|
class Pidfile
|
||||||
{
|
{
|
||||||
private $file;
|
|
||||||
private $running;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $dir path
|
* @brief Read the pid from a given pid file
|
||||||
* @param string $name filename
|
*
|
||||||
* @return void
|
* @param string $file Filename of pid file
|
||||||
|
*
|
||||||
|
* @return boolean|string PID or "false" if not existent
|
||||||
*/
|
*/
|
||||||
public function __construct($dir, $name)
|
static private function pidFromFile($file) {
|
||||||
{
|
if (!file_exists($file)) {
|
||||||
$this->_file = "$dir/$name.pid";
|
return false;
|
||||||
|
|
||||||
if (file_exists($this->_file)) {
|
|
||||||
$pid = trim(@file_get_contents($this->file));
|
|
||||||
if (($pid != "") && posix_kill($pid, 0)) {
|
|
||||||
$this->running = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->running) {
|
return trim(@file_get_contents($file));
|
||||||
$pid = getmypid();
|
|
||||||
file_put_contents($this->file, $pid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @brief Is there a running process with the given pid file
|
||||||
|
*
|
||||||
|
* @param string $file Filename of pid file
|
||||||
|
*
|
||||||
|
* @return boolean Is it running?
|
||||||
*/
|
*/
|
||||||
public function __destruct()
|
static public function isRunningProcess($file) {
|
||||||
{
|
$pid = self::pidFromFile($file);
|
||||||
if ((! $this->running) && file_exists($this->file)) {
|
|
||||||
@unlink($this->file);
|
if (!$pid) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Is the process running?
|
||||||
* @return boolean
|
$running = posix_kill($pid, 0);
|
||||||
*/
|
|
||||||
public static function isRunning()
|
|
||||||
{
|
|
||||||
return self::$running;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// If not, then we will kill the stale file
|
||||||
* @return object
|
if (!$running) {
|
||||||
*/
|
self::delete($file);
|
||||||
public static function runningTime()
|
|
||||||
{
|
|
||||||
return time() - @filectime(self::$file);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public static function kill()
|
|
||||||
{
|
|
||||||
if (file_exists(self::$file)) {
|
|
||||||
return posix_kill(file_get_contents(self::$file), SIGTERM);
|
|
||||||
}
|
}
|
||||||
|
return $running;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Kills a process from a given pid file
|
||||||
|
*
|
||||||
|
* @param string $file Filename of pid file
|
||||||
|
*
|
||||||
|
* @return boolean Was it killed successfully?
|
||||||
|
*/
|
||||||
|
static public function killProcess($file) {
|
||||||
|
$pid = self::pidFromFile($file);
|
||||||
|
|
||||||
|
// We don't have a process id? then we quit
|
||||||
|
if (!$pid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now kill the process
|
||||||
|
$killed = posix_kill($pid, SIGTERM);
|
||||||
|
|
||||||
|
// If we killed the process successfully, we can remove the pidfile
|
||||||
|
if ($killed) {
|
||||||
|
self::delete($file);
|
||||||
|
}
|
||||||
|
return $killed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates a pid file
|
||||||
|
*
|
||||||
|
* @param string $file Filename of pid file
|
||||||
|
*
|
||||||
|
* @return boolean|string PID or "false" if not created
|
||||||
|
*/
|
||||||
|
static public function create($file) {
|
||||||
|
$pid = self::pidFromFile($file);
|
||||||
|
|
||||||
|
// We have a process id? then we quit
|
||||||
|
if ($pid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pid = getmypid();
|
||||||
|
file_put_contents($file, $pid);
|
||||||
|
|
||||||
|
// Now we check if everything is okay
|
||||||
|
return self::pidFromFile($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deletes a given pid file
|
||||||
|
*
|
||||||
|
* @param string $file Filename of pid file
|
||||||
|
*
|
||||||
|
* @return boolean Is it running?
|
||||||
|
*/
|
||||||
|
static public function delete($file) {
|
||||||
|
return @unlink($file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue