uloop: clear processes and timeouts on uloop_done()
[project/libubox.git] / uloop.c
diff --git a/uloop.c b/uloop.c
index f226f50..4de26d4 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -201,16 +201,26 @@ static int register_poll(struct uloop_fd *fd, unsigned int flags)
        return epoll_ctl(poll_fd, op, fd->fd, &ev);
 }
 
+static int cur_fd, cur_nfds;
+static struct epoll_event events[ULOOP_MAX_EVENTS];
+
 int uloop_fd_delete(struct uloop_fd *sock)
 {
+       int i;
+
+       for (i = cur_fd + 1; i < cur_nfds; i++) {
+               if (events[i].data.ptr != sock)
+                       continue;
+
+               events[i].data.ptr = NULL;
+       }
        sock->registered = false;
        return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
 }
 
 static void uloop_run_events(int timeout)
 {
-       struct epoll_event events[ULOOP_MAX_EVENTS];
-       int nfds, n;
+       int n, nfds;
 
        nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
        for(n = 0; n < nfds; ++n)
@@ -218,12 +228,15 @@ static void uloop_run_events(int timeout)
                struct uloop_fd *u = events[n].data.ptr;
                unsigned int ev = 0;
 
-               if(events[n].events & EPOLLERR) {
+               if (!u)
+                       continue;
+
+               if(events[n].events & (EPOLLERR|EPOLLHUP)) {
                        u->error = true;
                        uloop_fd_delete(u);
                }
 
-               if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
+               if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
                        continue;
 
                if(events[n].events & EPOLLRDHUP)
@@ -235,9 +248,13 @@ static void uloop_run_events(int timeout)
                if(events[n].events & EPOLLOUT)
                        ev |= ULOOP_WRITE;
 
-               if(u->cb)
+               if(u->cb) {
+                       cur_fd = n;
+                       cur_nfds = nfds;
                        u->cb(u, ev);
+               }
        }
+       cur_nfds = 0;
 }
 
 #endif
@@ -438,6 +455,22 @@ static void uloop_process_timeouts(struct timeval *tv)
        }
 }
 
+static void uloop_clear_timeouts(void)
+{
+       struct uloop_timeout *t, *tmp;
+
+       list_for_each_entry_safe(t, tmp, &timeouts, list)
+               uloop_timeout_cancel(t);
+}
+
+static void uloop_clear_processes(void)
+{
+       struct uloop_process *p, *tmp;
+
+       list_for_each_entry_safe(p, tmp, &processes, list)
+               uloop_process_delete(p);
+}
+
 void uloop_run(void)
 {
        struct timeval tv;
@@ -463,4 +496,7 @@ void uloop_done(void)
 
        close(poll_fd);
        poll_fd = -1;
+
+       uloop_clear_timeouts();
+       uloop_clear_processes();
 }