Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyve/mevent.c
Show All 29 Lines | |||||
* Micro event library for FreeBSD, designed for a single i/o thread | * Micro event library for FreeBSD, designed for a single i/o thread | ||||
* using kqueue, and having events be persistent by default. | * using kqueue, and having events be persistent by default. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <assert.h> | #include <assert.h> | ||||
#include <err.h> | |||||
#include <errno.h> | #include <errno.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#ifndef WITHOUT_CAPSICUM | |||||
#include <sys/capsicum.h> | |||||
#endif | |||||
#include <sys/event.h> | #include <sys/event.h> | ||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <pthread_np.h> | #include <pthread_np.h> | ||||
#include "mevent.h" | #include "mevent.h" | ||||
▲ Show 20 Lines • Show All 343 Lines • ▼ Show 20 Lines | |||||
mevent_dispatch(void) | mevent_dispatch(void) | ||||
{ | { | ||||
struct kevent changelist[MEVENT_MAX]; | struct kevent changelist[MEVENT_MAX]; | ||||
struct kevent eventlist[MEVENT_MAX]; | struct kevent eventlist[MEVENT_MAX]; | ||||
struct mevent *pipev; | struct mevent *pipev; | ||||
int mfd; | int mfd; | ||||
int numev; | int numev; | ||||
int ret; | int ret; | ||||
#ifndef WITHOUT_CAPSICUM | |||||
cap_rights_t rights; | |||||
#endif | |||||
mevent_tid = pthread_self(); | mevent_tid = pthread_self(); | ||||
mevent_set_name(); | mevent_set_name(); | ||||
mfd = kqueue(); | mfd = kqueue(); | ||||
assert(mfd > 0); | assert(mfd > 0); | ||||
#ifndef WITHOUT_CAPSICUM | |||||
cap_rights_init(&rights, CAP_KQUEUE); | |||||
if (cap_rights_limit(mfd, &rights) == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
#endif | |||||
/* | /* | ||||
* Open the pipe that will be used for other threads to force | * Open the pipe that will be used for other threads to force | ||||
* the blocking kqueue call to exit by writing to it. Set the | * the blocking kqueue call to exit by writing to it. Set the | ||||
* descriptor to non-blocking. | * descriptor to non-blocking. | ||||
*/ | */ | ||||
ret = pipe(mevent_pipefd); | ret = pipe(mevent_pipefd); | ||||
if (ret < 0) { | if (ret < 0) { | ||||
perror("pipe"); | perror("pipe"); | ||||
exit(0); | exit(0); | ||||
} | } | ||||
#ifndef WITHOUT_CAPSICUM | |||||
cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE); | |||||
if (cap_rights_limit(mevent_pipefd[0], &rights) == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
if (cap_rights_limit(mevent_pipefd[1], &rights) == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
#endif | |||||
/* | /* | ||||
* Add internal event handler for the pipe write fd | * Add internal event handler for the pipe write fd | ||||
*/ | */ | ||||
pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL); | pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL); | ||||
assert(pipev != NULL); | assert(pipev != NULL); | ||||
for (;;) { | for (;;) { | ||||
Show All 28 Lines |