3 * $Id: main.cc,v 1.427 2006/07/02 16:53:46 serassio Exp $
5 * DEBUG: section 1 Startup and Main Loop
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "AccessLogEntry.h"
38 #include "authenticate.h"
39 #include "CacheManager.h"
40 #include "ConfigParser.h"
41 #include "ExternalACL.h"
44 #include "HttpReply.h"
50 #include "StoreFileSystem.h"
51 #include "DiskIO/DiskIOModule.h"
54 #include "comm_epoll.h"
57 #include "comm_kqueue.h"
60 #include "comm_poll.h"
63 #include "comm_select.h"
66 #include "comm_select.h"
68 #include "SquidTime.h"
75 #include "squid_windows.h"
78 static int opt_install_service
= FALSE
;
79 static int opt_remove_service
= FALSE
;
80 static int opt_signal_service
= FALSE
;
81 static int opt_command_line
= FALSE
;
82 extern void WIN32_svcstatusupdate(DWORD
, DWORD
);
83 void WINAPI
WIN32_svcHandler(DWORD
);
87 /* for error reporting from xmalloc and friends */
88 SQUIDCEXTERN
void (*failure_notify
) (const char *);
90 static int opt_parse_cfg_only
= 0;
91 static char *opt_syslog_facility
= NULL
;
92 static int icpPortNumOverride
= 1; /* Want to detect "-u 0" */
93 static int configured_once
= 0;
95 static int malloc_debug_level
= 0;
97 static volatile int do_reconfigure
= 0;
98 static volatile int do_rotate
= 0;
99 static volatile int do_shutdown
= 0;
100 static volatile int shutdown_status
= 0;
102 static void mainRotate(void);
103 static void mainReconfigure(void);
104 static void mainInitialize(void);
105 static void usage(void);
106 static void mainParseOptions(int, char **);
107 static void sendSignal(void);
108 static void serverConnectionsOpen(void);
109 static void watch_child(char **);
110 static void setEffectiveUser(void);
112 extern void log_trace_done();
113 extern void log_trace_init(char *);
115 static EVH SquidShutdown
;
116 static void mainSetCwd(void);
117 static int checkRunningPid(void);
119 static CacheManager manager
;
121 #ifndef _SQUID_MSWIN_
122 static const char *squid_start_script
= "squid_start";
126 #include "test_access.c"
133 #if USE_WIN32_SERVICE
134 "Usage: %s [-cdhirvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal] [-n name] [-O CommandLine]\n"
136 "Usage: %s [-cdhvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]\n"
138 " -a port Specify HTTP port number (default: %d).\n"
139 " -d level Write debugging to stderr also.\n"
140 " -f file Use given config-file instead of\n"
142 " -h Print help message.\n"
143 #if USE_WIN32_SERVICE
144 " -i Installs as a Windows Service (see -n option).\n"
146 " -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse\n"
147 " Parse configuration file, then send signal to \n"
148 " running copy (except -k parse) and exit.\n"
149 #if USE_WIN32_SERVICE
150 " -n name Specify Windows Service name to use for service operations\n"
151 " default is: " _WIN_SQUID_DEFAULT_SERVICE_NAME
".\n"
152 " -r Removes a Windows Service (see -n option).\n"
154 " -s | -l facility\n"
155 " Enable logging to syslog.\n"
156 " -u port Specify ICP port number (default: %d), disable with 0.\n"
157 " -v Print version.\n"
158 " -z Create swap directories\n"
159 " -C Do not catch fatal signals.\n"
160 " -D Disable initial DNS tests.\n"
161 " -F Don't serve any requests until store is rebuilt.\n"
162 " -N No daemon mode.\n"
163 #if USE_WIN32_SERVICE
165 " Set Windows Service Command line options in Registry.\n"
167 " -R Do not set REUSEADDR on port.\n"
168 " -S Double-check swap during rebuild.\n"
169 " -V Virtual host httpd-accelerator.\n"
170 " -X Force full debugging.\n"
171 " -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.\n",
172 appname
, CACHE_HTTP_PORT
, DefaultConfigFile
, CACHE_ICP_PORT
);
177 mainParseOptions(int argc
, char *argv
[])
182 #if USE_WIN32_SERVICE
184 while ((c
= getopt(argc
, argv
, "CDFNO:RSVYXa:d:f:hik:m::n:rsl:u:vz?")) != -1)
187 while ((c
= getopt(argc
, argv
, "CDFNRSVYXa:d:f:hk:m::sl:u:vz?")) != -1)
196 opt_catch_signals
= 0;
204 opt_foreground_rebuild
= 1;
210 #if USE_WIN32_SERVICE
213 opt_command_line
= 1;
214 WIN32_Command_Line
= xstrdup(optarg
);
223 opt_store_doublecheck
= 1;
228 if (Config
.Sockaddr
.http
)
229 Config
.Sockaddr
.http
->vhost
= 1;
233 else if (Config
.Sockaddr
.https
)
234 Config
.Sockaddr
.https
->http
.vhost
= 1;
239 fatal("No http_port specified\n");
244 /* force full debugging */
245 sigusr2_handle(SIGUSR2
);
250 opt_reload_hit_only
= 1;
254 #if USE_WIN32_SERVICE
257 opt_install_service
= TRUE
;
264 add_http_port(optarg
);
269 opt_debug_stderr
= atoi(optarg
);
276 ConfigFile
= xstrdup(optarg
);
286 if ((int) strlen(optarg
) < 1)
289 if (!strncmp(optarg
, "reconfigure", strlen(optarg
)))
290 opt_send_signal
= SIGHUP
;
291 else if (!strncmp(optarg
, "rotate", strlen(optarg
)))
292 #ifdef _SQUID_LINUX_THREADS_
294 opt_send_signal
= SIGQUIT
;
298 opt_send_signal
= SIGUSR1
;
302 else if (!strncmp(optarg
, "debug", strlen(optarg
)))
303 #ifdef _SQUID_LINUX_THREADS_
305 opt_send_signal
= SIGTRAP
;
309 opt_send_signal
= SIGUSR2
;
313 else if (!strncmp(optarg
, "shutdown", strlen(optarg
)))
314 opt_send_signal
= SIGTERM
;
315 else if (!strncmp(optarg
, "interrupt", strlen(optarg
)))
316 opt_send_signal
= SIGINT
;
317 else if (!strncmp(optarg
, "kill", strlen(optarg
)))
318 opt_send_signal
= SIGKILL
;
322 else if (!strncmp(optarg
, "restart", strlen(optarg
)))
323 opt_send_signal
= SIGTTIN
; /* exit and restart by parent */
327 else if (!strncmp(optarg
, "check", strlen(optarg
)))
328 opt_send_signal
= 0; /* SIGNULL */
329 else if (!strncmp(optarg
, "parse", strlen(optarg
)))
330 opt_parse_cfg_only
= 1; /* parse cfg file only */
338 if (*optarg
== 'c') {
339 MemPools::GetInstance().setDefaultPoolChunking(0);
342 malloc_debug_level
= atoi(optarg
);
345 fatal("Need to add -DMALLOC_DBG when compiling to use -mX option");
352 xmalloc_trace
= !xmalloc_trace
;
355 fatal("Need to configure --enable-xmalloc-debug-trace to use -m option");
363 #if USE_WIN32_SERVICE
366 xfree(WIN32_Service_name
);
368 WIN32_Service_name
= xstrdup(optarg
);
370 opt_signal_service
= TRUE
;
375 opt_remove_service
= TRUE
;
382 opt_syslog_facility
= xstrdup(optarg
);
387 _db_set_syslog(opt_syslog_facility
);
393 fatal("Logging to syslog not available on this platform");
399 icpPortNumOverride
= atoi(optarg
);
401 if (icpPortNumOverride
< 0)
402 icpPortNumOverride
= 0;
407 printf("Squid Cache: Version %s\nconfigure options: %s\n", version_string
, SQUID_CONFIGURE_OPTIONS
);
409 #if USE_WIN32_SERVICE
411 printf("Compiled as Windows System Service.\n");
420 opt_debug_stderr
= 1;
422 opt_create_swap_dirs
= 1;
442 #ifndef _SQUID_MSWIN_
445 signal(sig
, rotate_logs
);
455 #ifndef _SQUID_MSWIN_
458 signal(sig
, reconfigure
);
466 do_shutdown
= sig
== SIGINT
? -1 : 1;
473 #ifndef _SQUID_MSWIN_
474 #ifdef KILL_PARENT_OPT
477 debugs(1, 1, "Killing RunCache, pid " << getppid());
479 if (kill(getppid(), sig
) < 0)
480 debugs(1, 1, "kill " << getppid() << ": " << xstrerror());
484 #if SA_RESETHAND == 0
485 signal(SIGTERM
, SIG_DFL
);
487 signal(SIGINT
, SIG_DFL
);
494 serverConnectionsOpen(void)
496 clientOpenListenSockets();
497 icpConnectionsOpen();
504 snmpConnectionOpen();
508 wccpConnectionOpen();
513 wccp2ConnectionOpen();
529 serverConnectionsClose(void)
531 assert(shutting_down
|| reconfiguring
);
532 clientHttpConnectionsClose();
533 icpConnectionShutdown();
536 htcpSocketShutdown();
542 snmpConnectionShutdown();
546 wccpConnectionClose();
550 wccp2ConnectionClose();
557 mainReconfigure(void)
559 debug(1, 1) ("Reconfiguring Squid Cache (version %s)...\n", version_string
);
561 /* Already called serverConnectionsClose and ipcacheShutdownServers() */
562 serverConnectionsClose();
563 icpConnectionClose();
570 snmpConnectionClose();
581 authenticateShutdown();
582 externalAclShutdown();
583 storeDirCloseSwapLogs();
589 enter_suid(); /* root to read config file */
590 parseConfigFile(ConfigFile
, manager
);
592 _db_init(Config
.Log
.log
, Config
.debugOptions
);
593 ipcache_restart(); /* clear stuck entries */
594 authenticateUserCacheRestart(); /* clear stuck ACL entries */
595 fqdncache_restart(); /* sigh, fqdncache too */
597 errorInitialize(); /* reload error pages */
611 authenticateInit(&Config
.authConfiguration
);
622 serverConnectionsOpen();
624 if (theOutIcpConnection
>= 0) {
626 neighborsRegisterWithCacheManager(manager
);
629 storeDirOpenSwapLogs();
631 mimeInit(Config
.mimeTablePathname
);
633 writePidFile(); /* write PID file */
635 debug(1, 1) ("Ready to serve requests.\n");
650 authenticateShutdown();
651 externalAclShutdown();
652 _db_rotate_log(); /* cache.log */
653 storeDirWriteCleanLogs(1);
654 storeLogRotate(); /* store.log */
655 accessLogRotate(); /* access.log */
656 useragentRotateLog(); /* useragent.log */
657 refererRotateLog(); /* referer.log */
670 authenticateInit(&Config
.authConfiguration
);
675 setEffectiveUser(void)
677 leave_suid(); /* Run as non privilegied user */
683 if (geteuid() == 0) {
684 debug(0, 0) ("Squid is not safe to run as root! If you must\n");
685 debug(0, 0) ("start Squid as root, then you must configure\n");
686 debug(0, 0) ("it to run as a non-priveledged user with the\n");
687 debug(0, 0) ("'cache_effective_user' option in the config file.\n");
688 fatal("Don't run Squid as root, set 'cache_effective_user'!");
695 char pathbuf
[MAXPATHLEN
];
697 if (Config
.coredump_dir
) {
698 if (0 == strcmp("none", Config
.coredump_dir
)) {
700 } else if (chdir(Config
.coredump_dir
) == 0) {
701 debug(0, 1) ("Set Current Directory to %s\n", Config
.coredump_dir
);
704 debug(50, 0) ("chdir: %s: %s\n", Config
.coredump_dir
, xstrerror());
708 /* If we don't have coredump_dir or couldn't cd there, report current dir */
709 if (getcwd(pathbuf
, MAXPATHLEN
)) {
710 debug(0, 1) ("Current Directory is %s\n", pathbuf
);
712 debug(50, 0) ("WARNING: Can't find current directory, getcwd: %s\n", xstrerror());
717 #include "DelayPools.h"
723 /* chroot if configured to run inside chroot */
725 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
726 fatal("failed to chroot");
729 if (opt_catch_signals
) {
730 squid_signal(SIGSEGV
, death
, SA_NODEFER
| SA_RESETHAND
);
731 squid_signal(SIGBUS
, death
, SA_NODEFER
| SA_RESETHAND
);
734 squid_signal(SIGPIPE
, SIG_IGN
, SA_RESTART
);
735 squid_signal(SIGCHLD
, sig_child
, SA_NODEFER
| SA_RESTART
);
739 if (icpPortNumOverride
!= 1)
740 Config
.Port
.icp
= (u_short
) icpPortNumOverride
;
742 _db_init(Config
.Log
.log
, Config
.debugOptions
);
744 fd_open(fileno(debug_log
), FD_LOG
, Config
.Log
.log
);
748 log_trace_init("/tmp/squid.alloc");
752 debug(1, 0) ("Starting Squid Cache version %s for %s...\n",
758 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
759 debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name
, WIN32_OS_string
);
760 debug(1, 0) ("Service command line is: %s\n", WIN32_Service_Command_Line
);
762 debug(1, 0) ("Running on %s\n",WIN32_OS_string
);
766 debugs(1, 1, "Process ID " << getpid());
768 debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD
);
772 debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
774 if (WIN32_Socks_initialized
)
775 debug(1, 1)("Windows sockets initialized\n");
779 if (!configured_once
)
780 disk_init(); /* disk_init must go before ipcache_init() */
800 authenticateInit(&Config
.authConfiguration
);
808 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
810 httpReplyInitModule(); /* must go before accepting replies */
828 malloc_debug(0, malloc_debug_level
);
832 if (!configured_once
) {
841 /* after this point we want to see the mallinfo() output */
843 mimeInit(Config
.mimeTablePathname
);
850 FwdState::initModule();
851 /* register the modules in the cache manager menus */
852 accessLogRegisterWithCacheManager(manager
);
853 asnRegisterWithCacheManager(manager
);
854 authenticateRegisterWithCacheManager(&Config
.authConfiguration
, manager
);
857 carpRegisterWithCacheManager(manager
);
860 cbdataRegisterWithCacheManager(manager
);
861 /* These use separate calls so that the comm loops can eventually
866 commEPollRegisterWithCacheManager(manager
);
870 commKQueueRegisterWithCacheManager(manager
);
874 commPollRegisterWithCacheManager(manager
);
878 commSelectRegisterWithCacheManager(manager
);
881 clientdbRegisterWithCacheManager(manager
);
884 DelayPools::RegisterWithCacheManager(manager
);
887 DiskIOModule::RegisterAllModulesWithCacheManager(manager
);
890 dnsRegisterWithCacheManager(manager
);
894 externalAclRegisterWithCacheManager(manager
);
895 fqdncacheRegisterWithCacheManager(manager
);
896 FwdState::RegisterWithCacheManager(manager
);
897 httpHeaderRegisterWithCacheManager(manager
);
900 idnsRegisterWithCacheManager(manager
);
903 ipcacheRegisterWithCacheManager(manager
);
904 Mem::RegisterWithCacheManager(manager
);
905 netdbRegisterWitHCacheManager(manager
);
906 PconnModule::GetInstance()->registerWithCacheManager(manager
);
907 redirectRegisterWithCacheManager(manager
);
908 refreshRegisterWithCacheManager(manager
);
909 statRegisterWithCacheManager(manager
);
910 storeDigestRegisterWithCacheManager(manager
);
911 StoreFileSystem::RegisterAllFsWithCacheManager(manager
);
912 storeRegisterWithCacheManager(manager
);
915 StringRegistry::Instance().registerWithCacheManager(manager
);
920 xprofRegisterWithCacheManager(manager
);
935 serverConnectionsOpen();
937 if (theOutIcpConnection
>= 0) {
939 neighborsRegisterWithCacheManager(manager
);
942 if (Config
.chroot_dir
)
945 if (!configured_once
)
946 writePidFile(); /* write PID file */
948 #ifdef _SQUID_LINUX_THREADS_
950 squid_signal(SIGQUIT
, rotate_logs
, SA_RESTART
);
952 squid_signal(SIGTRAP
, sigusr2_handle
, SA_RESTART
);
956 squid_signal(SIGUSR1
, rotate_logs
, SA_RESTART
);
958 squid_signal(SIGUSR2
, sigusr2_handle
, SA_RESTART
);
962 squid_signal(SIGHUP
, reconfigure
, SA_RESTART
);
964 squid_signal(SIGTERM
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
966 squid_signal(SIGINT
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
970 squid_signal(SIGTTIN
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
976 debug(1, 1) ("Ready to serve requests.\n");
978 if (!configured_once
) {
979 eventAdd("storeMaintain", Store::Maintain
, NULL
, 1.0, 1);
981 if (Config
.onoff
.announce
)
982 eventAdd("start_announce", start_announce
, NULL
, 3600.0, 1);
984 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
986 eventAdd("fqdncache_purgelru", fqdncache_purgelru
, NULL
, 15.0, 1);
990 eventAdd("cpuProfiling", xprof_event
, NULL
, 1.0, 1);
994 eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools
, NULL
, 15.0, 1);
996 eventAdd("commCheckHalfClosed", commCheckHalfClosed
, NULL
, 1.0, false);
1002 #if USE_WIN32_SERVICE
1003 /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
1004 extern "C" void WINAPI
1005 SquidWinSvcMain(int argc
, char **argv
)
1007 SquidMain(argc
, argv
);
1011 SquidMain(int argc
, char **argv
)
1014 main(int argc
, char **argv
)
1019 #ifdef _SQUID_WIN32_
1026 sbrk_start
= sbrk(0);
1029 Debug::parseOptions("ALL,1");
1032 #if defined(SQUID_MAXFD_LIMIT)
1034 if (SQUID_MAXFD_LIMIT
< Squid_MaxFD
)
1035 Squid_MaxFD
= SQUID_MAXFD_LIMIT
;
1039 #ifdef _SQUID_WIN32_
1041 if ((WIN32_init_err
= WIN32_Subsystem_Init(&argc
, &argv
)))
1042 return WIN32_init_err
;
1046 /* call mallopt() before anything else */
1049 /* Round up all sizes to a multiple of this */
1050 mallopt(M_GRAIN
, 16);
1054 /* biggest size that is considered a small block */
1055 mallopt(M_MXFAST
, 256);
1059 /* allocate this many small blocks at once */
1060 mallopt(M_NLBLKS
, 32);
1063 #endif /* HAVE_MALLOPT */
1066 * The plan here is to set the umask to 007 (deny others for
1067 * read,write,execute), but only if the umask is not already
1068 * set. Unfortunately, there is no way to get the current
1069 * umask value without setting it.
1071 oldmask
= umask(S_IRWXO
);
1076 memset(&local_addr
, '\0', sizeof(struct IN_ADDR
));
1078 safe_inet_addr(localhost
, &local_addr
);
1080 memset(&any_addr
, '\0', sizeof(struct IN_ADDR
));
1082 safe_inet_addr("0.0.0.0", &any_addr
);
1084 memset(&no_addr
, '\0', sizeof(struct IN_ADDR
));
1086 safe_inet_addr("255.255.255.255", &no_addr
);
1088 squid_srandom(time(NULL
));
1092 squid_start
= current_time
;
1094 failure_notify
= fatal_dump
;
1096 #if USE_WIN32_SERVICE
1098 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1102 mainParseOptions(argc
, argv
);
1104 #if USE_WIN32_SERVICE
1106 if (opt_install_service
)
1108 WIN32_InstallService();
1112 if (opt_remove_service
)
1114 WIN32_RemoveService();
1118 if (opt_command_line
)
1120 WIN32_SetServiceCommandLine();
1126 /* parse configuration file
1127 * note: in "normal" case this used to be called from mainInitialize() */
1132 ConfigFile
= xstrdup(DefaultConfigFile
);
1134 assert(!configured_once
);
1140 storeFsInit(); /* required for config parsing */
1142 /* May not be needed for parsing, have not audited for such */
1143 DiskIOModule::SetupAllModules();
1145 /* Shouldn't be needed for config parsing, but have not audited for such */
1146 StoreFileSystem::SetupAllFs();
1148 /* we may want the parsing process to set this up in the future */
1149 Store::Root(new StoreController
);
1151 parse_err
= parseConfigFile(ConfigFile
, manager
);
1153 if (opt_parse_cfg_only
)
1157 if (-1 == opt_send_signal
)
1158 if (checkRunningPid())
1175 /* send signal to running copy and exit */
1176 if (opt_send_signal
!= -1)
1178 /* chroot if configured to run inside chroot */
1180 if (Config
.chroot_dir
) {
1181 if (chroot(Config
.chroot_dir
))
1182 fatal("failed to chroot");
1193 if (opt_create_swap_dirs
)
1195 /* chroot if configured to run inside chroot */
1197 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1198 fatal("failed to chroot");
1202 debug(0, 0) ("Creating Swap Directories\n");
1203 Store::Root().create();
1213 /* init comm module */
1220 /* we have to init fdstat here. */
1221 fd_open(0, FD_LOG
, "stdin");
1222 fd_open(1, FD_LOG
, "stdout");
1223 fd_open(2, FD_LOG
, "stderr");
1226 #if USE_WIN32_SERVICE
1228 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1234 #if USE_WIN32_SERVICE
1236 WIN32_svcstatusupdate(SERVICE_RUNNING
, 0);
1244 if (do_reconfigure
) {
1247 #if defined(_SQUID_MSWIN_) && defined(_DEBUG)
1249 } else if (do_debug_trap
) {
1254 } else if (do_rotate
) {
1257 } else if (do_shutdown
) {
1258 time_t wait
= do_shutdown
> 0 ? (int) Config
.shutdownLifetime
: 0;
1259 debug(1, 1) ("Preparing for shutdown after %d requests\n",
1260 statCounter
.client_http
.requests
);
1261 debug(1, 1) ("Waiting %d seconds for active connections to finish\n",
1265 #if USE_WIN32_SERVICE
1267 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, (wait
+ 1) * 1000);
1270 serverConnectionsClose();
1271 eventAdd("SquidShutdown", SquidShutdown
, NULL
, (double) (wait
+ 1), 1);
1276 /* Attempt any pending storedir IO
1277 * Note: the storedir is roughly a reactor of its own.
1279 Store::Root().callback();
1281 comm_calliocallback();
1283 /* and again to deal with indirectly queued events
1284 * resulting from the first call. These are usually
1285 * callbacks and should be dealt with immediately.
1288 if (comm_iocallbackpending())
1289 comm_calliocallback();
1291 int loop_delay
= eventNextTime();
1296 switch (comm_select(loop_delay
)) {
1299 errcount
= 0; /* reset if successful */
1304 debug(1, 0) ("Select loop Error. Retry %d\n", errcount
);
1307 fatal_dump("Select Loop failed!");
1315 SquidShutdown(NULL
);
1320 fatal_dump("MAIN: Internal error -- this should never happen.");
1336 if (strcmp(Config
.pidFilename
, "none") == 0) {
1337 debug(0, 1) ("No pid_filename specified. Trusting you know what you are doing.\n");
1340 pid
= readPidFile();
1343 #if USE_WIN32_SERVICE
1345 if (opt_signal_service
) {
1346 WIN32_sendSignal(opt_send_signal
);
1349 #ifdef _SQUID_MSWIN_
1351 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1352 fprintf(stderr
, "signal to Squid Service:\n");
1353 fprintf(stderr
, "missing -n command line switch.\n");
1362 if (kill(pid
, opt_send_signal
) &&
1363 /* ignore permissions if just running check */
1364 !(opt_send_signal
== 0 && errno
== EPERM
)) {
1365 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1366 fprintf(stderr
, "signal %d to process %d: %s\n",
1367 opt_send_signal
, (int) pid
, xstrerror());
1371 fprintf(stderr
, "%s: ERROR: No running copy\n", appname
);
1375 /* signal successfully sent */
1379 #ifndef _SQUID_MSWIN_
1381 * This function is run when Squid is in daemon mode, just
1382 * before the parent forks and starts up the child process.
1383 * It can be used for admin-specific tasks, such as notifying
1384 * someone that Squid is (re)started.
1387 mainStartScript(const char *prog
)
1389 char script
[SQUID_MAXPATHLEN
];
1394 xstrncpy(script
, prog
, MAXPATHLEN
);
1396 if ((t
= strrchr(script
, '/'))) {
1398 sl
= strlen(script
);
1401 xstrncpy(&script
[sl
], squid_start_script
, MAXPATHLEN
- sl
);
1403 if ((cpid
= fork()) == 0) {
1405 execl(script
, squid_start_script
, (char *)NULL
);
1411 rpid
= wait3(&status
, 0, NULL
);
1415 rpid
= waitpid(-1, &status
, 0);
1418 } while (rpid
!= cpid
);
1422 #endif /* _SQUID_MSWIN_ */
1425 checkRunningPid(void)
1432 pid
= readPidFile();
1437 if (kill(pid
, 0) < 0)
1440 debugs(0, 0, "Squid is already running! Process ID " << pid
);
1446 watch_child(char *argv
[])
1448 #ifndef _SQUID_MSWIN_
1469 if (*(argv
[0]) == '(')
1472 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1474 if ((pid
= fork()) < 0)
1475 syslog(LOG_ALERT
, "fork failed: %s", xstrerror());
1480 syslog(LOG_ALERT
, "setsid failed: %s", xstrerror());
1486 if ((i
= open("/dev/tty", O_RDWR
| O_TEXT
)) >= 0) {
1487 ioctl(i
, TIOCNOTTY
, NULL
);
1495 * RBCOLLINS - if cygwin stackdumps when squid is run without
1496 * -N, check the cygwin1.dll version, it needs to be AT LEAST
1497 * 1.1.3. execvp had a bit overflow error in a loop..
1499 /* Connect stdio to /dev/null in daemon mode */
1500 nullfd
= open("/dev/null", O_RDWR
| O_TEXT
);
1503 fatalf("/dev/null: %s\n", xstrerror());
1507 if (opt_debug_stderr
< 0) {
1513 mainStartScript(argv
[0]);
1515 if ((pid
= fork()) == 0) {
1517 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1518 prog
= xstrdup(argv
[0]);
1519 argv
[0] = xstrdup("(squid)");
1521 syslog(LOG_ALERT
, "execvp failed: %s", xstrerror());
1525 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1527 syslog(LOG_NOTICE
, "Squid Parent: child process %d started", pid
);
1531 squid_signal(SIGINT
, SIG_IGN
, SA_RESTART
);
1535 pid
= wait3(&status
, 0, NULL
);
1539 pid
= waitpid(-1, &status
, 0);
1545 if (WIFEXITED(status
)) {
1547 "Squid Parent: child process %d exited with status %d",
1548 pid
, WEXITSTATUS(status
));
1549 } else if (WIFSIGNALED(status
)) {
1551 "Squid Parent: child process %d exited due to signal %d",
1552 pid
, WTERMSIG(status
));
1554 syslog(LOG_NOTICE
, "Squid Parent: child process %d exited", pid
);
1557 if (stop
- start
< 10)
1562 if (failcount
== 5) {
1563 syslog(LOG_ALERT
, "Exiting due to repeated, frequent failures");
1567 if (WIFEXITED(status
))
1568 if (WEXITSTATUS(status
) == 0)
1571 if (WIFSIGNALED(status
)) {
1572 switch (WTERMSIG(status
)) {
1583 squid_signal(SIGINT
, SIG_DFL
, SA_RESTART
);
1588 #endif /* _SQUID_MSWIN_ */
1593 SquidShutdown(void *unused
)
1595 #if USE_WIN32_SERVICE
1596 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1599 debug(1, 1) ("Shutting down...\n");
1609 externalAclShutdown();
1610 icpConnectionClose();
1617 snmpConnectionClose();
1621 wccpConnectionClose();
1625 wccp2ConnectionClose();
1628 releaseServerSockets();
1629 commCloseAllSockets();
1632 DelayPools::FreePools();
1635 authenticateShutdown();
1640 #if USE_WIN32_SERVICE
1642 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1645 Store::Root().sync(); /* Flush pending object writes/unlinks */
1646 storeDirWriteCleanLogs(0);
1649 Store::Root().sync(); /* Flush log writes */
1652 useragentLogClose();
1659 Store::Root().sync(); /* Flush log close */
1660 StoreFileSystem::FreeAllFs();
1661 DiskIOModule::FreeAllModules();
1662 #if PURIFY || XMALLOC_TRACE
1666 /*stmemFreeMemory(); */
1668 ipcacheFreeMemory();
1669 fqdncacheFreeMemory();
1671 clientdbFreeMemory();
1672 httpHeaderCleanModule();
1680 if (opt_no_daemon
) {
1695 xmalloc_find_leaks();
1697 debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total
);
1706 if (Config
.pidFilename
&& strcmp(Config
.pidFilename
, "none") != 0) {
1708 safeunlink(Config
.pidFilename
, 0);
1712 debug(1, 1) ("Squid Cache (Version %s): Exiting normally.\n",
1718 exit(shutdown_status
);