]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.c
fdpass: fix complilation on FreeBSD.
[ccan] / ccan / io / io.c
index 7ebd5a202d7899f356ed1963eb3c6f463ee986e2..58ae19bdbd83dc91ea9954020b6fd10798106ea6 100644 (file)
@@ -15,6 +15,7 @@
 void *io_loop_return;
 
 struct io_plan io_conn_freed;
+static bool io_extended_errors;
 
 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
                                     struct io_plan *(*init)(struct io_conn *,
@@ -119,6 +120,16 @@ struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
        return conn;
 }
 
+bool io_conn_exclusive(struct io_conn *conn, bool exclusive)
+{
+       return backend_set_exclusive(&conn->plan[IO_IN], exclusive);
+}
+
+bool io_conn_out_exclusive(struct io_conn *conn, bool exclusive)
+{
+       return backend_set_exclusive(&conn->plan[IO_OUT], exclusive);
+}
+
 void io_set_finish_(struct io_conn *conn,
                    void (*finish)(struct io_conn *, void *),
                    void *arg)
@@ -373,9 +384,19 @@ void io_wake(const void *wait)
        backend_wake(wait);
 }
 
-/* Returns false if this should not be touched (eg. freed). */
-static bool do_plan(struct io_conn *conn, struct io_plan *plan,
-                   bool idle_on_epipe)
+enum plan_result {
+       /* Destroyed, do not touch */
+       FREED,
+       /* Worked, call again. */
+       KEEP_GOING,
+       /* Failed with EAGAIN or did partial. */
+       EXHAUSTED,
+       /* No longer interested in read (or write) */
+       UNINTERESTED
+};
+
+static enum plan_result do_plan(struct io_conn *conn, struct io_plan *plan,
+                               bool idle_on_epipe)
 {
        /* We shouldn't have polled for this event if this wasn't true! */
        assert(plan->status == IO_POLLING_NOTSTARTED
@@ -383,18 +404,26 @@ static bool do_plan(struct io_conn *conn, struct io_plan *plan,
 
        switch (plan->io(conn->fd.fd, &plan->arg)) {
        case -1:
+               /* This is expected, as we call optimistically! */
+               if (errno == EAGAIN)
+                       return EXHAUSTED;
                if (errno == EPIPE && idle_on_epipe) {
                        plan->status = IO_UNSET;
                        backend_new_plan(conn);
-                       return false;
+                       return UNINTERESTED;
                }
                io_close(conn);
-               return false;
+               return FREED;
        case 0:
                plan->status = IO_POLLING_STARTED;
-               return true;
+               /* If it started but didn't finish, don't call again. */
+               return EXHAUSTED;
        case 1:
-               return next_plan(conn, plan);
+               if (!next_plan(conn, plan))
+                       return FREED;
+               if (plan->status == IO_POLLING_NOTSTARTED)
+                       return KEEP_GOING;
+               return UNINTERESTED;
        default:
                /* IO should only return -1, 0 or 1 */
                abort();
@@ -403,16 +432,43 @@ static bool do_plan(struct io_conn *conn, struct io_plan *plan,
 
 void io_ready(struct io_conn *conn, int pollflags)
 {
-       if (pollflags & POLLIN)
-               if (!do_plan(conn, &conn->plan[IO_IN], false))
-                       return;
+       enum plan_result res;
+
+       if (pollflags & POLLIN) {
+               for (;;) {
+                       res = do_plan(conn, &conn->plan[IO_IN], false);
+                       switch (res) {
+                       case FREED:
+                               return;
+                       case EXHAUSTED:
+                       case UNINTERESTED:
+                               goto try_write;
+                       case KEEP_GOING:
+                               continue;
+                       }
+                       abort();
+               }
+       }
 
-       if (pollflags & POLLOUT)
-               /* If we're writing to a closed pipe, we need to wait for
-                * read to fail if we're duplex: we want to drain it! */
-               do_plan(conn, &conn->plan[IO_OUT],
-                       conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED
-                       || conn->plan[IO_IN].status == IO_POLLING_STARTED);
+try_write:
+       if (pollflags & POLLOUT) {
+               for (;;) {
+                       /* If we're writing to a closed pipe, we need to wait for
+                        * read to fail if we're duplex: we want to drain it! */
+                       res = do_plan(conn, &conn->plan[IO_OUT],
+                                     conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED
+                                     || conn->plan[IO_IN].status == IO_POLLING_STARTED);
+                       switch (res) {
+                       case FREED:
+                       case EXHAUSTED:
+                       case UNINTERESTED:
+                               return;
+                       case KEEP_GOING:
+                               continue;
+                       }
+                       abort();
+               }
+       }
 }
 
 void io_do_always(struct io_plan *plan)
@@ -519,6 +575,18 @@ bool io_plan_out_started(const struct io_conn *conn)
        return conn->plan[IO_OUT].status == IO_POLLING_STARTED;
 }
 
+/* Despite being a TCP expert, I missed the full extent of this
+ * problem.  The legendary ZmnSCPxj implemented it (with the URL
+ * pointing to the explanation), and I imitate that here. */
+struct io_plan *io_sock_shutdown(struct io_conn *conn)
+{
+       if (shutdown(io_conn_fd(conn), SHUT_WR) != 0)
+               return io_close(conn);
+
+       /* And leave unset .*/
+       return &conn->plan[IO_IN];
+}
+
 bool io_flush_sync(struct io_conn *conn)
 {
        struct io_plan *plan = &conn->plan[IO_OUT];
@@ -554,3 +622,13 @@ again:
        io_fd_block(io_conn_fd(conn), false);
        return ok;
 }
+
+void io_set_extended_errors(bool state)
+{
+       io_extended_errors = state;
+}
+
+bool io_get_extended_errors(void)
+{
+       return io_extended_errors;
+}