1 ce062d50 2019-08-21 martijn /* $OpenBSD: ioev.c,v 1.42 2019/06/12 17:42:53 eric Exp $ */
3 ce062d50 2019-08-21 martijn * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 ce062d50 2019-08-21 martijn * Permission to use, copy, modify, and distribute this software for any
6 ce062d50 2019-08-21 martijn * purpose with or without fee is hereby granted, provided that the above
7 ce062d50 2019-08-21 martijn * copyright notice and this permission notice appear in all copies.
9 ce062d50 2019-08-21 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ce062d50 2019-08-21 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ce062d50 2019-08-21 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ce062d50 2019-08-21 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ce062d50 2019-08-21 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ce062d50 2019-08-21 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ce062d50 2019-08-21 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 957c2372 2019-11-14 martijn #define _GNU_SOURCE 1
18 13f08f83 2019-11-14 martijn #define _BSD_SOURCE 1
20 ce062d50 2019-08-21 martijn #include <sys/types.h>
21 ce062d50 2019-08-21 martijn #include <sys/queue.h>
22 ce062d50 2019-08-21 martijn #include <sys/socket.h>
24 ce062d50 2019-08-21 martijn #include <err.h>
25 ce062d50 2019-08-21 martijn #include <errno.h>
26 ce062d50 2019-08-21 martijn #include <event.h>
27 ce062d50 2019-08-21 martijn #include <fcntl.h>
28 ce062d50 2019-08-21 martijn #include <inttypes.h>
29 ce062d50 2019-08-21 martijn #include <stdlib.h>
30 ce062d50 2019-08-21 martijn #include <string.h>
31 ce062d50 2019-08-21 martijn #include <stdio.h>
32 ce062d50 2019-08-21 martijn #include <unistd.h>
34 957c2372 2019-11-14 martijn #include "openbsd-compat.h"
35 ce062d50 2019-08-21 martijn #include "ioev.h"
36 ce062d50 2019-08-21 martijn #include "iobuf.h"
38 ce062d50 2019-08-21 martijn #ifdef IO_TLS
39 ce062d50 2019-08-21 martijn #include <openssl/err.h>
40 ce062d50 2019-08-21 martijn #include <openssl/ssl.h>
44 ce062d50 2019-08-21 martijn IO_STATE_NONE,
45 ce062d50 2019-08-21 martijn IO_STATE_CONNECT,
46 ce062d50 2019-08-21 martijn IO_STATE_CONNECT_TLS,
47 ce062d50 2019-08-21 martijn IO_STATE_ACCEPT_TLS,
48 ce062d50 2019-08-21 martijn IO_STATE_UP,
50 ce062d50 2019-08-21 martijn IO_STATE_MAX,
53 ce062d50 2019-08-21 martijn #define IO_PAUSE_IN IO_IN
54 ce062d50 2019-08-21 martijn #define IO_PAUSE_OUT IO_OUT
55 ce062d50 2019-08-21 martijn #define IO_READ 0x04
56 ce062d50 2019-08-21 martijn #define IO_WRITE 0x08
57 ce062d50 2019-08-21 martijn #define IO_RW (IO_READ | IO_WRITE)
58 ce062d50 2019-08-21 martijn #define IO_RESET 0x10 /* internal */
59 ce062d50 2019-08-21 martijn #define IO_HELD 0x20 /* internal */
61 ce062d50 2019-08-21 martijn struct io {
62 ce062d50 2019-08-21 martijn int sock;
63 ce062d50 2019-08-21 martijn void *arg;
64 ce062d50 2019-08-21 martijn void (*cb)(struct io*, int, void *);
65 ce062d50 2019-08-21 martijn struct iobuf iobuf;
66 ce062d50 2019-08-21 martijn size_t lowat;
67 ce062d50 2019-08-21 martijn int timeout;
68 ce062d50 2019-08-21 martijn int flags;
69 ce062d50 2019-08-21 martijn int state;
70 ce062d50 2019-08-21 martijn struct event ev;
71 ce062d50 2019-08-21 martijn void *tls;
72 ce062d50 2019-08-21 martijn const char *error; /* only valid immediately on callback */
75 ce062d50 2019-08-21 martijn const char* io_strflags(int);
76 ce062d50 2019-08-21 martijn const char* io_evstr(short);
78 ce062d50 2019-08-21 martijn void _io_init(void);
79 ce062d50 2019-08-21 martijn void io_hold(struct io *);
80 ce062d50 2019-08-21 martijn void io_release(struct io *);
81 ce062d50 2019-08-21 martijn void io_callback(struct io*, int);
82 ce062d50 2019-08-21 martijn void io_dispatch(int, short, void *);
83 ce062d50 2019-08-21 martijn void io_dispatch_connect(int, short, void *);
84 ce062d50 2019-08-21 martijn size_t io_pending(struct io *);
85 ce062d50 2019-08-21 martijn size_t io_queued(struct io*);
86 ce062d50 2019-08-21 martijn void io_reset(struct io *, short, void (*)(int, short, void*));
87 ce062d50 2019-08-21 martijn void io_frame_enter(const char *, struct io *, int);
88 ce062d50 2019-08-21 martijn void io_frame_leave(struct io *);
90 ce062d50 2019-08-21 martijn #ifdef IO_TLS
91 ce062d50 2019-08-21 martijn void ssl_error(const char *); /* XXX external */
93 ce062d50 2019-08-21 martijn static const char* io_tls_error(void);
94 ce062d50 2019-08-21 martijn void io_dispatch_accept_tls(int, short, void *);
95 ce062d50 2019-08-21 martijn void io_dispatch_connect_tls(int, short, void *);
96 ce062d50 2019-08-21 martijn void io_dispatch_read_tls(int, short, void *);
97 ce062d50 2019-08-21 martijn void io_dispatch_write_tls(int, short, void *);
98 ce062d50 2019-08-21 martijn void io_reload_tls(struct io *io);
101 ce062d50 2019-08-21 martijn static struct io *current = NULL;
102 ce062d50 2019-08-21 martijn static uint64_t frame = 0;
103 ce062d50 2019-08-21 martijn static int _io_debug = 0;
105 ce062d50 2019-08-21 martijn #define io_debug(args...) do { if (_io_debug) printf(args); } while(0)
108 ce062d50 2019-08-21 martijn const char*
109 ce062d50 2019-08-21 martijn io_strio(struct io *io)
111 ce062d50 2019-08-21 martijn static char buf[128];
112 ce062d50 2019-08-21 martijn char ssl[128];
114 ce062d50 2019-08-21 martijn ssl[0] = '\0';
115 ce062d50 2019-08-21 martijn #ifdef IO_TLS
116 ce062d50 2019-08-21 martijn if (io->tls) {
117 ce062d50 2019-08-21 martijn (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d",
118 ce062d50 2019-08-21 martijn SSL_get_version(io->tls),
119 ce062d50 2019-08-21 martijn SSL_get_cipher_name(io->tls),
120 ce062d50 2019-08-21 martijn SSL_get_cipher_bits(io->tls, NULL));
124 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof buf,
125 ce062d50 2019-08-21 martijn "<io:%p fd=%d to=%d fl=%s%s ib=%zu ob=%zu>",
126 ce062d50 2019-08-21 martijn io, io->sock, io->timeout, io_strflags(io->flags), ssl,
127 ce062d50 2019-08-21 martijn io_pending(io), io_queued(io));
129 ce062d50 2019-08-21 martijn return (buf);
132 ce062d50 2019-08-21 martijn #define CASE(x) case x : return #x
134 ce062d50 2019-08-21 martijn const char*
135 ce062d50 2019-08-21 martijn io_strevent(int evt)
137 ce062d50 2019-08-21 martijn static char buf[32];
139 ce062d50 2019-08-21 martijn switch (evt) {
140 ce062d50 2019-08-21 martijn CASE(IO_CONNECTED);
141 ce062d50 2019-08-21 martijn CASE(IO_TLSREADY);
142 ce062d50 2019-08-21 martijn CASE(IO_DATAIN);
143 ce062d50 2019-08-21 martijn CASE(IO_LOWAT);
144 ce062d50 2019-08-21 martijn CASE(IO_DISCONNECTED);
145 ce062d50 2019-08-21 martijn CASE(IO_TIMEOUT);
146 ce062d50 2019-08-21 martijn CASE(IO_ERROR);
147 ce062d50 2019-08-21 martijn default:
148 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
149 ce062d50 2019-08-21 martijn return buf;
154 ce062d50 2019-08-21 martijn io_set_nonblocking(int fd)
156 ce062d50 2019-08-21 martijn int flags;
158 ce062d50 2019-08-21 martijn if ((flags = fcntl(fd, F_GETFL)) == -1)
159 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_GETFL)");
161 ce062d50 2019-08-21 martijn flags |= O_NONBLOCK;
163 ce062d50 2019-08-21 martijn if (fcntl(fd, F_SETFL, flags) == -1)
164 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_SETFL)");
168 ce062d50 2019-08-21 martijn io_set_nolinger(int fd)
170 ce062d50 2019-08-21 martijn struct linger l;
172 ce062d50 2019-08-21 martijn memset(&l, 0, sizeof(l));
173 ce062d50 2019-08-21 martijn if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
174 ce062d50 2019-08-21 martijn err(1, "io_set_linger:setsockopt()");
178 ce062d50 2019-08-21 martijn * Event framing must not rely on an io pointer to refer to the "same" io
179 ce062d50 2019-08-21 martijn * throughout the frame, because this is not always the case:
181 ce062d50 2019-08-21 martijn * 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV
182 ce062d50 2019-08-21 martijn * 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD!
184 ce062d50 2019-08-21 martijn * In both case, the problem is that the io is freed in the callback, so
185 ce062d50 2019-08-21 martijn * the pointer becomes invalid. If that happens, the user is required to
186 ce062d50 2019-08-21 martijn * call io_clear, so we can adapt the frame state there.
189 ce062d50 2019-08-21 martijn io_frame_enter(const char *where, struct io *io, int ev)
191 ce062d50 2019-08-21 martijn io_debug("\n=== %" PRIu64 " ===\n"
192 ce062d50 2019-08-21 martijn "io_frame_enter(%s, %s, %s)\n",
193 ce062d50 2019-08-21 martijn frame, where, io_evstr(ev), io_strio(io));
195 ce062d50 2019-08-21 martijn if (current)
196 ce062d50 2019-08-21 martijn errx(1, "io_frame_enter: interleaved frames");
198 ce062d50 2019-08-21 martijn current = io;
200 ce062d50 2019-08-21 martijn io_hold(io);
204 ce062d50 2019-08-21 martijn io_frame_leave(struct io *io)
206 ce062d50 2019-08-21 martijn io_debug("io_frame_leave(%" PRIu64 ")\n", frame);
208 ce062d50 2019-08-21 martijn if (current && current != io)
209 ce062d50 2019-08-21 martijn errx(1, "io_frame_leave: io mismatch");
211 ce062d50 2019-08-21 martijn /* io has been cleared */
212 ce062d50 2019-08-21 martijn if (current == NULL)
213 ce062d50 2019-08-21 martijn goto done;
215 ce062d50 2019-08-21 martijn /* TODO: There is a possible optimization there:
216 ce062d50 2019-08-21 martijn * In a typical half-duplex request/response scenario,
217 ce062d50 2019-08-21 martijn * the io is waiting to read a request, and when done, it queues
218 ce062d50 2019-08-21 martijn * the response in the output buffer and goes to write mode.
219 ce062d50 2019-08-21 martijn * There, the write event is set and will be triggered in the next
220 ce062d50 2019-08-21 martijn * event frame. In most case, the write call could be done
221 ce062d50 2019-08-21 martijn * immediately as part of the last read frame, thus avoiding to go
222 ce062d50 2019-08-21 martijn * through the event loop machinery. So, as an optimisation, we
223 ce062d50 2019-08-21 martijn * could detect that case here and force an event dispatching.
226 ce062d50 2019-08-21 martijn /* Reload the io if it has not been reset already. */
227 ce062d50 2019-08-21 martijn io_release(io);
228 ce062d50 2019-08-21 martijn current = NULL;
230 ce062d50 2019-08-21 martijn io_debug("=== /%" PRIu64 "\n", frame);
232 ce062d50 2019-08-21 martijn frame += 1;
236 ce062d50 2019-08-21 martijn _io_init()
238 ce062d50 2019-08-21 martijn static int init = 0;
240 ce062d50 2019-08-21 martijn if (init)
243 ce062d50 2019-08-21 martijn init = 1;
244 ce062d50 2019-08-21 martijn _io_debug = getenv("IO_DEBUG") != NULL;
247 ce062d50 2019-08-21 martijn struct io *
248 ce062d50 2019-08-21 martijn io_new(void)
250 ce062d50 2019-08-21 martijn struct io *io;
252 ce062d50 2019-08-21 martijn _io_init();
254 ce062d50 2019-08-21 martijn if ((io = calloc(1, sizeof(*io))) == NULL)
255 ce062d50 2019-08-21 martijn return NULL;
257 ce062d50 2019-08-21 martijn io->sock = -1;
258 ce062d50 2019-08-21 martijn io->timeout = -1;
260 ce062d50 2019-08-21 martijn if (iobuf_init(&io->iobuf, 0, 0) == -1) {
261 ce062d50 2019-08-21 martijn free(io);
262 ce062d50 2019-08-21 martijn return NULL;
265 ce062d50 2019-08-21 martijn return io;
269 ce062d50 2019-08-21 martijn io_free(struct io *io)
271 ce062d50 2019-08-21 martijn io_debug("io_clear(%p)\n", io);
273 ce062d50 2019-08-21 martijn /* the current io is virtually dead */
274 ce062d50 2019-08-21 martijn if (io == current)
275 ce062d50 2019-08-21 martijn current = NULL;
277 ce062d50 2019-08-21 martijn #ifdef IO_TLS
278 ce062d50 2019-08-21 martijn SSL_free(io->tls);
279 ce062d50 2019-08-21 martijn io->tls = NULL;
282 ce062d50 2019-08-21 martijn if (event_initialized(&io->ev))
283 ce062d50 2019-08-21 martijn event_del(&io->ev);
284 ce062d50 2019-08-21 martijn if (io->sock != -1) {
285 ce062d50 2019-08-21 martijn close(io->sock);
286 ce062d50 2019-08-21 martijn io->sock = -1;
289 ce062d50 2019-08-21 martijn iobuf_clear(&io->iobuf);
290 ce062d50 2019-08-21 martijn free(io);
294 ce062d50 2019-08-21 martijn io_hold(struct io *io)
296 ce062d50 2019-08-21 martijn io_debug("io_enter(%p)\n", io);
298 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
299 ce062d50 2019-08-21 martijn errx(1, "io_hold: io is already held");
301 ce062d50 2019-08-21 martijn io->flags &= ~IO_RESET;
302 ce062d50 2019-08-21 martijn io->flags |= IO_HELD;
306 ce062d50 2019-08-21 martijn io_release(struct io *io)
308 ce062d50 2019-08-21 martijn if (!(io->flags & IO_HELD))
309 ce062d50 2019-08-21 martijn errx(1, "io_release: io is not held");
311 ce062d50 2019-08-21 martijn io->flags &= ~IO_HELD;
312 ce062d50 2019-08-21 martijn if (!(io->flags & IO_RESET))
313 ce062d50 2019-08-21 martijn io_reload(io);
317 ce062d50 2019-08-21 martijn io_set_fd(struct io *io, int fd)
319 ce062d50 2019-08-21 martijn io->sock = fd;
320 ce062d50 2019-08-21 martijn if (fd != -1)
321 ce062d50 2019-08-21 martijn io_reload(io);
325 ce062d50 2019-08-21 martijn io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
327 ce062d50 2019-08-21 martijn io->cb = cb;
328 ce062d50 2019-08-21 martijn io->arg = arg;
332 ce062d50 2019-08-21 martijn io_set_timeout(struct io *io, int msec)
334 ce062d50 2019-08-21 martijn io_debug("io_set_timeout(%p, %d)\n", io, msec);
336 ce062d50 2019-08-21 martijn io->timeout = msec;
340 ce062d50 2019-08-21 martijn io_set_lowat(struct io *io, size_t lowat)
342 ce062d50 2019-08-21 martijn io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
344 ce062d50 2019-08-21 martijn io->lowat = lowat;
348 ce062d50 2019-08-21 martijn io_pause(struct io *io, int dir)
350 ce062d50 2019-08-21 martijn io_debug("io_pause(%p, %x)\n", io, dir);
352 ce062d50 2019-08-21 martijn io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT);
353 ce062d50 2019-08-21 martijn io_reload(io);
357 ce062d50 2019-08-21 martijn io_resume(struct io *io, int dir)
359 ce062d50 2019-08-21 martijn io_debug("io_resume(%p, %x)\n", io, dir);
361 ce062d50 2019-08-21 martijn io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT));
362 ce062d50 2019-08-21 martijn io_reload(io);
366 ce062d50 2019-08-21 martijn io_set_read(struct io *io)
368 ce062d50 2019-08-21 martijn int mode;
370 ce062d50 2019-08-21 martijn io_debug("io_set_read(%p)\n", io);
372 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
373 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_WRITE))
374 ce062d50 2019-08-21 martijn errx(1, "io_set_read(): full-duplex or reading");
376 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
377 ce062d50 2019-08-21 martijn io->flags |= IO_READ;
378 ce062d50 2019-08-21 martijn io_reload(io);
382 ce062d50 2019-08-21 martijn io_set_write(struct io *io)
384 ce062d50 2019-08-21 martijn int mode;
386 ce062d50 2019-08-21 martijn io_debug("io_set_write(%p)\n", io);
388 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
389 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_READ))
390 ce062d50 2019-08-21 martijn errx(1, "io_set_write(): full-duplex or writing");
392 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
393 ce062d50 2019-08-21 martijn io->flags |= IO_WRITE;
394 ce062d50 2019-08-21 martijn io_reload(io);
397 ce062d50 2019-08-21 martijn const char *
398 ce062d50 2019-08-21 martijn io_error(struct io *io)
400 ce062d50 2019-08-21 martijn return io->error;
404 ce062d50 2019-08-21 martijn io_tls(struct io *io)
406 ce062d50 2019-08-21 martijn return io->tls;
410 ce062d50 2019-08-21 martijn io_fileno(struct io *io)
412 ce062d50 2019-08-21 martijn return io->sock;
416 ce062d50 2019-08-21 martijn io_paused(struct io *io, int what)
418 ce062d50 2019-08-21 martijn return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what;
422 ce062d50 2019-08-21 martijn * Buffered output functions
426 ce062d50 2019-08-21 martijn io_write(struct io *io, const void *buf, size_t len)
430 ce062d50 2019-08-21 martijn r = iobuf_queue(&io->iobuf, buf, len);
432 ce062d50 2019-08-21 martijn io_reload(io);
434 ce062d50 2019-08-21 martijn return r;
438 ce062d50 2019-08-21 martijn io_writev(struct io *io, const struct iovec *iov, int iovcount)
442 ce062d50 2019-08-21 martijn r = iobuf_queuev(&io->iobuf, iov, iovcount);
444 ce062d50 2019-08-21 martijn io_reload(io);
446 ce062d50 2019-08-21 martijn return r;
450 ce062d50 2019-08-21 martijn io_print(struct io *io, const char *s)
452 ce062d50 2019-08-21 martijn return io_write(io, s, strlen(s));
456 ce062d50 2019-08-21 martijn io_printf(struct io *io, const char *fmt, ...)
458 ce062d50 2019-08-21 martijn va_list ap;
461 ce062d50 2019-08-21 martijn va_start(ap, fmt);
462 ce062d50 2019-08-21 martijn r = io_vprintf(io, fmt, ap);
463 ce062d50 2019-08-21 martijn va_end(ap);
465 ce062d50 2019-08-21 martijn return r;
469 ce062d50 2019-08-21 martijn io_vprintf(struct io *io, const char *fmt, va_list ap)
472 ce062d50 2019-08-21 martijn char *buf;
473 ce062d50 2019-08-21 martijn int len;
475 ce062d50 2019-08-21 martijn len = vasprintf(&buf, fmt, ap);
476 ce062d50 2019-08-21 martijn if (len == -1)
477 ce062d50 2019-08-21 martijn return -1;
478 ce062d50 2019-08-21 martijn len = io_write(io, buf, len);
479 ce062d50 2019-08-21 martijn free(buf);
481 ce062d50 2019-08-21 martijn return len;
485 ce062d50 2019-08-21 martijn io_queued(struct io *io)
487 ce062d50 2019-08-21 martijn return iobuf_queued(&io->iobuf);
491 ce062d50 2019-08-21 martijn * Buffered input functions
495 ce062d50 2019-08-21 martijn io_data(struct io *io)
497 ce062d50 2019-08-21 martijn return iobuf_data(&io->iobuf);
501 ce062d50 2019-08-21 martijn io_datalen(struct io *io)
503 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
507 ce062d50 2019-08-21 martijn io_getline(struct io *io, size_t *sz)
509 ce062d50 2019-08-21 martijn return iobuf_getline(&io->iobuf, sz);
513 ce062d50 2019-08-21 martijn io_drop(struct io *io, size_t sz)
515 ce062d50 2019-08-21 martijn return iobuf_drop(&io->iobuf, sz);
519 ce062d50 2019-08-21 martijn #define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
520 ce062d50 2019-08-21 martijn #define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
523 ce062d50 2019-08-21 martijn * Setup the necessary events as required by the current io state,
524 ce062d50 2019-08-21 martijn * honouring duplex mode and i/o pauses.
527 ce062d50 2019-08-21 martijn io_reload(struct io *io)
529 ce062d50 2019-08-21 martijn short events;
531 ce062d50 2019-08-21 martijn /* io will be reloaded at release time */
532 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
535 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
537 ce062d50 2019-08-21 martijn #ifdef IO_TLS
538 ce062d50 2019-08-21 martijn if (io->tls) {
539 ce062d50 2019-08-21 martijn io_reload_tls(io);
544 ce062d50 2019-08-21 martijn io_debug("io_reload(%p)\n", io);
546 ce062d50 2019-08-21 martijn events = 0;
547 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
548 ce062d50 2019-08-21 martijn events = EV_READ;
549 ce062d50 2019-08-21 martijn if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
550 ce062d50 2019-08-21 martijn events |= EV_WRITE;
552 ce062d50 2019-08-21 martijn io_reset(io, events, io_dispatch);
555 ce062d50 2019-08-21 martijn /* Set the requested event. */
557 ce062d50 2019-08-21 martijn io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
559 ce062d50 2019-08-21 martijn struct timeval tv, *ptv;
561 ce062d50 2019-08-21 martijn io_debug("io_reset(%p, %s, %p) -> %s\n",
562 ce062d50 2019-08-21 martijn io, io_evstr(events), dispatch, io_strio(io));
565 ce062d50 2019-08-21 martijn * Indicate that the event has already been reset so that reload
566 ce062d50 2019-08-21 martijn * is not called on frame_leave.
568 ce062d50 2019-08-21 martijn io->flags |= IO_RESET;
570 ce062d50 2019-08-21 martijn if (event_initialized(&io->ev))
571 ce062d50 2019-08-21 martijn event_del(&io->ev);
574 ce062d50 2019-08-21 martijn * The io is paused by the user, so we don't want the timeout to be
575 ce062d50 2019-08-21 martijn * effective.
577 ce062d50 2019-08-21 martijn if (events == 0)
580 ce062d50 2019-08-21 martijn event_set(&io->ev, io->sock, events, dispatch, io);
581 ce062d50 2019-08-21 martijn if (io->timeout >= 0) {
582 ce062d50 2019-08-21 martijn tv.tv_sec = io->timeout / 1000;
583 ce062d50 2019-08-21 martijn tv.tv_usec = (io->timeout % 1000) * 1000;
584 ce062d50 2019-08-21 martijn ptv = &tv;
586 ce062d50 2019-08-21 martijn ptv = NULL;
588 ce062d50 2019-08-21 martijn event_add(&io->ev, ptv);
592 ce062d50 2019-08-21 martijn io_pending(struct io *io)
594 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
597 ce062d50 2019-08-21 martijn const char*
598 ce062d50 2019-08-21 martijn io_strflags(int flags)
600 ce062d50 2019-08-21 martijn static char buf[64];
602 ce062d50 2019-08-21 martijn buf[0] = '\0';
604 ce062d50 2019-08-21 martijn switch (flags & IO_RW) {
606 ce062d50 2019-08-21 martijn (void)strlcat(buf, "rw", sizeof buf);
608 ce062d50 2019-08-21 martijn case IO_READ:
609 ce062d50 2019-08-21 martijn (void)strlcat(buf, "R", sizeof buf);
611 ce062d50 2019-08-21 martijn case IO_WRITE:
612 ce062d50 2019-08-21 martijn (void)strlcat(buf, "W", sizeof buf);
614 ce062d50 2019-08-21 martijn case IO_RW:
615 ce062d50 2019-08-21 martijn (void)strlcat(buf, "RW", sizeof buf);
619 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_IN)
620 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PI", sizeof buf);
621 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_OUT)
622 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PO", sizeof buf);
624 ce062d50 2019-08-21 martijn return buf;
627 ce062d50 2019-08-21 martijn const char*
628 ce062d50 2019-08-21 martijn io_evstr(short ev)
630 ce062d50 2019-08-21 martijn static char buf[64];
631 ce062d50 2019-08-21 martijn char buf2[16];
635 ce062d50 2019-08-21 martijn buf[0] = '\0';
637 ce062d50 2019-08-21 martijn if (ev == 0) {
638 ce062d50 2019-08-21 martijn (void)strlcat(buf, "<NONE>", sizeof(buf));
639 ce062d50 2019-08-21 martijn return buf;
642 ce062d50 2019-08-21 martijn if (ev & EV_TIMEOUT) {
643 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
644 ce062d50 2019-08-21 martijn ev &= ~EV_TIMEOUT;
648 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
650 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
651 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_READ", sizeof(buf));
652 ce062d50 2019-08-21 martijn ev &= ~EV_READ;
656 ce062d50 2019-08-21 martijn if (ev & EV_WRITE) {
658 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
659 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_WRITE", sizeof(buf));
660 ce062d50 2019-08-21 martijn ev &= ~EV_WRITE;
664 ce062d50 2019-08-21 martijn if (ev & EV_SIGNAL) {
666 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
667 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
668 ce062d50 2019-08-21 martijn ev &= ~EV_SIGNAL;
672 ce062d50 2019-08-21 martijn if (ev) {
674 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
675 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_?=0x", sizeof(buf));
676 ce062d50 2019-08-21 martijn (void)snprintf(buf2, sizeof(buf2), "%hx", ev);
677 ce062d50 2019-08-21 martijn (void)strlcat(buf, buf2, sizeof(buf));
680 ce062d50 2019-08-21 martijn return buf;
684 957c2372 2019-11-14 martijn io_dispatch(__unused int fd, short ev, void *humppa)
686 ce062d50 2019-08-21 martijn struct io *io = humppa;
687 ce062d50 2019-08-21 martijn size_t w;
688 ce062d50 2019-08-21 martijn ssize_t n;
689 ce062d50 2019-08-21 martijn int saved_errno;
691 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch", io, ev);
693 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
694 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
695 ce062d50 2019-08-21 martijn goto leave;
698 ce062d50 2019-08-21 martijn if (ev & EV_WRITE && (w = io_queued(io))) {
699 ce062d50 2019-08-21 martijn if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) {
700 ce062d50 2019-08-21 martijn if (n == IOBUF_WANT_WRITE) /* kqueue bug? */
701 ce062d50 2019-08-21 martijn goto read;
702 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
703 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
705 ce062d50 2019-08-21 martijn saved_errno = errno;
706 ce062d50 2019-08-21 martijn io->error = strerror(errno);
707 ce062d50 2019-08-21 martijn errno = saved_errno;
708 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
710 ce062d50 2019-08-21 martijn goto leave;
712 ce062d50 2019-08-21 martijn if (w > io->lowat && w - n <= io->lowat)
713 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
717 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
718 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
719 ce062d50 2019-08-21 martijn if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) {
720 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
721 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
723 ce062d50 2019-08-21 martijn saved_errno = errno;
724 ce062d50 2019-08-21 martijn io->error = strerror(errno);
725 ce062d50 2019-08-21 martijn errno = saved_errno;
726 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
728 ce062d50 2019-08-21 martijn goto leave;
731 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
735 ce062d50 2019-08-21 martijn io_frame_leave(io);
739 ce062d50 2019-08-21 martijn io_callback(struct io *io, int evt)
741 ce062d50 2019-08-21 martijn io->cb(io, evt, io->arg);
745 ce062d50 2019-08-21 martijn io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa)
747 ce062d50 2019-08-21 martijn int sock, errno_save;
749 ce062d50 2019-08-21 martijn if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
750 ce062d50 2019-08-21 martijn goto fail;
752 ce062d50 2019-08-21 martijn io_set_nonblocking(sock);
753 ce062d50 2019-08-21 martijn io_set_nolinger(sock);
755 957c2372 2019-11-14 martijn if (bsa && bind(sock, bsa, SA_LEN(bsa)) == -1)
756 ce062d50 2019-08-21 martijn goto fail;
758 957c2372 2019-11-14 martijn if (connect(sock, sa, SA_LEN(sa)) == -1)
759 ce062d50 2019-08-21 martijn if (errno != EINPROGRESS)
760 ce062d50 2019-08-21 martijn goto fail;
762 ce062d50 2019-08-21 martijn io->sock = sock;
763 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect);
765 ce062d50 2019-08-21 martijn return (sock);
768 ce062d50 2019-08-21 martijn if (sock != -1) {
769 ce062d50 2019-08-21 martijn errno_save = errno;
770 ce062d50 2019-08-21 martijn close(sock);
771 ce062d50 2019-08-21 martijn errno = errno_save;
772 ce062d50 2019-08-21 martijn io->error = strerror(errno);
774 ce062d50 2019-08-21 martijn return (-1);
778 ce062d50 2019-08-21 martijn io_dispatch_connect(int fd, short ev, void *humppa)
780 ce062d50 2019-08-21 martijn struct io *io = humppa;
781 ce062d50 2019-08-21 martijn int r, e;
782 ce062d50 2019-08-21 martijn socklen_t sl;
784 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect", io, ev);
786 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
787 ce062d50 2019-08-21 martijn close(fd);
788 ce062d50 2019-08-21 martijn io->sock = -1;
789 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
790 ce062d50 2019-08-21 martijn } else {
791 ce062d50 2019-08-21 martijn sl = sizeof(e);
792 ce062d50 2019-08-21 martijn r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
793 ce062d50 2019-08-21 martijn if (r == -1) {
794 ce062d50 2019-08-21 martijn warn("io_dispatch_connect: getsockopt");
795 ce062d50 2019-08-21 martijn e = errno;
797 ce062d50 2019-08-21 martijn if (e) {
798 ce062d50 2019-08-21 martijn close(fd);
799 ce062d50 2019-08-21 martijn io->sock = -1;
800 ce062d50 2019-08-21 martijn io->error = strerror(e);
801 ce062d50 2019-08-21 martijn io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
804 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
805 ce062d50 2019-08-21 martijn io_callback(io, IO_CONNECTED);
809 ce062d50 2019-08-21 martijn io_frame_leave(io);
812 ce062d50 2019-08-21 martijn #ifdef IO_TLS
814 ce062d50 2019-08-21 martijn static const char*
815 ce062d50 2019-08-21 martijn io_tls_error(void)
817 ce062d50 2019-08-21 martijn static char buf[128];
818 ce062d50 2019-08-21 martijn unsigned long e;
820 ce062d50 2019-08-21 martijn e = ERR_peek_last_error();
821 ce062d50 2019-08-21 martijn if (e) {
822 ce062d50 2019-08-21 martijn ERR_error_string(e, buf);
823 ce062d50 2019-08-21 martijn return (buf);
826 ce062d50 2019-08-21 martijn return ("No TLS error");
830 ce062d50 2019-08-21 martijn io_start_tls(struct io *io, void *tls)
832 ce062d50 2019-08-21 martijn int mode;
834 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
835 ce062d50 2019-08-21 martijn if (mode == 0 || mode == IO_RW)
836 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): full-duplex or unset");
838 ce062d50 2019-08-21 martijn if (io->tls)
839 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): TLS already started");
840 ce062d50 2019-08-21 martijn io->tls = tls;
842 ce062d50 2019-08-21 martijn if (SSL_set_fd(io->tls, io->sock) == 0) {
843 ce062d50 2019-08-21 martijn ssl_error("io_start_tls:SSL_set_fd");
844 ce062d50 2019-08-21 martijn return (-1);
847 ce062d50 2019-08-21 martijn if (mode == IO_WRITE) {
848 ce062d50 2019-08-21 martijn io->state = IO_STATE_CONNECT_TLS;
849 ce062d50 2019-08-21 martijn SSL_set_connect_state(io->tls);
850 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
851 ce062d50 2019-08-21 martijn } else {
852 ce062d50 2019-08-21 martijn io->state = IO_STATE_ACCEPT_TLS;
853 ce062d50 2019-08-21 martijn SSL_set_accept_state(io->tls);
854 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
857 ce062d50 2019-08-21 martijn return (0);
861 ce062d50 2019-08-21 martijn io_dispatch_accept_tls(int fd, short event, void *humppa)
863 ce062d50 2019-08-21 martijn struct io *io = humppa;
864 ce062d50 2019-08-21 martijn int e, ret;
866 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_accept_tls", io, event);
868 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
869 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
870 ce062d50 2019-08-21 martijn goto leave;
873 ce062d50 2019-08-21 martijn if ((ret = SSL_accept(io->tls)) > 0) {
874 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
875 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
876 ce062d50 2019-08-21 martijn goto leave;
879 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
880 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
881 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
883 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
884 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_accept_tls);
886 ce062d50 2019-08-21 martijn default:
887 ce062d50 2019-08-21 martijn io->error = io_tls_error();
888 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_accept_tls:SSL_accept");
889 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
894 ce062d50 2019-08-21 martijn io_frame_leave(io);
898 ce062d50 2019-08-21 martijn io_dispatch_connect_tls(int fd, short event, void *humppa)
900 ce062d50 2019-08-21 martijn struct io *io = humppa;
901 ce062d50 2019-08-21 martijn int e, ret;
903 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect_tls", io, event);
905 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
906 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
907 ce062d50 2019-08-21 martijn goto leave;
910 ce062d50 2019-08-21 martijn if ((ret = SSL_connect(io->tls)) > 0) {
911 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
912 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
913 ce062d50 2019-08-21 martijn goto leave;
916 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
917 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
918 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_connect_tls);
920 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
921 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
923 ce062d50 2019-08-21 martijn default:
924 ce062d50 2019-08-21 martijn io->error = io_tls_error();
925 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_connect_ssl:SSL_connect");
926 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSERROR);
931 ce062d50 2019-08-21 martijn io_frame_leave(io);
935 ce062d50 2019-08-21 martijn io_dispatch_read_tls(int fd, short event, void *humppa)
937 ce062d50 2019-08-21 martijn struct io *io = humppa;
938 ce062d50 2019-08-21 martijn int n, saved_errno;
940 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_read_tls", io, event);
942 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
943 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
944 ce062d50 2019-08-21 martijn goto leave;
948 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
949 ce062d50 2019-08-21 martijn switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) {
950 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
951 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_read_tls);
953 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
954 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_read_tls);
956 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
957 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
959 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
960 ce062d50 2019-08-21 martijn saved_errno = errno;
961 ce062d50 2019-08-21 martijn io->error = strerror(errno);
962 ce062d50 2019-08-21 martijn errno = saved_errno;
963 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
965 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
966 ce062d50 2019-08-21 martijn io->error = io_tls_error();
967 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_read_tls:SSL_read");
968 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
970 ce062d50 2019-08-21 martijn default:
971 ce062d50 2019-08-21 martijn io_debug("io_dispatch_read_tls(...) -> r=%d\n", n);
972 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
973 ce062d50 2019-08-21 martijn if (current == io && IO_READING(io) && SSL_pending(io->tls))
974 ce062d50 2019-08-21 martijn goto again;
978 ce062d50 2019-08-21 martijn io_frame_leave(io);
982 ce062d50 2019-08-21 martijn io_dispatch_write_tls(int fd, short event, void *humppa)
984 ce062d50 2019-08-21 martijn struct io *io = humppa;
985 ce062d50 2019-08-21 martijn int n, saved_errno;
986 ce062d50 2019-08-21 martijn size_t w2, w;
988 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_write_tls", io, event);
990 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
991 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
992 ce062d50 2019-08-21 martijn goto leave;
995 ce062d50 2019-08-21 martijn w = io_queued(io);
996 ce062d50 2019-08-21 martijn switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) {
997 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
998 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_write_tls);
1000 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
1001 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_write_tls);
1003 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
1004 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
1006 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
1007 ce062d50 2019-08-21 martijn saved_errno = errno;
1008 ce062d50 2019-08-21 martijn io->error = strerror(errno);
1009 ce062d50 2019-08-21 martijn errno = saved_errno;
1010 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1012 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
1013 ce062d50 2019-08-21 martijn io->error = io_tls_error();
1014 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_write_tls:SSL_write");
1015 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1017 ce062d50 2019-08-21 martijn default:
1018 ce062d50 2019-08-21 martijn io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
1019 ce062d50 2019-08-21 martijn w2 = io_queued(io);
1020 ce062d50 2019-08-21 martijn if (w > io->lowat && w2 <= io->lowat)
1021 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
1026 ce062d50 2019-08-21 martijn io_frame_leave(io);
1030 ce062d50 2019-08-21 martijn io_reload_tls(struct io *io)
1032 ce062d50 2019-08-21 martijn short ev = 0;
1033 ce062d50 2019-08-21 martijn void (*dispatch)(int, short, void*) = NULL;
1035 ce062d50 2019-08-21 martijn switch (io->state) {
1036 ce062d50 2019-08-21 martijn case IO_STATE_CONNECT_TLS:
1037 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1038 ce062d50 2019-08-21 martijn dispatch = io_dispatch_connect_tls;
1040 ce062d50 2019-08-21 martijn case IO_STATE_ACCEPT_TLS:
1041 ce062d50 2019-08-21 martijn ev = EV_READ;
1042 ce062d50 2019-08-21 martijn dispatch = io_dispatch_accept_tls;
1044 ce062d50 2019-08-21 martijn case IO_STATE_UP:
1045 ce062d50 2019-08-21 martijn ev = 0;
1046 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
1047 ce062d50 2019-08-21 martijn ev = EV_READ;
1048 ce062d50 2019-08-21 martijn dispatch = io_dispatch_read_tls;
1050 ce062d50 2019-08-21 martijn else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
1051 ce062d50 2019-08-21 martijn io_queued(io)) {
1052 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1053 ce062d50 2019-08-21 martijn dispatch = io_dispatch_write_tls;
1055 ce062d50 2019-08-21 martijn if (!ev)
1056 ce062d50 2019-08-21 martijn return; /* paused */
1058 ce062d50 2019-08-21 martijn default:
1059 ce062d50 2019-08-21 martijn errx(1, "io_reload_tls(): bad state");
1062 ce062d50 2019-08-21 martijn io_reset(io, ev, dispatch);
1065 ce062d50 2019-08-21 martijn #endif /* IO_TLS */