Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Flock based file locking factory.
  19   *
  20   * The file lock factory returns file locks locked with the flock function. Works OK, except on some
  21   * NFS, exotic shared storage and exotic server OSes (like windows). On windows, a second attempt to get a
  22   * lock will block indefinitely instead of timing out.
  23   *
  24   * @package    core
  25   * @category   lock
  26   * @copyright  Damyon Wiese 2013
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  namespace core\lock;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * Flock based file locking factory.
  36   *
  37   * The file lock factory returns file locks locked with the flock function. Works OK, except on some
  38   * NFS, exotic shared storage and exotic server OSes (like windows). On windows, a second attempt to get a
  39   * lock will block indefinitely instead of timing out.
  40   *
  41   * @package   core
  42   * @category  lock
  43   * @copyright Damyon Wiese 2013
  44   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class file_lock_factory implements lock_factory {
  47  
  48      /** @var string $type - The type of lock, e.g. cache, cron, session. */
  49      protected $type;
  50  
  51      /** @var string $lockdirectory - Full system path to the directory used to store file locks. */
  52      protected $lockdirectory;
  53  
  54      /** @var boolean $verbose - If true, debugging info about the owner of the lock will be written to the lock file. */
  55      protected $verbose;
  56  
  57      /**
  58       * Create this lock factory.
  59       *
  60       * @param string $type - The type, e.g. cron, cache, session
  61       */
  62      public function __construct($type) {
  63          global $CFG;
  64  
  65          $this->type = $type;
  66          if (!isset($CFG->file_lock_root)) {
  67              $this->lockdirectory = $CFG->dataroot . '/lock';
  68          } else {
  69              $this->lockdirectory = $CFG->file_lock_root;
  70          }
  71          $this->verbose = false;
  72          if ($CFG->debugdeveloper) {
  73              $this->verbose = true;
  74          }
  75      }
  76  
  77      /**
  78       * Return information about the blocking behaviour of the lock type on this platform.
  79       * @return boolean - False if attempting to get a lock will block indefinitely.
  80       */
  81      public function supports_timeout() {
  82          global $CFG;
  83  
  84          return $CFG->ostype !== 'WINDOWS';
  85      }
  86  
  87      /**
  88       * This lock type will be automatically released when a process ends.
  89       * @return boolean - True
  90       */
  91      public function supports_auto_release() {
  92          return true;
  93      }
  94  
  95      /**
  96       * Is available.
  97       * @return boolean - True if preventfilelocking is not set - or the file_lock_root is not in dataroot.
  98       */
  99      public function is_available() {
 100          global $CFG;
 101          $preventfilelocking = !empty($CFG->preventfilelocking);
 102          $lockdirisdataroot = true;
 103          if (!empty($CFG->file_lock_root) && strpos($CFG->file_lock_root, $CFG->dataroot) !== 0) {
 104              $lockdirisdataroot = false;
 105          }
 106          return !$preventfilelocking || !$lockdirisdataroot;
 107      }
 108  
 109      /**
 110       * Multiple locks for the same resource cannot be held from a single process.
 111       * @return boolean - False
 112       */
 113      public function supports_recursion() {
 114          return false;
 115      }
 116  
 117      /**
 118       * Get some info that might be useful for debugging.
 119       * @return boolean - string
 120       */
 121      protected function get_debug_info() {
 122          return 'host:' . php_uname('n') . ', pid:' . getmypid() . ', time:' . time();
 123      }
 124  
 125      /**
 126       * Get a lock within the specified timeout or return false.
 127       * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
 128       * @param int $timeout - The number of seconds to wait for a lock before giving up.
 129       * @param int $maxlifetime - Unused by this lock type.
 130       * @return boolean - true if a lock was obtained.
 131       */
 132      public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 133          $giveuptime = time() + $timeout;
 134  
 135          $hash = md5($this->type . '_' . $resource);
 136          $lockdir = $this->lockdirectory . '/' . substr($hash, 0, 2);
 137  
 138          if (!check_dir_exists($lockdir, true, true)) {
 139              return false;
 140          }
 141  
 142          $lockfilename = $lockdir . '/' . $hash;
 143  
 144          $filehandle = fopen($lockfilename, "wb");
 145  
 146          // Could not open the lock file.
 147          if (!$filehandle) {
 148              return false;
 149          }
 150  
 151          do {
 152              // Will block on windows. So sad.
 153              $wouldblock = false;
 154              $locked = flock($filehandle, LOCK_EX | LOCK_NB, $wouldblock);
 155              if (!$locked && $wouldblock && $timeout > 0) {
 156                  usleep(rand(10000, 250000)); // Sleep between 10 and 250 milliseconds.
 157              }
 158              // Try until the giveup time.
 159          } while (!$locked && $wouldblock && time() < $giveuptime);
 160  
 161          if (!$locked) {
 162              fclose($filehandle);
 163              return false;
 164          }
 165          if ($this->verbose) {
 166              fwrite($filehandle, $this->get_debug_info());
 167          }
 168          return new lock($filehandle, $this);
 169      }
 170  
 171      /**
 172       * Release a lock that was previously obtained with @lock.
 173       * @param lock $lock - A lock obtained from this factory.
 174       * @return boolean - true if the lock is no longer held (including if it was never held).
 175       */
 176      public function release_lock(lock $lock) {
 177          $handle = $lock->get_key();
 178  
 179          if (!$handle) {
 180              // We didn't have a lock.
 181              return false;
 182          }
 183  
 184          $result = flock($handle, LOCK_UN);
 185          fclose($handle);
 186          return $result;
 187      }
 188  
 189      /**
 190       * Extend a lock that was previously obtained with @lock.
 191       * @param lock $lock - not used
 192       * @param int $maxlifetime - not used
 193       * @return boolean - true if the lock was extended.
 194       */
 195      public function extend_lock(lock $lock, $maxlifetime = 86400) {
 196          // Not supported by this factory.
 197          return false;
 198      }
 199  
 200  }