]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
copied over v1.1 -z semantics
authorwessels <>
Tue, 6 Jan 1998 14:11:53 +0000 (14:11 +0000)
committerwessels <>
Tue, 6 Jan 1998 14:11:53 +0000 (14:11 +0000)
src/globals.h
src/main.cc
src/protos.h
src/store.cc
src/store_dir.cc

index f7ed13d97a39347a2a1e6bf7189e86c400162d96..d65dc4258107f379525ef4923afd5cfff10eb4bc 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: globals.h,v 1.23 1998/01/06 05:12:09 wessels Exp $
+ * $Id: globals.h,v 1.24 1998/01/06 07:11:53 wessels Exp $
  */
 
 extern FILE *debug_log;                /* NULL */
@@ -64,7 +64,7 @@ extern int opt_mem_pools;     /* 1 */
 extern int opt_reload_hit_only;        /* 0 */
 extern int opt_syslog_enable;  /* 0 */
 extern int opt_udp_hit_obj;    /* 0 */
-extern int opt_zap_disk_store; /* 0 */
+extern int opt_create_swap_dirs;       /* 0 */
 extern int syslog_enable;      /* 0 */
 extern int theInIcpConnection; /* -1 */
 extern int theOutIcpConnection;        /* -1 */
index 457aec3f853b3273ea286d8c7df0e9dfedcdb5ae..3ce3c357a4b8e353eb826fabb79cf2221e6893cf 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: main.cc,v 1.202 1998/01/05 20:49:11 wessels Exp $
+ * $Id: main.cc,v 1.203 1998/01/06 07:11:53 wessels Exp $
  *
  * DEBUG: section 1     Startup and Main Loop
  * AUTHOR: Harvest Derived
@@ -130,6 +130,8 @@ static void mainParseOptions(int, char **);
 static void sendSignal(void);
 static void serverConnectionsOpen(void);
 static void watch_child(char **);
+static void setEffectiveUser(void);
+
 static void
 usage(void)
 {
@@ -145,7 +147,7 @@ usage(void)
        "       -s        Enable logging to syslog.\n"
        "       -u port   Specify ICP port number (default: %d), disable with 0.\n"
        "       -v        Print version.\n"
-       "       -z        Zap disk storage -- deletes all objects in disk cache.\n"
+       "       -z        Create swap directories\n"
        "       -C        Do not catch fatal signals.\n"
        "       -D        Disable initial DNS tests.\n"
        "       -F        Foreground fast store rebuild.\n"
@@ -246,7 +248,7 @@ mainParseOptions(int argc, char *argv[])
            exit(0);
            /* NOTREACHED */
        case 'z':
-           opt_zap_disk_store = 1;
+           opt_create_swap_dirs = 1;
            break;
        case '?':
        default:
@@ -411,6 +413,19 @@ mainReconfigure(void)
     debug(1, 0) ("Ready to serve requests.\n");
 }
 
+static void 
+setEffectiveUser(void)
+{
+    leave_suid();              /* Run as non privilegied user */
+    if (geteuid() == 0) {
+       debug(0, 0) ("Squid is not safe to run as root!  If you must\n");
+       debug(0, 0) ("start Squid as root, then you must configure\n");
+       debug(0, 0) ("it to run as a non-priveledged user with the\n");
+       debug(0, 0) ("'cache_effective_user' option in the config file.\n");
+       fatal("Don't run Squid as root, set 'cache_effective_user'!");
+    }
+}
+
 static void
 mainInitialize(void)
 {
@@ -427,14 +442,7 @@ mainInitialize(void)
        ConfigFile = xstrdup(DefaultConfigFile);
     parseConfigFile(ConfigFile);
 
-    leave_suid();              /* Run as non privilegied user */
-    if (geteuid() == 0) {
-       debug(0, 0) ("Squid is not safe to run as root!  If you must\n");
-       debug(0, 0) ("start Squid as root, then you must configure\n");
-       debug(0, 0) ("it to run as a non-priveledged user with the\n");
-       debug(0, 0) ("'cache_effective_user' option in the config file.\n");
-       fatal("Don't run Squid as root, set 'cache_effective_user'!");
-    }
+    setEffectiveUser();
     assert(Config.Port.http);
     if (httpPortNumOverride != 1)
        Config.Port.http->i = (u_short) httpPortNumOverride;
@@ -566,6 +574,16 @@ main(int argc, char **argv)
        sendSignal();
        /* NOTREACHED */
     }
+    if (opt_create_swap_dirs) {
+        if (ConfigFile == NULL)
+            ConfigFile = xstrdup(DefaultConfigFile);
+        cbdataInit();
+        parseConfigFile(ConfigFile);
+        setEffectiveUser();
+        debug(0, 0)("Creating Swap Directories\n");
+        storeCreateSwapDirectories();
+        return 0;
+    }   
     if (!opt_no_daemon)
        watch_child(argv);
     setMaxFD();
index 80d363d37b2170140baf9bbb076cb167975386a9..abfefe63990e0849cd6d1e48557cceb9cf289fa9 100644 (file)
@@ -488,7 +488,6 @@ extern void storeAppendPrintf();
 extern char *storeSwapFullPath(int, char *);
 extern char *storeSwapSubSubDir(int, char *);
 extern int storeVerifySwapDirs(void);
-extern void storeCreateSwapSubDirs(int);
 extern const char *storeSwapPath(int);
 extern int storeDirMapBitTest(int fn);
 extern void storeDirMapBitSet(int fn);
@@ -504,6 +503,8 @@ extern void storeDirSwapLog(const StoreEntry *);
 extern int storeDirNumber(int fileno);
 extern void storeDirUpdateSwapSize(int fn, size_t size, int sign);
 extern int storeDirProperFileno(int dirn, int fn);
+extern void storeCreateSwapDirectories(void);
+extern int storeVerifyCacheDirs(void);
 
 extern const char *getMyHostname(void);
 extern void safeunlink(const char *path, int quiet);
index d5fccb1174a7f9a7fc44a4dec56c8e729c354919..47d16cfb3238d1eb55ab6fb00925894f91608620 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: store.cc,v 1.363 1998/01/06 00:42:12 wessels Exp $
+ * $Id: store.cc,v 1.364 1998/01/06 07:11:55 wessels Exp $
  *
  * DEBUG: section 20    Storeage Manager
  * AUTHOR: Harvest Derived
@@ -1899,12 +1899,16 @@ storeInit(void)
        storelog_fd = file_open(fname, O_WRONLY | O_CREAT, NULL, NULL);
     if (storelog_fd < 0)
        debug(20, 1) ("Store logging disabled\n");
-    storeVerifySwapDirs();
+    if (storeVerifyCacheDirs() < 0) {
+        xstrncpy(tmp_error_buf,
+            "\tFailed to verify one of the swap directories, Check cache.log\n"
+            "\tfor details.  Run 'squid -z' to create swap directories\n"
+            "\tif needed, or if running Squid for the first time.",
+            ERROR_BUF_SZ); 
+        fatal(tmp_error_buf);
+    }   
     storeDirOpenSwapLogs();
-    if (!opt_zap_disk_store)
-       storeStartRebuildFromDisk();
-    else
-       store_rebuilding = 0;
+    storeStartRebuildFromDisk();
     all_list.head = all_list.tail = NULL;
     inmem_list.head = inmem_list.tail = NULL;
 }
index 4a955fd6148afcb48ebaa94ef560e7c448cd634f..5d41dde4467bb5ea06061afa3cb67af5d5afa6ed 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: store_dir.cc,v 1.38 1997/11/14 04:55:09 wessels Exp $
+ * $Id: store_dir.cc,v 1.39 1998/01/06 07:11:56 wessels Exp $
  *
  * DEBUG: section 47    Store Directory Routines
  * AUTHOR: Duane Wessels
 #define DefaultLevelOneDirs     16
 #define DefaultLevelTwoDirs     256
 
+static char *storeSwapSubDir(int dirn, int subdirn);
+static int storeMostFreeSwapDir(void);
+static int storeVerifyDirectory(const char *path);
+static void storeCreateDirectory(const char *path, int lvl);
+static void storeCreateSwapSubDirs(int j);
+
 /* return full name to swapfile */
 char *
 storeSwapFullPath(int fn, char *fullpath)
@@ -54,7 +60,20 @@ storeSwapFullPath(int fn, char *fullpath)
     return fullpath;
 }
 
-/* return full name to swapfile */
+static char *
+storeSwapSubDir(int dirn, int subdirn)
+{
+    LOCAL_ARRAY(char, fullfilename, SQUID_MAXPATHLEN);
+    SwapDir *SD;
+    assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
+    SD = &Config.cacheSwap.swapDirs[dirn];
+    assert(0 <= subdirn && subdirn < SD->l1);
+    snprintf(fullfilename, SQUID_MAXPATHLEN, "%s/%02X",
+       Config.cacheSwap.swapDirs[dirn].path,
+       subdirn);
+    return fullfilename;
+}
+
 char *
 storeSwapSubSubDir(int fn, char *fullpath)
 {
@@ -71,49 +90,77 @@ storeSwapSubSubDir(int fn, char *fullpath)
     return fullpath;
 }
 
+static void
+storeCreateDirectory(const char *path, int lvl)
+{
+    struct stat st;
+    if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+       debug(20, lvl) ("%s exists\n", path);
+    } else if (mkdir(path, 0755) == 0) {
+       debug(20, lvl) ("%s created\n", path);
+    } else if (errno == EEXIST) {
+       debug(20, lvl) ("%s exists\n", path);
+    } else {
+       snprintf(tmp_error_buf, ERROR_BUF_SZ,
+           "Failed to make swap directory %s: %s",
+           path, xstrerror());
+       fatal(tmp_error_buf);
+    }
+}
+
 static int
-storeVerifyOrCreateDir(const char *path)
+storeVerifyDirectory(const char *path)
 {
     struct stat sb;
-    if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
-       debug(47, 3) ("%s exists\n", path);
-       return 0;
+    if (stat(path, &sb) < 0) {
+       debug(20, 0) ("%s: %s\n", path, xstrerror());
+       return -1;
     }
-    safeunlink(path, 1);
-    if (mkdir(path, 0777) < 0) {
-       if (errno != EEXIST) {
-           snprintf(tmp_error_buf, ERROR_BUF_SZ,
-               "Failed to create swap directory %s: %s",
-               path,
-               xstrerror());
-           fatal(tmp_error_buf);
-       }
+    if (S_ISDIR(sb.st_mode) == 0) {
+       debug(20, 0) ("%s is not a directory\n", path);
+       return -1;
     }
-    debug(47, 1) ("Created directory %s\n", path);
-    if (stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) {
-       snprintf(tmp_error_buf, ERROR_BUF_SZ,
-           "Failed to create directory %s: %s", path, xstrerror());
-       fatal(tmp_error_buf);
-    }
-    return 1;
+    return 0;
 }
 
+/*
+ * This function is called by storeInit().  If this returns < 0,
+ * then Squid exits, complains about swap directories not
+ * existing, and instructs the admin to run 'squid -z'
+ */
 int
-storeVerifySwapDirs(void)
+storeVerifyCacheDirs(void)
+{
+    int i;
+    int j;
+    const char *path;
+    for (i = 0; i < Config.cacheSwap.n_configured; i++) {
+       path = Config.cacheSwap.swapDirs[i].path;
+       if (storeVerifyDirectory(path) < 0)
+           return -1;
+       for (j = 0; j < Config.cacheSwap.swapDirs[i].l1; j++) {
+           path = storeSwapSubDir(i, j);
+           if (storeVerifyDirectory(path) < 0)
+               return -1;
+       }
+    }
+    return 0;
+}
+
+void
+storeCreateSwapDirectories(void)
 {
     int i;
     const char *path = NULL;
-    int directory_created = 0;
     for (i = 0; i < Config.cacheSwap.n_configured; i++) {
        path = Config.cacheSwap.swapDirs[i].path;
-       debug(47, 3) ("storeVerifySwapDirs: Creating swap space in %s\n", path);
-       storeVerifyOrCreateDir(path);
+       debug(47, 3) ("Creating swap space in %s\n", path);
+       storeCreateDirectory(path, 0);
        storeCreateSwapSubDirs(i);
     }
-    return directory_created;
 }
 
-void
+static void
 storeCreateSwapSubDirs(int j)
 {
     int i, k;
@@ -121,17 +168,16 @@ storeCreateSwapSubDirs(int j)
     LOCAL_ARRAY(char, name, MAXPATHLEN);
     for (i = 0; i < SD->l1; i++) {
        snprintf(name, MAXPATHLEN, "%s/%02X", SD->path, i);
-       if (storeVerifyOrCreateDir(name) == 0)
-           continue;
+       storeCreateDirectory(name, 0);
        debug(47, 1) ("Making directories in %s\n", name);
        for (k = 0; k < SD->l2; k++) {
            snprintf(name, MAXPATHLEN, "%s/%02X/%02X", SD->path, i, k);
-           storeVerifyOrCreateDir(name);
+           storeCreateDirectory(name, 2);
        }
     }
 }
 
-int
+static int
 storeMostFreeSwapDir(void)
 {
     double least_used = 1.0;