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_listener *io_new_listener_(const tal_t *ctx, int fd,
18 struct io_plan *(*init)(struct io_conn *,
22 struct io_listener *l = tal(ctx, struct io_listener);
26 l->fd.listener = true;
36 void io_close_listener(struct io_listener *l)
43 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
48 static void next_plan(struct io_conn *conn, struct io_plan *plan)
50 struct io_plan *(*next)(struct io_conn *, void *arg);
54 plan->status = IO_UNSET;
56 plan->next = io_never_called;
58 plan = next(conn, plan->next_arg);
60 /* It should have set a plan inside this conn (or duplex) */
61 assert(plan == &conn->plan[IO_IN]
62 || plan == &conn->plan[IO_OUT]
63 || plan == &conn->plan[2]);
64 assert(conn->plan[IO_IN].status != IO_UNSET
65 || conn->plan[IO_OUT].status != IO_UNSET);
67 backend_new_plan(conn);
70 static void set_blocking(int fd, bool block)
72 int flags = fcntl(fd, F_GETFL);
79 fcntl(fd, F_SETFL, flags);
82 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
83 struct io_plan *(*init)(struct io_conn *, void *),
86 struct io_conn *conn = tal(ctx, struct io_conn);
91 conn->fd.listener = false;
94 conn->finish_arg = NULL;
99 return tal_free(conn);
101 /* Keep our I/O async. */
102 set_blocking(fd, false);
104 /* We start with out doing nothing, and in doing our init. */
105 conn->plan[IO_OUT].status = IO_UNSET;
107 conn->plan[IO_IN].next = init;
108 conn->plan[IO_IN].next_arg = arg;
109 next_plan(conn, &conn->plan[IO_IN]);
114 void io_set_finish_(struct io_conn *conn,
115 void (*finish)(struct io_conn *, void *),
118 conn->finish = finish;
119 conn->finish_arg = arg;
122 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
124 assert(conn->plan[dir].status == IO_UNSET);
126 conn->plan[dir].status = IO_POLLING;
127 return &conn->plan[dir].arg;
130 static struct io_plan *set_always(struct io_conn *conn,
131 enum io_direction dir,
132 struct io_plan *(*next)(struct io_conn *,
136 struct io_plan *plan = &conn->plan[dir];
138 plan->status = IO_ALWAYS;
139 backend_new_always(conn);
140 return io_set_plan(conn, dir, NULL, next, arg);
143 static struct io_plan *io_always_dir(struct io_conn *conn,
144 enum io_direction dir,
145 struct io_plan *(*next)(struct io_conn *,
149 return set_always(conn, dir, next, arg);
152 struct io_plan *io_always_(struct io_conn *conn,
153 struct io_plan *(*next)(struct io_conn *, void *),
156 return io_always_dir(conn, IO_IN, next, arg);
159 struct io_plan *io_out_always_(struct io_conn *conn,
160 struct io_plan *(*next)(struct io_conn *,
164 return io_always_dir(conn, IO_OUT, next, arg);
167 static int do_write(int fd, struct io_plan_arg *arg)
169 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
175 return arg->u2.s == 0;
178 /* Queue some data to be written. */
179 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
180 struct io_plan *(*next)(struct io_conn *, void *),
183 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
186 return set_always(conn, IO_OUT, next, next_arg);
188 arg->u1.const_vp = data;
191 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
194 static int do_read(int fd, struct io_plan_arg *arg)
196 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
202 return arg->u2.s == 0;
205 /* Queue a request to read into a buffer. */
206 struct io_plan *io_read_(struct io_conn *conn,
207 void *data, size_t len,
208 struct io_plan *(*next)(struct io_conn *, void *),
211 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
214 return set_always(conn, IO_IN, next, next_arg);
219 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
222 static int do_read_partial(int fd, struct io_plan_arg *arg)
224 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
228 *(size_t *)arg->u2.vp = ret;
232 /* Queue a partial request to read into a buffer. */
233 struct io_plan *io_read_partial_(struct io_conn *conn,
234 void *data, size_t maxlen, size_t *len,
235 struct io_plan *(*next)(struct io_conn *,
239 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
242 return set_always(conn, IO_IN, next, next_arg);
245 /* We store the max len in here temporarily. */
249 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
252 static int do_write_partial(int fd, struct io_plan_arg *arg)
254 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
258 *(size_t *)arg->u2.vp = ret;
262 /* Queue a partial write request. */
263 struct io_plan *io_write_partial_(struct io_conn *conn,
264 const void *data, size_t maxlen, size_t *len,
265 struct io_plan *(*next)(struct io_conn *,
269 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
272 return set_always(conn, IO_OUT, next, next_arg);
274 arg->u1.const_vp = data;
275 /* We store the max len in here temporarily. */
279 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
282 static int do_connect(int fd, struct io_plan_arg *arg)
285 socklen_t len = sizeof(err);
287 /* Has async connect finished? */
288 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
294 } else if (err == EINPROGRESS)
301 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
302 struct io_plan *(*next)(struct io_conn *, void *),
305 int fd = io_conn_fd(conn);
307 /* We don't actually need the arg, but we need it polling. */
308 io_plan_arg(conn, IO_OUT);
310 /* Note that io_new_conn() will make fd O_NONBLOCK */
312 /* Immediate connect can happen. */
313 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
314 return set_always(conn, IO_OUT, next, next_arg);
316 if (errno != EINPROGRESS)
317 return io_close(conn);
319 return io_set_plan(conn, IO_OUT, do_connect, next, next_arg);
322 static struct io_plan *io_wait_dir(struct io_conn *conn,
324 enum io_direction dir,
325 struct io_plan *(*next)(struct io_conn *,
329 struct io_plan_arg *arg = io_plan_arg(conn, dir);
330 arg->u1.const_vp = wait;
332 conn->plan[dir].status = IO_WAITING;
334 return io_set_plan(conn, dir, NULL, next, next_arg);
337 struct io_plan *io_wait_(struct io_conn *conn,
339 struct io_plan *(*next)(struct io_conn *, void *),
342 return io_wait_dir(conn, wait, IO_IN, next, next_arg);
345 struct io_plan *io_out_wait_(struct io_conn *conn,
347 struct io_plan *(*next)(struct io_conn *, void *),
350 return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
353 void io_wake(const void *wait)
358 static int do_plan(struct io_conn *conn, struct io_plan *plan)
360 /* Someone else might have called io_close() on us. */
361 if (plan->status == IO_CLOSING)
364 /* We shouldn't have polled for this event if this wasn't true! */
365 assert(plan->status == IO_POLLING);
367 switch (plan->io(conn->fd.fd, &plan->arg)) {
374 next_plan(conn, plan);
377 /* IO should only return -1, 0 or 1 */
382 void io_ready(struct io_conn *conn, int pollflags)
384 if (pollflags & POLLIN)
385 do_plan(conn, &conn->plan[IO_IN]);
387 if (pollflags & POLLOUT)
388 do_plan(conn, &conn->plan[IO_OUT]);
391 void io_do_always(struct io_conn *conn)
393 if (conn->plan[IO_IN].status == IO_ALWAYS)
394 next_plan(conn, &conn->plan[IO_IN]);
396 if (conn->plan[IO_OUT].status == IO_ALWAYS)
397 next_plan(conn, &conn->plan[IO_OUT]);
400 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
402 struct io_plan *plan = &conn->plan[dir];
404 assert(plan->status == IO_WAITING);
406 set_always(conn, dir, plan->next, plan->next_arg);
409 /* Close the connection, we're done. */
410 struct io_plan *io_close(struct io_conn *conn)
412 /* Already closing? Don't close twice. */
413 if (conn->plan[IO_IN].status == IO_CLOSING)
414 return &conn->plan[IO_IN];
416 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
417 conn->plan[IO_IN].arg.u1.s = errno;
418 backend_new_closing(conn);
420 return io_set_plan(conn, IO_IN, NULL, NULL, NULL);
423 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
425 return io_close(conn);
428 /* Exit the loop, returning this (non-NULL) arg. */
429 void io_break(const void *ret)
432 io_loop_return = (void *)ret;
435 struct io_plan *io_never(struct io_conn *conn, void *unused)
437 return io_always(conn, io_never_called, NULL);
440 int io_conn_fd(const struct io_conn *conn)
445 void io_duplex_prepare(struct io_conn *conn)
447 assert(conn->plan[IO_IN].status == IO_UNSET);
448 assert(conn->plan[IO_OUT].status == IO_UNSET);
450 /* We can't sync debug until we've set both: io_wait() and io_always
451 * can't handle it. */
452 conn->debug_saved = conn->debug;
453 io_set_debug(conn, false);
456 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
458 struct io_conn *conn;
460 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
461 assert(out_plan == in_plan + 1);
464 conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
465 io_set_debug(conn, conn->debug_saved);
467 /* Now set the plans again, to invoke sync debug. */
468 io_set_plan(conn, IO_OUT,
469 out_plan->io, out_plan->next, out_plan->next_arg);
470 io_set_plan(conn, IO_IN,
471 in_plan->io, in_plan->next, in_plan->next_arg);
476 struct io_plan *io_halfclose(struct io_conn *conn)
478 /* Already closing? Don't close twice. */
479 if (conn->plan[IO_IN].status == IO_CLOSING)
480 return &conn->plan[IO_IN];
482 /* Both unset? OK. */
483 if (conn->plan[IO_IN].status == IO_UNSET
484 && conn->plan[IO_OUT].status == IO_UNSET)
485 return io_close(conn);
487 /* We leave this unset then. */
488 if (conn->plan[IO_IN].status == IO_UNSET)
489 return &conn->plan[IO_IN];
491 return &conn->plan[IO_OUT];
494 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
495 int (*io)(int fd, struct io_plan_arg *arg),
496 struct io_plan *(*next)(struct io_conn *, void *),
499 struct io_plan *plan = &conn->plan[dir];
503 plan->next_arg = next_arg;
504 assert(plan->status == IO_CLOSING || next != NULL);
509 if (io_loop_return) {
510 io_debug_complete(conn);
514 switch (plan->status) {
516 while (do_plan(conn, plan) == 0);
518 /* Shouldn't happen, since you said you did plan! */
522 /* If other one is ALWAYS, leave in list! */
523 if (conn->plan[!dir].status != IO_ALWAYS)
524 remove_from_always(conn);
525 next_plan(conn, plan);
529 io_debug_complete(conn);
535 void io_set_debug(struct io_conn *conn, bool debug)
539 /* Debugging means fds must block. */
540 set_blocking(io_conn_fd(conn), debug);
543 void io_debug_complete(struct io_conn *conn)