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 681d8d49 2022-03-28 martijn //#define io_debug(args...) do { if (_io_debug) printf(args); } while(0)
107 681d8d49 2022-03-28 martijn #define io_debug(args...) do { fprintf(stderr, args); } while(0)
109 ce062d50 2019-08-21 martijn const char*
110 ce062d50 2019-08-21 martijn io_strio(struct io *io)
112 ce062d50 2019-08-21 martijn static char buf[128];
113 ce062d50 2019-08-21 martijn char ssl[128];
115 ce062d50 2019-08-21 martijn ssl[0] = '\0';
116 ce062d50 2019-08-21 martijn #ifdef IO_TLS
117 ce062d50 2019-08-21 martijn if (io->tls) {
118 ce062d50 2019-08-21 martijn (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d",
119 ce062d50 2019-08-21 martijn SSL_get_version(io->tls),
120 ce062d50 2019-08-21 martijn SSL_get_cipher_name(io->tls),
121 ce062d50 2019-08-21 martijn SSL_get_cipher_bits(io->tls, NULL));
125 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof buf,
126 ce062d50 2019-08-21 martijn "<io:%p fd=%d to=%d fl=%s%s ib=%zu ob=%zu>",
127 ce062d50 2019-08-21 martijn io, io->sock, io->timeout, io_strflags(io->flags), ssl,
128 ce062d50 2019-08-21 martijn io_pending(io), io_queued(io));
130 ce062d50 2019-08-21 martijn return (buf);
133 ce062d50 2019-08-21 martijn #define CASE(x) case x : return #x
135 ce062d50 2019-08-21 martijn const char*
136 ce062d50 2019-08-21 martijn io_strevent(int evt)
138 ce062d50 2019-08-21 martijn static char buf[32];
140 ce062d50 2019-08-21 martijn switch (evt) {
141 ce062d50 2019-08-21 martijn CASE(IO_CONNECTED);
142 ce062d50 2019-08-21 martijn CASE(IO_TLSREADY);
143 ce062d50 2019-08-21 martijn CASE(IO_DATAIN);
144 ce062d50 2019-08-21 martijn CASE(IO_LOWAT);
145 ce062d50 2019-08-21 martijn CASE(IO_DISCONNECTED);
146 ce062d50 2019-08-21 martijn CASE(IO_TIMEOUT);
147 ce062d50 2019-08-21 martijn CASE(IO_ERROR);
148 ce062d50 2019-08-21 martijn default:
149 ce062d50 2019-08-21 martijn (void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
150 ce062d50 2019-08-21 martijn return buf;
155 ce062d50 2019-08-21 martijn io_set_nonblocking(int fd)
157 ce062d50 2019-08-21 martijn int flags;
159 ce062d50 2019-08-21 martijn if ((flags = fcntl(fd, F_GETFL)) == -1)
160 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_GETFL)");
162 ce062d50 2019-08-21 martijn flags |= O_NONBLOCK;
164 ce062d50 2019-08-21 martijn if (fcntl(fd, F_SETFL, flags) == -1)
165 ce062d50 2019-08-21 martijn err(1, "io_set_blocking:fcntl(F_SETFL)");
169 ce062d50 2019-08-21 martijn io_set_nolinger(int fd)
171 ce062d50 2019-08-21 martijn struct linger l;
173 ce062d50 2019-08-21 martijn memset(&l, 0, sizeof(l));
174 ce062d50 2019-08-21 martijn if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
175 ce062d50 2019-08-21 martijn err(1, "io_set_linger:setsockopt()");
179 ce062d50 2019-08-21 martijn * Event framing must not rely on an io pointer to refer to the "same" io
180 ce062d50 2019-08-21 martijn * throughout the frame, because this is not always the case:
182 ce062d50 2019-08-21 martijn * 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV
183 ce062d50 2019-08-21 martijn * 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD!
185 ce062d50 2019-08-21 martijn * In both case, the problem is that the io is freed in the callback, so
186 ce062d50 2019-08-21 martijn * the pointer becomes invalid. If that happens, the user is required to
187 ce062d50 2019-08-21 martijn * call io_clear, so we can adapt the frame state there.
190 ce062d50 2019-08-21 martijn io_frame_enter(const char *where, struct io *io, int ev)
192 ce062d50 2019-08-21 martijn io_debug("\n=== %" PRIu64 " ===\n"
193 ce062d50 2019-08-21 martijn "io_frame_enter(%s, %s, %s)\n",
194 ce062d50 2019-08-21 martijn frame, where, io_evstr(ev), io_strio(io));
196 ce062d50 2019-08-21 martijn if (current)
197 ce062d50 2019-08-21 martijn errx(1, "io_frame_enter: interleaved frames");
199 ce062d50 2019-08-21 martijn current = io;
201 ce062d50 2019-08-21 martijn io_hold(io);
205 ce062d50 2019-08-21 martijn io_frame_leave(struct io *io)
207 ce062d50 2019-08-21 martijn io_debug("io_frame_leave(%" PRIu64 ")\n", frame);
209 ce062d50 2019-08-21 martijn if (current && current != io)
210 ce062d50 2019-08-21 martijn errx(1, "io_frame_leave: io mismatch");
212 ce062d50 2019-08-21 martijn /* io has been cleared */
213 ce062d50 2019-08-21 martijn if (current == NULL)
214 ce062d50 2019-08-21 martijn goto done;
216 ce062d50 2019-08-21 martijn /* TODO: There is a possible optimization there:
217 ce062d50 2019-08-21 martijn * In a typical half-duplex request/response scenario,
218 ce062d50 2019-08-21 martijn * the io is waiting to read a request, and when done, it queues
219 ce062d50 2019-08-21 martijn * the response in the output buffer and goes to write mode.
220 ce062d50 2019-08-21 martijn * There, the write event is set and will be triggered in the next
221 ce062d50 2019-08-21 martijn * event frame. In most case, the write call could be done
222 ce062d50 2019-08-21 martijn * immediately as part of the last read frame, thus avoiding to go
223 ce062d50 2019-08-21 martijn * through the event loop machinery. So, as an optimisation, we
224 ce062d50 2019-08-21 martijn * could detect that case here and force an event dispatching.
227 ce062d50 2019-08-21 martijn /* Reload the io if it has not been reset already. */
228 ce062d50 2019-08-21 martijn io_release(io);
229 ce062d50 2019-08-21 martijn current = NULL;
231 ce062d50 2019-08-21 martijn io_debug("=== /%" PRIu64 "\n", frame);
233 ce062d50 2019-08-21 martijn frame += 1;
237 ce062d50 2019-08-21 martijn _io_init()
239 ce062d50 2019-08-21 martijn static int init = 0;
241 ce062d50 2019-08-21 martijn if (init)
244 ce062d50 2019-08-21 martijn init = 1;
245 ce062d50 2019-08-21 martijn _io_debug = getenv("IO_DEBUG") != NULL;
248 ce062d50 2019-08-21 martijn struct io *
249 ce062d50 2019-08-21 martijn io_new(void)
251 ce062d50 2019-08-21 martijn struct io *io;
253 ce062d50 2019-08-21 martijn _io_init();
255 ce062d50 2019-08-21 martijn if ((io = calloc(1, sizeof(*io))) == NULL)
256 ce062d50 2019-08-21 martijn return NULL;
258 ce062d50 2019-08-21 martijn io->sock = -1;
259 ce062d50 2019-08-21 martijn io->timeout = -1;
261 ce062d50 2019-08-21 martijn if (iobuf_init(&io->iobuf, 0, 0) == -1) {
262 ce062d50 2019-08-21 martijn free(io);
263 ce062d50 2019-08-21 martijn return NULL;
266 ce062d50 2019-08-21 martijn return io;
270 ce062d50 2019-08-21 martijn io_free(struct io *io)
272 ce062d50 2019-08-21 martijn io_debug("io_clear(%p)\n", io);
274 ce062d50 2019-08-21 martijn /* the current io is virtually dead */
275 ce062d50 2019-08-21 martijn if (io == current)
276 ce062d50 2019-08-21 martijn current = NULL;
278 ce062d50 2019-08-21 martijn #ifdef IO_TLS
279 ce062d50 2019-08-21 martijn SSL_free(io->tls);
280 ce062d50 2019-08-21 martijn io->tls = NULL;
283 ce062d50 2019-08-21 martijn if (event_initialized(&io->ev))
284 ce062d50 2019-08-21 martijn event_del(&io->ev);
285 ce062d50 2019-08-21 martijn if (io->sock != -1) {
286 ce062d50 2019-08-21 martijn close(io->sock);
287 ce062d50 2019-08-21 martijn io->sock = -1;
290 ce062d50 2019-08-21 martijn iobuf_clear(&io->iobuf);
291 ce062d50 2019-08-21 martijn free(io);
295 ce062d50 2019-08-21 martijn io_hold(struct io *io)
297 ce062d50 2019-08-21 martijn io_debug("io_enter(%p)\n", io);
299 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
300 ce062d50 2019-08-21 martijn errx(1, "io_hold: io is already held");
302 ce062d50 2019-08-21 martijn io->flags &= ~IO_RESET;
303 ce062d50 2019-08-21 martijn io->flags |= IO_HELD;
307 ce062d50 2019-08-21 martijn io_release(struct io *io)
309 ce062d50 2019-08-21 martijn if (!(io->flags & IO_HELD))
310 ce062d50 2019-08-21 martijn errx(1, "io_release: io is not held");
312 ce062d50 2019-08-21 martijn io->flags &= ~IO_HELD;
313 ce062d50 2019-08-21 martijn if (!(io->flags & IO_RESET))
314 ce062d50 2019-08-21 martijn io_reload(io);
318 ce062d50 2019-08-21 martijn io_set_fd(struct io *io, int fd)
320 ce062d50 2019-08-21 martijn io->sock = fd;
321 ce062d50 2019-08-21 martijn if (fd != -1)
322 ce062d50 2019-08-21 martijn io_reload(io);
326 ce062d50 2019-08-21 martijn io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
328 ce062d50 2019-08-21 martijn io->cb = cb;
329 ce062d50 2019-08-21 martijn io->arg = arg;
333 ce062d50 2019-08-21 martijn io_set_timeout(struct io *io, int msec)
335 ce062d50 2019-08-21 martijn io_debug("io_set_timeout(%p, %d)\n", io, msec);
337 ce062d50 2019-08-21 martijn io->timeout = msec;
341 ce062d50 2019-08-21 martijn io_set_lowat(struct io *io, size_t lowat)
343 ce062d50 2019-08-21 martijn io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
345 ce062d50 2019-08-21 martijn io->lowat = lowat;
349 ce062d50 2019-08-21 martijn io_pause(struct io *io, int dir)
351 ce062d50 2019-08-21 martijn io_debug("io_pause(%p, %x)\n", io, dir);
353 ce062d50 2019-08-21 martijn io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT);
354 ce062d50 2019-08-21 martijn io_reload(io);
358 ce062d50 2019-08-21 martijn io_resume(struct io *io, int dir)
360 ce062d50 2019-08-21 martijn io_debug("io_resume(%p, %x)\n", io, dir);
362 ce062d50 2019-08-21 martijn io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT));
363 ce062d50 2019-08-21 martijn io_reload(io);
367 ce062d50 2019-08-21 martijn io_set_read(struct io *io)
369 ce062d50 2019-08-21 martijn int mode;
371 ce062d50 2019-08-21 martijn io_debug("io_set_read(%p)\n", io);
373 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
374 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_WRITE))
375 ce062d50 2019-08-21 martijn errx(1, "io_set_read(): full-duplex or reading");
377 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
378 ce062d50 2019-08-21 martijn io->flags |= IO_READ;
379 ce062d50 2019-08-21 martijn io_reload(io);
383 ce062d50 2019-08-21 martijn io_set_write(struct io *io)
385 ce062d50 2019-08-21 martijn int mode;
387 ce062d50 2019-08-21 martijn io_debug("io_set_write(%p)\n", io);
389 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
390 ce062d50 2019-08-21 martijn if (!(mode == 0 || mode == IO_READ))
391 ce062d50 2019-08-21 martijn errx(1, "io_set_write(): full-duplex or writing");
393 ce062d50 2019-08-21 martijn io->flags &= ~IO_RW;
394 ce062d50 2019-08-21 martijn io->flags |= IO_WRITE;
395 ce062d50 2019-08-21 martijn io_reload(io);
398 ce062d50 2019-08-21 martijn const char *
399 ce062d50 2019-08-21 martijn io_error(struct io *io)
401 ce062d50 2019-08-21 martijn return io->error;
405 ce062d50 2019-08-21 martijn io_tls(struct io *io)
407 ce062d50 2019-08-21 martijn return io->tls;
411 ce062d50 2019-08-21 martijn io_fileno(struct io *io)
413 ce062d50 2019-08-21 martijn return io->sock;
417 ce062d50 2019-08-21 martijn io_paused(struct io *io, int what)
419 ce062d50 2019-08-21 martijn return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what;
423 ce062d50 2019-08-21 martijn * Buffered output functions
427 ce062d50 2019-08-21 martijn io_write(struct io *io, const void *buf, size_t len)
431 ce062d50 2019-08-21 martijn r = iobuf_queue(&io->iobuf, buf, len);
433 ce062d50 2019-08-21 martijn io_reload(io);
435 ce062d50 2019-08-21 martijn return r;
439 ce062d50 2019-08-21 martijn io_writev(struct io *io, const struct iovec *iov, int iovcount)
443 ce062d50 2019-08-21 martijn r = iobuf_queuev(&io->iobuf, iov, iovcount);
445 ce062d50 2019-08-21 martijn io_reload(io);
447 ce062d50 2019-08-21 martijn return r;
451 ce062d50 2019-08-21 martijn io_print(struct io *io, const char *s)
453 ce062d50 2019-08-21 martijn return io_write(io, s, strlen(s));
457 ce062d50 2019-08-21 martijn io_printf(struct io *io, const char *fmt, ...)
459 ce062d50 2019-08-21 martijn va_list ap;
462 ce062d50 2019-08-21 martijn va_start(ap, fmt);
463 ce062d50 2019-08-21 martijn r = io_vprintf(io, fmt, ap);
464 ce062d50 2019-08-21 martijn va_end(ap);
466 ce062d50 2019-08-21 martijn return r;
470 ce062d50 2019-08-21 martijn io_vprintf(struct io *io, const char *fmt, va_list ap)
473 ce062d50 2019-08-21 martijn char *buf;
474 ce062d50 2019-08-21 martijn int len;
476 ce062d50 2019-08-21 martijn len = vasprintf(&buf, fmt, ap);
477 ce062d50 2019-08-21 martijn if (len == -1)
478 ce062d50 2019-08-21 martijn return -1;
479 ce062d50 2019-08-21 martijn len = io_write(io, buf, len);
480 ce062d50 2019-08-21 martijn free(buf);
482 ce062d50 2019-08-21 martijn return len;
486 ce062d50 2019-08-21 martijn io_queued(struct io *io)
488 ce062d50 2019-08-21 martijn return iobuf_queued(&io->iobuf);
492 ce062d50 2019-08-21 martijn * Buffered input functions
496 ce062d50 2019-08-21 martijn io_data(struct io *io)
498 ce062d50 2019-08-21 martijn return iobuf_data(&io->iobuf);
502 ce062d50 2019-08-21 martijn io_datalen(struct io *io)
504 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
508 ce062d50 2019-08-21 martijn io_getline(struct io *io, size_t *sz)
510 ce062d50 2019-08-21 martijn return iobuf_getline(&io->iobuf, sz);
514 ce062d50 2019-08-21 martijn io_drop(struct io *io, size_t sz)
516 ce062d50 2019-08-21 martijn return iobuf_drop(&io->iobuf, sz);
520 ce062d50 2019-08-21 martijn #define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
521 ce062d50 2019-08-21 martijn #define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
524 ce062d50 2019-08-21 martijn * Setup the necessary events as required by the current io state,
525 ce062d50 2019-08-21 martijn * honouring duplex mode and i/o pauses.
528 ce062d50 2019-08-21 martijn io_reload(struct io *io)
530 ce062d50 2019-08-21 martijn short events;
532 ce062d50 2019-08-21 martijn /* io will be reloaded at release time */
533 ce062d50 2019-08-21 martijn if (io->flags & IO_HELD)
536 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
538 ce062d50 2019-08-21 martijn #ifdef IO_TLS
539 ce062d50 2019-08-21 martijn if (io->tls) {
540 ce062d50 2019-08-21 martijn io_reload_tls(io);
545 ce062d50 2019-08-21 martijn io_debug("io_reload(%p)\n", io);
547 ce062d50 2019-08-21 martijn events = 0;
548 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
549 ce062d50 2019-08-21 martijn events = EV_READ;
550 ce062d50 2019-08-21 martijn if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
551 ce062d50 2019-08-21 martijn events |= EV_WRITE;
553 ce062d50 2019-08-21 martijn io_reset(io, events, io_dispatch);
556 ce062d50 2019-08-21 martijn /* Set the requested event. */
558 ce062d50 2019-08-21 martijn io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
560 ce062d50 2019-08-21 martijn struct timeval tv, *ptv;
562 ce062d50 2019-08-21 martijn io_debug("io_reset(%p, %s, %p) -> %s\n",
563 ce062d50 2019-08-21 martijn io, io_evstr(events), dispatch, io_strio(io));
566 ce062d50 2019-08-21 martijn * Indicate that the event has already been reset so that reload
567 ce062d50 2019-08-21 martijn * is not called on frame_leave.
569 ce062d50 2019-08-21 martijn io->flags |= IO_RESET;
571 681d8d49 2022-03-28 martijn if (event_initialized(&io->ev) &&
572 681d8d49 2022-03-28 martijn event_pending(&io->ev, EV_READ|EV_WRITE, NULL))
573 ce062d50 2019-08-21 martijn event_del(&io->ev);
576 ce062d50 2019-08-21 martijn * The io is paused by the user, so we don't want the timeout to be
577 ce062d50 2019-08-21 martijn * effective.
579 ce062d50 2019-08-21 martijn if (events == 0)
582 ce062d50 2019-08-21 martijn event_set(&io->ev, io->sock, events, dispatch, io);
583 ce062d50 2019-08-21 martijn if (io->timeout >= 0) {
584 ce062d50 2019-08-21 martijn tv.tv_sec = io->timeout / 1000;
585 ce062d50 2019-08-21 martijn tv.tv_usec = (io->timeout % 1000) * 1000;
586 ce062d50 2019-08-21 martijn ptv = &tv;
588 ce062d50 2019-08-21 martijn ptv = NULL;
590 ce062d50 2019-08-21 martijn event_add(&io->ev, ptv);
594 ce062d50 2019-08-21 martijn io_pending(struct io *io)
596 ce062d50 2019-08-21 martijn return iobuf_len(&io->iobuf);
599 ce062d50 2019-08-21 martijn const char*
600 ce062d50 2019-08-21 martijn io_strflags(int flags)
602 ce062d50 2019-08-21 martijn static char buf[64];
604 ce062d50 2019-08-21 martijn buf[0] = '\0';
606 ce062d50 2019-08-21 martijn switch (flags & IO_RW) {
608 ce062d50 2019-08-21 martijn (void)strlcat(buf, "rw", sizeof buf);
610 ce062d50 2019-08-21 martijn case IO_READ:
611 ce062d50 2019-08-21 martijn (void)strlcat(buf, "R", sizeof buf);
613 ce062d50 2019-08-21 martijn case IO_WRITE:
614 ce062d50 2019-08-21 martijn (void)strlcat(buf, "W", sizeof buf);
616 ce062d50 2019-08-21 martijn case IO_RW:
617 ce062d50 2019-08-21 martijn (void)strlcat(buf, "RW", sizeof buf);
621 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_IN)
622 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PI", sizeof buf);
623 ce062d50 2019-08-21 martijn if (flags & IO_PAUSE_OUT)
624 ce062d50 2019-08-21 martijn (void)strlcat(buf, ",F_PO", sizeof buf);
626 ce062d50 2019-08-21 martijn return buf;
629 ce062d50 2019-08-21 martijn const char*
630 ce062d50 2019-08-21 martijn io_evstr(short ev)
632 ce062d50 2019-08-21 martijn static char buf[64];
633 ce062d50 2019-08-21 martijn char buf2[16];
637 ce062d50 2019-08-21 martijn buf[0] = '\0';
639 ce062d50 2019-08-21 martijn if (ev == 0) {
640 ce062d50 2019-08-21 martijn (void)strlcat(buf, "<NONE>", sizeof(buf));
641 ce062d50 2019-08-21 martijn return buf;
644 ce062d50 2019-08-21 martijn if (ev & EV_TIMEOUT) {
645 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
646 ce062d50 2019-08-21 martijn ev &= ~EV_TIMEOUT;
650 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
652 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
653 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_READ", sizeof(buf));
654 ce062d50 2019-08-21 martijn ev &= ~EV_READ;
658 ce062d50 2019-08-21 martijn if (ev & EV_WRITE) {
660 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
661 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_WRITE", sizeof(buf));
662 ce062d50 2019-08-21 martijn ev &= ~EV_WRITE;
666 ce062d50 2019-08-21 martijn if (ev & EV_SIGNAL) {
668 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
669 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
670 ce062d50 2019-08-21 martijn ev &= ~EV_SIGNAL;
674 ce062d50 2019-08-21 martijn if (ev) {
676 ce062d50 2019-08-21 martijn (void)strlcat(buf, "|", sizeof(buf));
677 ce062d50 2019-08-21 martijn (void)strlcat(buf, "EV_?=0x", sizeof(buf));
678 ce062d50 2019-08-21 martijn (void)snprintf(buf2, sizeof(buf2), "%hx", ev);
679 ce062d50 2019-08-21 martijn (void)strlcat(buf, buf2, sizeof(buf));
682 ce062d50 2019-08-21 martijn return buf;
686 957c2372 2019-11-14 martijn io_dispatch(__unused int fd, short ev, void *humppa)
688 ce062d50 2019-08-21 martijn struct io *io = humppa;
689 ce062d50 2019-08-21 martijn size_t w;
690 ce062d50 2019-08-21 martijn ssize_t n;
691 ce062d50 2019-08-21 martijn int saved_errno;
693 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch", io, ev);
695 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
696 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
697 ce062d50 2019-08-21 martijn goto leave;
700 ce062d50 2019-08-21 martijn if (ev & EV_WRITE && (w = io_queued(io))) {
701 ce062d50 2019-08-21 martijn if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) {
702 ce062d50 2019-08-21 martijn if (n == IOBUF_WANT_WRITE) /* kqueue bug? */
703 ce062d50 2019-08-21 martijn goto read;
704 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
705 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
707 ce062d50 2019-08-21 martijn saved_errno = errno;
708 ce062d50 2019-08-21 martijn io->error = strerror(errno);
709 ce062d50 2019-08-21 martijn errno = saved_errno;
710 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
712 ce062d50 2019-08-21 martijn goto leave;
714 ce062d50 2019-08-21 martijn if (w > io->lowat && w - n <= io->lowat)
715 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
719 ce062d50 2019-08-21 martijn if (ev & EV_READ) {
720 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
721 ce062d50 2019-08-21 martijn if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) {
722 ce062d50 2019-08-21 martijn if (n == IOBUF_CLOSED)
723 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
725 ce062d50 2019-08-21 martijn saved_errno = errno;
726 ce062d50 2019-08-21 martijn io->error = strerror(errno);
727 ce062d50 2019-08-21 martijn errno = saved_errno;
728 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
730 ce062d50 2019-08-21 martijn goto leave;
733 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
737 ce062d50 2019-08-21 martijn io_frame_leave(io);
741 ce062d50 2019-08-21 martijn io_callback(struct io *io, int evt)
743 ce062d50 2019-08-21 martijn io->cb(io, evt, io->arg);
747 ce062d50 2019-08-21 martijn io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa)
749 ce062d50 2019-08-21 martijn int sock, errno_save;
751 ce062d50 2019-08-21 martijn if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
752 ce062d50 2019-08-21 martijn goto fail;
754 ce062d50 2019-08-21 martijn io_set_nonblocking(sock);
755 ce062d50 2019-08-21 martijn io_set_nolinger(sock);
757 957c2372 2019-11-14 martijn if (bsa && bind(sock, bsa, SA_LEN(bsa)) == -1)
758 ce062d50 2019-08-21 martijn goto fail;
760 957c2372 2019-11-14 martijn if (connect(sock, sa, SA_LEN(sa)) == -1)
761 ce062d50 2019-08-21 martijn if (errno != EINPROGRESS)
762 ce062d50 2019-08-21 martijn goto fail;
764 ce062d50 2019-08-21 martijn io->sock = sock;
765 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect);
767 ce062d50 2019-08-21 martijn return (sock);
770 ce062d50 2019-08-21 martijn if (sock != -1) {
771 ce062d50 2019-08-21 martijn errno_save = errno;
772 ce062d50 2019-08-21 martijn close(sock);
773 ce062d50 2019-08-21 martijn errno = errno_save;
774 ce062d50 2019-08-21 martijn io->error = strerror(errno);
776 ce062d50 2019-08-21 martijn return (-1);
780 ce062d50 2019-08-21 martijn io_dispatch_connect(int fd, short ev, void *humppa)
782 ce062d50 2019-08-21 martijn struct io *io = humppa;
783 ce062d50 2019-08-21 martijn int r, e;
784 ce062d50 2019-08-21 martijn socklen_t sl;
786 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect", io, ev);
788 ce062d50 2019-08-21 martijn if (ev == EV_TIMEOUT) {
789 ce062d50 2019-08-21 martijn close(fd);
790 ce062d50 2019-08-21 martijn io->sock = -1;
791 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
792 ce062d50 2019-08-21 martijn } else {
793 ce062d50 2019-08-21 martijn sl = sizeof(e);
794 ce062d50 2019-08-21 martijn r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
795 ce062d50 2019-08-21 martijn if (r == -1) {
796 ce062d50 2019-08-21 martijn warn("io_dispatch_connect: getsockopt");
797 ce062d50 2019-08-21 martijn e = errno;
799 ce062d50 2019-08-21 martijn if (e) {
800 ce062d50 2019-08-21 martijn close(fd);
801 ce062d50 2019-08-21 martijn io->sock = -1;
802 ce062d50 2019-08-21 martijn io->error = strerror(e);
803 ce062d50 2019-08-21 martijn io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
806 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
807 ce062d50 2019-08-21 martijn io_callback(io, IO_CONNECTED);
811 ce062d50 2019-08-21 martijn io_frame_leave(io);
814 ce062d50 2019-08-21 martijn #ifdef IO_TLS
816 ce062d50 2019-08-21 martijn static const char*
817 ce062d50 2019-08-21 martijn io_tls_error(void)
819 ce062d50 2019-08-21 martijn static char buf[128];
820 ce062d50 2019-08-21 martijn unsigned long e;
822 ce062d50 2019-08-21 martijn e = ERR_peek_last_error();
823 ce062d50 2019-08-21 martijn if (e) {
824 ce062d50 2019-08-21 martijn ERR_error_string(e, buf);
825 ce062d50 2019-08-21 martijn return (buf);
828 ce062d50 2019-08-21 martijn return ("No TLS error");
832 ce062d50 2019-08-21 martijn io_start_tls(struct io *io, void *tls)
834 ce062d50 2019-08-21 martijn int mode;
836 ce062d50 2019-08-21 martijn mode = io->flags & IO_RW;
837 ce062d50 2019-08-21 martijn if (mode == 0 || mode == IO_RW)
838 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): full-duplex or unset");
840 ce062d50 2019-08-21 martijn if (io->tls)
841 ce062d50 2019-08-21 martijn errx(1, "io_start_tls(): TLS already started");
842 ce062d50 2019-08-21 martijn io->tls = tls;
844 ce062d50 2019-08-21 martijn if (SSL_set_fd(io->tls, io->sock) == 0) {
845 ce062d50 2019-08-21 martijn ssl_error("io_start_tls:SSL_set_fd");
846 ce062d50 2019-08-21 martijn return (-1);
849 ce062d50 2019-08-21 martijn if (mode == IO_WRITE) {
850 ce062d50 2019-08-21 martijn io->state = IO_STATE_CONNECT_TLS;
851 ce062d50 2019-08-21 martijn SSL_set_connect_state(io->tls);
852 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
853 ce062d50 2019-08-21 martijn } else {
854 ce062d50 2019-08-21 martijn io->state = IO_STATE_ACCEPT_TLS;
855 ce062d50 2019-08-21 martijn SSL_set_accept_state(io->tls);
856 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
859 ce062d50 2019-08-21 martijn return (0);
863 ce062d50 2019-08-21 martijn io_dispatch_accept_tls(int fd, short event, void *humppa)
865 ce062d50 2019-08-21 martijn struct io *io = humppa;
866 ce062d50 2019-08-21 martijn int e, ret;
868 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_accept_tls", io, event);
870 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
871 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
872 ce062d50 2019-08-21 martijn goto leave;
875 ce062d50 2019-08-21 martijn if ((ret = SSL_accept(io->tls)) > 0) {
876 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
877 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
878 ce062d50 2019-08-21 martijn goto leave;
881 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
882 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
883 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_accept_tls);
885 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
886 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_accept_tls);
888 ce062d50 2019-08-21 martijn default:
889 ce062d50 2019-08-21 martijn io->error = io_tls_error();
890 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_accept_tls:SSL_accept");
891 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
896 ce062d50 2019-08-21 martijn io_frame_leave(io);
900 ce062d50 2019-08-21 martijn io_dispatch_connect_tls(int fd, short event, void *humppa)
902 ce062d50 2019-08-21 martijn struct io *io = humppa;
903 ce062d50 2019-08-21 martijn int e, ret;
905 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_connect_tls", io, event);
907 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
908 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
909 ce062d50 2019-08-21 martijn goto leave;
912 ce062d50 2019-08-21 martijn if ((ret = SSL_connect(io->tls)) > 0) {
913 ce062d50 2019-08-21 martijn io->state = IO_STATE_UP;
914 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSREADY);
915 ce062d50 2019-08-21 martijn goto leave;
918 ce062d50 2019-08-21 martijn switch ((e = SSL_get_error(io->tls, ret))) {
919 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
920 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_connect_tls);
922 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
923 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_connect_tls);
925 ce062d50 2019-08-21 martijn default:
926 ce062d50 2019-08-21 martijn io->error = io_tls_error();
927 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_connect_ssl:SSL_connect");
928 ce062d50 2019-08-21 martijn io_callback(io, IO_TLSERROR);
933 ce062d50 2019-08-21 martijn io_frame_leave(io);
937 ce062d50 2019-08-21 martijn io_dispatch_read_tls(int fd, short event, void *humppa)
939 ce062d50 2019-08-21 martijn struct io *io = humppa;
940 ce062d50 2019-08-21 martijn int n, saved_errno;
942 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_read_tls", io, event);
944 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
945 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
946 ce062d50 2019-08-21 martijn goto leave;
950 ce062d50 2019-08-21 martijn iobuf_normalize(&io->iobuf);
951 ce062d50 2019-08-21 martijn switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) {
952 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
953 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_read_tls);
955 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
956 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_read_tls);
958 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
959 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
961 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
962 ce062d50 2019-08-21 martijn saved_errno = errno;
963 ce062d50 2019-08-21 martijn io->error = strerror(errno);
964 ce062d50 2019-08-21 martijn errno = saved_errno;
965 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
967 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
968 ce062d50 2019-08-21 martijn io->error = io_tls_error();
969 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_read_tls:SSL_read");
970 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
972 ce062d50 2019-08-21 martijn default:
973 ce062d50 2019-08-21 martijn io_debug("io_dispatch_read_tls(...) -> r=%d\n", n);
974 ce062d50 2019-08-21 martijn io_callback(io, IO_DATAIN);
975 ce062d50 2019-08-21 martijn if (current == io && IO_READING(io) && SSL_pending(io->tls))
976 ce062d50 2019-08-21 martijn goto again;
980 ce062d50 2019-08-21 martijn io_frame_leave(io);
984 ce062d50 2019-08-21 martijn io_dispatch_write_tls(int fd, short event, void *humppa)
986 ce062d50 2019-08-21 martijn struct io *io = humppa;
987 ce062d50 2019-08-21 martijn int n, saved_errno;
988 ce062d50 2019-08-21 martijn size_t w2, w;
990 ce062d50 2019-08-21 martijn io_frame_enter("io_dispatch_write_tls", io, event);
992 ce062d50 2019-08-21 martijn if (event == EV_TIMEOUT) {
993 ce062d50 2019-08-21 martijn io_callback(io, IO_TIMEOUT);
994 ce062d50 2019-08-21 martijn goto leave;
997 ce062d50 2019-08-21 martijn w = io_queued(io);
998 ce062d50 2019-08-21 martijn switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) {
999 ce062d50 2019-08-21 martijn case IOBUF_WANT_READ:
1000 ce062d50 2019-08-21 martijn io_reset(io, EV_READ, io_dispatch_write_tls);
1002 ce062d50 2019-08-21 martijn case IOBUF_WANT_WRITE:
1003 ce062d50 2019-08-21 martijn io_reset(io, EV_WRITE, io_dispatch_write_tls);
1005 ce062d50 2019-08-21 martijn case IOBUF_CLOSED:
1006 ce062d50 2019-08-21 martijn io_callback(io, IO_DISCONNECTED);
1008 ce062d50 2019-08-21 martijn case IOBUF_ERROR:
1009 ce062d50 2019-08-21 martijn saved_errno = errno;
1010 ce062d50 2019-08-21 martijn io->error = strerror(errno);
1011 ce062d50 2019-08-21 martijn errno = saved_errno;
1012 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1014 ce062d50 2019-08-21 martijn case IOBUF_TLSERROR:
1015 ce062d50 2019-08-21 martijn io->error = io_tls_error();
1016 ce062d50 2019-08-21 martijn ssl_error("io_dispatch_write_tls:SSL_write");
1017 ce062d50 2019-08-21 martijn io_callback(io, IO_ERROR);
1019 ce062d50 2019-08-21 martijn default:
1020 ce062d50 2019-08-21 martijn io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
1021 ce062d50 2019-08-21 martijn w2 = io_queued(io);
1022 ce062d50 2019-08-21 martijn if (w > io->lowat && w2 <= io->lowat)
1023 ce062d50 2019-08-21 martijn io_callback(io, IO_LOWAT);
1028 ce062d50 2019-08-21 martijn io_frame_leave(io);
1032 ce062d50 2019-08-21 martijn io_reload_tls(struct io *io)
1034 ce062d50 2019-08-21 martijn short ev = 0;
1035 ce062d50 2019-08-21 martijn void (*dispatch)(int, short, void*) = NULL;
1037 ce062d50 2019-08-21 martijn switch (io->state) {
1038 ce062d50 2019-08-21 martijn case IO_STATE_CONNECT_TLS:
1039 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1040 ce062d50 2019-08-21 martijn dispatch = io_dispatch_connect_tls;
1042 ce062d50 2019-08-21 martijn case IO_STATE_ACCEPT_TLS:
1043 ce062d50 2019-08-21 martijn ev = EV_READ;
1044 ce062d50 2019-08-21 martijn dispatch = io_dispatch_accept_tls;
1046 ce062d50 2019-08-21 martijn case IO_STATE_UP:
1047 ce062d50 2019-08-21 martijn ev = 0;
1048 ce062d50 2019-08-21 martijn if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
1049 ce062d50 2019-08-21 martijn ev = EV_READ;
1050 ce062d50 2019-08-21 martijn dispatch = io_dispatch_read_tls;
1052 ce062d50 2019-08-21 martijn else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
1053 ce062d50 2019-08-21 martijn io_queued(io)) {
1054 ce062d50 2019-08-21 martijn ev = EV_WRITE;
1055 ce062d50 2019-08-21 martijn dispatch = io_dispatch_write_tls;
1057 ce062d50 2019-08-21 martijn if (!ev)
1058 ce062d50 2019-08-21 martijn return; /* paused */
1060 ce062d50 2019-08-21 martijn default:
1061 ce062d50 2019-08-21 martijn errx(1, "io_reload_tls(): bad state");
1064 ce062d50 2019-08-21 martijn io_reset(io, ev, dispatch);
1067 ce062d50 2019-08-21 martijn #endif /* IO_TLS */