-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcledd
executable file
·84 lines (68 loc) · 2.25 KB
/
dcledd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/php -q
<?php
/*
* DCLED - state tracker
* repeatedly calls dcled to display what's in the memory
* see: https://github.com/misel228/dcled-state-tracker
*/
require 'config.php';
declare(ticks = 1); #necessary to catch ctrl-c
// Catch Ctrl+C, kill and SIGTERM (Rollback)
pcntl_signal(SIGTERM, 'sigintShutdown');
pcntl_signal(SIGINT, 'sigintShutdown');
define('COMMAND_STRING', 'echo %MESSAGE% | dcled -p %PRE% ');
$placeholders = array('%PRE%', '%MESSAGE%');
define('SHORT_INTERVAL',5);
define('LONG_INTERVAL',15);
# Init: create a shared memory place and write an empty message into each of the MAX_MESSAGE slots
$shm_id = shmop_open(SHM_IDENTIFIER, "c", 0666, MAX_MESSAGE*MESSAGE_SIZE);
if($shm_id !== false){
for($i=1;$i<=MAX_MESSAGE;$i++) {
$shm_bytes_written = shmop_write($shm_id, EMPTY_MESSAGE, ($i-1)*MESSAGE_SIZE);
}
#close write access - is reopened read-only later
shmop_close($shm_id);
} else {
die('Failed at creating shared memory area');
}
# Loop: reopen area read-only and loop through the slots
$shm_id = shmop_open(SHM_IDENTIFIER, "a", 0666, MAX_MESSAGE*MESSAGE_SIZE);
if($shm_id !== false){
while(true){
for($i = 1; $i <= MAX_MESSAGE; $i++) {
$message = shmop_read($shm_id, ($i-1)*MESSAGE_SIZE, MESSAGE_SIZE);
if($message != EMPTY_MESSAGE) {
$time_stamp = substr($message,0,TIME_STAMP_SIZE);
if((time()-MAX_TIME) < $time_stamp ) {
$replacements = array($i, escapeshellarg(substr($message,TIME_STAMP_SIZE,MESSAGE_SIZE-TIME_STAMP_SIZE)));
$command = str_replace($placeholders,$replacements, COMMAND_STRING);
system($command);
#echo $command."\n";
sleep(SHORT_INTERVAL);
}
}
}
sleep(LONG_INTERVAL);
}
}
// remove memory area at end of program
function shutdown() {
global $shm_id;
shmop_delete($shm_id);
shmop_close($shm_id);
die('END OF PROGRAM'."\n");
}
/**
* Function, that is executed, if script has been killed by
* SIGINT: Ctrl+C
* SIGTERM: kill
*
* @param int $signal
*
* shamelessly plugged from. http://www.phpdevblog.net/2009/05/cli-catching-ctrlc-kill-commands-and-fatal-errors.html
*/
function sigintShutdown($signal) {
if ($signal === SIGINT || $signal === SIGTERM) {
shutdown();
}
}