2012-11-24 19:14:32 +04:00
|
|
|
/* $NetBSD: kern_event.c,v 1.79 2012/11/24 15:14:32 christos Exp $ */
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
/*-
|
2009-04-04 14:12:51 +04:00
|
|
|
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
|
2008-03-22 00:53:35 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-04-04 14:12:51 +04:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2006-06-08 02:33:33 +04:00
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2008-03-22 00:53:35 +03:00
|
|
|
* FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
|
2003-06-23 17:14:49 +04:00
|
|
|
#include <sys/cdefs.h>
|
2012-11-24 19:14:32 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.79 2012/11/24 15:14:32 christos Exp $");
|
2003-06-23 17:14:49 +04:00
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/eventvar.h>
|
|
|
|
#include <sys/poll.h>
|
2008-03-22 00:53:35 +03:00
|
|
|
#include <sys/kmem.h>
|
2002-10-23 13:10:23 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/syscallargs.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2007-07-21 23:23:03 +04:00
|
|
|
#include <sys/conf.h>
|
2008-03-22 00:53:35 +03:00
|
|
|
#include <sys/atomic.h>
|
|
|
|
|
|
|
|
static int kqueue_scan(file_t *, size_t, struct kevent *,
|
|
|
|
const struct timespec *, register_t *,
|
|
|
|
const struct kevent_ops *, struct kevent *,
|
|
|
|
size_t);
|
|
|
|
static int kqueue_ioctl(file_t *, u_long, void *);
|
|
|
|
static int kqueue_fcntl(file_t *, u_int, void *);
|
|
|
|
static int kqueue_poll(file_t *, int);
|
|
|
|
static int kqueue_kqfilter(file_t *, struct knote *);
|
|
|
|
static int kqueue_stat(file_t *, struct stat *);
|
|
|
|
static int kqueue_close(file_t *);
|
|
|
|
static int kqueue_register(struct kqueue *, struct kevent *);
|
|
|
|
static void kqueue_doclose(struct kqueue *, struct klist *, int);
|
|
|
|
|
|
|
|
static void knote_detach(struct knote *, filedesc_t *fdp, bool);
|
|
|
|
static void knote_enqueue(struct knote *);
|
|
|
|
static void knote_activate(struct knote *);
|
|
|
|
|
|
|
|
static void filt_kqdetach(struct knote *);
|
|
|
|
static int filt_kqueue(struct knote *, long hint);
|
|
|
|
static int filt_procattach(struct knote *);
|
|
|
|
static void filt_procdetach(struct knote *);
|
|
|
|
static int filt_proc(struct knote *, long hint);
|
|
|
|
static int filt_fileattach(struct knote *);
|
|
|
|
static void filt_timerexpire(void *x);
|
|
|
|
static int filt_timerattach(struct knote *);
|
|
|
|
static void filt_timerdetach(struct knote *);
|
|
|
|
static int filt_timer(struct knote *, long hint);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2004-11-30 07:25:43 +03:00
|
|
|
static const struct fileops kqueueops = {
|
2009-04-04 14:12:51 +04:00
|
|
|
.fo_read = (void *)enxio,
|
|
|
|
.fo_write = (void *)enxio,
|
|
|
|
.fo_ioctl = kqueue_ioctl,
|
|
|
|
.fo_fcntl = kqueue_fcntl,
|
|
|
|
.fo_poll = kqueue_poll,
|
|
|
|
.fo_stat = kqueue_stat,
|
|
|
|
.fo_close = kqueue_close,
|
|
|
|
.fo_kqfilter = kqueue_kqfilter,
|
2009-12-20 12:36:05 +03:00
|
|
|
.fo_restart = fnullop_restart,
|
2002-10-23 13:10:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct filterops kqread_filtops =
|
|
|
|
{ 1, NULL, filt_kqdetach, filt_kqueue };
|
|
|
|
static const struct filterops proc_filtops =
|
|
|
|
{ 0, filt_procattach, filt_procdetach, filt_proc };
|
|
|
|
static const struct filterops file_filtops =
|
|
|
|
{ 1, filt_fileattach, NULL, NULL };
|
2006-04-21 18:00:18 +04:00
|
|
|
static const struct filterops timer_filtops =
|
2003-02-04 12:02:04 +03:00
|
|
|
{ 0, filt_timerattach, filt_timerdetach, filt_timer };
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
static u_int kq_ncallouts = 0;
|
2003-02-04 12:02:04 +03:00
|
|
|
static int kq_calloutmax = (4 * 1024);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
#define KN_HASHSIZE 64 /* XXX should be tunable */
|
|
|
|
#define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
|
|
|
|
|
|
|
|
extern const struct filterops sig_filtops;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table for for all system-defined filters.
|
|
|
|
* These should be listed in the numeric order of the EVFILT_* defines.
|
|
|
|
* If filtops is NULL, the filter isn't implemented in NetBSD.
|
|
|
|
* End of list is when name is NULL.
|
2008-03-22 00:53:35 +03:00
|
|
|
*
|
|
|
|
* Note that 'refcnt' is meaningless for built-in filters.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
struct kfilter {
|
2008-03-22 00:53:35 +03:00
|
|
|
const char *name; /* name of filter */
|
|
|
|
uint32_t filter; /* id of filter */
|
|
|
|
unsigned refcnt; /* reference count */
|
2002-10-23 13:10:23 +04:00
|
|
|
const struct filterops *filtops;/* operations for filter */
|
2008-03-22 00:53:35 +03:00
|
|
|
size_t namelen; /* length of name string */
|
2002-10-23 13:10:23 +04:00
|
|
|
};
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
/* System defined filters */
|
|
|
|
static struct kfilter sys_kfilters[] = {
|
|
|
|
{ "EVFILT_READ", EVFILT_READ, 0, &file_filtops, 0 },
|
|
|
|
{ "EVFILT_WRITE", EVFILT_WRITE, 0, &file_filtops, 0, },
|
|
|
|
{ "EVFILT_AIO", EVFILT_AIO, 0, NULL, 0 },
|
|
|
|
{ "EVFILT_VNODE", EVFILT_VNODE, 0, &file_filtops, 0 },
|
|
|
|
{ "EVFILT_PROC", EVFILT_PROC, 0, &proc_filtops, 0 },
|
|
|
|
{ "EVFILT_SIGNAL", EVFILT_SIGNAL, 0, &sig_filtops, 0 },
|
|
|
|
{ "EVFILT_TIMER", EVFILT_TIMER, 0, &timer_filtops, 0 },
|
|
|
|
{ NULL, 0, 0, NULL, 0 },
|
2002-10-23 13:10:23 +04:00
|
|
|
};
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
/* User defined kfilters */
|
2002-10-23 13:10:23 +04:00
|
|
|
static struct kfilter *user_kfilters; /* array */
|
|
|
|
static int user_kfilterc; /* current offset */
|
|
|
|
static int user_kfiltermaxc; /* max size so far */
|
2008-03-22 00:53:35 +03:00
|
|
|
static size_t user_kfiltersz; /* size of allocated memory */
|
|
|
|
|
|
|
|
/* Locks */
|
|
|
|
static krwlock_t kqueue_filter_lock; /* lock on filter lists */
|
|
|
|
static kmutex_t kqueue_misc_lock; /* miscellaneous */
|
|
|
|
|
2009-10-03 04:14:07 +04:00
|
|
|
static kauth_listener_t kqueue_listener;
|
|
|
|
|
|
|
|
static int
|
|
|
|
kqueue_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
|
|
|
|
void *arg0, void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_DEFER;
|
|
|
|
p = arg0;
|
|
|
|
|
|
|
|
if (action != KAUTH_PROCESS_KEVENT_FILTER)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if ((kauth_cred_getuid(p->p_cred) != kauth_cred_getuid(cred) ||
|
|
|
|
ISSET(p->p_flag, PK_SUGID)))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
/*
|
|
|
|
* Initialize the kqueue subsystem.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kqueue_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
rw_init(&kqueue_filter_lock);
|
|
|
|
mutex_init(&kqueue_misc_lock, MUTEX_DEFAULT, IPL_NONE);
|
2009-10-03 04:14:07 +04:00
|
|
|
|
|
|
|
kqueue_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
|
|
|
|
kqueue_listener_cb, NULL);
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find kfilter entry by name, or NULL if not found.
|
|
|
|
*/
|
2008-03-22 00:53:35 +03:00
|
|
|
static struct kfilter *
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter_byname_sys(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
KASSERT(rw_lock_held(&kqueue_filter_lock));
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
for (i = 0; sys_kfilters[i].name != NULL; i++) {
|
|
|
|
if (strcmp(name, sys_kfilters[i].name) == 0)
|
2008-03-22 00:53:35 +03:00
|
|
|
return &sys_kfilters[i];
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
return NULL;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct kfilter *
|
|
|
|
kfilter_byname_user(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
KASSERT(rw_lock_held(&kqueue_filter_lock));
|
|
|
|
|
2006-09-30 06:39:18 +04:00
|
|
|
/* user filter slots have a NULL name if previously deregistered */
|
|
|
|
for (i = 0; i < user_kfilterc ; i++) {
|
|
|
|
if (user_kfilters[i].name != NULL &&
|
2002-10-23 13:10:23 +04:00
|
|
|
strcmp(name, user_kfilters[i].name) == 0)
|
2008-03-22 00:53:35 +03:00
|
|
|
return &user_kfilters[i];
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
return NULL;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
static struct kfilter *
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter_byname(const char *name)
|
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kfilter *kfilter;
|
|
|
|
|
|
|
|
KASSERT(rw_lock_held(&kqueue_filter_lock));
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if ((kfilter = kfilter_byname_sys(name)) != NULL)
|
2008-03-22 00:53:35 +03:00
|
|
|
return kfilter;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return kfilter_byname_user(name);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find kfilter entry by filter id, or NULL if not found.
|
|
|
|
* Assumes entries are indexed in filter id order, for speed.
|
|
|
|
*/
|
2008-03-22 00:53:35 +03:00
|
|
|
static struct kfilter *
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter_byfilter(uint32_t filter)
|
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kfilter *kfilter;
|
|
|
|
|
|
|
|
KASSERT(rw_lock_held(&kqueue_filter_lock));
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if (filter < EVFILT_SYSCOUNT) /* it's a system filter */
|
|
|
|
kfilter = &sys_kfilters[filter];
|
|
|
|
else if (user_kfilters != NULL &&
|
|
|
|
filter < EVFILT_SYSCOUNT + user_kfilterc)
|
|
|
|
/* it's a user filter */
|
|
|
|
kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
|
|
|
|
else
|
|
|
|
return (NULL); /* out of range */
|
|
|
|
KASSERT(kfilter->filter == filter); /* sanity check! */
|
|
|
|
return (kfilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a new kfilter. Stores the entry in user_kfilters.
|
|
|
|
* Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
|
|
|
|
* If retfilter != NULL, the new filterid is returned in it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
kfilter_register(const char *name, const struct filterops *filtops,
|
2008-03-22 00:53:35 +03:00
|
|
|
int *retfilter)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kfilter *kfilter;
|
2008-03-22 00:53:35 +03:00
|
|
|
size_t len;
|
2006-09-30 06:39:18 +04:00
|
|
|
int i;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if (name == NULL || name[0] == '\0' || filtops == NULL)
|
|
|
|
return (EINVAL); /* invalid args */
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
rw_enter(&kqueue_filter_lock, RW_WRITER);
|
|
|
|
if (kfilter_byname(name) != NULL) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (EEXIST); /* already exists */
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
|
|
|
if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (EINVAL); /* too many */
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2006-09-30 06:39:18 +04:00
|
|
|
for (i = 0; i < user_kfilterc; i++) {
|
|
|
|
kfilter = &user_kfilters[i];
|
|
|
|
if (kfilter->name == NULL) {
|
|
|
|
/* Previously deregistered slot. Reuse. */
|
|
|
|
goto reuse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/* check if need to grow user_kfilters */
|
|
|
|
if (user_kfilterc + 1 > user_kfiltermaxc) {
|
2008-03-22 00:53:35 +03:00
|
|
|
/* Grow in KFILTER_EXTENT chunks. */
|
2002-10-23 13:10:23 +04:00
|
|
|
user_kfiltermaxc += KFILTER_EXTENT;
|
2009-12-22 23:50:46 +03:00
|
|
|
len = user_kfiltermaxc * sizeof(*kfilter);
|
2008-03-22 00:53:35 +03:00
|
|
|
kfilter = kmem_alloc(len, KM_SLEEP);
|
|
|
|
memset((char *)kfilter + user_kfiltersz, 0, len - user_kfiltersz);
|
|
|
|
if (user_kfilters != NULL) {
|
|
|
|
memcpy(kfilter, user_kfilters, user_kfiltersz);
|
|
|
|
kmem_free(user_kfilters, user_kfiltersz);
|
|
|
|
}
|
|
|
|
user_kfiltersz = len;
|
2002-10-23 13:10:23 +04:00
|
|
|
user_kfilters = kfilter;
|
|
|
|
}
|
2006-09-30 06:39:18 +04:00
|
|
|
/* Adding new slot */
|
|
|
|
kfilter = &user_kfilters[user_kfilterc++];
|
|
|
|
reuse:
|
2008-03-22 00:53:35 +03:00
|
|
|
kfilter->namelen = strlen(name) + 1;
|
|
|
|
kfilter->name = kmem_alloc(kfilter->namelen, KM_SLEEP);
|
|
|
|
memcpy(__UNCONST(kfilter->name), name, kfilter->namelen);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2006-09-30 06:39:18 +04:00
|
|
|
kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kfilter->filtops = kmem_alloc(sizeof(*filtops), KM_SLEEP);
|
|
|
|
memcpy(__UNCONST(kfilter->filtops), filtops, sizeof(*filtops));
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if (retfilter != NULL)
|
2006-09-30 06:39:18 +04:00
|
|
|
*retfilter = kfilter->filter;
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unregister a kfilter previously registered with kfilter_register.
|
|
|
|
* This retains the filter id, but clears the name and frees filtops (filter
|
|
|
|
* operations), so that the number isn't reused during a boot.
|
|
|
|
* Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
kfilter_unregister(const char *name)
|
|
|
|
{
|
|
|
|
struct kfilter *kfilter;
|
|
|
|
|
|
|
|
if (name == NULL || name[0] == '\0')
|
|
|
|
return (EINVAL); /* invalid name */
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_enter(&kqueue_filter_lock, RW_WRITER);
|
|
|
|
if (kfilter_byname_sys(name) != NULL) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (EINVAL); /* can't detach system filters */
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
kfilter = kfilter_byname_user(name);
|
2008-03-22 00:53:35 +03:00
|
|
|
if (kfilter == NULL) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (ENOENT);
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
|
|
|
if (kfilter->refcnt != 0) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
/* Cast away const (but we know it's safe. */
|
|
|
|
kmem_free(__UNCONST(kfilter->name), kfilter->namelen);
|
2006-09-30 06:39:18 +04:00
|
|
|
kfilter->name = NULL; /* mark as `not implemented' */
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kfilter->filtops != NULL) {
|
2008-03-22 00:53:35 +03:00
|
|
|
/* Cast away const (but we know it's safe. */
|
|
|
|
kmem_free(__UNCONST(kfilter->filtops),
|
|
|
|
sizeof(*kfilter->filtops));
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter->filtops = NULL; /* mark as `not implemented' */
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
|
2008-03-22 00:53:35 +03:00
|
|
|
* descriptors. Calls fileops kqfilter method for given file descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
filt_fileattach(struct knote *kn)
|
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
file_t *fp;
|
|
|
|
|
|
|
|
fp = kn->kn_obj;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return (*fp->f_ops->fo_kqfilter)(fp, kn);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter detach method for EVFILT_READ on kqueue descriptor.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
filt_kqdetach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = ((file_t *)kn->kn_obj)->f_data;
|
|
|
|
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
2002-11-26 21:44:34 +03:00
|
|
|
SLIST_REMOVE(&kq->kq_sel.sel_klist, kn, knote, kn_selnext);
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter event method for EVFILT_READ on kqueue descriptor.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
filt_kqueue(struct knote *kn, long hint)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
2008-03-22 00:53:35 +03:00
|
|
|
int rv;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = ((file_t *)kn->kn_obj)->f_data;
|
|
|
|
|
|
|
|
if (hint != NOTE_SUBMIT)
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_data = kq->kq_count;
|
2008-03-22 00:53:35 +03:00
|
|
|
rv = (kn->kn_data > 0);
|
|
|
|
if (hint != NOTE_SUBMIT)
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
|
|
|
|
return rv;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter attach method for EVFILT_PROC.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
filt_procattach(struct knote *kn)
|
|
|
|
{
|
2012-11-18 22:36:01 +04:00
|
|
|
struct proc *p;
|
2006-07-24 02:06:03 +04:00
|
|
|
struct lwp *curl;
|
|
|
|
|
|
|
|
curl = curlwp;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_enter(proc_lock);
|
2012-11-18 01:55:24 +04:00
|
|
|
if (kn->kn_flags & EV_FLAG1) {
|
|
|
|
/*
|
|
|
|
* NOTE_TRACK attaches to the child process too early
|
|
|
|
* for proc_find, so do a raw look up and check the state
|
|
|
|
* explicitly.
|
|
|
|
*/
|
|
|
|
p = proc_find_raw(kn->kn_id);
|
|
|
|
if (p != NULL && p->p_stat != SIDL)
|
|
|
|
p = NULL;
|
|
|
|
} else {
|
|
|
|
p = proc_find(kn->kn_id);
|
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
if (p == NULL) {
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
2008-03-22 00:53:35 +03:00
|
|
|
return ESRCH;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if it's not owned by you, or the last exec gave us
|
|
|
|
* setuid/setgid privs (unless you're root).
|
|
|
|
*/
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2008-04-24 19:35:27 +04:00
|
|
|
mutex_exit(proc_lock);
|
Tons of process scope changes.
- Add a KAUTH_PROCESS_SCHEDULER action, to handle scheduler related
requests, and add specific requests for set/get scheduler policy and
set/get scheduler parameters.
- Add a KAUTH_PROCESS_KEVENT_FILTER action, to handle kevent(2) related
requests.
- Add a KAUTH_DEVICE_TTY_STI action to handle requests to TIOCSTI.
- Add requests for the KAUTH_PROCESS_CANSEE action, indicating what
process information is being looked at (entry itself, args, env,
open files).
- Add requests for the KAUTH_PROCESS_RLIMIT action indicating set/get.
- Add requests for the KAUTH_PROCESS_CORENAME action indicating set/get.
- Make bsd44 secmodel code handle the newly added rqeuests appropriately.
All of the above make it possible to issue finer-grained kauth(9) calls in
many places, removing some KAUTH_GENERIC_ISSUSER requests.
- Remove the "CAN" from KAUTH_PROCESS_CAN{KTRACE,PROCFS,PTRACE,SIGNAL}.
Discussed with christos@ and yamt@.
2008-01-23 18:04:38 +03:00
|
|
|
if (kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_KEVENT_FILTER,
|
2008-03-22 00:53:35 +03:00
|
|
|
p, NULL, NULL, NULL) != 0) {
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2008-03-22 00:53:35 +03:00
|
|
|
return EACCES;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kn->kn_obj = p;
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_flags |= EV_CLEAR; /* automatically set */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* internal flag indicating registration done by kernel
|
|
|
|
*/
|
|
|
|
if (kn->kn_flags & EV_FLAG1) {
|
|
|
|
kn->kn_data = kn->kn_sdata; /* ppid */
|
|
|
|
kn->kn_fflags = NOTE_CHILD;
|
|
|
|
kn->kn_flags &= ~EV_FLAG1;
|
|
|
|
}
|
|
|
|
SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter detach method for EVFILT_PROC.
|
|
|
|
*
|
|
|
|
* The knote may be attached to a different process, which may exit,
|
|
|
|
* leaving nothing for the knote to be attached to. So when the process
|
|
|
|
* exits, the knote is marked as DETACHED and also flagged as ONESHOT so
|
|
|
|
* it will be deleted when read out. However, as part of the knote deletion,
|
|
|
|
* this routine is called, so a check is needed to avoid actually performing
|
|
|
|
* a detach, because the original process might not exist any more.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
filt_procdetach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
if (kn->kn_status & KN_DETACHED)
|
|
|
|
return;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
p = kn->kn_obj;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter event method for EVFILT_PROC.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
filt_proc(struct knote *kn, long hint)
|
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
u_int event, fflag;
|
|
|
|
struct kevent kev;
|
|
|
|
struct kqueue *kq;
|
|
|
|
int error;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
event = (u_int)hint & NOTE_PCTRLMASK;
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = kn->kn_kq;
|
|
|
|
fflag = 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
/* If the user is interested in this event, record it. */
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kn->kn_sfflags & event)
|
2008-03-22 00:53:35 +03:00
|
|
|
fflag |= event;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if (event == NOTE_EXIT) {
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Process is gone, so flag the event as finished.
|
|
|
|
*
|
2002-10-23 13:10:23 +04:00
|
|
|
* Detach the knote from watched process and mark
|
|
|
|
* it as such. We can't leave this to kqueue_scan(),
|
|
|
|
* since the process might not exist by then. And we
|
|
|
|
* have to do this now, since psignal KNOTE() is called
|
|
|
|
* also for zombies and we might end up reading freed
|
|
|
|
* memory if the kevent would already be picked up
|
2005-02-27 00:34:55 +03:00
|
|
|
* and knote g/c'ed.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
2008-03-22 00:53:35 +03:00
|
|
|
filt_procdetach(kn);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
kn->kn_status |= KN_DETACHED;
|
2002-10-23 13:10:23 +04:00
|
|
|
/* Mark as ONESHOT, so that the knote it g/c'ed when read */
|
2005-02-27 00:34:55 +03:00
|
|
|
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
|
2008-03-22 00:53:35 +03:00
|
|
|
kn->kn_fflags |= fflag;
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
|
|
|
|
return 1;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Process forked, and user wants to track the new process,
|
|
|
|
* so attach a new knote to it, and immediately report an
|
|
|
|
* event with the parent's pid. Register knote with new
|
|
|
|
* process.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
kev.ident = hint & NOTE_PDATAMASK; /* pid */
|
|
|
|
kev.filter = kn->kn_filter;
|
|
|
|
kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
|
|
|
|
kev.fflags = kn->kn_sfflags;
|
|
|
|
kev.data = kn->kn_id; /* parent */
|
|
|
|
kev.udata = kn->kn_kevent.udata; /* preserve udata */
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
error = kqueue_register(kq, &kev);
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
if (error != 0)
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_fflags |= NOTE_TRACKERR;
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
kn->kn_fflags |= fflag;
|
|
|
|
fflag = kn->kn_fflags;
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return fflag != 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2003-02-04 12:02:04 +03:00
|
|
|
static void
|
|
|
|
filt_timerexpire(void *knx)
|
|
|
|
{
|
|
|
|
struct knote *kn = knx;
|
|
|
|
int tticks;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_enter(&kqueue_misc_lock);
|
2003-02-04 12:02:04 +03:00
|
|
|
kn->kn_data++;
|
2008-03-22 00:53:35 +03:00
|
|
|
knote_activate(kn);
|
2003-02-04 12:02:04 +03:00
|
|
|
if ((kn->kn_flags & EV_ONESHOT) == 0) {
|
|
|
|
tticks = mstohz(kn->kn_sdata);
|
2011-11-17 05:19:37 +04:00
|
|
|
if (tticks <= 0)
|
|
|
|
tticks = 1;
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_schedule((callout_t *)kn->kn_hook, tticks);
|
2003-02-04 12:02:04 +03:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&kqueue_misc_lock);
|
2003-02-04 12:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* data contains amount of time to sleep, in milliseconds
|
2005-02-27 00:34:55 +03:00
|
|
|
*/
|
2003-02-04 12:02:04 +03:00
|
|
|
static int
|
|
|
|
filt_timerattach(struct knote *kn)
|
|
|
|
{
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_t *calloutp;
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kqueue *kq;
|
2003-02-04 12:02:04 +03:00
|
|
|
int tticks;
|
|
|
|
|
|
|
|
tticks = mstohz(kn->kn_sdata);
|
|
|
|
|
|
|
|
/* if the supplied value is under our resolution, use 1 tick */
|
|
|
|
if (tticks == 0) {
|
|
|
|
if (kn->kn_sdata == 0)
|
2008-03-22 00:53:35 +03:00
|
|
|
return EINVAL;
|
2003-02-04 12:02:04 +03:00
|
|
|
tticks = 1;
|
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
|
|
|
|
(calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
|
|
|
|
atomic_dec_uint(&kq_ncallouts);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2008-04-22 15:44:24 +04:00
|
|
|
callout_init(calloutp, CALLOUT_MPSAFE);
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
kq = kn->kn_kq;
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
kn->kn_flags |= EV_CLEAR; /* automatically set */
|
2003-02-04 12:02:04 +03:00
|
|
|
kn->kn_hook = calloutp;
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
|
|
|
|
callout_reset(calloutp, tticks, filt_timerexpire, kn);
|
2003-02-04 12:02:04 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filt_timerdetach(struct knote *kn)
|
|
|
|
{
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_t *calloutp;
|
2003-02-04 12:02:04 +03:00
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
calloutp = (callout_t *)kn->kn_hook;
|
2008-04-22 16:04:22 +04:00
|
|
|
callout_halt(calloutp, NULL);
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_destroy(calloutp);
|
2008-03-22 00:53:35 +03:00
|
|
|
kmem_free(calloutp, sizeof(*calloutp));
|
|
|
|
atomic_dec_uint(&kq_ncallouts);
|
2003-02-04 12:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-01 13:17:58 +03:00
|
|
|
filt_timer(struct knote *kn, long hint)
|
2003-02-04 12:02:04 +03:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
mutex_enter(&kqueue_misc_lock);
|
|
|
|
rv = (kn->kn_data != 0);
|
|
|
|
mutex_exit(&kqueue_misc_lock);
|
|
|
|
|
|
|
|
return rv;
|
2003-02-04 12:02:04 +03:00
|
|
|
}
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
|
|
|
* filt_seltrue:
|
|
|
|
*
|
|
|
|
* This filter "event" routine simulates seltrue().
|
|
|
|
*/
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
filt_seltrue(struct knote *kn, long hint)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't know how much data can be read/written,
|
|
|
|
* but we know that it *can* be. This is about as
|
|
|
|
* good as select/poll does as well.
|
|
|
|
*/
|
|
|
|
kn->kn_data = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This provides full kqfilter entry for device switch tables, which
|
|
|
|
* has same effect as filter using filt_seltrue() as filter method.
|
|
|
|
*/
|
|
|
|
static void
|
2006-11-01 13:17:58 +03:00
|
|
|
filt_seltruedetach(struct knote *kn)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
2007-12-03 18:21:57 +03:00
|
|
|
const struct filterops seltrue_filtops =
|
2002-10-23 13:10:23 +04:00
|
|
|
{ 1, NULL, filt_seltruedetach, filt_seltrue };
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
seltrue_kqfilter(dev_t dev, struct knote *kn)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
switch (kn->kn_filter) {
|
|
|
|
case EVFILT_READ:
|
|
|
|
case EVFILT_WRITE:
|
|
|
|
kn->kn_fop = &seltrue_filtops;
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-05 20:19:46 +03:00
|
|
|
return (EINVAL);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Nothing more to do */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kqueue(2) system call.
|
|
|
|
*/
|
2011-06-26 20:42:39 +04:00
|
|
|
static int
|
|
|
|
kqueue1(struct lwp *l, int flags, register_t *retval)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kqueue *kq;
|
|
|
|
file_t *fp;
|
|
|
|
int fd, error;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
if ((error = fd_allocfile(&fp, &fd)) != 0)
|
|
|
|
return error;
|
2012-01-25 04:28:35 +04:00
|
|
|
fp->f_flag = FREAD | FWRITE | (flags & (FNONBLOCK|FNOSIGPIPE));
|
2002-10-23 13:10:23 +04:00
|
|
|
fp->f_type = DTYPE_KQUEUE;
|
|
|
|
fp->f_ops = &kqueueops;
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = kmem_zalloc(sizeof(*kq), KM_SLEEP);
|
|
|
|
mutex_init(&kq->kq_lock, MUTEX_DEFAULT, IPL_SCHED);
|
|
|
|
cv_init(&kq->kq_cv, "kqueue");
|
2008-03-01 17:16:49 +03:00
|
|
|
selinit(&kq->kq_sel);
|
2008-03-22 00:53:35 +03:00
|
|
|
TAILQ_INIT(&kq->kq_head);
|
|
|
|
fp->f_data = kq;
|
2002-10-23 13:10:23 +04:00
|
|
|
*retval = fd;
|
2008-03-22 00:53:35 +03:00
|
|
|
kq->kq_fdp = curlwp->l_fd;
|
2011-06-26 20:42:39 +04:00
|
|
|
fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
|
2008-03-22 00:53:35 +03:00
|
|
|
fd_affix(curproc, fp, fd);
|
|
|
|
return error;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2011-06-26 20:42:39 +04:00
|
|
|
/*
|
|
|
|
* kqueue(2) system call.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_kqueue(struct lwp *l, const void *v, register_t *retval)
|
|
|
|
{
|
|
|
|
return kqueue1(l, 0, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_kqueue1(struct lwp *l, const struct sys_kqueue1_args *uap,
|
|
|
|
register_t *retval)
|
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(int) flags;
|
|
|
|
} */
|
|
|
|
return kqueue1(l, SCARG(uap, flags), retval);
|
|
|
|
}
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
|
|
|
* kevent(2) system call.
|
|
|
|
*/
|
2009-01-11 05:45:45 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
kevent_fetch_changes(void *private, const struct kevent *changelist,
|
2009-01-11 05:45:45 +03:00
|
|
|
struct kevent *changes, size_t index, int n)
|
2005-10-23 05:33:32 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
|
2005-10-23 05:33:32 +04:00
|
|
|
return copyin(changelist + index, changes, n * sizeof(*changes));
|
|
|
|
}
|
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
kevent_put_events(void *private, struct kevent *events,
|
2009-01-11 05:45:45 +03:00
|
|
|
struct kevent *eventlist, size_t index, int n)
|
2005-10-23 05:33:32 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
|
2005-10-23 05:33:32 +04:00
|
|
|
return copyout(events, eventlist + index, n * sizeof(*events));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct kevent_ops kevent_native_ops = {
|
2008-06-24 14:27:35 +04:00
|
|
|
.keo_private = NULL,
|
|
|
|
.keo_fetch_timeout = copyin,
|
|
|
|
.keo_fetch_changes = kevent_fetch_changes,
|
|
|
|
.keo_put_events = kevent_put_events,
|
2005-10-23 05:33:32 +04:00
|
|
|
};
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
int
|
2009-01-11 05:45:45 +03:00
|
|
|
sys___kevent50(struct lwp *l, const struct sys___kevent50_args *uap,
|
|
|
|
register_t *retval)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2007-12-21 02:02:38 +03:00
|
|
|
/* {
|
2002-10-23 13:10:23 +04:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(const struct kevent *) changelist;
|
|
|
|
syscallarg(size_t) nchanges;
|
|
|
|
syscallarg(struct kevent *) eventlist;
|
|
|
|
syscallarg(size_t) nevents;
|
|
|
|
syscallarg(const struct timespec *) timeout;
|
2007-12-21 02:02:38 +03:00
|
|
|
} */
|
2005-10-23 05:33:32 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
|
2005-10-23 05:33:32 +04:00
|
|
|
SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
|
|
|
|
SCARG(uap, timeout), &kevent_native_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-03-22 00:53:35 +03:00
|
|
|
kevent1(register_t *retval, int fd,
|
|
|
|
const struct kevent *changelist, size_t nchanges,
|
|
|
|
struct kevent *eventlist, size_t nevents,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
const struct kevent_ops *keops)
|
2005-10-23 05:33:32 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kevent *kevp;
|
|
|
|
struct kqueue *kq;
|
2002-10-23 13:10:23 +04:00
|
|
|
struct timespec ts;
|
2008-03-22 00:53:35 +03:00
|
|
|
size_t i, n, ichange;
|
|
|
|
int nerrors, error;
|
|
|
|
struct kevent kevbuf[8]; /* approx 300 bytes on 64-bit */
|
|
|
|
file_t *fp;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* check that we're dealing with a kq */
|
2008-03-22 00:53:35 +03:00
|
|
|
fp = fd_getfile(fd);
|
2003-02-23 17:37:32 +03:00
|
|
|
if (fp == NULL)
|
2002-10-23 13:10:23 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
2003-02-23 17:37:32 +03:00
|
|
|
if (fp->f_type != DTYPE_KQUEUE) {
|
2008-03-22 00:53:35 +03:00
|
|
|
fd_putfile(fd);
|
2003-02-23 17:37:32 +03:00
|
|
|
return (EBADF);
|
|
|
|
}
|
|
|
|
|
2005-10-23 05:33:32 +04:00
|
|
|
if (timeout != NULL) {
|
|
|
|
error = (*keops->keo_fetch_timeout)(timeout, &ts, sizeof(ts));
|
2002-10-23 13:10:23 +04:00
|
|
|
if (error)
|
|
|
|
goto done;
|
2005-10-23 05:33:32 +04:00
|
|
|
timeout = &ts;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
kq = (struct kqueue *)fp->f_data;
|
|
|
|
nerrors = 0;
|
2005-10-23 05:33:32 +04:00
|
|
|
ichange = 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* traverse list of events to register */
|
2005-10-23 05:33:32 +04:00
|
|
|
while (nchanges > 0) {
|
2008-03-22 00:53:35 +03:00
|
|
|
n = MIN(nchanges, __arraycount(kevbuf));
|
2005-10-23 05:33:32 +04:00
|
|
|
error = (*keops->keo_fetch_changes)(keops->keo_private,
|
2008-03-22 00:53:35 +03:00
|
|
|
changelist, kevbuf, ichange, n);
|
2002-10-23 13:10:23 +04:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
for (i = 0; i < n; i++) {
|
2008-03-22 00:53:35 +03:00
|
|
|
kevp = &kevbuf[i];
|
2002-10-23 13:10:23 +04:00
|
|
|
kevp->flags &= ~EV_SYSFLAGS;
|
|
|
|
/* register each knote */
|
2008-03-22 00:53:35 +03:00
|
|
|
error = kqueue_register(kq, kevp);
|
2002-10-23 13:10:23 +04:00
|
|
|
if (error) {
|
2005-10-23 05:33:32 +04:00
|
|
|
if (nevents != 0) {
|
2002-10-23 13:10:23 +04:00
|
|
|
kevp->flags = EV_ERROR;
|
|
|
|
kevp->data = error;
|
2005-10-23 05:33:32 +04:00
|
|
|
error = (*keops->keo_put_events)
|
|
|
|
(keops->keo_private, kevp,
|
|
|
|
eventlist, nerrors, 1);
|
2002-10-23 13:10:23 +04:00
|
|
|
if (error)
|
|
|
|
goto done;
|
2005-10-23 05:33:32 +04:00
|
|
|
nevents--;
|
2002-10-23 13:10:23 +04:00
|
|
|
nerrors++;
|
|
|
|
} else {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-23 05:33:32 +04:00
|
|
|
nchanges -= n; /* update the results */
|
|
|
|
ichange += n;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
if (nerrors) {
|
|
|
|
*retval = nerrors;
|
|
|
|
error = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* actually scan through the events */
|
2008-03-22 00:53:35 +03:00
|
|
|
error = kqueue_scan(fp, nevents, eventlist, timeout, retval, keops,
|
|
|
|
kevbuf, __arraycount(kevbuf));
|
2002-10-23 13:10:23 +04:00
|
|
|
done:
|
2008-03-22 00:53:35 +03:00
|
|
|
fd_putfile(fd);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a given kevent kev onto the kqueue
|
|
|
|
*/
|
2008-03-22 00:53:35 +03:00
|
|
|
static int
|
|
|
|
kqueue_register(struct kqueue *kq, struct kevent *kev)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kfilter *kfilter;
|
|
|
|
filedesc_t *fdp;
|
|
|
|
file_t *fp;
|
|
|
|
fdfile_t *ff;
|
|
|
|
struct knote *kn, *newkn;
|
|
|
|
struct klist *list;
|
|
|
|
int error, fd, rv;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
fdp = kq->kq_fdp;
|
|
|
|
fp = NULL;
|
|
|
|
kn = NULL;
|
|
|
|
error = 0;
|
2008-03-22 00:53:35 +03:00
|
|
|
fd = 0;
|
|
|
|
|
|
|
|
newkn = kmem_zalloc(sizeof(*newkn), KM_SLEEP);
|
|
|
|
|
|
|
|
rw_enter(&kqueue_filter_lock, RW_READER);
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter = kfilter_byfilter(kev->filter);
|
|
|
|
if (kfilter == NULL || kfilter->filtops == NULL) {
|
|
|
|
/* filter not found nor implemented */
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
kmem_free(newkn, sizeof(*newkn));
|
2002-10-23 13:10:23 +04:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search if knote already exists */
|
|
|
|
if (kfilter->filtops->f_isfd) {
|
|
|
|
/* monitoring a file descriptor */
|
2008-03-22 00:53:35 +03:00
|
|
|
fd = kev->ident;
|
|
|
|
if ((fp = fd_getfile(fd)) == NULL) {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
kmem_free(newkn, sizeof(*newkn));
|
|
|
|
return EBADF;
|
|
|
|
}
|
2011-11-18 02:41:55 +04:00
|
|
|
mutex_enter(&fdp->fd_lock);
|
2009-05-25 01:41:25 +04:00
|
|
|
ff = fdp->fd_dt->dt_ff[fd];
|
2008-03-22 00:53:35 +03:00
|
|
|
if (fd <= fdp->fd_lastkqfile) {
|
|
|
|
SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kq == kn->kn_kq &&
|
|
|
|
kev->filter == kn->kn_filter)
|
|
|
|
break;
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* not monitoring a file descriptor, so
|
|
|
|
* lookup knotes in internal hash table
|
|
|
|
*/
|
2011-11-18 02:41:55 +04:00
|
|
|
mutex_enter(&fdp->fd_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
if (fdp->fd_knhashmask != 0) {
|
|
|
|
list = &fdp->fd_knhash[
|
|
|
|
KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
|
2008-03-22 00:53:35 +03:00
|
|
|
SLIST_FOREACH(kn, list, kn_link) {
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kev->ident == kn->kn_id &&
|
|
|
|
kq == kn->kn_kq &&
|
|
|
|
kev->filter == kn->kn_filter)
|
|
|
|
break;
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kn now contains the matching knote, or NULL if no match
|
|
|
|
*/
|
|
|
|
if (kev->flags & EV_ADD) {
|
|
|
|
if (kn == NULL) {
|
|
|
|
/* create new knote */
|
2008-03-22 00:53:35 +03:00
|
|
|
kn = newkn;
|
|
|
|
newkn = NULL;
|
|
|
|
kn->kn_obj = fp;
|
2012-11-24 19:14:32 +04:00
|
|
|
kn->kn_id = kev->ident;
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_kq = kq;
|
|
|
|
kn->kn_fop = kfilter->filtops;
|
2008-03-22 00:53:35 +03:00
|
|
|
kn->kn_kfilter = kfilter;
|
|
|
|
kn->kn_sfflags = kev->fflags;
|
|
|
|
kn->kn_sdata = kev->data;
|
|
|
|
kev->fflags = 0;
|
|
|
|
kev->data = 0;
|
|
|
|
kn->kn_kevent = *kev;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* apply reference count to knote structure, and
|
|
|
|
* do not release it at the end of this routine.
|
|
|
|
*/
|
|
|
|
fp = NULL;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
if (!kn->kn_fop->f_isfd) {
|
|
|
|
/*
|
|
|
|
* If knote is not on an fd, store on
|
|
|
|
* internal hash table.
|
|
|
|
*/
|
|
|
|
if (fdp->fd_knhashmask == 0) {
|
|
|
|
/* XXXAD can block with fd_lock held */
|
|
|
|
fdp->fd_knhash = hashinit(KN_HASHSIZE,
|
2008-05-05 21:11:16 +04:00
|
|
|
HASH_LIST, true,
|
2008-03-22 00:53:35 +03:00
|
|
|
&fdp->fd_knhashmask);
|
|
|
|
}
|
|
|
|
list = &fdp->fd_knhash[KN_HASH(kn->kn_id,
|
|
|
|
fdp->fd_knhashmask)];
|
|
|
|
} else {
|
|
|
|
/* Otherwise, knote is on an fd. */
|
|
|
|
list = (struct klist *)
|
2009-05-25 01:41:25 +04:00
|
|
|
&fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
|
2008-03-22 00:53:35 +03:00
|
|
|
if ((int)kn->kn_id > fdp->fd_lastkqfile)
|
|
|
|
fdp->fd_lastkqfile = kn->kn_id;
|
|
|
|
}
|
|
|
|
SLIST_INSERT_HEAD(list, kn, kn_link);
|
|
|
|
|
|
|
|
KERNEL_LOCK(1, NULL); /* XXXSMP */
|
|
|
|
error = (*kfilter->filtops->f_attach)(kn);
|
|
|
|
KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
|
|
|
|
if (error != 0) {
|
2012-11-24 19:14:32 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("%s: event not supported for file type"
|
|
|
|
" %d\n", __func__, fp ? fp->f_type : -1);
|
|
|
|
#endif
|
2008-03-22 00:53:35 +03:00
|
|
|
/* knote_detach() drops fdp->fd_lock */
|
|
|
|
knote_detach(kn, fdp, false);
|
2002-10-23 13:10:23 +04:00
|
|
|
goto done;
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
atomic_inc_uint(&kfilter->refcnt);
|
2002-10-23 13:10:23 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The user may change some filter values after the
|
2005-02-27 00:34:55 +03:00
|
|
|
* initial EV_ADD, but doing so will not reset any
|
2002-10-23 13:10:23 +04:00
|
|
|
* filter which have already been triggered.
|
|
|
|
*/
|
|
|
|
kn->kn_sfflags = kev->fflags;
|
|
|
|
kn->kn_sdata = kev->data;
|
|
|
|
kn->kn_kevent.udata = kev->udata;
|
|
|
|
}
|
2012-11-24 19:14:32 +04:00
|
|
|
/*
|
|
|
|
* We can get here if we are trying to attach
|
|
|
|
* an event to a file descriptor that does not
|
|
|
|
* support events, and the attach routine is
|
|
|
|
* broken and does not return an error.
|
|
|
|
*/
|
|
|
|
KASSERT(kn->kn_fop->f_event != NULL);
|
2008-03-22 00:53:35 +03:00
|
|
|
KERNEL_LOCK(1, NULL); /* XXXSMP */
|
|
|
|
rv = (*kn->kn_fop->f_event)(kn, 0);
|
|
|
|
KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
|
|
|
|
if (rv)
|
|
|
|
knote_activate(kn);
|
|
|
|
} else {
|
|
|
|
if (kn == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
mutex_exit(&fdp->fd_lock);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (kev->flags & EV_DELETE) {
|
|
|
|
/* knote_detach() drops fdp->fd_lock */
|
|
|
|
knote_detach(kn, fdp, true);
|
|
|
|
goto done;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* disable knote */
|
2008-03-22 00:53:35 +03:00
|
|
|
if ((kev->flags & EV_DISABLE)) {
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
if ((kn->kn_status & KN_DISABLED) == 0)
|
|
|
|
kn->kn_status |= KN_DISABLED;
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable knote */
|
2008-03-22 00:53:35 +03:00
|
|
|
if ((kev->flags & EV_ENABLE)) {
|
|
|
|
knote_enqueue(kn);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&fdp->fd_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
done:
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
if (newkn != NULL)
|
|
|
|
kmem_free(newkn, sizeof(*newkn));
|
2002-10-23 13:10:23 +04:00
|
|
|
if (fp != NULL)
|
2008-03-22 00:53:35 +03:00
|
|
|
fd_putfile(fd);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2008-03-24 12:09:55 +03:00
|
|
|
#if defined(DEBUG)
|
|
|
|
static void
|
|
|
|
kq_check(struct kqueue *kq)
|
|
|
|
{
|
|
|
|
const struct knote *kn;
|
|
|
|
int count;
|
|
|
|
int nmarker;
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&kq->kq_lock));
|
|
|
|
KASSERT(kq->kq_count >= 0);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
nmarker = 0;
|
|
|
|
TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
|
|
|
|
if ((kn->kn_status & (KN_MARKER | KN_QUEUED)) == 0) {
|
|
|
|
panic("%s: kq=%p kn=%p inconsist 1", __func__, kq, kn);
|
|
|
|
}
|
|
|
|
if ((kn->kn_status & KN_MARKER) == 0) {
|
|
|
|
if (kn->kn_kq != kq) {
|
|
|
|
panic("%s: kq=%p kn=%p inconsist 2",
|
|
|
|
__func__, kq, kn);
|
|
|
|
}
|
|
|
|
if ((kn->kn_status & KN_ACTIVE) == 0) {
|
|
|
|
panic("%s: kq=%p kn=%p: not active",
|
|
|
|
__func__, kq, kn);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
if (count > kq->kq_count) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nmarker++;
|
|
|
|
#if 0
|
|
|
|
if (nmarker > 10000) {
|
|
|
|
panic("%s: kq=%p too many markers: %d != %d, "
|
|
|
|
"nmarker=%d",
|
|
|
|
__func__, kq, kq->kq_count, count, nmarker);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kq->kq_count != count) {
|
|
|
|
bad:
|
|
|
|
panic("%s: kq=%p inconsist 3: %d != %d, nmarker=%d",
|
|
|
|
__func__, kq, kq->kq_count, count, nmarker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* defined(DEBUG) */
|
|
|
|
#define kq_check(a) /* nothing */
|
|
|
|
#endif /* defined(DEBUG) */
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
|
|
|
* Scan through the list of events on fp (for a maximum of maxevents),
|
|
|
|
* returning the results in to ulistp. Timeout is determined by tsp; if
|
|
|
|
* NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
|
|
|
|
* as appropriate.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_scan(file_t *fp, size_t maxevents, struct kevent *ulistp,
|
|
|
|
const struct timespec *tsp, register_t *retval,
|
|
|
|
const struct kevent_ops *keops, struct kevent *kevbuf,
|
|
|
|
size_t kevcnt)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
|
|
|
struct kevent *kevp;
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec ats, sleepts;
|
2008-03-22 00:53:35 +03:00
|
|
|
struct knote *kn, *marker;
|
2005-10-23 05:33:32 +04:00
|
|
|
size_t count, nkev, nevents;
|
2008-03-22 00:53:35 +03:00
|
|
|
int timeout, error, rv;
|
|
|
|
filedesc_t *fdp;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
fdp = curlwp->l_fd;
|
|
|
|
kq = fp->f_data;
|
2002-10-23 13:10:23 +04:00
|
|
|
count = maxevents;
|
2005-10-23 05:33:32 +04:00
|
|
|
nkev = nevents = error = 0;
|
2008-03-22 00:53:35 +03:00
|
|
|
if (count == 0) {
|
|
|
|
*retval = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2003-02-21 23:57:09 +03:00
|
|
|
if (tsp) { /* timeout supplied */
|
2009-03-31 02:22:44 +04:00
|
|
|
ats = *tsp;
|
2009-03-29 23:21:19 +04:00
|
|
|
if (inittimeleft(&ats, &sleepts) == -1) {
|
2008-03-22 00:53:35 +03:00
|
|
|
*retval = maxevents;
|
|
|
|
return EINVAL;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2009-03-29 23:21:19 +04:00
|
|
|
timeout = tstohz(&ats);
|
2003-02-21 23:57:09 +03:00
|
|
|
if (timeout <= 0)
|
2006-07-15 02:35:15 +04:00
|
|
|
timeout = -1; /* do poll */
|
2002-10-23 13:10:23 +04:00
|
|
|
} else {
|
2003-02-21 23:57:09 +03:00
|
|
|
/* no timeout, wait forever */
|
2002-10-23 13:10:23 +04:00
|
|
|
timeout = 0;
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
marker = kmem_zalloc(sizeof(*marker), KM_SLEEP);
|
|
|
|
marker->kn_status = KN_MARKER;
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
retry:
|
2008-03-22 00:53:35 +03:00
|
|
|
kevp = kevbuf;
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kq->kq_count == 0) {
|
2008-03-22 00:53:35 +03:00
|
|
|
if (timeout >= 0) {
|
|
|
|
error = cv_timedwait_sig(&kq->kq_cv,
|
|
|
|
&kq->kq_lock, timeout);
|
|
|
|
if (error == 0) {
|
|
|
|
if (tsp == NULL || (timeout =
|
2009-03-29 23:21:19 +04:00
|
|
|
gettimeleft(&ats, &sleepts)) > 0)
|
2008-03-22 00:53:35 +03:00
|
|
|
goto retry;
|
|
|
|
} else {
|
|
|
|
/* don't restart after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
error = EINTR;
|
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = 0;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
} else {
|
|
|
|
/* mark end of knote list */
|
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
|
|
|
|
|
|
|
|
while (count != 0) {
|
|
|
|
kn = TAILQ_FIRST(&kq->kq_head); /* get next knote */
|
2008-03-24 01:39:48 +03:00
|
|
|
while ((kn->kn_status & KN_MARKER) != 0) {
|
|
|
|
if (kn == marker) {
|
|
|
|
/* it's our marker, stop */
|
|
|
|
TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
|
|
|
|
if (count < maxevents || (tsp != NULL &&
|
2009-03-29 23:21:19 +04:00
|
|
|
(timeout = gettimeleft(&ats,
|
|
|
|
&sleepts)) <= 0))
|
2008-03-24 01:39:48 +03:00
|
|
|
goto done;
|
|
|
|
goto retry;
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
/* someone else's marker. */
|
|
|
|
kn = TAILQ_NEXT(kn, kn_tqe);
|
|
|
|
}
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
|
|
|
|
kq->kq_count--;
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_status &= ~KN_QUEUED;
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
if (kn->kn_status & KN_DISABLED) {
|
|
|
|
/* don't want disabled events */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((kn->kn_flags & EV_ONESHOT) == 0) {
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
KERNEL_LOCK(1, NULL); /* XXXSMP */
|
|
|
|
rv = (*kn->kn_fop->f_event)(kn, 0);
|
|
|
|
KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
|
2008-03-26 16:32:32 +03:00
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
/* Re-poll if note was re-enqueued. */
|
|
|
|
if ((kn->kn_status & KN_QUEUED) != 0)
|
|
|
|
continue;
|
2008-03-22 00:53:35 +03:00
|
|
|
if (rv == 0) {
|
|
|
|
/*
|
|
|
|
* non-ONESHOT event that hasn't
|
|
|
|
* triggered again, so de-queue.
|
|
|
|
*/
|
|
|
|
kn->kn_status &= ~KN_ACTIVE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-03-26 16:32:32 +03:00
|
|
|
/* XXXAD should be got from f_event if !oneshot. */
|
2008-03-22 00:53:35 +03:00
|
|
|
*kevp++ = kn->kn_kevent;
|
|
|
|
nkev++;
|
|
|
|
if (kn->kn_flags & EV_ONESHOT) {
|
|
|
|
/* delete ONESHOT events after retrieval */
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
mutex_enter(&fdp->fd_lock);
|
|
|
|
knote_detach(kn, fdp, true);
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
} else if (kn->kn_flags & EV_CLEAR) {
|
|
|
|
/* clear state after retrieval */
|
|
|
|
kn->kn_data = 0;
|
|
|
|
kn->kn_fflags = 0;
|
|
|
|
kn->kn_status &= ~KN_ACTIVE;
|
|
|
|
} else {
|
|
|
|
/* add event back on list */
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
|
|
|
|
kq->kq_count++;
|
|
|
|
kn->kn_status |= KN_QUEUED;
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
|
|
|
if (nkev == kevcnt) {
|
|
|
|
/* do copyouts in kevcnt chunks */
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2008-03-22 13:24:17 +03:00
|
|
|
error = (*keops->keo_put_events)
|
|
|
|
(keops->keo_private,
|
2008-03-22 00:53:35 +03:00
|
|
|
kevbuf, ulistp, nevents, nkev);
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
nevents += nkev;
|
|
|
|
nkev = 0;
|
|
|
|
kevp = kevbuf;
|
|
|
|
}
|
|
|
|
count--;
|
|
|
|
if (error != 0 || count == 0) {
|
|
|
|
/* remove marker */
|
|
|
|
TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
|
2002-10-23 13:10:23 +04:00
|
|
|
break;
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
}
|
2008-03-24 01:39:48 +03:00
|
|
|
done:
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
if (marker != NULL)
|
|
|
|
kmem_free(marker, sizeof(*marker));
|
|
|
|
if (nkev != 0) {
|
2002-10-23 13:10:23 +04:00
|
|
|
/* copyout remaining events */
|
2005-10-23 05:33:32 +04:00
|
|
|
error = (*keops->keo_put_events)(keops->keo_private,
|
2008-03-22 00:53:35 +03:00
|
|
|
kevbuf, ulistp, nevents, nkev);
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
*retval = maxevents - count;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
return error;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* fileops ioctl method for a kqueue descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
*
|
|
|
|
* Two ioctls are currently supported. They both use struct kfilter_mapping:
|
|
|
|
* KFILTER_BYNAME find name for filter, and return result in
|
|
|
|
* name, which is of size len.
|
|
|
|
* KFILTER_BYFILTER find filter for name. len is ignored.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_ioctl(file_t *fp, u_long com, void *data)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kfilter_mapping *km;
|
|
|
|
const struct kfilter *kfilter;
|
|
|
|
char *name;
|
|
|
|
int error;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
km = data;
|
2002-10-23 13:10:23 +04:00
|
|
|
error = 0;
|
2008-03-22 00:53:35 +03:00
|
|
|
name = kmem_alloc(KFILTER_MAXNAME, KM_SLEEP);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
switch (com) {
|
|
|
|
case KFILTER_BYFILTER: /* convert filter -> name */
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_enter(&kqueue_filter_lock, RW_READER);
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter = kfilter_byfilter(km->filter);
|
2008-03-22 00:53:35 +03:00
|
|
|
if (kfilter != NULL) {
|
|
|
|
strlcpy(name, kfilter->name, KFILTER_MAXNAME);
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
|
|
|
error = copyoutstr(name, km->name, km->len, NULL);
|
|
|
|
} else {
|
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
error = ENOENT;
|
2008-03-22 00:53:35 +03:00
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KFILTER_BYNAME: /* convert name -> filter */
|
|
|
|
error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
|
|
|
|
if (error) {
|
|
|
|
break;
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_enter(&kqueue_filter_lock, RW_READER);
|
2002-10-23 13:10:23 +04:00
|
|
|
kfilter = kfilter_byname(name);
|
|
|
|
if (kfilter != NULL)
|
|
|
|
km->filter = kfilter->filter;
|
|
|
|
else
|
|
|
|
error = ENOENT;
|
2008-03-22 00:53:35 +03:00
|
|
|
rw_exit(&kqueue_filter_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
2008-03-22 00:53:35 +03:00
|
|
|
break;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
kmem_free(name, KFILTER_MAXNAME);
|
2002-10-23 13:10:23 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* fileops fcntl method for a kqueue descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_fcntl(file_t *fp, u_int com, void *data)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* fileops poll method for a kqueue descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
* Determine if kqueue has events pending.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_poll(file_t *fp, int events)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
|
|
|
int revents;
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = fp->f_data;
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
revents = 0;
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
if (kq->kq_count != 0) {
|
2002-10-23 13:10:23 +04:00
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
} else {
|
2008-03-22 00:53:35 +03:00
|
|
|
selrecord(curlwp, &kq->kq_sel);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
return revents;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* fileops stat method for a kqueue descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
* Returns dummy info, with st_size being number of events pending.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_stat(file_t *fp, struct stat *st)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kqueue *kq;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = fp->f_data;
|
|
|
|
|
|
|
|
memset(st, 0, sizeof(*st));
|
2002-10-23 13:10:23 +04:00
|
|
|
st->st_size = kq->kq_count;
|
|
|
|
st->st_blksize = sizeof(struct kevent);
|
|
|
|
st->st_mode = S_IFIFO;
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
return 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
static void
|
|
|
|
kqueue_doclose(struct kqueue *kq, struct klist *list, int fd)
|
|
|
|
{
|
|
|
|
struct knote *kn;
|
|
|
|
filedesc_t *fdp;
|
|
|
|
|
|
|
|
fdp = kq->kq_fdp;
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&fdp->fd_lock));
|
|
|
|
|
|
|
|
for (kn = SLIST_FIRST(list); kn != NULL;) {
|
|
|
|
if (kq != kn->kn_kq) {
|
|
|
|
kn = SLIST_NEXT(kn, kn_link);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
knote_detach(kn, fdp, true);
|
|
|
|
mutex_enter(&fdp->fd_lock);
|
|
|
|
kn = SLIST_FIRST(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* fileops close method for a kqueue descriptor.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_close(file_t *fp)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kqueue *kq;
|
|
|
|
filedesc_t *fdp;
|
|
|
|
fdfile_t *ff;
|
|
|
|
int i;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
kq = fp->f_data;
|
2012-11-24 19:14:32 +04:00
|
|
|
fp->f_data = NULL;
|
|
|
|
fp->f_type = 0;
|
2008-03-22 00:53:35 +03:00
|
|
|
fdp = curlwp->l_fd;
|
|
|
|
|
|
|
|
mutex_enter(&fdp->fd_lock);
|
|
|
|
for (i = 0; i <= fdp->fd_lastkqfile; i++) {
|
2009-05-25 01:41:25 +04:00
|
|
|
if ((ff = fdp->fd_dt->dt_ff[i]) == NULL)
|
2008-03-22 00:53:35 +03:00
|
|
|
continue;
|
|
|
|
kqueue_doclose(kq, (struct klist *)&ff->ff_knlist, i);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
if (fdp->fd_knhashmask != 0) {
|
|
|
|
for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_doclose(kq, &fdp->fd_knhash[i], -1);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&fdp->fd_lock);
|
|
|
|
|
|
|
|
KASSERT(kq->kq_count == 0);
|
|
|
|
mutex_destroy(&kq->kq_lock);
|
|
|
|
cv_destroy(&kq->kq_cv);
|
2008-03-01 17:16:49 +03:00
|
|
|
seldestroy(&kq->kq_sel);
|
2008-03-22 00:53:35 +03:00
|
|
|
kmem_free(kq, sizeof(*kq));
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct fileops kqfilter method for a kqueue descriptor.
|
|
|
|
* Event triggered when monitored kqueue changes.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-22 00:53:35 +03:00
|
|
|
kqueue_kqfilter(file_t *fp, struct knote *kn)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
kq = ((file_t *)kn->kn_obj)->f_data;
|
|
|
|
|
|
|
|
KASSERT(fp == kn->kn_obj);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
if (kn->kn_filter != EVFILT_READ)
|
2008-03-22 00:53:35 +03:00
|
|
|
return 1;
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_fop = &kqread_filtops;
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_enter(&kq->kq_lock);
|
2002-11-26 21:44:34 +03:00
|
|
|
SLIST_INSERT_HEAD(&kq->kq_sel.sel_klist, kn, kn_selnext);
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&kq->kq_lock);
|
|
|
|
|
|
|
|
return 0;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Walk down a list of knotes, activating them if their event has
|
|
|
|
* triggered. The caller's object lock (e.g. device driver lock)
|
|
|
|
* must be held.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
knote(struct klist *list, long hint)
|
|
|
|
{
|
2010-09-10 14:23:46 +04:00
|
|
|
struct knote *kn, *tmpkn;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2010-09-10 14:23:46 +04:00
|
|
|
SLIST_FOREACH_SAFE(kn, list, kn_selnext, tmpkn) {
|
2008-03-22 00:53:35 +03:00
|
|
|
if ((*kn->kn_fop->f_event)(kn, hint))
|
|
|
|
knote_activate(kn);
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Remove all knotes referencing a specified fd
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2008-03-22 00:53:35 +03:00
|
|
|
knote_fdclose(int fd)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct klist *list;
|
2002-10-23 13:10:23 +04:00
|
|
|
struct knote *kn;
|
2008-03-22 00:53:35 +03:00
|
|
|
filedesc_t *fdp;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
fdp = curlwp->l_fd;
|
2009-05-25 01:41:25 +04:00
|
|
|
list = (struct klist *)&fdp->fd_dt->dt_ff[fd]->ff_knlist;
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_enter(&fdp->fd_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
while ((kn = SLIST_FIRST(list)) != NULL) {
|
2008-03-22 00:53:35 +03:00
|
|
|
knote_detach(kn, fdp, true);
|
|
|
|
mutex_enter(&fdp->fd_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&fdp->fd_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-22 00:53:35 +03:00
|
|
|
* Drop knote. Called with fdp->fd_lock held, and will drop before
|
|
|
|
* returning.
|
2002-10-23 13:10:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2008-03-22 00:53:35 +03:00
|
|
|
knote_detach(struct knote *kn, filedesc_t *fdp, bool dofop)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct klist *list;
|
2008-03-26 16:32:32 +03:00
|
|
|
struct kqueue *kq;
|
|
|
|
|
|
|
|
kq = kn->kn_kq;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
KASSERT((kn->kn_status & KN_MARKER) == 0);
|
|
|
|
KASSERT(mutex_owned(&fdp->fd_lock));
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-26 16:32:32 +03:00
|
|
|
/* Remove from monitored object. */
|
2008-03-22 00:53:35 +03:00
|
|
|
if (dofop) {
|
|
|
|
KERNEL_LOCK(1, NULL); /* XXXSMP */
|
|
|
|
(*kn->kn_fop->f_detach)(kn);
|
|
|
|
KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-03-26 16:32:32 +03:00
|
|
|
/* Remove from descriptor table. */
|
2002-10-23 13:10:23 +04:00
|
|
|
if (kn->kn_fop->f_isfd)
|
2009-05-25 01:41:25 +04:00
|
|
|
list = (struct klist *)&fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
|
2002-10-23 13:10:23 +04:00
|
|
|
else
|
|
|
|
list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
|
|
|
|
|
|
|
|
SLIST_REMOVE(list, kn, knote, kn_link);
|
2008-03-26 16:32:32 +03:00
|
|
|
|
|
|
|
/* Remove from kqueue. */
|
|
|
|
/* XXXAD should verify not in use by kqueue_scan. */
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
if ((kn->kn_status & KN_QUEUED) != 0) {
|
|
|
|
kq_check(kq);
|
|
|
|
TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
|
|
|
|
kn->kn_status &= ~KN_QUEUED;
|
|
|
|
kq->kq_count--;
|
|
|
|
kq_check(kq);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
|
|
|
|
2008-03-22 00:53:35 +03:00
|
|
|
mutex_exit(&fdp->fd_lock);
|
|
|
|
if (kn->kn_fop->f_isfd)
|
|
|
|
fd_putfile(kn->kn_id);
|
|
|
|
atomic_dec_uint(&kn->kn_kfilter->refcnt);
|
|
|
|
kmem_free(kn, sizeof(*kn));
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Queue new event for knote.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
knote_enqueue(struct knote *kn)
|
|
|
|
{
|
2008-03-22 00:53:35 +03:00
|
|
|
struct kqueue *kq;
|
|
|
|
|
|
|
|
KASSERT((kn->kn_status & KN_MARKER) == 0);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
kq = kn->kn_kq;
|
2008-03-22 00:53:35 +03:00
|
|
|
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
if ((kn->kn_status & KN_DISABLED) != 0) {
|
|
|
|
kn->kn_status &= ~KN_DISABLED;
|
|
|
|
}
|
|
|
|
if ((kn->kn_status & (KN_ACTIVE | KN_QUEUED)) == KN_ACTIVE) {
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
|
|
|
|
kn->kn_status |= KN_QUEUED;
|
|
|
|
kq->kq_count++;
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
cv_broadcast(&kq->kq_cv);
|
|
|
|
selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-03-22 00:53:35 +03:00
|
|
|
/*
|
|
|
|
* Queue new event for knote.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
knote_activate(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct kqueue *kq;
|
|
|
|
|
|
|
|
KASSERT((kn->kn_status & KN_MARKER) == 0);
|
|
|
|
|
|
|
|
kq = kn->kn_kq;
|
|
|
|
|
|
|
|
mutex_spin_enter(&kq->kq_lock);
|
|
|
|
kn->kn_status |= KN_ACTIVE;
|
|
|
|
if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) {
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
|
|
|
|
kn->kn_status |= KN_QUEUED;
|
|
|
|
kq->kq_count++;
|
2008-03-24 12:09:55 +03:00
|
|
|
kq_check(kq);
|
2008-03-22 00:53:35 +03:00
|
|
|
cv_broadcast(&kq->kq_cv);
|
|
|
|
selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&kq->kq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|