‪TYPO3CMS  ‪main
SemaphoreLockStrategy.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
30 
31  public const ‪FILE_LOCK_FOLDER = 'lock/';
32  public const ‪DEFAULT_PRIORITY = 25;
33 
37  protected ‪$id;
38 
42  protected ‪$resource;
43 
47  protected ‪$filePath = '';
48 
52  protected ‪$isAcquired = false;
53 
58  public function ‪__construct($subject)
59  {
60  /*
61  * Tests if the directory for semaphore locks is available.
62  * If not, the directory will be created. The lock path is usually
63  * below typo3temp/var, typo3temp/var itself should exist already (or root-path/var/ respectively)
64  */
65  if (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
66  $path = ‪Environment::getProjectPath() . '/'
67  . trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
68  . '/';
69  } else {
71  }
72  if (!is_dir($path)) {
73  // Not using mkdir_deep on purpose here, if typo3temp/var itself
74  // does not exist, this issue should be solved on a different
75  // level of the application.
76  if (!‪GeneralUtility::mkdir($path)) {
77  throw new ‪LockCreateException('Cannot create directory ' . $path, 1460976250);
78  }
79  }
80  if (!is_writable($path)) {
81  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1460976320);
82  }
83  $this->filePath = $path . 'sem_' . md5((string)$subject);
84  touch($this->filePath);
85  $this->id = ftok($this->filePath, 'A');
86  if ($this->id === -1) {
87  throw new ‪LockCreateException('Cannot create key for semaphore using path ' . $this->filePath, 1396278734);
88  }
89  }
90 
94  public function ‪__destruct()
95  {
96  $this->‪release();
97  // We do not call sem_remove() since this would remove the resource for other processes,
98  // we leave that to the system. This is not clean, but there's no other way to determine when
99  // a semaphore is no longer needed as a website is generally running endlessly
100  // and we have no way to detect if there is a process currently waiting on that lock
101  // or if the server is shutdown
102  }
103 
109  public function ‪release()
110  {
111  if (!$this->‪isAcquired) {
112  return true;
113  }
114  $this->‪isAcquired = false;
115  return (bool)@sem_release($this->resource);
116  }
117 
123  public function ‪isAcquired()
124  {
125  return ‪$this->isAcquired;
126  }
127 
131  public static function ‪getCapabilities()
132  {
133  if (function_exists('sem_get')) {
135  }
136  return 0;
137  }
138 
146  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
147  {
148  if ($this->‪isAcquired) {
149  return true;
150  }
151 
152  ‪$resource = sem_get($this->id, 1);
153  if (‪$resource === false) {
154  throw new LockAcquireException('Unable to get semaphore with id ' . $this->id, 1313828196);
155  }
156  $this->resource = ‪$resource;
157 
158  $this->‪isAcquired = (bool)sem_acquire($this->resource);
159  return ‪$this->isAcquired;
160  }
161 
165  public static function ‪getPriority()
166  {
167  return ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
169  }
170 
174  public function ‪destroy()
175  {
176  if ($this->resource) {
177  sem_remove($this->resource);
178  @unlink($this->filePath);
179  }
180  }
181 }
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: SemaphoreLockStrategy.php:32
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$id
‪int $id
Definition: SemaphoreLockStrategy.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir(string $newFolder)
Definition: GeneralUtility.php:1633
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\getPriority
‪static int getPriority()
Definition: SemaphoreLockStrategy.php:161
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$isAcquired
‪bool $isAcquired
Definition: SemaphoreLockStrategy.php:48
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\__construct
‪__construct($subject)
Definition: SemaphoreLockStrategy.php:54
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:26
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_EXCLUSIVE
‪const LOCK_CAPABILITY_EXCLUSIVE
Definition: LockingStrategyInterface.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy
Definition: SemaphoreLockStrategy.php:28
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: SemaphoreLockStrategy.php:127
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\destroy
‪destroy()
Definition: SemaphoreLockStrategy.php:170
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$filePath
‪string $filePath
Definition: SemaphoreLockStrategy.php:44
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\$resource
‪resource SysvSemaphore null $resource
Definition: SemaphoreLockStrategy.php:40
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\__destruct
‪__destruct()
Definition: SemaphoreLockStrategy.php:90
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\release
‪bool release()
Definition: SemaphoreLockStrategy.php:105
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:23
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:23
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\isAcquired
‪bool isAcquired()
Definition: SemaphoreLockStrategy.php:119
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: SemaphoreLockStrategy.php:142
‪TYPO3\CMS\Core\Locking\SemaphoreLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SemaphoreLockStrategy.php:31