/*
- * $Id: peer_select.cc,v 1.14 1997/06/18 00:20:01 wessels Exp $
+ * $Id: peer_select.cc,v 1.15 1997/06/21 02:38:13 wessels Exp $
*
* DEBUG: section 44 Peer Selection Algorithm
* AUTHOR: Duane Wessels
debug(44, 3) ("peerSelect: direct = %d\n", direct);
if (direct == DIRECT_YES) {
debug(44, 3) ("peerSelect: DIRECT\n");
- hierarchyNote(request, DIRECT, &psstate->icp, request->host);
+ hierarchyNote(&request->hier, DIRECT, &psstate->icp, request->host);
peerSelectCallback(psstate, NULL);
return;
}
if ((p = psstate->first_parent_miss)) {
code = FIRST_PARENT_MISS;
debug(44, 3) ("peerSelect: %s/%s\n", hier_strings[code], p->host);
- hierarchyNote(request, code, &psstate->icp, p->host);
+ hierarchyNote(&request->hier, code, &psstate->icp, p->host);
peerSelectCallback(psstate, p);
} else if (direct != DIRECT_NO) {
code = DIRECT;
debug(44, 3) ("peerSelect: %s/%s\n", hier_strings[code], request->host);
- hierarchyNote(request, code, &psstate->icp, request->host);
+ hierarchyNote(&request->hier, code, &psstate->icp, request->host);
peerSelectCallback(psstate, NULL);
} else if ((p = peerGetSomeParent(request, &code))) {
debug(44, 3) ("peerSelect: %s/%s\n", hier_strings[code], p->host);
- hierarchyNote(request, code, &psstate->icp, p->host);
+ hierarchyNote(&request->hier, code, &psstate->icp, p->host);
peerSelectCallback(psstate, p);
} else {
code = NO_DIRECT_FAIL;
- hierarchyNote(request, code, &psstate->icp, NULL);
+ hierarchyNote(&request->hier, code, &psstate->icp, NULL);
peerSelectCallbackFail(psstate);
}
}
}
}
} else if (op == ICP_OP_HIT || op == ICP_OP_HIT_OBJ) {
- hierarchyNote(request,
+ hierarchyNote(&request->hier,
type == PEER_PARENT ? PARENT_HIT : SIBLING_HIT,
&psstate->icp,
p->host);
peerSelectCallback(psstate, p);
return;
} else if (op == ICP_OP_SECHO) {
- hierarchyNote(request,
+ hierarchyNote(&request->hier,
SOURCE_FASTEST,
&psstate->icp,
request->host);
/*
- * $Id: stat.cc,v 1.144 1997/06/20 05:08:32 wessels Exp $
+ * $Id: stat.cc,v 1.145 1997/06/21 02:38:16 wessels Exp $
*
* DEBUG: section 18 Cache Manager Statistics
* AUTHOR: Harvest Derived
static const char *describeStatuses _PARAMS((const StoreEntry *));
static const char *describeFlags _PARAMS((const StoreEntry *));
static const char *describeTimestamps _PARAMS((const StoreEntry *));
-static FILE_WALK_HD logReadEndHandler;
static void proto_count _PARAMS((cacheinfo *, protocol_t, log_type));
static void proto_newobject _PARAMS((cacheinfo *, protocol_t, int, int));
static void proto_purgeobject _PARAMS((cacheinfo *, protocol_t, int));
static void proto_touchobject _PARAMS((cacheinfo *, protocol_t, int));
-static FILE_WALK_HD squidReadEndHandler;
static void statFiledescriptors _PARAMS((StoreEntry *));
static void stat_io_get _PARAMS((StoreEntry *));
static void stat_objects_get _PARAMS((StoreEntry *, int vm_or_not));
static void stat_utilization_get _PARAMS((cacheinfo *, StoreEntry *, const char *desc));
-static FILE_WALK_LHD logReadHandler;
-static FILE_WALK_LHD squidReadHandler;
static int memoryAccounted _PARAMS((void));
-static int LogfileStatus;
-static int LogfileFD;
-static char *LogfileName;
-
#ifdef XMALLOC_STATISTICS
static void info_get_mallstat _PARAMS((int, int, StoreEntry *));
#endif
}
}
-
-/* generate logfile status information */
-void
-log_status_get(StoreEntry * sentry)
-{
- if (LogfileStatus == LOG_ENABLE) {
- storeAppendPrintf(sentry, "{\"Logfile is Enabled. Filename: %s\"}\n",
- LogfileName);
- } else {
- storeAppendPrintf(sentry, "{\"Logfile is Disabled.\"}\n");
- }
-}
-
-
-
-/* log convert handler */
-/* call for each line in file, use fileWalk routine */
-static void
-logReadHandler(int fd_unused, const char *buf, int size_unused, void *data)
-{
- log_read_data_t *ctrl = data;
- storeAppendPrintf(ctrl->sentry, "{%s}\n", buf);
-}
-
-/* log convert end handler */
-/* call when a walk is completed or error. */
-static void
-logReadEndHandler(int fd, int errflag_unused, void *data)
-{
- log_read_data_t *ctrl = data;
- storeAppendPrintf(ctrl->sentry, close_bracket);
- storeComplete(ctrl->sentry);
- safe_free(ctrl);
- file_close(fd);
-}
-
-/* start converting logfile to processed format */
-void
-log_get_start(StoreEntry * sentry)
-{
- log_read_data_t *data = NULL;
- int fd;
-
- if (LogfileStatus == LOG_DISABLE) {
- /* Manufacture status when logging is disabled */
- log_status_get(sentry);
- storeComplete(sentry);
- return;
- }
- fd = file_open(LogfileName, O_RDONLY, NULL, NULL);
- if (fd < 0) {
- debug(50, 0) ("Cannot open logfile: %s: %s\n",
- LogfileName, xstrerror());
- return;
- }
- data = xcalloc(1, sizeof(log_read_data_t));
- data->sentry = sentry;
- storeAppendPrintf(sentry, "{\n");
- file_walk(fd,
- logReadEndHandler,
- data,
- logReadHandler,
- data);
- return;
-}
-
-
-/* squid convert handler */
-/* call for each line in file, use fileWalk routine */
-static void
-squidReadHandler(int fd_unused, const char *buf, int size_unused, void *data)
-{
- squid_read_data_t *ctrl = data;
- storeAppendPrintf(ctrl->sentry, "{\"%s\"}\n", buf);
-}
-
-/* squid convert end handler */
-/* call when a walk is completed or error. */
-static void
-squidReadEndHandler(int fd_unused, int errflag_unused, void *data)
-{
- squid_read_data_t *ctrl = data;
- storeAppendPrintf(ctrl->sentry, close_bracket);
- storeComplete(ctrl->sentry);
- file_close(ctrl->fd);
- safe_free(ctrl);
-}
-
-
-/* start convert squid.conf file to processed format */
-void
-squid_get_start(StoreEntry * sentry)
-{
- squid_read_data_t *data;
-
- data = xcalloc(1, sizeof(squid_read_data_t));
- data->sentry = sentry;
- data->fd = file_open(ConfigFile, O_RDONLY, NULL, NULL);
- storeAppendPrintf(sentry, open_bracket);
- file_walk(data->fd, squidReadEndHandler, data, squidReadHandler, data);
-}
-
void
server_list(StoreEntry * sentry)
{
storeAppendPrintf(sentry, close_bracket);
}
-#if LOG_FULL_HEADERS
-static const char c2x[] =
-"000102030405060708090a0b0c0d0e0f"
-"101112131415161718191a1b1c1d1e1f"
-"202122232425262728292a2b2c2d2e2f"
-"303132333435363738393a3b3c3d3e3f"
-"404142434445464748494a4b4c4d4e4f"
-"505152535455565758595a5b5c5d5e5f"
-"606162636465666768696a6b6c6d6e6f"
-"707172737475767778797a7b7c7d7e7f"
-"808182838485868788898a8b8c8d8e8f"
-"909192939495969798999a9b9c9d9e9f"
-"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
-"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
-"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
-"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
-"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
-"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
-
-/* log_quote -- URL-style encoding on MIME headers. */
-
-static char *
-log_quote(const char *header)
-{
- int c, i;
- char *buf, *buf_cursor;
- if (header == NULL) {
- buf = xcalloc(1, 1);
- *buf = '\0';
- return buf;
- }
- buf = xcalloc((strlen(header) * 3) + 1, 1);
- buf_cursor = buf;
- /*
- * We escape: \x00-\x1F"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF
- * which is the default escape list for the CPAN Perl5 URI module
- * modulo the inclusion of space (x40) to make the raw logs a bit
- * more readable.
- */
- while ((c = *(const unsigned char *) header++)) {
- if (c <= 0x1F
- || c >= 0x7F
- || c == '"'
- || c == '#'
- || c == '%'
- || c == ';'
- || c == '<'
- || c == '>'
- || c == '?'
- || c == '{'
- || c == '}'
- || c == '|'
- || c == '\\'
- || c == '^'
- || c == '~'
- || c == '`'
- || c == '['
- || c == ']') {
- *buf_cursor++ = '%';
- i = c * 2;
- *buf_cursor++ = c2x[i];
- *buf_cursor++ = c2x[i + 1];
- } else {
- *buf_cursor++ = c;
- }
- }
- *buf_cursor = '\0';
- return buf;
-}
-#endif /* LOG_FULL_HEADERS */
-
-
-static void
-log_append(const cacheinfo * obj,
- const char *url,
- struct in_addr caddr,
- int size,
- const char *action,
- const char *method,
- int http_code,
- int msec,
- const char *ident,
- const struct _hierarchyLogData *hierData,
-#if LOG_FULL_HEADERS
- const char *request_hdr,
- const char *reply_hdr,
-#endif /* LOG_FULL_HEADERS */
- const char *content_type
-)
-{
-#if LOG_FULL_HEADERS
- LOCAL_ARRAY(char, tmp, 10000); /* MAX_URL is 4096 */
-#else
- LOCAL_ARRAY(char, tmp, 6000); /* MAX_URL is 4096 */
-#endif /* LOG_FULL_HEADERS */
- int x;
- const char *client = NULL;
- hier_code hier_code = HIER_NONE;
- const char *hier_host = dash_str;
- int hier_timeout = 0;
-#ifdef LOG_ICP_NUMBERS
- int ns = 0;
- int ne = 0;
- int nr = 0;
- int tt = 0;
-#endif
-
- if (LogfileStatus != LOG_ENABLE)
- return;
-
- if (Config.Log.log_fqdn)
- client = fqdncache_gethostbyaddr(caddr, 0);
- if (client == NULL)
- client = inet_ntoa(caddr);
- if (!method)
- method = dash_str;
- if (!url)
- url = dash_str;
- if (!content_type || *content_type == '\0')
- content_type = dash_str;
- if (!ident || *ident == '\0')
- ident = dash_str;
- if (hierData) {
- hier_code = hierData->code;
- hier_host = hierData->host ? hierData->host : dash_str;
- hier_timeout = hierData->timeout;
-#ifdef LOG_ICP_NUMBERS
- tt = hierData->delay;
- ns = hierData->n_sent;
- ne = hierData->n_expect;
- nr = hierData->n_recv;
-#endif
- }
- if (Config.commonLogFormat)
- sprintf(tmp, "%s %s - [%s] \"%s %s\" %s:%s %d\n",
- client,
- ident,
- mkhttpdlogtime(&squid_curtime),
- method,
- url,
- action,
- hier_strings[hier_code],
- size);
- else
-#ifdef LOG_ICP_NUMBERS
- sprintf(tmp, "%9d.%03d %6d %s %s/%03d %d %s %s %s %s%s/%s/%d/%d/%d/%d %s\n",
-#else
- sprintf(tmp, "%9d.%03d %6d %s %s/%03d %d %s %s %s %s%s/%s %s\n",
-#endif
- (int) current_time.tv_sec,
- (int) current_time.tv_usec / 1000,
- msec,
- client,
- action,
- http_code,
- size,
- method,
- url,
- ident,
- hier_timeout ? "TIMEOUT_" : "",
- hier_strings[hier_code],
- hier_host,
-#ifdef LOG_ICP_NUMBERS
- ns, ne, nr, tt,
-#endif
- content_type);
-#if LOG_FULL_HEADERS
- if (Config.logMimeHdrs) {
- int msize = strlen(tmp);
- char *ereq = log_quote(request_hdr);
- char *erep = log_quote(reply_hdr);
-
- if (msize + strlen(ereq) + strlen(erep) + 7 <= sizeof(tmp))
- sprintf(tmp + msize - 1, " [%s] [%s]\n", ereq, erep);
- else
- debug(18, 1) ("log_append: Long headers not logged.\n");
- safe_free(ereq);
- safe_free(erep);
- }
-#endif /* LOG_FULL_HEADERS */
- x = file_write(LogfileFD,
- xstrdup(tmp),
- strlen(tmp),
- NULL,
- NULL,
- xfree);
- if (x != DISK_OK)
- debug(18, 1) ("log_append: File write failed.\n");
-}
-
-void
-log_enable(StoreEntry * sentry)
-{
- if (LogfileStatus == LOG_DISABLE) {
- LogfileStatus = LOG_ENABLE;
- /* open the logfile */
- LogfileFD = file_open(LogfileName, O_WRONLY | O_CREAT, NULL, NULL);
- if (LogfileFD == DISK_ERROR) {
- debug(18, 0) ("Cannot open logfile: %s\n", LogfileName);
- LogfileStatus = LOG_DISABLE;
- }
- }
-}
-
-void
-log_disable(StoreEntry * sentry)
-{
- if (LogfileStatus == LOG_ENABLE)
- file_close(LogfileFD);
- LogfileStatus = LOG_DISABLE;
-}
-
-void
-log_clear(StoreEntry * sentry)
-{
- /* what should be done here. Erase file ??? or move it to another name? At the moment, just erase it. bug here need to be fixed. what if there are still data in memory. Need flush here */
- if (LogfileStatus == LOG_ENABLE)
- file_close(LogfileFD);
- unlink(LogfileName);
- /* reopen it anyway */
- LogfileFD = file_open(LogfileName, O_WRONLY | O_CREAT, NULL, NULL);
- if (LogfileFD == DISK_ERROR) {
- debug(18, 0) ("Cannot open logfile: %s\n", LogfileName);
- LogfileStatus = LOG_DISABLE;
- }
- /* at the moment, store one char to make a storage manager happy */
- storeAppendPrintf(sentry, " ");
-}
-
-
-
static void
proto_newobject(cacheinfo * obj, protocol_t proto_id, int size, int restart)
{
debug(18, 5) ("stat_init: Initializing...\n");
obj = xcalloc(1, sizeof(cacheinfo));
- if (logfilename) {
- memset(LogfileName, '\0', SQUID_MAXPATHLEN);
- xstrncpy(LogfileName, logfilename, SQUID_MAXPATHLEN);
- LogfileFD = file_open(LogfileName, O_WRONLY | O_CREAT, NULL, NULL);
- if (LogfileFD == DISK_ERROR) {
- debug(50, 0) ("%s: %s\n", LogfileName, xstrerror());
- fatal("Cannot open logfile.");
- }
- }
- obj->log_append = log_append;
+ if (logfilename)
+ accessLogOpen(logfilename);
obj->proto_id = urlParseProtocol;
obj->proto_newobject = proto_newobject;
obj->proto_purgeobject = proto_purgeobject;
}
*object = obj;
}
-
-void
-stat_rotate_log(void)
-{
- int i;
- LOCAL_ARRAY(char, from, MAXPATHLEN);
- LOCAL_ARRAY(char, to, MAXPATHLEN);
- char *fname = NULL;
- struct stat sb;
-
- if ((fname = LogfileName) == NULL)
- return;
-#ifdef S_ISREG
- if (stat(fname, &sb) == 0)
- if (S_ISREG(sb.st_mode) == 0)
- return;
-#endif
-
- debug(18, 1) ("stat_rotate_log: Rotating\n");
-
- /* Rotate numbers 0 through N up one */
- for (i = Config.Log.rotateNumber; i > 1;) {
- i--;
- sprintf(from, "%s.%d", fname, i - 1);
- sprintf(to, "%s.%d", fname, i);
- rename(from, to);
- }
- /* Rotate the current log to .0 */
- if (Config.Log.rotateNumber > 0) {
- sprintf(to, "%s.%d", fname, 0);
- rename(fname, to);
- }
- /* Close and reopen the log. It may have been renamed "manually"
- * before HUP'ing us. */
- file_close(LogfileFD);
- LogfileFD = file_open(fname, O_WRONLY | O_CREAT, NULL, NULL);
- if (LogfileFD == DISK_ERROR) {
- debug(18, 0) ("stat_rotate_log: Cannot open logfile: %s\n", fname);
- LogfileStatus = LOG_DISABLE;
- fatal("Cannot open logfile.");
- }
-}
-
-void
-statCloseLog(void)
-{
- file_close(LogfileFD);
-}