MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
kqueue.c
1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
2 
3 /*
4  * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #else
37 #include <sys/_time.h>
38 #endif
39 #include <sys/queue.h>
40 #include <sys/event.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <assert.h>
48 #ifdef HAVE_INTTYPES_H
49 #include <inttypes.h>
50 #endif
51 
52 /* Some platforms apparently define the udata field of struct kevent as
53  * intptr_t, whereas others define it as void*. There doesn't seem to be an
54  * easy way to tell them apart via autoconf, so we need to use OS macros. */
55 #if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
56 #define PTR_TO_UDATA(x) ((intptr_t)(x))
57 #else
58 #define PTR_TO_UDATA(x) (x)
59 #endif
60 
61 #include "event.h"
62 #include "event-internal.h"
63 #include "log.h"
64 #include "event-internal.h"
65 
66 #define EVLIST_X_KQINKERNEL 0x1000
67 
68 #define NEVENT 64
69 
70 struct kqop {
71  struct kevent *changes;
72  int nchanges;
73  struct kevent *events;
74  struct event_list evsigevents[NSIG];
75  int nevents;
76  int kq;
77  pid_t pid;
78 };
79 
80 static void *kq_init (struct event_base *);
81 static int kq_add (void *, struct event *);
82 static int kq_del (void *, struct event *);
83 static int kq_dispatch (struct event_base *, void *, struct timeval *);
84 static int kq_insert (struct kqop *, struct kevent *);
85 static void kq_dealloc (struct event_base *, void *);
86 
87 const struct eventop kqops = {
88  "kqueue",
89  kq_init,
90  kq_add,
91  kq_del,
92  kq_dispatch,
93  kq_dealloc,
94  1 /* need reinit */
95 };
96 
97 static void *
98 kq_init(struct event_base *base)
99 {
100  int i, kq;
101  struct kqop *kqueueop;
102 
103  /* Disable kqueue when this environment variable is set */
104  if (getenv("EVENT_NOKQUEUE"))
105  return (NULL);
106 
107  if (!(kqueueop = calloc(1, sizeof(struct kqop))))
108  return (NULL);
109 
110  /* Initalize the kernel queue */
111 
112  if ((kq = kqueue()) == -1) {
113  event_warn("kqueue");
114  free (kqueueop);
115  return (NULL);
116  }
117 
118  kqueueop->kq = kq;
119 
120  kqueueop->pid = getpid();
121 
122  /* Initalize fields */
123  kqueueop->changes = malloc(NEVENT * sizeof(struct kevent));
124  if (kqueueop->changes == NULL) {
125  free (kqueueop);
126  return (NULL);
127  }
128  kqueueop->events = malloc(NEVENT * sizeof(struct kevent));
129  if (kqueueop->events == NULL) {
130  free (kqueueop->changes);
131  free (kqueueop);
132  return (NULL);
133  }
134  kqueueop->nevents = NEVENT;
135 
136  /* we need to keep track of multiple events per signal */
137  for (i = 0; i < NSIG; ++i) {
138  TAILQ_INIT(&kqueueop->evsigevents[i]);
139  }
140 
141  /* Check for Mac OS X kqueue bug. */
142  kqueueop->changes[0].ident = -1;
143  kqueueop->changes[0].filter = EVFILT_READ;
144  kqueueop->changes[0].flags = EV_ADD;
145  /*
146  * If kqueue works, then kevent will succeed, and it will
147  * stick an error in events[0]. If kqueue is broken, then
148  * kevent will fail.
149  */
150  if (kevent(kq,
151  kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
152  kqueueop->events[0].ident != -1 ||
153  kqueueop->events[0].flags != EV_ERROR) {
154  event_warn("%s: detected broken kqueue; not using.", __func__);
155  free(kqueueop->changes);
156  free(kqueueop->events);
157  free(kqueueop);
158  close(kq);
159  return (NULL);
160  }
161 
162  return (kqueueop);
163 }
164 
165 static int
166 kq_insert(struct kqop *kqop, struct kevent *kev)
167 {
168  int nevents = kqop->nevents;
169 
170  if (kqop->nchanges == nevents) {
171  struct kevent *newchange;
172  struct kevent *newresult;
173 
174  nevents *= 2;
175 
176  newchange = realloc(kqop->changes,
177  nevents * sizeof(struct kevent));
178  if (newchange == NULL) {
179  event_warn("%s: malloc", __func__);
180  return (-1);
181  }
182  kqop->changes = newchange;
183 
184  newresult = realloc(kqop->events,
185  nevents * sizeof(struct kevent));
186 
187  /*
188  * If we fail, we don't have to worry about freeing,
189  * the next realloc will pick it up.
190  */
191  if (newresult == NULL) {
192  event_warn("%s: malloc", __func__);
193  return (-1);
194  }
195  kqop->events = newresult;
196 
197  kqop->nevents = nevents;
198  }
199 
200  memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent));
201 
202  event_debug(("%s: fd %d %s%s",
203  __func__, (int)kev->ident,
204  kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE",
205  kev->flags == EV_DELETE ? " (del)" : ""));
206 
207  return (0);
208 }
209 
210 static void
211 kq_sighandler(int sig)
212 {
213  /* Do nothing here */
214 }
215 
216 static int
217 kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
218 {
219  struct kqop *kqop = arg;
220  struct kevent *changes = kqop->changes;
221  struct kevent *events = kqop->events;
222  struct event *ev;
223  struct timespec ts, *ts_p = NULL;
224  int i, res;
225 
226  if (tv != NULL) {
227  TIMEVAL_TO_TIMESPEC(tv, &ts);
228  ts_p = &ts;
229  }
230 
231  res = kevent(kqop->kq, changes, kqop->nchanges,
232  events, kqop->nevents, ts_p);
233  kqop->nchanges = 0;
234  if (res == -1) {
235  if (errno != EINTR) {
236  event_warn("kevent");
237  return (-1);
238  }
239 
240  return (0);
241  }
242 
243  event_debug(("%s: kevent reports %d", __func__, res));
244 
245  for (i = 0; i < res; i++) {
246  int which = 0;
247 
248  if (events[i].flags & EV_ERROR) {
249  /*
250  * Error messages that can happen, when a delete fails.
251  * EBADF happens when the file discriptor has been
252  * closed,
253  * ENOENT when the file discriptor was closed and
254  * then reopened.
255  * EINVAL for some reasons not understood; EINVAL
256  * should not be returned ever; but FreeBSD does :-\
257  * An error is also indicated when a callback deletes
258  * an event we are still processing. In that case
259  * the data field is set to ENOENT.
260  */
261  if (events[i].data == EBADF ||
262  events[i].data == EINVAL ||
263  events[i].data == ENOENT)
264  continue;
265  errno = events[i].data;
266  return (-1);
267  }
268 
269  if (events[i].filter == EVFILT_READ) {
270  which |= EV_READ;
271  } else if (events[i].filter == EVFILT_WRITE) {
272  which |= EV_WRITE;
273  } else if (events[i].filter == EVFILT_SIGNAL) {
274  which |= EV_SIGNAL;
275  }
276 
277  if (!which)
278  continue;
279 
280  if (events[i].filter == EVFILT_SIGNAL) {
281  struct event_list *head =
282  (struct event_list *)events[i].udata;
283  TAILQ_FOREACH(ev, head, ev_signal_next) {
284  event_active(ev, which, events[i].data);
285  }
286  } else {
287  ev = (struct event *)events[i].udata;
288 
289  if (!(ev->ev_events & EV_PERSIST))
290  ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
291 
292  event_active(ev, which, 1);
293  }
294  }
295 
296  return (0);
297 }
298 
299 
300 static int
301 kq_add(void *arg, struct event *ev)
302 {
303  struct kqop *kqop = arg;
304  struct kevent kev;
305 
306  if (ev->ev_events & EV_SIGNAL) {
307  int nsignal = EVENT_SIGNAL(ev);
308 
309  assert(nsignal >= 0 && nsignal < NSIG);
310  if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) {
311  struct timespec timeout = { 0, 0 };
312 
313  memset(&kev, 0, sizeof(kev));
314  kev.ident = nsignal;
315  kev.filter = EVFILT_SIGNAL;
316  kev.flags = EV_ADD;
317  kev.udata = PTR_TO_UDATA(&kqop->evsigevents[nsignal]);
318 
319  /* Be ready for the signal if it is sent any
320  * time between now and the next call to
321  * kq_dispatch. */
322  if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
323  return (-1);
324 
325  if (_evsignal_set_handler(ev->ev_base, nsignal,
326  kq_sighandler) == -1)
327  return (-1);
328  }
329 
330  TAILQ_INSERT_TAIL(&kqop->evsigevents[nsignal], ev,
331  ev_signal_next);
332  ev->ev_flags |= EVLIST_X_KQINKERNEL;
333  return (0);
334  }
335 
336  if (ev->ev_events & EV_READ) {
337  memset(&kev, 0, sizeof(kev));
338  kev.ident = ev->ev_fd;
339  kev.filter = EVFILT_READ;
340 #ifdef NOTE_EOF
341  /* Make it behave like select() and poll() */
342  kev.fflags = NOTE_EOF;
343 #endif
344  kev.flags = EV_ADD;
345  if (!(ev->ev_events & EV_PERSIST))
346  kev.flags |= EV_ONESHOT;
347  kev.udata = PTR_TO_UDATA(ev);
348 
349  if (kq_insert(kqop, &kev) == -1)
350  return (-1);
351 
352  ev->ev_flags |= EVLIST_X_KQINKERNEL;
353  }
354 
355  if (ev->ev_events & EV_WRITE) {
356  memset(&kev, 0, sizeof(kev));
357  kev.ident = ev->ev_fd;
358  kev.filter = EVFILT_WRITE;
359  kev.flags = EV_ADD;
360  if (!(ev->ev_events & EV_PERSIST))
361  kev.flags |= EV_ONESHOT;
362  kev.udata = PTR_TO_UDATA(ev);
363 
364  if (kq_insert(kqop, &kev) == -1)
365  return (-1);
366 
367  ev->ev_flags |= EVLIST_X_KQINKERNEL;
368  }
369 
370  return (0);
371 }
372 
373 static int
374 kq_del(void *arg, struct event *ev)
375 {
376  struct kqop *kqop = arg;
377  struct kevent kev;
378 
379  if (!(ev->ev_flags & EVLIST_X_KQINKERNEL))
380  return (0);
381 
382  if (ev->ev_events & EV_SIGNAL) {
383  int nsignal = EVENT_SIGNAL(ev);
384  struct timespec timeout = { 0, 0 };
385 
386  assert(nsignal >= 0 && nsignal < NSIG);
387  TAILQ_REMOVE(&kqop->evsigevents[nsignal], ev, ev_signal_next);
388  if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) {
389  memset(&kev, 0, sizeof(kev));
390  kev.ident = nsignal;
391  kev.filter = EVFILT_SIGNAL;
392  kev.flags = EV_DELETE;
393 
394  /* Because we insert signal events
395  * immediately, we need to delete them
396  * immediately, too */
397  if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
398  return (-1);
399 
400  if (_evsignal_restore_handler(ev->ev_base,
401  nsignal) == -1)
402  return (-1);
403  }
404 
405  ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
406  return (0);
407  }
408 
409  if (ev->ev_events & EV_READ) {
410  memset(&kev, 0, sizeof(kev));
411  kev.ident = ev->ev_fd;
412  kev.filter = EVFILT_READ;
413  kev.flags = EV_DELETE;
414 
415  if (kq_insert(kqop, &kev) == -1)
416  return (-1);
417 
418  ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
419  }
420 
421  if (ev->ev_events & EV_WRITE) {
422  memset(&kev, 0, sizeof(kev));
423  kev.ident = ev->ev_fd;
424  kev.filter = EVFILT_WRITE;
425  kev.flags = EV_DELETE;
426 
427  if (kq_insert(kqop, &kev) == -1)
428  return (-1);
429 
430  ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
431  }
432 
433  return (0);
434 }
435 
436 static void
437 kq_dealloc(struct event_base *base, void *arg)
438 {
439  struct kqop *kqop = arg;
440 
441  if (kqop->changes)
442  free(kqop->changes);
443  if (kqop->events)
444  free(kqop->events);
445  if (kqop->kq >= 0 && kqop->pid == getpid())
446  close(kqop->kq);
447  memset(kqop, 0, sizeof(struct kqop));
448  free(kqop);
449 }