Add metrics for when a cast is started or stopped from the system tray.
[chromium-blink-merge.git] / third_party / libevent / kqueue.c
blob556b73c0a9ca9f1ef92e10e1ac197a455df47e5f
1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
3 /*
4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5 * All rights reserved.
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.
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.
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #define _GNU_SOURCE 1
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #else
39 #include <sys/_libevent_time.h>
40 #endif
41 #include <sys/queue.h>
42 #include <sys/event.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <assert.h>
50 #ifdef HAVE_INTTYPES_H
51 #include <inttypes.h>
52 #endif
54 /* Some platforms apparently define the udata field of struct kevent as
55 * intptr_t, whereas others define it as void*. There doesn't seem to be an
56 * easy way to tell them apart via autoconf, so we need to use OS macros. */
57 #if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
58 #define PTR_TO_UDATA(x) ((intptr_t)(x))
59 #else
60 #define PTR_TO_UDATA(x) (x)
61 #endif
63 #include "event.h"
64 #include "event-internal.h"
65 #include "log.h"
67 #define EVLIST_X_KQINKERNEL 0x1000
69 #define NEVENT 64
71 struct kqop {
72 struct kevent *changes;
73 int nchanges;
74 struct kevent *events;
75 struct event_list evsigevents[NSIG];
76 int nevents;
77 int kq;
78 pid_t pid;
81 static void *kq_init (struct event_base *);
82 static int kq_add (void *, struct event *);
83 static int kq_del (void *, struct event *);
84 static int kq_dispatch (struct event_base *, void *, struct timeval *);
85 static int kq_insert (struct kqop *, struct kevent *);
86 static void kq_dealloc (struct event_base *, void *);
88 const struct eventop kqops = {
89 "kqueue",
90 kq_init,
91 kq_add,
92 kq_del,
93 kq_dispatch,
94 kq_dealloc,
95 1 /* need reinit */
98 static void *
99 kq_init(struct event_base *base)
101 int i, kq;
102 struct kqop *kqueueop;
104 /* Disable kqueue when this environment variable is set */
105 if (evutil_getenv("EVENT_NOKQUEUE"))
106 return (NULL);
108 if (!(kqueueop = calloc(1, sizeof(struct kqop))))
109 return (NULL);
111 /* Initalize the kernel queue */
113 if ((kq = kqueue()) == -1) {
114 event_warn("kqueue");
115 free (kqueueop);
116 return (NULL);
119 kqueueop->kq = kq;
121 kqueueop->pid = getpid();
123 /* Initalize fields */
124 kqueueop->changes = malloc(NEVENT * sizeof(struct kevent));
125 if (kqueueop->changes == NULL) {
126 free (kqueueop);
127 return (NULL);
129 kqueueop->events = malloc(NEVENT * sizeof(struct kevent));
130 if (kqueueop->events == NULL) {
131 free (kqueueop->changes);
132 free (kqueueop);
133 return (NULL);
135 kqueueop->nevents = NEVENT;
137 /* we need to keep track of multiple events per signal */
138 for (i = 0; i < NSIG; ++i) {
139 TAILQ_INIT(&kqueueop->evsigevents[i]);
142 /* Check for Mac OS X kqueue bug. */
143 kqueueop->changes[0].ident = -1;
144 kqueueop->changes[0].filter = EVFILT_READ;
145 kqueueop->changes[0].flags = EV_ADD;
147 * If kqueue works, then kevent will succeed, and it will
148 * stick an error in events[0]. If kqueue is broken, then
149 * kevent will fail.
151 if (kevent(kq,
152 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
153 kqueueop->events[0].ident != -1 ||
154 kqueueop->events[0].flags != EV_ERROR) {
155 event_warn("%s: detected broken kqueue; not using.", __func__);
156 free(kqueueop->changes);
157 free(kqueueop->events);
158 free(kqueueop);
159 close(kq);
160 return (NULL);
163 return (kqueueop);
166 static int
167 kq_insert(struct kqop *kqop, struct kevent *kev)
169 int nevents = kqop->nevents;
171 if (kqop->nchanges == nevents) {
172 struct kevent *newchange;
173 struct kevent *newresult;
175 nevents *= 2;
177 newchange = realloc(kqop->changes,
178 nevents * sizeof(struct kevent));
179 if (newchange == NULL) {
180 event_warn("%s: malloc", __func__);
181 return (-1);
183 kqop->changes = newchange;
185 newresult = realloc(kqop->events,
186 nevents * sizeof(struct kevent));
189 * If we fail, we don't have to worry about freeing,
190 * the next realloc will pick it up.
192 if (newresult == NULL) {
193 event_warn("%s: malloc", __func__);
194 return (-1);
196 kqop->events = newresult;
198 kqop->nevents = nevents;
201 memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent));
203 event_debug(("%s: fd %d %s%s",
204 __func__, (int)kev->ident,
205 kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE",
206 kev->flags == EV_DELETE ? " (del)" : ""));
208 return (0);
211 static void
212 kq_sighandler(int sig)
214 /* Do nothing here */
217 static int
218 kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
220 struct kqop *kqop = arg;
221 struct kevent *changes = kqop->changes;
222 struct kevent *events = kqop->events;
223 struct event *ev;
224 struct timespec ts, *ts_p = NULL;
225 int i, res;
227 if (tv != NULL) {
228 TIMEVAL_TO_TIMESPEC(tv, &ts);
229 ts_p = &ts;
232 res = kevent(kqop->kq, changes, kqop->nchanges,
233 events, kqop->nevents, ts_p);
234 kqop->nchanges = 0;
235 if (res == -1) {
236 if (errno != EINTR) {
237 event_warn("kevent");
238 return (-1);
241 return (0);
244 event_debug(("%s: kevent reports %d", __func__, res));
246 for (i = 0; i < res; i++) {
247 int which = 0;
249 if (events[i].flags & EV_ERROR) {
251 * Error messages that can happen, when a delete fails.
252 * EBADF happens when the file discriptor has been
253 * closed,
254 * ENOENT when the file discriptor was closed and
255 * then reopened.
256 * EINVAL for some reasons not understood; EINVAL
257 * should not be returned ever; but FreeBSD does :-\
258 * An error is also indicated when a callback deletes
259 * an event we are still processing. In that case
260 * the data field is set to ENOENT.
262 if (events[i].data == EBADF ||
263 events[i].data == EINVAL ||
264 events[i].data == ENOENT)
265 continue;
266 errno = events[i].data;
267 return (-1);
270 if (events[i].filter == EVFILT_READ) {
271 which |= EV_READ;
272 } else if (events[i].filter == EVFILT_WRITE) {
273 which |= EV_WRITE;
274 } else if (events[i].filter == EVFILT_SIGNAL) {
275 which |= EV_SIGNAL;
278 if (!which)
279 continue;
281 if (events[i].filter == EVFILT_SIGNAL) {
282 struct event_list *head =
283 (struct event_list *)events[i].udata;
284 TAILQ_FOREACH(ev, head, ev_signal_next) {
285 event_active(ev, which, events[i].data);
287 } else {
288 ev = (struct event *)events[i].udata;
290 if (!(ev->ev_events & EV_PERSIST))
291 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
293 event_active(ev, which, 1);
297 return (0);
301 static int
302 kq_add(void *arg, struct event *ev)
304 struct kqop *kqop = arg;
305 struct kevent kev;
307 if (ev->ev_events & EV_SIGNAL) {
308 int nsignal = EVENT_SIGNAL(ev);
310 assert(nsignal >= 0 && nsignal < NSIG);
311 if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) {
312 struct timespec timeout = { 0, 0 };
314 memset(&kev, 0, sizeof(kev));
315 kev.ident = nsignal;
316 kev.filter = EVFILT_SIGNAL;
317 kev.flags = EV_ADD;
318 kev.udata = PTR_TO_UDATA(&kqop->evsigevents[nsignal]);
320 /* Be ready for the signal if it is sent any
321 * time between now and the next call to
322 * kq_dispatch. */
323 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
324 return (-1);
326 if (_evsignal_set_handler(ev->ev_base, nsignal,
327 kq_sighandler) == -1)
328 return (-1);
331 TAILQ_INSERT_TAIL(&kqop->evsigevents[nsignal], ev,
332 ev_signal_next);
333 ev->ev_flags |= EVLIST_X_KQINKERNEL;
334 return (0);
337 if (ev->ev_events & EV_READ) {
338 memset(&kev, 0, sizeof(kev));
339 kev.ident = ev->ev_fd;
340 kev.filter = EVFILT_READ;
341 #ifdef NOTE_EOF
342 /* Make it behave like select() and poll() */
343 kev.fflags = NOTE_EOF;
344 #endif
345 kev.flags = EV_ADD;
346 if (!(ev->ev_events & EV_PERSIST))
347 kev.flags |= EV_ONESHOT;
348 kev.udata = PTR_TO_UDATA(ev);
350 if (kq_insert(kqop, &kev) == -1)
351 return (-1);
353 ev->ev_flags |= EVLIST_X_KQINKERNEL;
356 if (ev->ev_events & EV_WRITE) {
357 memset(&kev, 0, sizeof(kev));
358 kev.ident = ev->ev_fd;
359 kev.filter = EVFILT_WRITE;
360 kev.flags = EV_ADD;
361 if (!(ev->ev_events & EV_PERSIST))
362 kev.flags |= EV_ONESHOT;
363 kev.udata = PTR_TO_UDATA(ev);
365 if (kq_insert(kqop, &kev) == -1)
366 return (-1);
368 ev->ev_flags |= EVLIST_X_KQINKERNEL;
371 return (0);
374 static int
375 kq_del(void *arg, struct event *ev)
377 struct kqop *kqop = arg;
378 struct kevent kev;
380 if (!(ev->ev_flags & EVLIST_X_KQINKERNEL))
381 return (0);
383 if (ev->ev_events & EV_SIGNAL) {
384 int nsignal = EVENT_SIGNAL(ev);
385 struct timespec timeout = { 0, 0 };
387 assert(nsignal >= 0 && nsignal < NSIG);
388 TAILQ_REMOVE(&kqop->evsigevents[nsignal], ev, ev_signal_next);
389 if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) {
390 memset(&kev, 0, sizeof(kev));
391 kev.ident = nsignal;
392 kev.filter = EVFILT_SIGNAL;
393 kev.flags = EV_DELETE;
395 /* Because we insert signal events
396 * immediately, we need to delete them
397 * immediately, too */
398 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
399 return (-1);
401 if (_evsignal_restore_handler(ev->ev_base,
402 nsignal) == -1)
403 return (-1);
406 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
407 return (0);
410 if (ev->ev_events & EV_READ) {
411 memset(&kev, 0, sizeof(kev));
412 kev.ident = ev->ev_fd;
413 kev.filter = EVFILT_READ;
414 kev.flags = EV_DELETE;
416 if (kq_insert(kqop, &kev) == -1)
417 return (-1);
419 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
422 if (ev->ev_events & EV_WRITE) {
423 memset(&kev, 0, sizeof(kev));
424 kev.ident = ev->ev_fd;
425 kev.filter = EVFILT_WRITE;
426 kev.flags = EV_DELETE;
428 if (kq_insert(kqop, &kev) == -1)
429 return (-1);
431 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
434 return (0);
437 static void
438 kq_dealloc(struct event_base *base, void *arg)
440 struct kqop *kqop = arg;
442 if (kqop->changes)
443 free(kqop->changes);
444 if (kqop->events)
445 free(kqop->events);
446 if (kqop->kq >= 0 && kqop->pid == getpid())
447 close(kqop->kq);
448 memset(kqop, 0, sizeof(struct kqop));
449 free(kqop);