talloc: use the system pytalloc-util for python3 as well
[sfrench/samba-autobuild/.git] / lib / tevent / tevent_poll.c
index 68885e94c0feb09f8c37e00dc096155ea95e0656..09d85fa322adff298eb6a1f5e7c72bccd6121881 100644 (file)
@@ -38,6 +38,14 @@ struct poll_event_context {
         * picked up yet by poll_event_loop_once
         */
        struct tevent_fd *fresh;
+       /*
+        * A DLIST for disabled fde's.
+        */
+       struct tevent_fd *disabled;
+       /*
+        * one or more events were deleted or disabled
+        */
+       bool deleted;
 
        /*
         * These two arrays are maintained together.
@@ -50,9 +58,6 @@ struct poll_event_context {
         * Signal fd to wake the poll() thread
         */
        int signal_fd;
-
-       /* information for exiting from the event loop */
-       int exit_code;
 };
 
 static int poll_event_context_destructor(struct poll_event_context *poll_ev)
@@ -65,6 +70,12 @@ static int poll_event_context_destructor(struct poll_event_context *poll_ev)
                DLIST_REMOVE(poll_ev->fresh, fd);
        }
 
+       for (fd = poll_ev->disabled; fd; fd = fn) {
+               fn = fd->next;
+               fd->event_ctx = NULL;
+               DLIST_REMOVE(poll_ev->disabled, fd);
+       }
+
        if (poll_ev->signal_fd == -1) {
                /*
                 * Non-threaded, no signal pipe
@@ -218,24 +229,17 @@ static int poll_event_fd_destructor(struct tevent_fd *fde)
        poll_ev = talloc_get_type_abort(
                ev->additional_data, struct poll_event_context);
 
-       poll_ev->fdes[del_idx] = NULL;
-       poll_event_wake_pollthread(poll_ev);
-done:
-       return tevent_common_fd_destructor(fde);
-}
-
-static int poll_fresh_fde_destructor(struct tevent_fd *fde)
-{
-       struct tevent_context *ev = fde->event_ctx;
-       struct poll_event_context *poll_ev;
+       if (del_idx == UINT64_MAX) {
+               struct tevent_fd **listp =
+                       (struct tevent_fd **)fde->additional_data;
 
-       if (ev == NULL) {
+               DLIST_REMOVE((*listp), fde);
                goto done;
        }
-       poll_ev = talloc_get_type_abort(
-               ev->additional_data, struct poll_event_context);
 
-       DLIST_REMOVE(poll_ev->fresh, fde);
+       poll_ev->fdes[del_idx] = NULL;
+       poll_ev->deleted = true;
+       poll_event_wake_pollthread(poll_ev);
 done:
        return tevent_common_fd_destructor(fde);
 }
@@ -264,11 +268,19 @@ _PRIVATE_ void tevent_poll_event_add_fd_internal(struct tevent_context *ev,
 {
        struct poll_event_context *poll_ev = talloc_get_type_abort(
                ev->additional_data, struct poll_event_context);
+       struct tevent_fd **listp;
+
+       if (fde->flags != 0) {
+               listp = &poll_ev->fresh;
+       } else {
+               listp = &poll_ev->disabled;
+       }
 
        fde->additional_flags   = UINT64_MAX;
-       fde->additional_data    = NULL;
-       DLIST_ADD(poll_ev->fresh, fde);
-       talloc_set_destructor(fde, poll_fresh_fde_destructor);
+       fde->additional_data    = listp;
+
+       DLIST_ADD((*listp), fde);
+       talloc_set_destructor(fde, poll_event_fd_destructor);
 }
 
 /*
@@ -306,8 +318,7 @@ static struct tevent_fd *poll_event_add_fd(struct tevent_context *ev,
        fde->additional_flags   = UINT64_MAX;
        fde->additional_data    = NULL;
 
-       DLIST_ADD(poll_ev->fresh, fde);
-       talloc_set_destructor(fde, poll_fresh_fde_destructor);
+       tevent_poll_event_add_fd_internal(ev, fde);
        poll_event_wake_pollthread(poll_ev);
 
        /*
@@ -336,11 +347,28 @@ static void poll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
        fde->flags = flags;
 
        if (idx == UINT64_MAX) {
+               struct tevent_fd **listp =
+                       (struct tevent_fd **)fde->additional_data;
+
+               /*
+                * We move it between the fresh and disabled lists.
+                */
+               DLIST_REMOVE((*listp), fde);
+               tevent_poll_event_add_fd_internal(ev, fde);
+               poll_event_wake_pollthread(poll_ev);
+               return;
+       }
+
+       if (fde->flags == 0) {
                /*
-                * poll_event_setup_fresh not yet called after this fde was
-                * added. We don't have to do anything to transfer the changed
-                * flags to the array passed to poll(2)
+                * We need to remove it from the array
+                * and move it to the disabled list.
                 */
+               poll_ev->fdes[idx] = NULL;
+               poll_ev->deleted = true;
+               DLIST_REMOVE(ev->fd_events, fde);
+               tevent_poll_event_add_fd_internal(ev, fde);
+               poll_event_wake_pollthread(poll_ev);
                return;
        }
 
@@ -363,6 +391,34 @@ static bool poll_event_setup_fresh(struct tevent_context *ev,
        struct tevent_fd *fde, *next;
        unsigned num_fresh, num_fds;
 
+       if (poll_ev->deleted) {
+               unsigned first_fd = (poll_ev->signal_fd != -1) ? 1 : 0;
+               unsigned i;
+
+               for (i=first_fd; i < poll_ev->num_fds;) {
+                       fde = poll_ev->fdes[i];
+                       if (fde != NULL) {
+                               i++;
+                               continue;
+                       }
+
+                       /*
+                        * This fde was talloc_free()'ed. Delete it
+                        * from the arrays
+                        */
+                       poll_ev->num_fds -= 1;
+                       if (poll_ev->num_fds == i) {
+                               break;
+                       }
+                       poll_ev->fds[i] = poll_ev->fds[poll_ev->num_fds];
+                       poll_ev->fdes[i] = poll_ev->fdes[poll_ev->num_fds];
+                       if (poll_ev->fdes[i] != NULL) {
+                               poll_ev->fdes[i]->additional_flags = i;
+                       }
+               }
+               poll_ev->deleted = false;
+       }
+
        if (poll_ev->fresh == NULL) {
                return true;
        }
@@ -425,8 +481,6 @@ static bool poll_event_setup_fresh(struct tevent_context *ev,
                DLIST_REMOVE(poll_ev->fresh, fde);
                DLIST_ADD(ev->fd_events, fde);
 
-               talloc_set_destructor(fde, poll_event_fd_destructor);
-
                poll_ev->num_fds += 1;
        }
        return true;
@@ -442,9 +496,10 @@ static int poll_event_loop_poll(struct tevent_context *ev,
                ev->additional_data, struct poll_event_context);
        int pollrtn;
        int timeout = -1;
-       unsigned first_fd;
-       unsigned i, next_i;
        int poll_errno;
+       struct tevent_fd *fde = NULL;
+       struct tevent_fd *next = NULL;
+       unsigned i;
 
        if (ev->signal_events && tevent_common_check_signal(ev)) {
                return 0;
@@ -484,41 +539,43 @@ static int poll_event_loop_poll(struct tevent_context *ev,
                return 0;
        }
 
-       first_fd = (poll_ev->signal_fd != -1) ? 1 : 0;
-
        /* at least one file descriptor is ready - check
           which ones and call the handler, being careful to allow
           the handler to remove itself when called */
 
-       for (i=first_fd; i<poll_ev->num_fds; i = next_i) {
+       for (fde = ev->fd_events; fde; fde = next) {
+               uint64_t idx = fde->additional_flags;
                struct pollfd *pfd;
-               struct tevent_fd *fde;
                uint16_t flags = 0;
 
-               next_i = i + 1;
+               next = fde->next;
+
+               if (idx == UINT64_MAX) {
+                       continue;
+               }
+
+               pfd = &poll_ev->fds[idx];
 
-               fde = poll_ev->fdes[i];
-               if (fde == NULL) {
+               if (pfd->revents & POLLNVAL) {
                        /*
-                        * This fde was talloc_free()'ed. Delete it
-                        * from the arrays
+                        * the socket is dead! this should never
+                        * happen as the socket should have first been
+                        * made readable and that should have removed
+                        * the event, so this must be a bug.
+                        *
+                        * We ignore it here to match the epoll
+                        * behavior.
                         */
-                       poll_ev->num_fds -= 1;
-                       if (poll_ev->num_fds == i) {
-                               break;
-                       }
-                       poll_ev->fds[i] = poll_ev->fds[poll_ev->num_fds];
-                       poll_ev->fdes[i] = poll_ev->fdes[poll_ev->num_fds];
-                       if (poll_ev->fdes[i] != NULL) {
-                               poll_ev->fdes[i]->additional_flags = i;
-                       }
-                       /* we have to reprocess position 'i' */
-                       next_i = i;
+                       tevent_debug(ev, TEVENT_DEBUG_ERROR,
+                                    "POLLNVAL on fde[%p] fd[%d] - disabling\n",
+                                    fde, pfd->fd);
+                       poll_ev->fdes[idx] = NULL;
+                       poll_ev->deleted = true;
+                       DLIST_REMOVE(ev->fd_events, fde);
+                       fde->event_ctx = NULL;
                        continue;
                }
 
-               pfd = &poll_ev->fds[i];
-
                if (pfd->revents & (POLLHUP|POLLERR)) {
                        /* If we only wait for TEVENT_FD_WRITE, we
                           should not tell the event handler about it,
@@ -537,9 +594,38 @@ static int poll_event_loop_poll(struct tevent_context *ev,
                if (pfd->revents & POLLOUT) {
                        flags |= TEVENT_FD_WRITE;
                }
+               /*
+                * Note that fde->flags could be changed when using
+                * the poll_mt backend together with threads,
+                * that why we need to check pfd->revents and fde->flags
+                */
+               flags &= fde->flags;
                if (flags != 0) {
+                       DLIST_DEMOTE(ev->fd_events, fde);
                        fde->handler(ev, fde, flags, fde->private_data);
-                       break;
+                       return 0;
+               }
+       }
+
+       for (i = 0; i < poll_ev->num_fds; i++) {
+               if (poll_ev->fds[i].revents & POLLNVAL) {
+                       /*
+                        * the socket is dead! this should never
+                        * happen as the socket should have first been
+                        * made readable and that should have removed
+                        * the event, so this must be a bug or
+                        * a race in the poll_mt usage.
+                        */
+                       fde = poll_ev->fdes[i];
+                       tevent_debug(ev, TEVENT_DEBUG_WARNING,
+                                    "POLLNVAL on dangling fd[%d] fde[%p] - disabling\n",
+                                    poll_ev->fds[i].fd, fde);
+                       poll_ev->fdes[i] = NULL;
+                       poll_ev->deleted = true;
+                       if (fde != NULL) {
+                               DLIST_REMOVE(ev->fd_events, fde);
+                               fde->event_ctx = NULL;
+                       }
                }
        }
 
@@ -559,6 +645,10 @@ static int poll_event_loop_once(struct tevent_context *ev,
                return 0;
        }
 
+       if (ev->threaded_contexts != NULL) {
+               tevent_common_threaded_activate_immediate(ev);
+       }
+
        if (ev->immediate_events &&
            tevent_common_loop_immediate(ev)) {
                return 0;
@@ -581,11 +671,9 @@ static int poll_event_loop_wait(struct tevent_context *ev,
        /*
         * loop as long as we have events pending
         */
-       while (ev->fd_events ||
-              ev->timer_events ||
-              ev->immediate_events ||
-              ev->signal_events ||
-              poll_ev->fresh) {
+       while (tevent_common_have_events(ev) ||
+              poll_ev->fresh ||
+              poll_ev->disabled) {
                int ret;
                ret = _tevent_loop_once(ev, location);
                if (ret != 0) {
@@ -607,7 +695,7 @@ static const struct tevent_ops poll_event_ops = {
        .set_fd_close_fn        = tevent_common_fd_set_close_fn,
        .get_fd_flags           = tevent_common_fd_get_flags,
        .set_fd_flags           = poll_event_set_fd_flags,
-       .add_timer              = tevent_common_add_timer,
+       .add_timer              = tevent_common_add_timer_v2,
        .schedule_immediate     = tevent_common_schedule_immediate,
        .add_signal             = tevent_common_add_signal,
        .loop_once              = poll_event_loop_once,
@@ -625,7 +713,7 @@ static const struct tevent_ops poll_event_mt_ops = {
        .set_fd_close_fn        = tevent_common_fd_set_close_fn,
        .get_fd_flags           = tevent_common_fd_get_flags,
        .set_fd_flags           = poll_event_set_fd_flags,
-       .add_timer              = tevent_common_add_timer,
+       .add_timer              = tevent_common_add_timer_v2,
        .schedule_immediate     = poll_event_schedule_immediate,
        .add_signal             = tevent_common_add_signal,
        .loop_once              = poll_event_loop_once,