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 /* If it closed, close duplex. */
50 if (!current->plan.next && current->duplex) {
51 current->duplex->plan = io_close_();
52 backend_plan_changed(current->duplex);
55 /* Call back into the loop immediately. */
56 io_loop_return = do_io_loop(&ready);
60 if (!ready->plan.next) {
61 /* Call finish function immediately. */
63 errno = ready->plan.u1.s;
64 ready->finish(ready, ready->finish_arg);
67 backend_del_conn(ready);
69 /* Calls back in itself, via io_debug_io(). */
70 if (ready->plan.io(ready->fd.fd, &ready->plan) != 2)
76 /* Return a do-nothing plan, so backend_plan_changed in
77 * io_ready doesn't do anything (it's already been called). */
81 int io_debug_io(int ret)
83 /* Cache it for debugging; current changes. */
84 struct io_conn *conn = current;
85 int saved_errno = errno;
87 if (!doing_debug_on(conn))
90 /* These will all go linearly through the io_debug() path above. */
93 /* This will call io_debug above. */
97 case 0: /* Keep going with plan. */
100 case 1: /* Done: get next plan. */
101 if (timeout_active(conn))
102 backend_del_timeout(conn);
103 conn->plan.next(conn, conn->plan.next_arg);
109 /* Normally-invalid value, used for sanity check. */
113 static void debug_io_wake(struct io_conn *conn)
115 /* We want linear if we wake a debugged connection, too. */
116 if (io_debug_conn && io_debug_conn(conn))
117 io_debug_wakeup = true;
120 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
121 static void io_plan_debug_again(void)
123 io_plan_nodebug = false;
126 static void debug_io_wake(struct io_conn *conn)
129 static void io_plan_debug_again(void)
134 struct io_listener *io_new_listener_(int fd,
135 void (*init)(int fd, void *arg),
138 struct io_listener *l = io_alloc.alloc(sizeof(*l));
143 l->fd.listener = true;
147 if (!add_listener(l)) {
154 void io_close_listener(struct io_listener *l)
161 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
163 struct io_conn *conn = io_alloc.alloc(sizeof(*conn));
165 io_plan_debug_again();
170 conn->fd.listener = false;
174 conn->finish_arg = NULL;
176 conn->timeout = NULL;
177 if (!add_conn(conn)) {
184 void io_set_finish_(struct io_conn *conn,
185 void (*finish)(struct io_conn *, void *),
188 conn->finish = finish;
189 conn->finish_arg = arg;
192 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
194 struct io_conn *conn;
196 io_plan_debug_again();
198 assert(!old->duplex);
200 conn = io_alloc.alloc(sizeof(*conn));
204 conn->fd.listener = false;
205 conn->fd.fd = old->fd.fd;
209 conn->finish_arg = NULL;
210 conn->timeout = NULL;
211 if (!add_duplex(conn)) {
219 bool io_timeout_(struct io_conn *conn, struct timespec ts,
220 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
224 if (!conn->timeout) {
225 conn->timeout = io_alloc.alloc(sizeof(*conn->timeout));
229 assert(!timeout_active(conn));
231 conn->timeout->next = cb;
232 conn->timeout->next_arg = arg;
233 backend_add_timeout(conn, ts);
237 /* Returns true if we're finished. */
238 static int do_write(int fd, struct io_plan *plan)
240 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
242 return io_debug_io(-1);
246 return io_debug_io(plan->u2.s == 0);
249 /* Queue some data to be written. */
250 struct io_plan io_write_(const void *data, size_t len,
251 struct io_plan (*cb)(struct io_conn *, void *),
257 plan.u1.const_vp = data;
262 plan.pollflag = POLLOUT;
267 static int do_read(int fd, struct io_plan *plan)
269 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
271 return io_debug_io(-1);
275 return io_debug_io(plan->u2.s == 0);
278 /* Queue a request to read into a buffer. */
279 struct io_plan io_read_(void *data, size_t len,
280 struct io_plan (*cb)(struct io_conn *, void *),
291 plan.pollflag = POLLIN;
296 static int do_read_partial(int fd, struct io_plan *plan)
298 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
300 return io_debug_io(-1);
302 *(size_t *)plan->u2.vp = ret;
303 return io_debug_io(1);
306 /* Queue a partial request to read into a buffer. */
307 struct io_plan io_read_partial_(void *data, size_t *len,
308 struct io_plan (*cb)(struct io_conn *, void *),
316 plan.io = do_read_partial;
319 plan.pollflag = POLLIN;
324 static int do_write_partial(int fd, struct io_plan *plan)
326 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
328 return io_debug_io(-1);
330 *(size_t *)plan->u2.vp = ret;
331 return io_debug_io(1);
334 /* Queue a partial write request. */
335 struct io_plan io_write_partial_(const void *data, size_t *len,
336 struct io_plan (*cb)(struct io_conn*, void *),
342 plan.u1.const_vp = data;
344 plan.io = do_write_partial;
347 plan.pollflag = POLLOUT;
352 static int already_connected(int fd, struct io_plan *plan)
354 return io_debug_io(1);
357 static int do_connect(int fd, struct io_plan *plan)
360 socklen_t len = sizeof(err);
362 /* Has async connect finished? */
363 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
368 /* Restore blocking if it was initially. */
369 fcntl(fd, F_SETFD, plan->u1.s);
375 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
376 struct io_plan (*cb)(struct io_conn*, void *),
386 /* Save old flags, set nonblock if not already. */
387 plan.u1.s = fcntl(fd, F_GETFD);
388 fcntl(fd, F_SETFD, plan.u1.s | O_NONBLOCK);
390 /* Immediate connect can happen. */
391 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
392 /* Dummy will be called immediately. */
393 plan.pollflag = POLLOUT;
394 plan.io = already_connected;
396 if (errno != EINPROGRESS)
399 plan.pollflag = POLLIN;
400 plan.io = do_connect;
405 struct io_plan io_idle_(void)
411 /* Never called (overridden by io_wake), but NULL means closing */
412 plan.next = (void *)io_idle_;
417 void io_wake_(struct io_conn *conn, struct io_plan plan)
420 io_plan_debug_again();
422 /* It might be closing, but we haven't called its finish() yet. */
423 if (!conn->plan.next)
425 /* It was idle, right? */
426 assert(!conn->plan.io);
428 backend_plan_changed(conn);
433 void io_ready(struct io_conn *conn)
436 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
437 case -1: /* Failure means a new plan: close up. */
438 conn->plan = io_close();
439 backend_plan_changed(conn);
441 case 0: /* Keep going with plan. */
443 case 1: /* Done: get next plan. */
444 if (timeout_active(conn))
445 backend_del_timeout(conn);
446 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
447 backend_plan_changed(conn);
451 /* If it closed, close duplex. */
452 if (!conn->plan.next && conn->duplex) {
453 set_current(conn->duplex);
454 conn->duplex->plan = io_close();
455 backend_plan_changed(conn->duplex);
460 /* Close the connection, we're done. */
461 struct io_plan io_close_(void)
466 /* This means we're closing. */
473 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
478 /* Exit the loop, returning this (non-NULL) arg. */
479 struct io_plan io_break_(void *ret, struct io_plan plan)
481 io_plan_debug_again();
484 io_loop_return = ret;
489 int io_conn_fd(const struct io_conn *conn)
494 void io_set_alloc(void *(*allocfn)(size_t size),
495 void *(*reallocfn)(void *ptr, size_t size),
496 void (*freefn)(void *ptr))
498 io_alloc.alloc = allocfn;
499 io_alloc.realloc = reallocfn;
500 io_alloc.free = freefn;