From: wessels <> Date: Wed, 11 Oct 2000 00:15:30 +0000 (+0000) Subject: DW: X-Git-Tag: SQUID_3_0_PRE1~1815 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=08e8e020569dfd9d49330bc4e65a13b6cca85533;p=thirdparty%2Fsquid.git DW: - My change to eliminate stdio from net_db.c sucked. It leaked memory, and it had awkward buffer offset junk. Its probably better to use the logfile.c code, which unfortunately requires a change to logfileOpen(). Previously the logfile code exited with a fatal message if there was an error opening or writing the file. For netdb we don't care that much, so I added a "fatal" flag to logfileOpen(). --- diff --git a/src/access_log.cc b/src/access_log.cc index 69809a3597..bc995936c8 100644 --- a/src/access_log.cc +++ b/src/access_log.cc @@ -1,6 +1,6 @@ /* - * $Id: access_log.cc,v 1.58 2000/06/06 19:34:31 hno Exp $ + * $Id: access_log.cc,v 1.59 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 46 Access Log * AUTHOR: Duane Wessels @@ -329,7 +329,7 @@ void accessLogInit(void) { assert(sizeof(log_tags) == (LOG_TYPE_MAX + 1) * sizeof(char *)); - logfile = logfileOpen(Config.Log.access, MAX_URL << 1); + logfile = logfileOpen(Config.Log.access, MAX_URL << 1, 1); LogfileStatus = LOG_ENABLE; #if HEADERS_LOG headerslog = logfileOpen("/usr/local/squid/logs/headers.log", 512); diff --git a/src/logfile.cc b/src/logfile.cc index 04d8a6b936..707771d131 100644 --- a/src/logfile.cc +++ b/src/logfile.cc @@ -1,5 +1,5 @@ /* - * $Id: logfile.cc,v 1.5 2000/07/12 16:15:15 wessels Exp $ + * $Id: logfile.cc,v 1.6 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 50 Log file handling * AUTHOR: Duane Wessels @@ -37,17 +37,17 @@ static void logfileWriteWrapper(Logfile * lf, const void *buf, size_t len); Logfile * -logfileOpen(const char *path, size_t bufsz) +logfileOpen(const char *path, size_t bufsz, int fatal_flag) { int fd; Logfile *lf; fd = file_open(path, O_WRONLY | O_CREAT); if (DISK_ERROR == fd) { - if (ENOENT == errno) { + if (ENOENT == errno && fatal_flag) { fatalf("Cannot open '%s' because\n" "\tthe parent directory does not exist.\n" "\tPlease create the directory.\n", path); - } else if (EACCES == errno) { + } else if (EACCES == errno && fatal_flag) { fatalf("Cannot open '%s' for writing.\n" "\tThe parent directory must be writeable by the\n" "\tuser '%s', which is the cache_effective_user\n" @@ -59,6 +59,8 @@ logfileOpen(const char *path, size_t bufsz) } lf = xcalloc(1, sizeof(*lf)); lf->fd = fd; + if (fatal_flag) + lf->flags.fatal = 1; xstrncpy(lf->path, path, MAXPATHLEN); if (bufsz > 0) { lf->buf = xmalloc(bufsz); @@ -107,7 +109,7 @@ logfileRotate(Logfile * lf) } /* Reopen the log. It may have been renamed "manually" */ lf->fd = file_open(lf->path, O_WRONLY | O_CREAT); - if (DISK_ERROR == lf->fd) { + if (DISK_ERROR == lf->fd && lf->flags.fatal) { debug(50, 1) ("logfileRotate: %s: %s\n", lf->path, xstrerror()); fatalf("Cannot open %s: %s", lf->path, xstrerror()); } @@ -179,5 +181,7 @@ logfileWriteWrapper(Logfile * lf, const void *buf, size_t len) fd_bytes(lf->fd, s, FD_WRITE); if (s == len) return; + if (!lf->flags.fatal) + return; fatalf("logfileWrite: %s: %s\n", lf->path, xstrerror()); } diff --git a/src/net_db.cc b/src/net_db.cc index 2516688ec4..9cd8ad9bfe 100644 --- a/src/net_db.cc +++ b/src/net_db.cc @@ -1,6 +1,6 @@ /* - * $Id: net_db.cc,v 1.148 2000/10/06 05:42:46 wessels Exp $ + * $Id: net_db.cc,v 1.149 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 38 Network Measurement Database * AUTHOR: Duane Wessels @@ -368,34 +368,27 @@ static void netdbSaveState(void *foo) { LOCAL_ARRAY(char, path, SQUID_MAXPATHLEN); - int fd; + Logfile *lf; netdbEntry *n; net_db_name *x; struct timeval start = current_time; int count = 0; - size_t wl = 4096; - char *wbuf = xmalloc(wl); - off_t wo = 0; snprintf(path, SQUID_MAXPATHLEN, "%s/netdb_state", storeSwapDir(0)); /* * This was nicer when we were using stdio, but thanks to * Solaris bugs, its a bad idea. fopen can fail if more than * 256 FDs are open. */ - fd = file_open(path, O_WRONLY | O_CREAT | O_TRUNC); - if (fd < 0) { + lf = logfileOpen(path, 4096, 0); + if (NULL == lf) { debug(50, 1) ("netdbSaveState: %s: %s\n", path, xstrerror()); return; } hash_first(addr_table); while ((n = (netdbEntry *) hash_next(addr_table))) { -#define RBUF_SZ 4096 - char rbuf[RBUF_SZ]; - off_t ro; if (n->pings_recv == 0) continue; - ro = 0; - ro += snprintf(rbuf, RBUF_SZ, "%s %d %d %10.5f %10.5f %d %d", + logfilePrintf(lf, "%s %d %d %10.5f %10.5f %d %d", n->network, n->pings_sent, n->pings_recv, @@ -404,24 +397,12 @@ netdbSaveState(void *foo) (int) n->next_ping_time, (int) n->last_use_time); for (x = n->hosts; x; x = x->next) - ro += snprintf(rbuf + ro, RBUF_SZ - ro, " %s", x->name); - ro += snprintf(rbuf + ro, RBUF_SZ - ro, "\n"); - assert(ro <= RBUF_SZ); - while (ro + wo > wl) { - char *t; - t = wbuf; - wl <<= 1; - wbuf = xmalloc(wl); - xmemcpy(wbuf, t, wo); - xfree(t); - } - xmemcpy(wbuf + wo, rbuf, ro); - wo += ro; + logfilePrintf(lf, " %s", x->name); + logfilePrintf(lf, "\n"); count++; #undef RBUF_SZ } - write(fd, wbuf, wo); - file_close(fd); + logfileClose(lf); getCurrentTime(); debug(38, 1) ("NETDB state saved; %d entries, %d msec\n", count, tvSubMsec(start, current_time)); diff --git a/src/protos.h b/src/protos.h index df34490757..2e172ac4f5 100644 --- a/src/protos.h +++ b/src/protos.h @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.381 2000/10/10 02:10:42 wessels Exp $ + * $Id: protos.h,v 1.382 2000/10/10 18:15:30 wessels Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -1179,7 +1179,7 @@ extern void *leakFreeFL(void *, const char *, int); #endif /* logfile.c */ -extern Logfile *logfileOpen(const char *path, size_t bufsz); +extern Logfile *logfileOpen(const char *path, size_t bufsz, int); extern void logfileClose(Logfile * lf); extern void logfileRotate(Logfile * lf); extern void logfileWrite(Logfile * lf, void *buf, size_t len); diff --git a/src/referer.cc b/src/referer.cc index d401b61e8a..96b0a9daac 100644 --- a/src/referer.cc +++ b/src/referer.cc @@ -1,6 +1,6 @@ /* - * $Id: referer.cc,v 1.1 2000/07/13 06:13:43 wessels Exp $ + * $Id: referer.cc,v 1.2 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 40 User-Agent and Referer logging * AUTHOR: Joe Ramey (useragent) @@ -49,7 +49,7 @@ refererOpenLog(void) debug(40, 1) ("Referer logging is disabled.\n"); return; } - logfileOpen(Config.Log.referer, 0); + logfileOpen(Config.Log.referer, 0, 1); #endif } diff --git a/src/store_log.cc b/src/store_log.cc index e29e46f9f5..da476217c1 100644 --- a/src/store_log.cc +++ b/src/store_log.cc @@ -1,6 +1,6 @@ /* - * $Id: store_log.cc,v 1.18 2000/07/21 06:06:22 wessels Exp $ + * $Id: store_log.cc,v 1.19 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 20 Storage Manager Logging Functions * AUTHOR: Duane Wessels @@ -119,5 +119,5 @@ storeLogOpen(void) debug(20, 1) ("Store logging disabled\n"); return; } - storelog = logfileOpen(Config.Log.store, 0); + storelog = logfileOpen(Config.Log.store, 0, 1); } diff --git a/src/structs.h b/src/structs.h index 01aa4612d0..7f792b4973 100644 --- a/src/structs.h +++ b/src/structs.h @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.354 2000/10/04 15:32:14 wessels Exp $ + * $Id: structs.h,v 1.355 2000/10/10 18:15:30 wessels Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -1889,4 +1889,7 @@ struct _Logfile { char *buf; size_t bufsz; off_t offset; + struct { + unsigned int fatal:1; + } flags; }; diff --git a/src/useragent.cc b/src/useragent.cc index 3f978dd690..956b1ec1c0 100644 --- a/src/useragent.cc +++ b/src/useragent.cc @@ -1,6 +1,6 @@ /* - * $Id: useragent.cc,v 1.21 2000/06/21 20:34:37 hno Exp $ + * $Id: useragent.cc,v 1.22 2000/10/10 18:15:30 wessels Exp $ * * DEBUG: section 40 User-Agent logging * AUTHOR: Joe Ramey @@ -48,7 +48,7 @@ useragentOpenLog(void) debug(40, 1) ("User-Agent logging is disabled.\n"); return; } - logfileOpen(Config.Log.useragent, 0); + logfileOpen(Config.Log.useragent, 0, 1); #endif }