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.04'; |
39 |
$::RELEASE_DATE='not yet released'; |
40 |
|
41 |
############################################################################### |
42 |
# Configuration |
43 |
############################################################################### |
44 |
|
45 |
use strict; |
46 |
|
47 |
use Meteor::Syslog; |
48 |
|
49 |
use Meteor::Socket; |
50 |
use Meteor::Connection; |
51 |
use Meteor::Controller; |
52 |
use Meteor::Subscriber; |
53 |
use Meteor::Channel; |
54 |
use Meteor::Document; |
55 |
use Meteor::Config; |
56 |
|
57 |
$::CRLF="\r\n"; # Line separator to be used throughout all modules |
58 |
|
59 |
our $CONTROL_QUEUE_SIZE=5; |
60 |
our $SUBSCRIBER_QUEUE_SIZE=20; |
61 |
|
62 |
our $MAIN_LOOP_TIMEOUT=60; |
63 |
our $AGE_CHECK_INTERVALL=60; |
64 |
|
65 |
our $MAX_EXIT_DELAY=120; |
66 |
|
67 |
############################################################################### |
68 |
# Main |
69 |
############################################################################### |
70 |
|
71 |
# |
72 |
# Record startup time |
73 |
# |
74 |
$::STARTUP_TIME=time; |
75 |
$::STARTUP_TIME+=0; # avoid warning |
76 |
|
77 |
# |
78 |
# Program name |
79 |
# |
80 |
$::PGM=$0; |
81 |
$::PGM=~s/^.*\///; |
82 |
|
83 |
# |
84 |
# Handle command line options and config file |
85 |
# |
86 |
Meteor::Config->setCommandLineParameters(@ARGV); |
87 |
|
88 |
# |
89 |
# Do something about warn and die |
90 |
# |
91 |
unless($::CONF{'Debug'}) |
92 |
{ |
93 |
$SIG{'__WARN__'}=\&Meteor::Syslog::myWarn; |
94 |
$SIG{'__DIE__'}=\&Meteor::Syslog::myDie; |
95 |
} |
96 |
|
97 |
&::syslog('info',"$::PGM launched!"); |
98 |
|
99 |
# |
100 |
# Daemonize |
101 |
# |
102 |
{ |
103 |
$0="$::PGM daemon"; |
104 |
|
105 |
my $facility=$::CONF{'SyslogFacility'} || $Meteor::Syslog::DEFAULT_FACILITY; |
106 |
|
107 |
unless($::CONF{'Debug'} || $facility eq 'none') |
108 |
{ |
109 |
# close standard file descriptors |
110 |
close(STDIN); |
111 |
close(STDOUT); |
112 |
close(STDERR); |
113 |
chdir("/"); |
114 |
umask(0); |
115 |
# fork and exit parent |
116 |
exit if fork; |
117 |
setpgrp(0, $$) if defined $SIG{TTOU}; |
118 |
$SIG{TTOU}='ignore' if defined $SIG{TTOU}; |
119 |
|
120 |
# Avoid 'stdin reopened for output' warning with newer perls |
121 |
open(NULL,'/dev/null'); |
122 |
<NULL> if(0); |
123 |
|
124 |
open(OUT,">/var/run/$::PGM.pid"); |
125 |
print OUT "$$\n"; |
126 |
close(OUT); |
127 |
} |
128 |
else |
129 |
{ |
130 |
&::syslog('info',"PID\t%s",$$); |
131 |
} |
132 |
} |
133 |
|
134 |
# |
135 |
# Signal handlers |
136 |
# |
137 |
$::HUP=$::TERM=$::USR1=$::USR2=0; |
138 |
$SIG{'HUP'}=sub{$::HUP=1}; |
139 |
$SIG{'TERM'}=sub{$::TERM=1}; |
140 |
$SIG{'USR1'}=sub{$::USR1=1}; |
141 |
$SIG{'USR2'}=sub{$::USR2=1}; |
142 |
|
143 |
# |
144 |
# Run server |
145 |
# |
146 |
my $con_counter=0; |
147 |
my $con; |
148 |
|
149 |
my $controlServer=Meteor::Socket->newServer( |
150 |
$::CONF{'ControllerPort'}, |
151 |
$CONTROL_QUEUE_SIZE, |
152 |
$::CONF{'ControllerIP'} |
153 |
); |
154 |
my $controlServerFN=$controlServer->fileno(); |
155 |
|
156 |
my $subscriberServer=Meteor::Socket->newServer( |
157 |
$::CONF{'SubscriberPort'}, |
158 |
$SUBSCRIBER_QUEUE_SIZE, |
159 |
$::CONF{'SubscriberIP'} |
160 |
); |
161 |
my $subscriberServerFN=$subscriberServer->fileno(); |
162 |
|
163 |
my $serverVector=''; |
164 |
vec($serverVector,$controlServerFN,1)=1; |
165 |
vec($serverVector,$subscriberServerFN,1)=1; |
166 |
|
167 |
my $lastAgeCheck=time; |
168 |
|
169 |
my $nextPing=undef; |
170 |
if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2) |
171 |
{ |
172 |
$nextPing=$::CONF{'PingInterval'}+$lastAgeCheck; |
173 |
} |
174 |
|
175 |
while(!$::TERM) |
176 |
{ |
177 |
eval |
178 |
{ |
179 |
while(!$::TERM) |
180 |
{ |
181 |
my $rVec=$serverVector; |
182 |
my $wVec=''; |
183 |
my $eVec=''; |
184 |
|
185 |
my $rout; |
186 |
my $wout; |
187 |
my $eout; |
188 |
|
189 |
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
190 |
|
191 |
my $timeout=$MAIN_LOOP_TIMEOUT; |
192 |
if(defined($nextPing)) |
193 |
{ |
194 |
$timeout=$nextPing-time; |
195 |
} |
196 |
|
197 |
my $result=0; |
198 |
if($timeout>0) |
199 |
{ |
200 |
$result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout); |
201 |
} |
202 |
|
203 |
if($result>0) |
204 |
{ |
205 |
if(vec($rout,$controlServerFN,1)) |
206 |
{ |
207 |
Meteor::Controller->newFromServer($controlServer); |
208 |
} |
209 |
if(vec($rout,$subscriberServerFN,1)) |
210 |
{ |
211 |
Meteor::Subscriber->newFromServer($subscriberServer); |
212 |
} |
213 |
|
214 |
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
215 |
} |
216 |
elsif($result<0) |
217 |
{ |
218 |
&::syslog('crit',"Select failed: $!"); |
219 |
sleep(30); |
220 |
} |
221 |
|
222 |
if($::HUP) |
223 |
{ |
224 |
$::HUP=0; |
225 |
|
226 |
&::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!"); |
227 |
|
228 |
Meteor::Config->readConfig(); |
229 |
Meteor::Config->updateConfig(); |
230 |
|
231 |
Meteor::Document->clearDocuments() |
232 |
} |
233 |
|
234 |
if($::USR1) |
235 |
{ |
236 |
$::USR1=0; |
237 |
|
238 |
&::syslog('info',"Received SIGUSR1, clearing channel buffers!"); |
239 |
|
240 |
Meteor::Channel->clearAllBuffers(); |
241 |
} |
242 |
|
243 |
if($::USR2) |
244 |
{ |
245 |
$::USR2=0; |
246 |
|
247 |
&::syslog('info',"Received SIGUSR2, clearing document cache!"); |
248 |
|
249 |
Meteor::Document->clearDocuments() |
250 |
} |
251 |
|
252 |
my $t=time; |
253 |
if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL) |
254 |
{ |
255 |
my $minTimeStap=time-$::CONF{'MaxMessageAge'}; |
256 |
Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap); |
257 |
$lastAgeCheck=time; |
258 |
$t=$lastAgeCheck; |
259 |
|
260 |
Meteor::Subscriber->checkPersistentConnectionsForMaxTime(); |
261 |
} |
262 |
|
263 |
if(defined($nextPing) && $nextPing<=$t) |
264 |
{ |
265 |
$nextPing=undef; |
266 |
|
267 |
Meteor::Subscriber->pingPersistentConnections(); |
268 |
|
269 |
if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2) |
270 |
{ |
271 |
$nextPing=$::CONF{'PingInterval'}+time; |
272 |
} |
273 |
} |
274 |
} |
275 |
}; |
276 |
unless($::TERM) |
277 |
{ |
278 |
&::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@"); |
279 |
sleep(2); |
280 |
} |
281 |
} |
282 |
|
283 |
# |
284 |
# Proper shutdown |
285 |
# |
286 |
if($::TERM) |
287 |
{ |
288 |
&::syslog('info',"Received SIGTERM, begin shutdown!"); |
289 |
|
290 |
$subscriberServer->close(); |
291 |
$controlServer->close(); |
292 |
|
293 |
unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'}); |
294 |
|
295 |
Meteor::Connection->closeAllConnections(); |
296 |
|
297 |
my $timoutAt=time+$MAX_EXIT_DELAY; |
298 |
|
299 |
while(Meteor::Connection->connectionCount() && time<$timoutAt) |
300 |
{ |
301 |
my $rVec=''; |
302 |
my $wVec=''; |
303 |
my $eVec=''; |
304 |
|
305 |
my $rout; |
306 |
my $wout; |
307 |
my $eout; |
308 |
|
309 |
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
310 |
|
311 |
my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time); |
312 |
|
313 |
if($result>0) |
314 |
{ |
315 |
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
316 |
} |
317 |
} |
318 |
|
319 |
if(my $cnt=Meteor::Connection->connectionCount()) |
320 |
{ |
321 |
&::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway"); |
322 |
|
323 |
exit(1); |
324 |
} |
325 |
|
326 |
&::syslog('info',"shutdown succeeded"); |
327 |
|
328 |
exit(0); |
329 |
} |
330 |
|
331 |
&::syslog('emerg',"$::PGM loop exited"); |
332 |
|
333 |
1; |
334 |
############################################################################EOF |