fix resuming accept() calls after exceeding client limit
[project/uhttpd.git] / listen.c
index 55d7143..f11768a 100644 (file)
--- a/listen.c
+++ b/listen.c
@@ -37,6 +37,14 @@ struct listener {
 static LIST_HEAD(listeners);
 static int n_blocked;
 
+void uh_close_listen_fds(void)
+{
+       struct listener *l;
+
+       list_for_each_entry(l, &listeners, list)
+               close(l->fd.fd);
+}
+
 static void uh_block_listener(struct listener *l)
 {
        uloop_fd_delete(&l->fd);
@@ -48,7 +56,7 @@ void uh_unblock_listeners(void)
 {
        struct listener *l;
 
-       if (!n_blocked && conf.max_requests &&
+       if ((!n_blocked && conf.max_requests) ||
            n_clients >= conf.max_requests)
                return;
 
@@ -56,6 +64,10 @@ void uh_unblock_listeners(void)
                if (!l->blocked)
                        continue;
 
+               l->fd.cb(&l->fd, ULOOP_READ);
+           if (n_clients >= conf.max_requests)
+                       break;
+
                n_blocked--;
                l->blocked = false;
                uloop_fd_add(&l->fd, ULOOP_READ);
@@ -66,7 +78,10 @@ static void listener_cb(struct uloop_fd *fd, unsigned int events)
 {
        struct listener *l = container_of(fd, struct listener, fd);
 
-       uh_accept_client(fd->fd);
+       while (1) {
+               if (!uh_accept_client(fd->fd))
+                       break;
+       }
 
        if (conf.max_requests && n_clients >= conf.max_requests)
                uh_block_listener(l);