* @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).
*
--- /dev/null
+#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();
+}