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.
18 ce062d50 2019-08-21 martijn #include <sys/types.h>
19 ce062d50 2019-08-21 martijn #include <sys/queue.h>
20 ce062d50 2019-08-21 martijn #include <sys/socket.h>
22 ce062d50 2019-08-21 martijn #include <err.h>
23 ce062d50 2019-08-21 martijn #include <errno.h>
24 ce062d50 2019-08-21 martijn #include <event.h>
25 ce062d50 2019-08-21 martijn #include <fcntl.h>
26 ce062d50 2019-08-21 martijn #include <inttypes.h>
27 ce062d50 2019-08-21 martijn #include <stdlib.h>
28 ce062d50 2019-08-21 martijn #include <string.h>
29 ce062d50 2019-08-21 martijn #include <stdio.h>
30 ce062d50 2019-08-21 martijn #include <unistd.h>
32 ce062d50 2019-08-21 martijn #include "ioev.h"
33 ce062d50 2019-08-21 martijn #include "iobuf.h"
35 ce062d50 2019-08-21 martijn #ifdef IO_TLS
36 ce062d50 2019-08-21 martijn #include <openssl/err.h>
37 ce062d50 2019-08-21 martijn #include <openssl/ssl.h>
41 ce062d50 2019-08-21 martijn IO_STATE_NONE,
42 ce062d50 2019-08-21 martijn IO_STATE_CONNECT,
43 ce062d50 2019-08-21 martijn IO_STATE_CONNECT_TLS,
44 ce062d50 2019-08-21 martijn IO_STATE_ACCEPT_TLS,
45 ce062d50 2019-08-21 martijn IO_STATE_UP,
47 ce062d50 2019-08-21 martijn IO_STATE_MAX,
50 ce062d50 2019-08-21 martijn #define IO_PAUSE_IN IO_IN
51 ce062d50 2019-08-21 martijn #define IO_PAUSE_OUT IO_OUT
52 ce062d50 2019-08-21 martijn #define IO_READ 0x04
53 ce062d50 2019-08-21 martijn #define IO_WRITE 0x08
54 ce062d50 2019-08-21 martijn #define IO_RW (IO_READ | IO_WRITE)
55 ce062d50 2019-08-21 martijn #define IO_RESET 0x10 /* internal */
56 ce062d50 2019-08-21 martijn #define IO_HELD 0x20 /* internal */
58 ce062d50 2019-08-21 martijn struct io {
59 ce062d50 2019-08-21 martijn int sock;
60 ce062d50 2019-08-21 martijn void *arg;
61 ce062d50 2019-08-21 martijn void (*cb)(struct io*, int, void *);
62 ce062d50 2019-08-21 martijn struct iobuf iobuf;
63 ce062d50 2019-08-21 martijn size_t lowat;
64 ce062d50 2019-08-21 martijn int timeout;
65 ce062d50 2019-08-21 martijn int flags;
66 ce062d50 2019-08-21 martijn int state;
67 ce062d50 2019-08-21 martijn struct event ev;
68 ce062d50 2019-08-21 martijn void *tls;
69 ce062d50 2019-08-21 martijn const char *error; /* only valid immediately on callback */
72 ce062d50 2019-08-21 martijn const char* io_strflags(int);
73 ce062d50 2019-08-21 martijn const char* io_evstr(short);
75 ce062d50 2019-08-21 martijn void _io_init(void);
76 ce062d50 2019-08-21 martijn void io_hold(struct io *);
77 ce062d50 2019-08-21 martijn void io_release(struct io *);
78 ce062d50 2019-08-21 martijn void io_callback(struct io*, int);
79 ce062d50 2019-08-21 martijn void io_dispatch(int, short, void *);
80 ce062d50 2019-08-21 martijn void io_dispatch_connect(int, short, void *);
81 ce062d50 2019-08-21 martijn size_t io_pending(struct io *);
82 ce062d50 2019-08-21 martijn size_t io_queued(struct io*);
83 ce062d50 2019-08-21 martijn void io_reset(struct io *, short, void (*)(int, short, void*));
84 ce062d50 2019-08-21 martijn void io_frame_enter(const char *, struct io *, int);
85 ce062d50 2019-08-21 martijn void io_frame_leave(struct io *);
87 ce062d50 2019-08-21 martijn #ifdef IO_TLS
88 ce062d50 2019-08-21 martijn void ssl_error(const char *); /* XXX external */
90 ce062d50 2019-08-21 martijn static const char* io_tls_error(void);
91 ce062d50 2019-08-21 martijn void io_dispatch_accept_tls(int, short, void *);
92 ce062d50 2019-08-21 martijn void io_dispatch_connect_tls(int, short, void *);
93 ce062d50 2019-08-21 martijn void io_dispatch_read_tls(int, short, void *);
94 ce062d50 2019-08-21 martijn void io_dispatch_write_tls(int, short, void *);
95 ce062d50 2019-08-21 martijn void io_reload_tls(struct io *io);
98 ce062d50 2019-08-21 martijn static struct io *current = NULL;
99 ce062d50 2019-08-21 martijn static uint64_t frame = 0;
100 ce062d50 2019-08-21 martijn static int _io_debug = 0;
102 ce062d50 2019-08-21 martijn #define io_debug(args...) do { if (_io_debug) printf(args); } while(0)
105 ce062d50 2019-08-21 martijn const char*
106 ce062d50 2019-08-21 martijn io_strio(struct io *io)
108 ce062d50 2019-08-21 martijn static char buf[128];
109 ce062d50 2019-08-21 martijn char ssl[128];
111 ce062d50 2019-08-21 martijn ssl[0] = '\0';
112 ce062d50 2019-08-21 martijn #ifdef IO_TLS
113 ce062d50 2019-08-21 martijn if (io->tls) {
114 ce062d50 2019-08-21 martijn (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d",
115 ce062d50 2019-08-21 martijn SSL_get_version(io->tls),
116 ce062d50 2019-08-21 martijn SSL_get_cipher_name(io->tls),
117 ce062d50 2019-08-21 martijn SSL_get_cipher_bits(io->tls, NULL));
121 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof buf,
122 ce062d50 2019-08-21 martijn "<io:%p fd=%d to=%d fl=%s%s ib=%zu ob=%zu>",
123 ce062d50 2019-08-21 martijn io, io->sock, io->timeout, io_strflags(io->flags), ssl,
124 ce062d50 2019-08-21 martijn io_pending(io), io_queued(io));
126 ce062d50 2019-08-21 martijn return (buf);
129 ce062d50 2019-08-21 martijn #define CASE(x) case x : return #x
131 ce062d50 2019-08-21 martijn const char*
132 ce062d50 2019-08-21 martijn io_strevent(int evt)
134 ce062d50 2019-08-21 martijn static char buf[32];
136 ce062d50 2019-08-21 martijn switch (evt) {
137 ce062d50 2019-08-21 martijn CASE(IO_CONNECTED);
138 ce062d50 2019-08-21 martijn CASE(IO_TLSREADY);
139 ce062d50 2019-08-21 martijn CASE(IO_DATAIN);
140 ce062d50 2019-08-21 martijn CASE(IO_LOWAT);
141 ce062d50 2019-08-21 martijn CASE(IO_DISCONNECTED);
142 ce062d50 2019-08-21 martijn CASE(IO_TIMEOUT);
143 ce062d50 2019-08-21 martijn CASE(IO_ERROR);
144 ce062d50 2019-08-21 martijn default:
145 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
146 ce062d50 2019-08-21 martijn return buf;
151 ce062d50 2019-08-21 martijn io_set_nonblocking(int fd)
153 ce062d50 2019-08-21 martijn int flags;
155 ce062d50 2019-08-21 martijn if ((flags = fcntl(fd, F_GETFL)) == -1)
156 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_GETFL)");
158 ce062d50 2019-08-21 martijn flags |= O_NONBLOCK;
160 ce062d50 2019-08-21 martijn if (fcntl(fd, F_SETFL, flags) == -1)
161 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_SETFL)");
165 ce062d50 2019-08-21 martijn io_set_nolinger(int fd)
167 ce062d50 2019-08-21 martijn struct linger l;
169 ce062d50 2019-08-21 martijn memset(&l, 0, sizeof(l));
170 ce062d50 2019-08-21 martijn if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
171 ce062d50 2019-08-21 martijn err(1, "io_set_linger:setsockopt()");
175 ce062d50 2019-08-21 martijn * Event framing must not rely on an io pointer to refer to the "same" io
176 ce062d50 2019-08-21 martijn * throughout the frame, because this is not always the case:
178 ce062d50 2019-08-21 martijn * 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV
179 ce062d50 2019-08-21 martijn * 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD!
181 ce062d50 2019-08-21 martijn * In both case, the problem is that the io is freed in the callback, so
182 ce062d50 2019-08-21 martijn * the pointer becomes invalid. If that happens, the user is required to
183 ce062d50 2019-08-21 martijn * call io_clear, so we can adapt the frame state there.
186 ce062d50 2019-08-21 martijn io_frame_enter(const char *where, struct io *io, int ev)
188 ce062d50 2019-08-21 martijn io_debug("\n=== %" PRIu64 " ===\n"
189 ce062d50 2019-08-21 martijn "io_frame_enter(%s, %s, %s)\n",
190 ce062d50 2019-08-21 martijn frame, where, io_evstr(ev), io_strio(io));
192 ce062d50 2019-08-21 martijn if (current)
193 ce062d50 2019-08-21 martijn errx(1, "io_frame_enter: interleaved frames");
195 ce062d50 2019-08-21 martijn current = io;
197 ce062d50 2019-08-21 martijn io_hold(io);
201 ce062d50 2019-08-21 martijn io_frame_leave(struct io *io)
203 ce062d50 2019-08-21 martijn io_debug("io_frame_leave(%" PRIu64 ")\n", frame);
205 ce062d50 2019-08-21 martijn if (current && current != io)
206 ce062d50 2019-08-21 martijn errx(1, "io_frame_leave: io mismatch");
208 ce062d50 2019-08-21 martijn /* io has been cleared */
209 ce062d50 2019-08-21 martijn if (current == NULL)
210 ce062d50 2019-08-21 martijn goto done;
212 ce062d50 2019-08-21 martijn /* TODO: There is a possible optimization there:
213 ce062d50 2019-08-21 martijn * In a typical half-duplex request/response scenario,
214 ce062d50 2019-08-21 martijn * the io is waiting to read a request, and when done, it queues
215 ce062d50 2019-08-21 martijn * the response in the output buffer and goes to write mode.
216 ce062d50 2019-08-21 martijn * There, the write event is set and will be triggered in the next
217 ce062d50 2019-08-21 martijn * event frame. In most case, the write call could be done
218 ce062d50 2019-08-21 martijn * immediately as part of the last read frame, thus avoiding to go
219 ce062d50 2019-08-21 martijn * through the event loop machinery. So, as an optimisation, we
220 ce062d50 2019-08-21 martijn * could detect that case here and force an event dispatching.
223 ce062d50 2019-08-21 martijn /* Reload the io if it has not been reset already. */
224 ce062d50 2019-08-21 martijn io_release(io);
225 ce062d50 2019-08-21 martijn current = NULL;
227 ce062d50 2019-08-21 martijn io_debug("=== /%" PRIu64 "\n", frame);
229 ce062d50 2019-08-21 martijn frame += 1;
233 ce062d50 2019-08-21 martijn _io_init()
235 ce062d50 2019-08-21 martijn static int init = 0;
237 ce062d50 2019-08-21 martijn if (init)
240 ce062d50 2019-08-21 martijn init = 1;
241 ce062d50 2019-08-21 martijn _io_debug = getenv("IO_DEBUG") != NULL;
244 ce062d50 2019-08-21 martijn struct io *
245 ce062d50 2019-08-21 martijn io_new(void)
247 ce062d50 2019-08-21 martijn struct io *io;
249 ce062d50 2019-08-21 martijn _io_init();
251 ce062d50 2019-08-21 martijn if ((io = calloc(1, sizeof(*io))) == NULL)
252 ce062d50 2019-08-21 martijn return NULL;
254 ce062d50 2019-08-21 martijn io->sock = -1;
255 ce062d50 2019-08-21 martijn io->timeout = -1;
257 ce062d50 2019-08-21 martijn if (iobuf_init(&io->iobuf, 0, 0) == -1) {
258 ce062d50 2019-08-21 martijn free(io);
259 ce062d50 2019-08-21 martijn return NULL;
262 ce062d50 2019-08-21 martijn return io;
266 ce062d50 2019-08-21 martijn io_free(struct io *io)
268 ce062d50 2019-08-21 martijn io_debug("io_clear(%p)\n", io);
270 ce062d50 2019-08-21 martijn /* the current io is virtually dead */
271 ce062d50 2019-08-21 martijn if (io == current)
272 ce062d50 2019-08-21 martijn current = NULL;
274 ce062d50 2019-08-21 martijn #ifdef IO_TLS
275 ce062d50 2019-08-21 martijn SSL_free(io->tls);
276 ce062d50 2019-08-21 martijn io->tls = NULL;
279 ce062d50 2019-08-21 martijn if (event_initialized(&io->ev))
280 ce062d50 2019-08-21 martijn event_del(&io->ev);
281 ce062d50 2019-08-21 martijn if (io->sock != -1) {
282 ce062d50 2019-08-21 martijn close(io->sock);
283 ce062d50 2019-08-21 martijn io->sock = -1;
286 ce062d50 2019-08-21 martijn iobuf_clear(&io->iobuf);
287 ce062d50 2019-08-21 martijn free(io);
291 ce062d50 2019-08-21 martijn io_hold(struct io *io)
293 ce062d50 2019-08-21 martijn io_debug("io_enter(%p)\n", io);
295 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
296 ce062d50 2019-08-21 martijn errx(1, "io_hold: io is already held");
298 ce062d50 2019-08-21 martijn io->flags &= ~IO_RESET;
299 ce062d50 2019-08-21 martijn io->flags |= IO_HELD;
303 ce062d50 2019-08-21 martijn io_release(struct io *io)
305 ce062d50 2019-08-21 martijn if (!(io->flags & IO_HELD))
306 ce062d50 2019-08-21 martijn errx(1, "io_release: io is not held");
308 ce062d50 2019-08-21 martijn io->flags &= ~IO_HELD;
309 ce062d50 2019-08-21 martijn if (!(io->flags & IO_RESET))
310 ce062d50 2019-08-21 martijn io_reload(io);
314 ce062d50 2019-08-21 martijn io_set_fd(struct io *io, int fd)
316 ce062d50 2019-08-21 martijn io->sock = fd;
317 ce062d50 2019-08-21 martijn if (fd != -1)
318 ce062d50 2019-08-21 martijn io_reload(io);
322 ce062d50 2019-08-21 martijn io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
324 ce062d50 2019-08-21 martijn io->cb = cb;
325 ce062d50 2019-08-21 martijn io->arg = arg;
329 ce062d50 2019-08-21 martijn io_set_timeout(struct io *io, int msec)
331 ce062d50 2019-08-21 martijn io_debug("io_set_timeout(%p, %d)\n", io, msec);
333 ce062d50 2019-08-21 martijn io->timeout = msec;
337 ce062d50 2019-08-21 martijn io_set_lowat(struct io *io, size_t lowat)
339 ce062d50 2019-08-21 martijn io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
341 ce062d50 2019-08-21 martijn io->lowat = lowat;
345 ce062d50 2019-08-21 martijn io_pause(struct io *io, int dir)
347 ce062d50 2019-08-21 martijn io_debug("io_pause(%p, %x)\n", io, dir);
349 ce062d50 2019-08-21 martijn io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT);
350 ce062d50 2019-08-21 martijn io_reload(io);
354 ce062d50 2019-08-21 martijn io_resume(struct io *io, int dir)
356 ce062d50 2019-08-21 martijn io_debug("io_resume(%p, %x)\n", io, dir);
358 ce062d50 2019-08-21 martijn io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT));
359 ce062d50 2019-08-21 martijn io_reload(io);
363 ce062d50 2019-08-21 martijn io_set_read(struct io *io)
365 ce062d50 2019-08-21 martijn int mode;
367 ce062d50 2019-08-21 martijn io_debug("io_set_read(%p)\n", io);
369 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
370 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_WRITE))
371 ce062d50 2019-08-21 martijn errx(1, "io_set_read(): full-duplex or reading");
373 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
374 ce062d50 2019-08-21 martijn io->flags |= IO_READ;
375 ce062d50 2019-08-21 martijn io_reload(io);
379 ce062d50 2019-08-21 martijn io_set_write(struct io *io)
381 ce062d50 2019-08-21 martijn int mode;
383 ce062d50 2019-08-21 martijn io_debug("io_set_write(%p)\n", io);
385 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
386 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_READ))
387 ce062d50 2019-08-21 martijn errx(1, "io_set_write(): full-duplex or writing");
389 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
390 ce062d50 2019-08-21 martijn io->flags |= IO_WRITE;
391 ce062d50 2019-08-21 martijn io_reload(io);
394 ce062d50 2019-08-21 martijn const char *
395 ce062d50 2019-08-21 martijn io_error(struct io *io)
397 ce062d50 2019-08-21 martijn return io->error;
401 ce062d50 2019-08-21 martijn io_tls(struct io *io)
403 ce062d50 2019-08-21 martijn return io->tls;
407 ce062d50 2019-08-21 martijn io_fileno(struct io *io)
409 ce062d50 2019-08-21 martijn return io->sock;
413 ce062d50 2019-08-21 martijn io_paused(struct io *io, int what)
415 ce062d50 2019-08-21 martijn return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what;
419 ce062d50 2019-08-21 martijn * Buffered output functions
423 ce062d50 2019-08-21 martijn io_write(struct io *io, const void *buf, size_t len)
427 ce062d50 2019-08-21 martijn r = iobuf_queue(&io->iobuf, buf, len);
429 ce062d50 2019-08-21 martijn io_reload(io);
431 ce062d50 2019-08-21 martijn return r;
435 ce062d50 2019-08-21 martijn io_writev(struct io *io, const struct iovec *iov, int iovcount)
439 ce062d50 2019-08-21 martijn r = iobuf_queuev(&io->iobuf, iov, iovcount);
441 ce062d50 2019-08-21 martijn io_reload(io);
443 ce062d50 2019-08-21 martijn return r;
447 ce062d50 2019-08-21 martijn io_print(struct io *io, const char *s)
449 ce062d50 2019-08-21 martijn return io_write(io, s, strlen(s));
453 ce062d50 2019-08-21 martijn io_printf(struct io *io, const char *fmt, ...)
455 ce062d50 2019-08-21 martijn va_list ap;
458 ce062d50 2019-08-21 martijn va_start(ap, fmt);
459 ce062d50 2019-08-21 martijn r = io_vprintf(io, fmt, ap);
460 ce062d50 2019-08-21 martijn va_end(ap);
462 ce062d50 2019-08-21 martijn return r;
466 ce062d50 2019-08-21 martijn io_vprintf(struct io *io, const char *fmt, va_list ap)
469 ce062d50 2019-08-21 martijn char *buf;
470 ce062d50 2019-08-21 martijn int len;
472 ce062d50 2019-08-21 martijn len = vasprintf(&buf, fmt, ap);
473 ce062d50 2019-08-21 martijn if (len == -1)
474 ce062d50 2019-08-21 martijn return -1;
475 ce062d50 2019-08-21 martijn len = io_write(io, buf, len);
476 ce062d50 2019-08-21 martijn free(buf);
478 ce062d50 2019-08-21 martijn return len;
482 ce062d50 2019-08-21 martijn io_queued(struct io *io)
484 ce062d50 2019-08-21 martijn return iobuf_queued(&io->iobuf);
488 ce062d50 2019-08-21 martijn * Buffered input functions
492 ce062d50 2019-08-21 martijn io_data(struct io *io)
494 ce062d50 2019-08-21 martijn return iobuf_data(&io->iobuf);
498 ce062d50 2019-08-21 martijn io_datalen(struct io *io)
500 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
504 ce062d50 2019-08-21 martijn io_getline(struct io *io, size_t *sz)
506 ce062d50 2019-08-21 martijn return iobuf_getline(&io->iobuf, sz);
510 ce062d50 2019-08-21 martijn io_drop(struct io *io, size_t sz)
512 ce062d50 2019-08-21 martijn return iobuf_drop(&io->iobuf, sz);
516 ce062d50 2019-08-21 martijn #define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
517 ce062d50 2019-08-21 martijn #define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
520 ce062d50 2019-08-21 martijn * Setup the necessary events as required by the current io state,
521 ce062d50 2019-08-21 martijn * honouring duplex mode and i/o pauses.
524 ce062d50 2019-08-21 martijn io_reload(struct io *io)
526 ce062d50 2019-08-21 martijn short events;
528 ce062d50 2019-08-21 martijn /* io will be reloaded at release time */
529 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
532 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
534 ce062d50 2019-08-21 martijn #ifdef IO_TLS
535 ce062d50 2019-08-21 martijn if (io->tls) {
536 ce062d50 2019-08-21 martijn io_reload_tls(io);
541 ce062d50 2019-08-21 martijn io_debug("io_reload(%p)\n", io);
543 ce062d50 2019-08-21 martijn events = 0;
544 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
545 ce062d50 2019-08-21 martijn events = EV_READ;
546 ce062d50 2019-08-21 martijn if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
547 ce062d50 2019-08-21 martijn events |= EV_WRITE;
549 ce062d50 2019-08-21 martijn io_reset(io, events, io_dispatch);
552 ce062d50 2019-08-21 martijn /* Set the requested event. */
554 ce062d50 2019-08-21 martijn io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
556 ce062d50 2019-08-21 martijn struct timeval tv, *ptv;
558 ce062d50 2019-08-21 martijn io_debug("io_reset(%p, %s, %p) -> %s\n",
559 ce062d50 2019-08-21 martijn io, io_evstr(events), dispatch, io_strio(io));
562 ce062d50 2019-08-21 martijn * Indicate that the event has already been reset so that reload
563 ce062d50 2019-08-21 martijn * is not called on frame_leave.
565 ce062d50 2019-08-21 martijn io->flags |= IO_RESET;
567 ce062d50 2019-08-21 martijn if (event_initialized(&io->ev))
568 ce062d50 2019-08-21 martijn event_del(&io->ev);
571 ce062d50 2019-08-21 martijn * The io is paused by the user, so we don't want the timeout to be
572 ce062d50 2019-08-21 martijn * effective.
574 ce062d50 2019-08-21 martijn if (events == 0)
577 ce062d50 2019-08-21 martijn event_set(&io->ev, io->sock, events, dispatch, io);
578 ce062d50 2019-08-21 martijn if (io->timeout >= 0) {
579 ce062d50 2019-08-21 martijn tv.tv_sec = io->timeout / 1000;
580 ce062d50 2019-08-21 martijn tv.tv_usec = (io->timeout % 1000) * 1000;
581 ce062d50 2019-08-21 martijn ptv = &tv;
583 ce062d50 2019-08-21 martijn ptv = NULL;
585 ce062d50 2019-08-21 martijn event_add(&io->ev, ptv);
589 ce062d50 2019-08-21 martijn io_pending(struct io *io)
591 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
594 ce062d50 2019-08-21 martijn const char*
595 ce062d50 2019-08-21 martijn io_strflags(int flags)
597 ce062d50 2019-08-21 martijn static char buf[64];
599 ce062d50 2019-08-21 martijn buf[0] = '\0';
601 ce062d50 2019-08-21 martijn switch (flags & IO_RW) {
603 ce062d50 2019-08-21 martijn (void)strlcat(buf, "rw", sizeof buf);
605 ce062d50 2019-08-21 martijn case IO_READ:
606 ce062d50 2019-08-21 martijn (void)strlcat(buf, "R", sizeof buf);
608 ce062d50 2019-08-21 martijn case IO_WRITE:
609 ce062d50 2019-08-21 martijn (void)strlcat(buf, "W", sizeof buf);
611 ce062d50 2019-08-21 martijn case IO_RW:
612 ce062d50 2019-08-21 martijn (void)strlcat(buf, "RW", sizeof buf);
616 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_IN)
617 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PI", sizeof buf);
618 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_OUT)
619 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PO", sizeof buf);
621 ce062d50 2019-08-21 martijn return buf;
624 ce062d50 2019-08-21 martijn const char*
625 ce062d50 2019-08-21 martijn io_evstr(short ev)
627 ce062d50 2019-08-21 martijn static char buf[64];
628 ce062d50 2019-08-21 martijn char buf2[16];
632 ce062d50 2019-08-21 martijn buf[0] = '\0';
634 ce062d50 2019-08-21 martijn if (ev == 0) {
635 ce062d50 2019-08-21 martijn (void)strlcat(buf, "<NONE>", sizeof(buf));
636 ce062d50 2019-08-21 martijn return buf;
639 ce062d50 2019-08-21 martijn if (ev & EV_TIMEOUT) {
640 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
641 ce062d50 2019-08-21 martijn ev &= ~EV_TIMEOUT;
645 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
647 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
648 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_READ", sizeof(buf));
649 ce062d50 2019-08-21 martijn ev &= ~EV_READ;
653 ce062d50 2019-08-21 martijn if (ev & EV_WRITE) {
655 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
656 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_WRITE", sizeof(buf));
657 ce062d50 2019-08-21 martijn ev &= ~EV_WRITE;
661 ce062d50 2019-08-21 martijn if (ev & EV_SIGNAL) {
663 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
664 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
665 ce062d50 2019-08-21 martijn ev &= ~EV_SIGNAL;
669 ce062d50 2019-08-21 martijn if (ev) {
671 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
672 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_?=0x", sizeof(buf));
673 ce062d50 2019-08-21 martijn (void)snprintf(buf2, sizeof(buf2), "%hx", ev);
674 ce062d50 2019-08-21 martijn (void)strlcat(buf, buf2, sizeof(buf));
677 ce062d50 2019-08-21 martijn return buf;
681 ce062d50 2019-08-21 martijn io_dispatch(int fd, short ev, void *humppa)
683 ce062d50 2019-08-21 martijn struct io *io = humppa;
684 ce062d50 2019-08-21 martijn size_t w;
685 ce062d50 2019-08-21 martijn ssize_t n;
686 ce062d50 2019-08-21 martijn int saved_errno;
688 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch", io, ev);
690 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
691 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
692 ce062d50 2019-08-21 martijn goto leave;
695 ce062d50 2019-08-21 martijn if (ev & EV_WRITE && (w = io_queued(io))) {
696 ce062d50 2019-08-21 martijn if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) {
697 ce062d50 2019-08-21 martijn if (n == IOBUF_WANT_WRITE) /* kqueue bug? */
698 ce062d50 2019-08-21 martijn goto read;
699 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
700 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
702 ce062d50 2019-08-21 martijn saved_errno = errno;
703 ce062d50 2019-08-21 martijn io->error = strerror(errno);
704 ce062d50 2019-08-21 martijn errno = saved_errno;
705 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
707 ce062d50 2019-08-21 martijn goto leave;
709 ce062d50 2019-08-21 martijn if (w > io->lowat && w - n <= io->lowat)
710 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
714 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
715 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
716 ce062d50 2019-08-21 martijn if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) {
717 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
718 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
720 ce062d50 2019-08-21 martijn saved_errno = errno;
721 ce062d50 2019-08-21 martijn io->error = strerror(errno);
722 ce062d50 2019-08-21 martijn errno = saved_errno;
723 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
725 ce062d50 2019-08-21 martijn goto leave;
728 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
732 ce062d50 2019-08-21 martijn io_frame_leave(io);
736 ce062d50 2019-08-21 martijn io_callback(struct io *io, int evt)
738 ce062d50 2019-08-21 martijn io->cb(io, evt, io->arg);
742 ce062d50 2019-08-21 martijn io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa)
744 ce062d50 2019-08-21 martijn int sock, errno_save;
746 ce062d50 2019-08-21 martijn if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
747 ce062d50 2019-08-21 martijn goto fail;
749 ce062d50 2019-08-21 martijn io_set_nonblocking(sock);
750 ce062d50 2019-08-21 martijn io_set_nolinger(sock);
752 ce062d50 2019-08-21 martijn if (bsa && bind(sock, bsa, bsa->sa_len) == -1)
753 ce062d50 2019-08-21 martijn goto fail;
755 ce062d50 2019-08-21 martijn if (connect(sock, sa, sa->sa_len) == -1)
756 ce062d50 2019-08-21 martijn if (errno != EINPROGRESS)
757 ce062d50 2019-08-21 martijn goto fail;
759 ce062d50 2019-08-21 martijn io->sock = sock;
760 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect);
762 ce062d50 2019-08-21 martijn return (sock);
765 ce062d50 2019-08-21 martijn if (sock != -1) {
766 ce062d50 2019-08-21 martijn errno_save = errno;
767 ce062d50 2019-08-21 martijn close(sock);
768 ce062d50 2019-08-21 martijn errno = errno_save;
769 ce062d50 2019-08-21 martijn io->error = strerror(errno);
771 ce062d50 2019-08-21 martijn return (-1);
775 ce062d50 2019-08-21 martijn io_dispatch_connect(int fd, short ev, void *humppa)
777 ce062d50 2019-08-21 martijn struct io *io = humppa;
778 ce062d50 2019-08-21 martijn int r, e;
779 ce062d50 2019-08-21 martijn socklen_t sl;
781 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect", io, ev);
783 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
784 ce062d50 2019-08-21 martijn close(fd);
785 ce062d50 2019-08-21 martijn io->sock = -1;
786 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
787 ce062d50 2019-08-21 martijn } else {
788 ce062d50 2019-08-21 martijn sl = sizeof(e);
789 ce062d50 2019-08-21 martijn r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
790 ce062d50 2019-08-21 martijn if (r == -1) {
791 ce062d50 2019-08-21 martijn warn("io_dispatch_connect: getsockopt");
792 ce062d50 2019-08-21 martijn e = errno;
794 ce062d50 2019-08-21 martijn if (e) {
795 ce062d50 2019-08-21 martijn close(fd);
796 ce062d50 2019-08-21 martijn io->sock = -1;
797 ce062d50 2019-08-21 martijn io->error = strerror(e);
798 ce062d50 2019-08-21 martijn io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
801 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
802 ce062d50 2019-08-21 martijn io_callback(io, IO_CONNECTED);
806 ce062d50 2019-08-21 martijn io_frame_leave(io);
809 ce062d50 2019-08-21 martijn #ifdef IO_TLS
811 ce062d50 2019-08-21 martijn static const char*
812 ce062d50 2019-08-21 martijn io_tls_error(void)
814 ce062d50 2019-08-21 martijn static char buf[128];
815 ce062d50 2019-08-21 martijn unsigned long e;
817 ce062d50 2019-08-21 martijn e = ERR_peek_last_error();
818 ce062d50 2019-08-21 martijn if (e) {
819 ce062d50 2019-08-21 martijn ERR_error_string(e, buf);
820 ce062d50 2019-08-21 martijn return (buf);
823 ce062d50 2019-08-21 martijn return ("No TLS error");
827 ce062d50 2019-08-21 martijn io_start_tls(struct io *io, void *tls)
829 ce062d50 2019-08-21 martijn int mode;
831 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
832 ce062d50 2019-08-21 martijn if (mode == 0 || mode == IO_RW)
833 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): full-duplex or unset");
835 ce062d50 2019-08-21 martijn if (io->tls)
836 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): TLS already started");
837 ce062d50 2019-08-21 martijn io->tls = tls;
839 ce062d50 2019-08-21 martijn if (SSL_set_fd(io->tls, io->sock) == 0) {
840 ce062d50 2019-08-21 martijn ssl_error("io_start_tls:SSL_set_fd");
841 ce062d50 2019-08-21 martijn return (-1);
844 ce062d50 2019-08-21 martijn if (mode == IO_WRITE) {
845 ce062d50 2019-08-21 martijn io->state = IO_STATE_CONNECT_TLS;
846 ce062d50 2019-08-21 martijn SSL_set_connect_state(io->tls);
847 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
848 ce062d50 2019-08-21 martijn } else {
849 ce062d50 2019-08-21 martijn io->state = IO_STATE_ACCEPT_TLS;
850 ce062d50 2019-08-21 martijn SSL_set_accept_state(io->tls);
851 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
854 ce062d50 2019-08-21 martijn return (0);
858 ce062d50 2019-08-21 martijn io_dispatch_accept_tls(int fd, short event, void *humppa)
860 ce062d50 2019-08-21 martijn struct io *io = humppa;
861 ce062d50 2019-08-21 martijn int e, ret;
863 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_accept_tls", io, event);
865 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
866 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
867 ce062d50 2019-08-21 martijn goto leave;
870 ce062d50 2019-08-21 martijn if ((ret = SSL_accept(io->tls)) > 0) {
871 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
872 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
873 ce062d50 2019-08-21 martijn goto leave;
876 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
877 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
878 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
880 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
881 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_accept_tls);
883 ce062d50 2019-08-21 martijn default:
884 ce062d50 2019-08-21 martijn io->error = io_tls_error();
885 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_accept_tls:SSL_accept");
886 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
891 ce062d50 2019-08-21 martijn io_frame_leave(io);
895 ce062d50 2019-08-21 martijn io_dispatch_connect_tls(int fd, short event, void *humppa)
897 ce062d50 2019-08-21 martijn struct io *io = humppa;
898 ce062d50 2019-08-21 martijn int e, ret;
900 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect_tls", io, event);
902 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
903 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
904 ce062d50 2019-08-21 martijn goto leave;
907 ce062d50 2019-08-21 martijn if ((ret = SSL_connect(io->tls)) > 0) {
908 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
909 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
910 ce062d50 2019-08-21 martijn goto leave;
913 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
914 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
915 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_connect_tls);
917 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
918 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
920 ce062d50 2019-08-21 martijn default:
921 ce062d50 2019-08-21 martijn io->error = io_tls_error();
922 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_connect_ssl:SSL_connect");
923 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSERROR);
928 ce062d50 2019-08-21 martijn io_frame_leave(io);
932 ce062d50 2019-08-21 martijn io_dispatch_read_tls(int fd, short event, void *humppa)
934 ce062d50 2019-08-21 martijn struct io *io = humppa;
935 ce062d50 2019-08-21 martijn int n, saved_errno;
937 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_read_tls", io, event);
939 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
940 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
941 ce062d50 2019-08-21 martijn goto leave;
945 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
946 ce062d50 2019-08-21 martijn switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) {
947 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
948 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_read_tls);
950 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
951 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_read_tls);
953 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
954 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
956 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
957 ce062d50 2019-08-21 martijn saved_errno = errno;
958 ce062d50 2019-08-21 martijn io->error = strerror(errno);
959 ce062d50 2019-08-21 martijn errno = saved_errno;
960 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
962 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
963 ce062d50 2019-08-21 martijn io->error = io_tls_error();
964 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_read_tls:SSL_read");
965 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
967 ce062d50 2019-08-21 martijn default:
968 ce062d50 2019-08-21 martijn io_debug("io_dispatch_read_tls(...) -> r=%d\n", n);
969 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
970 ce062d50 2019-08-21 martijn if (current == io && IO_READING(io) && SSL_pending(io->tls))
971 ce062d50 2019-08-21 martijn goto again;
975 ce062d50 2019-08-21 martijn io_frame_leave(io);
979 ce062d50 2019-08-21 martijn io_dispatch_write_tls(int fd, short event, void *humppa)
981 ce062d50 2019-08-21 martijn struct io *io = humppa;
982 ce062d50 2019-08-21 martijn int n, saved_errno;
983 ce062d50 2019-08-21 martijn size_t w2, w;
985 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_write_tls", io, event);
987 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
988 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
989 ce062d50 2019-08-21 martijn goto leave;
992 ce062d50 2019-08-21 martijn w = io_queued(io);
993 ce062d50 2019-08-21 martijn switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) {
994 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
995 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_write_tls);
997 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
998 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_write_tls);
1000 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
1001 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
1003 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
1004 ce062d50 2019-08-21 martijn saved_errno = errno;
1005 ce062d50 2019-08-21 martijn io->error = strerror(errno);
1006 ce062d50 2019-08-21 martijn errno = saved_errno;
1007 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1009 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
1010 ce062d50 2019-08-21 martijn io->error = io_tls_error();
1011 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_write_tls:SSL_write");
1012 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1014 ce062d50 2019-08-21 martijn default:
1015 ce062d50 2019-08-21 martijn io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
1016 ce062d50 2019-08-21 martijn w2 = io_queued(io);
1017 ce062d50 2019-08-21 martijn if (w > io->lowat && w2 <= io->lowat)
1018 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
1023 ce062d50 2019-08-21 martijn io_frame_leave(io);
1027 ce062d50 2019-08-21 martijn io_reload_tls(struct io *io)
1029 ce062d50 2019-08-21 martijn short ev = 0;
1030 ce062d50 2019-08-21 martijn void (*dispatch)(int, short, void*) = NULL;
1032 ce062d50 2019-08-21 martijn switch (io->state) {
1033 ce062d50 2019-08-21 martijn case IO_STATE_CONNECT_TLS:
1034 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1035 ce062d50 2019-08-21 martijn dispatch = io_dispatch_connect_tls;
1037 ce062d50 2019-08-21 martijn case IO_STATE_ACCEPT_TLS:
1038 ce062d50 2019-08-21 martijn ev = EV_READ;
1039 ce062d50 2019-08-21 martijn dispatch = io_dispatch_accept_tls;
1041 ce062d50 2019-08-21 martijn case IO_STATE_UP:
1042 ce062d50 2019-08-21 martijn ev = 0;
1043 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
1044 ce062d50 2019-08-21 martijn ev = EV_READ;
1045 ce062d50 2019-08-21 martijn dispatch = io_dispatch_read_tls;
1047 ce062d50 2019-08-21 martijn else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
1048 ce062d50 2019-08-21 martijn io_queued(io)) {
1049 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1050 ce062d50 2019-08-21 martijn dispatch = io_dispatch_write_tls;
1052 ce062d50 2019-08-21 martijn if (!ev)
1053 ce062d50 2019-08-21 martijn return; /* paused */
1055 ce062d50 2019-08-21 martijn default:
1056 ce062d50 2019-08-21 martijn errx(1, "io_reload_tls(): bad state");
1059 ce062d50 2019-08-21 martijn io_reset(io, ev, dispatch);
1062 ce062d50 2019-08-21 martijn #endif /* IO_TLS */