s3: signals are processed twice in child.
[ira/wip.git] / lib / tevent / tevent.c
index 867cfc08feff88c3532a98f76c94d1f5b8fb0e47..a0ee208663880d0429de3744a2b8c951e6ed183f 100644 (file)
@@ -143,10 +143,13 @@ int tevent_common_context_destructor(struct tevent_context *ev)
 {
        struct tevent_fd *fd, *fn;
        struct tevent_timer *te, *tn;
+       struct tevent_immediate *ie, *in;
        struct tevent_signal *se, *sn;
 
        if (ev->pipe_fde) {
                talloc_free(ev->pipe_fde);
+               close(ev->pipe_fds[0]);
+               close(ev->pipe_fds[1]);
                ev->pipe_fde = NULL;
        }
 
@@ -162,10 +165,24 @@ int tevent_common_context_destructor(struct tevent_context *ev)
                DLIST_REMOVE(ev->timer_events, te);
        }
 
+       for (ie = ev->immediate_events; ie; ie = in) {
+               in = ie->next;
+               ie->event_ctx = NULL;
+               ie->cancel_fn = NULL;
+               DLIST_REMOVE(ev->immediate_events, ie);
+       }
+
        for (se = ev->signal_events; se; se = sn) {
                sn = se->next;
                se->event_ctx = NULL;
                DLIST_REMOVE(ev->signal_events, se);
+               /*
+                * This is important, Otherwise signals
+                * are handled twice in child. eg, SIGHUP.
+                * one added in parent, and another one in
+                * the child. -- BoYang
+                */
+               tevent_cleanup_pending_signal_handlers(se);
        }
 
        return 0;
@@ -349,6 +366,47 @@ struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
                                  handler_name, location);
 }
 
+/*
+  allocate an immediate event
+  return NULL on failure (memory allocation error)
+*/
+struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
+                                                 const char *location)
+{
+       struct tevent_immediate *im;
+
+       im = talloc(mem_ctx, struct tevent_immediate);
+       if (im == NULL) return NULL;
+
+       im->prev                = NULL;
+       im->next                = NULL;
+       im->event_ctx           = NULL;
+       im->create_location     = location;
+       im->handler             = NULL;
+       im->private_data        = NULL;
+       im->handler_name        = NULL;
+       im->schedule_location   = NULL;
+       im->cancel_fn           = NULL;
+       im->additional_data     = NULL;
+
+       return im;
+}
+
+/*
+  schedule an immediate event
+  return NULL on failure
+*/
+void _tevent_schedule_immediate(struct tevent_immediate *im,
+                               struct tevent_context *ev,
+                               tevent_immediate_handler_t handler,
+                               void *private_data,
+                               const char *handler_name,
+                               const char *location)
+{
+       ev->ops->schedule_immediate(im, ev, handler, private_data,
+                                   handler_name, location);
+}
+
 /*
   add a signal event
 
@@ -378,6 +436,14 @@ void tevent_loop_set_nesting_hook(struct tevent_context *ev,
                                  tevent_nesting_hook hook,
                                  void *private_data)
 {
+       if (ev->nesting.hook_fn && 
+           (ev->nesting.hook_fn != hook ||
+            ev->nesting.hook_private != private_data)) {
+               /* the way the nesting hook code is currently written
+                  we cannot support two different nesting hooks at the
+                  same time. */
+               tevent_abort(ev, "tevent: Violation of nesting hook rules\n");
+       }
        ev->nesting.hook_fn = hook;
        ev->nesting.hook_private = private_data;
 }
@@ -411,6 +477,8 @@ int _tevent_loop_once(struct tevent_context *ev, const char *location)
                        errno = ELOOP;
                        return -1;
                }
+       }
+       if (ev->nesting.level > 0) {
                if (ev->nesting.hook_fn) {
                        int ret2;
                        ret2 = ev->nesting.hook_fn(ev,
@@ -428,7 +496,7 @@ int _tevent_loop_once(struct tevent_context *ev, const char *location)
 
        ret = ev->ops->loop_once(ev, location);
 
-       if (ev->nesting.level > 1) {
+       if (ev->nesting.level > 0) {
                if (ev->nesting.hook_fn) {
                        int ret2;
                        ret2 = ev->nesting.hook_fn(ev,
@@ -468,6 +536,8 @@ int _tevent_loop_until(struct tevent_context *ev,
                        errno = ELOOP;
                        return -1;
                }
+       }
+       if (ev->nesting.level > 0) {
                if (ev->nesting.hook_fn) {
                        int ret2;
                        ret2 = ev->nesting.hook_fn(ev,
@@ -490,7 +560,7 @@ int _tevent_loop_until(struct tevent_context *ev,
                }
        }
 
-       if (ev->nesting.level > 1) {
+       if (ev->nesting.level > 0) {
                if (ev->nesting.hook_fn) {
                        int ret2;
                        ret2 = ev->nesting.hook_fn(ev,
@@ -511,6 +581,34 @@ done:
        return ret;
 }
 
+/*
+  return on failure or (with 0) if all fd events are removed
+*/
+int tevent_common_loop_wait(struct tevent_context *ev,
+                           const char *location)
+{
+       /*
+        * loop as long as we have events pending
+        */
+       while (ev->fd_events ||
+              ev->timer_events ||
+              ev->immediate_events ||
+              ev->signal_events) {
+               int ret;
+               ret = _tevent_loop_once(ev, location);
+               if (ret != 0) {
+                       tevent_debug(ev, TEVENT_DEBUG_FATAL,
+                                    "_tevent_loop_once() failed: %d - %s\n",
+                                    ret, strerror(errno));
+                       return ret;
+               }
+       }
+
+       tevent_debug(ev, TEVENT_DEBUG_WARNING,
+                    "tevent_common_loop_wait() out of events\n");
+       return 0;
+}
+
 /*
   return on failure or (with 0) if all fd events are removed
 */