]> Pileus Git - ~andy/fetchmail/blobdiff - daemon.c
Eliminate spurious protocol error messages.
[~andy/fetchmail] / daemon.c
index db6ee78b6c6e40d24a96569db7643aa228c3fbd6..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>
@@ -81,13 +82,47 @@ sigchld_handler (int sig)
     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
 daemonize (const char *logfile, void (*termhook)(int))
 /* detach from control TTY, become process group leader, catch SIGCHLD */
 {
   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 */
@@ -96,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
@@ -144,7 +199,11 @@ 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) {
     report(stderr, "fork (%)\n", strerror(errno));
     return(PS_IOERR);
@@ -196,11 +255,7 @@ 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);
 }
@@ -210,9 +265,14 @@ flag isafile(int fd)
 {
     struct stat stbuf;
 
-    if (fstat(fd, &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 | S_IFBLK))
+    else if (stbuf.st_mode & (S_IFREG))
        return(1);
     return(0);
 }