1 |
#!/usr/bin/perl -w |
2 |
############################################################################### |
3 |
# Meteor |
4 |
# An HTTP server for the 2.0 web |
5 |
# Copyright (c) 2006 contributing authors |
6 |
# |
7 |
# The Meteor daemon |
8 |
# |
9 |
# Main program should call Meteor::Config::setCommandLineParameters(@ARGV),. |
10 |
# Afterwards anybody can access $::CONF{<parameterName>}, where |
11 |
# <parameterName> is any valid parameter (except 'Help') listed in the |
12 |
# @DEFAULTS array below. |
13 |
# |
14 |
############################################################################### |
15 |
# |
16 |
# This program is free software; you can redistribute it and/or modify it |
17 |
# under the terms of the GNU General Public License as published by the Free |
18 |
# Software Foundation; either version 2 of the License, or (at your option) |
19 |
# any later version. |
20 |
# |
21 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
22 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
24 |
# more details. |
25 |
# |
26 |
# You should have received a copy of the GNU General Public License along |
27 |
# with this program; if not, write to the Free Software Foundation, Inc., |
28 |
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
29 |
# |
30 |
# For more information visit www.meteorserver.org |
31 |
# |
32 |
############################################################################### |
33 |
|
34 |
############################################################################### |
35 |
# meterod version |
36 |
################################################################################ |
37 |
|
38 |
$::VERSION='1.05.01'; |
39 |
|
40 |
############################################################################### |
41 |
# Configuration |
42 |
############################################################################### |
43 |
|
44 |
use strict; |
45 |
|
46 |
use Meteor::Syslog; |
47 |
|
48 |
use Meteor::Socket; |
49 |
use Meteor::Connection; |
50 |
use Meteor::Controller; |
51 |
use Meteor::Subscriber; |
52 |
use Meteor::Channel; |
53 |
use Meteor::Document; |
54 |
use Meteor::Config; |
55 |
|
56 |
$::CRLF="\r\n"; # Line separator to be used throughout all modules |
57 |
|
58 |
our $CONTROL_QUEUE_SIZE=5; |
59 |
our $SUBSCRIBER_QUEUE_SIZE=20; |
60 |
|
61 |
our $MAIN_LOOP_TIMEOUT=60; |
62 |
our $AGE_CHECK_INTERVALL=60; |
63 |
|
64 |
our $MAX_EXIT_DELAY=120; |
65 |
|
66 |
############################################################################### |
67 |
# Main |
68 |
############################################################################### |
69 |
|
70 |
# |
71 |
# Record startup time |
72 |
# |
73 |
$::STARTUP_TIME=time; |
74 |
$::STARTUP_TIME+=0; # avoid warning |
75 |
|
76 |
# |
77 |
# Program name |
78 |
# |
79 |
$::PGM=$0; |
80 |
$::PGM=~s/^.*\///; |
81 |
|
82 |
# |
83 |
# Handle command line options and config file |
84 |
# |
85 |
Meteor::Config->setCommandLineParameters(@ARGV); |
86 |
|
87 |
# |
88 |
# Do something about warn and die |
89 |
# |
90 |
unless($::CONF{'Debug'}) |
91 |
{ |
92 |
$SIG{'__WARN__'}=\&Meteor::Syslog::myWarn; |
93 |
$SIG{'__DIE__'}=\&Meteor::Syslog::myDie; |
94 |
} |
95 |
|
96 |
&::syslog('info',"$::PGM launched!"); |
97 |
|
98 |
# |
99 |
# Daemonize |
100 |
# |
101 |
{ |
102 |
$0="$::PGM daemon"; |
103 |
|
104 |
unless($::CONF{'Debug'}) |
105 |
{ |
106 |
# close standard file descriptors |
107 |
close(STDIN); |
108 |
close(STDOUT); |
109 |
close(STDERR); |
110 |
chdir("/"); |
111 |
umask(0); |
112 |
# fork and exit parent |
113 |
exit if fork; |
114 |
setpgrp(0, $$) if defined $SIG{TTOU}; |
115 |
$SIG{TTOU}='ignore' if defined $SIG{TTOU}; |
116 |
|
117 |
# Avoid 'stdin reopened for output' warning with newer perls |
118 |
open(NULL,'/dev/null'); |
119 |
<NULL> if(0); |
120 |
|
121 |
open(OUT,">/var/run/$::PGM.pid"); |
122 |
print OUT "$$\n"; |
123 |
close(OUT); |
124 |
} |
125 |
else |
126 |
{ |
127 |
print "$::PGM PID: $$\n"; |
128 |
} |
129 |
} |
130 |
|
131 |
# |
132 |
# Signal handlers |
133 |
# |
134 |
$::HUP=$::TERM=$::USR1=$::USR2=0; |
135 |
$SIG{'HUP'}=sub{$::HUP=1}; |
136 |
$SIG{'TERM'}=sub{$::TERM=1}; |
137 |
$SIG{'USR1'}=sub{$::USR1=1}; |
138 |
$SIG{'USR2'}=sub{$::USR2=1}; |
139 |
|
140 |
# |
141 |
# Run server |
142 |
# |
143 |
my $con_counter=0; |
144 |
my $con; |
145 |
|
146 |
my $controlServer=Meteor::Socket->newServer( |
147 |
$::CONF{'ControllerPort'}, |
148 |
$CONTROL_QUEUE_SIZE, |
149 |
$::CONF{'ControllerIP'} |
150 |
); |
151 |
my $controlServerFN=$controlServer->fileno(); |
152 |
|
153 |
my $subscriberServer=Meteor::Socket->newServer( |
154 |
$::CONF{'SubscriberPort'}, |
155 |
$SUBSCRIBER_QUEUE_SIZE, |
156 |
$::CONF{'SubscriberIP'} |
157 |
); |
158 |
my $subscriberServerFN=$subscriberServer->fileno(); |
159 |
|
160 |
my $serverVector=''; |
161 |
vec($serverVector,$controlServerFN,1)=1; |
162 |
vec($serverVector,$subscriberServerFN,1)=1; |
163 |
|
164 |
my $lastAgeCheck=time; |
165 |
|
166 |
my $nextPing=undef; |
167 |
if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2) |
168 |
{ |
169 |
$nextPing=$::CONF{'PingInterval'}+$lastAgeCheck; |
170 |
} |
171 |
|
172 |
while(!$::TERM) |
173 |
{ |
174 |
eval |
175 |
{ |
176 |
while(!$::TERM) |
177 |
{ |
178 |
my $rVec=$serverVector; |
179 |
my $wVec=''; |
180 |
my $eVec=''; |
181 |
|
182 |
my $rout; |
183 |
my $wout; |
184 |
my $eout; |
185 |
|
186 |
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
187 |
|
188 |
my $timeout=$MAIN_LOOP_TIMEOUT; |
189 |
if(defined($nextPing)) |
190 |
{ |
191 |
$timeout=$nextPing-time; |
192 |
} |
193 |
|
194 |
my $result=0; |
195 |
if($timeout>0) |
196 |
{ |
197 |
$result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout); |
198 |
} |
199 |
|
200 |
if($result>0) |
201 |
{ |
202 |
if(vec($rout,$controlServerFN,1)) |
203 |
{ |
204 |
Meteor::Controller->newFromServer($controlServer); |
205 |
} |
206 |
if(vec($rout,$subscriberServerFN,1)) |
207 |
{ |
208 |
Meteor::Subscriber->newFromServer($subscriberServer); |
209 |
} |
210 |
|
211 |
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
212 |
} |
213 |
elsif($result<0) |
214 |
{ |
215 |
&::syslog('crit',"Select failed: $!"); |
216 |
sleep(30); |
217 |
} |
218 |
|
219 |
if($::HUP) |
220 |
{ |
221 |
$::HUP=0; |
222 |
|
223 |
&::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!"); |
224 |
|
225 |
Meteor::Config->readConfig(); |
226 |
Meteor::Config->updateConfig(); |
227 |
|
228 |
Meteor::Document->clearDocuments() |
229 |
} |
230 |
|
231 |
if($::USR1) |
232 |
{ |
233 |
$::USR1=0; |
234 |
|
235 |
&::syslog('info',"Received SIGUSR1, clearing channel buffers!"); |
236 |
|
237 |
Meteor::Channel->clearAllBuffers(); |
238 |
} |
239 |
|
240 |
if($::USR2) |
241 |
{ |
242 |
$::USR2=0; |
243 |
|
244 |
&::syslog('info',"Received SIGUSR2, clearing document cache!"); |
245 |
|
246 |
Meteor::Document->clearDocuments() |
247 |
} |
248 |
|
249 |
my $t=time; |
250 |
if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL) |
251 |
{ |
252 |
my $minTimeStap=time-$::CONF{'MaxMessageAge'}; |
253 |
Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap); |
254 |
$lastAgeCheck=time; |
255 |
$t=$lastAgeCheck; |
256 |
|
257 |
Meteor::Subscriber->checkPersistentConnectionsForMaxTime(); |
258 |
} |
259 |
|
260 |
if(defined($nextPing) && $nextPing<=$t) |
261 |
{ |
262 |
$nextPing=undef; |
263 |
|
264 |
Meteor::Subscriber->pingPersistentConnections(); |
265 |
|
266 |
if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2) |
267 |
{ |
268 |
$nextPing=$::CONF{'PingInterval'}+time; |
269 |
} |
270 |
} |
271 |
} |
272 |
}; |
273 |
unless($::TERM) |
274 |
{ |
275 |
&::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@"); |
276 |
sleep(2); |
277 |
} |
278 |
} |
279 |
|
280 |
# |
281 |
# Proper shutdown |
282 |
# |
283 |
if($::TERM) |
284 |
{ |
285 |
&::syslog('info',"Received SIGTERM, begin shutdown!"); |
286 |
|
287 |
$subscriberServer->close(); |
288 |
$controlServer->close(); |
289 |
|
290 |
unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'}); |
291 |
|
292 |
Meteor::Connection->closeAllConnections(); |
293 |
|
294 |
my $timoutAt=time+$MAX_EXIT_DELAY; |
295 |
|
296 |
while(Meteor::Connection->connectionCount() && time<$timoutAt) |
297 |
{ |
298 |
my $rVec=''; |
299 |
my $wVec=''; |
300 |
my $eVec=''; |
301 |
|
302 |
my $rout; |
303 |
my $wout; |
304 |
my $eout; |
305 |
|
306 |
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
307 |
|
308 |
my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time); |
309 |
|
310 |
if($result>0) |
311 |
{ |
312 |
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
313 |
} |
314 |
} |
315 |
|
316 |
if(my $cnt=Meteor::Connection->connectionCount()) |
317 |
{ |
318 |
&::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway"); |
319 |
|
320 |
exit(1); |
321 |
} |
322 |
|
323 |
&::syslog('info',"shutdown succeeded"); |
324 |
|
325 |
exit(0); |
326 |
} |
327 |
|
328 |
&::syslog('emerg',"$::PGM loop exited"); |
329 |
|
330 |
1; |
331 |
############################################################################EOF |