lib/tevent: change to LGPLv3+
[jra/samba/.git] / lib / tevent / tevent_standard.c
index 2a292a4c3966ed27b7440917c6ac2eb2d3f5ce21..8e107617cfc4022a1d09b11748548877c281de5a 100644 (file)
@@ -2,20 +2,24 @@
    Unix SMB/CIFS implementation.
    main select loop and event handling
    Copyright (C) Andrew Tridgell       2003-2005
-   Copyright (C) Stefan Metzmacher     2005
-   
-   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 3 of the License, or
-   (at your option) any later version.
-   
-   This program is distributed in the hope that it will be useful,
+   Copyright (C) Stefan Metzmacher     2005-2009
+
+     ** NOTE! The following LGPL license applies to the tevent
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library 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, see <http://www.gnu.org/licenses/>.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -38,9 +42,6 @@ struct std_event_context {
        /* a pointer back to the generic event_context */
        struct tevent_context *ev;
 
-       /* list of filedescriptor events */
-       struct tevent_fd *fd_events;
-
        /* the maximum file descriptor number in fd_events */
        int maxfd;
 
@@ -134,7 +135,7 @@ static void epoll_check_reopen(struct std_event_context *std_ev)
                return;
        }
        std_ev->pid = getpid();
-       for (fde=std_ev->fd_events;fde;fde=fde->next) {
+       for (fde=std_ev->ev->fd_events;fde;fde=fde->next) {
                epoll_add_event(std_ev, fde);
        }
 }
@@ -264,14 +265,14 @@ static int epoll_event_loop(struct std_event_context *std_ev, struct timeval *tv
                timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
        }
 
-       if (std_ev->ev->num_signal_handlers && 
+       if (std_ev->ev->signal_events &&
            tevent_common_check_signal(std_ev->ev)) {
                return 0;
        }
 
        ret = epoll_wait(std_ev->epoll_fd, events, MAXEVENTS, timeout);
 
-       if (ret == -1 && errno == EINTR && std_ev->ev->num_signal_handlers) {
+       if (ret == -1 && errno == EINTR && std_ev->ev->signal_events) {
                if (tevent_common_check_signal(std_ev->ev)) {
                        return 0;
                }
@@ -358,7 +359,7 @@ static void calc_maxfd(struct std_event_context *std_ev)
        struct tevent_fd *fde;
 
        std_ev->maxfd = 0;
-       for (fde = std_ev->fd_events; fde; fde = fde->next) {
+       for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
                if (fde->fd > std_ev->maxfd) {
                        std_ev->maxfd = fde->fd;
                }
@@ -377,26 +378,24 @@ static void calc_maxfd(struct std_event_context *std_ev)
 static int std_event_fd_destructor(struct tevent_fd *fde)
 {
        struct tevent_context *ev = fde->event_ctx;
-       struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
-                                                          struct std_event_context);
+       struct std_event_context *std_ev = NULL;
 
-       epoll_check_reopen(std_ev);
+       if (ev) {
+               std_ev = talloc_get_type(ev->additional_data,
+                                        struct std_event_context);
 
-       if (std_ev->maxfd == fde->fd) {
-               std_ev->maxfd = EVENT_INVALID_MAXFD;
-       }
+               epoll_check_reopen(std_ev);
 
-       DLIST_REMOVE(std_ev->fd_events, fde);
-       std_ev->destruction_count++;
+               if (std_ev->maxfd == fde->fd) {
+                       std_ev->maxfd = EVENT_INVALID_MAXFD;
+               }
 
-       epoll_del_event(std_ev, fde);
+               std_ev->destruction_count++;
 
-       if (fde->close_fn) {
-               fde->close_fn(ev, fde, fde->fd, fde->private_data);
-               fde->fd = -1;
+               epoll_del_event(std_ev, fde);
        }
 
-       return 0;
+       return tevent_common_fd_destructor(fde);
 }
 
 /*
@@ -416,21 +415,11 @@ static struct tevent_fd *std_event_add_fd(struct tevent_context *ev, TALLOC_CTX
 
        epoll_check_reopen(std_ev);
 
-       fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
+       fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
+                                  handler, private_data,
+                                  handler_name, location);
        if (!fde) return NULL;
 
-       fde->event_ctx          = ev;
-       fde->fd                 = fd;
-       fde->flags              = flags;
-       fde->handler            = handler;
-       fde->close_fn           = NULL;
-       fde->private_data       = private_data;
-       fde->handler_name       = handler_name;
-       fde->location           = location;
-       fde->additional_flags   = 0;
-       fde->additional_data    = NULL;
-
-       DLIST_ADD(std_ev->fd_events, fde);
        if ((std_ev->maxfd != EVENT_INVALID_MAXFD)
            && (fde->fd > std_ev->maxfd)) {
                std_ev->maxfd = fde->fd;
@@ -481,7 +470,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva
        FD_ZERO(&w_fds);
 
        /* setup any fd events */
-       for (fde = std_ev->fd_events; fde; fde = fde->next) {
+       for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
                if (fde->flags & TEVENT_FD_READ) {
                        FD_SET(fde->fd, &r_fds);
                }
@@ -490,7 +479,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva
                }
        }
 
-       if (std_ev->ev->num_signal_handlers && 
+       if (std_ev->ev->signal_events &&
            tevent_common_check_signal(std_ev->ev)) {
                return 0;
        }
@@ -498,7 +487,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva
        selrtn = select(std_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
 
        if (selrtn == -1 && errno == EINTR && 
-           std_ev->ev->num_signal_handlers) {
+           std_ev->ev->signal_events) {
                tevent_common_check_signal(std_ev->ev);
                return 0;
        }
@@ -525,7 +514,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva
                /* 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 (fde = std_ev->fd_events; fde; fde = fde->next) {
+               for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
                        uint16_t flags = 0;
 
                        if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
@@ -574,7 +563,7 @@ static int std_event_loop_wait(struct tevent_context *ev)
                                                           struct std_event_context);
        std_ev->exit_code = 0;
 
-       while (std_ev->fd_events && std_ev->exit_code == 0) {
+       while (ev->fd_events && std_ev->exit_code == 0) {
                if (std_event_loop_once(ev) != 0) {
                        break;
                }