]> Pileus Git - ~andy/fetchmail/blobdiff - daemon.c
Eliminate spurious protocol error messages.
[~andy/fetchmail] / daemon.c
index e6e7b10bb4331970fee72fda226a2b1db895acb7..331ca767049cc7347425d0dad96140ff629da659 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <signal.h>
+#include <string.h>
 #include <sys/types.h>
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
@@ -56,6 +57,7 @@ RETSIGTYPE
 sigchld_handler (int sig)
 /* process SIGCHLD to obtain the exit code of the terminating process */
 {
+    extern volatile int lastsig;               /* last signal received */
     pid_t pid;
 
 #if    defined(HAVE_WAITPID)                           /* the POSIX way */
@@ -77,6 +79,39 @@ sigchld_handler (int sig)
 
     wait(&status);
 #endif
+    lastsig = SIGCHLD;
+}
+
+/* 
+ * This function is called by other parts of the program to
+ * setup the sigchld handler after a change to the signal context.
+ * This is done to improve robustness of the signal handling code.
+ */
+void deal_with_sigchld(void)
+{
+  RETSIGTYPE sigchld_handler(int);
+#ifdef HAVE_SIGACTION
+  struct sigaction sa_new;
+
+  memset (&sa_new, 0, sizeof sa_new);
+  sigemptyset (&sa_new.sa_mask);
+  /* sa_new.sa_handler = SIG_IGN;     pointless */
+
+  /* set up to catch child process termination signals */ 
+  sa_new.sa_handler = sigchld_handler;
+#ifdef SA_RESTART      /* SunOS 4.1 portability hack */
+  sa_new.sa_flags = SA_RESTART | SA_NOCLDSTOP;
+#endif
+  sigaction (SIGCHLD, &sa_new, NULL);
+#if defined(SIGPWR)
+  sigaction (SIGPWR, &sa_new, NULL);
+#endif
+#else /* HAVE_SIGACTION */
+  signal(SIGCHLD, sigchld_handler); 
+#if defined(SIGPWR)
+  signal(SIGPWR, sigchld_handler); 
+#endif
+#endif /* HAVE_SIGACTION */
 }
 
 int
@@ -85,7 +120,9 @@ daemonize (const char *logfile, void (*termhook)(int))
 {
   int fd;
   pid_t childpid;
-  RETSIGTYPE sigchld_handler(int);
+#ifdef HAVE_SIGACTION
+  struct sigaction sa_new;
+#endif /* HAVE_SIGACTION */
 
   /* if we are started by init (process 1) via /etc/inittab we needn't 
      bother to detach from our process group context */
@@ -94,14 +131,34 @@ daemonize (const char *logfile, void (*termhook)(int))
     goto nottyDetach;
 
   /* Ignore BSD terminal stop signals */
+#ifdef HAVE_SIGACTION
+  memset (&sa_new, 0, sizeof sa_new);
+  sigemptyset (&sa_new.sa_mask);
+  sa_new.sa_handler = SIG_IGN;
+#ifdef SA_RESTART      /* SunOS 4.1 portability hack */
+  sa_new.sa_flags = SA_RESTART;
+#endif
+#endif /* HAVE_SIGACTION */
 #ifdef         SIGTTOU
+#ifndef HAVE_SIGACTION
   signal(SIGTTOU, SIG_IGN);
+#else
+  sigaction (SIGTTOU, &sa_new, NULL);
+#endif /* HAVE_SIGACTION */
 #endif
 #ifdef SIGTTIN
+#ifndef HAVE_SIGACTION
   signal(SIGTTIN, SIG_IGN);
+#else
+  sigaction (SIGTTIN, &sa_new, NULL);
+#endif /* HAVE_SIGACTION */
 #endif
 #ifdef SIGTSTP
+#ifndef HAVE_SIGACTION
   signal(SIGTSTP, SIG_IGN);
+#else
+  sigaction (SIGTSTP, &sa_new, NULL);
+#endif /* HAVE_SIGACTION */
 #endif
 
   /* In case we were not started in the background, fork and let
@@ -109,7 +166,7 @@ daemonize (const char *logfile, void (*termhook)(int))
      group leader */
 
   if ((childpid = fork()) < 0) {
-    error(0, errno, "fork");
+    report(stderr, "fork (%s)\n", strerror(errno));
     return(PS_IOERR);
   }
   else if (childpid > 0) 
@@ -122,7 +179,7 @@ daemonize (const char *logfile, void (*termhook)(int))
 #if    defined(HAVE_SETSID)            /* POSIX */
   /* POSIX makes this soooo easy to do */
   if (setsid() < 0) {
-    error(0, errno, "setsid");
+    report(stderr, "setsid (%s)\n", strerror(errno));
     return(PS_IOERR);
   }
 #elif  defined(SIGTSTP)                /* BSD */
@@ -133,7 +190,7 @@ daemonize (const char *logfile, void (*termhook)(int))
   /* lose controlling tty */
   if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
     ioctl(fd, TIOCNOTTY, (char *) 0);
-    close(fd);
+    close(fd); /* not checking should be safe, there were no writes */
   }
 #else                                  /* SVR3 and older */
   /* change process group */
@@ -142,9 +199,13 @@ daemonize (const char *logfile, void (*termhook)(int))
 #endif
   
   /* lose controlling tty */
+#ifndef HAVE_SIGACTION
   signal(SIGHUP, SIG_IGN);
+#else
+  sigaction (SIGHUP, &sa_new, NULL);
+#endif /* HAVE_SIGACTION */
   if ((childpid = fork()) < 0) {
-    error(0, errno, "fork");
+    report(stderr, "fork (%)\n", strerror(errno));
     return(PS_IOERR);
   }
   else if (childpid > 0) {
@@ -163,12 +224,12 @@ nottyDetach:
   for (fd = 19;  fd >= 0;  fd--)
 #endif
   {
-    close(fd);
+    close(fd); /* not checking this should be safe, no writes */
   }
 
   /* Reopen stdin descriptor on /dev/null */
   if ((fd = open("/dev/null", O_RDWR)) < 0) {   /* stdin */
-    error(0, errno, "open: /dev/null");
+    report(stderr, "open: /dev/null (%s)\n", strerror(errno));
     return(PS_IOERR);
   }
 
@@ -176,11 +237,11 @@ nottyDetach:
     fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666);       /* stdout */
   else
     if (dup(fd) < 0) {                         /* stdout */
-      error(0, errno, "dup");
+      report(stderr, "dup (%s)\n", strerror(errno));
       return(PS_IOERR);
     }
   if (dup(fd) < 0) {                           /* stderr */
-    error(0, errno, "dup");
+    report(stderr, "dup (%s)\n", strerror(errno));
     return(PS_IOERR);
   }
 
@@ -194,13 +255,26 @@ nottyDetach:
   umask(022);
 #endif
 
-  /* set up to catch child process termination signals */ 
-  signal(SIGCHLD, sigchld_handler); 
-#if defined(SIGPWR)
-  signal(SIGPWR, sigchld_handler); 
-#endif
+  deal_with_sigchld();
 
   return(0);
 }
 
+flag isafile(int fd)
+/* is the given fd attached to a file? (used to control logging) */
+{
+    struct stat stbuf;
+
+    /*
+     * We'd like just to return 1 on (S_IFREG | S_IFBLK),
+     * but weirdly enough, Linux ptys seem to have S_IFBLK
+     * so this test would fail when run on an xterm.
+     */
+    if (isatty(fd) || fstat(fd, &stbuf))
+       return(0);
+    else if (stbuf.st_mode & (S_IFREG))
+       return(1);
+    return(0);
+}
+
 /* daemon.c ends here */