Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
flock.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 /*
4  * @author Anakeen
5  * @package CORE
6 */
7 /**
8  * Mimic util-linux's flock behaviour to lock on a file and execute a command.
9  */
10 
11 main($argv);
12 
13 function usage($me)
14 {
15  $msg = <<<EOF
16 
17 Usage:
18 
19  $me <lockfile> <command>
20 
21 
22 EOF;
23  printerr($msg);
24 }
25 
26 function main(&$argv)
27 {
28  $me = array_shift($argv);
29  if (count($argv) < 2) {
30  usage($me);
31  exit(64);
32  }
33  $lockfile = array_shift($argv);
34  $fh = fopen($lockfile, 'c+');
35  if ($fh === false) {
36  printerr(sprintf("Error: cannot open lock file '%s'.\n", $lockfile));
37  exit(66);
38  }
39  if (flock($fh, LOCK_EX) === false) {
40  printerr("Error: failed to get lock.\n");
41  exit(1);
42  }
43  $cmd = join(' ', $argv);
44  passthru($cmd, $ret);
45  flock($fh, LOCK_UN);
46  exit($ret);
47 }
48 
49 function printerr($msg)
50 {
51  file_put_contents('php://stderr', $msg);
52 }
printerr($msg)
Definition: flock.php:49
$ret
foreach($argv as $arg) $cmd
usage($me)
Definition: flock.php:13
switch($command) exit
Definition: checkVault.php:46
main(&$argv)
Definition: flock.php:26
← centre documentaire © anakeen