TODO/UNTESTED: tevent: add kqueue backend
[metze/samba/wip.git] / lib / tevent / tevent.c
index 0c02e46f3ca16e6478fe820ae459d5272e4dfcbc..3efc8d909d524e31be8fd0657125625045e60041 100644 (file)
@@ -88,7 +88,7 @@ bool tevent_register_backend(const char *name, const struct tevent_ops *ops)
                }
        }
 
-       e = talloc(talloc_autofree_context(), struct tevent_ops_list);
+       e = talloc(NULL, struct tevent_ops_list);
        if (e == NULL) return false;
 
        e->name = name;
@@ -104,8 +104,7 @@ bool tevent_register_backend(const char *name, const struct tevent_ops *ops)
 void tevent_set_default_backend(const char *backend)
 {
        talloc_free(tevent_default_backend);
-       tevent_default_backend = talloc_strdup(talloc_autofree_context(),
-                                              backend);
+       tevent_default_backend = talloc_strdup(NULL, backend);
 }
 
 /*
@@ -113,11 +112,46 @@ void tevent_set_default_backend(const char *backend)
 */
 static void tevent_backend_init(void)
 {
+       static bool done;
+
+       if (done) {
+               return;
+       }
+
+       done = true;
+
        tevent_select_init();
-       tevent_standard_init();
+       tevent_poll_init();
+       tevent_poll_mt_init();
 #ifdef HAVE_EPOLL
        tevent_epoll_init();
 #endif
+#ifdef HAVE_KQUEUE
+       tevent_kqueue_init();
+#endif
+       tevent_standard_init();
+}
+
+_PRIVATE_ const struct tevent_ops *tevent_find_ops_byname(const char *name)
+{
+       struct tevent_ops_list *e;
+
+       tevent_backend_init();
+
+       if (name == NULL) {
+               name = tevent_default_backend;
+       }
+       if (name == NULL) {
+               name = "standard";
+       }
+
+       for (e = tevent_backends; e != NULL; e = e->next) {
+               if (0 == strcmp(e->name, name)) {
+                       return e->ops;
+               }
+       }
+
+       return NULL;
 }
 
 /*
@@ -148,6 +182,8 @@ int tevent_common_context_destructor(struct tevent_context *ev)
 
        if (ev->pipe_fde) {
                talloc_free(ev->pipe_fde);
+               close(ev->pipe_fds[0]);
+               close(ev->pipe_fds[1]);
                ev->pipe_fde = NULL;
        }
 
@@ -157,6 +193,7 @@ int tevent_common_context_destructor(struct tevent_context *ev)
                DLIST_REMOVE(ev->fd_events, fd);
        }
 
+       ev->last_zero_timer = NULL;
        for (te = ev->timer_events; te; te = tn) {
                tn = te->next;
                te->event_ctx = NULL;
@@ -174,8 +211,26 @@ int tevent_common_context_destructor(struct tevent_context *ev)
                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);
        }
 
+       /* removing nesting hook or we get an abort when nesting is
+        * not allowed. -- SSS
+        * Note that we need to leave the allowed flag at its current
+        * value, otherwise the use in tevent_re_initialise() will
+        * leave the event context with allowed forced to false, which
+        * will break users that expect nesting to be allowed
+        */
+       ev->nesting.level = 0;
+       ev->nesting.hook_fn = NULL;
+       ev->nesting.hook_private = NULL;
+
        return 0;
 }
 
@@ -190,8 +245,9 @@ int tevent_common_context_destructor(struct tevent_context *ev)
 
   NOTE: use tevent_context_init() inside of samba!
 */
-static struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
-                                                     const struct tevent_ops *ops)
+struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
+                                              const struct tevent_ops *ops,
+                                              void *additional_data)
 {
        struct tevent_context *ev;
        int ret;
@@ -202,6 +258,7 @@ static struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
        talloc_set_destructor(ev, tevent_common_context_destructor);
 
        ev->ops = ops;
+       ev->additional_data = additional_data;
 
        ret = ev->ops->context_init(ev);
        if (ret != 0) {
@@ -220,23 +277,14 @@ static struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx,
                                                  const char *name)
 {
-       struct tevent_ops_list *e;
-
-       tevent_backend_init();
+       const struct tevent_ops *ops;
 
-       if (name == NULL) {
-               name = tevent_default_backend;
-       }
-       if (name == NULL) {
-               name = "standard";
+       ops = tevent_find_ops_byname(name);
+       if (ops == NULL) {
+               return NULL;
        }
 
-       for (e=tevent_backends;e;e=e->next) {
-               if (strcmp(name, e->name) == 0) {
-                       return tevent_context_init_ops(mem_ctx, e->ops);
-               }
-       }
-       return NULL;
+       return tevent_context_init_ops(mem_ctx, ops, NULL);
 }
 
 
@@ -253,9 +301,6 @@ struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx)
 /*
   add a fd based event
   return NULL on failure (memory allocation error)
-
-  if flags contains TEVENT_FD_AUTOCLOSE then the fd will be closed when
-  the returned fd_event context is freed
 */
 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
                                 TALLOC_CTX *mem_ctx,
@@ -385,7 +430,6 @@ struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
 
 /*
   schedule an immediate event
-  return NULL on failure
 */
 void _tevent_schedule_immediate(struct tevent_immediate *im,
                                struct tevent_context *ev,
@@ -485,7 +529,9 @@ int _tevent_loop_once(struct tevent_context *ev, const char *location)
                }
        }
 
+       tevent_trace_point_callback(ev, TEVENT_TRACE_BEFORE_LOOP_ONCE);
        ret = ev->ops->loop_once(ev, location);
+       tevent_trace_point_callback(ev, TEVENT_TRACE_AFTER_LOOP_ONCE);
 
        if (ev->nesting.level > 0) {
                if (ev->nesting.hook_fn) {
@@ -545,7 +591,9 @@ int _tevent_loop_until(struct tevent_context *ev,
        }
 
        while (!finished(private_data)) {
+               tevent_trace_point_callback(ev, TEVENT_TRACE_BEFORE_LOOP_ONCE);
                ret = ev->ops->loop_once(ev, location);
+               tevent_trace_point_callback(ev, TEVENT_TRACE_AFTER_LOOP_ONCE);
                if (ret != 0) {
                        break;
                }
@@ -607,3 +655,18 @@ int _tevent_loop_wait(struct tevent_context *ev, const char *location)
 {
        return ev->ops->loop_wait(ev, location);
 }
+
+
+/*
+  re-initialise a tevent context. This leaves you with the same
+  event context, but all events are wiped and the structure is
+  re-initialised. This is most useful after a fork()  
+
+  zero is returned on success, non-zero on failure
+*/
+int tevent_re_initialise(struct tevent_context *ev)
+{
+       tevent_common_context_destructor(ev);
+
+       return ev->ops->context_init(ev);
+}