From 7236f2c2204245cee636e681cfccc666472cc6ed Mon Sep 17 00:00:00 2001 From: Dmitry Chagin Date: Sun, 24 May 2015 16:36:29 +0000 Subject: [PATCH] For future use in the Linuxulator: 1. Add a kern_kqueue() counterpart for kqueue() with flags parameter. 2. Be a bit secure. To avoid a double fp lookup add a kern_kevent_fp() counterpart for kern_kevent() with file pointer parameter instead of file descriptor an pass the buck to it. Suggested by: mjg [2] Differential Revision: https://reviews.freebsd.org/D1091 Reviewed by: trasz --- sys/kern/kern_event.c | 35 ++++++++++++++++++++++++++--------- sys/sys/syscallsubr.h | 4 ++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index b6d8a7973022..9000addbc29b 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -736,6 +736,13 @@ filt_usertouch(struct knote *kn, struct kevent *kev, u_long type) int sys_kqueue(struct thread *td, struct kqueue_args *uap) +{ + + return (kern_kqueue(td, 0)); +} + +int +kern_kqueue(struct thread *td, int flags) { struct filedesc *fdp; struct kqueue *kq; @@ -757,7 +764,7 @@ sys_kqueue(struct thread *td, struct kqueue_args *uap) PROC_UNLOCK(p); fdp = p->p_fd; - error = falloc(td, &fp, &fd, 0); + error = falloc(td, &fp, &fd, flags); if (error) goto done2; @@ -888,12 +895,9 @@ int kern_kevent(struct thread *td, int fd, int nchanges, int nevents, struct kevent_copyops *k_ops, const struct timespec *timeout) { - struct kevent keva[KQ_NEVENTS]; - struct kevent *kevp, *changes; - struct kqueue *kq; - struct file *fp; cap_rights_t rights; - int i, n, nerrors, error; + struct file *fp; + int error; cap_rights_init(&rights); if (nchanges > 0) @@ -904,9 +908,24 @@ kern_kevent(struct thread *td, int fd, int nchanges, int nevents, if (error != 0) return (error); + error = kern_kevent_fp(td, fp, nchanges, nevents, k_ops, timeout); + fdrop(fp, td); + + return (error); +} + +int +kern_kevent_fp(struct thread *td, struct file *fp, int nchanges, int nevents, + struct kevent_copyops *k_ops, const struct timespec *timeout) +{ + struct kevent keva[KQ_NEVENTS]; + struct kevent *kevp, *changes; + struct kqueue *kq; + int i, n, nerrors, error; + error = kqueue_acquire(fp, &kq); if (error != 0) - goto done_norel; + return (error); nerrors = 0; @@ -946,8 +965,6 @@ kern_kevent(struct thread *td, int fd, int nchanges, int nevents, error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td); done: kqueue_release(kq, 0); -done_norel: - fdrop(fp, td); return (error); } diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index a6dca3139308..4ff111ee8a3e 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -121,6 +121,10 @@ int kern_jail_get(struct thread *td, struct uio *options, int flags); int kern_jail_set(struct thread *td, struct uio *options, int flags); int kern_kevent(struct thread *td, int fd, int nchanges, int nevents, struct kevent_copyops *k_ops, const struct timespec *timeout); +int kern_kevent_fp(struct thread *td, struct file *fp, int nchanges, + int nevents, struct kevent_copyops *k_ops, + const struct timespec *timeout); +int kern_kqueue(struct thread *td, int flags); int kern_kldload(struct thread *td, const char *file, int *fileid); int kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat); int kern_kldunload(struct thread *td, int fileid, int flags);