forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add a new locking framework that uses flock. (librenms#6858)
Change locks to use flock, as pid checking is not sufficient when PID Namespaces are involved.
- Loading branch information
1 parent
f02b551
commit 701fbbc
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/** | ||
* LibreNMS | ||
* | ||
* This file is part of LibreNMS. | ||
* | ||
* @package LibreNMS | ||
* @subpackage FileLock | ||
* @copyright (C) 2017 | ||
* | ||
*/ | ||
|
||
namespace LibreNMS; | ||
|
||
class FileLock | ||
{ | ||
private $name = ""; | ||
private $file = ""; | ||
/** | ||
* @var resource | false | ||
*/ | ||
private $handle = false; | ||
private $acquired = false; | ||
|
||
private function __construct($lock_name) | ||
{ | ||
global $config; | ||
|
||
$this->name = $lock_name; | ||
$this->file = "$config[install_dir]/.$lock_name.lock"; | ||
$this->handle = fopen($this->file, "w+"); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->release(); | ||
} | ||
|
||
/** | ||
* Release the lock. | ||
*/ | ||
public function release() | ||
{ | ||
if (!$this->acquired) { | ||
return; | ||
} | ||
|
||
if ($this->handle !== false) { | ||
flock($this->handle, LOCK_UN); | ||
fclose($this->handle); | ||
} | ||
if (file_exists($this->file)) { | ||
unlink($this->file); | ||
} | ||
} | ||
|
||
/** | ||
* Given a lock name, try to acquire the lock. | ||
* On success return a FileLock object, or on failure return false. | ||
* @param string $lock_name Name of lock | ||
* @return mixed | ||
*/ | ||
public static function lock($lock_name) | ||
{ | ||
$lock = new self($lock_name); | ||
if ($lock->handle === false) { | ||
return false; | ||
} | ||
|
||
if (flock($lock->handle, LOCK_EX | LOCK_NB)) { | ||
$lock->acquired = true; | ||
return $lock; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Given a lock name, try to acquire the lock, exiting on failure. | ||
* On success return a FileLock object. | ||
* @param string $lock_name Name of lock | ||
* @return FileLock | ||
*/ | ||
public static function lockOrDie($lock_name) | ||
{ | ||
$lock = self::lock($lock_name); | ||
|
||
if ($lock === false) { | ||
echo "Failed to acquire lock $lock_name, exiting\n"; | ||
exit(1); | ||
} | ||
return $lock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters