chiark / gitweb /
Memory management for hands-off reader
[disorder] / lib / event.c
index 56278f6882188d8a6fb0542142bc3c6547ed59c8..64f1bdb5b481a77007df9494cbda698fbdf309d9 100644 (file)
@@ -1,27 +1,25 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2007 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /** @file lib/event.c
  * @brief DisOrder event loop
  */
 
-#include <config.h>
+#include "common.h"
 
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <assert.h>
 #include <signal.h>
 #include <errno.h>
-#include <string.h>
-#include <limits.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <sys/un.h>
-#include <stdio.h>
 #include "event.h"
 #include "mem.h"
 #include "log.h"
@@ -47,6 +41,8 @@
 #include "printf.h"
 #include "sink.h"
 #include "vector.h"
+#include "timeval.h"
+#include "heap.h"
 
 /** @brief A timeout */
 struct timeout {
@@ -54,9 +50,18 @@ struct timeout {
   struct timeval when;
   ev_timeout_callback *callback;
   void *u;
-  int resolve;
+  int active;
 };
 
+/** @brief Comparison function for timeouts */
+static int timeout_lt(const struct timeout *a,
+                     const struct timeout *b) {
+  return tvlt(&a->when, &b->when);
+}
+
+HEAP_TYPE(timeout_heap, struct timeout *, timeout_lt);
+HEAP_DEFINE(timeout_heap, struct timeout *, timeout_lt);
+
 /** @brief A file descriptor in one mode */
 struct fd {
   int fd;
@@ -106,11 +111,8 @@ struct ev_source {
   /** @brief File descriptors, per mode */
   struct fdmode mode[ev_nmodes];
 
-  /** @brief Sorted linked list of timeouts
-   *
-   * We could use @ref HEAP_TYPE now, but there aren't many timeouts.
-   */
-  struct timeout *timeouts;
+  /** @brief Heap of timeouts */
+  struct timeout_heap timeouts[1];
 
   /** @brief Array of handled signals */
   struct signal signals[NSIG];
@@ -146,27 +148,6 @@ static const char *modenames[] = { "read", "write", "except" };
 
 /* utilities ******************************************************************/
 
-/** @brief Great-than comparison for timevals
- *
- * Ought to be in @file lib/timeval.h
- */
-static inline int gt(const struct timeval *a, const struct timeval *b) {
-  if(a->tv_sec > b->tv_sec)
-    return 1;
-  if(a->tv_sec == b->tv_sec
-     && a->tv_usec > b->tv_usec)
-    return 1;
-  return 0;
-}
-
-/** @brief Greater-than-or-equal comparison for timevals
- *
- * Ought to be in @file lib/timeval.h
- */
-static inline int ge(const struct timeval *a, const struct timeval *b) {
-  return !gt(b, a);
-}
-
 /* creation *******************************************************************/
 
 /** @brief Create a new event loop */
@@ -179,6 +160,7 @@ ev_source *ev_new(void) {
     FD_ZERO(&ev->mode[n].enabled);
   ev->sigpipe[0] = ev->sigpipe[1] = -1;
   sigemptyset(&ev->sigmask);
+  timeout_heap_init(ev->timeouts);
   return ev;
 }
 
@@ -194,7 +176,7 @@ int ev_run(ev_source *ev) {
     int n, mode;
     int ret;
     int maxfd;
-    struct timeout *t, **tt;
+    struct timeout *timeouts, *t, **tt;
     struct stat sb;
 
     xgettimeofday(&now, 0);
@@ -202,10 +184,24 @@ int ev_run(ev_source *ev) {
      * while we're handling them (otherwise we'd have to break out of infinite
      * loops, preferrably without starving better-behaved subsystems).  Hence
      * the slightly complicated two-phase approach here. */
-    for(t = ev->timeouts;
-       t && ge(&now, &t->when);
-       t = t->next) {
-      t->resolve = 1;
+    /* First we read those timeouts that have triggered out of the heap.  We
+     * keep them in the same order they came out of the heap in. */
+    tt = &timeouts;
+    while(timeout_heap_count(ev->timeouts)
+         && tvle(&timeout_heap_first(ev->timeouts)->when, &now)) {
+      /* This timeout has reached its trigger time; provided it has not been
+       * cancelled we add it to the timeouts list. */
+      t = timeout_heap_remove(ev->timeouts);
+      if(t->active) {
+       *tt = t;
+       tt = &t->next;
+      }
+    }
+    *tt = 0;
+    /* Now we can run the callbacks for those timeouts.  They might add further
+     * timeouts that are already in the past but they won't trigger until the
+     * next time round the event loop. */
+    for(t = timeouts; t; t = t->next) {
       D(("calling timeout for %ld.%ld callback %p %p",
         (long)t->when.tv_sec, (long)t->when.tv_usec,
         (void *)t->callback, t->u));
@@ -213,13 +209,6 @@ int ev_run(ev_source *ev) {
       if(ret)
        return ret;
     }
-    tt = &ev->timeouts;
-    while((t = *tt)) {
-      if(t->resolve)
-       *tt = t->next;
-      else
-       tt = &t->next;
-    }
     maxfd = 0;
     for(mode = 0; mode < ev_nmodes; ++mode) {
       ev->mode[mode].tripped = ev->mode[mode].enabled;
@@ -228,10 +217,11 @@ int ev_run(ev_source *ev) {
     }
     xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0);
     do {
-      if(ev->timeouts) {
+      if(timeout_heap_count(ev->timeouts)) {
+       t = timeout_heap_first(ev->timeouts);
        xgettimeofday(&now, 0);
-       delta.tv_sec = ev->timeouts->when.tv_sec - now.tv_sec;
-       delta.tv_usec = ev->timeouts->when.tv_usec - now.tv_usec;
+       delta.tv_sec = t->when.tv_sec - now.tv_sec;
+       delta.tv_usec = t->when.tv_usec - now.tv_usec;
        if(delta.tv_usec < 0) {
          delta.tv_usec += 1000000;
          --delta.tv_sec;
@@ -253,7 +243,7 @@ int ev_run(ev_source *ev) {
     } while(n < 0 && errno == EINTR);
     xsigprocmask(SIG_BLOCK, &ev->sigmask, 0);
     if(n < 0) {
-      error(errno, "error calling select");
+      disorder_error(errno, "error calling select");
       if(errno == EBADF) {
        /* If there's a bad FD in the mix then check them all and log what we
         * find, to ease debugging */
@@ -263,13 +253,13 @@ int ev_run(ev_source *ev) {
 
            if(FD_ISSET(fd, &ev->mode[mode].enabled)
               && fstat(fd, &sb) < 0)
-             error(errno, "mode %s fstat %d (%s)",
-                   modenames[mode], fd, ev->mode[mode].fds[n].what);
+             disorder_error(errno, "mode %s fstat %d (%s)",
+                            modenames[mode], fd, ev->mode[mode].fds[n].what);
          }
          for(n = 0; n <= maxfd; ++n)
            if(FD_ISSET(n, &ev->mode[mode].enabled)
               && fstat(n, &sb) < 0)
-             error(errno, "mode %s fstat %d", modenames[mode], n);
+             disorder_error(errno, "mode %s fstat %d", modenames[mode], n);
        }
       }
       return -1;
@@ -321,6 +311,8 @@ int ev_fd(ev_source *ev,
 
   D(("registering %s fd %d callback %p %p", modenames[mode], fd,
      (void *)callback, u));
+  if(fd >= FD_SETSIZE)
+    return -1;
   assert(mode < ev_nmodes);
   if(ev->mode[mode].nfds >= ev->mode[mode].fdslots) {
     ev->mode[mode].fdslots = (ev->mode[mode].fdslots
@@ -455,7 +447,7 @@ void ev_report(ev_source *ev) {
 
 /** @brief Register a timeout
  * @param ev Event source
- * @param handle Where to store timeout handle, or @c NULL
+ * @param handlep Where to store timeout handle, or @c NULL
  * @param when Earliest time to call @p callback, or @c NULL
  * @param callback Function to call at or after @p when
  * @param u Passed to @p callback
@@ -473,7 +465,7 @@ int ev_timeout(ev_source *ev,
               const struct timeval *when,
               ev_timeout_callback *callback,
               void *u) {
-  struct timeout *t, *p, **pp;
+  struct timeout *t;
 
   D(("registering timeout at %ld.%ld callback %p %p",
      when ? (long)when->tv_sec : 0, when ? (long)when->tv_usec : 0,
@@ -483,11 +475,8 @@ int ev_timeout(ev_source *ev,
     t->when = *when;
   t->callback = callback;
   t->u = u;
-  pp = &ev->timeouts;
-  while((p = *pp) && gt(&t->when, &p->when))
-    pp = &p->next;
-  t->next = p;
-  *pp = t;
+  t->active = 1;
+  timeout_heap_insert(ev->timeouts, t);
   if(handlep)
     *handlep = t;
   return 0;
@@ -500,19 +489,13 @@ int ev_timeout(ev_source *ev,
  *
  * If @p handle is 0 then this is a no-op.
  */
-int ev_timeout_cancel(ev_source *ev,
+int ev_timeout_cancel(ev_source attribute((unused)) *ev,
                      ev_timeout_handle handle) {
-  struct timeout *t = handle, *p, **pp;
+  struct timeout *t = handle;
 
-  if(!t)
-    return 0;
-  for(pp = &ev->timeouts; (p = *pp) && p != t; pp = &p->next)
-    ;
-  if(p) {
-    *pp = p->next;
-    return 0;
-  } else
-    return -1;
+  if(t)
+    t->active = 0;
+  return 0;
 }
 
 /* signals ********************************************************************/
@@ -536,7 +519,9 @@ static void sighandler(int s) {
 
   /* probably the reader has stopped listening for some reason */
   if(write(sigfd[s], &sc, 1) < 0) {
-    write(2, errmsg, sizeof errmsg - 1);
+       /* do the best we can as we're about to abort; shut _up_, gcc */
+       int _ignore = write(2, errmsg, sizeof errmsg - 1);
+       (void)_ignore;
     abort();
   }
 }
@@ -554,7 +539,7 @@ static int signal_read(ev_source *ev,
       return ret;
   assert(n != 0);
   if(n < 0 && (errno != EINTR && errno != EAGAIN)) {
-    error(errno, "error reading from signal pipe %d", ev->sigpipe[0]);
+    disorder_error(errno, "error reading from signal pipe %d", ev->sigpipe[0]);
     return -1;
   }
   return 0;
@@ -695,8 +680,8 @@ static int sigchld_callback(ev_source *ev,
         * want the disorder server to bomb out because of it.  So we just log
         * the problem and ignore it.
         */
-       error(errno, "error calling wait4 for PID %lu (broken ptrace?)",
-             (unsigned long)ev->children[n].pid);
+       disorder_error(errno, "error calling wait4 for PID %lu (broken ptrace?)",
+                      (unsigned long)ev->children[n].pid);
        if(errno != ECHILD)
          return -1;
       }
@@ -769,6 +754,36 @@ int ev_child_cancel(ev_source *ev,
   return 0;
 }
 
+/** @brief Terminate and wait for all child processes
+ * @param ev Event loop
+ *
+ * Does *not* call the completion callbacks.  Only used during teardown.
+ */
+void ev_child_killall(ev_source *ev) {
+  int n, rc, w;
+
+  for(n = 0; n < ev->nchildren; ++n) {
+    if(kill(ev->children[n].pid, SIGTERM) < 0) {
+      disorder_error(errno, "sending SIGTERM to pid %lu",
+                    (unsigned long)ev->children[n].pid);
+      ev->children[n].pid = -1;
+    }
+  }
+  for(n = 0; n < ev->nchildren; ++n) {
+    if(ev->children[n].pid == -1)
+      continue;
+    do {
+      rc = waitpid(ev->children[n].pid, &w, 0);
+    } while(rc < 0 && errno == EINTR);
+    if(rc < 0) {
+      disorder_error(errno, "waiting for pid %lu",
+                    (unsigned long)ev->children[n].pid);
+      continue;
+    }
+  }
+  ev->nchildren = 0;
+}
+
 /* socket listeners ***********************************************************/
 
 /** @brief State for a socket listener */
@@ -804,22 +819,22 @@ static int listen_callback(ev_source *ev, int fd, void *u) {
     break;
 #ifdef ECONNABORTED
   case ECONNABORTED:
-    error(errno, "error calling accept");
+    disorder_error(errno, "error calling accept");
     break;
 #endif
 #ifdef EPROTO
   case EPROTO:
     /* XXX on some systems EPROTO should be fatal, but we don't know if
      * we're running on one of them */
-    error(errno, "error calling accept");
+    disorder_error(errno, "error calling accept");
     break;
 #endif
   default:
-    fatal(errno, "error calling accept");
+    disorder_fatal(errno, "error calling accept");
     break;
   }
   if(errno != EINTR && errno != EAGAIN)
-    error(errno, "error calling accept");
+    disorder_error(errno, "error calling accept");
   return 0;
 }
 
@@ -930,6 +945,9 @@ struct ev_writer {
 
   /** @brief Tied reader or 0 */
   ev_reader *reader;
+
+  /** @brief Set when abandoned */
+  int abandoned;
 };
 
 /** @brief State structure for a buffered reader */
@@ -998,9 +1016,12 @@ static int writer_timebound_exceeded(ev_source *ev,
                                     void *u) {
   ev_writer *const w = u;
 
-  error(0, "abandoning writer %s because no writes within %ds",
-       w->what, w->timebound);
-  w->error = ETIMEDOUT;
+  if(!w->abandoned) {
+    w->abandoned = 1;
+    disorder_error(0, "abandoning writer '%s' because no writes within %ds",
+                  w->what, w->timebound);
+    w->error = ETIMEDOUT;
+  }
   return writer_shutdown(ev, now, u);
 }
 
@@ -1070,16 +1091,20 @@ static int ev_writer_write(struct sink *sk, const void *s, int n) {
   if(!n)
     return 0;                          /* avoid silliness */
   if(w->fd == -1)
-    error(0, "ev_writer_write on %s after shutdown", w->what);
+    disorder_error(0, "ev_writer_write on %s after shutdown", w->what);
   if(w->spacebound && w->b.end - w->b.start + n > w->spacebound) {
     /* The new buffer contents will exceed the space bound.  We assume that the
      * remote client has gone away and TCP hasn't noticed yet, or that it's got
      * hopelessly stuck. */
-    error(0, "abandoning writer %s because buffer has reached %td bytes",
-         w->what, w->b.end - w->b.start);
-    ev_fd_disable(w->ev, ev_write, w->fd);
-    w->error = EPIPE;
-    return ev_timeout(w->ev, 0, 0, writer_shutdown, w);
+    if(!w->abandoned) {
+      w->abandoned = 1;
+      disorder_error(0, "abandoning writer '%s' because buffer has reached %td bytes",
+                    w->what, w->b.end - w->b.start);
+      ev_fd_disable(w->ev, ev_write, w->fd);
+      w->error = EPIPE;
+      return ev_timeout(w->ev, 0, 0, writer_shutdown, w);
+    } else
+      return 0;
   }
   /* Make sure there is space */
   buffer_space(&w->b, n);
@@ -1183,7 +1208,7 @@ int ev_writer_space_bound(ev_writer *w,
  */
 struct sink *ev_writer_sink(ev_writer *w) {
   if(!w)
-    fatal(0, "ev_write_sink called with null writer");
+    disorder_fatal(0, "ev_write_sink called with null writer");
   return &w->s;
 }
 
@@ -1224,7 +1249,7 @@ int ev_writer_flush(ev_writer *w) {
 
 /* buffered reader ************************************************************/
 
-/** @brief Shut down a reader*
+/** @brief Shut down a reader
  *
  * This is the only path through which we cancel and close the file descriptor.
  * As with the writer case it is given timeout signature to allow it be