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 /* It should have set a plan inside this conn (or duplex) */
65 assert(plan == &conn->plan[IO_IN]
66 || plan == &conn->plan[IO_OUT]
67 || plan == &conn->plan[2]);
68 assert(conn->plan[IO_IN].status != IO_UNSET
69 || conn->plan[IO_OUT].status != IO_UNSET);
71 backend_new_plan(conn);
75 bool io_fd_block(int fd, bool block)
77 int flags = fcntl(fd, F_GETFL);
87 return fcntl(fd, F_SETFL, flags) != -1;
90 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
91 struct io_plan *(*init)(struct io_conn *, void *),
94 struct io_conn *conn = tal(ctx, struct io_conn);
99 conn->fd.listener = false;
102 conn->finish_arg = NULL;
103 list_node_init(&conn->always);
106 return tal_free(conn);
108 /* Keep our I/O async. */
109 io_fd_block(fd, false);
111 /* We start with out doing nothing, and in doing our init. */
112 conn->plan[IO_OUT].status = IO_UNSET;
114 conn->plan[IO_IN].next = init;
115 conn->plan[IO_IN].next_arg = arg;
116 if (!next_plan(conn, &conn->plan[IO_IN]))
122 void io_set_finish_(struct io_conn *conn,
123 void (*finish)(struct io_conn *, void *),
126 conn->finish = finish;
127 conn->finish_arg = arg;
130 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
132 assert(conn->plan[dir].status == IO_UNSET);
134 conn->plan[dir].status = IO_POLLING_NOTSTARTED;
135 return &conn->plan[dir].arg;
138 static struct io_plan *set_always(struct io_conn *conn,
139 enum io_direction dir,
140 struct io_plan *(*next)(struct io_conn *,
144 struct io_plan *plan = &conn->plan[dir];
146 plan->status = IO_ALWAYS;
147 backend_new_always(conn);
148 return io_set_plan(conn, dir, NULL, next, arg);
151 static struct io_plan *io_always_dir(struct io_conn *conn,
152 enum io_direction dir,
153 struct io_plan *(*next)(struct io_conn *,
157 return set_always(conn, dir, next, arg);
160 struct io_plan *io_always_(struct io_conn *conn,
161 struct io_plan *(*next)(struct io_conn *, void *),
164 return io_always_dir(conn, IO_IN, next, arg);
167 struct io_plan *io_out_always_(struct io_conn *conn,
168 struct io_plan *(*next)(struct io_conn *,
172 return io_always_dir(conn, IO_OUT, next, arg);
175 static int do_write(int fd, struct io_plan_arg *arg)
177 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
183 return arg->u2.s == 0;
186 /* Queue some data to be written. */
187 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
188 struct io_plan *(*next)(struct io_conn *, void *),
191 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
194 return set_always(conn, IO_OUT, next, next_arg);
196 arg->u1.const_vp = data;
199 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
202 static int do_read(int fd, struct io_plan_arg *arg)
204 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
206 /* Errno isn't set if we hit EOF, so set it to distinct value */
214 return arg->u2.s == 0;
217 /* Queue a request to read into a buffer. */
218 struct io_plan *io_read_(struct io_conn *conn,
219 void *data, size_t len,
220 struct io_plan *(*next)(struct io_conn *, void *),
223 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
226 return set_always(conn, IO_IN, next, next_arg);
231 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
234 static int do_read_partial(int fd, struct io_plan_arg *arg)
236 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
238 /* Errno isn't set if we hit EOF, so set it to distinct value */
244 *(size_t *)arg->u2.vp = ret;
248 /* Queue a partial request to read into a buffer. */
249 struct io_plan *io_read_partial_(struct io_conn *conn,
250 void *data, size_t maxlen, size_t *len,
251 struct io_plan *(*next)(struct io_conn *,
255 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
258 return set_always(conn, IO_IN, next, next_arg);
261 /* We store the max len in here temporarily. */
265 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
268 static int do_write_partial(int fd, struct io_plan_arg *arg)
270 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
274 *(size_t *)arg->u2.vp = ret;
278 /* Queue a partial write request. */
279 struct io_plan *io_write_partial_(struct io_conn *conn,
280 const void *data, size_t maxlen, size_t *len,
281 struct io_plan *(*next)(struct io_conn *,
285 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
288 return set_always(conn, IO_OUT, next, next_arg);
290 arg->u1.const_vp = data;
291 /* We store the max len in here temporarily. */
295 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
298 static int do_connect(int fd, struct io_plan_arg *arg)
301 socklen_t len = sizeof(err);
303 /* Has async connect finished? */
304 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
310 } else if (err == EINPROGRESS)
317 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
318 struct io_plan *(*next)(struct io_conn *, void *),
321 int fd = io_conn_fd(conn);
323 /* We don't actually need the arg, but we need it polling. */
324 io_plan_arg(conn, IO_OUT);
326 /* Note that io_new_conn() will make fd O_NONBLOCK */
328 /* Immediate connect can happen. */
329 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
330 return set_always(conn, IO_OUT, next, next_arg);
332 if (errno != EINPROGRESS)
333 return io_close(conn);
335 return io_set_plan(conn, IO_OUT, do_connect, next, next_arg);
338 static struct io_plan *io_wait_dir(struct io_conn *conn,
340 enum io_direction dir,
341 struct io_plan *(*next)(struct io_conn *,
345 struct io_plan_arg *arg = io_plan_arg(conn, dir);
346 arg->u1.const_vp = wait;
348 conn->plan[dir].status = IO_WAITING;
350 return io_set_plan(conn, dir, NULL, next, next_arg);
353 struct io_plan *io_wait_(struct io_conn *conn,
355 struct io_plan *(*next)(struct io_conn *, void *),
358 return io_wait_dir(conn, wait, IO_IN, next, next_arg);
361 struct io_plan *io_out_wait_(struct io_conn *conn,
363 struct io_plan *(*next)(struct io_conn *, void *),
366 return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
369 void io_wake(const void *wait)
374 /* Returns false if this should not be touched (eg. freed). */
375 static bool do_plan(struct io_conn *conn, struct io_plan *plan,
378 /* We shouldn't have polled for this event if this wasn't true! */
379 assert(plan->status == IO_POLLING_NOTSTARTED
380 || plan->status == IO_POLLING_STARTED);
382 switch (plan->io(conn->fd.fd, &plan->arg)) {
384 if (errno == EPIPE && idle_on_epipe) {
385 plan->status = IO_UNSET;
386 backend_new_plan(conn);
392 plan->status = IO_POLLING_STARTED;
395 return next_plan(conn, plan);
397 /* IO should only return -1, 0 or 1 */
402 void io_ready(struct io_conn *conn, int pollflags)
404 if (pollflags & POLLIN)
405 if (!do_plan(conn, &conn->plan[IO_IN], false))
408 if (pollflags & POLLOUT)
409 /* If we're writing to a closed pipe, we need to wait for
410 * read to fail if we're duplex: we want to drain it! */
411 do_plan(conn, &conn->plan[IO_OUT],
412 conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED
413 || conn->plan[IO_IN].status == IO_POLLING_STARTED);
416 void io_do_always(struct io_conn *conn)
418 /* There's a corner case where the in next_plan wakes up the
419 * out, placing it in IO_ALWAYS and we end up processing it immediately,
420 * only to leave it in the always list.
422 * Yet we can't just process one, in case they are both supposed
423 * to be done, so grab state beforehand.
425 bool always_out = (conn->plan[IO_OUT].status == IO_ALWAYS);
427 if (conn->plan[IO_IN].status == IO_ALWAYS)
428 if (!next_plan(conn, &conn->plan[IO_IN]))
432 /* You can't *unalways* a conn (except by freeing, in which
433 * case next_plan() returned false */
434 assert(conn->plan[IO_OUT].status == IO_ALWAYS);
435 next_plan(conn, &conn->plan[IO_OUT]);
439 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
441 struct io_plan *plan = &conn->plan[dir];
443 assert(plan->status == IO_WAITING);
445 set_always(conn, dir, plan->next, plan->next_arg);
448 /* Close the connection, we're done. */
449 struct io_plan *io_close(struct io_conn *conn)
452 return &io_conn_freed;
455 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
457 return io_close(conn);
460 struct io_plan *io_close_taken_fd(struct io_conn *conn)
462 io_fd_block(conn->fd.fd, true);
464 cleanup_conn_without_close(conn);
465 return io_close(conn);
468 /* Exit the loop, returning this (non-NULL) arg. */
469 void io_break(const void *ret)
472 io_loop_return = (void *)ret;
475 struct io_plan *io_never(struct io_conn *conn, void *unused)
477 return io_always(conn, io_never_called, NULL);
480 int io_conn_fd(const struct io_conn *conn)
485 struct io_plan *io_duplex(struct io_conn *conn,
486 struct io_plan *in_plan, struct io_plan *out_plan)
488 assert(conn == container_of(in_plan, struct io_conn, plan[IO_IN]));
489 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
490 assert(out_plan == in_plan + 1);
494 struct io_plan *io_halfclose(struct io_conn *conn)
496 /* Both unset? OK. */
497 if (conn->plan[IO_IN].status == IO_UNSET
498 && conn->plan[IO_OUT].status == IO_UNSET)
499 return io_close(conn);
501 /* We leave this unset then. */
502 if (conn->plan[IO_IN].status == IO_UNSET)
503 return &conn->plan[IO_IN];
505 return &conn->plan[IO_OUT];
508 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
509 int (*io)(int fd, struct io_plan_arg *arg),
510 struct io_plan *(*next)(struct io_conn *, void *),
513 struct io_plan *plan = &conn->plan[dir];
517 plan->next_arg = next_arg;
518 assert(next != NULL);
523 bool io_plan_in_started(const struct io_conn *conn)
525 return conn->plan[IO_IN].status == IO_POLLING_STARTED;
528 bool io_plan_out_started(const struct io_conn *conn)
530 return conn->plan[IO_OUT].status == IO_POLLING_STARTED;
533 bool io_flush_sync(struct io_conn *conn)
535 struct io_plan *plan = &conn->plan[IO_OUT];
538 /* Not writing? Nothing to do. */
539 if (plan->status != IO_POLLING_STARTED
540 && plan->status != IO_POLLING_NOTSTARTED)
543 /* Synchronous please. */
544 io_fd_block(io_conn_fd(conn), true);
547 switch (plan->io(conn->fd.fd, &plan->arg)) {
551 /* Incomplete, try again. */
553 plan->status = IO_POLLING_STARTED;
557 /* In case they come back. */
558 set_always(conn, IO_OUT, plan->next, plan->next_arg);
561 /* IO should only return -1, 0 or 1 */
565 io_fd_block(io_conn_fd(conn), false);