3 * Simple version of LockManager based on using FS lock files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup LockManager
25 * Simple version of LockManager based on using FS lock files.
26 * All locks are non-blocking, which avoids deadlocks.
28 * This should work fine for small sites running off one server.
29 * Do not use this with 'lockDirectory' set to an NFS mount unless the
30 * NFS client is at least version 2.6.12. Otherwise, the BSD flock()
31 * locks will be ignored; see http://nfs.sourceforge.net/#section_d.
33 * @ingroup LockManager
36 class FSLockManager
extends LockManager
{
37 /** @var Array Mapping of lock types to the type actually used */
38 protected $lockTypeMap = array(
39 self
::LOCK_SH
=> self
::LOCK_SH
,
40 self
::LOCK_UW
=> self
::LOCK_SH
,
41 self
::LOCK_EX
=> self
::LOCK_EX
44 protected $lockDir; // global dir for all servers
46 /** @var Array Map of (locked key => lock type => lock file handle) */
47 protected $handles = array();
50 * Construct a new instance from configuration.
53 * 'lockDirectory' : Directory containing the lock files
55 * @param array $config
57 function __construct( array $config ) {
58 parent
::__construct( $config );
60 $this->lockDir
= $config['lockDirectory'];
64 * @see LockManager::doLock()
69 protected function doLock( array $paths, $type ) {
70 $status = Status
::newGood();
72 $lockedPaths = array(); // files locked in this attempt
73 foreach ( $paths as $path ) {
74 $status->merge( $this->doSingleLock( $path, $type ) );
75 if ( $status->isOK() ) {
76 $lockedPaths[] = $path;
78 // Abort and unlock everything
79 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
88 * @see LockManager::doUnlock()
93 protected function doUnlock( array $paths, $type ) {
94 $status = Status
::newGood();
96 foreach ( $paths as $path ) {
97 $status->merge( $this->doSingleUnlock( $path, $type ) );
104 * Lock a single resource key
106 * @param $path string
107 * @param $type integer
110 protected function doSingleLock( $path, $type ) {
111 $status = Status
::newGood();
113 if ( isset( $this->locksHeld
[$path][$type] ) ) {
114 ++
$this->locksHeld
[$path][$type];
115 } elseif ( isset( $this->locksHeld
[$path][self
::LOCK_EX
] ) ) {
116 $this->locksHeld
[$path][$type] = 1;
118 wfSuppressWarnings();
119 $handle = fopen( $this->getLockPath( $path ), 'a+' );
121 if ( !$handle ) { // lock dir missing?
122 wfMkdirParents( $this->lockDir
);
123 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
126 // Either a shared or exclusive lock
127 $lock = ( $type == self
::LOCK_SH
) ? LOCK_SH
: LOCK_EX
;
128 if ( flock( $handle, $lock | LOCK_NB
) ) {
129 // Record this lock as active
130 $this->locksHeld
[$path][$type] = 1;
131 $this->handles
[$path][$type] = $handle;
134 $status->fatal( 'lockmanager-fail-acquirelock', $path );
137 $status->fatal( 'lockmanager-fail-openlock', $path );
145 * Unlock a single resource key
147 * @param $path string
148 * @param $type integer
151 protected function doSingleUnlock( $path, $type ) {
152 $status = Status
::newGood();
154 if ( !isset( $this->locksHeld
[$path] ) ) {
155 $status->warning( 'lockmanager-notlocked', $path );
156 } elseif ( !isset( $this->locksHeld
[$path][$type] ) ) {
157 $status->warning( 'lockmanager-notlocked', $path );
159 $handlesToClose = array();
160 --$this->locksHeld
[$path][$type];
161 if ( $this->locksHeld
[$path][$type] <= 0 ) {
162 unset( $this->locksHeld
[$path][$type] );
163 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
164 // actually add a handler, so check for handler existence.
165 if ( isset( $this->handles
[$path][$type] ) ) {
166 if ( $type === self
::LOCK_EX
167 && isset( $this->locksHeld
[$path][self
::LOCK_SH
] )
168 && !isset( $this->handles
[$path][self
::LOCK_SH
] ) )
170 // EX lock came first: move this handle to the SH one
171 $this->handles
[$path][self
::LOCK_SH
] = $this->handles
[$path][$type];
173 // Mark this handle to be unlocked and closed
174 $handlesToClose[] = $this->handles
[$path][$type];
176 unset( $this->handles
[$path][$type] );
179 // Unlock handles to release locks and delete
180 // any lock files that end up with no locks on them...
181 if ( wfIsWindows() ) {
182 // Windows: for any process, including this one,
183 // calling unlink() on a locked file will fail
184 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
185 $status->merge( $this->pruneKeyLockFiles( $path ) );
187 // Unix: unlink() can be used on files currently open by this
188 // process and we must do so in order to avoid race conditions
189 $status->merge( $this->pruneKeyLockFiles( $path ) );
190 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
198 * @param $path string
199 * @param $handlesToClose array
202 private function closeLockHandles( $path, array $handlesToClose ) {
203 $status = Status
::newGood();
204 foreach ( $handlesToClose as $handle ) {
205 if ( !flock( $handle, LOCK_UN
) ) {
206 $status->fatal( 'lockmanager-fail-releaselock', $path );
208 if ( !fclose( $handle ) ) {
209 $status->warning( 'lockmanager-fail-closelock', $path );
216 * @param $path string
219 private function pruneKeyLockFiles( $path ) {
220 $status = Status
::newGood();
221 if ( !count( $this->locksHeld
[$path] ) ) {
222 # No locks are held for the lock file anymore
223 if ( !unlink( $this->getLockPath( $path ) ) ) {
224 $status->warning( 'lockmanager-fail-deletelock', $path );
226 unset( $this->locksHeld
[$path] );
227 unset( $this->handles
[$path] );
233 * Get the path to the lock file for a key
234 * @param $path string
237 protected function getLockPath( $path ) {
238 $hash = self
::sha1Base36( $path );
239 return "{$this->lockDir}/{$hash}.lock";
242 function __destruct() {
243 // Make sure remaining locks get cleared for sanity
244 foreach ( $this->locksHeld
as $path => $locks ) {
245 $this->doSingleUnlock( $path, self
::LOCK_EX
);
246 $this->doSingleUnlock( $path, self
::LOCK_SH
);