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 conn->plan.next(conn, conn->plan.next_arg);
103 /* Normally-invalid value, used for sanity check. */
107 static void debug_io_wake(struct io_conn *conn)
109 /* We want linear if we wake a debugged connection, too. */
110 if (io_debug_conn && io_debug_conn(conn))
111 io_debug_wakeup = true;
114 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
115 static void io_plan_debug_again(void)
117 io_plan_nodebug = false;
120 static void debug_io_wake(struct io_conn *conn)
123 static void io_plan_debug_again(void)
128 struct io_listener *io_new_listener_(int fd,
129 void (*init)(int fd, void *arg),
132 struct io_listener *l = io_alloc.alloc(sizeof(*l));
137 l->fd.listener = true;
141 if (!add_listener(l)) {
148 void io_close_listener(struct io_listener *l)
155 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
157 struct io_conn *conn = io_alloc.alloc(sizeof(*conn));
159 io_plan_debug_again();
164 conn->fd.listener = false;
168 conn->finish_arg = NULL;
170 conn->timeout = NULL;
171 if (!add_conn(conn)) {
178 void io_set_finish_(struct io_conn *conn,
179 void (*finish)(struct io_conn *, void *),
182 conn->finish = finish;
183 conn->finish_arg = arg;
186 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
188 struct io_conn *conn;
190 io_plan_debug_again();
192 assert(!old->duplex);
194 conn = io_alloc.alloc(sizeof(*conn));
198 conn->fd.listener = false;
199 conn->fd.fd = old->fd.fd;
203 conn->finish_arg = NULL;
204 conn->timeout = NULL;
205 if (!add_duplex(conn)) {
213 bool io_timeout_(struct io_conn *conn, struct timespec ts,
214 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
218 if (!conn->timeout) {
219 conn->timeout = io_alloc.alloc(sizeof(*conn->timeout));
223 assert(!timeout_active(conn));
225 conn->timeout->next = cb;
226 conn->timeout->next_arg = arg;
227 backend_add_timeout(conn, ts);
231 /* Returns true if we're finished. */
232 static int do_write(int fd, struct io_plan *plan)
234 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
236 return io_debug_io(-1);
240 return io_debug_io(plan->u2.s == 0);
243 /* Queue some data to be written. */
244 struct io_plan io_write_(const void *data, size_t len,
245 struct io_plan (*cb)(struct io_conn *, void *),
251 plan.u1.const_vp = data;
256 plan.pollflag = POLLOUT;
261 static int do_read(int fd, struct io_plan *plan)
263 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
265 return io_debug_io(-1);
269 return io_debug_io(plan->u2.s == 0);
272 /* Queue a request to read into a buffer. */
273 struct io_plan io_read_(void *data, size_t len,
274 struct io_plan (*cb)(struct io_conn *, void *),
285 plan.pollflag = POLLIN;
290 static int do_read_partial(int fd, struct io_plan *plan)
292 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
294 return io_debug_io(-1);
296 *(size_t *)plan->u2.vp = ret;
297 return io_debug_io(1);
300 /* Queue a partial request to read into a buffer. */
301 struct io_plan io_read_partial_(void *data, size_t *len,
302 struct io_plan (*cb)(struct io_conn *, void *),
310 plan.io = do_read_partial;
313 plan.pollflag = POLLIN;
318 static int do_write_partial(int fd, struct io_plan *plan)
320 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
322 return io_debug_io(-1);
324 *(size_t *)plan->u2.vp = ret;
325 return io_debug_io(1);
328 /* Queue a partial write request. */
329 struct io_plan io_write_partial_(const void *data, size_t *len,
330 struct io_plan (*cb)(struct io_conn*, void *),
336 plan.u1.const_vp = data;
338 plan.io = do_write_partial;
341 plan.pollflag = POLLOUT;
346 static int already_connected(int fd, struct io_plan *plan)
348 return io_debug_io(1);
351 static int do_connect(int fd, struct io_plan *plan)
354 socklen_t len = sizeof(err);
356 /* Has async connect finished? */
357 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
362 /* Restore blocking if it was initially. */
363 fcntl(fd, F_SETFD, plan->u1.s);
369 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
370 struct io_plan (*cb)(struct io_conn*, void *),
380 /* Save old flags, set nonblock if not already. */
381 plan.u1.s = fcntl(fd, F_GETFD);
382 fcntl(fd, F_SETFD, plan.u1.s | O_NONBLOCK);
384 /* Immediate connect can happen. */
385 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
386 /* Dummy will be called immediately. */
387 plan.pollflag = POLLOUT;
388 plan.io = already_connected;
390 if (errno != EINPROGRESS)
393 plan.pollflag = POLLIN;
394 plan.io = do_connect;
399 struct io_plan io_idle_(void)
405 /* Never called (overridden by io_wake), but NULL means closing */
406 plan.next = (void *)io_idle_;
411 void io_wake_(struct io_conn *conn, struct io_plan plan)
414 io_plan_debug_again();
416 /* It might be closing, but we haven't called its finish() yet. */
417 if (!conn->plan.next)
419 /* It was idle, right? */
420 assert(!conn->plan.io);
422 backend_plan_changed(conn);
427 void io_ready(struct io_conn *conn)
430 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
431 case -1: /* Failure means a new plan: close up. */
432 conn->plan = io_close();
433 backend_plan_changed(conn);
435 case 0: /* Keep going with plan. */
437 case 1: /* Done: get next plan. */
438 if (timeout_active(conn))
439 backend_del_timeout(conn);
440 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
441 backend_plan_changed(conn);
446 /* Close the connection, we're done. */
447 struct io_plan io_close_(void)
452 /* This means we're closing. */
459 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
464 /* Exit the loop, returning this (non-NULL) arg. */
465 struct io_plan io_break_(void *ret, struct io_plan plan)
467 io_plan_debug_again();
470 io_loop_return = ret;
475 int io_conn_fd(const struct io_conn *conn)
480 void io_set_alloc(void *(*allocfn)(size_t size),
481 void *(*reallocfn)(void *ptr, size_t size),
482 void (*freefn)(void *ptr))
484 io_alloc.alloc = allocfn;
485 io_alloc.realloc = reallocfn;
486 io_alloc.free = freefn;