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 /* In case they call io_duplex, clear our poll flags so
104 * both sides don't seem to be both doing read or write
105 * (See assert(!mask || pfd->events != mask) in poll.c) */
106 conn->plan.pollflag = 0;
107 conn->plan.next(conn, conn->plan.next_arg);
113 /* Normally-invalid value, used for sanity check. */
117 static void debug_io_wake(struct io_conn *conn)
119 /* We want linear if we wake a debugged connection, too. */
120 if (io_debug_conn && io_debug_conn(conn))
121 io_debug_wakeup = true;
124 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
125 static void io_plan_debug_again(void)
127 io_plan_nodebug = false;
130 static void debug_io_wake(struct io_conn *conn)
133 static void io_plan_debug_again(void)
138 struct io_listener *io_new_listener_(int fd,
139 void (*init)(int fd, void *arg),
142 struct io_listener *l = io_alloc.alloc(sizeof(*l));
147 l->fd.listener = true;
151 if (!add_listener(l)) {
158 void io_close_listener(struct io_listener *l)
165 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
167 struct io_conn *conn = io_alloc.alloc(sizeof(*conn));
169 io_plan_debug_again();
174 conn->fd.listener = false;
178 conn->finish_arg = NULL;
180 conn->timeout = NULL;
181 if (!add_conn(conn)) {
188 void io_set_finish_(struct io_conn *conn,
189 void (*finish)(struct io_conn *, void *),
192 conn->finish = finish;
193 conn->finish_arg = arg;
196 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
198 struct io_conn *conn;
200 io_plan_debug_again();
202 assert(!old->duplex);
204 conn = io_alloc.alloc(sizeof(*conn));
208 conn->fd.listener = false;
209 conn->fd.fd = old->fd.fd;
213 conn->finish_arg = NULL;
214 conn->timeout = NULL;
215 if (!add_duplex(conn)) {
223 bool io_timeout_(struct io_conn *conn, struct timespec ts,
224 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
228 if (!conn->timeout) {
229 conn->timeout = io_alloc.alloc(sizeof(*conn->timeout));
233 assert(!timeout_active(conn));
235 conn->timeout->next = cb;
236 conn->timeout->next_arg = arg;
237 backend_add_timeout(conn, ts);
241 /* Returns true if we're finished. */
242 static int do_write(int fd, struct io_plan *plan)
244 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
246 return io_debug_io(-1);
250 return io_debug_io(plan->u2.s == 0);
253 /* Queue some data to be written. */
254 struct io_plan io_write_(const void *data, size_t len,
255 struct io_plan (*cb)(struct io_conn *, void *),
261 plan.u1.const_vp = data;
266 plan.pollflag = POLLOUT;
271 static int do_read(int fd, struct io_plan *plan)
273 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
275 return io_debug_io(-1);
279 return io_debug_io(plan->u2.s == 0);
282 /* Queue a request to read into a buffer. */
283 struct io_plan io_read_(void *data, size_t len,
284 struct io_plan (*cb)(struct io_conn *, void *),
295 plan.pollflag = POLLIN;
300 static int do_read_partial(int fd, struct io_plan *plan)
302 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
304 return io_debug_io(-1);
306 *(size_t *)plan->u2.vp = ret;
307 return io_debug_io(1);
310 /* Queue a partial request to read into a buffer. */
311 struct io_plan io_read_partial_(void *data, size_t *len,
312 struct io_plan (*cb)(struct io_conn *, void *),
320 plan.io = do_read_partial;
323 plan.pollflag = POLLIN;
328 static int do_write_partial(int fd, struct io_plan *plan)
330 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
332 return io_debug_io(-1);
334 *(size_t *)plan->u2.vp = ret;
335 return io_debug_io(1);
338 /* Queue a partial write request. */
339 struct io_plan io_write_partial_(const void *data, size_t *len,
340 struct io_plan (*cb)(struct io_conn*, void *),
346 plan.u1.const_vp = data;
348 plan.io = do_write_partial;
351 plan.pollflag = POLLOUT;
356 static int already_connected(int fd, struct io_plan *plan)
358 return io_debug_io(1);
361 static int do_connect(int fd, struct io_plan *plan)
364 socklen_t len = sizeof(err);
366 /* Has async connect finished? */
367 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
372 /* Restore blocking if it was initially. */
373 fcntl(fd, F_SETFD, plan->u1.s);
379 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
380 struct io_plan (*cb)(struct io_conn*, void *),
390 /* Save old flags, set nonblock if not already. */
391 plan.u1.s = fcntl(fd, F_GETFD);
392 fcntl(fd, F_SETFD, plan.u1.s | O_NONBLOCK);
394 /* Immediate connect can happen. */
395 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
396 /* Dummy will be called immediately. */
397 plan.pollflag = POLLOUT;
398 plan.io = already_connected;
400 if (errno != EINPROGRESS)
403 plan.pollflag = POLLIN;
404 plan.io = do_connect;
409 struct io_plan io_idle_(void)
415 /* Never called (overridden by io_wake), but NULL means closing */
416 plan.next = (void *)io_idle_;
421 bool io_is_idle(const struct io_conn *conn)
423 return conn->plan.io == NULL;
426 void io_wake_(struct io_conn *conn, struct io_plan plan)
429 io_plan_debug_again();
431 /* It might be closing, but we haven't called its finish() yet. */
432 if (!conn->plan.next)
434 /* It was idle, right? */
435 assert(!conn->plan.io);
437 backend_plan_changed(conn);
442 void io_ready(struct io_conn *conn)
445 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
446 case -1: /* Failure means a new plan: close up. */
447 conn->plan = io_close();
448 backend_plan_changed(conn);
450 case 0: /* Keep going with plan. */
452 case 1: /* Done: get next plan. */
453 if (timeout_active(conn))
454 backend_del_timeout(conn);
455 /* In case they call io_duplex, clear our poll flags so
456 * both sides don't seem to be both doing read or write
457 * (See assert(!mask || pfd->events != mask) in poll.c) */
458 conn->plan.pollflag = 0;
459 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
460 backend_plan_changed(conn);
464 /* If it closed, close duplex if not already */
465 if (!conn->plan.next && conn->duplex && conn->duplex->plan.next) {
466 set_current(conn->duplex);
467 conn->duplex->plan = io_close();
468 backend_plan_changed(conn->duplex);
473 /* Close the connection, we're done. */
474 struct io_plan io_close_(void)
479 /* This means we're closing. */
486 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
491 /* Exit the loop, returning this (non-NULL) arg. */
492 struct io_plan io_break_(void *ret, struct io_plan plan)
494 io_plan_debug_again();
497 io_loop_return = ret;
502 int io_conn_fd(const struct io_conn *conn)
507 void io_set_alloc(void *(*allocfn)(size_t size),
508 void *(*reallocfn)(void *ptr, size_t size),
509 void (*freefn)(void *ptr))
511 io_alloc.alloc = allocfn;
512 io_alloc.realloc = reallocfn;
513 io_alloc.free = freefn;