summaryrefslogtreecommitdiff
path: root/ot_event.c
blob: d24227d1e6bd45e5954cbed917869c8a0ae11a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

/* System */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Opentracker */
#include "ot_event.h"

/* One caller waits, so the scratch the mechanism needs is kept here rather
   than allocated per call. */

/* poll works everywhere and is what the BSDs get until kqueue is written.
   Defining OT_EVENT_FORCE_POLL picks it on a platform that would otherwise
   use something better, which is how both are exercised by the tests. */
#if defined(OT_EVENT_FORCE_POLL)
#define OT_EVENT_POLL
#elif defined(OT_EVENT_FORCE_KQUEUE)
#define OT_EVENT_KQUEUE
#elif defined(__linux__)
#define OT_EVENT_EPOLL
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#define OT_EVENT_KQUEUE
#else
#define OT_EVENT_POLL
#endif

#ifdef OT_EVENT_EPOLL
/* ------------------------------------------------------------- epoll */

#include <sys/epoll.h>

static int                 g_epoll = -1;
static struct epoll_event *g_ready;
static int                 g_ready_space;

const char *event_mechanism(void) { return "epoll"; }

int event_init(void) {
  g_ready       = NULL;
  g_ready_space = 0;
  return (g_epoll = epoll_create1(EPOLL_CLOEXEC)) == -1 ? -1 : 0;
}

void event_deinit(void) {
  if (g_epoll != -1)
    close(g_epoll);
  free(g_ready);
  g_epoll       = -1;
  g_ready       = NULL;
  g_ready_space = 0;
}

static int event_ctl(int op, int fd, int what, uint32_t generation) {
  struct epoll_event ev;

  memset(&ev, 0, sizeof(ev));
  ev.events = (what & EVENT_READ ? (uint32_t)EPOLLIN : 0) | (what & EVENT_WRITE ? (uint32_t)EPOLLOUT : 0);
  /* The generation rides along inside the kernel, so an event says which
     connection it belongs to without anybody looking it up */
  ev.data.u64 = ((uint64_t)generation << 32) | (uint32_t)fd;
  return epoll_ctl(g_epoll, op, fd, &ev);
}

int event_add(int fd, int what, uint32_t generation) { return event_ctl(EPOLL_CTL_ADD, fd, what, generation); }
int event_modify(int fd, int what, uint32_t generation) { return event_ctl(EPOLL_CTL_MOD, fd, what, generation); }
int event_del(int fd) { return epoll_ctl(g_epoll, EPOLL_CTL_DEL, fd, NULL); }

int event_wait(ot_event *out, int max, int timeout_ms) {
  int count, i;

  if (max > g_ready_space) {
    struct epoll_event *grown = realloc(g_ready, (size_t)max * sizeof(struct epoll_event));

    if (!grown)
      return -1;
    g_ready       = grown;
    g_ready_space = max;
  }

  while ((count = epoll_wait(g_epoll, g_ready, max, timeout_ms)) == -1 && errno == EINTR)
    ;
  for (i = 0; i < count; ++i) {
    uint32_t events = g_ready[i].events;

    out[i].fd         = (int)(uint32_t)g_ready[i].data.u64;
    out[i].generation = (uint32_t)(g_ready[i].data.u64 >> 32);
    /* A hangup or an error is reported as both, so whichever side the caller
       was waiting on gets to notice */
    out[i].what = (events & (EPOLLIN | EPOLLHUP | EPOLLERR) ? EVENT_READ : 0) |
                  (events & (EPOLLOUT | EPOLLHUP | EPOLLERR) ? EVENT_WRITE : 0);
  }
  return count;
}

#endif /* OT_EVENT_EPOLL */

#ifdef OT_EVENT_KQUEUE
/* ------------------------------------------------------------ kqueue */

#include <sys/event.h>
#include <sys/time.h>
#include <sys/types.h>

static int            g_kqueue = -1;
static struct kevent *g_ready;
static int            g_ready_space;

const char *event_mechanism(void) { return "kqueue"; }

int event_init(void) {
  g_ready       = NULL;
  g_ready_space = 0;
  return (g_kqueue = kqueue()) == -1 ? -1 : 0;
}

void event_deinit(void) {
  if (g_kqueue != -1)
    close(g_kqueue);
  free(g_ready);
  g_kqueue      = -1;
  g_ready       = NULL;
  g_ready_space = 0;
}

/* kqueue keeps one registration per filter, so reading and writing are two
   entries. A change of interest is an add of the one wanted and a delete
   of the one not. The generation travels in udata, which the kernel passes
   back untouched. */
static int event_change(int fd, int what, uint32_t generation, int deleting) {
  struct kevent change[2], result[2];
  void         *udata     = (void *)(uintptr_t)(((uint64_t)generation << 32) | (uint32_t)fd);
  int           succeeded = 0, count, i;

  EV_SET(&change[0], (uintptr_t)fd, EVFILT_READ,
         EV_RECEIPT | ((deleting || !(what & EVENT_READ)) ? EV_DELETE : EV_ADD), 0, 0, udata);
  EV_SET(&change[1], (uintptr_t)fd, EVFILT_WRITE,
         EV_RECEIPT | ((deleting || !(what & EVENT_WRITE)) ? EV_DELETE : EV_ADD), 0, 0, udata);

  if ((count = kevent(g_kqueue, change, 2, result, 2, NULL)) == -1)
    return -1;

  for (i = 0; i < count && i < 2; ++i) {
    if (!result[i].data) {
      ++succeeded;
      continue;
    }
    /* Dropping interest we never registered is not a failure */
    if ((change[i].flags & EV_DELETE) && result[i].data == ENOENT)
      continue;
    errno = (int)result[i].data;
    return -1;
  }

  /* Nothing at all was registered or removed, so the descriptor is unknown */
  if (!succeeded) {
    errno = ENOENT;
    return -1;
  }
  return 0;
}

int event_add(int fd, int what, uint32_t generation) { return event_change(fd, what, generation, 0); }
int event_modify(int fd, int what, uint32_t generation) { return event_change(fd, what, generation, 0); }
int event_del(int fd) { return event_change(fd, 0, 0, 1); }

int event_wait(ot_event *out, int max, int timeout_ms) {
  struct timespec  timeout;
  struct timespec *deadline = NULL;
  int              count, i;

  if (max > g_ready_space) {
    struct kevent *grown = realloc(g_ready, (size_t)max * sizeof(struct kevent));

    if (!grown)
      return -1;
    g_ready       = grown;
    g_ready_space = max;
  }

  if (timeout_ms >= 0) {
    timeout.tv_sec  = timeout_ms / 1000;
    timeout.tv_nsec = (long)(timeout_ms % 1000) * 1000000;
    deadline        = &timeout;
  }

  while ((count = kevent(g_kqueue, NULL, 0, g_ready, max, deadline)) == -1 && errno == EINTR)
    ;
  for (i = 0; i < count; ++i) {
    uint64_t udata = (uint64_t)(uintptr_t)g_ready[i].udata;
    int      eof   = (g_ready[i].flags & (EV_EOF | EV_ERROR)) != 0;

    out[i].fd         = (int)(uint32_t)udata;
    out[i].generation = (uint32_t)(udata >> 32);
    out[i].what       = ((g_ready[i].filter == EVFILT_READ || eof) ? EVENT_READ : 0) |
                  ((g_ready[i].filter == EVFILT_WRITE || eof) ? EVENT_WRITE : 0);
  }
  return count;
}

#endif /* OT_EVENT_KQUEUE */

#ifdef OT_EVENT_POLL
/* -------------------------------------------------------------- poll */

#include <poll.h>

/* poll wants its descriptors packed, so the registered ones live in one
   array and a second maps a descriptor number to its place in it. */
static struct pollfd *g_pfds;
static uint32_t      *g_generation;
static int           *g_where;
static int            g_count, g_space, g_where_space;

const char *event_mechanism(void) { return "poll"; }

int event_init(void) {
  g_pfds = NULL, g_generation = NULL, g_where = NULL;
  g_count = g_space = g_where_space = 0;
  return 0;
}

void event_deinit(void) {
  free(g_pfds), free(g_generation), free(g_where);
  g_pfds = NULL, g_generation = NULL, g_where = NULL;
  g_count = g_space = g_where_space = 0;
}

static short event_to_poll(int what) {
  return (short)((what & EVENT_READ ? POLLIN : 0) | (what & EVENT_WRITE ? POLLOUT : 0));
}

static int event_room_for(int fd) {
  if (fd >= g_where_space) {
    int  space = g_where_space ? g_where_space : 64;
    int *grown;

    while (space <= fd)
      space *= 2;
    if (!(grown = realloc(g_where, (size_t)space * sizeof(int))))
      return -1;
    while (g_where_space < space)
      grown[g_where_space++] = -1;
    g_where = grown;
  }

  if (g_count == g_space) {
    int            space = g_space ? g_space * 2 : 64;
    struct pollfd *pfds  = realloc(g_pfds, (size_t)space * sizeof(struct pollfd));
    uint32_t      *gens;

    if (!pfds)
      return -1;
    g_pfds = pfds;
    if (!(gens = realloc(g_generation, (size_t)space * sizeof(uint32_t))))
      return -1;
    g_generation = gens;
    g_space      = space;
  }
  return 0;
}

int event_add(int fd, int what, uint32_t generation) {
  if (fd < 0 || event_room_for(fd) == -1)
    return -1;
  if (g_where[fd] != -1) {
    errno = EEXIST;
    return -1;
  }

  g_pfds[g_count].fd      = fd;
  g_pfds[g_count].events  = event_to_poll(what);
  g_pfds[g_count].revents = 0;
  g_generation[g_count]   = generation;
  g_where[fd]             = g_count++;
  return 0;
}

int event_modify(int fd, int what, uint32_t generation) {
  if (fd < 0 || fd >= g_where_space || g_where[fd] == -1) {
    errno = ENOENT;
    return -1;
  }
  g_pfds[g_where[fd]].events = event_to_poll(what);
  g_generation[g_where[fd]]  = generation;
  return 0;
}

int event_del(int fd) {
  int at;

  if (fd < 0 || fd >= g_where_space || (at = g_where[fd]) == -1) {
    errno = ENOENT;
    return -1;
  }

  /* The last one moves into the gap, so the array stays packed */
  g_pfds[at]            = g_pfds[--g_count];
  g_generation[at]      = g_generation[g_count];
  g_where[g_pfds[at].fd] = at;
  g_where[fd]            = -1;
  return 0;
}

int event_wait(ot_event *out, int max, int timeout_ms) {
  int ready, found = 0, i;

  while ((ready = poll(g_pfds, (nfds_t)g_count, timeout_ms)) == -1 && errno == EINTR)
    ;
  if (ready <= 0)
    return ready;

  for (i = 0; i < g_count && found < max; ++i) {
    short revents = g_pfds[i].revents;

    if (!revents)
      continue;
    out[found].fd         = g_pfds[i].fd;
    out[found].generation = g_generation[i];
    out[found].what       = (revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL) ? EVENT_READ : 0) |
                      (revents & (POLLOUT | POLLHUP | POLLERR | POLLNVAL) ? EVENT_WRITE : 0);
    ++found;
  }
  return found;
}

#endif /* OT_EVENT_POLL */