]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Making squid more a of a true daemon process
authorwessels <>
Fri, 13 Nov 1998 06:07:34 +0000 (06:07 +0000)
committerwessels <>
Fri, 13 Nov 1998 06:07:34 +0000 (06:07 +0000)
src/comm.cc
src/ipc.cc
src/main.cc
src/protos.h
src/squid.h
src/tools.cc

index 295f728046986f4259fe34e31f5bd2336dffd567..3d306f58009c571751a1f823434891ae16d92aa4 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: comm.cc,v 1.290 1998/10/13 23:33:33 wessels Exp $
+ * $Id: comm.cc,v 1.291 1998/11/12 23:07:34 wessels Exp $
  *
  * DEBUG: section 5     Socket Functions
  * AUTHOR: Harvest Derived
@@ -411,7 +411,8 @@ comm_connect_addr(int sock, const struct sockaddr_in *address)
        F->flags.called_connect = 1;
        Counter.syscalls.sock.connects++;
        x = connect(sock, (struct sockaddr *) address, sizeof(*address));
-       debug(5, 9) ("connect FD %d: %s\n", sock, xstrerror());
+       if (x < 0)
+           debug(5, 9) ("connect FD %d: %s\n", sock, xstrerror());
     } else {
        errlen = sizeof(err);
        x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
index 63cae273bda7c63944e906f3fd7d2e9adf0822e6..77097746ef2399030861eef832dcfd035fff66e2 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: ipc.cc,v 1.12 1998/11/12 06:28:12 wessels Exp $
+ * $Id: ipc.cc,v 1.13 1998/11/12 23:07:35 wessels Exp $
  *
  * DEBUG: section 54    Interprocess Communication
  * AUTHOR: Duane Wessels
@@ -218,7 +218,7 @@ ipcCreate(int type, const char *prog, char *const args[], const char *name, int
            debug(50, 0) ("ipcCreate: FD %d accept: %s\n", crfd, xstrerror());
            _exit(1);
        }
-       debug(54, 3) ("ipcCreate: accepted new FD %d\n", fd);
+       debug(54, 3) ("ipcCreate: CHILD accepted new FD %d\n", fd);
        close(crfd);
        cwfd = crfd = fd;
     } else if (type == IPC_UDP_SOCKET) {
@@ -244,10 +244,11 @@ ipcCreate(int type, const char *prog, char *const args[], const char *name, int
     snprintf(env_str, tmp_s, "SQUID_DEBUG=%s", Config.debugOptions);
     putenv(env_str);
 #endif
+    dup2(fileno(debug_log), 2);
+    if (fileno(debug_log) > 2)
+       fclose(debug_log);
     dup2(crfd, 0);
     dup2(cwfd, 1);
-    dup2(fileno(debug_log), 2);
-    fclose(debug_log);
     /*
      * Solaris pthreads seems to close FD 0 upon fork(), so don't close
      * this FD if its 0, 1, or 2.
index b0a6b6a74c9a04ba6c506abd03985d3339000079..5454941c9939da16fcf19f832a738ed1c17aaa4b 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: main.cc,v 1.276 1998/11/12 06:28:14 wessels Exp $
+ * $Id: main.cc,v 1.277 1998/11/12 23:07:36 wessels Exp $
  *
  * DEBUG: section 1     Startup and Main Loop
  * AUTHOR: Harvest Derived
@@ -551,11 +551,12 @@ main(int argc, char **argv)
     comm_init();
     comm_select_init();
 
-    /* we have to init fdstat here. */
-    fd_open(0, FD_LOG, "stdin");
-    fd_open(1, FD_LOG, "stdout");
-    fd_open(2, FD_LOG, "stderr");
-
+    if (opt_no_daemon) {
+       /* we have to init fdstat here. */
+       fd_open(0, FD_LOG, "stdin");
+       fd_open(1, FD_LOG, "stdout");
+       fd_open(2, FD_LOG, "stderr");
+    }
     mainInitialize();
 
     /* main loop */
@@ -652,32 +653,62 @@ watch_child(char *argv[])
     int status;
 #endif
     pid_t pid;
+    int i;
     if (*(argv[0]) == '(')
        return;
+    openlog(appname, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4);
+    if ((pid = fork()) < 0)
+       syslog(LOG_ALERT, "fork failed: %s", xstrerror());
+    else if (pid > 0)
+       exit(0);
+    if (setsid() < 0)
+       syslog(LOG_ALERT, "setsid failed: %s", xstrerror());
+#ifdef TIOCNOTTY
+    if ((i = open("/dev/tty", O_RDWR)) >= 0) {
+       ioctl(i, TIOCNOTTY, NULL);
+       close(i);
+    }
+#endif
+    for (i = 0; i < Squid_MaxFD; i++)
+       close(i);
+    umask(0);
     for (;;) {
-       if (fork() == 0) {
+       if ((pid = fork()) == 0) {
            /* child */
            prog = xstrdup(argv[0]);
            argv[0] = xstrdup("(squid)");
            execvp(prog, argv);
-           fatal("execvp failed");
+           syslog(LOG_ALERT, "execvp failed: %s", xstrerror());
        }
-       /* parent */ time(&start);
-       do {
-           squid_signal(SIGINT, SIG_IGN, SA_RESTART);
+       /* parent */
+       syslog(LOG_NOTICE, "Squid Parent: child process %d started", pid);
+       time(&start);
+       squid_signal(SIGINT, SIG_IGN, SA_RESTART);
 #ifdef _SQUID_NEXT_
-           pid = wait3(&status, 0, NULL);
+       pid = wait3(&status, 0, NULL);
 #else
-           pid = waitpid(-1, &status, 0);
+       pid = waitpid(-1, &status, 0);
 #endif
-       } while (pid > 0);
        time(&stop);
+       if (WIFEXITED(status)) {
+           syslog(LOG_NOTICE,
+               "Squid Parent: child process %d exited with status %d",
+               pid, WEXITSTATUS(status));
+       } else if (WIFSIGNALED(status)) {
+           syslog(LOG_NOTICE,
+               "Squid Parent: child process %d exited due to signal %d",
+               pid, WTERMSIG(status));
+       } else {
+           syslog(LOG_NOTICE, "Squid Parent: child process %d exited", pid);
+       }
        if (stop - start < 10)
            failcount++;
        else
            failcount = 0;
-       if (failcount == 5)
+       if (failcount == 5) {
+           syslog(LOG_ALERT, "Exiting due to repeated, frequent failures");
            exit(1);
+       }
        if (WIFEXITED(status))
            if (WEXITSTATUS(status) == 0)
                exit(0);
@@ -728,9 +759,11 @@ SquidShutdown(void *unused)
 #endif
     memClean();
 #if !XMALLOC_TRACE
-    file_close(0);
-    file_close(1);
-    file_close(2);
+    if (opt_no_daemon) {
+       file_close(0);
+       file_close(1);
+       file_close(2);
+    }
 #endif
     fdDumpOpen();
     fdFreeMemory();
index d7a879ba301ec92c94294ad1d12082946c232a93..2aba51afe4b74c3c4fb74b480d0c2d3fb84a190b 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: protos.h,v 1.285 1998/11/12 06:28:20 wessels Exp $
+ * $Id: protos.h,v 1.286 1998/11/12 23:07:37 wessels Exp $
  *
  *
  * SQUID Internet Object Cache  http://squid.nlanr.net/Squid/
@@ -1016,6 +1016,7 @@ extern double gb_to_double(const gb_t *);
 extern const char *gb_to_str(const gb_t *);
 extern void gb_flush(gb_t *);  /* internal, do not use this */
 extern int stringHasWhitespace(const char *);
+extern void xassert(const char *, const char *, int);
 
 #if USE_HTCP
 extern void htcpInit(void);
index 3e7e1d3456d04776112f4ac89ff6cfd746ef3adb..834ec8adc8c7b13163d5ff5562cdf77903572e7b 100644 (file)
@@ -2,7 +2,7 @@
 
 
 /*
- * $Id: squid.h,v 1.178 1998/11/12 06:28:24 wessels Exp $
+ * $Id: squid.h,v 1.179 1998/11/12 23:07:38 wessels Exp $
  *
  * AUTHOR: Duane Wessels
  *
 #endif
 
 #if PURIFY
-/* disable assert() under purify */
-#define NODEBUG
+#define assert(EX) ((void)0)
+#elif __STDC__
+#define assert(EX)  ((EX)?((void)0):xassert( # EX , __FILE__, __LINE__))
+#else
+#define assert(EX)  ((EX)?((void)0):xassert("EX", __FILE__, __LINE__))
 #endif
 
 #if HAVE_UNISTD_H
 #if HAVE_GETOPT_H
 #include <getopt.h>
 #endif
-#if HAVE_ASSERT_H
-#include <assert.h>
-#else
-#define assert(X) ((void)0)
-#endif
 
 #if HAVE_DIRENT_H
 #include <dirent.h>
index 3d2e3f87444009df0920a897f8e51a4075af461b..a6eacea0e8357143fd983c8b1072373a1f585ab1 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: tools.cc,v 1.170 1998/10/14 21:12:03 wessels Exp $
+ * $Id: tools.cc,v 1.171 1998/11/12 23:07:39 wessels Exp $
  *
  * DEBUG: section 21    Misc Functions
  * AUTHOR: Harvest Derived
@@ -322,7 +322,7 @@ fatal(const char *message)
     if (!store_rebuilding)
        storeDirWriteCleanLogs(0);
     fatal_common(message);
-    exit(1);
+    exit(shutting_down ? 0 : 1);
 }
 
 /* printf-style interface for fatal */
@@ -836,3 +836,11 @@ stringHasWhitespace(const char *s)
 {
     return (strcspn(s, w_space) != strlen(s));
 }
+
+void
+xassert(const char *msg, const char *file, int line)
+{
+    debug(0, 0) ("assertion failed: %s:%d: \"%s\"\n", file, line, msg);
+    if (!shutting_down)
+        abort();
+}