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