1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
13 #include <ccan/container_of/container_of.h>
17 struct io_plan io_conn_freed;
19 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
20 struct io_plan *(*init)(struct io_conn *,
24 struct io_listener *l = tal(ctx, struct io_listener);
28 l->fd.listener = true;
38 void io_close_listener(struct io_listener *l)
43 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
48 /* Returns false if conn was freed. */
49 static bool next_plan(struct io_conn *conn, struct io_plan *plan)
51 struct io_plan *(*next)(struct io_conn *, void *arg);
55 plan->status = IO_UNSET;
57 plan->next = io_never_called;
59 plan = next(conn, plan->next_arg);
61 if (plan == &io_conn_freed)
64 assert(plan == &conn->plan[plan->dir]);
65 assert(conn->plan[IO_IN].status != IO_UNSET
66 || conn->plan[IO_OUT].status != IO_UNSET);
68 backend_new_plan(conn);
72 bool io_fd_block(int fd, bool block)
74 int flags = fcntl(fd, F_GETFL);
84 return fcntl(fd, F_SETFL, flags) != -1;
87 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
88 struct io_plan *(*init)(struct io_conn *, void *),
91 struct io_conn *conn = tal(ctx, struct io_conn);
96 conn->fd.listener = false;
99 conn->finish_arg = NULL;
100 list_node_init(&conn->always);
103 return tal_free(conn);
105 /* Keep our I/O async. */
106 io_fd_block(fd, false);
108 /* So we can get back from plan -> conn later */
109 conn->plan[IO_OUT].dir = IO_OUT;
110 conn->plan[IO_IN].dir = IO_IN;
112 /* We start with out doing nothing, and in doing our init. */
113 conn->plan[IO_OUT].status = IO_UNSET;
115 conn->plan[IO_IN].next = init;
116 conn->plan[IO_IN].next_arg = arg;
117 if (!next_plan(conn, &conn->plan[IO_IN]))
123 void io_set_finish_(struct io_conn *conn,
124 void (*finish)(struct io_conn *, void *),
127 conn->finish = finish;
128 conn->finish_arg = arg;
131 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
133 assert(conn->plan[dir].status == IO_UNSET);
135 conn->plan[dir].status = IO_POLLING_NOTSTARTED;
136 return &conn->plan[dir].arg;
139 static struct io_plan *set_always(struct io_conn *conn,
140 enum io_direction dir,
141 struct io_plan *(*next)(struct io_conn *,
145 struct io_plan *plan = &conn->plan[dir];
147 plan->status = IO_ALWAYS;
148 backend_new_always(conn);
149 return io_set_plan(conn, dir, NULL, next, arg);
152 static struct io_plan *io_always_dir(struct io_conn *conn,
153 enum io_direction dir,
154 struct io_plan *(*next)(struct io_conn *,
158 return set_always(conn, dir, next, arg);
161 struct io_plan *io_always_(struct io_conn *conn,
162 struct io_plan *(*next)(struct io_conn *, void *),
165 return io_always_dir(conn, IO_IN, next, arg);
168 struct io_plan *io_out_always_(struct io_conn *conn,
169 struct io_plan *(*next)(struct io_conn *,
173 return io_always_dir(conn, IO_OUT, next, arg);
176 static int do_write(int fd, struct io_plan_arg *arg)
178 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
184 return arg->u2.s == 0;
187 /* Queue some data to be written. */
188 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
189 struct io_plan *(*next)(struct io_conn *, void *),
192 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
195 return set_always(conn, IO_OUT, next, next_arg);
197 arg->u1.const_vp = data;
200 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
203 static int do_read(int fd, struct io_plan_arg *arg)
205 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
207 /* Errno isn't set if we hit EOF, so set it to distinct value */
215 return arg->u2.s == 0;
218 /* Queue a request to read into a buffer. */
219 struct io_plan *io_read_(struct io_conn *conn,
220 void *data, size_t len,
221 struct io_plan *(*next)(struct io_conn *, void *),
224 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
227 return set_always(conn, IO_IN, next, next_arg);
232 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
235 static int do_read_partial(int fd, struct io_plan_arg *arg)
237 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
239 /* Errno isn't set if we hit EOF, so set it to distinct value */
245 *(size_t *)arg->u2.vp = ret;
249 /* Queue a partial request to read into a buffer. */
250 struct io_plan *io_read_partial_(struct io_conn *conn,
251 void *data, size_t maxlen, size_t *len,
252 struct io_plan *(*next)(struct io_conn *,
256 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
259 return set_always(conn, IO_IN, next, next_arg);
262 /* We store the max len in here temporarily. */
266 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
269 static int do_write_partial(int fd, struct io_plan_arg *arg)
271 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
275 *(size_t *)arg->u2.vp = ret;
279 /* Queue a partial write request. */
280 struct io_plan *io_write_partial_(struct io_conn *conn,
281 const void *data, size_t maxlen, size_t *len,
282 struct io_plan *(*next)(struct io_conn *,
286 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
289 return set_always(conn, IO_OUT, next, next_arg);
291 arg->u1.const_vp = data;
292 /* We store the max len in here temporarily. */
296 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
299 static int do_connect(int fd, struct io_plan_arg *arg)
302 socklen_t len = sizeof(err);
304 /* Has async connect finished? */
305 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
311 } else if (err == EINPROGRESS)
318 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
319 struct io_plan *(*next)(struct io_conn *, void *),
322 int fd = io_conn_fd(conn);
324 /* We don't actually need the arg, but we need it polling. */
325 io_plan_arg(conn, IO_OUT);
327 /* Note that io_new_conn() will make fd O_NONBLOCK */
329 /* Immediate connect can happen. */
330 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
331 return set_always(conn, IO_OUT, next, next_arg);
333 if (errno != EINPROGRESS)
334 return io_close(conn);
336 return io_set_plan(conn, IO_OUT, do_connect, next, next_arg);
339 static struct io_plan *io_wait_dir(struct io_conn *conn,
341 enum io_direction dir,
342 struct io_plan *(*next)(struct io_conn *,
346 struct io_plan_arg *arg = io_plan_arg(conn, dir);
347 arg->u1.const_vp = wait;
349 conn->plan[dir].status = IO_WAITING;
351 return io_set_plan(conn, dir, NULL, next, next_arg);
354 struct io_plan *io_wait_(struct io_conn *conn,
356 struct io_plan *(*next)(struct io_conn *, void *),
359 return io_wait_dir(conn, wait, IO_IN, next, next_arg);
362 struct io_plan *io_out_wait_(struct io_conn *conn,
364 struct io_plan *(*next)(struct io_conn *, void *),
367 return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
370 void io_wake(const void *wait)
375 /* Returns false if this should not be touched (eg. freed). */
376 static bool do_plan(struct io_conn *conn, struct io_plan *plan,
379 /* We shouldn't have polled for this event if this wasn't true! */
380 assert(plan->status == IO_POLLING_NOTSTARTED
381 || plan->status == IO_POLLING_STARTED);
383 switch (plan->io(conn->fd.fd, &plan->arg)) {
385 if (errno == EPIPE && idle_on_epipe) {
386 plan->status = IO_UNSET;
387 backend_new_plan(conn);
393 plan->status = IO_POLLING_STARTED;
396 return next_plan(conn, plan);
398 /* IO should only return -1, 0 or 1 */
403 void io_ready(struct io_conn *conn, int pollflags)
405 if (pollflags & POLLIN)
406 if (!do_plan(conn, &conn->plan[IO_IN], false))
409 if (pollflags & POLLOUT)
410 /* If we're writing to a closed pipe, we need to wait for
411 * read to fail if we're duplex: we want to drain it! */
412 do_plan(conn, &conn->plan[IO_OUT],
413 conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED
414 || conn->plan[IO_IN].status == IO_POLLING_STARTED);
417 void io_do_always(struct io_conn *conn)
419 /* There's a corner case where the in next_plan wakes up the
420 * out, placing it in IO_ALWAYS and we end up processing it immediately,
421 * only to leave it in the always list.
423 * Yet we can't just process one, in case they are both supposed
424 * to be done, so grab state beforehand.
426 bool always_out = (conn->plan[IO_OUT].status == IO_ALWAYS);
428 if (conn->plan[IO_IN].status == IO_ALWAYS)
429 if (!next_plan(conn, &conn->plan[IO_IN]))
433 /* You can't *unalways* a conn (except by freeing, in which
434 * case next_plan() returned false */
435 assert(conn->plan[IO_OUT].status == IO_ALWAYS);
436 next_plan(conn, &conn->plan[IO_OUT]);
440 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
442 struct io_plan *plan = &conn->plan[dir];
444 assert(plan->status == IO_WAITING);
446 set_always(conn, dir, plan->next, plan->next_arg);
449 /* Close the connection, we're done. */
450 struct io_plan *io_close(struct io_conn *conn)
453 return &io_conn_freed;
456 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
458 return io_close(conn);
461 struct io_plan *io_close_taken_fd(struct io_conn *conn)
463 io_fd_block(conn->fd.fd, true);
465 cleanup_conn_without_close(conn);
466 return io_close(conn);
469 /* Exit the loop, returning this (non-NULL) arg. */
470 void io_break(const void *ret)
473 io_loop_return = (void *)ret;
476 struct io_plan *io_never(struct io_conn *conn, void *unused)
478 return io_always(conn, io_never_called, NULL);
481 int io_conn_fd(const struct io_conn *conn)
486 struct io_plan *io_duplex(struct io_conn *conn,
487 struct io_plan *in_plan, struct io_plan *out_plan)
489 assert(conn == container_of(in_plan, struct io_conn, plan[IO_IN]));
490 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
491 assert(out_plan == in_plan + 1);
495 struct io_plan *io_halfclose(struct io_conn *conn)
497 /* Both unset? OK. */
498 if (conn->plan[IO_IN].status == IO_UNSET
499 && conn->plan[IO_OUT].status == IO_UNSET)
500 return io_close(conn);
502 /* We leave this unset then. */
503 if (conn->plan[IO_IN].status == IO_UNSET)
504 return &conn->plan[IO_IN];
506 return &conn->plan[IO_OUT];
509 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
510 int (*io)(int fd, struct io_plan_arg *arg),
511 struct io_plan *(*next)(struct io_conn *, void *),
514 struct io_plan *plan = &conn->plan[dir];
518 plan->next_arg = next_arg;
519 assert(next != NULL);
524 bool io_plan_in_started(const struct io_conn *conn)
526 return conn->plan[IO_IN].status == IO_POLLING_STARTED;
529 bool io_plan_out_started(const struct io_conn *conn)
531 return conn->plan[IO_OUT].status == IO_POLLING_STARTED;
534 bool io_flush_sync(struct io_conn *conn)
536 struct io_plan *plan = &conn->plan[IO_OUT];
539 /* Not writing? Nothing to do. */
540 if (plan->status != IO_POLLING_STARTED
541 && plan->status != IO_POLLING_NOTSTARTED)
544 /* Synchronous please. */
545 io_fd_block(io_conn_fd(conn), true);
548 switch (plan->io(conn->fd.fd, &plan->arg)) {
552 /* Incomplete, try again. */
554 plan->status = IO_POLLING_STARTED;
558 /* In case they come back. */
559 set_always(conn, IO_OUT, plan->next, plan->next_arg);
562 /* IO should only return -1, 0 or 1 */
566 io_fd_block(io_conn_fd(conn), false);