dma: prevent races from sharing fd between children
authorSimon Schubert <simon.schubert@epfl.ch>
Thu, 9 Jul 2009 21:24:35 +0000 (23:24 +0200)
committerSimon Schubert <corecode@dragonflybsd.org>
Thu, 16 Jul 2009 14:13:06 +0000 (16:13 +0200)
On fork, fds are shared between children.  If two processes work on
different recipients, but on the same queue file, they might get
confused when the fd (and thus the offset) is shared.  Prevent this by
re-opening the queue file after fork.

Reported-by: Daniel Roethlisberger <daniel@roe.ch>
libexec/dma/dma.c

index 4633b0c..063c6b2 100644 (file)
@@ -484,6 +484,7 @@ go_background(struct queue *queue)
 {
        struct sigaction sa;
        struct qitem *it;
+       FILE *newqf;
        pid_t pid;
 
        if (daemonize && daemon(0, 0) != 0) {
@@ -515,6 +516,18 @@ go_background(struct queue *queue)
                         *
                         * return and deliver mail
                         */
+                       /*
+                        * We have to prevent sharing of fds between children, so
+                        * we have to re-open the queue file.
+                        */
+                       newqf = fopen(it->queuefn, "r");
+                       if (newqf == NULL) {
+                               syslog(LOG_ERR, "can not re-open queue file `%s': %m",
+                                      it->queuefn);
+                               exit(1);
+                       }
+                       fclose(it->queuef);
+                       it->queuef = newqf;
                        return (it);
 
                default: