3 * $Id: main.cc,v 1.382 2003/07/06 15:29:35 hno 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"
47 #include "squid_windows.h"
50 static int opt_install_service
= FALSE
;
51 static int opt_remove_service
= FALSE
;
52 static int opt_signal_service
= FALSE
;
53 static int opt_command_line
= FALSE
;
54 extern void WIN32_svcstatusupdate(DWORD
, DWORD
);
55 void WINAPI
WIN32_svcHandler(DWORD
);
59 /* for error reporting from xmalloc and friends */
60 SQUIDCEXTERN
void (*failure_notify
) (const char *);
62 static int opt_send_signal
= -1;
63 static int opt_parse_cfg_only
= 0;
64 static int icpPortNumOverride
= 1; /* Want to detect "-u 0" */
65 static int configured_once
= 0;
67 static int malloc_debug_level
= 0;
69 static volatile int do_reconfigure
= 0;
70 static volatile int do_rotate
= 0;
71 static volatile int do_shutdown
= 0;
73 static void mainRotate(void);
74 static void mainReconfigure(void);
75 static void mainInitialize(void);
76 static void usage(void);
77 static void mainParseOptions(int, char **);
78 static void sendSignal(void);
79 static void serverConnectionsOpen(void);
80 static void watch_child(char **);
81 static void setEffectiveUser(void);
83 extern void log_trace_done();
84 extern void log_trace_init(char *);
86 static EVH SquidShutdown
;
87 static void mainSetCwd(void);
88 static int checkRunningPid(void);
91 static const char *squid_start_script
= "squid_start";
95 #include "test_access.c"
102 #if USE_WIN32_SERVICE
103 "Usage: %s [-dhirsvzCDFNRVYX] [-f config-file] [-[au] port] [-k signal] [-n name] [-O CommandLine]\n"
105 "Usage: %s [-dhsvzCDFNRVYX] [-f config-file] [-[au] port] [-k signal]\n"
107 " -a port Specify HTTP port number (default: %d).\n"
108 " -d level Write debugging to stderr also.\n"
109 " -f file Use given config-file instead of\n"
111 " -h Print help message.\n"
112 #if USE_WIN32_SERVICE
113 " -i Installs as a Windows Service (see -n option).\n"
115 " -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse\n"
116 " Parse configuration file, then send signal to \n"
117 " running copy (except -k parse) and exit.\n"
118 #if USE_WIN32_SERVICE
119 " -n name Specify Windows Service name to use for service operations\n"
120 " default is: " _WIN_SQUID_DEFAULT_SERVICE_NAME
".\n"
121 " -r Removes a Windows Service (see -n option).\n"
123 " -s Enable logging to syslog.\n"
124 " -u port Specify ICP port number (default: %d), disable with 0.\n"
125 " -v Print version.\n"
126 " -z Create swap directories\n"
127 " -C Do not catch fatal signals.\n"
128 " -D Disable initial DNS tests.\n"
129 " -F Don't serve any requests until store is rebuilt.\n"
130 " -N No daemon mode.\n"
131 #if USE_WIN32_SERVICE
133 " Set Windows Service Command line options in Registry.\n"
135 " -R Do not set REUSEADDR on port.\n"
136 " -S Double-check swap during rebuild.\n"
137 " -V Virtual host httpd-accelerator.\n"
138 " -X Force full debugging.\n"
139 " -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.\n",
140 appname
, CACHE_HTTP_PORT
, DefaultConfigFile
, CACHE_ICP_PORT
);
145 mainParseOptions(int argc
, char *argv
[])
150 #if USE_WIN32_SERVICE
152 while ((c
= getopt(argc
, argv
, "CDFNO:RSVYXa:d:f:hik:m::n:rsu:vz?")) != -1)
155 while ((c
= getopt(argc
, argv
, "CDFNRSVYXa:d:f:hk:m::su:vz?")) != -1)
164 opt_catch_signals
= 0;
172 opt_foreground_rebuild
= 1;
178 #if USE_WIN32_SERVICE
181 opt_command_line
= 1;
182 WIN32_Command_Line
= xstrdup(optarg
);
191 opt_store_doublecheck
= 1;
196 if (Config
.Sockaddr
.http
)
197 Config
.Sockaddr
.http
->vhost
= 1;
201 else if (Config
.Sockaddr
.https
)
202 Config
.Sockaddr
.https
->http
.vhost
= 1;
207 fatal("No http_port specified\n");
212 /* force full debugging */
213 sigusr2_handle(SIGUSR2
);
218 opt_reload_hit_only
= 1;
222 #if USE_WIN32_SERVICE
225 opt_install_service
= TRUE
;
232 add_http_port(optarg
);
237 opt_debug_stderr
= atoi(optarg
);
244 ConfigFile
= xstrdup(optarg
);
254 if ((int) strlen(optarg
) < 1)
257 if (!strncmp(optarg
, "reconfigure", strlen(optarg
)))
258 opt_send_signal
= SIGHUP
;
259 else if (!strncmp(optarg
, "rotate", strlen(optarg
)))
260 #ifdef _SQUID_LINUX_THREADS_
262 opt_send_signal
= SIGQUIT
;
266 opt_send_signal
= SIGUSR1
;
270 else if (!strncmp(optarg
, "debug", strlen(optarg
)))
271 #ifdef _SQUID_LINUX_THREADS_
273 opt_send_signal
= SIGTRAP
;
277 opt_send_signal
= SIGUSR2
;
281 else if (!strncmp(optarg
, "shutdown", strlen(optarg
)))
282 opt_send_signal
= SIGTERM
;
283 else if (!strncmp(optarg
, "interrupt", strlen(optarg
)))
284 opt_send_signal
= SIGINT
;
285 else if (!strncmp(optarg
, "kill", strlen(optarg
)))
286 opt_send_signal
= SIGKILL
;
287 else if (!strncmp(optarg
, "check", strlen(optarg
)))
288 opt_send_signal
= 0; /* SIGNULL */
289 else if (!strncmp(optarg
, "parse", strlen(optarg
)))
290 opt_parse_cfg_only
= 1; /* parse cfg file only */
299 malloc_debug_level
= atoi(optarg
);
304 fatal("Need to add -DMALLOC_DBG when compiling to use -mX option");
310 xmalloc_trace
= !xmalloc_trace
;
313 fatal("Need to configure --enable-xmalloc-debug-trace to use -m option");
318 #if USE_WIN32_SERVICE
321 xfree(WIN32_Service_name
);
323 WIN32_Service_name
= xstrdup(optarg
);
325 opt_signal_service
= TRUE
;
330 opt_remove_service
= TRUE
;
339 opt_syslog_enable
= 1;
345 fatal("Logging to syslog not available on this platform");
351 icpPortNumOverride
= atoi(optarg
);
353 if (icpPortNumOverride
< 0)
354 icpPortNumOverride
= 0;
359 printf("Squid Cache: Version %s\nconfigure options: %s\n", version_string
, SQUID_CONFIGURE_OPTIONS
);
361 #if USE_WIN32_SERVICE
363 printf("Compiled as Windows System Service.\n");
372 opt_create_swap_dirs
= 1;
392 #ifndef _SQUID_MSWIN_
395 signal(sig
, rotate_logs
);
405 #ifndef _SQUID_MSWIN_
408 signal(sig
, reconfigure
);
416 do_shutdown
= sig
== SIGINT
? -1 : 1;
417 #ifndef _SQUID_MSWIN_
418 #ifdef KILL_PARENT_OPT
421 debug(1, 1) ("Killing RunCache, pid %d\n", getppid());
423 if (kill(getppid(), sig
) < 0)
424 debug(1, 1) ("kill %d: %s\n", getppid(), xstrerror());
428 #if SA_RESETHAND == 0
429 signal(SIGTERM
, SIG_DFL
);
431 signal(SIGINT
, SIG_DFL
);
438 serverConnectionsOpen(void)
440 clientOpenListenSockets();
441 icpConnectionsOpen();
448 snmpConnectionOpen();
452 wccpConnectionOpen();
468 serverConnectionsClose(void)
470 assert(shutting_down
|| reconfiguring
);
471 clientHttpConnectionsClose();
472 icpConnectionShutdown();
475 htcpSocketShutdown();
481 snmpConnectionShutdown();
485 wccpConnectionShutdown();
492 mainReconfigure(void)
494 debug(1, 1) ("Restarting Squid Cache (version %s)...\n", version_string
);
496 /* Already called serverConnectionsClose and ipcacheShutdownServers() */
497 serverConnectionsClose();
498 icpConnectionClose();
505 snmpConnectionClose();
509 wccpConnectionClose();
520 authenticateShutdown();
521 externalAclShutdown();
522 storeDirCloseSwapLogs();
528 enter_suid(); /* root to read config file */
529 parseConfigFile(ConfigFile
);
531 _db_init(Config
.Log
.log
, Config
.debugOptions
);
532 ipcache_restart(); /* clear stuck entries */
533 authenticateUserCacheRestart(); /* clear stuck ACL entries */
534 fqdncache_restart(); /* sigh, fqdncache too */
536 errorInitialize(); /* reload error pages */
550 authenticateInit(&Config
.authConfiguration
);
557 serverConnectionsOpen();
559 storeDirOpenSwapLogs();
560 mimeInit(Config
.mimeTablePathname
);
561 writePidFile(); /* write PID file */
562 debug(1, 1) ("Ready to serve requests.\n");
576 authenticateShutdown();
577 externalAclShutdown();
578 _db_rotate_log(); /* cache.log */
579 storeDirWriteCleanLogs(1);
580 storeLogRotate(); /* store.log */
581 accessLogRotate(); /* access.log */
582 useragentRotateLog(); /* useragent.log */
583 refererRotateLog(); /* referer.log */
596 authenticateInit(&Config
.authConfiguration
);
601 setEffectiveUser(void)
603 leave_suid(); /* Run as non privilegied user */
609 if (geteuid() == 0) {
610 debug(0, 0) ("Squid is not safe to run as root! If you must\n");
611 debug(0, 0) ("start Squid as root, then you must configure\n");
612 debug(0, 0) ("it to run as a non-priveledged user with the\n");
613 debug(0, 0) ("'cache_effective_user' option in the config file.\n");
614 fatal("Don't run Squid as root, set 'cache_effective_user'!");
621 char pathbuf
[MAXPATHLEN
];
623 if (Config
.coredump_dir
) {
624 if (0 == strcmp("none", Config
.coredump_dir
)) {
626 } else if (chdir(Config
.coredump_dir
) == 0) {
627 debug(0, 1) ("Set Current Directory to %s\n", Config
.coredump_dir
);
630 debug(50, 0) ("chdir: %s: %s\n", Config
.coredump_dir
, xstrerror());
634 /* If we don't have coredump_dir or couldn't cd there, report current dir */
635 if (getcwd(pathbuf
, MAXPATHLEN
)) {
636 debug(0, 1) ("Current Directory is %s\n", pathbuf
);
638 debug(50, 0) ("WARNING: Can't find current directory, getcwd: %s\n", xstrerror());
643 #include "DelayPools.h"
649 /* chroot if configured to run inside chroot */
651 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
652 fatal("failed to chroot");
655 if (opt_catch_signals
) {
656 squid_signal(SIGSEGV
, death
, SA_NODEFER
| SA_RESETHAND
);
657 squid_signal(SIGBUS
, death
, SA_NODEFER
| SA_RESETHAND
);
660 squid_signal(SIGPIPE
, SIG_IGN
, SA_RESTART
);
661 squid_signal(SIGCHLD
, sig_child
, SA_NODEFER
| SA_RESTART
);
665 if (icpPortNumOverride
!= 1)
666 Config
.Port
.icp
= (u_short
) icpPortNumOverride
;
668 _db_init(Config
.Log
.log
, Config
.debugOptions
);
670 fd_open(fileno(debug_log
), FD_LOG
, Config
.Log
.log
);
674 log_trace_init("/tmp/squid.alloc");
678 debug(1, 0) ("Starting Squid Cache version %s for %s...\n",
684 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
685 debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name
, WIN32_OS_string
);
686 debug(1, 0) ("Service command line is: %s\n", WIN32_Service_Command_Line
);
688 debug(1, 0) ("Running on %s\n",WIN32_OS_string
);
692 debug(1, 1) ("Process ID %d\n", (int) getpid());
694 debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD
);
698 debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
700 if (WIN32_Socks_initialized
)
701 debug(1, 1)("Windows sockets initialized\n");
705 if (!configured_once
)
706 disk_init(); /* disk_init must go before ipcache_init() */
726 authenticateInit(&Config
.authConfiguration
);
734 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
736 httpReplyInitModule(); /* must go before accepting replies */
754 malloc_debug(0, malloc_debug_level
);
758 if (!configured_once
) {
768 /* after this point we want to see the mallinfo() output */
770 mimeInit(Config
.mimeTablePathname
);
786 serverConnectionsOpen();
790 if (Config
.chroot_dir
)
793 if (!configured_once
)
794 writePidFile(); /* write PID file */
796 #ifdef _SQUID_LINUX_THREADS_
798 squid_signal(SIGQUIT
, rotate_logs
, SA_RESTART
);
800 squid_signal(SIGTRAP
, sigusr2_handle
, SA_RESTART
);
804 squid_signal(SIGUSR1
, rotate_logs
, SA_RESTART
);
806 squid_signal(SIGUSR2
, sigusr2_handle
, SA_RESTART
);
810 squid_signal(SIGHUP
, reconfigure
, SA_RESTART
);
812 squid_signal(SIGTERM
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
814 squid_signal(SIGINT
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
818 debug(1, 1) ("Ready to serve requests.\n");
820 if (!configured_once
) {
821 eventAdd("storeMaintain", storeMaintainSwapSpace
, NULL
, 1.0, 1);
823 if (Config
.onoff
.announce
)
824 eventAdd("start_announce", start_announce
, NULL
, 3600.0, 1);
826 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
828 eventAdd("fqdncache_purgelru", fqdncache_purgelru
, NULL
, 15.0, 1);
832 eventAdd("cpuProfiling", xprof_event
, NULL
, 1.0, 1);
836 eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools
, NULL
, 15.0, 1);
842 #if USE_WIN32_SERVICE
843 /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
844 extern "C" void WINAPI
845 SquidMain(int argc
, char **argv
)
848 main(int argc
, char **argv
)
852 int n
; /* # of GC'd objects */
857 sbrk_start
= sbrk(0);
862 if (FD_SETSIZE
< Squid_MaxFD
)
863 Squid_MaxFD
= FD_SETSIZE
;
866 #ifdef USE_WIN32_SERVICE
868 if (WIN32_Subsystem_Init(&argc
, &argv
))
876 if ((WIN32_init_err
= WIN32_Subsystem_Init()))
877 return WIN32_init_err
;
883 /* call mallopt() before anything else */
886 /* Round up all sizes to a multiple of this */
887 mallopt(M_GRAIN
, 16);
891 /* biggest size that is considered a small block */
892 mallopt(M_MXFAST
, 256);
896 /* allocate this many small blocks at once */
897 mallopt(M_NLBLKS
, 32);
900 #endif /* HAVE_MALLOPT */
903 * The plan here is to set the umask to 007 (deny others for
904 * read,write,execute), but only if the umask is not already
905 * set. Unfortunately, there is no way to get the current
906 * umask value without setting it.
908 oldmask
= umask(S_IRWXO
);
913 memset(&local_addr
, '\0', sizeof(struct in_addr
));
915 safe_inet_addr(localhost
, &local_addr
);
917 memset(&any_addr
, '\0', sizeof(struct in_addr
));
919 safe_inet_addr("0.0.0.0", &any_addr
);
921 memset(&no_addr
, '\0', sizeof(struct in_addr
));
923 safe_inet_addr("255.255.255.255", &no_addr
);
925 squid_srandom(time(NULL
));
929 squid_start
= current_time
;
931 failure_notify
= fatal_dump
;
933 #if USE_WIN32_SERVICE
935 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
939 mainParseOptions(argc
, argv
);
941 #if USE_WIN32_SERVICE
943 if (opt_install_service
) {
944 WIN32_InstallService();
948 if (opt_remove_service
) {
949 WIN32_RemoveService();
953 if (opt_command_line
) {
954 WIN32_SetServiceCommandLine();
960 /* parse configuration file
961 * note: in "normal" case this used to be called from mainInitialize() */
966 ConfigFile
= xstrdup(DefaultConfigFile
);
968 assert(!configured_once
);
980 eventInit(); /* eventInit() is required for config parsing */
982 storeFsInit(); /* required for config parsing */
984 authenticateSchemeInit(); /* required for config parsign */
986 parse_err
= parseConfigFile(ConfigFile
);
988 if (opt_parse_cfg_only
)
989 #if USE_WIN32_SERVICE
1000 if (-1 == opt_send_signal
)
1001 if (checkRunningPid())
1018 /* send signal to running copy and exit */
1019 if (opt_send_signal
!= -1) {
1020 /* chroot if configured to run inside chroot */
1022 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1023 fatal("failed to chroot");
1030 if (opt_create_swap_dirs
) {
1031 /* chroot if configured to run inside chroot */
1033 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1034 fatal("failed to chroot");
1038 debug(0, 0) ("Creating Swap Directories\n");
1039 storeCreateSwapDirectories();
1040 #if USE_WIN32_SERVICE
1055 if (opt_catch_signals
)
1056 for (n
= Squid_MaxFD
; n
> 2; n
--)
1059 /* init comm module */
1064 if (opt_no_daemon
) {
1065 /* we have to init fdstat here. */
1066 fd_open(0, FD_LOG
, "stdin");
1067 fd_open(1, FD_LOG
, "stdout");
1068 fd_open(2, FD_LOG
, "stderr");
1071 #if USE_WIN32_SERVICE
1073 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1079 #if USE_WIN32_SERVICE
1081 WIN32_svcstatusupdate(SERVICE_RUNNING
, 0);
1088 if (do_reconfigure
) {
1091 } else if (do_rotate
) {
1094 } else if (do_shutdown
) {
1095 time_t wait
= do_shutdown
> 0 ? (int) Config
.shutdownLifetime
: 0;
1096 debug(1, 1) ("Preparing for shutdown after %d requests\n",
1097 statCounter
.client_http
.requests
);
1098 debug(1, 1) ("Waiting %d seconds for active connections to finish\n",
1102 #if USE_WIN32_SERVICE
1104 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, (wait
+ 1) * 1000);
1107 serverConnectionsClose();
1108 eventAdd("SquidShutdown", SquidShutdown
, NULL
, (double) (wait
+ 1), 1);
1112 int loop_delay
= eventNextTime();
1117 /* Attempt any pending storedir IO */
1120 /* twice to deal with indirectly queued events
1121 * resulting from the first call. These are usually
1122 * callbacks and should be dealt with immediately.
1124 comm_calliocallback();
1126 if (comm_iocallbackpending())
1127 comm_calliocallback();
1129 switch (comm_select(loop_delay
)) {
1132 errcount
= 0; /* reset if successful */
1137 debug(1, 0) ("Select loop Error. Retry %d\n", errcount
);
1140 fatal_dump("Select Loop failed!");
1148 SquidShutdown(NULL
);
1153 fatal_dump("MAIN: Internal error -- this should never happen.");
1160 #if USE_WIN32_SERVICE
1175 pid
= readPidFile();
1178 #if USE_WIN32_SERVICE
1180 if (opt_signal_service
) {
1181 WIN32_sendSignal(opt_send_signal
);
1184 #ifdef _SQUID_MSWIN_
1186 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1187 fprintf(stderr
, "signal to Squid Service:\n");
1188 fprintf(stderr
, "missing -n command line switch.\n");
1197 if (kill(pid
, opt_send_signal
) &&
1198 /* ignore permissions if just running check */
1199 !(opt_send_signal
== 0 && errno
== EPERM
)) {
1200 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1201 fprintf(stderr
, "signal %d to process %d: %s\n",
1202 opt_send_signal
, (int) pid
, xstrerror());
1206 fprintf(stderr
, "%s: ERROR: No running copy\n", appname
);
1210 /* signal successfully sent */
1214 #ifndef _SQUID_MSWIN_
1216 * This function is run when Squid is in daemon mode, just
1217 * before the parent forks and starts up the child process.
1218 * It can be used for admin-specific tasks, such as notifying
1219 * someone that Squid is (re)started.
1222 mainStartScript(const char *prog
)
1224 char script
[SQUID_MAXPATHLEN
];
1229 xstrncpy(script
, prog
, MAXPATHLEN
);
1231 if ((t
= strrchr(script
, '/'))) {
1233 sl
= strlen(script
);
1236 xstrncpy(&script
[sl
], squid_start_script
, MAXPATHLEN
- sl
);
1238 if ((cpid
= fork()) == 0) {
1240 execl(script
, squid_start_script
, 0);
1246 rpid
= wait3(&status
, 0, NULL
);
1250 rpid
= waitpid(-1, &status
, 0);
1253 } while (rpid
!= cpid
);
1257 #endif /* _SQUID_MSWIN_ */
1260 checkRunningPid(void)
1264 pid
= readPidFile();
1269 if (kill(pid
, 0) < 0)
1272 debug(0, 0) ("Squid is already running! Process ID %d\n", pid
);
1278 watch_child(char *argv
[])
1280 #ifndef _SQUID_MSWIN_
1297 if (*(argv
[0]) == '(')
1300 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1302 if ((pid
= fork()) < 0)
1303 syslog(LOG_ALERT
, "fork failed: %s", xstrerror());
1308 syslog(LOG_ALERT
, "setsid failed: %s", xstrerror());
1314 if ((i
= open("/dev/tty", O_RDWR
| O_TEXT
)) >= 0) {
1315 ioctl(i
, TIOCNOTTY
, NULL
);
1323 * RBCOLLINS - if cygwin stackdumps when squid is run without
1324 * -N, check the cygwin1.dll version, it needs to be AT LEAST
1325 * 1.1.3. execvp had a bit overflow error in a loop..
1327 /* Connect stdio to /dev/null in daemon mode */
1328 nullfd
= open("/dev/null", O_RDWR
| O_TEXT
);
1331 fatalf("/dev/null: %s\n", xstrerror());
1335 if (opt_debug_stderr
< 0) {
1340 /* Close all else */
1341 for (i
= 3; i
< Squid_MaxFD
; i
++)
1345 mainStartScript(argv
[0]);
1347 if ((pid
= fork()) == 0) {
1349 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1350 prog
= xstrdup(argv
[0]);
1351 argv
[0] = xstrdup("(squid)");
1353 syslog(LOG_ALERT
, "execvp failed: %s", xstrerror());
1357 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1359 syslog(LOG_NOTICE
, "Squid Parent: child process %d started", pid
);
1363 squid_signal(SIGINT
, SIG_IGN
, SA_RESTART
);
1367 pid
= wait3(&status
, 0, NULL
);
1371 pid
= waitpid(-1, &status
, 0);
1377 if (WIFEXITED(status
)) {
1379 "Squid Parent: child process %d exited with status %d",
1380 pid
, WEXITSTATUS(status
));
1381 } else if (WIFSIGNALED(status
)) {
1383 "Squid Parent: child process %d exited due to signal %d",
1384 pid
, WTERMSIG(status
));
1386 syslog(LOG_NOTICE
, "Squid Parent: child process %d exited", pid
);
1389 if (stop
- start
< 10)
1394 if (failcount
== 5) {
1395 syslog(LOG_ALERT
, "Exiting due to repeated, frequent failures");
1399 if (WIFEXITED(status
))
1400 if (WEXITSTATUS(status
) == 0)
1403 if (WIFSIGNALED(status
)) {
1404 switch (WTERMSIG(status
)) {
1415 squid_signal(SIGINT
, SIG_DFL
, SA_RESTART
);
1420 #endif /* _SQUID_MSWIN_ */
1424 SquidShutdown(void *unused
)
1426 #if USE_WIN32_SERVICE
1427 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1430 debug(1, 1) ("Shutting down...\n");
1440 externalAclShutdown();
1441 icpConnectionClose();
1448 snmpConnectionClose();
1452 wccpConnectionClose();
1455 releaseServerSockets();
1456 commCloseAllSockets();
1459 DelayPools::FreePools();
1462 authenticateShutdown();
1467 #if USE_WIN32_SERVICE
1469 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1472 storeDirSync(); /* Flush pending object writes/unlinks */
1473 storeDirWriteCleanLogs(0);
1476 storeDirSync(); /* Flush log writes */
1479 useragentLogClose();
1486 storeDirSync(); /* Flush log close */
1488 #if PURIFY || XMALLOC_TRACE
1492 /*stmemFreeMemory(); */
1494 ipcacheFreeMemory();
1495 fqdncacheFreeMemory();
1497 clientdbFreeMemory();
1498 httpHeaderCleanModule();
1506 if (opt_no_daemon
) {
1521 xmalloc_find_leaks();
1523 debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total
);
1532 if (Config
.pidFilename
&& strcmp(Config
.pidFilename
, "none") != 0) {
1534 safeunlink(Config
.pidFilename
, 0);
1538 debug(1, 1) ("Squid Cache (Version %s): Exiting normally.\n",