3 * Simple lock server daemon that accepts lock/unlock requests.
5 * This code should not require MediaWiki setup or PHP files.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup LockManager Maintenance
26 if ( php_sapi_name() !== 'cli' ) {
27 die( "This is not a valid entry point.\n" );
29 error_reporting( E_ALL
);
33 LockServerDaemon
::init(
35 'address:', 'port:', 'authKey:',
36 'lockTimeout::', 'maxClients::', 'maxBacklog::', 'maxLocks::',
41 * Simple lock server daemon that accepts lock/unlock requests
43 * @ingroup LockManager Maintenance
45 class LockServerDaemon
{
47 protected $sock; // socket to listen/accept on
49 protected $sessions = array(); // (session => resource)
51 protected $deadSessions = array(); // (session => UNIX timestamp)
53 /** @var LockHolder */
54 protected $lockHolder;
56 protected $address; // string IP address
57 protected $port; // integer
58 protected $authKey; // string key
59 protected $lockTimeout; // integer number of seconds
60 protected $maxBacklog; // integer
61 protected $maxClients; // integer
63 protected $startTime; // integer UNIX timestamp
64 protected $ticks = 0; // integer counter
66 /* @var LockServerDaemon */
67 protected static $instance = null;
70 * @params $config Array
71 * @return LockServerDaemon
73 public static function init( array $config ) {
74 if ( self
::$instance ) {
75 throw new Exception( 'LockServer already initialized.' );
77 foreach ( array( 'address', 'port', 'authKey' ) as $par ) {
78 if ( !isset( $config[$par] ) ) {
79 die( "Usage: php LockServerDaemon.php " .
80 "--address <address> --port <port> --authkey <key> " .
81 "[--lockTimeout <seconds>] " .
82 "[--maxLocks <integer>] [--maxClients <integer>] [--maxBacklog <integer>]"
86 self
::$instance = new self( $config );
87 return self
::$instance;
91 * @params $config Array
93 protected function __construct( array $config ) {
94 // Required parameters...
95 $this->address
= $config['address'];
96 $this->port
= $config['port'];
97 $this->authKey
= $config['authKey'];
98 // Parameters with defaults...
99 $this->lockTimeout
= isset( $config['lockTimeout'] )
100 ?
(int)$config['lockTimeout']
102 $this->maxClients
= isset( $config['maxClients'] )
103 ?
(int)$config['maxClients']
104 : 1000; // less than default FD_SETSIZE
105 $this->maxBacklog
= isset( $config['maxBacklog'] )
106 ?
(int)$config['maxBacklog']
108 $maxLocks = isset( $config['maxLocks'] )
109 ?
(int)$config['maxLocks']
112 $this->lockHolder
= new LockHolder( $maxLocks );
118 protected function setupServerSocket() {
119 if ( !function_exists( 'socket_create' ) ) {
120 throw new Exception( "PHP sockets extension missing from PHP CLI mode." );
122 $sock = socket_create( AF_INET
, SOCK_STREAM
, SOL_TCP
);
123 if ( $sock === false ) {
124 throw new Exception( "socket_create(): " . socket_strerror( socket_last_error() ) );
126 socket_set_option( $sock, SOL_SOCKET
, SO_REUSEADDR
, 1 ); // bypass 2MLS
127 socket_set_nonblock( $sock ); // don't block on accept()
128 if ( socket_bind( $sock, $this->address
, $this->port
) === false ) {
129 throw new Exception( "socket_bind(): " .
130 socket_strerror( socket_last_error( $sock ) ) );
131 } elseif ( socket_listen( $sock, $this->maxBacklog
) === false ) {
132 throw new Exception( "socket_listen(): " .
133 socket_strerror( socket_last_error( $sock ) ) );
136 $this->startTime
= time();
140 * Entry-point function that listens to the server socket, accepts
141 * new clients, and recieves/responds to requests to lock resources.
143 public function main() {
144 $this->setupServerSocket(); // setup listening socket
145 $socketArray = new SocketArray(); // sockets being serviced
146 $socketArray->addSocket( $this->sock
); // add listening socket
148 list( $read, $write ) = $socketArray->socketsForSelect();
149 if ( socket_select( $read, $write, $except = NULL, NULL ) < 1 ) {
152 // Check if there is a client trying to connect...
153 if ( in_array( $this->sock
, $read ) && $socketArray->size() < $this->maxClients
) {
154 $newSock = socket_accept( $this->sock
);
156 socket_set_option( $newSock, SOL_SOCKET
, SO_KEEPALIVE
, 1 );
157 socket_set_nonblock( $newSock ); // don't block on read()/write()
158 $socketArray->addSocket( $newSock );
161 // Loop through all the clients that have data to read...
162 foreach ( $read as $read_sock ) {
163 if ( $read_sock === $this->sock
) {
164 continue; // skip listening socket
166 // Avoids PHP_NORMAL_READ per https://bugs.php.net/bug.php?id=33471
167 $data = socket_read( $read_sock, 65535 );
168 // Check if the client is disconnected
169 if ( $data === false ||
$data === '' ) {
170 $socketArray->closeSocket( $read_sock );
171 $this->recordDeadSocket( $read_sock ); // remove session
172 // Check if we reached the end of a message
173 } elseif ( substr( $data, -1 ) === "\n" ) {
174 // Newline is the last char (given ping-pong message usage)
175 $cmd = $socketArray->readRcvBuffer( $read_sock ) . $data;
176 // Perform the requested command...
177 $response = $this->doCommand( rtrim( $cmd ), $read_sock );
178 // Send the response to the client...
179 $socketArray->appendSndBuffer( $read_sock, $response . "\n" );
180 // Otherwise, we just have more message data to append
181 } elseif ( !$socketArray->appendRcvBuffer( $read_sock, $data ) ) {
182 $socketArray->closeSocket( $read_sock ); // too big
183 $this->recordDeadSocket( $read_sock ); // remove session
186 // Loop through all the clients that have data to write...
187 foreach ( $write as $write_sock ) {
188 $bytes = socket_write( $write_sock, $socketArray->readSndBuffer( $write_sock ) );
189 // Check if the client is disconnected
190 if ( $bytes === false ) {
191 $socketArray->closeSocket( $write_sock );
192 $this->recordDeadSocket( $write_sock ); // remove session
193 // Otherwise, truncate these bytes from the start of the write buffer
195 $socketArray->consumeSndBuffer( $write_sock, $bytes );
198 // Prune dead locks every few socket events...
199 if ( ++
$this->ticks
>= 9 ) {
201 $this->purgeExpiredLocks();
207 * @param $data string
208 * @param $sourceSock resource
211 protected function doCommand( $data, $sourceSock ) {
212 $cmdArr = $this->getCommand( $data );
213 if ( is_string( $cmdArr ) ) {
214 return $cmdArr; // error
216 list( $function, $session, $type, $resources ) = $cmdArr;
217 // On first command, track the session => sock correspondence
218 if ( !isset( $this->sessions
[$session] ) ) {
219 $this->sessions
[$session] = $sourceSock;
220 unset( $this->deadSessions
[$session] ); // renew if dead
222 if ( $function === 'ACQUIRE' ) {
223 return $this->lockHolder
->lock( $session, $type, $resources );
224 } elseif ( $function === 'RELEASE' ) {
225 return $this->lockHolder
->unlock( $session, $type, $resources );
226 } elseif ( $function === 'RELEASE_ALL' ) {
227 return $this->lockHolder
->release( $session );
228 } elseif ( $function === 'STAT' ) {
229 return $this->stat();
231 return 'INTERNAL_ERROR';
235 * @param $data string
238 protected function getCommand( $data ) {
239 $m = explode( ':', $data ); // <session, key, command, type, values>
240 if ( count( $m ) == 5 ) {
241 list( $session, $key, $command, $type, $values ) = $m;
242 if ( sha1( $session . $command . $type . $values . $this->authKey
) !== $key ) {
244 } elseif ( strlen( $session ) !== 32 ) {
245 return 'BAD_SESSION';
247 $values = explode( '|', $values );
248 if ( $command === 'ACQUIRE' ) {
249 $needsLockArgs = true;
250 } elseif ( $command === 'RELEASE' ) {
251 $needsLockArgs = true;
252 } elseif ( $command === 'RELEASE_ALL' ) {
253 $needsLockArgs = false;
254 } elseif ( $command === 'STAT' ) {
255 $needsLockArgs = false;
257 return 'BAD_COMMAND';
259 if ( $needsLockArgs ) {
260 if ( $type !== 'SH' && $type !== 'EX' ) {
263 foreach ( $values as $value ) {
264 if ( strlen( $value ) !== 31 ) {
269 return array( $command, $session, $type, $values );
275 * Remove a socket's corresponding session from tracking and
276 * store it in the dead session tracking if it still has locks.
278 * @param $socket resource
281 protected function recordDeadSocket( $socket ) {
282 $session = array_search( $socket, $this->sessions
);
283 if ( $session !== false ) {
284 unset( $this->sessions
[$session] );
285 // Record recently killed sessions that still have locks
286 if ( $this->lockHolder
->sessionHasLocks( $session ) ) {
287 $this->deadSessions
[$session] = time();
295 * Clear locks for sessions that have been dead for a while
297 * @return integer Number of sessions purged
299 protected function purgeExpiredLocks() {
302 foreach ( $this->deadSessions
as $session => $timestamp ) {
303 if ( ( $now - $timestamp ) > $this->lockTimeout
) {
304 $this->lockHolder
->release( $session );
305 unset( $this->deadSessions
[$session] );
313 * Get the current timestamp and memory usage
317 protected function stat() {
318 return ( time() - $this->startTime
) . ':' . memory_get_usage();
323 * LockServerDaemon helper class that keeps track socket states
327 protected $clients = array(); // array of client sockets
329 protected $rBuffers = array(); // corresponding socket read buffers
331 protected $wBuffers = array(); // corresponding socket write buffers
333 const BUFFER_SIZE
= 65535;
336 * @return Array (list of sockets to read, list of sockets to write)
338 public function socketsForSelect() {
341 foreach ( $this->clients
as $key => $socket ) {
342 if ( $this->wBuffers
[$key] !== '' ) {
343 $wSockets[] = $socket; // wait for writing to unblock
345 $rSockets[] = $socket; // wait for reading to unblock
348 return array( $rSockets, $wSockets );
352 * @return integer Number of client sockets
354 public function size() {
355 return count( $this->clients
);
359 * @param $sock resource
362 public function addSocket( $sock ) {
363 $this->clients
[] = $sock;
364 $this->rBuffers
[] = '';
365 $this->wBuffers
[] = '';
370 * @param $sock resource
373 public function closeSocket( $sock ) {
374 $key = array_search( $sock, $this->clients
);
375 if ( $key === false ) {
378 socket_close( $sock );
379 unset( $this->clients
[$key] );
380 unset( $this->rBuffers
[$key] );
381 unset( $this->wBuffers
[$key] );
386 * @param $sock resource
387 * @param $data string
390 public function appendRcvBuffer( $sock, $data ) {
391 $key = array_search( $sock, $this->clients
);
392 if ( $key === false ) {
394 } elseif ( ( strlen( $this->rBuffers
[$key] ) +
strlen( $data ) ) > self
::BUFFER_SIZE
) {
397 $this->rBuffers
[$key] .= $data;
402 * @param $sock resource
403 * @return string|bool
405 public function readRcvBuffer( $sock ) {
406 $key = array_search( $sock, $this->clients
);
407 if ( $key === false ) {
410 $data = $this->rBuffers
[$key];
411 $this->rBuffers
[$key] = ''; // consume data
416 * @param $sock resource
417 * @param $data string
420 public function appendSndBuffer( $sock, $data ) {
421 $key = array_search( $sock, $this->clients
);
422 if ( $key === false ) {
424 } elseif ( ( strlen( $this->wBuffers
[$key] ) +
strlen( $data ) ) > self
::BUFFER_SIZE
) {
427 $this->wBuffers
[$key] .= $data;
432 * @param $sock resource
435 public function readSndBuffer( $sock ) {
436 $key = array_search( $sock, $this->clients
);
437 if ( $key === false ) {
440 return $this->wBuffers
[$key];
444 * @param $sock resource
445 * @param $bytes integer
448 public function consumeSndBuffer( $sock, $bytes ) {
449 $key = array_search( $sock, $this->clients
);
450 if ( $key === false ) {
453 $this->wBuffers
[$key] = (string)substr( $this->wBuffers
[$key], $bytes );
459 * LockServerDaemon helper class that keeps track of the locks
463 protected $shLocks = array(); // (key => session => 1)
465 protected $exLocks = array(); // (key => session)
468 protected $sessionIndexSh = array(); // (session => key => 1)
470 protected $sessionIndexEx = array(); // (session => key => 1)
471 protected $lockCount = 0; // integer
473 protected $maxLocks; // integer
476 * @params $maxLocks integer Maximum number of locks to allow
478 public function __construct( $maxLocks ) {
479 $this->maxLocks
= $maxLocks;
483 * @param $session string
486 public function sessionHasLocks( $session ) {
487 return isset( $this->sessionIndexSh
[$session] )
488 ||
isset( $this->sessionIndexEx
[$session] );
492 * @param $session string
493 * @param $type string
497 public function lock( $session, $type, array $keys ) {
498 if ( ( $this->lockCount +
count( $keys ) ) > $this->maxLocks
) {
499 return 'TOO_MANY_LOCKS';
501 if ( $type === 'SH' ) {
502 // Check if any keys are already write-locked...
503 foreach ( $keys as $key ) {
504 if ( isset( $this->exLocks
[$key] ) && $this->exLocks
[$key] !== $session ) {
505 return 'CANT_ACQUIRE';
508 // Acquire the read-locks...
509 foreach ( $keys as $key ) {
510 $this->set_sh_lock( $key, $session );
513 } elseif ( $type === 'EX' ) {
514 // Check if any keys are already read-locked or write-locked...
515 foreach ( $keys as $key ) {
516 if ( isset( $this->exLocks
[$key] ) && $this->exLocks
[$key] !== $session ) {
517 return 'CANT_ACQUIRE';
519 if ( isset( $this->shLocks
[$key] ) ) {
520 foreach ( $this->shLocks
[$key] as $otherSession => $x ) {
521 if ( $otherSession !== $session ) {
522 return 'CANT_ACQUIRE';
527 // Acquire the write-locks...
528 foreach ( $keys as $key ) {
529 $this->set_ex_lock( $key, $session );
533 return 'INTERNAL_ERROR';
537 * @param $session string
538 * @param $type string
542 public function unlock( $session, $type, array $keys ) {
543 if ( $type === 'SH' ) {
544 foreach ( $keys as $key ) {
545 $this->unset_sh_lock( $key, $session );
548 } elseif ( $type === 'EX' ) {
549 foreach ( $keys as $key ) {
550 $this->unset_ex_lock( $key, $session );
554 return 'INTERNAL_ERROR';
558 * @param $session string
561 public function release( $session ) {
562 if ( isset( $this->sessionIndexSh
[$session] ) ) {
563 foreach ( $this->sessionIndexSh
[$session] as $key => $x ) {
564 $this->unset_sh_lock( $key, $session );
567 if ( isset( $this->sessionIndexEx
[$session] ) ) {
568 foreach ( $this->sessionIndexEx
[$session] as $key => $x ) {
569 $this->unset_ex_lock( $key, $session );
572 return 'RELEASED_ALL';
577 * @param $session string
580 protected function set_sh_lock( $key, $session ) {
581 if ( !isset( $this->shLocks
[$key][$session] ) ) {
582 $this->shLocks
[$key][$session] = 1;
583 $this->sessionIndexSh
[$session][$key] = 1;
584 ++
$this->lockCount
; // we are adding a lock
590 * @param $session string
593 protected function set_ex_lock( $key, $session ) {
594 if ( !isset( $this->exLocks
[$key][$session] ) ) {
595 $this->exLocks
[$key] = $session;
596 $this->sessionIndexEx
[$session][$key] = 1;
597 ++
$this->lockCount
; // we are adding a lock
603 * @param $session string
606 protected function unset_sh_lock( $key, $session ) {
607 if ( isset( $this->shLocks
[$key][$session] ) ) {
608 unset( $this->shLocks
[$key][$session] );
609 if ( !count( $this->shLocks
[$key] ) ) {
610 unset( $this->shLocks
[$key] );
612 unset( $this->sessionIndexSh
[$session][$key] );
613 if ( !count( $this->sessionIndexSh
[$session] ) ) {
614 unset( $this->sessionIndexSh
[$session] );
622 * @param $session string
625 protected function unset_ex_lock( $key, $session ) {
626 if ( isset( $this->exLocks
[$key] ) && $this->exLocks
[$key] === $session ) {
627 unset( $this->exLocks
[$key] );
628 unset( $this->sessionIndexEx
[$session][$key] );
629 if ( !count( $this->sessionIndexEx
[$session] ) ) {
630 unset( $this->sessionIndexEx
[$session] );