3 * $Id: main.cc,v 1.384 2003/07/15 06:50:42 robertc 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 "authenticate.h"
40 #include "HttpReply.h"
48 #include "squid_windows.h"
51 static int opt_install_service
= FALSE
;
52 static int opt_remove_service
= FALSE
;
53 static int opt_signal_service
= FALSE
;
54 static int opt_command_line
= FALSE
;
55 extern void WIN32_svcstatusupdate(DWORD
, DWORD
);
56 void WINAPI
WIN32_svcHandler(DWORD
);
60 /* for error reporting from xmalloc and friends */
61 SQUIDCEXTERN
void (*failure_notify
) (const char *);
63 static int opt_send_signal
= -1;
64 static int opt_parse_cfg_only
= 0;
65 static int icpPortNumOverride
= 1; /* Want to detect "-u 0" */
66 static int configured_once
= 0;
68 static int malloc_debug_level
= 0;
70 static volatile int do_reconfigure
= 0;
71 static volatile int do_rotate
= 0;
72 static volatile int do_shutdown
= 0;
74 static void mainRotate(void);
75 static void mainReconfigure(void);
76 static void mainInitialize(void);
77 static void usage(void);
78 static void mainParseOptions(int, char **);
79 static void sendSignal(void);
80 static void serverConnectionsOpen(void);
81 static void watch_child(char **);
82 static void setEffectiveUser(void);
84 extern void log_trace_done();
85 extern void log_trace_init(char *);
87 static EVH SquidShutdown
;
88 static void mainSetCwd(void);
89 static int checkRunningPid(void);
92 static const char *squid_start_script
= "squid_start";
96 #include "test_access.c"
103 #if USE_WIN32_SERVICE
104 "Usage: %s [-dhirsvzCDFNRVYX] [-f config-file] [-[au] port] [-k signal] [-n name] [-O CommandLine]\n"
106 "Usage: %s [-dhsvzCDFNRVYX] [-f config-file] [-[au] port] [-k signal]\n"
108 " -a port Specify HTTP port number (default: %d).\n"
109 " -d level Write debugging to stderr also.\n"
110 " -f file Use given config-file instead of\n"
112 " -h Print help message.\n"
113 #if USE_WIN32_SERVICE
114 " -i Installs as a Windows Service (see -n option).\n"
116 " -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse\n"
117 " Parse configuration file, then send signal to \n"
118 " running copy (except -k parse) and exit.\n"
119 #if USE_WIN32_SERVICE
120 " -n name Specify Windows Service name to use for service operations\n"
121 " default is: " _WIN_SQUID_DEFAULT_SERVICE_NAME
".\n"
122 " -r Removes a Windows Service (see -n option).\n"
124 " -s Enable logging to syslog.\n"
125 " -u port Specify ICP port number (default: %d), disable with 0.\n"
126 " -v Print version.\n"
127 " -z Create swap directories\n"
128 " -C Do not catch fatal signals.\n"
129 " -D Disable initial DNS tests.\n"
130 " -F Don't serve any requests until store is rebuilt.\n"
131 " -N No daemon mode.\n"
132 #if USE_WIN32_SERVICE
134 " Set Windows Service Command line options in Registry.\n"
136 " -R Do not set REUSEADDR on port.\n"
137 " -S Double-check swap during rebuild.\n"
138 " -V Virtual host httpd-accelerator.\n"
139 " -X Force full debugging.\n"
140 " -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.\n",
141 appname
, CACHE_HTTP_PORT
, DefaultConfigFile
, CACHE_ICP_PORT
);
146 mainParseOptions(int argc
, char *argv
[])
151 #if USE_WIN32_SERVICE
153 while ((c
= getopt(argc
, argv
, "CDFNO:RSVYXa:d:f:hik:m::n:rsu:vz?")) != -1)
156 while ((c
= getopt(argc
, argv
, "CDFNRSVYXa:d:f:hk:m::su:vz?")) != -1)
165 opt_catch_signals
= 0;
173 opt_foreground_rebuild
= 1;
179 #if USE_WIN32_SERVICE
182 opt_command_line
= 1;
183 WIN32_Command_Line
= xstrdup(optarg
);
192 opt_store_doublecheck
= 1;
197 if (Config
.Sockaddr
.http
)
198 Config
.Sockaddr
.http
->vhost
= 1;
202 else if (Config
.Sockaddr
.https
)
203 Config
.Sockaddr
.https
->http
.vhost
= 1;
208 fatal("No http_port specified\n");
213 /* force full debugging */
214 sigusr2_handle(SIGUSR2
);
219 opt_reload_hit_only
= 1;
223 #if USE_WIN32_SERVICE
226 opt_install_service
= TRUE
;
233 add_http_port(optarg
);
238 opt_debug_stderr
= atoi(optarg
);
245 ConfigFile
= xstrdup(optarg
);
255 if ((int) strlen(optarg
) < 1)
258 if (!strncmp(optarg
, "reconfigure", strlen(optarg
)))
259 opt_send_signal
= SIGHUP
;
260 else if (!strncmp(optarg
, "rotate", strlen(optarg
)))
261 #ifdef _SQUID_LINUX_THREADS_
263 opt_send_signal
= SIGQUIT
;
267 opt_send_signal
= SIGUSR1
;
271 else if (!strncmp(optarg
, "debug", strlen(optarg
)))
272 #ifdef _SQUID_LINUX_THREADS_
274 opt_send_signal
= SIGTRAP
;
278 opt_send_signal
= SIGUSR2
;
282 else if (!strncmp(optarg
, "shutdown", strlen(optarg
)))
283 opt_send_signal
= SIGTERM
;
284 else if (!strncmp(optarg
, "interrupt", strlen(optarg
)))
285 opt_send_signal
= SIGINT
;
286 else if (!strncmp(optarg
, "kill", strlen(optarg
)))
287 opt_send_signal
= SIGKILL
;
288 else if (!strncmp(optarg
, "check", strlen(optarg
)))
289 opt_send_signal
= 0; /* SIGNULL */
290 else if (!strncmp(optarg
, "parse", strlen(optarg
)))
291 opt_parse_cfg_only
= 1; /* parse cfg file only */
300 malloc_debug_level
= atoi(optarg
);
305 fatal("Need to add -DMALLOC_DBG when compiling to use -mX option");
311 xmalloc_trace
= !xmalloc_trace
;
314 fatal("Need to configure --enable-xmalloc-debug-trace to use -m option");
319 #if USE_WIN32_SERVICE
322 xfree(WIN32_Service_name
);
324 WIN32_Service_name
= xstrdup(optarg
);
326 opt_signal_service
= TRUE
;
331 opt_remove_service
= TRUE
;
340 opt_syslog_enable
= 1;
346 fatal("Logging to syslog not available on this platform");
352 icpPortNumOverride
= atoi(optarg
);
354 if (icpPortNumOverride
< 0)
355 icpPortNumOverride
= 0;
360 printf("Squid Cache: Version %s\nconfigure options: %s\n", version_string
, SQUID_CONFIGURE_OPTIONS
);
362 #if USE_WIN32_SERVICE
364 printf("Compiled as Windows System Service.\n");
373 opt_create_swap_dirs
= 1;
393 #ifndef _SQUID_MSWIN_
396 signal(sig
, rotate_logs
);
406 #ifndef _SQUID_MSWIN_
409 signal(sig
, reconfigure
);
417 do_shutdown
= sig
== SIGINT
? -1 : 1;
418 #ifndef _SQUID_MSWIN_
419 #ifdef KILL_PARENT_OPT
422 debug(1, 1) ("Killing RunCache, pid %d\n", getppid());
424 if (kill(getppid(), sig
) < 0)
425 debug(1, 1) ("kill %d: %s\n", getppid(), xstrerror());
429 #if SA_RESETHAND == 0
430 signal(SIGTERM
, SIG_DFL
);
432 signal(SIGINT
, SIG_DFL
);
439 serverConnectionsOpen(void)
441 clientOpenListenSockets();
442 icpConnectionsOpen();
449 snmpConnectionOpen();
453 wccpConnectionOpen();
469 serverConnectionsClose(void)
471 assert(shutting_down
|| reconfiguring
);
472 clientHttpConnectionsClose();
473 icpConnectionShutdown();
476 htcpSocketShutdown();
482 snmpConnectionShutdown();
486 wccpConnectionShutdown();
493 mainReconfigure(void)
495 debug(1, 1) ("Restarting Squid Cache (version %s)...\n", version_string
);
497 /* Already called serverConnectionsClose and ipcacheShutdownServers() */
498 serverConnectionsClose();
499 icpConnectionClose();
506 snmpConnectionClose();
510 wccpConnectionClose();
521 authenticateShutdown();
522 externalAclShutdown();
523 storeDirCloseSwapLogs();
529 enter_suid(); /* root to read config file */
530 parseConfigFile(ConfigFile
);
532 _db_init(Config
.Log
.log
, Config
.debugOptions
);
533 ipcache_restart(); /* clear stuck entries */
534 authenticateUserCacheRestart(); /* clear stuck ACL entries */
535 fqdncache_restart(); /* sigh, fqdncache too */
537 errorInitialize(); /* reload error pages */
551 authenticateInit(&Config
.authConfiguration
);
558 serverConnectionsOpen();
560 storeDirOpenSwapLogs();
561 mimeInit(Config
.mimeTablePathname
);
562 writePidFile(); /* write PID file */
563 debug(1, 1) ("Ready to serve requests.\n");
577 authenticateShutdown();
578 externalAclShutdown();
579 _db_rotate_log(); /* cache.log */
580 storeDirWriteCleanLogs(1);
581 storeLogRotate(); /* store.log */
582 accessLogRotate(); /* access.log */
583 useragentRotateLog(); /* useragent.log */
584 refererRotateLog(); /* referer.log */
597 authenticateInit(&Config
.authConfiguration
);
602 setEffectiveUser(void)
604 leave_suid(); /* Run as non privilegied user */
610 if (geteuid() == 0) {
611 debug(0, 0) ("Squid is not safe to run as root! If you must\n");
612 debug(0, 0) ("start Squid as root, then you must configure\n");
613 debug(0, 0) ("it to run as a non-priveledged user with the\n");
614 debug(0, 0) ("'cache_effective_user' option in the config file.\n");
615 fatal("Don't run Squid as root, set 'cache_effective_user'!");
622 char pathbuf
[MAXPATHLEN
];
624 if (Config
.coredump_dir
) {
625 if (0 == strcmp("none", Config
.coredump_dir
)) {
627 } else if (chdir(Config
.coredump_dir
) == 0) {
628 debug(0, 1) ("Set Current Directory to %s\n", Config
.coredump_dir
);
631 debug(50, 0) ("chdir: %s: %s\n", Config
.coredump_dir
, xstrerror());
635 /* If we don't have coredump_dir or couldn't cd there, report current dir */
636 if (getcwd(pathbuf
, MAXPATHLEN
)) {
637 debug(0, 1) ("Current Directory is %s\n", pathbuf
);
639 debug(50, 0) ("WARNING: Can't find current directory, getcwd: %s\n", xstrerror());
644 #include "DelayPools.h"
650 /* chroot if configured to run inside chroot */
652 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
653 fatal("failed to chroot");
656 if (opt_catch_signals
) {
657 squid_signal(SIGSEGV
, death
, SA_NODEFER
| SA_RESETHAND
);
658 squid_signal(SIGBUS
, death
, SA_NODEFER
| SA_RESETHAND
);
661 squid_signal(SIGPIPE
, SIG_IGN
, SA_RESTART
);
662 squid_signal(SIGCHLD
, sig_child
, SA_NODEFER
| SA_RESTART
);
666 if (icpPortNumOverride
!= 1)
667 Config
.Port
.icp
= (u_short
) icpPortNumOverride
;
669 _db_init(Config
.Log
.log
, Config
.debugOptions
);
671 fd_open(fileno(debug_log
), FD_LOG
, Config
.Log
.log
);
675 log_trace_init("/tmp/squid.alloc");
679 debug(1, 0) ("Starting Squid Cache version %s for %s...\n",
685 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
686 debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name
, WIN32_OS_string
);
687 debug(1, 0) ("Service command line is: %s\n", WIN32_Service_Command_Line
);
689 debug(1, 0) ("Running on %s\n",WIN32_OS_string
);
693 debug(1, 1) ("Process ID %d\n", (int) getpid());
695 debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD
);
699 debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
701 if (WIN32_Socks_initialized
)
702 debug(1, 1)("Windows sockets initialized\n");
706 if (!configured_once
)
707 disk_init(); /* disk_init must go before ipcache_init() */
727 authenticateInit(&Config
.authConfiguration
);
735 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
737 httpReplyInitModule(); /* must go before accepting replies */
755 malloc_debug(0, malloc_debug_level
);
759 if (!configured_once
) {
769 /* after this point we want to see the mallinfo() output */
771 mimeInit(Config
.mimeTablePathname
);
787 serverConnectionsOpen();
791 if (Config
.chroot_dir
)
794 if (!configured_once
)
795 writePidFile(); /* write PID file */
797 #ifdef _SQUID_LINUX_THREADS_
799 squid_signal(SIGQUIT
, rotate_logs
, SA_RESTART
);
801 squid_signal(SIGTRAP
, sigusr2_handle
, SA_RESTART
);
805 squid_signal(SIGUSR1
, rotate_logs
, SA_RESTART
);
807 squid_signal(SIGUSR2
, sigusr2_handle
, SA_RESTART
);
811 squid_signal(SIGHUP
, reconfigure
, SA_RESTART
);
813 squid_signal(SIGTERM
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
815 squid_signal(SIGINT
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
819 debug(1, 1) ("Ready to serve requests.\n");
821 if (!configured_once
) {
822 eventAdd("storeMaintain", storeMaintainSwapSpace
, NULL
, 1.0, 1);
824 if (Config
.onoff
.announce
)
825 eventAdd("start_announce", start_announce
, NULL
, 3600.0, 1);
827 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
829 eventAdd("fqdncache_purgelru", fqdncache_purgelru
, NULL
, 15.0, 1);
833 eventAdd("cpuProfiling", xprof_event
, NULL
, 1.0, 1);
837 eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools
, NULL
, 15.0, 1);
843 #if USE_WIN32_SERVICE
844 /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
845 extern "C" void WINAPI
846 SquidMain(int argc
, char **argv
)
849 main(int argc
, char **argv
)
853 int n
; /* # of GC'd objects */
858 sbrk_start
= sbrk(0);
863 if (FD_SETSIZE
< Squid_MaxFD
)
864 Squid_MaxFD
= FD_SETSIZE
;
867 #ifdef USE_WIN32_SERVICE
869 if (WIN32_Subsystem_Init(&argc
, &argv
))
877 if ((WIN32_init_err
= WIN32_Subsystem_Init()))
878 return WIN32_init_err
;
884 /* call mallopt() before anything else */
887 /* Round up all sizes to a multiple of this */
888 mallopt(M_GRAIN
, 16);
892 /* biggest size that is considered a small block */
893 mallopt(M_MXFAST
, 256);
897 /* allocate this many small blocks at once */
898 mallopt(M_NLBLKS
, 32);
901 #endif /* HAVE_MALLOPT */
904 * The plan here is to set the umask to 007 (deny others for
905 * read,write,execute), but only if the umask is not already
906 * set. Unfortunately, there is no way to get the current
907 * umask value without setting it.
909 oldmask
= umask(S_IRWXO
);
914 memset(&local_addr
, '\0', sizeof(struct in_addr
));
916 safe_inet_addr(localhost
, &local_addr
);
918 memset(&any_addr
, '\0', sizeof(struct in_addr
));
920 safe_inet_addr("0.0.0.0", &any_addr
);
922 memset(&no_addr
, '\0', sizeof(struct in_addr
));
924 safe_inet_addr("255.255.255.255", &no_addr
);
926 squid_srandom(time(NULL
));
930 squid_start
= current_time
;
932 failure_notify
= fatal_dump
;
934 #if USE_WIN32_SERVICE
936 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
940 mainParseOptions(argc
, argv
);
942 #if USE_WIN32_SERVICE
944 if (opt_install_service
) {
945 WIN32_InstallService();
949 if (opt_remove_service
) {
950 WIN32_RemoveService();
954 if (opt_command_line
) {
955 WIN32_SetServiceCommandLine();
961 /* parse configuration file
962 * note: in "normal" case this used to be called from mainInitialize() */
967 ConfigFile
= xstrdup(DefaultConfigFile
);
969 assert(!configured_once
);
981 eventInit(); /* eventInit() is required for config parsing */
983 storeFsInit(); /* required for config parsing */
985 authenticateSchemeInit(); /* required for config parsign */
987 parse_err
= parseConfigFile(ConfigFile
);
989 if (opt_parse_cfg_only
)
990 #if USE_WIN32_SERVICE
1001 if (-1 == opt_send_signal
)
1002 if (checkRunningPid())
1019 /* send signal to running copy and exit */
1020 if (opt_send_signal
!= -1) {
1021 /* chroot if configured to run inside chroot */
1023 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1024 fatal("failed to chroot");
1031 if (opt_create_swap_dirs
) {
1032 /* chroot if configured to run inside chroot */
1034 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1035 fatal("failed to chroot");
1039 debug(0, 0) ("Creating Swap Directories\n");
1040 storeCreateSwapDirectories();
1041 #if USE_WIN32_SERVICE
1056 if (opt_catch_signals
)
1057 for (n
= Squid_MaxFD
; n
> 2; n
--)
1060 /* init comm module */
1065 if (opt_no_daemon
) {
1066 /* we have to init fdstat here. */
1067 fd_open(0, FD_LOG
, "stdin");
1068 fd_open(1, FD_LOG
, "stdout");
1069 fd_open(2, FD_LOG
, "stderr");
1072 #if USE_WIN32_SERVICE
1074 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1080 #if USE_WIN32_SERVICE
1082 WIN32_svcstatusupdate(SERVICE_RUNNING
, 0);
1089 if (do_reconfigure
) {
1092 } else if (do_rotate
) {
1095 } else if (do_shutdown
) {
1096 time_t wait
= do_shutdown
> 0 ? (int) Config
.shutdownLifetime
: 0;
1097 debug(1, 1) ("Preparing for shutdown after %d requests\n",
1098 statCounter
.client_http
.requests
);
1099 debug(1, 1) ("Waiting %d seconds for active connections to finish\n",
1103 #if USE_WIN32_SERVICE
1105 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, (wait
+ 1) * 1000);
1108 serverConnectionsClose();
1109 eventAdd("SquidShutdown", SquidShutdown
, NULL
, (double) (wait
+ 1), 1);
1113 int loop_delay
= eventNextTime();
1118 /* Attempt any pending storedir IO */
1121 comm_calliocallback();
1123 /* and again to deal with indirectly queued events
1124 * resulting from the first call. These are usually
1125 * callbacks and should be dealt with immediately.
1127 if (comm_iocallbackpending())
1128 comm_calliocallback();
1130 switch (comm_select(loop_delay
)) {
1133 errcount
= 0; /* reset if successful */
1138 debug(1, 0) ("Select loop Error. Retry %d\n", errcount
);
1141 fatal_dump("Select Loop failed!");
1149 SquidShutdown(NULL
);
1154 fatal_dump("MAIN: Internal error -- this should never happen.");
1161 #if USE_WIN32_SERVICE
1176 pid
= readPidFile();
1179 #if USE_WIN32_SERVICE
1181 if (opt_signal_service
) {
1182 WIN32_sendSignal(opt_send_signal
);
1185 #ifdef _SQUID_MSWIN_
1187 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1188 fprintf(stderr
, "signal to Squid Service:\n");
1189 fprintf(stderr
, "missing -n command line switch.\n");
1198 if (kill(pid
, opt_send_signal
) &&
1199 /* ignore permissions if just running check */
1200 !(opt_send_signal
== 0 && errno
== EPERM
)) {
1201 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1202 fprintf(stderr
, "signal %d to process %d: %s\n",
1203 opt_send_signal
, (int) pid
, xstrerror());
1207 fprintf(stderr
, "%s: ERROR: No running copy\n", appname
);
1211 /* signal successfully sent */
1215 #ifndef _SQUID_MSWIN_
1217 * This function is run when Squid is in daemon mode, just
1218 * before the parent forks and starts up the child process.
1219 * It can be used for admin-specific tasks, such as notifying
1220 * someone that Squid is (re)started.
1223 mainStartScript(const char *prog
)
1225 char script
[SQUID_MAXPATHLEN
];
1230 xstrncpy(script
, prog
, MAXPATHLEN
);
1232 if ((t
= strrchr(script
, '/'))) {
1234 sl
= strlen(script
);
1237 xstrncpy(&script
[sl
], squid_start_script
, MAXPATHLEN
- sl
);
1239 if ((cpid
= fork()) == 0) {
1241 execl(script
, squid_start_script
, 0);
1247 rpid
= wait3(&status
, 0, NULL
);
1251 rpid
= waitpid(-1, &status
, 0);
1254 } while (rpid
!= cpid
);
1258 #endif /* _SQUID_MSWIN_ */
1261 checkRunningPid(void)
1265 pid
= readPidFile();
1270 if (kill(pid
, 0) < 0)
1273 debug(0, 0) ("Squid is already running! Process ID %d\n", pid
);
1279 watch_child(char *argv
[])
1281 #ifndef _SQUID_MSWIN_
1298 if (*(argv
[0]) == '(')
1301 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1303 if ((pid
= fork()) < 0)
1304 syslog(LOG_ALERT
, "fork failed: %s", xstrerror());
1309 syslog(LOG_ALERT
, "setsid failed: %s", xstrerror());
1315 if ((i
= open("/dev/tty", O_RDWR
| O_TEXT
)) >= 0) {
1316 ioctl(i
, TIOCNOTTY
, NULL
);
1324 * RBCOLLINS - if cygwin stackdumps when squid is run without
1325 * -N, check the cygwin1.dll version, it needs to be AT LEAST
1326 * 1.1.3. execvp had a bit overflow error in a loop..
1328 /* Connect stdio to /dev/null in daemon mode */
1329 nullfd
= open("/dev/null", O_RDWR
| O_TEXT
);
1332 fatalf("/dev/null: %s\n", xstrerror());
1336 if (opt_debug_stderr
< 0) {
1341 /* Close all else */
1342 for (i
= 3; i
< Squid_MaxFD
; i
++)
1346 mainStartScript(argv
[0]);
1348 if ((pid
= fork()) == 0) {
1350 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1351 prog
= xstrdup(argv
[0]);
1352 argv
[0] = xstrdup("(squid)");
1354 syslog(LOG_ALERT
, "execvp failed: %s", xstrerror());
1358 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1360 syslog(LOG_NOTICE
, "Squid Parent: child process %d started", pid
);
1364 squid_signal(SIGINT
, SIG_IGN
, SA_RESTART
);
1368 pid
= wait3(&status
, 0, NULL
);
1372 pid
= waitpid(-1, &status
, 0);
1378 if (WIFEXITED(status
)) {
1380 "Squid Parent: child process %d exited with status %d",
1381 pid
, WEXITSTATUS(status
));
1382 } else if (WIFSIGNALED(status
)) {
1384 "Squid Parent: child process %d exited due to signal %d",
1385 pid
, WTERMSIG(status
));
1387 syslog(LOG_NOTICE
, "Squid Parent: child process %d exited", pid
);
1390 if (stop
- start
< 10)
1395 if (failcount
== 5) {
1396 syslog(LOG_ALERT
, "Exiting due to repeated, frequent failures");
1400 if (WIFEXITED(status
))
1401 if (WEXITSTATUS(status
) == 0)
1404 if (WIFSIGNALED(status
)) {
1405 switch (WTERMSIG(status
)) {
1416 squid_signal(SIGINT
, SIG_DFL
, SA_RESTART
);
1421 #endif /* _SQUID_MSWIN_ */
1425 SquidShutdown(void *unused
)
1427 #if USE_WIN32_SERVICE
1428 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1431 debug(1, 1) ("Shutting down...\n");
1441 externalAclShutdown();
1442 icpConnectionClose();
1449 snmpConnectionClose();
1453 wccpConnectionClose();
1456 releaseServerSockets();
1457 commCloseAllSockets();
1460 DelayPools::FreePools();
1463 authenticateShutdown();
1468 #if USE_WIN32_SERVICE
1470 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1473 storeDirSync(); /* Flush pending object writes/unlinks */
1474 storeDirWriteCleanLogs(0);
1477 storeDirSync(); /* Flush log writes */
1480 useragentLogClose();
1487 storeDirSync(); /* Flush log close */
1489 #if PURIFY || XMALLOC_TRACE
1493 /*stmemFreeMemory(); */
1495 ipcacheFreeMemory();
1496 fqdncacheFreeMemory();
1498 clientdbFreeMemory();
1499 httpHeaderCleanModule();
1507 if (opt_no_daemon
) {
1522 xmalloc_find_leaks();
1524 debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total
);
1533 if (Config
.pidFilename
&& strcmp(Config
.pidFilename
, "none") != 0) {
1535 safeunlink(Config
.pidFilename
, 0);
1539 debug(1, 1) ("Squid Cache (Version %s): Exiting normally.\n",