-
Notifications
You must be signed in to change notification settings - Fork 1
/
indigo_daemon.php
146 lines (140 loc) · 3.63 KB
/
indigo_daemon.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?
class indigo_daemon
{
public $log_file;
public $pid_file;
private $quit;
public $daemon_name;
public function sig_handler($signal)
{
switch($signal)
{
case SIGTERM:
$this->quit=1;
write_log_file($this->daemon_name." sigterm");
exit;
case SIGKILL:
$this->quit=1;
write_log_file($this->daemon_name." killed");
exit;
case SIGINT:
$this->quit=1;
write_log_file($this->daemon_name." killed");
exit;
case SIGQUIT:
$this->quit=1;
write_log_file($this->daemon_name." shutdown");
exit;
case SIGSTOP:
$this->quit=1;
write_log_file($this->daemon_name." stop");
exit;
case SIGCHLD:
$this->quit=1;
write_log_file($this->daemon_name." stop");
exit;
}
}
public function init()
{
declare(ticks = 1);
gc_enable();
$pid_h=$this->open_pid_file($this->pid_file);
error_reporting(0);
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);
ob_implicit_flush ();
pcntl_signal(SIGCHLD,"sig_handler");
pcntl_signal(SIGKILL,"sig_handler");
pcntl_signal(SIGTERM,"sig_handler");
pcntl_signal(SIGINT,"sig_handler");
pcntl_signal(SIGQUIT,"sig_handler");
pcntl_signal(SIGSTOP,"sig_handler");
$pid = pcntl_fork();
if ($pid === -1)
{
die('Could not fork!');
}
elseif ($pid)
{
// this is the parent process
exit;
}
else
{
// this is the child process
chdir("/");
umask(0);
// reopen standard file descriptors
// this is necessary to decouple the daemon from the TTY
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
$STDIN = fopen('/dev/null', 'r');
$STDOUT = fopen('/dev/null', 'wb');
$STDERR = fopen('/dev/null', 'wb');
// make it session leader
posix_setsid();
chdir("/");
umask(0);
$pid_id=posix_getpid();
pcntl_signal(SIGCHLD, "signal_handler");
pcntl_signal(SIGTERM, "signal_handler");
pcntl_signal(SIGINT, "signal_handler");
fputs($pid_h,$pid_id);
fclose($pid_h);
$this->write_log_file($this->daemon_name." started");
}
}
public function shutdown()
{
$this->write_log_file($this->daemon_name." shutdown");
gc_disable();
}
public function write_log_file($text)
{
$fh = fopen($this->log_file, 'a') or die("can't open log file");
fwrite($fh, date("Y:m:d H:i", time())." ".$text." \n");
fclose($fh);
}
public function open_pid_file($file)
{
if(file_exists($file))
{
$fp = fopen($file,"r");
$pid = fgets($fp,1024);
fclose($fp);
if(posix_kill($pid,0))
{
print "Server already running with PID: $pid\n";
exit;
}
print "Removing PID file for defunct server process $pid\n";
if(!unlink($file))
{
print "Cannot unlink PID file $file\n";
exit;
}
}
if($fp = fopen($file,"w"))
{
return $fp;
}
else
{
print "Unable to open PID file $file for writing...\n";
exit;
}
}
public function infinite($useconds)
{
while($this->quit!=1)
{
flush();
ob_clean();
$this->start_process();
usleep($useconds);
}
}
}