[exim-dev] [patch] please consider closing all fds on daemon…

Top Page
Delete this message
Reply to this message
Author: Marc Haber
Date:  
To: exim-dev
Old-Topics: [exim-dev] Changes in the daemon startup code, fd's not properly closed?
Subject: [exim-dev] [patch] please consider closing all fds on daemon startup (was: Changes in the daemon startup code, fd's not properly closed?)
On Tue, Mar 01, 2005 at 11:19:26PM +0100, Marc Haber wrote:
> After reconfiguring a Debian package, the daemon is restarted. When
> that restart is happening, the Debconf frontend is still running which
> does funky things with the file descriptors.
>
> We now have the situation that the reconfiguration script never exits
> after restarting the daemon. The Debconf frontend stays around,
> waiting to read from a file descriptor associated with a pipe that
> seems to be connected to the newly started exim daemon.


I have now found the solution. Debian patches daemon.c so that all fds
from 0 to 254 are closed on daemon startup. This has, presumably, been
done to prevent the debconf issue mentioned above from happening.

The patch didn't apply to 4.50 and was rejected. And for some reason
our toolchain decided to hide the error message in a status directory
instead of aborting the build process. I have filed a bug against our
toolchain, and will add a step to my build process to look for an
error message.

I would like to suggest applying the attached patch to the exim sources.

Greetings
Marc

-- 
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Mannheim, Germany  |  lose things."    Winona Ryder | Fon: *49 621 72739834
Nordisch by Nature |  How to make an American Quilt | Fax: *49 621 72739835

#! /bin/sh -e
## 10_daemon_close_fds.dpatch by Steve Haslam <araqnid@???>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Close FDs 0-254 when going into background

if [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch) patch -f --no-backup-if-mismatch --dry-run -p0 < $0 && patch -f --no-backup-if-mismatch -p0 < $0;;
    -unpatch) patch -f --no-backup-if-mismatch -R -p0 < $0;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1;;
esac
exit 0


@DPATCH@
diff -urNad ./src/daemon.c /tmp/dpep-work.hKTslA/trunk/src/daemon.c
--- ./src/daemon.c    2005-02-17 14:49:11.000000000 +0000
+++ /tmp/dpep-work.hKTslA/trunk/src/daemon.c    2005-03-02 07:29:15.120314386 +0000
@@ -44,7 +44,13 @@


static BOOL write_pid = TRUE;

-
+static void close_fds(void)
+{
+  int fd;
+  for (fd = 0; fd < 255; fd++) {
+    close(fd);
+  }
+}


 /*************************************************
 *             SIGHUP Handler                     *
@@ -1208,9 +1214,7 @@
   {
   log_close_all();    /* Just in case anything was logged earlier */
   search_tidyup();    /* Just in case any were used in reading the config. */
-  close(0);           /* Get rid of stdin/stdout/stderr */
-  close(1);
-  close(2);
+  close_fds();          /* Get rid of all fds */
   exim_nullstd();     /* Connect stdin/stdout/stderr to /dev/null */
   log_stderr = NULL;  /* So no attempt to copy paniclog output */