diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 19:05:22 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 19:05:22 +0100 |
| commit | f2f5b0450d4b7269bafdfb1bd1788303f6d8ce7b (patch) | |
| tree | 346678fd8194ed036b2ee98d7b8e1042d5beac06 /ot_event.c | |
| parent | 78c3cebc389a2b0d426d74900a65b8656edb426d (diff) | |
Add poll/epoll/kqueue wrapper to wait for events
Diffstat (limited to 'ot_event.c')
| -rw-r--r-- | ot_event.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/ot_event.c b/ot_event.c new file mode 100644 index 0000000..d24227d --- /dev/null +++ b/ot_event.c | |||
| @@ -0,0 +1,332 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <errno.h> | ||
| 8 | #include <stdlib.h> | ||
| 9 | #include <string.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | |||
| 12 | /* Opentracker */ | ||
| 13 | #include "ot_event.h" | ||
| 14 | |||
| 15 | /* One caller waits, so the scratch the mechanism needs is kept here rather | ||
| 16 | than allocated per call. */ | ||
| 17 | |||
| 18 | /* poll works everywhere and is what the BSDs get until kqueue is written. | ||
| 19 | Defining OT_EVENT_FORCE_POLL picks it on a platform that would otherwise | ||
| 20 | use something better, which is how both are exercised by the tests. */ | ||
| 21 | #if defined(OT_EVENT_FORCE_POLL) | ||
| 22 | #define OT_EVENT_POLL | ||
| 23 | #elif defined(OT_EVENT_FORCE_KQUEUE) | ||
| 24 | #define OT_EVENT_KQUEUE | ||
| 25 | #elif defined(__linux__) | ||
| 26 | #define OT_EVENT_EPOLL | ||
| 27 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) | ||
| 28 | #define OT_EVENT_KQUEUE | ||
| 29 | #else | ||
| 30 | #define OT_EVENT_POLL | ||
| 31 | #endif | ||
| 32 | |||
| 33 | #ifdef OT_EVENT_EPOLL | ||
| 34 | /* ------------------------------------------------------------- epoll */ | ||
| 35 | |||
| 36 | #include <sys/epoll.h> | ||
| 37 | |||
| 38 | static int g_epoll = -1; | ||
| 39 | static struct epoll_event *g_ready; | ||
| 40 | static int g_ready_space; | ||
| 41 | |||
| 42 | const char *event_mechanism(void) { return "epoll"; } | ||
| 43 | |||
| 44 | int event_init(void) { | ||
| 45 | g_ready = NULL; | ||
| 46 | g_ready_space = 0; | ||
| 47 | return (g_epoll = epoll_create1(EPOLL_CLOEXEC)) == -1 ? -1 : 0; | ||
| 48 | } | ||
| 49 | |||
| 50 | void event_deinit(void) { | ||
| 51 | if (g_epoll != -1) | ||
| 52 | close(g_epoll); | ||
| 53 | free(g_ready); | ||
| 54 | g_epoll = -1; | ||
| 55 | g_ready = NULL; | ||
| 56 | g_ready_space = 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | static int event_ctl(int op, int fd, int what, uint32_t generation) { | ||
| 60 | struct epoll_event ev; | ||
| 61 | |||
| 62 | memset(&ev, 0, sizeof(ev)); | ||
| 63 | ev.events = (what & EVENT_READ ? (uint32_t)EPOLLIN : 0) | (what & EVENT_WRITE ? (uint32_t)EPOLLOUT : 0); | ||
| 64 | /* The generation rides along inside the kernel, so an event says which | ||
| 65 | connection it belongs to without anybody looking it up */ | ||
| 66 | ev.data.u64 = ((uint64_t)generation << 32) | (uint32_t)fd; | ||
| 67 | return epoll_ctl(g_epoll, op, fd, &ev); | ||
| 68 | } | ||
| 69 | |||
| 70 | int event_add(int fd, int what, uint32_t generation) { return event_ctl(EPOLL_CTL_ADD, fd, what, generation); } | ||
| 71 | int event_modify(int fd, int what, uint32_t generation) { return event_ctl(EPOLL_CTL_MOD, fd, what, generation); } | ||
| 72 | int event_del(int fd) { return epoll_ctl(g_epoll, EPOLL_CTL_DEL, fd, NULL); } | ||
| 73 | |||
| 74 | int event_wait(ot_event *out, int max, int timeout_ms) { | ||
| 75 | int count, i; | ||
| 76 | |||
| 77 | if (max > g_ready_space) { | ||
| 78 | struct epoll_event *grown = realloc(g_ready, (size_t)max * sizeof(struct epoll_event)); | ||
| 79 | |||
| 80 | if (!grown) | ||
| 81 | return -1; | ||
| 82 | g_ready = grown; | ||
| 83 | g_ready_space = max; | ||
| 84 | } | ||
| 85 | |||
| 86 | while ((count = epoll_wait(g_epoll, g_ready, max, timeout_ms)) == -1 && errno == EINTR) | ||
| 87 | ; | ||
| 88 | for (i = 0; i < count; ++i) { | ||
| 89 | uint32_t events = g_ready[i].events; | ||
| 90 | |||
| 91 | out[i].fd = (int)(uint32_t)g_ready[i].data.u64; | ||
| 92 | out[i].generation = (uint32_t)(g_ready[i].data.u64 >> 32); | ||
| 93 | /* A hangup or an error is reported as both, so whichever side the caller | ||
| 94 | was waiting on gets to notice */ | ||
| 95 | out[i].what = (events & (EPOLLIN | EPOLLHUP | EPOLLERR) ? EVENT_READ : 0) | | ||
| 96 | (events & (EPOLLOUT | EPOLLHUP | EPOLLERR) ? EVENT_WRITE : 0); | ||
| 97 | } | ||
| 98 | return count; | ||
| 99 | } | ||
| 100 | |||
| 101 | #endif /* OT_EVENT_EPOLL */ | ||
| 102 | |||
| 103 | #ifdef OT_EVENT_KQUEUE | ||
| 104 | /* ------------------------------------------------------------ kqueue */ | ||
| 105 | |||
| 106 | #include <sys/event.h> | ||
| 107 | #include <sys/time.h> | ||
| 108 | #include <sys/types.h> | ||
| 109 | |||
| 110 | static int g_kqueue = -1; | ||
| 111 | static struct kevent *g_ready; | ||
| 112 | static int g_ready_space; | ||
| 113 | |||
| 114 | const char *event_mechanism(void) { return "kqueue"; } | ||
| 115 | |||
| 116 | int event_init(void) { | ||
| 117 | g_ready = NULL; | ||
| 118 | g_ready_space = 0; | ||
| 119 | return (g_kqueue = kqueue()) == -1 ? -1 : 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | void event_deinit(void) { | ||
| 123 | if (g_kqueue != -1) | ||
| 124 | close(g_kqueue); | ||
| 125 | free(g_ready); | ||
| 126 | g_kqueue = -1; | ||
| 127 | g_ready = NULL; | ||
| 128 | g_ready_space = 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | /* kqueue keeps one registration per filter, so reading and writing are two | ||
| 132 | entries. A change of interest is an add of the one wanted and a delete | ||
| 133 | of the one not. The generation travels in udata, which the kernel passes | ||
| 134 | back untouched. */ | ||
| 135 | static int event_change(int fd, int what, uint32_t generation, int deleting) { | ||
| 136 | struct kevent change[2], result[2]; | ||
| 137 | void *udata = (void *)(uintptr_t)(((uint64_t)generation << 32) | (uint32_t)fd); | ||
| 138 | int succeeded = 0, count, i; | ||
| 139 | |||
| 140 | EV_SET(&change[0], (uintptr_t)fd, EVFILT_READ, | ||
| 141 | EV_RECEIPT | ((deleting || !(what & EVENT_READ)) ? EV_DELETE : EV_ADD), 0, 0, udata); | ||
| 142 | EV_SET(&change[1], (uintptr_t)fd, EVFILT_WRITE, | ||
| 143 | EV_RECEIPT | ((deleting || !(what & EVENT_WRITE)) ? EV_DELETE : EV_ADD), 0, 0, udata); | ||
| 144 | |||
| 145 | if ((count = kevent(g_kqueue, change, 2, result, 2, NULL)) == -1) | ||
| 146 | return -1; | ||
| 147 | |||
| 148 | for (i = 0; i < count && i < 2; ++i) { | ||
| 149 | if (!result[i].data) { | ||
| 150 | ++succeeded; | ||
| 151 | continue; | ||
| 152 | } | ||
| 153 | /* Dropping interest we never registered is not a failure */ | ||
| 154 | if ((change[i].flags & EV_DELETE) && result[i].data == ENOENT) | ||
| 155 | continue; | ||
| 156 | errno = (int)result[i].data; | ||
| 157 | return -1; | ||
| 158 | } | ||
| 159 | |||
| 160 | /* Nothing at all was registered or removed, so the descriptor is unknown */ | ||
| 161 | if (!succeeded) { | ||
| 162 | errno = ENOENT; | ||
| 163 | return -1; | ||
| 164 | } | ||
| 165 | return 0; | ||
| 166 | } | ||
| 167 | |||
| 168 | int event_add(int fd, int what, uint32_t generation) { return event_change(fd, what, generation, 0); } | ||
| 169 | int event_modify(int fd, int what, uint32_t generation) { return event_change(fd, what, generation, 0); } | ||
| 170 | int event_del(int fd) { return event_change(fd, 0, 0, 1); } | ||
| 171 | |||
| 172 | int event_wait(ot_event *out, int max, int timeout_ms) { | ||
| 173 | struct timespec timeout; | ||
| 174 | struct timespec *deadline = NULL; | ||
| 175 | int count, i; | ||
| 176 | |||
| 177 | if (max > g_ready_space) { | ||
| 178 | struct kevent *grown = realloc(g_ready, (size_t)max * sizeof(struct kevent)); | ||
| 179 | |||
| 180 | if (!grown) | ||
| 181 | return -1; | ||
| 182 | g_ready = grown; | ||
| 183 | g_ready_space = max; | ||
| 184 | } | ||
| 185 | |||
| 186 | if (timeout_ms >= 0) { | ||
| 187 | timeout.tv_sec = timeout_ms / 1000; | ||
| 188 | timeout.tv_nsec = (long)(timeout_ms % 1000) * 1000000; | ||
| 189 | deadline = &timeout; | ||
| 190 | } | ||
| 191 | |||
| 192 | while ((count = kevent(g_kqueue, NULL, 0, g_ready, max, deadline)) == -1 && errno == EINTR) | ||
| 193 | ; | ||
| 194 | for (i = 0; i < count; ++i) { | ||
| 195 | uint64_t udata = (uint64_t)(uintptr_t)g_ready[i].udata; | ||
| 196 | int eof = (g_ready[i].flags & (EV_EOF | EV_ERROR)) != 0; | ||
| 197 | |||
| 198 | out[i].fd = (int)(uint32_t)udata; | ||
| 199 | out[i].generation = (uint32_t)(udata >> 32); | ||
| 200 | out[i].what = ((g_ready[i].filter == EVFILT_READ || eof) ? EVENT_READ : 0) | | ||
| 201 | ((g_ready[i].filter == EVFILT_WRITE || eof) ? EVENT_WRITE : 0); | ||
| 202 | } | ||
| 203 | return count; | ||
| 204 | } | ||
| 205 | |||
| 206 | #endif /* OT_EVENT_KQUEUE */ | ||
| 207 | |||
| 208 | #ifdef OT_EVENT_POLL | ||
| 209 | /* -------------------------------------------------------------- poll */ | ||
| 210 | |||
| 211 | #include <poll.h> | ||
| 212 | |||
| 213 | /* poll wants its descriptors packed, so the registered ones live in one | ||
| 214 | array and a second maps a descriptor number to its place in it. */ | ||
| 215 | static struct pollfd *g_pfds; | ||
| 216 | static uint32_t *g_generation; | ||
| 217 | static int *g_where; | ||
| 218 | static int g_count, g_space, g_where_space; | ||
| 219 | |||
| 220 | const char *event_mechanism(void) { return "poll"; } | ||
| 221 | |||
| 222 | int event_init(void) { | ||
| 223 | g_pfds = NULL, g_generation = NULL, g_where = NULL; | ||
| 224 | g_count = g_space = g_where_space = 0; | ||
| 225 | return 0; | ||
| 226 | } | ||
| 227 | |||
| 228 | void event_deinit(void) { | ||
| 229 | free(g_pfds), free(g_generation), free(g_where); | ||
| 230 | g_pfds = NULL, g_generation = NULL, g_where = NULL; | ||
| 231 | g_count = g_space = g_where_space = 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | static short event_to_poll(int what) { | ||
| 235 | return (short)((what & EVENT_READ ? POLLIN : 0) | (what & EVENT_WRITE ? POLLOUT : 0)); | ||
| 236 | } | ||
| 237 | |||
| 238 | static int event_room_for(int fd) { | ||
| 239 | if (fd >= g_where_space) { | ||
| 240 | int space = g_where_space ? g_where_space : 64; | ||
| 241 | int *grown; | ||
| 242 | |||
| 243 | while (space <= fd) | ||
| 244 | space *= 2; | ||
| 245 | if (!(grown = realloc(g_where, (size_t)space * sizeof(int)))) | ||
| 246 | return -1; | ||
| 247 | while (g_where_space < space) | ||
| 248 | grown[g_where_space++] = -1; | ||
| 249 | g_where = grown; | ||
| 250 | } | ||
| 251 | |||
| 252 | if (g_count == g_space) { | ||
| 253 | int space = g_space ? g_space * 2 : 64; | ||
| 254 | struct pollfd *pfds = realloc(g_pfds, (size_t)space * sizeof(struct pollfd)); | ||
| 255 | uint32_t *gens; | ||
| 256 | |||
| 257 | if (!pfds) | ||
| 258 | return -1; | ||
| 259 | g_pfds = pfds; | ||
| 260 | if (!(gens = realloc(g_generation, (size_t)space * sizeof(uint32_t)))) | ||
| 261 | return -1; | ||
| 262 | g_generation = gens; | ||
| 263 | g_space = space; | ||
| 264 | } | ||
| 265 | return 0; | ||
| 266 | } | ||
| 267 | |||
| 268 | int event_add(int fd, int what, uint32_t generation) { | ||
| 269 | if (fd < 0 || event_room_for(fd) == -1) | ||
| 270 | return -1; | ||
| 271 | if (g_where[fd] != -1) { | ||
| 272 | errno = EEXIST; | ||
| 273 | return -1; | ||
| 274 | } | ||
| 275 | |||
| 276 | g_pfds[g_count].fd = fd; | ||
| 277 | g_pfds[g_count].events = event_to_poll(what); | ||
| 278 | g_pfds[g_count].revents = 0; | ||
| 279 | g_generation[g_count] = generation; | ||
| 280 | g_where[fd] = g_count++; | ||
| 281 | return 0; | ||
| 282 | } | ||
| 283 | |||
| 284 | int event_modify(int fd, int what, uint32_t generation) { | ||
| 285 | if (fd < 0 || fd >= g_where_space || g_where[fd] == -1) { | ||
| 286 | errno = ENOENT; | ||
| 287 | return -1; | ||
| 288 | } | ||
| 289 | g_pfds[g_where[fd]].events = event_to_poll(what); | ||
| 290 | g_generation[g_where[fd]] = generation; | ||
| 291 | return 0; | ||
| 292 | } | ||
| 293 | |||
| 294 | int event_del(int fd) { | ||
| 295 | int at; | ||
| 296 | |||
| 297 | if (fd < 0 || fd >= g_where_space || (at = g_where[fd]) == -1) { | ||
| 298 | errno = ENOENT; | ||
| 299 | return -1; | ||
| 300 | } | ||
| 301 | |||
| 302 | /* The last one moves into the gap, so the array stays packed */ | ||
| 303 | g_pfds[at] = g_pfds[--g_count]; | ||
| 304 | g_generation[at] = g_generation[g_count]; | ||
| 305 | g_where[g_pfds[at].fd] = at; | ||
| 306 | g_where[fd] = -1; | ||
| 307 | return 0; | ||
| 308 | } | ||
| 309 | |||
| 310 | int event_wait(ot_event *out, int max, int timeout_ms) { | ||
| 311 | int ready, found = 0, i; | ||
| 312 | |||
| 313 | while ((ready = poll(g_pfds, (nfds_t)g_count, timeout_ms)) == -1 && errno == EINTR) | ||
| 314 | ; | ||
| 315 | if (ready <= 0) | ||
| 316 | return ready; | ||
| 317 | |||
| 318 | for (i = 0; i < g_count && found < max; ++i) { | ||
| 319 | short revents = g_pfds[i].revents; | ||
| 320 | |||
| 321 | if (!revents) | ||
| 322 | continue; | ||
| 323 | out[found].fd = g_pfds[i].fd; | ||
| 324 | out[found].generation = g_generation[i]; | ||
| 325 | out[found].what = (revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL) ? EVENT_READ : 0) | | ||
| 326 | (revents & (POLLOUT | POLLHUP | POLLERR | POLLNVAL) ? EVENT_WRITE : 0); | ||
| 327 | ++found; | ||
| 328 | } | ||
| 329 | return found; | ||
| 330 | } | ||
| 331 | |||
| 332 | #endif /* OT_EVENT_POLL */ | ||
