1 |
knops.gerd |
11 |
#!/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 |
knops.gerd |
42 |
############################################################################### |
35 |
|
|
# meterod version |
36 |
|
|
################################################################################ |
37 |
|
|
|
38 |
knops.gerd |
45 |
$::VERSION='1.05.02'; |
39 |
|
|
$::RELEASE_DATE='not yet released'; |
40 |
knops.gerd |
11 |
|
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 |
knops.gerd |
25 |
# Record startup time |
73 |
|
|
# |
74 |
|
|
$::STARTUP_TIME=time; |
75 |
|
|
$::STARTUP_TIME+=0; # avoid warning |
76 |
|
|
|
77 |
|
|
# |
78 |
knops.gerd |
11 |
# 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 |
|
|
unless($::CONF{'Debug'}) |
106 |
|
|
{ |
107 |
|
|
# close standard file descriptors |
108 |
|
|
close(STDIN); |
109 |
|
|
close(STDOUT); |
110 |
|
|
close(STDERR); |
111 |
|
|
chdir("/"); |
112 |
|
|
umask(0); |
113 |
|
|
# fork and exit parent |
114 |
|
|
exit if fork; |
115 |
|
|
setpgrp(0, $$) if defined $SIG{TTOU}; |
116 |
|
|
$SIG{TTOU}='ignore' if defined $SIG{TTOU}; |
117 |
|
|
|
118 |
|
|
# Avoid 'stdin reopened for output' warning with newer perls |
119 |
|
|
open(NULL,'/dev/null'); |
120 |
|
|
<NULL> if(0); |
121 |
|
|
|
122 |
|
|
open(OUT,">/var/run/$::PGM.pid"); |
123 |
|
|
print OUT "$$\n"; |
124 |
|
|
close(OUT); |
125 |
|
|
} |
126 |
|
|
else |
127 |
|
|
{ |
128 |
|
|
print "$::PGM PID: $$\n"; |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
# |
133 |
|
|
# Signal handlers |
134 |
|
|
# |
135 |
|
|
$::HUP=$::TERM=$::USR1=$::USR2=0; |
136 |
|
|
$SIG{'HUP'}=sub{$::HUP=1}; |
137 |
|
|
$SIG{'TERM'}=sub{$::TERM=1}; |
138 |
|
|
$SIG{'USR1'}=sub{$::USR1=1}; |
139 |
|
|
$SIG{'USR2'}=sub{$::USR2=1}; |
140 |
|
|
|
141 |
|
|
# |
142 |
|
|
# Run server |
143 |
|
|
# |
144 |
|
|
my $con_counter=0; |
145 |
|
|
my $con; |
146 |
|
|
|
147 |
|
|
my $controlServer=Meteor::Socket->newServer( |
148 |
|
|
$::CONF{'ControllerPort'}, |
149 |
|
|
$CONTROL_QUEUE_SIZE, |
150 |
|
|
$::CONF{'ControllerIP'} |
151 |
|
|
); |
152 |
|
|
my $controlServerFN=$controlServer->fileno(); |
153 |
|
|
|
154 |
|
|
my $subscriberServer=Meteor::Socket->newServer( |
155 |
|
|
$::CONF{'SubscriberPort'}, |
156 |
|
|
$SUBSCRIBER_QUEUE_SIZE, |
157 |
|
|
$::CONF{'SubscriberIP'} |
158 |
|
|
); |
159 |
|
|
my $subscriberServerFN=$subscriberServer->fileno(); |
160 |
|
|
|
161 |
|
|
my $serverVector=''; |
162 |
|
|
vec($serverVector,$controlServerFN,1)=1; |
163 |
|
|
vec($serverVector,$subscriberServerFN,1)=1; |
164 |
|
|
|
165 |
|
|
my $lastAgeCheck=time; |
166 |
|
|
|
167 |
|
|
my $nextPing=undef; |
168 |
|
|
if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2) |
169 |
|
|
{ |
170 |
|
|
$nextPing=$::CONF{'PingInterval'}+$lastAgeCheck; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
while(!$::TERM) |
174 |
|
|
{ |
175 |
|
|
eval |
176 |
|
|
{ |
177 |
|
|
while(!$::TERM) |
178 |
|
|
{ |
179 |
|
|
my $rVec=$serverVector; |
180 |
|
|
my $wVec=''; |
181 |
|
|
my $eVec=''; |
182 |
|
|
|
183 |
|
|
my $rout; |
184 |
|
|
my $wout; |
185 |
|
|
my $eout; |
186 |
|
|
|
187 |
|
|
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
188 |
|
|
|
189 |
|
|
my $timeout=$MAIN_LOOP_TIMEOUT; |
190 |
|
|
if(defined($nextPing)) |
191 |
|
|
{ |
192 |
|
|
$timeout=$nextPing-time; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
my $result=0; |
196 |
|
|
if($timeout>0) |
197 |
|
|
{ |
198 |
|
|
$result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
if($result>0) |
202 |
|
|
{ |
203 |
|
|
if(vec($rout,$controlServerFN,1)) |
204 |
|
|
{ |
205 |
|
|
Meteor::Controller->newFromServer($controlServer); |
206 |
|
|
} |
207 |
|
|
if(vec($rout,$subscriberServerFN,1)) |
208 |
|
|
{ |
209 |
|
|
Meteor::Subscriber->newFromServer($subscriberServer); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
213 |
|
|
} |
214 |
|
|
elsif($result<0) |
215 |
|
|
{ |
216 |
|
|
&::syslog('crit',"Select failed: $!"); |
217 |
|
|
sleep(30); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
if($::HUP) |
221 |
|
|
{ |
222 |
|
|
$::HUP=0; |
223 |
|
|
|
224 |
|
|
&::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!"); |
225 |
|
|
|
226 |
|
|
Meteor::Config->readConfig(); |
227 |
|
|
Meteor::Config->updateConfig(); |
228 |
|
|
|
229 |
|
|
Meteor::Document->clearDocuments() |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
if($::USR1) |
233 |
|
|
{ |
234 |
|
|
$::USR1=0; |
235 |
|
|
|
236 |
|
|
&::syslog('info',"Received SIGUSR1, clearing channel buffers!"); |
237 |
|
|
|
238 |
|
|
Meteor::Channel->clearAllBuffers(); |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
if($::USR2) |
242 |
|
|
{ |
243 |
|
|
$::USR2=0; |
244 |
|
|
|
245 |
|
|
&::syslog('info',"Received SIGUSR2, clearing document cache!"); |
246 |
|
|
|
247 |
|
|
Meteor::Document->clearDocuments() |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
my $t=time; |
251 |
|
|
if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL) |
252 |
|
|
{ |
253 |
|
|
my $minTimeStap=time-$::CONF{'MaxMessageAge'}; |
254 |
|
|
Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap); |
255 |
|
|
$lastAgeCheck=time; |
256 |
|
|
$t=$lastAgeCheck; |
257 |
|
|
|
258 |
|
|
Meteor::Subscriber->checkPersistentConnectionsForMaxTime(); |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
if(defined($nextPing) && $nextPing<=$t) |
262 |
|
|
{ |
263 |
|
|
$nextPing=undef; |
264 |
|
|
|
265 |
|
|
Meteor::Subscriber->pingPersistentConnections(); |
266 |
|
|
|
267 |
|
|
if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2) |
268 |
|
|
{ |
269 |
|
|
$nextPing=$::CONF{'PingInterval'}+time; |
270 |
|
|
} |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
}; |
274 |
|
|
unless($::TERM) |
275 |
|
|
{ |
276 |
|
|
&::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@"); |
277 |
|
|
sleep(2); |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
# |
282 |
|
|
# Proper shutdown |
283 |
|
|
# |
284 |
|
|
if($::TERM) |
285 |
|
|
{ |
286 |
|
|
&::syslog('info',"Received SIGTERM, begin shutdown!"); |
287 |
|
|
|
288 |
|
|
$subscriberServer->close(); |
289 |
|
|
$controlServer->close(); |
290 |
|
|
|
291 |
|
|
unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'}); |
292 |
|
|
|
293 |
|
|
Meteor::Connection->closeAllConnections(); |
294 |
|
|
|
295 |
|
|
my $timoutAt=time+$MAX_EXIT_DELAY; |
296 |
|
|
|
297 |
|
|
while(Meteor::Connection->connectionCount() && time<$timoutAt) |
298 |
|
|
{ |
299 |
|
|
my $rVec=''; |
300 |
|
|
my $wVec=''; |
301 |
|
|
my $eVec=''; |
302 |
|
|
|
303 |
|
|
my $rout; |
304 |
|
|
my $wout; |
305 |
|
|
my $eout; |
306 |
|
|
|
307 |
|
|
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); |
308 |
|
|
|
309 |
|
|
my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time); |
310 |
|
|
|
311 |
|
|
if($result>0) |
312 |
|
|
{ |
313 |
|
|
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); |
314 |
|
|
} |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
if(my $cnt=Meteor::Connection->connectionCount()) |
318 |
|
|
{ |
319 |
|
|
&::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway"); |
320 |
|
|
|
321 |
|
|
exit(1); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
&::syslog('info',"shutdown succeeded"); |
325 |
|
|
|
326 |
|
|
exit(0); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
&::syslog('emerg',"$::PGM loop exited"); |
330 |
|
|
|
331 |
|
|
1; |
332 |
andrew.betts |
3 |
############################################################################EOF |