TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
/**
|
||||
* A locking exception
|
||||
*/
|
||||
class Exception extends \TYPO3\CMS\Core\Exception {}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking\Exception;
|
||||
|
||||
use TYPO3\CMS\Core\Locking\Exception;
|
||||
|
||||
/**
|
||||
* An exception indicating a lock acquisition error
|
||||
*/
|
||||
class LockAcquireException extends Exception {}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking\Exception;
|
||||
|
||||
/**
|
||||
* An exception indicating that acquiring a lock would have blocked
|
||||
*/
|
||||
class LockAcquireWouldBlockException extends LockAcquireException {}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking\Exception;
|
||||
|
||||
use TYPO3\CMS\Core\Locking\Exception;
|
||||
|
||||
/**
|
||||
* An exception indicating a lock creation error
|
||||
*/
|
||||
class LockCreateException extends Exception {}
|
||||
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use TYPO3\CMS\Core\Core\Environment;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
use TYPO3\CMS\Core\Security\BlockSerializationTrait;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* flock() locking
|
||||
*/
|
||||
class FileLockStrategy implements LockingStrategyInterface
|
||||
{
|
||||
use BlockSerializationTrait;
|
||||
|
||||
public const FILE_LOCK_FOLDER = 'lock/';
|
||||
public const DEFAULT_PRIORITY = 75;
|
||||
|
||||
/**
|
||||
* @var resource File pointer if using flock method
|
||||
*/
|
||||
protected $filePointer;
|
||||
|
||||
/**
|
||||
* @var string File used for locking
|
||||
*/
|
||||
protected $filePath;
|
||||
|
||||
/**
|
||||
* @var bool True if lock is acquired
|
||||
*/
|
||||
protected $isAcquired = false;
|
||||
|
||||
/**
|
||||
* @param string $subject ID to identify this lock in the system
|
||||
* @throws LockCreateException if the lock could not be created
|
||||
*/
|
||||
public function __construct($subject)
|
||||
{
|
||||
/*
|
||||
* Tests if the directory for file locks is available.
|
||||
* If not, the directory will be created. The lock path is usually
|
||||
* below typo3temp/var, typo3temp/var itself should exist already (or root-path/var/ respectively)
|
||||
*/
|
||||
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
|
||||
$path = Environment::getProjectPath() . '/'
|
||||
. trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
|
||||
. '/';
|
||||
} else {
|
||||
$path = Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER;
|
||||
}
|
||||
if (!is_dir($path)) {
|
||||
// Not using mkdir_deep on purpose here, if typo3temp itself
|
||||
// does not exist, this issue should be solved on a different
|
||||
// level of the application.
|
||||
if (!GeneralUtility::mkdir($path)) {
|
||||
throw new LockCreateException('Cannot create directory ' . $path, 1395140007);
|
||||
}
|
||||
}
|
||||
if (!is_writable($path)) {
|
||||
throw new LockCreateException('Cannot write to directory ' . $path, 1396278700);
|
||||
}
|
||||
$this->filePath = $path . 'flock_' . md5((string)$subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor:
|
||||
* Releases lock automatically when instance is destroyed and release resources
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->release();
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to acquire an exclusive lock
|
||||
*
|
||||
* @param int $mode LOCK_CAPABILITY_EXCLUSIVE or LOCK_CAPABILITY_SHARED or self::LOCK_CAPABILITY_NOBLOCK
|
||||
* @return bool Returns TRUE if the lock was acquired successfully
|
||||
* @throws LockAcquireException if the lock could not be acquired
|
||||
* @throws LockAcquireWouldBlockException if the acquire would have blocked and NOBLOCK was set
|
||||
*/
|
||||
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
|
||||
{
|
||||
if ($this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$filePointer = fopen($this->filePath, 'c');
|
||||
if ($filePointer === false) {
|
||||
throw new LockAcquireException('Lock file could not be opened', 1294586099);
|
||||
}
|
||||
$this->filePointer = $filePointer;
|
||||
GeneralUtility::fixPermissions($this->filePath);
|
||||
|
||||
$operation = $mode & self::LOCK_CAPABILITY_EXCLUSIVE ? LOCK_EX : LOCK_SH;
|
||||
if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
|
||||
$operation |= LOCK_NB;
|
||||
}
|
||||
|
||||
$wouldBlock = 0;
|
||||
$this->isAcquired = flock($this->filePointer, $operation, $wouldBlock);
|
||||
|
||||
if (!$this->isAcquired) {
|
||||
// Make sure to cleanup any dangling resources for this process/thread, which are not needed any longer
|
||||
fclose($this->filePointer);
|
||||
}
|
||||
if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->isAcquired && $wouldBlock) {
|
||||
throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1428700748);
|
||||
}
|
||||
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the lock
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
if (!$this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
$success = true;
|
||||
if (is_resource($this->filePointer)) {
|
||||
if (flock($this->filePointer, LOCK_UN) === false) {
|
||||
$success = false;
|
||||
}
|
||||
fclose($this->filePointer);
|
||||
}
|
||||
$this->isAcquired = false;
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of this lock
|
||||
*
|
||||
* @return bool Returns TRUE if lock is acquired by this locker, FALSE otherwise
|
||||
*/
|
||||
public function isAcquired()
|
||||
{
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Returns a priority for the method. 0 to 100, 100 is highest
|
||||
*/
|
||||
public static function getPriority()
|
||||
{
|
||||
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
|
||||
?? self::DEFAULT_PRIORITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int LOCK_CAPABILITY_* elements combined with bit-wise OR
|
||||
*/
|
||||
public static function getCapabilities()
|
||||
{
|
||||
if (PHP_SAPI === 'isapi') {
|
||||
// From php docs: When using a multi-threaded server API like ISAPI you may not be able to rely on flock()
|
||||
// to protect files against other PHP scripts running in parallel threads of the same server instance!
|
||||
return 0;
|
||||
}
|
||||
$capabilities = self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_SHARED | self::LOCK_CAPABILITY_NOBLOCK;
|
||||
|
||||
return $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the resource associated with the lock
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
@unlink($this->filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
use TYPO3\CMS\Core\SingletonInterface;
|
||||
|
||||
/**
|
||||
* Factory class to retrieve a locking method
|
||||
*/
|
||||
class LockFactory implements SingletonInterface
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, bool>
|
||||
*/
|
||||
protected array $lockingStrategy = [
|
||||
SemaphoreLockStrategy::class => true,
|
||||
FileLockStrategy::class => true,
|
||||
SimpleLockStrategy::class => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* Add a locking method.
|
||||
*
|
||||
* @param class-string $className
|
||||
*/
|
||||
public function addLockingStrategy(string $className): void
|
||||
{
|
||||
$interfaces = class_implements($className);
|
||||
if (isset($interfaces[LockingStrategyInterface::class])) {
|
||||
$this->lockingStrategy[$className] = true;
|
||||
} else {
|
||||
throw new \InvalidArgumentException('The given class name ' . $className . ' does not implement the required LockingStrategyInterface interface.', 1425990198);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a locking method.
|
||||
*
|
||||
* @param class-string $className
|
||||
*/
|
||||
public function removeLockingStrategy(string $className): void
|
||||
{
|
||||
unset($this->lockingStrategy[$className]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get best matching locking method
|
||||
*
|
||||
* @param string $id ID to identify this lock in the system
|
||||
* @param int-mask-of<LockingStrategyInterface::LOCK_CAPABILITY_*> $capabilities LockingStrategyInterface::LOCK_CAPABILITY_* elements combined with bit-wise OR
|
||||
* @return LockingStrategyInterface Class name for a locking method
|
||||
* @throws LockCreateException if no locker could be created with the requested capabilities
|
||||
*/
|
||||
public function createLocker(string $id, int $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE): LockingStrategyInterface
|
||||
{
|
||||
$queue = new \SplPriorityQueue();
|
||||
|
||||
/** @var class-string<LockingStrategyInterface> $method */
|
||||
foreach ($this->lockingStrategy as $method => $_) {
|
||||
$supportedCapabilities = $capabilities & $method::getCapabilities();
|
||||
if ($supportedCapabilities === $capabilities) {
|
||||
$queue->insert($method, $method::getPriority());
|
||||
}
|
||||
}
|
||||
if ($queue->count() > 0) {
|
||||
$className = $queue->top();
|
||||
// We use 'new' here on purpose!
|
||||
// Locking might be used very early in the bootstrap process, where makeInstance() does not work
|
||||
return new $className($id);
|
||||
}
|
||||
throw new LockCreateException('Could not find a matching locking method with requested capabilities.', 1425990190);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
|
||||
/**
|
||||
* Interface for locking methods
|
||||
*/
|
||||
interface LockingStrategyInterface
|
||||
{
|
||||
/**
|
||||
* Exclusive locks can be acquired
|
||||
*/
|
||||
public const LOCK_CAPABILITY_EXCLUSIVE = 1;
|
||||
|
||||
/**
|
||||
* Shared locks can be acquired
|
||||
*/
|
||||
public const LOCK_CAPABILITY_SHARED = 2;
|
||||
|
||||
/**
|
||||
* Do not block when acquiring the lock
|
||||
*/
|
||||
public const LOCK_CAPABILITY_NOBLOCK = 4;
|
||||
|
||||
/**
|
||||
* @return int LOCK_CAPABILITY_* elements combined with bit-wise OR
|
||||
*/
|
||||
public static function getCapabilities();
|
||||
|
||||
/**
|
||||
* @return int Returns a priority for the method. 0 to 100, 100 is highest
|
||||
*/
|
||||
public static function getPriority();
|
||||
|
||||
/**
|
||||
* @param string $subject ID to identify this lock in the system
|
||||
* @throws LockCreateException if the lock could not be created
|
||||
*/
|
||||
public function __construct($subject);
|
||||
|
||||
/**
|
||||
* Try to acquire a lock
|
||||
*
|
||||
* @param int $mode LOCK_CAPABILITY_EXCLUSIVE or LOCK_CAPABILITY_SHARED
|
||||
* @return bool Returns TRUE if the lock was acquired successfully
|
||||
* @throws LockAcquireException if the lock could not be acquired
|
||||
* @throws LockAcquireWouldBlockException if the acquire would have blocked and NOBLOCK was set
|
||||
*/
|
||||
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE);
|
||||
|
||||
/**
|
||||
* Release the lock
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function release();
|
||||
|
||||
/**
|
||||
* Destroys the resource associated with the lock
|
||||
*/
|
||||
public function destroy();
|
||||
|
||||
/**
|
||||
* Get status of this lock
|
||||
*
|
||||
* @return bool Returns TRUE if lock is acquired by this locker, FALSE otherwise
|
||||
*/
|
||||
public function isAcquired();
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
|
||||
/**
|
||||
* Wrapper for locking API that uses two locks to not exhaust locking resources and still block properly.
|
||||
*
|
||||
* The schematics here is:
|
||||
* - First acquire an access lock. This is using the type of the requested lock as key.
|
||||
* Since the number of types is rather limited we can use the type as key as it will only
|
||||
* eat up a limited number of lock resources on the system (files, semaphores)
|
||||
* - Second, we acquire the actual lock. We can be sure we are the only process at this
|
||||
* very moment, hence we either get the lock for the given key or we get an error as
|
||||
* we request a non-blocking mode.
|
||||
*
|
||||
* Interleaving two locks is important, because the actual lock uses a hash value as key (see callers).
|
||||
* If we would simply employ a normal blocking lock, we would get a potentially unlimited number of
|
||||
* different locks. Depending on the available locking methods on the system we might run out of available
|
||||
* resources: For instance maximum limit of semaphores is a system setting and applies to the whole system.
|
||||
*
|
||||
* We therefore must make sure that page locks are destroyed again if they are not used anymore, such that
|
||||
* we never use more locking resources than parallel requests.
|
||||
*
|
||||
* In order to ensure this, we need to guarantee that no other process is waiting on a lock when
|
||||
* the process currently having the lock on the lock is about to release the lock again.
|
||||
*
|
||||
* This can only be achieved by using a non-blocking mode, such that a process is never put into wait state
|
||||
* by the kernel, but only checks the availability of the lock. The access lock is our guard to be sure
|
||||
* that no two processes are at the same time releasing/destroying a lock, whilst the other one tries to
|
||||
* get a lock for this page lock.
|
||||
*
|
||||
* The only drawback of this implementation is that we basically have to poll the availability of the page lock.
|
||||
*
|
||||
* Note that the access lock resources are NEVER deleted/destroyed, otherwise the whole thing would be broken.
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
class ResourceMutex
|
||||
{
|
||||
/**
|
||||
* @var array<string,LockingStrategyInterface|null>
|
||||
*/
|
||||
private array $accessLocks = [];
|
||||
|
||||
/**
|
||||
* @var array<string,LockingStrategyInterface|null>
|
||||
*/
|
||||
private array $workerLocks = [];
|
||||
|
||||
public function __construct(private readonly LockFactory $lockFactory) {}
|
||||
|
||||
/**
|
||||
* Acquire a specific lock for the given scope.
|
||||
*
|
||||
* @throws LockAcquireException
|
||||
* @throws LockCreateException
|
||||
* @return bool True if we did not get the lock immediately and had to wait. This can be useful to
|
||||
* know in the consumer since another process may have created something that we can
|
||||
* re-use immediately.
|
||||
*/
|
||||
public function acquireLock(string $scope, string $key): bool
|
||||
{
|
||||
$this->accessLocks[$scope] = $this->lockFactory->createLocker($scope);
|
||||
$this->workerLocks[$scope] = $this->lockFactory->createLocker(
|
||||
$key,
|
||||
LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
|
||||
);
|
||||
$hadToWaitForLock = false;
|
||||
do {
|
||||
if (!$this->accessLocks[$scope]->acquire()) {
|
||||
throw new \RuntimeException('Could not acquire access lock for "' . $scope . '".', 1601923209);
|
||||
}
|
||||
try {
|
||||
$locked = $this->workerLocks[$scope]->acquire(
|
||||
LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
|
||||
);
|
||||
} catch (LockAcquireWouldBlockException $e) {
|
||||
// Somebody else has the lock, we keep waiting.
|
||||
// First release the access lock, it will be acquired in next iteration again.
|
||||
$this->accessLocks[$scope]->release();
|
||||
// Mark "We had to wait".
|
||||
$hadToWaitForLock = true;
|
||||
// Now lets make a short break (20ms) until we try again, since
|
||||
// the page generation by the lock owner will take a while.
|
||||
usleep(20000);
|
||||
continue;
|
||||
}
|
||||
$this->accessLocks[$scope]->release();
|
||||
if ($locked) {
|
||||
break;
|
||||
}
|
||||
throw new \RuntimeException('Could not acquire process lock for "' . $scope . '" with key "' . $key . '".', 1601923215);
|
||||
} while (true);
|
||||
return $hadToWaitForLock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a worker specific lock.
|
||||
*
|
||||
* @throws LockAcquireException
|
||||
* @throws LockAcquireWouldBlockException
|
||||
*/
|
||||
public function releaseLock(string $scope): void
|
||||
{
|
||||
if ($this->accessLocks[$scope] ?? null) {
|
||||
if (!$this->accessLocks[$scope]->acquire()) {
|
||||
throw new \RuntimeException('Could not acquire access lock for "' . $scope . '".', 1601923319);
|
||||
}
|
||||
$this->workerLocks[$scope]->release();
|
||||
$this->workerLocks[$scope]->destroy();
|
||||
$this->workerLocks[$scope] = null;
|
||||
$this->accessLocks[$scope]->release();
|
||||
$this->accessLocks[$scope] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use TYPO3\CMS\Core\Core\Environment;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
use TYPO3\CMS\Core\Security\BlockSerializationTrait;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Semaphore locking
|
||||
*/
|
||||
class SemaphoreLockStrategy implements LockingStrategyInterface
|
||||
{
|
||||
use BlockSerializationTrait;
|
||||
|
||||
public const FILE_LOCK_FOLDER = 'lock/';
|
||||
public const DEFAULT_PRIORITY = 25;
|
||||
|
||||
/**
|
||||
* @var int Identifier used for this lock
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @var resource|\SysvSemaphore|null Semaphore Resource used for this lock
|
||||
*/
|
||||
protected $resource;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $filePath = '';
|
||||
|
||||
/**
|
||||
* @var bool TRUE if lock is acquired
|
||||
*/
|
||||
protected $isAcquired = false;
|
||||
|
||||
/**
|
||||
* @param string $subject ID to identify this lock in the system
|
||||
* @throws LockCreateException
|
||||
*/
|
||||
public function __construct($subject)
|
||||
{
|
||||
/*
|
||||
* Tests if the directory for semaphore locks is available.
|
||||
* If not, the directory will be created. The lock path is usually
|
||||
* below typo3temp/var, typo3temp/var itself should exist already (or root-path/var/ respectively)
|
||||
*/
|
||||
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
|
||||
$path = Environment::getProjectPath() . '/'
|
||||
. trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
|
||||
. '/';
|
||||
} else {
|
||||
$path = Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER;
|
||||
}
|
||||
if (!is_dir($path)) {
|
||||
// Not using mkdir_deep on purpose here, if typo3temp/var itself
|
||||
// does not exist, this issue should be solved on a different
|
||||
// level of the application.
|
||||
if (!GeneralUtility::mkdir($path)) {
|
||||
throw new LockCreateException('Cannot create directory ' . $path, 1460976250);
|
||||
}
|
||||
}
|
||||
if (!is_writable($path)) {
|
||||
throw new LockCreateException('Cannot write to directory ' . $path, 1460976320);
|
||||
}
|
||||
$this->filePath = $path . 'sem_' . md5((string)$subject);
|
||||
touch($this->filePath);
|
||||
$this->id = ftok($this->filePath, 'A');
|
||||
if ($this->id === -1) {
|
||||
throw new LockCreateException('Cannot create key for semaphore using path ' . $this->filePath, 1396278734);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->release();
|
||||
// We do not call sem_remove() since this would remove the resource for other processes,
|
||||
// we leave that to the system. This is not clean, but there's no other way to determine when
|
||||
// a semaphore is no longer needed as a website is generally running endlessly
|
||||
// and we have no way to detect if there is a process currently waiting on that lock
|
||||
// or if the server is shutdown
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the lock
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
if (!$this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
$this->isAcquired = false;
|
||||
return (bool)@sem_release($this->resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of this lock
|
||||
*
|
||||
* @return bool Returns TRUE if lock is acquired by this locker, FALSE otherwise
|
||||
*/
|
||||
public function isAcquired()
|
||||
{
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int LOCK_CAPABILITY_* elements combined with bit-wise OR
|
||||
*/
|
||||
public static function getCapabilities()
|
||||
{
|
||||
if (function_exists('sem_get')) {
|
||||
return self::LOCK_CAPABILITY_EXCLUSIVE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to acquire a lock
|
||||
*
|
||||
* @param int $mode LOCK_CAPABILITY_EXCLUSIVE
|
||||
* @return bool Returns TRUE if the lock was acquired successfully
|
||||
* @throws LockAcquireException if a semaphore could not be retrieved
|
||||
*/
|
||||
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
|
||||
{
|
||||
if ($this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$resource = sem_get($this->id, 1);
|
||||
if ($resource === false) {
|
||||
throw new LockAcquireException('Unable to get semaphore with id ' . $this->id, 1313828196);
|
||||
}
|
||||
$this->resource = $resource;
|
||||
|
||||
$this->isAcquired = (bool)sem_acquire($this->resource);
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Returns a priority for the method. 0 to 100, 100 is highest
|
||||
*/
|
||||
public static function getPriority()
|
||||
{
|
||||
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
|
||||
?? self::DEFAULT_PRIORITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the resource associated with the lock
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
if ($this->resource) {
|
||||
sem_remove($this->resource);
|
||||
@unlink($this->filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Locking;
|
||||
|
||||
use TYPO3\CMS\Core\Core\Environment;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException;
|
||||
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
|
||||
use TYPO3\CMS\Core\Security\BlockSerializationTrait;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Simple file locking
|
||||
*/
|
||||
class SimpleLockStrategy implements LockingStrategyInterface
|
||||
{
|
||||
use BlockSerializationTrait;
|
||||
|
||||
public const FILE_LOCK_FOLDER = 'lock/';
|
||||
public const DEFAULT_PRIORITY = 50;
|
||||
|
||||
/**
|
||||
* @var string File path used for this lock
|
||||
*/
|
||||
protected $filePath;
|
||||
|
||||
/**
|
||||
* @var bool True if lock is acquired
|
||||
*/
|
||||
protected $isAcquired = false;
|
||||
|
||||
/**
|
||||
* @var int Number of times a locked resource is tried to be acquired. Only used in manual locks method "simple".
|
||||
*/
|
||||
protected $loops = 150;
|
||||
|
||||
/**
|
||||
* @var int Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used in manual lock method "simple".
|
||||
*/
|
||||
protected $step = 200;
|
||||
|
||||
/**
|
||||
* @param string $subject ID to identify this lock in the system
|
||||
* @throws LockCreateException if the lock could not be created
|
||||
*/
|
||||
public function __construct($subject)
|
||||
{
|
||||
// Tests if the directory for simple locks is available.
|
||||
// If not, the directory will be created. The lock path is usually
|
||||
// below typo3temp/var, typo3temp/var itself should exist already (or getProjectPath . /var/ respectively)
|
||||
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
|
||||
$path = Environment::getProjectPath() . '/'
|
||||
. trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
|
||||
. '/';
|
||||
} else {
|
||||
$path = Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER;
|
||||
}
|
||||
if (!is_dir($path)) {
|
||||
// Not using mkdir_deep on purpose here, if typo3temp/var itself
|
||||
// does not exist, this issue should be solved on a different
|
||||
// level of the application.
|
||||
if (!GeneralUtility::mkdir($path)) {
|
||||
throw new LockCreateException('Cannot create directory ' . $path, 1460976286);
|
||||
}
|
||||
}
|
||||
if (!is_writable($path)) {
|
||||
throw new LockCreateException('Cannot write to directory ' . $path, 1460976340);
|
||||
}
|
||||
$this->filePath = $path . 'simple_' . md5((string)$subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $loops Number of times a locked resource is tried to be acquired.
|
||||
* @param int $step Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock.
|
||||
*/
|
||||
public function init($loops = 0, $step = 0)
|
||||
{
|
||||
$this->loops = (int)$loops;
|
||||
$this->step = (int)$step;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor:
|
||||
* Releases lock automatically when instance is destroyed and release resources
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->release();
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the lock
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
if (!$this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$success = true;
|
||||
if (
|
||||
GeneralUtility::isAllowedAbsPath($this->filePath)
|
||||
&& str_starts_with($this->filePath, Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER)
|
||||
) {
|
||||
if (@unlink($this->filePath) === false) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->isAcquired = false;
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of this lock
|
||||
*
|
||||
* @return bool Returns TRUE if lock is acquired by this locker, FALSE otherwise
|
||||
*/
|
||||
public function isAcquired()
|
||||
{
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int LOCK_CAPABILITY_* elements combined with bit-wise OR
|
||||
*/
|
||||
public static function getCapabilities()
|
||||
{
|
||||
return self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_NOBLOCK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to acquire a lock
|
||||
*
|
||||
* @param int $mode LOCK_CAPABILITY_EXCLUSIVE or self::LOCK_CAPABILITY_NOBLOCK
|
||||
* @return bool Returns TRUE if the lock was acquired successfully
|
||||
* @throws LockAcquireWouldBlockException
|
||||
*/
|
||||
public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
|
||||
{
|
||||
if ($this->isAcquired) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (file_exists($this->filePath)) {
|
||||
$maxExecutionTime = (int)ini_get('max_execution_time');
|
||||
$maxAge = time() - ($maxExecutionTime ?: 120);
|
||||
if (@filectime($this->filePath) < $maxAge) {
|
||||
// Remove stale lock file
|
||||
@unlink($this->filePath);
|
||||
}
|
||||
}
|
||||
|
||||
$this->isAcquired = false;
|
||||
$wouldBlock = false;
|
||||
for ($i = 0; $i < $this->loops; $i++) {
|
||||
$filePointer = @fopen($this->filePath, 'x');
|
||||
if ($filePointer !== false) {
|
||||
fclose($filePointer);
|
||||
GeneralUtility::fixPermissions($this->filePath);
|
||||
$this->isAcquired = true;
|
||||
break;
|
||||
}
|
||||
if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
|
||||
$wouldBlock = true;
|
||||
break;
|
||||
}
|
||||
usleep($this->step * 1000);
|
||||
}
|
||||
|
||||
if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->isAcquired && $wouldBlock) {
|
||||
throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1460976403);
|
||||
}
|
||||
|
||||
return $this->isAcquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Returns a priority for the method. 0 to 100, 100 is highest
|
||||
*/
|
||||
public static function getPriority()
|
||||
{
|
||||
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
|
||||
?? self::DEFAULT_PRIORITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the resource associated with the lock
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
@unlink($this->filePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user