s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[garming/samba-autobuild/.git] / source3 / lib / events.c
index f850d23da23f03448ef823535ec0e63f07b0df38..f077f585812a5dce4ce5d899618dea1e0e2ad2d5 100644 (file)
 */
 
 #include "includes.h"
-#include <tevent_internal.h>
+#include "lib/tevent/tevent_internal.h"
 #include "../lib/util/select.h"
 #include "system/select.h"
 
-/*
- * Return if there's something in the queue
- */
-
-bool event_add_to_select_args(struct tevent_context *ev,
-                             fd_set *read_fds, fd_set *write_fds,
-                             struct timeval *timeout, int *maxfd)
-{
-       struct timeval now;
-       struct tevent_fd *fde;
-       struct timeval diff;
-       bool ret = false;
-
-       for (fde = ev->fd_events; fde; fde = fde->next) {
-               if (fde->flags & EVENT_FD_READ) {
-                       FD_SET(fde->fd, read_fds);
-                       ret = true;
-               }
-               if (fde->flags & EVENT_FD_WRITE) {
-                       FD_SET(fde->fd, write_fds);
-                       ret = true;
-               }
-
-               if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
-                   && (fde->fd > *maxfd)) {
-                       *maxfd = fde->fd;
-               }
-       }
-
-       if (ev->immediate_events != NULL) {
-               *timeout = timeval_zero();
-               return true;
-       }
-
-       if (ev->timer_events == NULL) {
-               return ret;
-       }
-
-       now = timeval_current();
-       diff = timeval_until(&now, &ev->timer_events->next_event);
-       *timeout = timeval_min(timeout, &diff);
-
-       return true;
-}
-
-bool run_events(struct tevent_context *ev,
-               int selrtn, fd_set *read_fds, fd_set *write_fds)
-{
-       struct tevent_fd *fde;
-       struct timeval now;
-
-       if (ev->signal_events &&
-           tevent_common_check_signal(ev)) {
-               return true;
-       }
-
-       if (ev->immediate_events &&
-           tevent_common_loop_immediate(ev)) {
-               return true;
-       }
-
-       GetTimeOfDay(&now);
-
-       if ((ev->timer_events != NULL)
-           && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
-               /* this older events system did not auto-free timed
-                  events on running them, and had a race condition
-                  where the event could be called twice if the
-                  talloc_free of the te happened after the callback
-                  made a call which invoked the event loop. To avoid
-                  this while still allowing old code which frees the
-                  te, we need to create a temporary context which
-                  will be used to ensure the te is freed. We also
-                  remove the te from the timed event list before we
-                  call the handler, to ensure we can't loop */
-
-               struct tevent_timer *te = ev->timer_events;
-               TALLOC_CTX *tmp_ctx = talloc_new(ev);
-
-               DEBUG(10, ("Running timed event \"%s\" %p\n",
-                          ev->timer_events->handler_name, ev->timer_events));
-
-               DLIST_REMOVE(ev->timer_events, te);
-               talloc_steal(tmp_ctx, te);
-
-               te->handler(ev, te, now, te->private_data);
-
-               talloc_free(tmp_ctx);
-               return true;
-       }
-
-       if (selrtn <= 0) {
-               /*
-                * No fd ready
-                */
-               return false;
-       }
-
-       for (fde = ev->fd_events; fde; fde = fde->next) {
-               uint16 flags = 0;
-
-               if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
-               if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
-
-               if (flags & fde->flags) {
-                       DLIST_DEMOTE(ev->fd_events, fde, struct tevent_fd);
-                       fde->handler(ev, fde, flags, fde->private_data);
-                       return true;
-               }
-       }
-
-       return false;
-}
-
 struct tevent_poll_private {
        /*
         * Index from file descriptor into the pollfd array
@@ -156,7 +42,7 @@ static struct tevent_poll_private *tevent_get_poll_private(
 
        state = (struct tevent_poll_private *)ev->additional_data;
        if (state == NULL) {
-               state = TALLOC_ZERO_P(ev, struct tevent_poll_private);
+               state = talloc_zero(ev, struct tevent_poll_private);
                ev->additional_data = (void *)state;
                if (state == NULL) {
                        DEBUG(10, ("talloc failed\n"));
@@ -204,7 +90,7 @@ bool event_add_to_poll_args(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
        idx_len = max_fd+1;
 
        if (talloc_array_length(state->pollfd_idx) < idx_len) {
-               state->pollfd_idx = TALLOC_REALLOC_ARRAY(
+               state->pollfd_idx = talloc_realloc(
                        state, state->pollfd_idx, int, idx_len);
                if (state->pollfd_idx == NULL) {
                        DEBUG(10, ("talloc_realloc failed\n"));
@@ -221,7 +107,7 @@ bool event_add_to_poll_args(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
         */
 
        if (talloc_array_length(fds) < num_pollfds + num_fds + 1) {
-               fds = TALLOC_REALLOC_ARRAY(mem_ctx, fds, struct pollfd,
+               fds = talloc_realloc(mem_ctx, fds, struct pollfd,
                                           num_pollfds + num_fds + 1);
                if (fds == NULL) {
                        DEBUG(10, ("talloc_realloc failed\n"));
@@ -280,7 +166,7 @@ bool event_add_to_poll_args(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
                return true;
        }
        if (ev->timer_events == NULL) {
-               *ptimeout = INT_MAX;
+               *ptimeout = MIN(*ptimeout, INT_MAX);
                return true;
        }
 
@@ -372,7 +258,20 @@ bool run_events_poll(struct tevent_context *ev, int pollrtn,
                        return false;
                }
 
-               if (pfd->revents & (POLLIN|POLLHUP|POLLERR)) {
+               if (pfd->revents & (POLLHUP|POLLERR)) {
+                       /* If we only wait for EVENT_FD_WRITE, we
+                          should not tell the event handler about it,
+                          and remove the writable flag, as we only
+                          report errors when waiting for read events
+                          to match the select behavior. */
+                       if (!(fde->flags & EVENT_FD_READ)) {
+                               EVENT_FD_NOT_WRITEABLE(fde);
+                               continue;
+                       }
+                       flags |= EVENT_FD_READ;
+               }
+
+               if (pfd->revents & POLLIN) {
                        flags |= EVENT_FD_READ;
                }
                if (pfd->revents & POLLOUT) {