From: wessels <> Date: Fri, 13 Nov 1998 06:07:34 +0000 (+0000) Subject: Making squid more a of a true daemon process X-Git-Tag: SQUID_3_0_PRE1~2530 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=54f742e;p=thirdparty%2Fsquid.git Making squid more a of a true daemon process --- diff --git a/src/comm.cc b/src/comm.cc index 295f728046..3d306f5800 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -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); diff --git a/src/ipc.cc b/src/ipc.cc index 63cae273bd..77097746ef 100644 --- a/src/ipc.cc +++ b/src/ipc.cc @@ -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. diff --git a/src/main.cc b/src/main.cc index b0a6b6a74c..5454941c99 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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(); diff --git a/src/protos.h b/src/protos.h index d7a879ba30..2aba51afe4 100644 --- a/src/protos.h +++ b/src/protos.h @@ -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); diff --git a/src/squid.h b/src/squid.h index 3e7e1d3456..834ec8adc8 100644 --- a/src/squid.h +++ b/src/squid.h @@ -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 * @@ -71,8 +71,11 @@ #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 @@ -174,11 +177,6 @@ #if HAVE_GETOPT_H #include #endif -#if HAVE_ASSERT_H -#include -#else -#define assert(X) ((void)0) -#endif #if HAVE_DIRENT_H #include diff --git a/src/tools.cc b/src/tools.cc index 3d2e3f8744..a6eacea0e8 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -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(); +}