]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
f1b77cec | 2 | * $Id: main.cc,v 1.69 1996/08/30 22:39:30 wessels Exp $ |
30a4f2a8 | 3 | * |
4 | * DEBUG: section 1 Startup and Main Loop | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
7 | * SQUID Internet Object Cache http://www.nlanr.net/Squid/ | |
8 | * -------------------------------------------------------- | |
9 | * | |
10 | * Squid is the result of efforts by numerous individuals from the | |
11 | * Internet community. Development is led by Duane Wessels of the | |
12 | * National Laboratory for Applied Network Research and funded by | |
13 | * the National Science Foundation. | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License as published by | |
17 | * the Free Software Foundation; either version 2 of the License, or | |
18 | * (at your option) any later version. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
28 | * | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Copyright (c) 1994, 1995. All rights reserved. | |
33 | * | |
34 | * The Harvest software was developed by the Internet Research Task | |
35 | * Force Research Group on Resource Discovery (IRTF-RD): | |
36 | * | |
37 | * Mic Bowman of Transarc Corporation. | |
38 | * Peter Danzig of the University of Southern California. | |
39 | * Darren R. Hardy of the University of Colorado at Boulder. | |
40 | * Udi Manber of the University of Arizona. | |
41 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
42 | * Duane Wessels of the University of Colorado at Boulder. | |
43 | * | |
44 | * This copyright notice applies to software in the Harvest | |
45 | * ``src/'' directory only. Users should consult the individual | |
46 | * copyright notices in the ``components/'' subdirectories for | |
47 | * copyright information about other software bundled with the | |
48 | * Harvest source code distribution. | |
49 | * | |
50 | * TERMS OF USE | |
51 | * | |
52 | * The Harvest software may be used and re-distributed without | |
53 | * charge, provided that the software origin and research team are | |
54 | * cited in any use of the system. Most commonly this is | |
55 | * accomplished by including a link to the Harvest Home Page | |
56 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
57 | * Broker you deploy, as well as in the query result pages. These | |
58 | * links are generated automatically by the standard Broker | |
59 | * software distribution. | |
60 | * | |
61 | * The Harvest software is provided ``as is'', without express or | |
62 | * implied warranty, and with no support nor obligation to assist | |
63 | * in its use, correction, modification or enhancement. We assume | |
64 | * no liability with respect to the infringement of copyrights, | |
65 | * trade secrets, or any patents, and are not responsible for | |
66 | * consequential damages. Proper use of the Harvest software is | |
67 | * entirely the responsibility of the user. | |
68 | * | |
69 | * DERIVATIVE WORKS | |
70 | * | |
71 | * Users may make derivative works from the Harvest software, subject | |
72 | * to the following constraints: | |
73 | * | |
74 | * - You must include the above copyright notice and these | |
75 | * accompanying paragraphs in all forms of derivative works, | |
76 | * and any documentation and other materials related to such | |
77 | * distribution and use acknowledge that the software was | |
78 | * developed at the above institutions. | |
79 | * | |
80 | * - You must notify IRTF-RD regarding your distribution of | |
81 | * the derivative work. | |
82 | * | |
83 | * - You must clearly notify users that your are distributing | |
84 | * a modified version and not the original Harvest software. | |
85 | * | |
86 | * - Any derivative product is also subject to these copyright | |
87 | * and use restrictions. | |
88 | * | |
89 | * Note that the Harvest software is NOT in the public domain. We | |
90 | * retain copyright, as specified above. | |
91 | * | |
92 | * HISTORY OF FREE SOFTWARE STATUS | |
93 | * | |
94 | * Originally we required sites to license the software in cases | |
95 | * where they were going to build commercial products/services | |
96 | * around Harvest. In June 1995 we changed this policy. We now | |
97 | * allow people to use the core Harvest software (the code found in | |
98 | * the Harvest ``src/'' directory) for free. We made this change | |
99 | * in the interest of encouraging the widest possible deployment of | |
100 | * the technology. The Harvest software is really a reference | |
101 | * implementation of a set of protocols and formats, some of which | |
102 | * we intend to standardize. We encourage commercial | |
103 | * re-implementations of code complying to this set of standards. | |
104 | */ | |
44a47c6e | 105 | |
106 | #include "squid.h" | |
107 | ||
b8de7ebe | 108 | time_t squid_starttime = 0; |
30a4f2a8 | 109 | int theHttpConnection = -1; |
110 | int theInIcpConnection = -1; | |
111 | int theOutIcpConnection = -1; | |
090089c4 | 112 | int do_reuse = 1; |
d68adf86 | 113 | int opt_unlink_on_reload = 0; |
30a4f2a8 | 114 | int opt_reload_hit_only = 0; /* only UDP_HIT during store relaod */ |
1758c627 | 115 | int opt_catch_signals = 1; |
30a4f2a8 | 116 | int opt_dns_tests = 1; |
117 | int opt_foreground_rebuild = 0; | |
b6f794d6 | 118 | int opt_zap_disk_store = 0; |
6e40f263 | 119 | int opt_syslog_enable = 0; /* disabled by default */ |
1758c627 | 120 | int opt_no_ipcache = 0; /* use ipcache by default */ |
090089c4 | 121 | int vhost_mode = 0; |
16128bc7 | 122 | int unbuffered_logs = 1; /* debug and hierarhcy unbuffered by default */ |
5f3f8d0e | 123 | int shutdown_pending = 0; /* set by SIGTERM handler (shut_down()) */ |
124 | int reread_pending = 0; /* set by SIGHUP handler */ | |
30a4f2a8 | 125 | char version_string[] = SQUID_VERSION; |
126 | char appname[] = "squid"; | |
127 | char localhost[] = "127.0.0.1"; | |
128 | struct in_addr local_addr; | |
e81957b7 | 129 | char *dash_str = "-"; |
30a4f2a8 | 130 | |
131 | /* for error reporting from xmalloc and friends */ | |
132 | extern void (*failure_notify) _PARAMS((char *)); | |
133 | ||
134 | static int rotate_pending = 0; /* set by SIGUSR1 handler */ | |
135 | static int httpPortNumOverride = 1; | |
136 | static int icpPortNumOverride = 1; /* Want to detect "-u 0" */ | |
137 | #if MALLOC_DBG | |
4d64d74a | 138 | static int malloc_debug_level = 0; |
30a4f2a8 | 139 | #endif |
4d7add01 | 140 | |
141 | static time_t next_cleaning; | |
142 | static time_t next_maintain; | |
143 | static time_t next_dirclean; | |
144 | static time_t next_announce; | |
145 | static time_t next_ip_purge; | |
146 | ||
30a4f2a8 | 147 | static void rotate_logs _PARAMS((int)); |
148 | static void reconfigure _PARAMS((int)); | |
9ca89c5a | 149 | static void mainInitialize _PARAMS((void)); |
5285c8ec | 150 | static void mainReinitialize _PARAMS((void)); |
4d7add01 | 151 | static time_t mainMaintenance _PARAMS((void)); |
9ca89c5a | 152 | static void usage _PARAMS((void)); |
153 | static void mainParseOptions _PARAMS((int, char **)); | |
ccff9601 | 154 | |
155 | static void usage() | |
156 | { | |
77ffc99f | 157 | fprintf(stderr, "\ |
30a4f2a8 | 158 | Usage: %s [-hsvzCDFRUVY] [-f config-file] [-[au] port]\n\ |
159 | -a port Specify ASCII port number (default: %d).\n\ | |
160 | -f file Use given config-file instead of\n\ | |
161 | %s\n\ | |
1758c627 | 162 | -i Disable IP caching.\n\ |
ccff9601 | 163 | -h Print help message.\n\ |
164 | -s Enable logging to syslog.\n\ | |
6e40f263 | 165 | -u port Specify ICP port number (default: %d), disable with 0.\n\ |
ccff9601 | 166 | -v Print version.\n\ |
167 | -z Zap disk storage -- deletes all objects in disk cache.\n\ | |
168 | -C Do not catch fatal signals.\n\ | |
169 | -D Disable initial DNS tests.\n\ | |
30a4f2a8 | 170 | -F Foreground fast store rebuild.\n\ |
ccff9601 | 171 | -R Do not set REUSEADDR on port.\n\ |
d68adf86 | 172 | -U Unlink expired objects on reload.\n\ |
30a4f2a8 | 173 | -V Virtual host httpd-accelerator.\n\ |
1061b406 | 174 | -Y Only return UDP_HIT or UDP_RELOADING during fast reload.\n", |
30a4f2a8 | 175 | appname, CACHE_HTTP_PORT, DefaultConfigFile, CACHE_ICP_PORT); |
77ffc99f | 176 | exit(1); |
ccff9601 | 177 | } |
178 | ||
4d64d74a | 179 | static void mainParseOptions(argc, argv) |
090089c4 | 180 | int argc; |
4d64d74a | 181 | char *argv[]; |
090089c4 | 182 | { |
090089c4 | 183 | extern char *optarg; |
4d64d74a | 184 | int c; |
090089c4 | 185 | |
1758c627 | 186 | while ((c = getopt(argc, argv, "CDFRUVYa:bf:him:su:vz?")) != -1) { |
090089c4 | 187 | switch (c) { |
090089c4 | 188 | case 'C': |
1758c627 | 189 | opt_catch_signals = 0; |
090089c4 | 190 | break; |
191 | case 'D': | |
30a4f2a8 | 192 | opt_dns_tests = 0; |
090089c4 | 193 | break; |
30a4f2a8 | 194 | case 'F': |
195 | opt_foreground_rebuild = 1; | |
090089c4 | 196 | break; |
090089c4 | 197 | case 'R': |
198 | do_reuse = 0; | |
199 | break; | |
d68adf86 | 200 | case 'U': |
201 | opt_unlink_on_reload = 1; | |
202 | break; | |
30a4f2a8 | 203 | case 'V': |
204 | vhost_mode = 1; | |
205 | break; | |
206 | case 'Y': | |
207 | opt_reload_hit_only = 1; | |
208 | break; | |
209 | case 'a': | |
210 | httpPortNumOverride = atoi(optarg); | |
211 | break; | |
212 | case 'b': | |
213 | unbuffered_logs = 0; | |
214 | break; | |
090089c4 | 215 | case 'f': |
00fac1f8 | 216 | xfree(ConfigFile); |
217 | ConfigFile = xstrdup(optarg); | |
090089c4 | 218 | break; |
30a4f2a8 | 219 | case 'h': |
220 | usage(); | |
090089c4 | 221 | break; |
1758c627 | 222 | case 'i': |
223 | opt_no_ipcache = 1; | |
224 | break; | |
090089c4 | 225 | case 'm': |
30a4f2a8 | 226 | #if MALLOC_DBG |
090089c4 | 227 | malloc_debug_level = atoi(optarg); |
34c54bb6 | 228 | /* NOTREACHED */ |
090089c4 | 229 | break; |
30a4f2a8 | 230 | #else |
231 | fatal("Need to add -DMALLOC_DBG when compiling to use -m option"); | |
232 | #endif | |
233 | case 's': | |
6e40f263 | 234 | opt_syslog_enable = 1; |
30a4f2a8 | 235 | break; |
236 | case 'u': | |
237 | icpPortNumOverride = atoi(optarg); | |
238 | if (icpPortNumOverride < 0) | |
239 | icpPortNumOverride = 0; | |
240 | break; | |
241 | case 'v': | |
242 | printf("Squid Cache: Version %s\n", version_string); | |
243 | exit(0); | |
244 | /* NOTREACHED */ | |
090089c4 | 245 | case 'z': |
b6f794d6 | 246 | opt_zap_disk_store = 1; |
090089c4 | 247 | break; |
248 | case '?': | |
090089c4 | 249 | default: |
ccff9601 | 250 | usage(); |
090089c4 | 251 | break; |
252 | } | |
090089c4 | 253 | } |
4d64d74a | 254 | } |
090089c4 | 255 | |
30a4f2a8 | 256 | void rotate_logs(sig) |
257 | int sig; | |
258 | { | |
259 | debug(21, 1, "rotate_logs: SIGUSR1 received.\n"); | |
260 | rotate_pending = 1; | |
261 | #if !HAVE_SIGACTION | |
262 | signal(sig, rotate_logs); | |
263 | #endif | |
264 | } | |
265 | ||
266 | void reconfigure(sig) | |
267 | int sig; | |
268 | { | |
269 | debug(21, 1, "reconfigure: SIGHUP received\n"); | |
270 | debug(21, 1, "Waiting %d seconds for active connections to finish\n", | |
b6f794d6 | 271 | Config.lifetimeShutdown); |
30a4f2a8 | 272 | reread_pending = 1; |
273 | #if !HAVE_SIGACTION | |
274 | signal(sig, reconfigure); | |
275 | #endif | |
276 | } | |
277 | ||
278 | void shut_down(sig) | |
279 | int sig; | |
280 | { | |
281 | debug(21, 1, "Preparing for shutdown after %d connections\n", | |
282 | ntcpconn + nudpconn); | |
283 | debug(21, 1, "Waiting %d seconds for active connections to finish\n", | |
b6f794d6 | 284 | Config.lifetimeShutdown); |
30a4f2a8 | 285 | shutdown_pending = 1; |
6e40f263 | 286 | #if SA_RESETHAND == 0 |
287 | signal(SIGTERM, SIG_DFL); | |
288 | signal(SIGINT, SIG_DFL); | |
289 | #endif | |
30a4f2a8 | 290 | } |
291 | ||
5f3f8d0e | 292 | void serverConnectionsOpen() |
4d64d74a | 293 | { |
30a4f2a8 | 294 | struct in_addr addr; |
295 | u_short port; | |
234967c9 | 296 | /* Get our real priviliges */ |
234967c9 | 297 | |
298 | /* Open server ports */ | |
30a4f2a8 | 299 | enter_suid(); |
300 | theHttpConnection = comm_open(COMM_NONBLOCKING, | |
b6f794d6 | 301 | Config.Addrs.tcp_incoming, |
302 | Config.Port.http, | |
30a4f2a8 | 303 | "HTTP Port"); |
304 | leave_suid(); | |
305 | if (theHttpConnection < 0) { | |
306 | fatal("Cannot open HTTP Port"); | |
090089c4 | 307 | } |
30a4f2a8 | 308 | fd_note(theHttpConnection, "HTTP socket"); |
309 | comm_listen(theHttpConnection); | |
310 | comm_set_select_handler(theHttpConnection, | |
090089c4 | 311 | COMM_SELECT_READ, |
312 | asciiHandleConn, | |
313 | 0); | |
30a4f2a8 | 314 | debug(1, 1, "Accepting HTTP connections on FD %d.\n", |
315 | theHttpConnection); | |
090089c4 | 316 | |
b6f794d6 | 317 | if (!httpd_accel_mode || Config.Accel.withProxy) { |
318 | if ((port = Config.Port.icp) > 0) { | |
30a4f2a8 | 319 | theInIcpConnection = comm_open(COMM_NONBLOCKING | COMM_DGRAM, |
b6f794d6 | 320 | Config.Addrs.udp_incoming, |
30a4f2a8 | 321 | port, |
322 | "ICP Port"); | |
323 | if (theInIcpConnection < 0) | |
324 | fatal("Cannot open ICP Port"); | |
325 | fd_note(theInIcpConnection, "ICP socket"); | |
326 | comm_set_select_handler(theInIcpConnection, | |
090089c4 | 327 | COMM_SELECT_READ, |
328 | icpHandleUdp, | |
329 | 0); | |
30a4f2a8 | 330 | debug(1, 1, "Accepting ICP connections on FD %d.\n", |
331 | theInIcpConnection); | |
332 | ||
b6f794d6 | 333 | if ((addr = Config.Addrs.udp_outgoing).s_addr != INADDR_NONE) { |
30a4f2a8 | 334 | theOutIcpConnection = comm_open(COMM_NONBLOCKING | COMM_DGRAM, |
335 | addr, | |
336 | port, | |
337 | "ICP Port"); | |
338 | if (theOutIcpConnection < 0) | |
339 | fatal("Cannot open Outgoing ICP Port"); | |
340 | comm_set_select_handler(theOutIcpConnection, | |
341 | COMM_SELECT_READ, | |
342 | icpHandleUdp, | |
343 | 0); | |
344 | debug(1, 1, "Accepting ICP connections on FD %d.\n", | |
345 | theOutIcpConnection); | |
346 | fd_note(theOutIcpConnection, "Outgoing ICP socket"); | |
347 | fd_note(theInIcpConnection, "Incoming ICP socket"); | |
348 | } else { | |
349 | theOutIcpConnection = theInIcpConnection; | |
350 | } | |
090089c4 | 351 | } |
352 | } | |
5f3f8d0e | 353 | } |
354 | ||
355 | void serverConnectionsClose() | |
356 | { | |
30a4f2a8 | 357 | /* NOTE, this function will be called repeatedly while shutdown |
358 | * is pending */ | |
359 | if (theHttpConnection >= 0) { | |
360 | debug(21, 1, "FD %d Closing HTTP connection\n", | |
361 | theHttpConnection); | |
362 | comm_close(theHttpConnection); | |
363 | comm_set_select_handler(theHttpConnection, | |
5f3f8d0e | 364 | COMM_SELECT_READ, |
365 | NULL, | |
366 | 0); | |
30a4f2a8 | 367 | theHttpConnection = -1; |
090089c4 | 368 | } |
30a4f2a8 | 369 | if (theInIcpConnection >= 0) { |
370 | /* NOTE, don't close outgoing ICP connection, we need to write to | |
371 | * it during shutdown */ | |
372 | debug(21, 1, "FD %d Closing ICP connection\n", | |
373 | theInIcpConnection); | |
374 | if (theInIcpConnection != theOutIcpConnection) | |
375 | comm_close(theInIcpConnection); | |
376 | comm_set_select_handler(theInIcpConnection, | |
5f3f8d0e | 377 | COMM_SELECT_READ, |
378 | NULL, | |
379 | 0); | |
30a4f2a8 | 380 | if (theInIcpConnection != theOutIcpConnection) |
381 | comm_set_select_handler(theOutIcpConnection, | |
382 | COMM_SELECT_READ, | |
383 | NULL, | |
384 | 0); | |
385 | theInIcpConnection = -1; | |
5f3f8d0e | 386 | } |
387 | } | |
388 | ||
0ffd22bc | 389 | static void mainReinitialize() |
5f3f8d0e | 390 | { |
ad6272a1 | 391 | debug(1, 0, "Restarting Squid Cache (version %s)...\n", version_string); |
0ffd22bc | 392 | /* Already called serverConnectionsClose and ipcacheShutdownServers() */ |
5f3f8d0e | 393 | neighborsDestroy(); |
00fac1f8 | 394 | parseConfigFile(ConfigFile); |
b6f794d6 | 395 | _db_init(Config.Log.log, Config.debugOptions); |
0ffd22bc | 396 | neighbors_init(); |
f88bb09c | 397 | dnsOpenServers(); |
d2af9477 | 398 | redirectOpenServers(); |
0ffd22bc | 399 | serverConnectionsOpen(); |
234967c9 | 400 | (void) ftpInitialize(); |
b6f794d6 | 401 | if (theOutIcpConnection >= 0 && (!httpd_accel_mode || Config.Accel.withProxy)) |
30a4f2a8 | 402 | neighbors_open(theOutIcpConnection); |
0ffd22bc | 403 | debug(1, 0, "Ready to serve requests.\n"); |
5f3f8d0e | 404 | } |
405 | ||
406 | static void mainInitialize() | |
407 | { | |
408 | static int first_time = 1; | |
1758c627 | 409 | if (opt_catch_signals) { |
30a4f2a8 | 410 | squid_signal(SIGSEGV, death, SA_NODEFER | SA_RESETHAND); |
411 | squid_signal(SIGBUS, death, SA_NODEFER | SA_RESETHAND); | |
44f99671 | 412 | } |
30a4f2a8 | 413 | squid_signal(SIGPIPE, SIG_IGN, SA_RESTART); |
414 | squid_signal(SIGCHLD, sig_child, SA_NODEFER | SA_RESTART); | |
44f99671 | 415 | |
00fac1f8 | 416 | if (ConfigFile == NULL) |
417 | ConfigFile = xstrdup(DefaultConfigFile); | |
418 | parseConfigFile(ConfigFile); | |
5f3f8d0e | 419 | |
30a4f2a8 | 420 | leave_suid(); /* Run as non privilegied user */ |
5f3f8d0e | 421 | |
34c54bb6 | 422 | #if USE_ASYNC_IO |
423 | #if HAVE_AIO_INIT | |
424 | if (first_time) | |
425 | aio_init(); | |
426 | #endif | |
427 | squid_signal(SIGIO, aioSigHandler, SA_RESTART); | |
428 | #endif | |
429 | ||
30a4f2a8 | 430 | if (httpPortNumOverride != 1) |
431 | setHttpPortNum((u_short) httpPortNumOverride); | |
432 | if (icpPortNumOverride != 1) | |
433 | setIcpPortNum((u_short) icpPortNumOverride); | |
434 | ||
b6f794d6 | 435 | _db_init(Config.Log.log, Config.debugOptions); |
30a4f2a8 | 436 | fdstat_open(fileno(debug_log), FD_LOG); |
b6f794d6 | 437 | fd_note(fileno(debug_log), Config.Log.log); |
5f3f8d0e | 438 | |
30a4f2a8 | 439 | debug(1, 0, "Starting Squid Cache version %s for %s...\n", |
440 | version_string, | |
441 | CONFIG_HOST_TYPE); | |
442 | debug(1, 1, "With %d file descriptors available\n", FD_SETSIZE); | |
5f3f8d0e | 443 | |
850c4ead | 444 | if (first_time) { |
94e891ea | 445 | stmemInit(); /* stmem must go before at least redirect */ |
234967c9 | 446 | disk_init(); /* disk_init must go before ipcache_init() */ |
850c4ead | 447 | } |
5f3f8d0e | 448 | ipcache_init(); |
f88bb09c | 449 | fqdncache_init(); |
450 | dnsOpenServers(); | |
d2af9477 | 451 | redirectOpenServers(); |
5f3f8d0e | 452 | neighbors_init(); |
234967c9 | 453 | (void) ftpInitialize(); |
5f3f8d0e | 454 | |
30a4f2a8 | 455 | #if MALLOC_DBG |
5f3f8d0e | 456 | malloc_debug(0, malloc_debug_level); |
457 | #endif | |
458 | ||
a26bdc75 | 459 | if (first_time) { |
5f3f8d0e | 460 | /* module initialization */ |
7111c86a | 461 | urlInitialize(); |
b6f794d6 | 462 | stat_init(&CacheInfo, Config.Log.access); |
5f3f8d0e | 463 | storeInit(); |
090089c4 | 464 | |
b6f794d6 | 465 | if (Config.effectiveUser) { |
234967c9 | 466 | /* we were probably started as root, so cd to a swap |
467 | * directory in case we dump core */ | |
468 | if (chdir(swappath(0)) < 0) { | |
469 | debug(1, 0, "%s: %s\n", swappath(0), xstrerror()); | |
470 | fatal_dump("Cannot cd to swap directory?"); | |
471 | } | |
472 | } | |
5f3f8d0e | 473 | /* after this point we want to see the mallinfo() output */ |
474 | do_mallinfo = 1; | |
475 | } | |
2285407f | 476 | serverConnectionsOpen(); |
b6f794d6 | 477 | if (theOutIcpConnection >= 0 && (!httpd_accel_mode || Config.Accel.withProxy)) |
30a4f2a8 | 478 | neighbors_open(theOutIcpConnection); |
479 | ||
0a5b9b32 | 480 | if (first_time) |
481 | writePidFile(); /* write PID file */ | |
482 | ||
30a4f2a8 | 483 | squid_signal(SIGUSR1, rotate_logs, SA_RESTART); |
484 | squid_signal(SIGUSR2, sigusr2_handle, SA_RESTART); | |
485 | squid_signal(SIGHUP, reconfigure, SA_RESTART); | |
486 | squid_signal(SIGTERM, shut_down, SA_NODEFER | SA_RESETHAND | SA_RESTART); | |
487 | squid_signal(SIGINT, shut_down, SA_NODEFER | SA_RESETHAND | SA_RESTART); | |
5f3f8d0e | 488 | debug(1, 0, "Ready to serve requests.\n"); |
4d7add01 | 489 | |
490 | if (first_time) { | |
491 | next_cleaning = squid_curtime + Config.cleanRate; | |
492 | next_maintain = squid_curtime + 0; | |
493 | next_dirclean = squid_curtime + 15; | |
494 | next_announce = squid_curtime + Config.Announce.rate; | |
495 | next_ip_purge = squid_curtime + 10; | |
496 | } | |
0a5b9b32 | 497 | first_time = 0; |
4d64d74a | 498 | } |
499 | ||
4d7add01 | 500 | static time_t mainMaintenance() |
9ca89c5a | 501 | { |
4d7add01 | 502 | time_t next; |
9ca89c5a | 503 | int n; |
4d7add01 | 504 | if (squid_curtime >= next_maintain) { |
28b2f45f | 505 | storeMaintainSwapSpace(); |
506 | next_maintain = squid_curtime + 1; | |
9ca89c5a | 507 | } |
4d7add01 | 508 | if (store_rebuilding != STORE_NOT_REBUILDING) |
509 | goto maintenance_done; | |
510 | if (squid_curtime >= next_ip_purge) { | |
511 | ipcache_purgelru(); | |
512 | next_ip_purge = squid_curtime + 10; | |
513 | } else if (squid_curtime >= next_dirclean) { | |
9ca89c5a | 514 | /* clean a cache directory every 15 seconds */ |
515 | /* 15 * 16 * 256 = 17 hrs */ | |
516 | storeDirClean(); | |
4d7add01 | 517 | next_dirclean = squid_curtime + 15; |
518 | } else if (squid_curtime >= next_cleaning) { | |
519 | n = storePurgeOld(); | |
520 | next_cleaning = squid_curtime + Config.cleanRate; | |
521 | } else if (squid_curtime >= next_announce) { | |
522 | send_announce(); | |
523 | next_announce = squid_curtime + Config.Announce.rate; | |
9ca89c5a | 524 | } |
4d7add01 | 525 | maintenance_done: |
526 | next = next_ip_purge; | |
527 | if (next_dirclean < next) | |
528 | next = next_dirclean; | |
529 | if (next_cleaning < next) | |
530 | next = next_cleaning; | |
531 | if (next_announce < next) | |
532 | next = next_announce; | |
533 | return next - squid_curtime; | |
9ca89c5a | 534 | } |
535 | ||
4d64d74a | 536 | int main(argc, argv) |
537 | int argc; | |
538 | char **argv; | |
539 | { | |
540 | int errcount = 0; | |
4d64d74a | 541 | int n; /* # of GC'd objects */ |
bc528c78 | 542 | time_t loop_delay; |
4d64d74a | 543 | |
7328e889 | 544 | /* call mallopt() before anything else */ |
545 | #if HAVE_MALLOPT | |
546 | #ifdef M_GRAIN | |
547 | /* Round up all sizes to a multiple of this */ | |
548 | mallopt(M_GRAIN, 16); | |
549 | #endif | |
550 | #ifdef M_MXFAST | |
551 | /* biggest size that is considered a small block */ | |
552 | mallopt(M_MXFAST, 256); | |
553 | #endif | |
554 | #ifdef M_NBLKS | |
555 | /* allocate this many small blocks at once */ | |
556 | mallopt(M_NLBLKS, 32); | |
557 | #endif | |
558 | #endif /* HAVE_MALLOPT */ | |
559 | ||
30a4f2a8 | 560 | memset(&local_addr, '\0', sizeof(struct in_addr)); |
561 | local_addr.s_addr = inet_addr(localhost); | |
562 | ||
4d64d74a | 563 | errorInitialize(); |
564 | ||
b8de7ebe | 565 | squid_starttime = getCurrentTime(); |
4d64d74a | 566 | failure_notify = fatal_dump; |
567 | ||
568 | mainParseOptions(argc, argv); | |
569 | ||
570 | setMaxFD(); | |
571 | ||
1758c627 | 572 | if (opt_catch_signals) |
30a4f2a8 | 573 | for (n = FD_SETSIZE; n > 2; n--) |
574 | close(n); | |
4d64d74a | 575 | |
4d64d74a | 576 | /*init comm module */ |
577 | comm_init(); | |
578 | ||
579 | /* we have to init fdstat here. */ | |
580 | fdstat_init(PREOPEN_FD); | |
30a4f2a8 | 581 | fdstat_open(0, FD_LOG); |
582 | fdstat_open(1, FD_LOG); | |
583 | fdstat_open(2, FD_LOG); | |
4d64d74a | 584 | fd_note(0, "STDIN"); |
585 | fd_note(1, "STDOUT"); | |
586 | fd_note(2, "STDERR"); | |
587 | ||
4d64d74a | 588 | /* preinit for debug module */ |
589 | debug_log = stderr; | |
590 | hash_init(0); | |
591 | ||
4d64d74a | 592 | mainInitialize(); |
593 | ||
090089c4 | 594 | /* main loop */ |
983061ed | 595 | for (;;) { |
30a4f2a8 | 596 | if (rotate_pending) { |
597 | ftpServerClose(); | |
598 | _db_rotate_log(); /* cache.log */ | |
599 | storeWriteCleanLog(); | |
600 | storeRotateLog(); /* store.log */ | |
30a4f2a8 | 601 | stat_rotate_log(); /* access.log */ |
602 | (void) ftpInitialize(); | |
603 | rotate_pending = 0; | |
604 | } | |
4d7add01 | 605 | if ((loop_delay = mainMaintenance()) < 0) |
606 | loop_delay = 0; | |
607 | else if (loop_delay > 10) | |
608 | loop_delay = 10; | |
bc528c78 | 609 | if (doBackgroundProcessing()) |
4d7add01 | 610 | loop_delay = 0; |
9ca89c5a | 611 | switch (comm_select(loop_delay)) { |
090089c4 | 612 | case COMM_OK: |
234967c9 | 613 | errcount = 0; /* reset if successful */ |
090089c4 | 614 | break; |
615 | case COMM_ERROR: | |
616 | errcount++; | |
234967c9 | 617 | debug(1, 0, "Select loop Error. Retry %d\n", errcount); |
090089c4 | 618 | if (errcount == 10) |
4d64d74a | 619 | fatal_dump("Select Loop failed!"); |
090089c4 | 620 | break; |
4d64d74a | 621 | case COMM_SHUTDOWN: |
43030d00 | 622 | /* delayed close so we can transmit while shutdown pending */ |
30a4f2a8 | 623 | if (theOutIcpConnection > 0) { |
624 | comm_close(theOutIcpConnection); | |
625 | theOutIcpConnection = -1; | |
43030d00 | 626 | } |
5f3f8d0e | 627 | if (shutdown_pending) { |
628 | normal_shutdown(); | |
629 | exit(0); | |
630 | } else if (reread_pending) { | |
0ffd22bc | 631 | mainReinitialize(); |
5f3f8d0e | 632 | reread_pending = 0; /* reset */ |
633 | } else { | |
634 | fatal_dump("MAIN: SHUTDOWN from comm_select, but nothing pending."); | |
635 | } | |
0ffd22bc | 636 | break; |
9ca89c5a | 637 | case COMM_TIMEOUT: |
6c93e119 | 638 | break; |
090089c4 | 639 | default: |
6eb42cae | 640 | fatal_dump("MAIN: Internal error -- this should never happen."); |
090089c4 | 641 | break; |
642 | } | |
643 | } | |
644 | /* NOTREACHED */ | |
983061ed | 645 | return 0; |
090089c4 | 646 | } |