1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
17 struct io_alloc io_alloc = {
22 /* Set to skip the next plan. */
24 /* The current connection to apply plan to. */
25 struct io_conn *current;
26 /* User-defined function to select which connection(s) to debug. */
27 bool (*io_debug_conn)(struct io_conn *conn);
28 /* Set when we wake up an connection we are debugging. */
31 struct io_plan io_debug(struct io_plan plan)
33 struct io_conn *ready = NULL;
35 if (io_plan_nodebug) {
36 io_plan_nodebug = false;
40 if (!current || !doing_debug_on(current)) {
45 io_debug_wakeup = false;
47 backend_plan_changed(current);
49 /* Call back into the loop immediately. */
50 io_loop_return = do_io_loop(&ready);
54 if (!ready->plan.next) {
55 /* Call finish function immediately. */
57 errno = ready->plan.u1.s;
58 ready->finish(ready, ready->finish_arg);
61 backend_del_conn(ready);
63 /* Calls back in itself, via io_debug_io(). */
64 if (ready->plan.io(ready->fd.fd, &ready->plan) != 2)
70 /* Return a do-nothing plan, so backend_plan_changed in
71 * io_ready doesn't do anything (it's already been called). */
75 int io_debug_io(int ret)
77 /* Cache it for debugging; current changes. */
78 struct io_conn *conn = current;
79 int saved_errno = errno;
81 if (!doing_debug_on(conn))
84 /* These will all go linearly through the io_debug() path above. */
87 /* This will call io_debug above. */
91 case 0: /* Keep going with plan. */
94 case 1: /* Done: get next plan. */
95 if (timeout_active(conn))
96 backend_del_timeout(conn);
97 /* In case they call io_duplex, clear our poll flags so
98 * both sides don't seem to be both doing read or write
99 * (See assert(!mask || pfd->events != mask) in poll.c) */
100 conn->plan.pollflag = 0;
101 conn->plan.next(conn, conn->plan.next_arg);
107 /* Normally-invalid value, used for sanity check. */
111 static void debug_io_wake(struct io_conn *conn)
113 /* We want linear if we wake a debugged connection, too. */
114 if (io_debug_conn && io_debug_conn(conn))
115 io_debug_wakeup = true;
118 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
119 static void io_plan_debug_again(void)
121 io_plan_nodebug = false;
124 static void debug_io_wake(struct io_conn *conn)
127 static void io_plan_debug_again(void)
132 struct io_listener *io_new_listener_(int fd,
133 void (*init)(int fd, void *arg),
136 struct io_listener *l = io_alloc.alloc(sizeof(*l));
141 l->fd.listener = true;
145 if (!add_listener(l)) {
152 void io_close_listener(struct io_listener *l)
159 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
161 struct io_conn *conn = io_alloc.alloc(sizeof(*conn));
163 io_plan_debug_again();
168 conn->fd.listener = false;
172 conn->finish_arg = NULL;
174 conn->timeout = NULL;
175 if (!add_conn(conn)) {
182 void io_set_finish_(struct io_conn *conn,
183 void (*finish)(struct io_conn *, void *),
186 conn->finish = finish;
187 conn->finish_arg = arg;
190 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
192 struct io_conn *conn;
194 io_plan_debug_again();
196 assert(!old->duplex);
198 conn = io_alloc.alloc(sizeof(*conn));
202 conn->fd.listener = false;
203 conn->fd.fd = old->fd.fd;
207 conn->finish_arg = NULL;
208 conn->timeout = NULL;
209 if (!add_duplex(conn)) {
217 bool io_timeout_(struct io_conn *conn, struct timespec ts,
218 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
222 if (!conn->timeout) {
223 conn->timeout = io_alloc.alloc(sizeof(*conn->timeout));
227 assert(!timeout_active(conn));
229 conn->timeout->next = cb;
230 conn->timeout->next_arg = arg;
231 backend_add_timeout(conn, ts);
235 /* Returns true if we're finished. */
236 static int do_write(int fd, struct io_plan *plan)
238 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
240 return io_debug_io(-1);
244 return io_debug_io(plan->u2.s == 0);
247 /* Queue some data to be written. */
248 struct io_plan io_write_(const void *data, size_t len,
249 struct io_plan (*cb)(struct io_conn *, void *),
255 plan.u1.const_vp = data;
260 plan.pollflag = POLLOUT;
265 static int do_read(int fd, struct io_plan *plan)
267 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
269 return io_debug_io(-1);
273 return io_debug_io(plan->u2.s == 0);
276 /* Queue a request to read into a buffer. */
277 struct io_plan io_read_(void *data, size_t len,
278 struct io_plan (*cb)(struct io_conn *, void *),
289 plan.pollflag = POLLIN;
294 static int do_read_partial(int fd, struct io_plan *plan)
296 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
298 return io_debug_io(-1);
300 *(size_t *)plan->u2.vp = ret;
301 return io_debug_io(1);
304 /* Queue a partial request to read into a buffer. */
305 struct io_plan io_read_partial_(void *data, size_t *len,
306 struct io_plan (*cb)(struct io_conn *, void *),
314 plan.io = do_read_partial;
317 plan.pollflag = POLLIN;
322 static int do_write_partial(int fd, struct io_plan *plan)
324 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
326 return io_debug_io(-1);
328 *(size_t *)plan->u2.vp = ret;
329 return io_debug_io(1);
332 /* Queue a partial write request. */
333 struct io_plan io_write_partial_(const void *data, size_t *len,
334 struct io_plan (*cb)(struct io_conn*, void *),
340 plan.u1.const_vp = data;
342 plan.io = do_write_partial;
345 plan.pollflag = POLLOUT;
350 static int already_connected(int fd, struct io_plan *plan)
352 return io_debug_io(1);
355 static int do_connect(int fd, struct io_plan *plan)
358 socklen_t len = sizeof(err);
360 /* Has async connect finished? */
361 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
366 /* Restore blocking if it was initially. */
367 fcntl(fd, F_SETFD, plan->u1.s);
373 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
374 struct io_plan (*cb)(struct io_conn*, void *),
384 /* Save old flags, set nonblock if not already. */
385 plan.u1.s = fcntl(fd, F_GETFD);
386 fcntl(fd, F_SETFD, plan.u1.s | O_NONBLOCK);
388 /* Immediate connect can happen. */
389 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
390 /* Dummy will be called immediately. */
391 plan.pollflag = POLLOUT;
392 plan.io = already_connected;
394 if (errno != EINPROGRESS)
397 plan.pollflag = POLLIN;
398 plan.io = do_connect;
403 struct io_plan io_idle_(void)
409 /* Never called (overridden by io_wake), but NULL means closing */
410 plan.next = (void *)io_idle_;
415 bool io_is_idle(const struct io_conn *conn)
417 return conn->plan.io == NULL;
420 void io_wake_(struct io_conn *conn, struct io_plan plan)
423 io_plan_debug_again();
425 /* It might be closing, but we haven't called its finish() yet. */
426 if (!conn->plan.next)
428 /* It was idle, right? */
429 assert(!conn->plan.io);
431 backend_plan_changed(conn);
436 void io_ready(struct io_conn *conn)
438 /* Beware io_close_other! */
439 if (!conn->plan.next)
443 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
444 case -1: /* Failure means a new plan: close up. */
445 conn->plan = io_close();
446 backend_plan_changed(conn);
448 case 0: /* Keep going with plan. */
450 case 1: /* Done: get next plan. */
451 if (timeout_active(conn))
452 backend_del_timeout(conn);
453 /* In case they call io_duplex, clear our poll flags so
454 * both sides don't seem to be both doing read or write
455 * (See assert(!mask || pfd->events != mask) in poll.c) */
456 conn->plan.pollflag = 0;
457 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
458 backend_plan_changed(conn);
463 /* Close the connection, we're done. */
464 struct io_plan io_close_(void)
469 /* This means we're closing. */
476 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
481 void io_close_other(struct io_conn *conn)
483 conn->plan = io_close_();
484 backend_plan_changed(conn);
487 /* Exit the loop, returning this (non-NULL) arg. */
488 struct io_plan io_break_(void *ret, struct io_plan plan)
490 io_plan_debug_again();
493 io_loop_return = ret;
498 int io_conn_fd(const struct io_conn *conn)
503 void io_set_alloc(void *(*allocfn)(size_t size),
504 void *(*reallocfn)(void *ptr, size_t size),
505 void (*freefn)(void *ptr))
507 io_alloc.alloc = allocfn;
508 io_alloc.realloc = reallocfn;
509 io_alloc.free = freefn;