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>
51 #include <sys/types.h>
52 #ifndef WITHOUT_CAPSICUM
53 #include <sys/capsicum.h>
55 #include <sys/event.h>
59 #include <pthread_np.h>
68 #define MEV_DEL_PENDING 4
69 #define MEV_ADD_DISABLED 5
73 static pthread_t mevent_tid;
74 static int mevent_timid = 43;
75 static int mevent_pipefd[2];
76 static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
79 void (*me_func)(int, enum ev_type, void *);
80 #define me_msecs me_fd
88 LIST_ENTRY(mevent) me_list;
91 static LIST_HEAD(listhead, mevent) global_head, change_head;
96 pthread_mutex_lock(&mevent_lmutex);
102 pthread_mutex_unlock(&mevent_lmutex);
106 mevent_pipe_read(int fd, enum ev_type type, void *param)
108 char buf[MEVENT_MAX];
112 * Drain the pipe read side. The fd is non-blocking so this is
116 status = read(fd, buf, sizeof(buf));
117 } while (status == MEVENT_MAX);
126 * If calling from outside the i/o thread, write a byte on the
127 * pipe to force the i/o thread to exit the blocking kevent call.
129 if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
130 write(mevent_pipefd[1], &c, 1);
135 mevent_kq_filter(struct mevent *mevp)
141 if (mevp->me_type == EVF_READ)
142 retval = EVFILT_READ;
144 if (mevp->me_type == EVF_WRITE)
145 retval = EVFILT_WRITE;
147 if (mevp->me_type == EVF_TIMER)
148 retval = EVFILT_TIMER;
150 if (mevp->me_type == EVF_SIGNAL)
151 retval = EVFILT_SIGNAL;
157 mevent_kq_flags(struct mevent *mevp)
161 switch (mevp->me_state) {
163 ret = EV_ADD; /* implicitly enabled */
165 case MEV_ADD_DISABLED:
166 ret = EV_ADD | EV_DISABLE;
174 case MEV_DEL_PENDING:
186 mevent_kq_fflags(struct mevent *mevp)
188 /* XXX nothing yet, perhaps EV_EOF for reads ? */
193 mevent_build(int mfd, struct kevent *kev)
195 struct mevent *mevp, *tmpp;
202 LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
203 if (mevp->me_closefd) {
205 * A close of the file descriptor will remove the
210 if (mevp->me_type == EVF_TIMER) {
211 kev[i].ident = mevp->me_timid;
212 kev[i].data = mevp->me_msecs;
214 kev[i].ident = mevp->me_fd;
217 kev[i].filter = mevent_kq_filter(mevp);
218 kev[i].flags = mevent_kq_flags(mevp);
219 kev[i].fflags = mevent_kq_fflags(mevp);
225 LIST_REMOVE(mevp, me_list);
227 if (mevp->me_state == MEV_DEL_PENDING) {
230 LIST_INSERT_HEAD(&global_head, mevp, me_list);
233 assert(i < MEVENT_MAX);
242 mevent_handle(struct kevent *kev, int numev)
247 for (i = 0; i < numev; i++) {
250 /* XXX check for EV_ERROR ? */
252 (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
256 static struct mevent *
257 mevent_add_state(int tfd, enum ev_type type,
258 void (*func)(int, enum ev_type, void *), void *param,
261 struct mevent *lp, *mevp;
263 if (tfd < 0 || func == NULL) {
272 * Verify that the fd/type tuple is not present in any list
274 LIST_FOREACH(lp, &global_head, me_list) {
275 if (type != EVF_TIMER && lp->me_fd == tfd &&
276 lp->me_type == type) {
281 LIST_FOREACH(lp, &change_head, me_list) {
282 if (type != EVF_TIMER && lp->me_fd == tfd &&
283 lp->me_type == type) {
289 * Allocate an entry, populate it, and add it to the change list.
291 mevp = calloc(1, sizeof(struct mevent));
296 if (type == EVF_TIMER) {
297 mevp->me_msecs = tfd;
298 mevp->me_timid = mevent_timid++;
301 mevp->me_type = type;
302 mevp->me_func = func;
303 mevp->me_param = param;
305 LIST_INSERT_HEAD(&change_head, mevp, me_list);
307 mevp->me_state = state;
317 mevent_add(int tfd, enum ev_type type,
318 void (*func)(int, enum ev_type, void *), void *param)
321 return mevent_add_state(tfd, type, func, param, MEV_ADD);
325 mevent_add_disabled(int tfd, enum ev_type type,
326 void (*func)(int, enum ev_type, void *), void *param)
329 return mevent_add_state(tfd, type, func, param, MEV_ADD_DISABLED);
333 mevent_update(struct mevent *evp, int newstate)
336 * It's not possible to enable/disable a deleted event
338 if (evp->me_state == MEV_DEL_PENDING)
342 * No update needed if state isn't changing
344 if (evp->me_state == newstate)
349 evp->me_state = newstate;
352 * Place the entry onto the changed list if not already there.
354 if (evp->me_cq == 0) {
356 LIST_REMOVE(evp, me_list);
357 LIST_INSERT_HEAD(&change_head, evp, me_list);
367 mevent_enable(struct mevent *evp)
370 return (mevent_update(evp, MEV_ENABLE));
374 mevent_disable(struct mevent *evp)
377 return (mevent_update(evp, MEV_DISABLE));
381 mevent_delete_event(struct mevent *evp, int closefd)
386 * Place the entry onto the changed list if not already there, and
387 * mark as to be deleted.
389 if (evp->me_cq == 0) {
391 LIST_REMOVE(evp, me_list);
392 LIST_INSERT_HEAD(&change_head, evp, me_list);
395 evp->me_state = MEV_DEL_PENDING;
406 mevent_delete(struct mevent *evp)
409 return (mevent_delete_event(evp, 0));
413 mevent_delete_close(struct mevent *evp)
416 return (mevent_delete_event(evp, 1));
420 mevent_set_name(void)
423 pthread_set_name_np(mevent_tid, "mevent");
427 mevent_dispatch(void)
429 struct kevent changelist[MEVENT_MAX];
430 struct kevent eventlist[MEVENT_MAX];
431 struct mevent *pipev;
435 #ifndef WITHOUT_CAPSICUM
439 mevent_tid = pthread_self();
445 #ifndef WITHOUT_CAPSICUM
446 cap_rights_init(&rights, CAP_KQUEUE);
447 if (caph_rights_limit(mfd, &rights) == -1)
448 errx(EX_OSERR, "Unable to apply rights for sandbox");
452 * Open the pipe that will be used for other threads to force
453 * the blocking kqueue call to exit by writing to it. Set the
454 * descriptor to non-blocking.
456 ret = pipe(mevent_pipefd);
462 #ifndef WITHOUT_CAPSICUM
463 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
464 if (caph_rights_limit(mevent_pipefd[0], &rights) == -1)
465 errx(EX_OSERR, "Unable to apply rights for sandbox");
466 if (caph_rights_limit(mevent_pipefd[1], &rights) == -1)
467 errx(EX_OSERR, "Unable to apply rights for sandbox");
471 * Add internal event handler for the pipe write fd
473 pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
474 assert(pipev != NULL);
478 * Build changelist if required.
479 * XXX the changelist can be put into the blocking call
480 * to eliminate the extra syscall. Currently better for
483 numev = mevent_build(mfd, changelist);
485 ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
487 perror("Error return from kevent change");
492 * Block awaiting events
494 ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
495 if (ret == -1 && errno != EINTR) {
496 perror("Error return from kevent monitor");
500 * Handle reported events
502 mevent_handle(eventlist, ret);