+ if (pfd->events)
+ num_waiting--;
+
+ pfd->events = 0;
+ if (conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED
+ || conn->plan[IO_IN].status == IO_POLLING_STARTED)
+ pfd->events |= POLLIN;
+ if (conn->plan[IO_OUT].status == IO_POLLING_NOTSTARTED
+ || conn->plan[IO_OUT].status == IO_POLLING_STARTED)
+ pfd->events |= POLLOUT;
+
+ if (pfd->events) {
+ num_waiting++;
+ pfd->fd = conn->fd.fd;
+ } else {
+ pfd->fd = -conn->fd.fd;
+ }
+}
+
+void backend_wake(const void *wait)
+{
+ unsigned int i;
+
+ for (i = 0; i < num_fds; i++) {
+ struct io_conn *c;
+
+ /* Ignore listeners */
+ if (fds[i]->listener)
+ continue;
+
+ c = (void *)fds[i];
+ if (c->plan[IO_IN].status == IO_WAITING
+ && c->plan[IO_IN].arg.u1.const_vp == wait)
+ io_do_wakeup(c, IO_IN);
+
+ if (c->plan[IO_OUT].status == IO_WAITING
+ && c->plan[IO_OUT].arg.u1.const_vp == wait)
+ io_do_wakeup(c, IO_OUT);
+ }
+}
+
+static void destroy_conn(struct io_conn *conn, bool close_fd)
+{
+ int saved_errno = errno;
+
+ if (close_fd)
+ close(conn->fd.fd);
+ del_fd(&conn->fd);
+ /* In case it's on always list, remove it. */
+ list_del_init(&conn->always);
+
+ /* errno saved/restored by tal_free itself. */
+ if (conn->finish) {
+ errno = saved_errno;
+ conn->finish(conn, conn->finish_arg);