2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Micro event library for FreeBSD, designed for a single i/o thread
33 * using kqueue, and having events be persistent by default.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 #ifndef WITHOUT_CAPSICUM
41 #include <capsicum_helpers.h>
52 #include <sys/types.h>
53 #ifndef WITHOUT_CAPSICUM
54 #include <sys/capsicum.h>
56 #include <sys/event.h>
60 #include <pthread_np.h>
66 static pthread_t mevent_tid;
67 static pthread_once_t mevent_once = PTHREAD_ONCE_INIT;
68 static int mevent_timid = 43;
69 static int mevent_pipefd[2];
71 static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
74 void (*me_func)(int, enum ev_type, void *);
75 #define me_msecs me_fd
81 int me_state; /* Desired kevent flags. */
84 LIST_ENTRY(mevent) me_list;
87 static LIST_HEAD(listhead, mevent) global_head, change_head;
92 pthread_mutex_lock(&mevent_lmutex);
98 pthread_mutex_unlock(&mevent_lmutex);
102 mevent_pipe_read(int fd, enum ev_type type, void *param)
104 char buf[MEVENT_MAX];
108 * Drain the pipe read side. The fd is non-blocking so this is
112 status = read(fd, buf, sizeof(buf));
113 } while (status == MEVENT_MAX);
122 * If calling from outside the i/o thread, write a byte on the
123 * pipe to force the i/o thread to exit the blocking kevent call.
125 if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
126 write(mevent_pipefd[1], &c, 1);
133 #ifndef WITHOUT_CAPSICUM
140 #ifndef WITHOUT_CAPSICUM
141 cap_rights_init(&rights, CAP_KQUEUE);
142 if (caph_rights_limit(mfd, &rights) == -1)
143 errx(EX_OSERR, "Unable to apply rights for sandbox");
146 LIST_INIT(&change_head);
147 LIST_INIT(&global_head);
151 mevent_kq_filter(struct mevent *mevp)
157 if (mevp->me_type == EVF_READ)
158 retval = EVFILT_READ;
160 if (mevp->me_type == EVF_WRITE)
161 retval = EVFILT_WRITE;
163 if (mevp->me_type == EVF_TIMER)
164 retval = EVFILT_TIMER;
166 if (mevp->me_type == EVF_SIGNAL)
167 retval = EVFILT_SIGNAL;
169 if (mevp->me_type == EVF_VNODE)
170 retval = EVFILT_VNODE;
176 mevent_kq_flags(struct mevent *mevp)
178 return (mevp->me_state);
182 mevent_kq_fflags(struct mevent *mevp)
188 switch (mevp->me_type) {
190 if ((mevp->me_fflags & EVFF_ATTRIB) != 0)
191 retval |= NOTE_ATTRIB;
199 mevent_populate(struct mevent *mevp, struct kevent *kev)
201 if (mevp->me_type == EVF_TIMER) {
202 kev->ident = mevp->me_timid;
203 kev->data = mevp->me_msecs;
205 kev->ident = mevp->me_fd;
208 kev->filter = mevent_kq_filter(mevp);
209 kev->flags = mevent_kq_flags(mevp);
210 kev->fflags = mevent_kq_fflags(mevp);
215 mevent_build(struct kevent *kev)
217 struct mevent *mevp, *tmpp;
224 LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
225 if (mevp->me_closefd) {
227 * A close of the file descriptor will remove the
232 assert((mevp->me_state & EV_ADD) == 0);
233 mevent_populate(mevp, &kev[i]);
238 LIST_REMOVE(mevp, me_list);
240 if (mevp->me_state & EV_DELETE) {
243 LIST_INSERT_HEAD(&global_head, mevp, me_list);
246 assert(i < MEVENT_MAX);
255 mevent_handle(struct kevent *kev, int numev)
260 for (i = 0; i < numev; i++) {
263 /* XXX check for EV_ERROR ? */
265 (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
269 static struct mevent *
270 mevent_add_state(int tfd, enum ev_type type,
271 void (*func)(int, enum ev_type, void *), void *param,
272 int state, int fflags)
275 struct mevent *lp, *mevp;
278 if (tfd < 0 || func == NULL) {
284 pthread_once(&mevent_once, mevent_init);
289 * Verify that the fd/type tuple is not present in any list
291 LIST_FOREACH(lp, &global_head, me_list) {
292 if (type != EVF_TIMER && lp->me_fd == tfd &&
293 lp->me_type == type) {
298 LIST_FOREACH(lp, &change_head, me_list) {
299 if (type != EVF_TIMER && lp->me_fd == tfd &&
300 lp->me_type == type) {
306 * Allocate an entry and populate it.
308 mevp = calloc(1, sizeof(struct mevent));
313 if (type == EVF_TIMER) {
314 mevp->me_msecs = tfd;
315 mevp->me_timid = mevent_timid++;
318 mevp->me_type = type;
319 mevp->me_func = func;
320 mevp->me_param = param;
321 mevp->me_state = state;
322 mevp->me_fflags = fflags;
325 * Try to add the event. If this fails, report the failure to
328 mevent_populate(mevp, &kev);
329 ret = kevent(mfd, &kev, 1, NULL, 0, NULL);
336 mevp->me_state &= ~EV_ADD;
337 LIST_INSERT_HEAD(&global_head, mevp, me_list);
346 mevent_add(int tfd, enum ev_type type,
347 void (*func)(int, enum ev_type, void *), void *param)
350 return (mevent_add_state(tfd, type, func, param, EV_ADD, 0));
354 mevent_add_flags(int tfd, enum ev_type type, int fflags,
355 void (*func)(int, enum ev_type, void *), void *param)
358 return (mevent_add_state(tfd, type, func, param, EV_ADD, fflags));
362 mevent_add_disabled(int tfd, enum ev_type type,
363 void (*func)(int, enum ev_type, void *), void *param)
366 return (mevent_add_state(tfd, type, func, param, EV_ADD | EV_DISABLE, 0));
370 mevent_update(struct mevent *evp, bool enable)
377 * It's not possible to enable/disable a deleted event
379 assert((evp->me_state & EV_DELETE) == 0);
381 newstate = evp->me_state;
383 newstate |= EV_ENABLE;
384 newstate &= ~EV_DISABLE;
386 newstate |= EV_DISABLE;
387 newstate &= ~EV_ENABLE;
391 * No update needed if state isn't changing
393 if (evp->me_state != newstate) {
394 evp->me_state = newstate;
397 * Place the entry onto the changed list if not
400 if (evp->me_cq == 0) {
402 LIST_REMOVE(evp, me_list);
403 LIST_INSERT_HEAD(&change_head, evp, me_list);
414 mevent_enable(struct mevent *evp)
417 return (mevent_update(evp, true));
421 mevent_disable(struct mevent *evp)
424 return (mevent_update(evp, false));
428 mevent_delete_event(struct mevent *evp, int closefd)
433 * Place the entry onto the changed list if not already there, and
434 * mark as to be deleted.
436 if (evp->me_cq == 0) {
438 LIST_REMOVE(evp, me_list);
439 LIST_INSERT_HEAD(&change_head, evp, me_list);
442 evp->me_state = EV_DELETE;
453 mevent_delete(struct mevent *evp)
456 return (mevent_delete_event(evp, 0));
460 mevent_delete_close(struct mevent *evp)
463 return (mevent_delete_event(evp, 1));
467 mevent_set_name(void)
470 pthread_set_name_np(mevent_tid, "mevent");
474 mevent_dispatch(void)
476 struct kevent changelist[MEVENT_MAX];
477 struct kevent eventlist[MEVENT_MAX];
478 struct mevent *pipev;
481 #ifndef WITHOUT_CAPSICUM
485 mevent_tid = pthread_self();
488 pthread_once(&mevent_once, mevent_init);
491 * Open the pipe that will be used for other threads to force
492 * the blocking kqueue call to exit by writing to it. Set the
493 * descriptor to non-blocking.
495 ret = pipe(mevent_pipefd);
501 #ifndef WITHOUT_CAPSICUM
502 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
503 if (caph_rights_limit(mevent_pipefd[0], &rights) == -1)
504 errx(EX_OSERR, "Unable to apply rights for sandbox");
505 if (caph_rights_limit(mevent_pipefd[1], &rights) == -1)
506 errx(EX_OSERR, "Unable to apply rights for sandbox");
510 * Add internal event handler for the pipe write fd
512 pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
513 assert(pipev != NULL);
517 * Build changelist if required.
518 * XXX the changelist can be put into the blocking call
519 * to eliminate the extra syscall. Currently better for
522 numev = mevent_build(changelist);
524 ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
526 perror("Error return from kevent change");
531 * Block awaiting events
533 ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
534 if (ret == -1 && errno != EINTR) {
535 perror("Error return from kevent monitor");
539 * Handle reported events
541 mevent_handle(eventlist, ret);