1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
18 /* Set to skip the next plan. */
20 /* The current connection to apply plan to. */
21 struct io_conn *current;
22 /* User-defined function to select which connection(s) to debug. */
23 bool (*io_debug_conn)(struct io_conn *conn);
24 /* Set when we wake up an connection we are debugging. */
27 struct io_plan io_debug(struct io_plan plan)
29 struct io_conn *ready = NULL;
31 if (io_plan_nodebug) {
32 io_plan_nodebug = false;
36 if (!current || !doing_debug_on(current)) {
41 io_debug_wakeup = false;
43 backend_plan_changed(current);
45 /* Call back into the loop immediately. */
46 io_loop_return = do_io_loop(&ready);
50 if (!ready->plan.next) {
51 /* Call finish function immediately. */
53 errno = ready->plan.u.close.saved_errno;
54 ready->finish(ready, ready->finish_arg);
57 backend_del_conn(ready);
59 /* Calls back in itself, via io_debug_io(). */
60 if (ready->plan.io(ready->fd.fd, &ready->plan) != 2)
66 /* Return a do-nothing plan, so backend_plan_changed in
67 * io_ready doesn't do anything (it's already been called). */
71 int io_debug_io(int ret)
73 /* Cache it for debugging; current changes. */
74 struct io_conn *conn = current;
75 int saved_errno = errno;
77 if (!doing_debug_on(conn))
80 /* These will all go linearly through the io_debug() path above. */
83 /* This will call io_debug above. */
87 case 0: /* Keep going with plan. */
90 case 1: /* Done: get next plan. */
91 if (timeout_active(conn))
92 backend_del_timeout(conn);
93 conn->plan.next(conn, conn->plan.next_arg);
99 /* Normally-invalid value, used for sanity check. */
103 static void debug_io_wake(struct io_conn *conn)
105 /* We want linear if we wake a debugged connection, too. */
106 if (io_debug_conn && io_debug_conn(conn))
107 io_debug_wakeup = true;
110 /* Counterpart to io_plan_no_debug(), called in macros in io.h */
111 static void io_plan_debug_again(void)
113 io_plan_nodebug = false;
116 static void debug_io_wake(struct io_conn *conn)
119 static void io_plan_debug_again(void)
124 struct io_listener *io_new_listener_(int fd,
125 void (*init)(int fd, void *arg),
128 struct io_listener *l = malloc(sizeof(*l));
133 l->fd.listener = true;
137 if (!add_listener(l)) {
144 void io_close_listener(struct io_listener *l)
151 struct io_conn *io_new_conn_(int fd, struct io_plan plan)
153 struct io_conn *conn = malloc(sizeof(*conn));
155 io_plan_debug_again();
160 conn->fd.listener = false;
164 conn->finish_arg = NULL;
166 conn->timeout = NULL;
167 if (!add_conn(conn)) {
174 void io_set_finish_(struct io_conn *conn,
175 void (*finish)(struct io_conn *, void *),
178 conn->finish = finish;
179 conn->finish_arg = arg;
182 struct io_conn *io_duplex_(struct io_conn *old, struct io_plan plan)
184 struct io_conn *conn;
186 io_plan_debug_again();
188 assert(!old->duplex);
190 conn = malloc(sizeof(*conn));
194 conn->fd.listener = false;
195 conn->fd.fd = old->fd.fd;
199 conn->finish_arg = NULL;
200 conn->timeout = NULL;
201 if (!add_duplex(conn)) {
209 bool io_timeout_(struct io_conn *conn, struct timespec ts,
210 struct io_plan (*cb)(struct io_conn *, void *), void *arg)
214 if (!conn->timeout) {
215 conn->timeout = malloc(sizeof(*conn->timeout));
219 assert(!timeout_active(conn));
221 conn->timeout->next = cb;
222 conn->timeout->next_arg = arg;
223 backend_add_timeout(conn, ts);
227 /* Returns true if we're finished. */
228 static int do_write(int fd, struct io_plan *plan)
230 ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
232 return io_debug_io(-1);
234 plan->u.write.buf += ret;
235 plan->u.write.len -= ret;
236 return io_debug_io(plan->u.write.len == 0);
239 /* Queue some data to be written. */
240 struct io_plan io_write_(const void *data, size_t len,
241 struct io_plan (*cb)(struct io_conn *, void *),
247 plan.u.write.buf = data;
248 plan.u.write.len = len;
252 plan.pollflag = POLLOUT;
257 static int do_read(int fd, struct io_plan *plan)
259 ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
261 return io_debug_io(-1);
263 plan->u.read.buf += ret;
264 plan->u.read.len -= ret;
265 return io_debug_io(plan->u.read.len == 0);
268 /* Queue a request to read into a buffer. */
269 struct io_plan io_read_(void *data, size_t len,
270 struct io_plan (*cb)(struct io_conn *, void *),
276 plan.u.read.buf = data;
277 plan.u.read.len = len;
281 plan.pollflag = POLLIN;
286 static int do_read_partial(int fd, struct io_plan *plan)
288 ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
290 return io_debug_io(-1);
292 *plan->u.readpart.lenp = ret;
293 return io_debug_io(1);
296 /* Queue a partial request to read into a buffer. */
297 struct io_plan io_read_partial_(void *data, size_t *len,
298 struct io_plan (*cb)(struct io_conn *, void *),
304 plan.u.readpart.buf = data;
305 plan.u.readpart.lenp = len;
306 plan.io = do_read_partial;
309 plan.pollflag = POLLIN;
314 static int do_write_partial(int fd, struct io_plan *plan)
316 ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
318 return io_debug_io(-1);
320 *plan->u.writepart.lenp = ret;
321 return io_debug_io(1);
324 /* Queue a partial write request. */
325 struct io_plan io_write_partial_(const void *data, size_t *len,
326 struct io_plan (*cb)(struct io_conn*, void *),
332 plan.u.writepart.buf = data;
333 plan.u.writepart.lenp = len;
334 plan.io = do_write_partial;
337 plan.pollflag = POLLOUT;
342 static int already_connected(int fd, struct io_plan *plan)
344 return io_debug_io(1);
347 static int do_connect(int fd, struct io_plan *plan)
350 socklen_t len = sizeof(err);
352 /* Has async connect finished? */
353 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
358 /* Restore blocking if it was initially. */
359 fcntl(fd, F_SETFD, plan->u.len_len.len1);
365 struct io_plan io_connect_(int fd, const struct addrinfo *addr,
366 struct io_plan (*cb)(struct io_conn*, void *),
376 /* Save old flags, set nonblock if not already. */
377 plan.u.len_len.len1 = fcntl(fd, F_GETFD);
378 fcntl(fd, F_SETFD, plan.u.len_len.len1 | O_NONBLOCK);
380 /* Immediate connect can happen. */
381 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
382 /* Dummy will be called immediately. */
383 plan.pollflag = POLLOUT;
384 plan.io = already_connected;
386 if (errno != EINPROGRESS)
389 plan.pollflag = POLLIN;
390 plan.io = do_connect;
395 struct io_plan io_idle_(void)
401 /* Never called (overridden by io_wake), but NULL means closing */
402 plan.next = (void *)io_idle_;
407 void io_wake_(struct io_conn *conn, struct io_plan plan)
410 io_plan_debug_again();
412 /* It might be closing, but we haven't called its finish() yet. */
413 if (!conn->plan.next)
415 /* It was idle, right? */
416 assert(!conn->plan.io);
418 backend_plan_changed(conn);
423 void io_ready(struct io_conn *conn)
426 switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
427 case -1: /* Failure means a new plan: close up. */
428 conn->plan = io_close();
429 backend_plan_changed(conn);
431 case 0: /* Keep going with plan. */
433 case 1: /* Done: get next plan. */
434 if (timeout_active(conn))
435 backend_del_timeout(conn);
436 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
437 backend_plan_changed(conn);
442 /* Close the connection, we're done. */
443 struct io_plan io_close_(void)
448 /* This means we're closing. */
450 plan.u.close.saved_errno = errno;
455 struct io_plan io_close_cb(struct io_conn *conn, void *arg)
460 /* Exit the loop, returning this (non-NULL) arg. */
461 struct io_plan io_break_(void *ret, struct io_plan plan)
463 io_plan_debug_again();
466 io_loop_return = ret;