From: wessels <> Date: Sat, 25 Jul 1998 06:16:23 +0000 (+0000) Subject: rewrote shutdown code. Using FD timeouts suck{s,ed}. Now we use event X-Git-Tag: SQUID_3_0_PRE1~2980 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d723bf6b9c7e68c0c0e56467ff8337c8607fbdcf;p=thirdparty%2Fsquid.git rewrote shutdown code. Using FD timeouts suck{s,ed}. Now we use event handlers and close any open sockets (or call timeout handlers) when the shutdown event runs. --- diff --git a/src/comm.cc b/src/comm.cc index bb01d92ce7..50cb9c6e6f 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1,7 +1,7 @@ /* - * $Id: comm.cc,v 1.278 1998/07/22 20:37:11 wessels Exp $ + * $Id: comm.cc,v 1.279 1998/07/25 00:16:23 wessels Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -869,3 +869,28 @@ ignoreErrno(int ierrno) } /* NOTREACHED */ } + +void +commCloseAllSockets(void) +{ + int fd; + fde *F = NULL; + PF *callback; + for (fd = 0; fd <= Biggest_FD; fd++) { + F = &fd_table[fd]; + if (F->open != FD_OPEN) + continue; + if (F->type != FD_SOCKET) + continue; + if (F->timeout_handler) { + debug(5, 5) ("commCloseAllSockets: FD %d: Calling timeout handler\n", + fd); + callback = F->timeout_handler; + F->timeout_handler = NULL; + callback(fd, F->timeout_data); + } else { + debug(5, 5) ("commCloseAllSockets: FD %d: calling comm_close()\n", fd); + comm_close(fd); + } + } +} diff --git a/src/comm_select.cc b/src/comm_select.cc index ab45f8759f..d893c90d4b 100644 --- a/src/comm_select.cc +++ b/src/comm_select.cc @@ -1,7 +1,7 @@ /* - * $Id: comm_select.cc,v 1.2 1998/07/22 20:37:11 wessels Exp $ + * $Id: comm_select.cc,v 1.3 1998/07/25 00:16:25 wessels Exp $ * * DEBUG: section 5 Socket Functions * @@ -269,18 +269,6 @@ comm_poll(int msec) #if !ALARM_UPDATES_TIME getCurrentTime(); #endif - if (shutting_down) { - serverConnectionsClose(); - dnsShutdownServers(); - redirectShutdownServers(); - /* shutting_down will be set to - * +1 for SIGTERM - * -1 for SIGINT */ - if (shutting_down > 0) - setSocketShutdownLifetimes(Config.shutdownLifetime); - else - setSocketShutdownLifetimes(1); - } #if USE_ASYNC_IO aioCheckCallbacks(); #endif @@ -306,10 +294,10 @@ comm_poll(int msec) nfds++; } } - if (shutting_down) - debug(5, 2) ("comm_poll: Still waiting on %d FDs\n", nfds); - if (nfds == 0) + if (nfds == 0) { + assert(shutting_down); return COMM_SHUTDOWN; + } if (msec > MAX_POLL_TIME) msec = MAX_POLL_TIME; for (;;) { @@ -537,18 +525,6 @@ comm_select(int msec) #endif FD_ZERO(&readfds); FD_ZERO(&writefds); - if (shutting_down) { - serverConnectionsClose(); - dnsShutdownServers(); - redirectShutdownServers(); - /* shutting_down will be set to - * +1 for SIGTERM - * -1 for SIGINT */ - if (shutting_down > 0) - setSocketShutdownLifetimes(Config.shutdownLifetime); - else - setSocketShutdownLifetimes(1); - } if (commCheckICPIncoming) comm_select_icp_incoming(); if (commCheckHTTPIncoming) @@ -567,10 +543,10 @@ comm_select(int msec) FD_SET(i, &writefds); } } - if (shutting_down) - debug(5, 2) ("comm_select: Still waiting on %d FDs\n", nfds); - if (nfds == 0) + if (nfds == 0) { + assert(shutting_down); return COMM_SHUTDOWN; + } if (msec > MAX_POLL_TIME) msec = MAX_POLL_TIME; for (;;) { diff --git a/src/dns.cc b/src/dns.cc index 3a487701f2..6920ca6bdb 100644 --- a/src/dns.cc +++ b/src/dns.cc @@ -1,6 +1,6 @@ /* - * $Id: dns.cc,v 1.61 1998/07/22 20:37:14 wessels Exp $ + * $Id: dns.cc,v 1.62 1998/07/25 00:16:26 wessels Exp $ * * DEBUG: section 34 Dnsserver interface * AUTHOR: Harvest Derived @@ -189,10 +189,11 @@ dnsStats(StoreEntry * sentry) } void -dnsShutdownServers(void) +dnsShutdownServers(void *notused) { dnsserver_t *dns = NULL; int k; + int na = 0; debug(34, 3) ("dnsShutdownServers:\n"); for (k = 0; k < NDnsServersAlloc; k++) { dns = *(dns_child_table + k); @@ -203,6 +204,7 @@ dnsShutdownServers(void) if (EBIT_TEST(dns->flags, HELPER_BUSY)) { debug(34, 3) ("dnsShutdownServers: #%d is BUSY.\n", dns->id); EBIT_SET(dns->flags, HELPER_SHUTDOWN); + na++; continue; } if (EBIT_TEST(dns->flags, HELPER_CLOSING)) { @@ -211,6 +213,8 @@ dnsShutdownServers(void) } dnsShutdownServer(dns); } + if (na) + eventAdd("dnsShutdownServers", dnsShutdownServers, NULL, 1.0, 1); } void diff --git a/src/main.cc b/src/main.cc index 72621e1951..df71eaa606 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,6 @@ /* - * $Id: main.cc,v 1.261 1998/07/24 16:14:13 wessels Exp $ + * $Id: main.cc,v 1.262 1998/07/25 00:16:27 wessels Exp $ * * DEBUG: section 1 Startup and Main Loop * AUTHOR: Harvest Derived @@ -63,12 +63,11 @@ static void sendSignal(void); static void serverConnectionsOpen(void); static void watch_child(char **); static void setEffectiveUser(void); -static void normal_shutdown(void); #if MEM_GEN_TRACE extern void log_trace_done(); extern void log_trace_init(char *); #endif -static EVH force_shutdown; +static EVH SquidShutdown; static void usage(void) @@ -288,10 +287,7 @@ serverConnectionsOpen(void) void serverConnectionsClose(void) { - /* - * NOTE, this function will be called repeatedly while shutdown - * is pending - */ + assert(shutting_down); clientHttpConnectionsClose(); icpConnectionShutdown(); #if USE_HTCP @@ -318,8 +314,8 @@ mainReconfigure(void) #ifdef SQUID_SNMP snmpConnectionClose(); #endif - dnsShutdownServers(); - redirectShutdownServers(); + dnsShutdownServers(NULL); + redirectShutdownServers(NULL); storeDirCloseSwapLogs(); errorFree(); parseConfigFile(ConfigFile); @@ -565,8 +561,10 @@ main(int argc, char **argv) wait); do_shutdown = 0; shutting_down = 1; - eventAdd("force_shutdown", force_shutdown, - NULL, (double) (wait+1), 1); + serverConnectionsClose(); + eventAdd("dnsShutdownServers", dnsShutdownServers, NULL, 0.0, 1); + eventAdd("redirectShutdownServers", redirectShutdownServers, NULL, 0.0, 1); + eventAdd("SquidShutdown", SquidShutdown, NULL, (double) (wait + 1), 1); } eventRun(); if ((loop_delay = eventNextTime()) < 0) @@ -585,23 +583,11 @@ main(int argc, char **argv) if (errcount == 10) fatal_dump("Select Loop failed!"); break; - case COMM_SHUTDOWN: - /* delayed close so we can transmit while shutdown pending */ - icpConnectionClose(); -#if USE_HTCP - htcpSocketClose(); -#endif -#ifdef SQUID_SNMP - snmpConnectionClose(); -#endif - if (shutting_down) { - normal_shutdown(); - } else { - fatal_dump("MAIN: Unexpected SHUTDOWN from comm_select."); - } - break; case COMM_TIMEOUT: break; + case COMM_SHUTDOWN: + SquidShutdown(NULL); + break; default: fatal_dump("MAIN: Internal error -- this should never happen."); break; @@ -688,7 +674,7 @@ watch_child(char *argv[]) } static void -normal_shutdown(void) +SquidShutdown(void *unused) { debug(1, 1) ("Shutting down...\n"); if (Config.pidFilename && strcmp(Config.pidFilename, "none")) { @@ -696,7 +682,15 @@ normal_shutdown(void) safeunlink(Config.pidFilename, 0); leave_suid(); } + icpConnectionClose(); +#if USE_HTCP + htcpSocketClose(); +#endif +#ifdef SQUID_SNMP + snmpConnectionClose(); +#endif releaseServerSockets(); + commCloseAllSockets(); unlinkdClose(); storeDirWriteCleanLogs(0); PrintRusage(); @@ -731,16 +725,8 @@ normal_shutdown(void) #if MEM_GEN_TRACE log_trace_done(); #endif - debug(1, 0) ("Squid Cache (Version %s): Exiting normally.\n", version_string); fclose(debug_log); exit(0); } - -static void -force_shutdown(void *unused) -{ - fdDumpOpen(); - fatal_dump("Shutdown procedure failed"); -} diff --git a/src/protos.h b/src/protos.h index 9d3339023d..c6fea1da16 100644 --- a/src/protos.h +++ b/src/protos.h @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.241 1998/07/23 19:57:52 wessels Exp $ + * $Id: protos.h,v 1.242 1998/07/25 00:16:28 wessels Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -151,6 +151,7 @@ extern void commCallCloseHandlers(int fd); extern int commSetTimeout(int fd, int, PF *, void *); extern void commSetDefer(int fd, DEFER * func, void *); extern int ignoreErrno(int); +extern void commCloseAllSockets(void); /* * comm_select.c @@ -195,7 +196,7 @@ extern int file_read(int, char *, int, off_t, DRCB *, void *); extern void disk_init(void); extern int diskWriteIsComplete(int); -extern void dnsShutdownServers(void); +extern void dnsShutdownServers(void *); extern void dnsShutdownServer(dnsserver_t * dns); extern void dnsOpenServers(void); extern dnsserver_t *dnsGetFirstAvailable(void); @@ -508,7 +509,6 @@ extern EVH ipcache_purgelru; extern const ipcache_addrs *ipcache_gethostbyname(const char *, int flags); extern void ipcacheInvalidate(const char *); extern void ipcacheReleaseInvalid(const char *); -extern void ipcacheShutdownServers(void); extern void ipcache_init(void); extern void stat_ipcache_get(StoreEntry *); extern int ipcacheQueueDrain(void); @@ -637,7 +637,7 @@ extern void urnStart(request_t *, StoreEntry *); extern void redirectStart(clientHttpRequest *, RH *, void *); extern void redirectOpenServers(void); -extern void redirectShutdownServers(void); +extern void redirectShutdownServers(void *); extern void redirectStats(StoreEntry *); extern int redirectUnregister(const char *url, void *); extern void redirectFreeMemory(void); diff --git a/src/redirect.cc b/src/redirect.cc index 236b5d2771..8746eda163 100644 --- a/src/redirect.cc +++ b/src/redirect.cc @@ -1,6 +1,6 @@ /* - * $Id: redirect.cc,v 1.63 1998/07/22 20:37:44 wessels Exp $ + * $Id: redirect.cc,v 1.64 1998/07/25 00:16:29 wessels Exp $ * * DEBUG: section 29 Redirector * AUTHOR: Duane Wessels @@ -370,11 +370,12 @@ redirectOpenServers(void) } void -redirectShutdownServers(void) +redirectShutdownServers(void *unused) { redirector_t *redirect = NULL; redirectStateData *r = NULL; int k; + int na = 0; if (Config.Program.redirect == NULL) return; if (redirectQueueHead) { @@ -386,19 +387,22 @@ redirectShutdownServers(void) redirect = *(redirect_child_table + k); if (!EBIT_TEST(redirect->flags, HELPER_ALIVE)) continue; - if (EBIT_TEST(redirect->flags, HELPER_BUSY)) - continue; if (EBIT_TEST(redirect->flags, HELPER_CLOSING)) continue; + if (EBIT_TEST(redirect->flags, HELPER_BUSY)) { + na++; + continue; + } debug(29, 3) ("redirectShutdownServers: closing redirector #%d, FD %d\n", redirect->index + 1, redirect->fd); comm_close(redirect->fd); EBIT_SET(redirect->flags, HELPER_CLOSING); EBIT_SET(redirect->flags, HELPER_BUSY); } + if (na) + eventAdd("redirectShutdownServers", redirectShutdownServers, NULL, 1.0, 1); } - int redirectUnregister(const char *url, void *data) { diff --git a/src/tools.cc b/src/tools.cc index d85dd53eb2..2897f55796 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -1,6 +1,6 @@ /* - * $Id: tools.cc,v 1.164 1998/07/22 20:38:02 wessels Exp $ + * $Id: tools.cc,v 1.165 1998/07/25 00:16:30 wessels Exp $ * * DEBUG: section 21 Misc Functions * AUTHOR: Harvest Derived @@ -48,7 +48,6 @@ Thanks!\n" static void fatal_common(const char *); static void fatalvf(const char *fmt, va_list args); static void mail_warranty(void); -static void shutdownTimeoutHandler(int fd, void *data); #if USE_ASYNC_IO static void safeunlinkComplete(void *data, int retcode, int errcode); #endif @@ -289,33 +288,6 @@ sigusr2_handle(int sig) #endif } -static void -shutdownTimeoutHandler(int fd, void *datanotused) -{ - debug(21, 1) ("Forcing close of FD %d\n", fd); - comm_close(fd); -} - -void -setSocketShutdownLifetimes(int to) -{ - fde *f = NULL; - int i; - for (i = Biggest_FD; i >= 0; i--) { - f = &fd_table[i]; - if (!f->read_handler && !f->write_handler) - continue; - if (f->type != FD_SOCKET) - continue; - if (f->timeout > 0 && (int) (f->timeout - squid_curtime) <= to) - continue; - commSetTimeout(i, - to, - f->timeout_handler ? f->timeout_handler : shutdownTimeoutHandler, - f->timeout_data); - } -} - static void fatal_common(const char *message) {