3 * $Id: main.cc,v 1.418 2006/04/29 13:53:16 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"
41 #include "HttpReply.h"
46 #include "StoreFileSystem.h"
47 #include "DiskIO/DiskIOModule.h"
54 #include "squid_windows.h"
57 static int opt_install_service
= FALSE
;
58 static int opt_remove_service
= FALSE
;
59 static int opt_signal_service
= FALSE
;
60 static int opt_command_line
= FALSE
;
61 extern void WIN32_svcstatusupdate(DWORD
, DWORD
);
62 void WINAPI
WIN32_svcHandler(DWORD
);
66 /* for error reporting from xmalloc and friends */
67 SQUIDCEXTERN
void (*failure_notify
) (const char *);
69 static int opt_parse_cfg_only
= 0;
70 static char *opt_syslog_facility
= NULL
;
71 static int icpPortNumOverride
= 1; /* Want to detect "-u 0" */
72 static int configured_once
= 0;
74 static int malloc_debug_level
= 0;
76 static volatile int do_reconfigure
= 0;
77 static volatile int do_rotate
= 0;
78 static volatile int do_shutdown
= 0;
80 static void mainRotate(void);
81 static void mainReconfigure(void);
82 static void mainInitialize(void);
83 static void usage(void);
84 static void mainParseOptions(int, char **);
85 static void sendSignal(void);
86 static void serverConnectionsOpen(void);
87 static void watch_child(char **);
88 static void setEffectiveUser(void);
90 extern void log_trace_done();
91 extern void log_trace_init(char *);
93 static EVH SquidShutdown
;
94 static void mainSetCwd(void);
95 static int checkRunningPid(void);
98 static const char *squid_start_script
= "squid_start";
102 #include "test_access.c"
109 #if USE_WIN32_SERVICE
110 "Usage: %s [-cdhirvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal] [-n name] [-O CommandLine]\n"
112 "Usage: %s [-cdhvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]\n"
114 " -a port Specify HTTP port number (default: %d).\n"
115 " -d level Write debugging to stderr also.\n"
116 " -f file Use given config-file instead of\n"
118 " -h Print help message.\n"
119 #if USE_WIN32_SERVICE
120 " -i Installs as a Windows Service (see -n option).\n"
122 " -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse\n"
123 " Parse configuration file, then send signal to \n"
124 " running copy (except -k parse) and exit.\n"
125 #if USE_WIN32_SERVICE
126 " -n name Specify Windows Service name to use for service operations\n"
127 " default is: " _WIN_SQUID_DEFAULT_SERVICE_NAME
".\n"
128 " -r Removes a Windows Service (see -n option).\n"
130 " -s | -l facility\n"
131 " Enable logging to syslog.\n"
132 " -u port Specify ICP port number (default: %d), disable with 0.\n"
133 " -v Print version.\n"
134 " -z Create swap directories\n"
135 " -C Do not catch fatal signals.\n"
136 " -D Disable initial DNS tests.\n"
137 " -F Don't serve any requests until store is rebuilt.\n"
138 " -N No daemon mode.\n"
139 #if USE_WIN32_SERVICE
141 " Set Windows Service Command line options in Registry.\n"
143 " -R Do not set REUSEADDR on port.\n"
144 " -S Double-check swap during rebuild.\n"
145 " -V Virtual host httpd-accelerator.\n"
146 " -X Force full debugging.\n"
147 " -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.\n",
148 appname
, CACHE_HTTP_PORT
, DefaultConfigFile
, CACHE_ICP_PORT
);
153 mainParseOptions(int argc
, char *argv
[])
158 #if USE_WIN32_SERVICE
160 while ((c
= getopt(argc
, argv
, "CDFNO:RSVYXa:d:f:hik:m::n:rsl:u:vz?")) != -1)
163 while ((c
= getopt(argc
, argv
, "CDFNRSVYXa:d:f:hk:m::sl:u:vz?")) != -1)
172 opt_catch_signals
= 0;
180 opt_foreground_rebuild
= 1;
186 #if USE_WIN32_SERVICE
189 opt_command_line
= 1;
190 WIN32_Command_Line
= xstrdup(optarg
);
199 opt_store_doublecheck
= 1;
204 if (Config
.Sockaddr
.http
)
205 Config
.Sockaddr
.http
->vhost
= 1;
209 else if (Config
.Sockaddr
.https
)
210 Config
.Sockaddr
.https
->http
.vhost
= 1;
215 fatal("No http_port specified\n");
220 /* force full debugging */
221 sigusr2_handle(SIGUSR2
);
226 opt_reload_hit_only
= 1;
230 #if USE_WIN32_SERVICE
233 opt_install_service
= TRUE
;
240 add_http_port(optarg
);
245 opt_debug_stderr
= atoi(optarg
);
252 ConfigFile
= xstrdup(optarg
);
262 if ((int) strlen(optarg
) < 1)
265 if (!strncmp(optarg
, "reconfigure", strlen(optarg
)))
266 opt_send_signal
= SIGHUP
;
267 else if (!strncmp(optarg
, "rotate", strlen(optarg
)))
268 #ifdef _SQUID_LINUX_THREADS_
270 opt_send_signal
= SIGQUIT
;
274 opt_send_signal
= SIGUSR1
;
278 else if (!strncmp(optarg
, "debug", strlen(optarg
)))
279 #ifdef _SQUID_LINUX_THREADS_
281 opt_send_signal
= SIGTRAP
;
285 opt_send_signal
= SIGUSR2
;
289 else if (!strncmp(optarg
, "shutdown", strlen(optarg
)))
290 opt_send_signal
= SIGTERM
;
291 else if (!strncmp(optarg
, "interrupt", strlen(optarg
)))
292 opt_send_signal
= SIGINT
;
293 else if (!strncmp(optarg
, "kill", strlen(optarg
)))
294 opt_send_signal
= SIGKILL
;
295 else if (!strncmp(optarg
, "check", strlen(optarg
)))
296 opt_send_signal
= 0; /* SIGNULL */
297 else if (!strncmp(optarg
, "parse", strlen(optarg
)))
298 opt_parse_cfg_only
= 1; /* parse cfg file only */
307 malloc_debug_level
= atoi(optarg
);
312 fatal("Need to add -DMALLOC_DBG when compiling to use -mX option");
318 xmalloc_trace
= !xmalloc_trace
;
321 fatal("Need to configure --enable-xmalloc-debug-trace to use -m option");
326 #if USE_WIN32_SERVICE
329 xfree(WIN32_Service_name
);
331 WIN32_Service_name
= xstrdup(optarg
);
333 opt_signal_service
= TRUE
;
338 opt_remove_service
= TRUE
;
345 opt_syslog_facility
= xstrdup(optarg
);
350 _db_set_syslog(opt_syslog_facility
);
356 fatal("Logging to syslog not available on this platform");
362 icpPortNumOverride
= atoi(optarg
);
364 if (icpPortNumOverride
< 0)
365 icpPortNumOverride
= 0;
370 printf("Squid Cache: Version %s\nconfigure options: %s\n", version_string
, SQUID_CONFIGURE_OPTIONS
);
372 #if USE_WIN32_SERVICE
374 printf("Compiled as Windows System Service.\n");
383 opt_debug_stderr
= 1;
385 opt_create_swap_dirs
= 1;
405 #ifndef _SQUID_MSWIN_
408 signal(sig
, rotate_logs
);
418 #ifndef _SQUID_MSWIN_
421 signal(sig
, reconfigure
);
429 do_shutdown
= sig
== SIGINT
? -1 : 1;
430 #ifndef _SQUID_MSWIN_
431 #ifdef KILL_PARENT_OPT
434 debugs(1, 1, "Killing RunCache, pid " << getppid());
436 if (kill(getppid(), sig
) < 0)
437 debugs(1, 1, "kill " << getppid() << ": " << xstrerror());
441 #if SA_RESETHAND == 0
442 signal(SIGTERM
, SIG_DFL
);
444 signal(SIGINT
, SIG_DFL
);
451 serverConnectionsOpen(void)
453 clientOpenListenSockets();
454 icpConnectionsOpen();
461 snmpConnectionOpen();
465 wccpConnectionOpen();
481 serverConnectionsClose(void)
483 assert(shutting_down
|| reconfiguring
);
484 clientHttpConnectionsClose();
485 icpConnectionShutdown();
488 htcpSocketShutdown();
494 snmpConnectionShutdown();
498 wccpConnectionShutdown();
505 mainReconfigure(void)
507 debug(1, 1) ("Reconfiguring Squid Cache (version %s)...\n", version_string
);
509 /* Already called serverConnectionsClose and ipcacheShutdownServers() */
510 serverConnectionsClose();
511 icpConnectionClose();
518 snmpConnectionClose();
522 wccpConnectionClose();
533 authenticateShutdown();
534 externalAclShutdown();
535 storeDirCloseSwapLogs();
541 enter_suid(); /* root to read config file */
542 parseConfigFile(ConfigFile
);
544 _db_init(Config
.Log
.log
, Config
.debugOptions
);
545 ipcache_restart(); /* clear stuck entries */
546 authenticateUserCacheRestart(); /* clear stuck ACL entries */
547 fqdncache_restart(); /* sigh, fqdncache too */
549 errorInitialize(); /* reload error pages */
563 authenticateInit(&Config
.authConfiguration
);
570 serverConnectionsOpen();
572 if (theOutIcpConnection
>= 0)
575 storeDirOpenSwapLogs();
577 mimeInit(Config
.mimeTablePathname
);
579 writePidFile(); /* write PID file */
581 debug(1, 1) ("Ready to serve requests.\n");
596 authenticateShutdown();
597 externalAclShutdown();
598 _db_rotate_log(); /* cache.log */
599 storeDirWriteCleanLogs(1);
600 storeLogRotate(); /* store.log */
601 accessLogRotate(); /* access.log */
602 useragentRotateLog(); /* useragent.log */
603 refererRotateLog(); /* referer.log */
616 authenticateInit(&Config
.authConfiguration
);
621 setEffectiveUser(void)
623 leave_suid(); /* Run as non privilegied user */
629 if (geteuid() == 0) {
630 debug(0, 0) ("Squid is not safe to run as root! If you must\n");
631 debug(0, 0) ("start Squid as root, then you must configure\n");
632 debug(0, 0) ("it to run as a non-priveledged user with the\n");
633 debug(0, 0) ("'cache_effective_user' option in the config file.\n");
634 fatal("Don't run Squid as root, set 'cache_effective_user'!");
641 char pathbuf
[MAXPATHLEN
];
643 if (Config
.coredump_dir
) {
644 if (0 == strcmp("none", Config
.coredump_dir
)) {
646 } else if (chdir(Config
.coredump_dir
) == 0) {
647 debug(0, 1) ("Set Current Directory to %s\n", Config
.coredump_dir
);
650 debug(50, 0) ("chdir: %s: %s\n", Config
.coredump_dir
, xstrerror());
654 /* If we don't have coredump_dir or couldn't cd there, report current dir */
655 if (getcwd(pathbuf
, MAXPATHLEN
)) {
656 debug(0, 1) ("Current Directory is %s\n", pathbuf
);
658 debug(50, 0) ("WARNING: Can't find current directory, getcwd: %s\n", xstrerror());
663 #include "DelayPools.h"
669 /* chroot if configured to run inside chroot */
671 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
672 fatal("failed to chroot");
675 if (opt_catch_signals
) {
676 squid_signal(SIGSEGV
, death
, SA_NODEFER
| SA_RESETHAND
);
677 squid_signal(SIGBUS
, death
, SA_NODEFER
| SA_RESETHAND
);
680 squid_signal(SIGPIPE
, SIG_IGN
, SA_RESTART
);
681 squid_signal(SIGCHLD
, sig_child
, SA_NODEFER
| SA_RESTART
);
685 if (icpPortNumOverride
!= 1)
686 Config
.Port
.icp
= (u_short
) icpPortNumOverride
;
688 _db_init(Config
.Log
.log
, Config
.debugOptions
);
690 fd_open(fileno(debug_log
), FD_LOG
, Config
.Log
.log
);
694 log_trace_init("/tmp/squid.alloc");
698 debug(1, 0) ("Starting Squid Cache version %s for %s...\n",
704 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
705 debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name
, WIN32_OS_string
);
706 debug(1, 0) ("Service command line is: %s\n", WIN32_Service_Command_Line
);
708 debug(1, 0) ("Running on %s\n",WIN32_OS_string
);
712 debugs(1, 1, "Process ID " << getpid());
714 debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD
);
718 debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
720 if (WIN32_Socks_initialized
)
721 debug(1, 1)("Windows sockets initialized\n");
725 if (!configured_once
)
726 disk_init(); /* disk_init must go before ipcache_init() */
746 authenticateInit(&Config
.authConfiguration
);
754 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
756 httpReplyInitModule(); /* must go before accepting replies */
774 malloc_debug(0, malloc_debug_level
);
778 if (!configured_once
) {
788 /* after this point we want to see the mallinfo() output */
790 mimeInit(Config
.mimeTablePathname
);
797 FwdState::initModule();
805 serverConnectionsOpen();
807 if (theOutIcpConnection
>= 0)
810 if (Config
.chroot_dir
)
813 if (!configured_once
)
814 writePidFile(); /* write PID file */
816 #ifdef _SQUID_LINUX_THREADS_
818 squid_signal(SIGQUIT
, rotate_logs
, SA_RESTART
);
820 squid_signal(SIGTRAP
, sigusr2_handle
, SA_RESTART
);
824 squid_signal(SIGUSR1
, rotate_logs
, SA_RESTART
);
826 squid_signal(SIGUSR2
, sigusr2_handle
, SA_RESTART
);
830 squid_signal(SIGHUP
, reconfigure
, SA_RESTART
);
832 squid_signal(SIGTERM
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
834 squid_signal(SIGINT
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
838 debug(1, 1) ("Ready to serve requests.\n");
840 if (!configured_once
) {
841 eventAdd("storeMaintain", Store::Maintain
, NULL
, 1.0, 1);
843 if (Config
.onoff
.announce
)
844 eventAdd("start_announce", start_announce
, NULL
, 3600.0, 1);
846 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
848 eventAdd("fqdncache_purgelru", fqdncache_purgelru
, NULL
, 15.0, 1);
852 eventAdd("cpuProfiling", xprof_event
, NULL
, 1.0, 1);
856 eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools
, NULL
, 15.0, 1);
858 eventAdd("commCheckHalfClosed", commCheckHalfClosed
, NULL
, 1.0, false);
864 #if USE_WIN32_SERVICE
865 /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
866 extern "C" void WINAPI
867 SquidWinSvcMain(int argc
, char **argv
)
869 SquidMain(argc
, argv
);
873 SquidMain(int argc
, char **argv
)
876 main(int argc
, char **argv
)
888 sbrk_start
= sbrk(0);
891 Debug::parseOptions("ALL,1");
894 if (FD_SETSIZE
< Squid_MaxFD
)
895 Squid_MaxFD
= FD_SETSIZE
;
899 if ((WIN32_init_err
= WIN32_Subsystem_Init(&argc
, &argv
)))
900 return WIN32_init_err
;
904 /* call mallopt() before anything else */
907 /* Round up all sizes to a multiple of this */
908 mallopt(M_GRAIN
, 16);
912 /* biggest size that is considered a small block */
913 mallopt(M_MXFAST
, 256);
917 /* allocate this many small blocks at once */
918 mallopt(M_NLBLKS
, 32);
921 #endif /* HAVE_MALLOPT */
924 * The plan here is to set the umask to 007 (deny others for
925 * read,write,execute), but only if the umask is not already
926 * set. Unfortunately, there is no way to get the current
927 * umask value without setting it.
929 oldmask
= umask(S_IRWXO
);
934 memset(&local_addr
, '\0', sizeof(struct IN_ADDR
));
936 safe_inet_addr(localhost
, &local_addr
);
938 memset(&any_addr
, '\0', sizeof(struct IN_ADDR
));
940 safe_inet_addr("0.0.0.0", &any_addr
);
942 memset(&no_addr
, '\0', sizeof(struct IN_ADDR
));
944 safe_inet_addr("255.255.255.255", &no_addr
);
946 squid_srandom(time(NULL
));
950 squid_start
= current_time
;
952 failure_notify
= fatal_dump
;
954 #if USE_WIN32_SERVICE
956 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
960 mainParseOptions(argc
, argv
);
962 #if USE_WIN32_SERVICE
964 if (opt_install_service
)
966 WIN32_InstallService();
970 if (opt_remove_service
)
972 WIN32_RemoveService();
976 if (opt_command_line
)
978 WIN32_SetServiceCommandLine();
984 /* parse configuration file
985 * note: in "normal" case this used to be called from mainInitialize() */
990 ConfigFile
= xstrdup(DefaultConfigFile
);
992 assert(!configured_once
);
998 eventInit(); /* eventInit() is required for config parsing */
1000 storeFsInit(); /* required for config parsing */
1002 /* May not be needed for parsing, have not audited for such */
1003 DiskIOModule::SetupAllModules();
1005 /* Shouldn't be needed for config parsing, but have not audited for such */
1006 StoreFileSystem::SetupAllFs();
1008 /* we may want the parsing process to set this up in the future */
1009 Store::Root(new StoreController
);
1011 parse_err
= parseConfigFile(ConfigFile
);
1013 if (opt_parse_cfg_only
)
1017 if (-1 == opt_send_signal
)
1018 if (checkRunningPid())
1035 /* send signal to running copy and exit */
1036 if (opt_send_signal
!= -1)
1038 /* chroot if configured to run inside chroot */
1040 if (Config
.chroot_dir
) {
1041 if (chroot(Config
.chroot_dir
))
1042 fatal("failed to chroot");
1053 if (opt_create_swap_dirs
)
1055 /* chroot if configured to run inside chroot */
1057 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1058 fatal("failed to chroot");
1062 debug(0, 0) ("Creating Swap Directories\n");
1063 Store::Root().create();
1073 /* init comm module */
1080 /* we have to init fdstat here. */
1081 fd_open(0, FD_LOG
, "stdin");
1082 fd_open(1, FD_LOG
, "stdout");
1083 fd_open(2, FD_LOG
, "stderr");
1086 #if USE_WIN32_SERVICE
1088 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1094 #if USE_WIN32_SERVICE
1096 WIN32_svcstatusupdate(SERVICE_RUNNING
, 0);
1104 if (do_reconfigure
) {
1107 #if defined(_SQUID_MSWIN_) && defined(_DEBUG)
1109 } else if (do_debug_trap
) {
1114 } else if (do_rotate
) {
1117 } else if (do_shutdown
) {
1118 time_t wait
= do_shutdown
> 0 ? (int) Config
.shutdownLifetime
: 0;
1119 debug(1, 1) ("Preparing for shutdown after %d requests\n",
1120 statCounter
.client_http
.requests
);
1121 debug(1, 1) ("Waiting %d seconds for active connections to finish\n",
1125 #if USE_WIN32_SERVICE
1127 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, (wait
+ 1) * 1000);
1130 serverConnectionsClose();
1131 eventAdd("SquidShutdown", SquidShutdown
, NULL
, (double) (wait
+ 1), 1);
1136 /* Attempt any pending storedir IO
1137 * Note: the storedir is roughly a reactor of its own.
1139 Store::Root().callback();
1141 comm_calliocallback();
1143 /* and again to deal with indirectly queued events
1144 * resulting from the first call. These are usually
1145 * callbacks and should be dealt with immediately.
1148 if (comm_iocallbackpending())
1149 comm_calliocallback();
1151 int loop_delay
= eventNextTime();
1156 switch (comm_select(loop_delay
)) {
1159 errcount
= 0; /* reset if successful */
1164 debug(1, 0) ("Select loop Error. Retry %d\n", errcount
);
1167 fatal_dump("Select Loop failed!");
1175 SquidShutdown(NULL
);
1180 fatal_dump("MAIN: Internal error -- this should never happen.");
1196 if (strcmp(Config
.pidFilename
, "none") == 0) {
1197 debug(0, 1) ("No pid_filename specified. Trusting you know what you are doing.\n");
1200 pid
= readPidFile();
1203 #if USE_WIN32_SERVICE
1205 if (opt_signal_service
) {
1206 WIN32_sendSignal(opt_send_signal
);
1209 #ifdef _SQUID_MSWIN_
1211 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1212 fprintf(stderr
, "signal to Squid Service:\n");
1213 fprintf(stderr
, "missing -n command line switch.\n");
1222 if (kill(pid
, opt_send_signal
) &&
1223 /* ignore permissions if just running check */
1224 !(opt_send_signal
== 0 && errno
== EPERM
)) {
1225 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1226 fprintf(stderr
, "signal %d to process %d: %s\n",
1227 opt_send_signal
, (int) pid
, xstrerror());
1231 fprintf(stderr
, "%s: ERROR: No running copy\n", appname
);
1235 /* signal successfully sent */
1239 #ifndef _SQUID_MSWIN_
1241 * This function is run when Squid is in daemon mode, just
1242 * before the parent forks and starts up the child process.
1243 * It can be used for admin-specific tasks, such as notifying
1244 * someone that Squid is (re)started.
1247 mainStartScript(const char *prog
)
1249 char script
[SQUID_MAXPATHLEN
];
1254 xstrncpy(script
, prog
, MAXPATHLEN
);
1256 if ((t
= strrchr(script
, '/'))) {
1258 sl
= strlen(script
);
1261 xstrncpy(&script
[sl
], squid_start_script
, MAXPATHLEN
- sl
);
1263 if ((cpid
= fork()) == 0) {
1265 execl(script
, squid_start_script
, (char *)NULL
);
1271 rpid
= wait3(&status
, 0, NULL
);
1275 rpid
= waitpid(-1, &status
, 0);
1278 } while (rpid
!= cpid
);
1282 #endif /* _SQUID_MSWIN_ */
1285 checkRunningPid(void)
1292 pid
= readPidFile();
1297 if (kill(pid
, 0) < 0)
1300 debugs(0, 0, "Squid is already running! Process ID " << pid
);
1306 watch_child(char *argv
[])
1308 #ifndef _SQUID_MSWIN_
1329 if (*(argv
[0]) == '(')
1332 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1334 if ((pid
= fork()) < 0)
1335 syslog(LOG_ALERT
, "fork failed: %s", xstrerror());
1340 syslog(LOG_ALERT
, "setsid failed: %s", xstrerror());
1346 if ((i
= open("/dev/tty", O_RDWR
| O_TEXT
)) >= 0) {
1347 ioctl(i
, TIOCNOTTY
, NULL
);
1355 * RBCOLLINS - if cygwin stackdumps when squid is run without
1356 * -N, check the cygwin1.dll version, it needs to be AT LEAST
1357 * 1.1.3. execvp had a bit overflow error in a loop..
1359 /* Connect stdio to /dev/null in daemon mode */
1360 nullfd
= open("/dev/null", O_RDWR
| O_TEXT
);
1363 fatalf("/dev/null: %s\n", xstrerror());
1367 if (opt_debug_stderr
< 0) {
1373 mainStartScript(argv
[0]);
1375 if ((pid
= fork()) == 0) {
1377 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1378 prog
= xstrdup(argv
[0]);
1379 argv
[0] = xstrdup("(squid)");
1381 syslog(LOG_ALERT
, "execvp failed: %s", xstrerror());
1385 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1387 syslog(LOG_NOTICE
, "Squid Parent: child process %d started", pid
);
1391 squid_signal(SIGINT
, SIG_IGN
, SA_RESTART
);
1395 pid
= wait3(&status
, 0, NULL
);
1399 pid
= waitpid(-1, &status
, 0);
1405 if (WIFEXITED(status
)) {
1407 "Squid Parent: child process %d exited with status %d",
1408 pid
, WEXITSTATUS(status
));
1409 } else if (WIFSIGNALED(status
)) {
1411 "Squid Parent: child process %d exited due to signal %d",
1412 pid
, WTERMSIG(status
));
1414 syslog(LOG_NOTICE
, "Squid Parent: child process %d exited", pid
);
1417 if (stop
- start
< 10)
1422 if (failcount
== 5) {
1423 syslog(LOG_ALERT
, "Exiting due to repeated, frequent failures");
1427 if (WIFEXITED(status
))
1428 if (WEXITSTATUS(status
) == 0)
1431 if (WIFSIGNALED(status
)) {
1432 switch (WTERMSIG(status
)) {
1443 squid_signal(SIGINT
, SIG_DFL
, SA_RESTART
);
1448 #endif /* _SQUID_MSWIN_ */
1453 SquidShutdown(void *unused
)
1455 #if USE_WIN32_SERVICE
1456 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1459 debug(1, 1) ("Shutting down...\n");
1469 externalAclShutdown();
1470 icpConnectionClose();
1477 snmpConnectionClose();
1481 wccpConnectionClose();
1484 releaseServerSockets();
1485 commCloseAllSockets();
1488 DelayPools::FreePools();
1491 authenticateShutdown();
1496 #if USE_WIN32_SERVICE
1498 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1501 Store::Root().sync(); /* Flush pending object writes/unlinks */
1502 storeDirWriteCleanLogs(0);
1505 Store::Root().sync(); /* Flush log writes */
1508 useragentLogClose();
1515 Store::Root().sync(); /* Flush log close */
1516 StoreFileSystem::FreeAllFs();
1517 DiskIOModule::FreeAllModules();
1518 #if PURIFY || XMALLOC_TRACE
1522 /*stmemFreeMemory(); */
1524 ipcacheFreeMemory();
1525 fqdncacheFreeMemory();
1527 clientdbFreeMemory();
1528 httpHeaderCleanModule();
1536 if (opt_no_daemon
) {
1551 xmalloc_find_leaks();
1553 debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total
);
1562 if (Config
.pidFilename
&& strcmp(Config
.pidFilename
, "none") != 0) {
1564 safeunlink(Config
.pidFilename
, 0);
1568 debug(1, 1) ("Squid Cache (Version %s): Exiting normally.\n",