]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Redo r12887 (bug 2066 fix) which introduced several related bugs:
authorAlex Rousskov <rousskov@measurement-factory.com>
Wed, 5 Jun 2013 15:38:09 +0000 (09:38 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Wed, 5 Jun 2013 15:38:09 +0000 (09:38 -0600)
  - fatal() if chroot is given
  - no chdir to / after chroot if coredump_dir is given
  - wrong detection of "none" coredump_dir
  - chdir to uninitialized pathbuf if no chroot is given

This emergency fix helps Squid start better but may need more testing/work.

src/main.cc

index 86f2d87fe768cf46d752c8a956f939f3a50f837b..ccbc6473a1f413d5880382379e616dd8fd6c7317 100644 (file)
@@ -951,34 +951,42 @@ setEffectiveUser(void)
     }
 }
 
+/// changes working directory, providing error reporting
+static bool
+mainChangeDir(const char *dir)
+{
+    if (chdir(dir) == 0)
+        return true;
+
+    debugs(50, DBG_CRITICAL, "cannot change current directory to " << dir <<
+           ": " << xstrerror());
+    return false;
+}
+
 /// set the working directory.
 static void
 mainSetCwd(void)
 {
-    char pathbuf[MAXPATHLEN];
     static bool chrooted = false;
-
     if (Config.chroot_dir && !chrooted) {
         chrooted = true;
 
-        if (chroot(Config.chroot_dir) != 0);
-        fatal("failed to chroot");
+        if (chroot(Config.chroot_dir) != 0)
+            fatalf("chroot to %s failed: %s", Config.chroot_dir, xstrerror());
 
-        strncpy(pathbuf, "/", sizeof(pathbuf)-1);
+        if (!mainChangeDir("/"))
+            fatalf("chdir to / after chroot to %s failed", Config.chroot_dir);
     }
 
-    if (Config.coredump_dir && strcmp("none", Config.coredump_dir) == 0) {
-        strncpy(pathbuf, Config.coredump_dir, sizeof(pathbuf)-1);
-    }
-
-    if (chdir(pathbuf) == 0) {
-        debugs(0, DBG_IMPORTANT, "Set Current Directory to " << Config.coredump_dir);
-        return;
-    } else {
-        debugs(50, DBG_CRITICAL, "chdir: " << pathbuf << ": " << xstrerror());
+    if (Config.coredump_dir && strcmp("none", Config.coredump_dir) != 0) {
+        if (mainChangeDir(Config.coredump_dir)) {
+            debugs(0, DBG_IMPORTANT, "Set Current Directory to " << Config.coredump_dir);
+            return;
+        }
     }
 
     /* If we don't have coredump_dir or couldn't cd there, report current dir */
+    char pathbuf[MAXPATHLEN];
     if (getcwd(pathbuf, MAXPATHLEN)) {
         debugs(0, DBG_IMPORTANT, "Current Directory is " << pathbuf);
     } else {