From: wessels <> Date: Thu, 28 Mar 1996 01:50:20 +0000 (+0000) Subject: make pid_filename a config option and create writePidFile() in tools.c X-Git-Tag: SQUID_3_0_PRE1~6368 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ccff960108dab2ce4bff860f2dc34def2f8ca1ae;p=thirdparty%2Fsquid.git make pid_filename a config option and create writePidFile() in tools.c --- diff --git a/src/cache_cf.cc b/src/cache_cf.cc index 37d78973d9..1377702f86 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -1,4 +1,4 @@ -/* $Id: cache_cf.cc,v 1.6 1996/03/27 18:15:40 wessels Exp $ */ +/* $Id: cache_cf.cc,v 1.7 1996/03/27 18:50:20 wessels Exp $ */ #include "squid.h" @@ -60,6 +60,7 @@ static struct { } Accel; char *appendDomain; char *debugOptions; + char *pidFilename; } Config; #define DefaultMemMaxSize (16 << 20) /* 16 MB */ @@ -106,7 +107,7 @@ static struct { #define DefaultEffectiveGroup (char *)NULL /* default NONE */ #define DefaultAppendDomain (char *)NULL /* default NONE */ -#define DefaultDebugOptions "ALL,1" +#define DefaultDebugOptions "ALL,1" /* All sections at level 1 */ #define DefaultAccelHost (char *)NULL /* default NONE */ #define DefaultAccelPrefix (char *)NULL /* default NONE */ #define DefaultAccelPort 0 /* default off */ @@ -117,6 +118,9 @@ static struct { #define DefaultNeighborTimeout 2 /* 2 seconds */ #define DefaultStallDelay 3 /* 3 seconds */ #define DefaultSingleParentBypass 0 /* default off */ +#define DefaultPidFilename (char *)NULL /* default NONE */ + +extern char *config_file; stoplist *http_stoplist = NULL; stoplist *gopher_stoplist = NULL; @@ -142,11 +146,6 @@ char w_space[] = " \t\n"; static void configSetFactoryDefaults(); static void configDoConfigure(); -extern int getMaxFD(); -extern void fatal _PARAMS((char *)); -extern void neighbors_cf_add _PARAMS((char *, char *, int, int, int)); -extern int neighbors_cf_domain _PARAMS((char *, char *)); - void self_destruct(in_string) char *in_string; { @@ -946,6 +945,17 @@ static void parseDebugOptionsLine(line_in) Config.debugOptions = xstrdup(token); } +static void parsePidFilenameLine(line_in) + char *line_in; +{ + char *token; + token = strtok(NULL, w_space); + safe_free(Config.pidFilename); + if (token == (char *) NULL) + self_destruct(line_in); + Config.pidFilename = xstrdup(token); +} + int parseConfigFile(file_name) char *file_name; { @@ -1198,6 +1208,9 @@ int parseConfigFile(file_name) else if (!strcmp(token, "debug_options")) parseDebugOptionsLine(line_in); + else if (!strcmp(token, "pid_filename")) + parsePidFilenameLine(line_in); + /* If unknown, treat as a comment line */ else { /* EMPTY */ ; @@ -1259,22 +1272,18 @@ int getHttpMax() { return Config.Http.maxObjSize; } - int getHttpTTL() { return Config.Http.defaultTtl; } - int getGopherMax() { return Config.Gopher.maxObjSize; } - int getGopherTTL() { return Config.Gopher.defaultTtl; } - int getWAISMax() { return Config.Wais.maxObjSize; @@ -1287,99 +1296,80 @@ int getWaisRelayPort() { return Config.Wais.relayPort; } - int getFtpMax() { return Config.Ftp.maxObjSize; } - int getFtpTTL() { return Config.Ftp.defaultTtl; } - int getNegativeTTL() { return Config.negativeTtl; } - int getCacheMemMax() { return Config.Mem.maxSize; } - int getCacheMemHighWaterMark() { return Config.Mem.highWatherMark; } - int getCacheMemLowWaterMark() { return Config.Mem.lowWaterMark; } - double getCacheHotVmFactor() { return Config.hotVmFactor; } - int getCacheSwapHighWaterMark() { return Config.Swap.highWatherMark; } - int getCacheSwapLowWaterMark() { return Config.Swap.lowWaterMark; } - int getCacheSwapMax() { return Config.Swap.maxSize; } - int setCacheSwapMax(size) int size; { Config.Swap.maxSize = size; return Config.Swap.maxSize; } - int getReadTimeout() { return Config.readTimeout; } - int getClientLifetime() { return Config.lifetimeDefault; } - int getConnectTimeout() { return Config.connectTimeout; } - int getCleanRate() { return Config.cleanRate; } - int getSourcePing() { return Config.sourcePing; } - int getDnsChildren() { return Config.dnsChildren; } - int getQuickAbort() { return Config.quickAbort; } - char *getAccelPrefix() { return Config.Accel.prefix; @@ -1452,6 +1442,11 @@ char *getEffectiveGroup() { return Config.effectiveGroup; } +char *getPidFilename() +{ + return Config.pidFilename; +} + int setAsciiPortNum(p) int p; { @@ -1536,7 +1531,7 @@ static void configSetFactoryDefaults() Config.Accel.prefix = safe_xstrdup(DefaultAccelPrefix); Config.Accel.port = DefaultAccelPort; Config.Accel.withProxy = DefaultAccelWithProxy; - + Config.pidFilename = safe_xstrdup(DefaultPidFilename); } static void configDoConfigure() diff --git a/src/debug.cc b/src/debug.cc index 209c1df705..8fa9fb8076 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -1,4 +1,4 @@ -/* $Id: debug.cc,v 1.6 1996/03/27 18:16:28 wessels Exp $ */ +/* $Id: debug.cc,v 1.7 1996/03/27 18:50:22 wessels Exp $ */ #include "squid.h" @@ -6,7 +6,6 @@ char *_db_file = __FILE__; int _db_line = 0; int syslog_enable = 0; -int stderr_enable = 0; FILE *debug_log = NULL; static char *debug_log_file = NULL; static time_t last_cached_curtime = 0; @@ -69,16 +68,12 @@ void _db_print(va_alist) syslog(LOG_ERR, tmpbuf); } #endif /* HAVE_SYSLOG */ + /* write to log file */ vfprintf(debug_log, f, args); if (unbuffered_logs) fflush(debug_log); - /* if requested, dump it to stderr also */ - if (stderr_enable) { - vfprintf(stderr, f, args); - fflush(stderr); - } va_end(args); } @@ -106,8 +101,27 @@ static void debugArg(arg) debugLevels[i] = l; } -void _db_init(prefix, logfile) - char *prefix; +static void debugOpenLog(logfile) + char *logfile; +{ + if (logfile == NULL) { + debug_log = stderr; + return; + } + if (debug_log_file) + free(debug_log_file); + debug_log_file = xstrdup(logfile); /* keep a static copy */ + debug_log = fopen(logfile, "a+"); + if (!debug_log) { + fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile); + perror(logfile); + fprintf(stderr, " messages will be sent to 'stderr'.\n"); + fflush(stderr); + debug_log = stderr; + } +} + +void _db_init(logfile) char *logfile; { int i; @@ -124,28 +138,8 @@ void _db_init(prefix, logfile) } xfree(p); } - /* open error logging file */ - if (logfile != NULL) { - if (debug_log_file) - free(debug_log_file); - debug_log_file = strdup(logfile); /* keep a static copy */ - debug_log = fopen(logfile, "a+"); - if (!debug_log) { - fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile); - perror(logfile); - fprintf(stderr, " messages will be sent to 'stderr'.\n"); - fflush(stderr); - debug_log = stderr; - /* avoid reduntancy */ - stderr_enable = 0; - } - } else { - fprintf(stderr, "WARNING: No log file specified?\n"); - fprintf(stderr, " messages will be sent to 'stderr'.\n"); - fflush(stderr); - debug_log = stderr; - stderr_enable = 0; - } + + debugOpenLog(logfile); #if HAVE_SYSLOG if (syslog_enable) @@ -154,7 +148,6 @@ void _db_init(prefix, logfile) } -/* gack! would be nice to use _db_init() instead */ void _db_rotate_log() { int i; @@ -178,16 +171,8 @@ void _db_rotate_log() } /* Close and reopen the log. It may have been renamed "manually" * before HUP'ing us. */ - fclose(debug_log); - debug_log = fopen(debug_log_file, "a+"); - if (debug_log == NULL) { - fprintf(stderr, "WARNING: Cannot write log file: %s\n", - debug_log_file); - perror(debug_log_file); - fprintf(stderr, " messages will be sent to 'stderr'.\n"); - fflush(stderr); - debug_log = stderr; - /* avoid redundancy */ - stderr_enable = 0; + if (debug_log != stderr) { + fclose(debug_log); + debugOpenLog(debug_log_file); } } diff --git a/src/main.cc b/src/main.cc index 545b9e9f6e..a32f9479c0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,4 @@ -/* $Id: main.cc,v 1.10 1996/03/27 18:15:49 wessels Exp $ */ +/* $Id: main.cc,v 1.11 1996/03/27 18:50:23 wessels Exp $ */ #include "squid.h" @@ -35,6 +35,26 @@ static int asciiPortNumOverride = 0; static int binaryPortNumOverride = 0; static int udpPortNumOverride = 0; + +static void usage() +{ + fprintf(stderr, "\ +Usage: cached [-Rsehvz] [-f config-file] [-[apu] port]\n\ + -h Print help message.\n\ + -s Enable logging to syslog.\n\ + -v Print version.\n\ + -z Zap disk storage -- deletes all objects in disk cache.\n\ + -C Do not catch fatal signals.\n\ + -D Disable initial DNS tests.\n\ + -R Do not set REUSEADDR on port.\n\ + -f file Use given config-file instead of\n\ + $HARVEST_HOME/lib/cached.conf.\n\ + -a port Specify ASCII port number (default: %d).\n\ + -u port Specify UDP port number (default: %d).\n", + CACHE_HTTP_PORT, CACHE_ICP_PORT); + exit(1); +} + int main(argc, argv) int argc; char **argv; @@ -48,11 +68,6 @@ int main(argc, argv) int n; /* # of GC'd objects */ time_t last_maintain = 0; -#ifdef WRITE_PID_FILE - FILE *pid_fp = NULL; - static char pidfn[MAXPATHLEN]; -#endif - cached_starttime = cached_curtime = time((time_t *) NULL); failure_notify = fatal_dump; @@ -101,14 +116,6 @@ int main(argc, argv) /*init comm module */ comm_init(); -#ifdef DAEMON - if (daemonize()) { - fprintf(stderr, "Error: couldn't create daemon process\n"); - exit(0); - } - /* signal( SIGHUP, restart ); *//* restart if/when proc dies */ -#endif /* DAEMON */ - /* we have to init fdstat here. */ fdstat_init(PREOPEN_FD); fdstat_open(0, LOG); @@ -126,14 +133,12 @@ int main(argc, argv) } /* enable syslog by default */ - syslog_enable = 1; - /* disable stderr debug printout by default */ - stderr_enable = 0; + syslog_enable = 0; /* preinit for debug module */ debug_log = stderr; hash_init(0); - while ((c = getopt(argc, argv, "vCDRVseif:a:p:u:d:m:zh?")) != -1) + while ((c = getopt(argc, argv, "vCDRVsif:a:p:u:m:zh?")) != -1) switch (c) { case 'v': printf("Harvest Cache: Version %s\n", SQUID_VERSION); @@ -151,8 +156,6 @@ int main(argc, argv) case 's': syslog_enable = 0; break; - case 'e': - stderr_enable = 1; break; case 'R': do_reuse = 0; @@ -179,23 +182,7 @@ int main(argc, argv) case '?': case 'h': default: - printf("\ -Usage: cached [-Rsehvz] [-f config-file] [-[apu] port]\n\ - -e Print messages to stderr.\n\ - -h Print help message.\n\ - -s Disable syslog output.\n\ - -v Print version.\n\ - -z Zap disk storage -- deletes all objects in disk cache.\n\ - -C Do not catch fatal signals.\n\ - -D Disable initial DNS tests.\n\ - -R Do not set REUSEADDR on port.\n\ - -f file Use given config-file instead of\n\ - $HARVEST_HOME/lib/cached.conf.\n\ - -a port Specify ASCII port number (default: %d).\n\ - -u port Specify UDP port number (default: %d).\n", - CACHE_HTTP_PORT, CACHE_ICP_PORT); - - exit(1); + usage(); break; } @@ -223,7 +210,7 @@ Usage: cached [-Rsehvz] [-f config-file] [-[apu] port]\n\ if (udpPortNumOverride > 0) setUdpPortNum(udpPortNumOverride); - _db_init("cached", getCacheLogFile()); + _db_init(getCacheLogFile()); fdstat_open(fileno(debug_log), LOG); fd_note(fileno(debug_log), getCacheLogFile()); @@ -307,22 +294,7 @@ Usage: cached [-Rsehvz] [-f config-file] [-[apu] port]\n\ stat_init(&CacheInfo, getAccessLogFile()); storeInit(); stmemInit(); - -#ifdef WRITE_PID_FILE - /* Try to write the pid to cached.pid in the same directory as - * cached.conf */ - memset(pidfn, '\0', MAXPATHLEN); - strcpy(pidfn, config_file); - if ((s = strrchr(pidfn, '/')) != NULL) - strcpy(s, "/cached.pid"); - else - strcpy(pidfn, "/usr/local/harvest/lib/cached.pid"); - pid_fp = fopen(pidfn, "w"); - if (pid_fp != NULL) { - fprintf(pid_fp, "%d\n", (int) getpid()); - fclose(pid_fp); - } -#endif + writePidFile(); /* after this point we want to see the mallinfo() output */ do_mallinfo = 1; diff --git a/src/tools.cc b/src/tools.cc index e4a4f9e08b..9f50221d9c 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -1,5 +1,5 @@ -/* $Id: tools.cc,v 1.10 1996/03/27 18:15:55 wessels Exp $ */ +/* $Id: tools.cc,v 1.11 1996/03/27 18:50:24 wessels Exp $ */ #include "squid.h" @@ -333,3 +333,19 @@ void check_suid() } setuid(pwd->pw_uid); } + +void writePidFile() +{ + FILE *pid_fp = NULL; + char *f = NULL; + + if ((f = getPidFilename()) == NULL) + return; + if ((pid_fp = fopen(f, "w")) == NULL) { + debug(0,0,"WARNING: Could not write pid file\n"); + debug(0,0," %s: %s\n", f, xstrerror()); + return; + } + fprintf(pid_fp, "%d\n", (int) getpid()); + fclose(pid_fp); +}