]> git.ozlabs.org Git - ccan/commitdiff
io: handle errors on listening file descriptors.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 9 Jan 2017 02:46:32 +0000 (13:16 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 9 Jan 2017 02:46:32 +0000 (13:16 +1030)
While investigating the previous patch, a bug caused poll to return
POLLHUP on the listening socket, which caused us to spin.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io/io.h
ccan/io/poll.c
ccan/io/test/run-22-POLLHUP-on-listening-socket.c [new file with mode: 0644]

index 8efc0024c9d230a580bd7838de4d7ad30503e381..54300ce4575ba684389c93c478afa16d8b4d1e6b 100644 (file)
@@ -109,7 +109,8 @@ void io_set_finish_(struct io_conn *conn,
  * @arg: the argument to @init.
  *
  * When @fd becomes readable, we accept(), create a new connection,
  * @arg: the argument to @init.
  *
  * When @fd becomes readable, we accept(), create a new connection,
- * (tal'ocated off @ctx) and pass that to init().
+ * (tal'ocated off @ctx) and pass that to init().  Note that if there is
+ * an error on this file descriptor, it will be freed.
  *
  * Returns NULL on error (and sets errno).
  *
  *
  * Returns NULL on error (and sets errno).
  *
index 98a64f42fbe4336283be8f5371688da439943ac6..229f7ce9ab4e6fb9bf69ccfd7ae5b650a4463613 100644 (file)
@@ -282,9 +282,14 @@ void *io_loop(struct timers *timers, struct timer **expired)
                                break;
 
                        if (fds[i]->listener) {
                                break;
 
                        if (fds[i]->listener) {
+                               struct io_listener *l = (void *)fds[i];
                                if (events & POLLIN) {
                                if (events & POLLIN) {
-                                       accept_conn((void *)c);
+                                       accept_conn(l);
                                        r--;
                                        r--;
+                               } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
+                                       r--;
+                                       errno = EBADF;
+                                       io_close_listener(l);
                                }
                        } else if (events & (POLLIN|POLLOUT)) {
                                r--;
                                }
                        } else if (events & (POLLIN|POLLOUT)) {
                                r--;
diff --git a/ccan/io/test/run-22-POLLHUP-on-listening-socket.c b/ccan/io/test/run-22-POLLHUP-on-listening-socket.c
new file mode 100644 (file)
index 0000000..e3b6c41
--- /dev/null
@@ -0,0 +1,61 @@
+#include <ccan/io/io.h>
+/* Include the C files directly. */
+#include <ccan/io/poll.c>
+#include <ccan/io/io.c>
+#include <ccan/tap/tap.h>
+#include <sys/wait.h>
+#include <stdio.h>
+
+#define PORT "65022"
+
+static int make_listen_fd(const char *port, struct addrinfo **info)
+{
+       int fd, on = 1;
+       struct addrinfo *addrinfo, hints;
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_flags = AI_PASSIVE;
+       hints.ai_protocol = 0;
+
+       if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
+               return -1;
+
+       fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
+                   addrinfo->ai_protocol);
+       if (fd < 0)
+               return -1;
+
+       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+       if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
+               close(fd);
+               return -1;
+       }
+       if (listen(fd, 1) != 0) {
+               close(fd);
+               return -1;
+       }
+       *info = addrinfo;
+       return fd;
+}
+
+int main(void)
+{
+       struct addrinfo *addrinfo = NULL;
+       int fd;
+
+       /* This is how many tests you plan to run */
+       plan_tests(1);
+       fd = make_listen_fd(PORT, &addrinfo);
+       freeaddrinfo(addrinfo);
+       io_new_listener(NULL, fd, io_never, NULL);
+
+       /* Anyone could do this; a child doing it will cause poll to return
+        * POLLHUP only! */
+       shutdown(fd, SHUT_RDWR);
+       ok1(io_loop(NULL, NULL) == NULL);
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}