3 * $Id: main.cc,v 1.408 2005/04/18 21:52:42 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 "AccessLogEntry.h"
38 #include "authenticate.h"
41 #include "HttpReply.h"
46 #include "StoreFileSystem.h"
47 #include "DiskIO/DiskIOModule.h"
53 #include "squid_windows.h"
56 static int opt_install_service
= FALSE
;
57 static int opt_remove_service
= FALSE
;
58 static int opt_signal_service
= FALSE
;
59 static int opt_command_line
= FALSE
;
60 extern void WIN32_svcstatusupdate(DWORD
, DWORD
);
61 void WINAPI
WIN32_svcHandler(DWORD
);
65 /* for error reporting from xmalloc and friends */
66 SQUIDCEXTERN
void (*failure_notify
) (const char *);
68 static int opt_send_signal
= -1;
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_create_swap_dirs
= 1;
403 #ifndef _SQUID_MSWIN_
406 signal(sig
, rotate_logs
);
416 #ifndef _SQUID_MSWIN_
419 signal(sig
, reconfigure
);
427 do_shutdown
= sig
== SIGINT
? -1 : 1;
428 #ifndef _SQUID_MSWIN_
429 #ifdef KILL_PARENT_OPT
432 debugs(1, 1, "Killing RunCache, pid " << getppid());
434 if (kill(getppid(), sig
) < 0)
435 debugs(1, 1, "kill " << getppid() << ": " << xstrerror());
439 #if SA_RESETHAND == 0
440 signal(SIGTERM
, SIG_DFL
);
442 signal(SIGINT
, SIG_DFL
);
449 serverConnectionsOpen(void)
451 clientOpenListenSockets();
452 icpConnectionsOpen();
459 snmpConnectionOpen();
463 wccpConnectionOpen();
479 serverConnectionsClose(void)
481 assert(shutting_down
|| reconfiguring
);
482 clientHttpConnectionsClose();
483 icpConnectionShutdown();
486 htcpSocketShutdown();
492 snmpConnectionShutdown();
496 wccpConnectionShutdown();
503 mainReconfigure(void)
505 debug(1, 1) ("Reconfiguring Squid Cache (version %s)...\n", version_string
);
507 /* Already called serverConnectionsClose and ipcacheShutdownServers() */
508 serverConnectionsClose();
509 icpConnectionClose();
516 snmpConnectionClose();
520 wccpConnectionClose();
531 authenticateShutdown();
532 externalAclShutdown();
533 storeDirCloseSwapLogs();
539 enter_suid(); /* root to read config file */
540 parseConfigFile(ConfigFile
);
542 _db_init(Config
.Log
.log
, Config
.debugOptions
);
543 ipcache_restart(); /* clear stuck entries */
544 authenticateUserCacheRestart(); /* clear stuck ACL entries */
545 fqdncache_restart(); /* sigh, fqdncache too */
547 errorInitialize(); /* reload error pages */
561 authenticateInit(&Config
.authConfiguration
);
568 serverConnectionsOpen();
570 if (theOutIcpConnection
>= 0)
573 storeDirOpenSwapLogs();
575 mimeInit(Config
.mimeTablePathname
);
577 writePidFile(); /* write PID file */
579 debug(1, 1) ("Ready to serve requests.\n");
594 authenticateShutdown();
595 externalAclShutdown();
596 _db_rotate_log(); /* cache.log */
597 storeDirWriteCleanLogs(1);
598 storeLogRotate(); /* store.log */
599 accessLogRotate(); /* access.log */
600 useragentRotateLog(); /* useragent.log */
601 refererRotateLog(); /* referer.log */
614 authenticateInit(&Config
.authConfiguration
);
619 setEffectiveUser(void)
621 leave_suid(); /* Run as non privilegied user */
627 if (geteuid() == 0) {
628 debug(0, 0) ("Squid is not safe to run as root! If you must\n");
629 debug(0, 0) ("start Squid as root, then you must configure\n");
630 debug(0, 0) ("it to run as a non-priveledged user with the\n");
631 debug(0, 0) ("'cache_effective_user' option in the config file.\n");
632 fatal("Don't run Squid as root, set 'cache_effective_user'!");
639 char pathbuf
[MAXPATHLEN
];
641 if (Config
.coredump_dir
) {
642 if (0 == strcmp("none", Config
.coredump_dir
)) {
644 } else if (chdir(Config
.coredump_dir
) == 0) {
645 debug(0, 1) ("Set Current Directory to %s\n", Config
.coredump_dir
);
648 debug(50, 0) ("chdir: %s: %s\n", Config
.coredump_dir
, xstrerror());
652 /* If we don't have coredump_dir or couldn't cd there, report current dir */
653 if (getcwd(pathbuf
, MAXPATHLEN
)) {
654 debug(0, 1) ("Current Directory is %s\n", pathbuf
);
656 debug(50, 0) ("WARNING: Can't find current directory, getcwd: %s\n", xstrerror());
661 #include "DelayPools.h"
667 /* chroot if configured to run inside chroot */
669 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
670 fatal("failed to chroot");
673 if (opt_catch_signals
) {
674 squid_signal(SIGSEGV
, death
, SA_NODEFER
| SA_RESETHAND
);
675 squid_signal(SIGBUS
, death
, SA_NODEFER
| SA_RESETHAND
);
678 squid_signal(SIGPIPE
, SIG_IGN
, SA_RESTART
);
679 squid_signal(SIGCHLD
, sig_child
, SA_NODEFER
| SA_RESTART
);
683 if (icpPortNumOverride
!= 1)
684 Config
.Port
.icp
= (u_short
) icpPortNumOverride
;
686 _db_init(Config
.Log
.log
, Config
.debugOptions
);
688 fd_open(fileno(debug_log
), FD_LOG
, Config
.Log
.log
);
692 log_trace_init("/tmp/squid.alloc");
696 debug(1, 0) ("Starting Squid Cache version %s for %s...\n",
702 if (WIN32_run_mode
== _WIN_SQUID_RUN_MODE_SERVICE
) {
703 debug(1, 0) ("Running as %s Windows System Service on %s\n", WIN32_Service_name
, WIN32_OS_string
);
704 debug(1, 0) ("Service command line is: %s\n", WIN32_Service_Command_Line
);
706 debug(1, 0) ("Running on %s\n",WIN32_OS_string
);
710 debugs(1, 1, "Process ID " << getpid());
712 debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD
);
716 debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
718 if (WIN32_Socks_initialized
)
719 debug(1, 1)("Windows sockets initialized\n");
723 if (!configured_once
)
724 disk_init(); /* disk_init must go before ipcache_init() */
744 authenticateInit(&Config
.authConfiguration
);
752 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
754 httpReplyInitModule(); /* must go before accepting replies */
772 malloc_debug(0, malloc_debug_level
);
776 if (!configured_once
) {
786 /* after this point we want to see the mallinfo() output */
788 mimeInit(Config
.mimeTablePathname
);
804 serverConnectionsOpen();
806 if (theOutIcpConnection
>= 0)
809 if (Config
.chroot_dir
)
812 if (!configured_once
)
813 writePidFile(); /* write PID file */
815 #ifdef _SQUID_LINUX_THREADS_
817 squid_signal(SIGQUIT
, rotate_logs
, SA_RESTART
);
819 squid_signal(SIGTRAP
, sigusr2_handle
, SA_RESTART
);
823 squid_signal(SIGUSR1
, rotate_logs
, SA_RESTART
);
825 squid_signal(SIGUSR2
, sigusr2_handle
, SA_RESTART
);
829 squid_signal(SIGHUP
, reconfigure
, SA_RESTART
);
831 squid_signal(SIGTERM
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
833 squid_signal(SIGINT
, shut_down
, SA_NODEFER
| SA_RESETHAND
| SA_RESTART
);
837 debug(1, 1) ("Ready to serve requests.\n");
839 if (!configured_once
) {
840 eventAdd("storeMaintain", Store::Maintain
, NULL
, 1.0, 1);
842 if (Config
.onoff
.announce
)
843 eventAdd("start_announce", start_announce
, NULL
, 3600.0, 1);
845 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
847 eventAdd("fqdncache_purgelru", fqdncache_purgelru
, NULL
, 15.0, 1);
851 eventAdd("cpuProfiling", xprof_event
, NULL
, 1.0, 1);
855 eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools
, NULL
, 15.0, 1);
857 eventAdd("commCheckHalfClosed", commCheckHalfClosed
, NULL
, 1.0, false);
863 #if USE_WIN32_SERVICE
864 /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */
865 extern "C" void WINAPI
866 SquidMain(int argc
, char **argv
)
869 main(int argc
, char **argv
)
877 sbrk_start
= sbrk(0);
880 Debug::parseOptions("ALL,1");
883 if (FD_SETSIZE
< Squid_MaxFD
)
884 Squid_MaxFD
= FD_SETSIZE
;
887 #ifdef USE_WIN32_SERVICE
889 if (WIN32_Subsystem_Init(&argc
, &argv
))
897 if ((WIN32_init_err
= WIN32_Subsystem_Init()))
898 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
) {
965 WIN32_InstallService();
969 if (opt_remove_service
) {
970 WIN32_RemoveService();
974 if (opt_command_line
) {
975 WIN32_SetServiceCommandLine();
981 /* parse configuration file
982 * note: in "normal" case this used to be called from mainInitialize() */
987 ConfigFile
= xstrdup(DefaultConfigFile
);
989 assert(!configured_once
);
1001 eventInit(); /* eventInit() is required for config parsing */
1003 storeFsInit(); /* required for config parsing */
1005 /* May not be needed for parsing, have not audited for such */
1006 DiskIOModule::SetupAllModules();
1008 /* Shouldn't be needed for config parsing, but have not audited for such */
1009 StoreFileSystem::SetupAllFs();
1011 /* we may want the parsing process to set this up in the future */
1012 Store::Root(new StoreController
);
1014 parse_err
= parseConfigFile(ConfigFile
);
1016 if (opt_parse_cfg_only
)
1017 #if USE_WIN32_SERVICE
1028 if (-1 == opt_send_signal
)
1029 if (checkRunningPid())
1046 /* send signal to running copy and exit */
1047 if (opt_send_signal
!= -1) {
1048 /* chroot if configured to run inside chroot */
1050 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1051 fatal("failed to chroot");
1058 if (opt_create_swap_dirs
) {
1059 /* chroot if configured to run inside chroot */
1061 if (Config
.chroot_dir
&& chroot(Config
.chroot_dir
)) {
1062 fatal("failed to chroot");
1066 debug(0, 0) ("Creating Swap Directories\n");
1067 Store::Root().create();
1068 #if USE_WIN32_SERVICE
1083 /* init comm module */
1088 if (opt_no_daemon
) {
1089 /* we have to init fdstat here. */
1090 fd_open(0, FD_LOG
, "stdin");
1091 fd_open(1, FD_LOG
, "stdout");
1092 fd_open(2, FD_LOG
, "stderr");
1095 #if USE_WIN32_SERVICE
1097 WIN32_svcstatusupdate(SERVICE_START_PENDING
, 10000);
1103 #if USE_WIN32_SERVICE
1105 WIN32_svcstatusupdate(SERVICE_RUNNING
, 0);
1112 if (do_reconfigure
) {
1115 #if defined(_SQUID_MSWIN_) && defined(_DEBUG)
1117 } else if (do_debug_trap
) {
1122 } else if (do_rotate
) {
1125 } else if (do_shutdown
) {
1126 time_t wait
= do_shutdown
> 0 ? (int) Config
.shutdownLifetime
: 0;
1127 debug(1, 1) ("Preparing for shutdown after %d requests\n",
1128 statCounter
.client_http
.requests
);
1129 debug(1, 1) ("Waiting %d seconds for active connections to finish\n",
1133 #if USE_WIN32_SERVICE
1135 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, (wait
+ 1) * 1000);
1138 serverConnectionsClose();
1139 eventAdd("SquidShutdown", SquidShutdown
, NULL
, (double) (wait
+ 1), 1);
1143 int loop_delay
= eventNextTime();
1148 /* Attempt any pending storedir IO */
1149 Store::Root().callback();
1151 comm_calliocallback();
1153 /* and again to deal with indirectly queued events
1154 * resulting from the first call. These are usually
1155 * callbacks and should be dealt with immediately.
1157 if (comm_iocallbackpending())
1158 comm_calliocallback();
1160 switch (comm_select(loop_delay
)) {
1163 errcount
= 0; /* reset if successful */
1168 debug(1, 0) ("Select loop Error. Retry %d\n", errcount
);
1171 fatal_dump("Select Loop failed!");
1179 SquidShutdown(NULL
);
1184 fatal_dump("MAIN: Internal error -- this should never happen.");
1191 #if USE_WIN32_SERVICE
1207 if (strcmp(Config
.pidFilename
, "none") == 0) {
1208 debug(0, 1) ("No pid_filename specified. Trusting you know what you are doing.\n");
1211 pid
= readPidFile();
1214 #if USE_WIN32_SERVICE
1216 if (opt_signal_service
) {
1217 WIN32_sendSignal(opt_send_signal
);
1220 #ifdef _SQUID_MSWIN_
1222 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1223 fprintf(stderr
, "signal to Squid Service:\n");
1224 fprintf(stderr
, "missing -n command line switch.\n");
1233 if (kill(pid
, opt_send_signal
) &&
1234 /* ignore permissions if just running check */
1235 !(opt_send_signal
== 0 && errno
== EPERM
)) {
1236 fprintf(stderr
, "%s: ERROR: Could not send ", appname
);
1237 fprintf(stderr
, "signal %d to process %d: %s\n",
1238 opt_send_signal
, (int) pid
, xstrerror());
1242 fprintf(stderr
, "%s: ERROR: No running copy\n", appname
);
1246 /* signal successfully sent */
1250 #ifndef _SQUID_MSWIN_
1252 * This function is run when Squid is in daemon mode, just
1253 * before the parent forks and starts up the child process.
1254 * It can be used for admin-specific tasks, such as notifying
1255 * someone that Squid is (re)started.
1258 mainStartScript(const char *prog
)
1260 char script
[SQUID_MAXPATHLEN
];
1265 xstrncpy(script
, prog
, MAXPATHLEN
);
1267 if ((t
= strrchr(script
, '/'))) {
1269 sl
= strlen(script
);
1272 xstrncpy(&script
[sl
], squid_start_script
, MAXPATHLEN
- sl
);
1274 if ((cpid
= fork()) == 0) {
1276 execl(script
, squid_start_script
, NULL
);
1282 rpid
= wait3(&status
, 0, NULL
);
1286 rpid
= waitpid(-1, &status
, 0);
1289 } while (rpid
!= cpid
);
1293 #endif /* _SQUID_MSWIN_ */
1296 checkRunningPid(void)
1303 pid
= readPidFile();
1308 if (kill(pid
, 0) < 0)
1311 debugs(0, 0, "Squid is already running! Process ID " << pid
);
1317 watch_child(char *argv
[])
1319 #ifndef _SQUID_MSWIN_
1340 if (*(argv
[0]) == '(')
1343 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1345 if ((pid
= fork()) < 0)
1346 syslog(LOG_ALERT
, "fork failed: %s", xstrerror());
1351 syslog(LOG_ALERT
, "setsid failed: %s", xstrerror());
1357 if ((i
= open("/dev/tty", O_RDWR
| O_TEXT
)) >= 0) {
1358 ioctl(i
, TIOCNOTTY
, NULL
);
1366 * RBCOLLINS - if cygwin stackdumps when squid is run without
1367 * -N, check the cygwin1.dll version, it needs to be AT LEAST
1368 * 1.1.3. execvp had a bit overflow error in a loop..
1370 /* Connect stdio to /dev/null in daemon mode */
1371 nullfd
= open("/dev/null", O_RDWR
| O_TEXT
);
1374 fatalf("/dev/null: %s\n", xstrerror());
1378 if (opt_debug_stderr
< 0) {
1384 mainStartScript(argv
[0]);
1386 if ((pid
= fork()) == 0) {
1388 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1389 prog
= xstrdup(argv
[0]);
1390 argv
[0] = xstrdup("(squid)");
1392 syslog(LOG_ALERT
, "execvp failed: %s", xstrerror());
1396 openlog(appname
, LOG_PID
| LOG_NDELAY
| LOG_CONS
, LOG_LOCAL4
);
1398 syslog(LOG_NOTICE
, "Squid Parent: child process %d started", pid
);
1402 squid_signal(SIGINT
, SIG_IGN
, SA_RESTART
);
1406 pid
= wait3(&status
, 0, NULL
);
1410 pid
= waitpid(-1, &status
, 0);
1416 if (WIFEXITED(status
)) {
1418 "Squid Parent: child process %d exited with status %d",
1419 pid
, WEXITSTATUS(status
));
1420 } else if (WIFSIGNALED(status
)) {
1422 "Squid Parent: child process %d exited due to signal %d",
1423 pid
, WTERMSIG(status
));
1425 syslog(LOG_NOTICE
, "Squid Parent: child process %d exited", pid
);
1428 if (stop
- start
< 10)
1433 if (failcount
== 5) {
1434 syslog(LOG_ALERT
, "Exiting due to repeated, frequent failures");
1438 if (WIFEXITED(status
))
1439 if (WEXITSTATUS(status
) == 0)
1442 if (WIFSIGNALED(status
)) {
1443 switch (WTERMSIG(status
)) {
1454 squid_signal(SIGINT
, SIG_DFL
, SA_RESTART
);
1459 #endif /* _SQUID_MSWIN_ */
1463 SquidShutdown(void *unused
)
1465 #if USE_WIN32_SERVICE
1466 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1469 debug(1, 1) ("Shutting down...\n");
1479 externalAclShutdown();
1480 icpConnectionClose();
1487 snmpConnectionClose();
1491 wccpConnectionClose();
1494 releaseServerSockets();
1495 commCloseAllSockets();
1498 DelayPools::FreePools();
1501 authenticateShutdown();
1506 #if USE_WIN32_SERVICE
1508 WIN32_svcstatusupdate(SERVICE_STOP_PENDING
, 10000);
1511 Store::Root().sync(); /* Flush pending object writes/unlinks */
1512 storeDirWriteCleanLogs(0);
1515 Store::Root().sync(); /* Flush log writes */
1518 useragentLogClose();
1525 Store::Root().sync(); /* Flush log close */
1526 StoreFileSystem::FreeAllFs();
1527 DiskIOModule::FreeAllModules();
1528 #if PURIFY || XMALLOC_TRACE
1532 /*stmemFreeMemory(); */
1534 ipcacheFreeMemory();
1535 fqdncacheFreeMemory();
1537 clientdbFreeMemory();
1538 httpHeaderCleanModule();
1546 if (opt_no_daemon
) {
1561 xmalloc_find_leaks();
1563 debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total
);
1572 if (Config
.pidFilename
&& strcmp(Config
.pidFilename
, "none") != 0) {
1574 safeunlink(Config
.pidFilename
, 0);
1578 debug(1, 1) ("Squid Cache (Version %s): Exiting normally.\n",