2000-04-16 19:02:08 +00:00
|
|
|
/*-
|
2001-02-24 01:44:03 +00:00
|
|
|
* Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
|
2004-08-15 06:24:42 +00:00
|
|
|
* Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
|
2000-04-16 19:02:08 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/proc.h>
|
2004-07-14 07:02:03 +00:00
|
|
|
#include <sys/malloc.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/file.h>
|
2003-01-01 01:56:19 +00:00
|
|
|
#include <sys/filedesc.h>
|
2004-07-14 07:02:03 +00:00
|
|
|
#include <sys/filio.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/fcntl.h>
|
2004-08-15 06:24:42 +00:00
|
|
|
#include <sys/kthread.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#include <sys/selinfo.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/eventvar.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/protosw.h>
|
2004-07-14 07:02:03 +00:00
|
|
|
#include <sys/sigio.h>
|
|
|
|
#include <sys/signalvar.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/stat.h>
|
2001-09-29 17:48:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/sysproto.h>
|
2005-03-01 17:45:55 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2004-08-15 06:24:42 +00:00
|
|
|
#include <sys/taskqueue.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2002-03-20 04:09:59 +00:00
|
|
|
#include <vm/uma.h>
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2005-02-10 12:02:37 +00:00
|
|
|
static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/*
|
|
|
|
* This lock is used if multiple kq locks are required. This possibly
|
|
|
|
* should be made into a per proc lock.
|
|
|
|
*/
|
|
|
|
static struct mtx kq_global;
|
|
|
|
MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
|
|
|
|
#define KQ_GLOBAL_LOCK(lck, haslck) do { \
|
|
|
|
if (!haslck) \
|
|
|
|
mtx_lock(lck); \
|
|
|
|
haslck = 1; \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
|
|
|
|
if (haslck) \
|
|
|
|
mtx_unlock(lck); \
|
|
|
|
haslck = 0; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
TASKQUEUE_DEFINE_THREAD(kqueue);
|
|
|
|
|
2005-03-01 17:45:55 +00:00
|
|
|
static int kevent_copyout(struct kevent **eventlist, enum uio_seg eventseg,
|
|
|
|
struct kevent *kevp, int count);
|
2004-08-15 06:24:42 +00:00
|
|
|
static int kqueue_aquire(struct file *fp, struct kqueue **kqp);
|
|
|
|
static void kqueue_release(struct kqueue *kq, int locked);
|
|
|
|
static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
|
|
|
|
uintptr_t ident, int waitok);
|
|
|
|
static void kqueue_task(void *arg, int pending);
|
|
|
|
static int kqueue_scan(struct kqueue *kq, int maxevents,
|
2005-03-01 17:45:55 +00:00
|
|
|
struct kevent *eventlist, enum uio_seg eventseg,
|
|
|
|
const struct timespec *timeout, struct kevent *keva,
|
|
|
|
struct thread *td);
|
2000-04-16 19:02:08 +00:00
|
|
|
static void kqueue_wakeup(struct kqueue *kq);
|
2004-08-15 06:24:42 +00:00
|
|
|
static struct filterops *kqueue_fo_find(int filt);
|
|
|
|
static void kqueue_fo_release(int filt);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2002-12-24 09:44:51 +00:00
|
|
|
static fo_rdwr_t kqueue_read;
|
|
|
|
static fo_rdwr_t kqueue_write;
|
|
|
|
static fo_ioctl_t kqueue_ioctl;
|
|
|
|
static fo_poll_t kqueue_poll;
|
|
|
|
static fo_kqfilter_t kqueue_kqfilter;
|
|
|
|
static fo_stat_t kqueue_stat;
|
|
|
|
static fo_close_t kqueue_close;
|
2002-12-23 21:53:20 +00:00
|
|
|
|
2001-02-15 16:34:11 +00:00
|
|
|
static struct fileops kqueueops = {
|
2003-06-18 18:16:40 +00:00
|
|
|
.fo_read = kqueue_read,
|
|
|
|
.fo_write = kqueue_write,
|
|
|
|
.fo_ioctl = kqueue_ioctl,
|
|
|
|
.fo_poll = kqueue_poll,
|
|
|
|
.fo_kqfilter = kqueue_kqfilter,
|
|
|
|
.fo_stat = kqueue_stat,
|
|
|
|
.fo_close = kqueue_close,
|
2001-02-15 16:34:11 +00:00
|
|
|
};
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
static int knote_attach(struct knote *kn, struct kqueue *kq);
|
2001-09-12 08:38:13 +00:00
|
|
|
static void knote_drop(struct knote *kn, struct thread *td);
|
2000-04-16 19:02:08 +00:00
|
|
|
static void knote_enqueue(struct knote *kn);
|
|
|
|
static void knote_dequeue(struct knote *kn);
|
|
|
|
static void knote_init(void);
|
2004-08-15 06:24:42 +00:00
|
|
|
static struct knote *knote_alloc(int waitok);
|
2000-04-16 19:02:08 +00:00
|
|
|
static void knote_free(struct knote *kn);
|
|
|
|
|
2001-02-15 16:34:11 +00:00
|
|
|
static void filt_kqdetach(struct knote *kn);
|
|
|
|
static int filt_kqueue(struct knote *kn, long hint);
|
|
|
|
static int filt_procattach(struct knote *kn);
|
|
|
|
static void filt_procdetach(struct knote *kn);
|
|
|
|
static int filt_proc(struct knote *kn, long hint);
|
|
|
|
static int filt_fileattach(struct knote *kn);
|
2001-07-19 18:34:40 +00:00
|
|
|
static void filt_timerexpire(void *knx);
|
|
|
|
static int filt_timerattach(struct knote *kn);
|
|
|
|
static void filt_timerdetach(struct knote *kn);
|
|
|
|
static int filt_timer(struct knote *kn, long hint);
|
2001-02-15 16:34:11 +00:00
|
|
|
|
2001-07-19 18:34:40 +00:00
|
|
|
static struct filterops file_filtops =
|
|
|
|
{ 1, filt_fileattach, NULL, NULL };
|
2001-02-15 16:34:11 +00:00
|
|
|
static struct filterops kqread_filtops =
|
|
|
|
{ 1, NULL, filt_kqdetach, filt_kqueue };
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_proc.c? */
|
2001-02-15 16:34:11 +00:00
|
|
|
static struct filterops proc_filtops =
|
|
|
|
{ 0, filt_procattach, filt_procdetach, filt_proc };
|
2001-07-19 18:34:40 +00:00
|
|
|
static struct filterops timer_filtops =
|
|
|
|
{ 0, filt_timerattach, filt_timerdetach, filt_timer };
|
2001-02-15 16:34:11 +00:00
|
|
|
|
2002-03-20 04:09:59 +00:00
|
|
|
static uma_zone_t knote_zone;
|
2001-09-29 17:48:39 +00:00
|
|
|
static int kq_ncallouts = 0;
|
|
|
|
static int kq_calloutmax = (4 * 1024);
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
|
|
|
|
&kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - ensure not KN_INFLUX?? */
|
|
|
|
#define KNOTE_ACTIVATE(kn, islock) do { \
|
|
|
|
if ((islock)) \
|
|
|
|
mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
|
|
|
|
else \
|
|
|
|
KQ_LOCK((kn)->kn_kq); \
|
|
|
|
(kn)->kn_status |= KN_ACTIVE; \
|
|
|
|
if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
|
|
|
|
knote_enqueue((kn)); \
|
|
|
|
if (!(islock)) \
|
|
|
|
KQ_UNLOCK((kn)->kn_kq); \
|
2000-04-16 19:02:08 +00:00
|
|
|
} while(0)
|
2004-08-15 06:24:42 +00:00
|
|
|
#define KQ_LOCK(kq) do { \
|
|
|
|
mtx_lock(&(kq)->kq_lock); \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_FLUX_WAKEUP(kq) do { \
|
|
|
|
if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
|
|
|
|
(kq)->kq_state &= ~KQ_FLUXWAIT; \
|
|
|
|
wakeup((kq)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_UNLOCK_FLUX(kq) do { \
|
|
|
|
KQ_FLUX_WAKEUP(kq); \
|
|
|
|
mtx_unlock(&(kq)->kq_lock); \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_UNLOCK(kq) do { \
|
|
|
|
mtx_unlock(&(kq)->kq_lock); \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_OWNED(kq) do { \
|
|
|
|
mtx_assert(&(kq)->kq_lock, MA_OWNED); \
|
|
|
|
} while (0)
|
|
|
|
#define KQ_NOTOWNED(kq) do { \
|
|
|
|
mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
|
|
|
|
} while (0)
|
|
|
|
#define KN_LIST_LOCK(kn) do { \
|
|
|
|
if (kn->kn_knlist != NULL) \
|
|
|
|
mtx_lock(kn->kn_knlist->kl_lock); \
|
|
|
|
} while (0)
|
|
|
|
#define KN_LIST_UNLOCK(kn) do { \
|
|
|
|
if (kn->kn_knlist != NULL) \
|
|
|
|
mtx_unlock(kn->kn_knlist->kl_lock); \
|
|
|
|
} while (0)
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
#define KN_HASHSIZE 64 /* XXX should be tunable */
|
|
|
|
#define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
|
|
|
|
|
2001-12-29 07:13:47 +00:00
|
|
|
static int
|
|
|
|
filt_nullattach(struct knote *kn)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (ENXIO);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct filterops null_filtops =
|
|
|
|
{ 0, filt_nullattach, NULL, NULL };
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - make SYSINIT to add these, and move into respective modules. */
|
2000-04-16 19:02:08 +00:00
|
|
|
extern struct filterops sig_filtops;
|
2004-07-04 10:52:54 +00:00
|
|
|
extern struct filterops fs_filtops;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-15 16:34:11 +00:00
|
|
|
* Table for for all system-defined filters.
|
2000-04-16 19:02:08 +00:00
|
|
|
*/
|
2004-08-15 06:24:42 +00:00
|
|
|
static struct mtx filterops_lock;
|
|
|
|
MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
|
|
|
|
MTX_DEF);
|
|
|
|
static struct {
|
|
|
|
struct filterops *for_fop;
|
|
|
|
int for_refcnt;
|
|
|
|
} sysfilt_ops[EVFILT_SYSCOUNT] = {
|
|
|
|
{ &file_filtops }, /* EVFILT_READ */
|
|
|
|
{ &file_filtops }, /* EVFILT_WRITE */
|
|
|
|
{ &null_filtops }, /* EVFILT_AIO */
|
|
|
|
{ &file_filtops }, /* EVFILT_VNODE */
|
|
|
|
{ &proc_filtops }, /* EVFILT_PROC */
|
|
|
|
{ &sig_filtops }, /* EVFILT_SIGNAL */
|
|
|
|
{ &timer_filtops }, /* EVFILT_TIMER */
|
|
|
|
{ &file_filtops }, /* EVFILT_NETDEV */
|
|
|
|
{ &fs_filtops }, /* EVFILT_FS */
|
2000-04-16 19:02:08 +00:00
|
|
|
};
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/*
|
|
|
|
* Simple redirection for all cdevsw style objects to call their fo_kqfilter
|
|
|
|
* method.
|
|
|
|
*/
|
2000-04-16 19:02:08 +00:00
|
|
|
static int
|
2001-02-15 16:34:11 +00:00
|
|
|
filt_fileattach(struct knote *kn)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-13 07:38:58 +00:00
|
|
|
|
2001-02-15 16:34:11 +00:00
|
|
|
return (fo_kqfilter(kn->kn_fp, kn));
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2001-02-15 16:34:11 +00:00
|
|
|
/*ARGSUSED*/
|
2000-04-16 19:02:08 +00:00
|
|
|
static int
|
2001-02-15 16:34:11 +00:00
|
|
|
kqueue_kqfilter(struct file *fp, struct knote *kn)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct kqueue *kq = kn->kn_fp->f_data;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2001-02-15 16:34:11 +00:00
|
|
|
if (kn->kn_filter != EVFILT_READ)
|
2004-08-15 06:24:42 +00:00
|
|
|
return (EINVAL);
|
2001-02-15 16:34:11 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status |= KN_KQUEUE;
|
2001-02-15 16:34:11 +00:00
|
|
|
kn->kn_fop = &kqread_filtops;
|
2004-08-15 06:24:42 +00:00
|
|
|
knlist_add(&kq->kq_sel.si_note, kn, 0);
|
|
|
|
|
2000-04-16 19:02:08 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filt_kqdetach(struct knote *kn)
|
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct kqueue *kq = kn->kn_fp->f_data;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
knlist_remove(&kq->kq_sel.si_note, kn, 0);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
|
|
|
filt_kqueue(struct knote *kn, long hint)
|
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct kqueue *kq = kn->kn_fp->f_data;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
kn->kn_data = kq->kq_count;
|
|
|
|
return (kn->kn_data > 0);
|
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_proc.c? */
|
2000-04-16 19:02:08 +00:00
|
|
|
static int
|
|
|
|
filt_procattach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
2003-04-12 01:57:04 +00:00
|
|
|
int immediate;
|
2001-04-12 21:32:02 +00:00
|
|
|
int error;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2003-04-12 01:57:04 +00:00
|
|
|
immediate = 0;
|
2000-04-16 19:02:08 +00:00
|
|
|
p = pfind(kn->kn_id);
|
2003-04-12 01:57:04 +00:00
|
|
|
if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
|
|
|
|
p = zpfind(kn->kn_id);
|
|
|
|
immediate = 1;
|
2004-08-15 06:24:42 +00:00
|
|
|
} else if (p != NULL && (p->p_flag & P_WEXIT)) {
|
|
|
|
immediate = 1;
|
2003-04-12 01:57:04 +00:00
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2003-11-04 01:41:47 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return (ESRCH);
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((error = p_cansee(curthread, p)))
|
2001-04-12 21:32:02 +00:00
|
|
|
return (error);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
kn->kn_ptr.p_proc = p;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-11-14 18:49:01 +00:00
|
|
|
if (immediate == 0)
|
2004-08-15 06:24:42 +00:00
|
|
|
knlist_add(&p->p_klist, kn, 1);
|
2003-04-12 01:57:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Immediately activate any exit notes if the target process is a
|
|
|
|
* zombie. This is necessary to handle the case where the target
|
|
|
|
* process, e.g. a child, dies before the kevent is registered.
|
|
|
|
*/
|
|
|
|
if (immediate && filt_proc(kn, NOTE_EXIT))
|
2004-08-15 06:24:42 +00:00
|
|
|
KNOTE_ACTIVATE(kn, 0);
|
2003-04-12 01:57:04 +00:00
|
|
|
|
2001-01-24 00:35:12 +00:00
|
|
|
PROC_UNLOCK(p);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 does not exist any more.
|
|
|
|
*/
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_proc.c? */
|
2000-04-16 19:02:08 +00:00
|
|
|
static void
|
|
|
|
filt_procdetach(struct knote *kn)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct proc *p;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
p = kn->kn_ptr.p_proc;
|
|
|
|
knlist_remove(&p->p_klist, kn, 0);
|
|
|
|
kn->kn_ptr.p_proc = NULL;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_proc.c? */
|
2000-04-16 19:02:08 +00:00
|
|
|
static int
|
|
|
|
filt_proc(struct knote *kn, long hint)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct proc *p = kn->kn_ptr.p_proc;
|
2000-04-16 19:02:08 +00:00
|
|
|
u_int event;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mask off extra data
|
|
|
|
*/
|
|
|
|
event = (u_int)hint & NOTE_PCTRLMASK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the user is interested in this event, record it.
|
|
|
|
*/
|
|
|
|
if (kn->kn_sfflags & event)
|
|
|
|
kn->kn_fflags |= event;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process is gone, so flag the event as finished.
|
|
|
|
*/
|
|
|
|
if (event == NOTE_EXIT) {
|
2004-08-15 06:24:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
knlist_remove_inevent(&p->p_klist, kn);
|
2004-08-12 18:06:21 +00:00
|
|
|
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_ptr.p_proc = NULL;
|
2000-04-16 19:02:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2003-11-04 01:14:58 +00:00
|
|
|
/*
|
2000-04-16 19:02:08 +00: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.
|
|
|
|
*/
|
|
|
|
if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
|
|
|
|
struct kevent kev;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* register knote with new process.
|
|
|
|
*/
|
|
|
|
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 */
|
2000-06-22 18:39:31 +00:00
|
|
|
kev.udata = kn->kn_kevent.udata; /* preserve udata */
|
2004-08-15 06:24:42 +00:00
|
|
|
error = kqueue_register(kn->kn_kq, &kev, NULL, 0);
|
2000-04-16 19:02:08 +00:00
|
|
|
if (error)
|
|
|
|
kn->kn_fflags |= NOTE_TRACKERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (kn->kn_fflags != 0);
|
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
static int
|
|
|
|
timertoticks(intptr_t data)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int tticks;
|
|
|
|
|
|
|
|
tv.tv_sec = data / 1000;
|
|
|
|
tv.tv_usec = (data % 1000) * 1000;
|
|
|
|
tticks = tvtohz(&tv);
|
|
|
|
|
|
|
|
return tticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - move to kern_timeout.c? */
|
2001-07-19 18:34:40 +00:00
|
|
|
static void
|
|
|
|
filt_timerexpire(void *knx)
|
|
|
|
{
|
|
|
|
struct knote *kn = knx;
|
2001-09-29 17:48:39 +00:00
|
|
|
struct callout *calloutp;
|
2001-07-19 18:34:40 +00:00
|
|
|
|
|
|
|
kn->kn_data++;
|
2004-08-15 06:24:42 +00:00
|
|
|
KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
|
2001-07-19 18:34:40 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
|
2001-09-29 17:48:39 +00:00
|
|
|
calloutp = (struct callout *)kn->kn_hook;
|
2004-08-15 06:24:42 +00:00
|
|
|
callout_reset(calloutp, timertoticks(kn->kn_sdata),
|
|
|
|
filt_timerexpire, kn);
|
2001-07-19 18:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* data contains amount of time to sleep, in milliseconds
|
2004-08-12 18:06:21 +00:00
|
|
|
*/
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_timeout.c? */
|
2001-07-19 18:34:40 +00:00
|
|
|
static int
|
|
|
|
filt_timerattach(struct knote *kn)
|
|
|
|
{
|
2001-09-29 17:48:39 +00:00
|
|
|
struct callout *calloutp;
|
2001-07-19 18:34:40 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
atomic_add_int(&kq_ncallouts, 1);
|
2001-09-29 17:48:39 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if (kq_ncallouts >= kq_calloutmax) {
|
|
|
|
atomic_add_int(&kq_ncallouts, -1);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
2001-07-19 18:34:40 +00:00
|
|
|
|
|
|
|
kn->kn_flags |= EV_CLEAR; /* automatically set */
|
2004-10-14 03:26:50 +00:00
|
|
|
kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
|
2001-09-29 17:48:39 +00:00
|
|
|
MALLOC(calloutp, struct callout *, sizeof(*calloutp),
|
2003-02-19 05:47:46 +00:00
|
|
|
M_KQUEUE, M_WAITOK);
|
2005-02-22 13:11:33 +00:00
|
|
|
callout_init(calloutp, CALLOUT_MPSAFE);
|
2002-06-29 00:29:12 +00:00
|
|
|
kn->kn_hook = calloutp;
|
2004-08-15 06:24:42 +00:00
|
|
|
callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
|
|
|
|
kn);
|
2001-07-19 18:34:40 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_timeout.c? */
|
2001-07-19 18:34:40 +00:00
|
|
|
static void
|
|
|
|
filt_timerdetach(struct knote *kn)
|
|
|
|
{
|
2001-09-29 17:48:39 +00:00
|
|
|
struct callout *calloutp;
|
2001-07-19 18:34:40 +00:00
|
|
|
|
2001-09-29 17:48:39 +00:00
|
|
|
calloutp = (struct callout *)kn->kn_hook;
|
2004-04-07 05:59:57 +00:00
|
|
|
callout_drain(calloutp);
|
2001-09-29 17:48:39 +00:00
|
|
|
FREE(calloutp, M_KQUEUE);
|
2004-08-15 06:24:42 +00:00
|
|
|
atomic_add_int(&kq_ncallouts, -1);
|
2004-10-14 03:26:50 +00:00
|
|
|
kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
|
2001-07-19 18:34:40 +00:00
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/* XXX - move to kern_timeout.c? */
|
2001-07-19 18:34:40 +00:00
|
|
|
static int
|
|
|
|
filt_timer(struct knote *kn, long hint)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (kn->kn_data != 0);
|
|
|
|
}
|
|
|
|
|
2001-09-01 03:04:31 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
2000-06-10 01:51:18 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kqueue(struct thread *td, struct kqueue_args *uap)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2001-09-01 03:04:31 +00:00
|
|
|
struct filedesc *fdp;
|
2000-04-16 19:02:08 +00:00
|
|
|
struct kqueue *kq;
|
2000-06-10 01:51:18 +00:00
|
|
|
struct file *fp;
|
|
|
|
int fd, error;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
fdp = td->td_proc->p_fd;
|
|
|
|
error = falloc(td, &fp, &fd);
|
2000-06-10 01:51:18 +00:00
|
|
|
if (error)
|
2001-09-01 03:04:31 +00:00
|
|
|
goto done2;
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2003-10-19 20:41:07 +00:00
|
|
|
/* An extra reference on `nfp' has been held for us by falloc(). */
|
2004-08-15 06:24:42 +00:00
|
|
|
kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
|
|
|
|
mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
|
2002-01-13 11:58:06 +00:00
|
|
|
TAILQ_INIT(&kq->kq_head);
|
2004-08-15 06:24:42 +00:00
|
|
|
kq->kq_fdp = fdp;
|
|
|
|
knlist_init(&kq->kq_sel.si_note, &kq->kq_lock);
|
|
|
|
TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
|
|
|
|
|
2004-11-13 11:53:02 +00:00
|
|
|
FILEDESC_LOCK_FAST(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
|
2004-11-13 11:53:02 +00:00
|
|
|
FILEDESC_UNLOCK_FAST(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2002-01-13 11:58:06 +00:00
|
|
|
FILE_LOCK(fp);
|
2000-06-10 01:51:18 +00:00
|
|
|
fp->f_flag = FREAD | FWRITE;
|
|
|
|
fp->f_type = DTYPE_KQUEUE;
|
|
|
|
fp->f_ops = &kqueueops;
|
2003-01-13 00:33:17 +00:00
|
|
|
fp->f_data = kq;
|
2002-01-13 11:58:06 +00:00
|
|
|
FILE_UNLOCK(fp);
|
2003-10-19 20:41:07 +00:00
|
|
|
fdrop(fp, td);
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = fd;
|
2001-09-01 03:04:31 +00:00
|
|
|
done2:
|
2000-06-10 01:51:18 +00:00
|
|
|
return (error);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _SYS_SYSPROTO_H_
|
|
|
|
struct kevent_args {
|
|
|
|
int fd;
|
2000-07-28 22:32:25 +00:00
|
|
|
const struct kevent *changelist;
|
2000-04-16 19:02:08 +00:00
|
|
|
int nchanges;
|
|
|
|
struct kevent *eventlist;
|
2000-07-18 19:31:52 +00:00
|
|
|
int nevents;
|
2000-07-28 22:32:25 +00:00
|
|
|
const struct timespec *timeout;
|
2000-04-16 19:02:08 +00:00
|
|
|
};
|
|
|
|
#endif
|
2001-09-01 03:04:31 +00:00
|
|
|
/*
|
|
|
|
* MPSAFE
|
|
|
|
*/
|
2000-04-16 19:02:08 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
kevent(struct thread *td, struct kevent_args *uap)
|
2005-03-01 17:45:55 +00:00
|
|
|
{
|
|
|
|
struct timespec ts, *tsp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (uap->timeout != NULL) {
|
|
|
|
error = copyin(uap->timeout, &ts, sizeof(ts));
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
tsp = &ts;
|
|
|
|
} else
|
|
|
|
tsp = NULL;
|
|
|
|
|
|
|
|
return (kern_kevent(td, uap->fd, uap->changelist, uap->nchanges,
|
|
|
|
UIO_USERSPACE, uap->eventlist, uap->nevents, UIO_USERSPACE, tsp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy 'count' items into the destination list pointd to by *eventlist. The
|
|
|
|
* eventlist and nevents values are updated to point after the copied out
|
|
|
|
* item(s) upon return.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
kevent_copyout(struct kevent **eventlist, enum uio_seg eventseg,
|
|
|
|
struct kevent *kevp, int count)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (eventseg == UIO_USERSPACE)
|
|
|
|
error = copyout(kevp, *eventlist,
|
|
|
|
sizeof(struct kevent) * count);
|
|
|
|
else {
|
|
|
|
bcopy(kevp, *eventlist, sizeof(struct kevent) * count);
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
*eventlist += count;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_kevent(struct thread *td, int fd, struct kevent *changelist, int nchanges,
|
|
|
|
enum uio_seg changeseg, struct kevent *eventlist, int nevents,
|
|
|
|
enum uio_seg eventseg, const struct timespec *timeout)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct kevent keva[KQ_NEVENTS];
|
2005-03-01 17:45:55 +00:00
|
|
|
struct kevent *kevp, *changes;
|
2000-04-16 19:02:08 +00:00
|
|
|
struct kqueue *kq;
|
2001-11-14 06:30:36 +00:00
|
|
|
struct file *fp;
|
2000-04-16 19:02:08 +00:00
|
|
|
int i, n, nerrors, error;
|
|
|
|
|
2005-03-01 17:45:55 +00:00
|
|
|
if ((error = fget(td, fd, &fp)) != 0)
|
2002-01-14 00:13:45 +00:00
|
|
|
return (error);
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((error = kqueue_aquire(fp, &kq)) != 0)
|
|
|
|
goto done_norel;
|
|
|
|
|
2000-04-16 19:02:08 +00:00
|
|
|
nerrors = 0;
|
|
|
|
|
2005-03-01 17:45:55 +00:00
|
|
|
while (nchanges > 0) {
|
|
|
|
if (changeseg == UIO_USERSPACE) {
|
|
|
|
n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
|
|
|
|
error = copyin(changelist, keva, n * sizeof *keva);
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
changes = keva;
|
|
|
|
} else {
|
|
|
|
changes = changelist;
|
|
|
|
n = nchanges;
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2005-03-01 17:45:55 +00:00
|
|
|
kevp = &changes[i];
|
2000-07-18 19:31:52 +00:00
|
|
|
kevp->flags &= ~EV_SYSFLAGS;
|
2004-08-15 06:24:42 +00:00
|
|
|
error = kqueue_register(kq, kevp, td, 1);
|
2000-04-16 19:02:08 +00:00
|
|
|
if (error) {
|
2005-03-01 17:45:55 +00:00
|
|
|
if (nevents != 0) {
|
2000-07-18 19:31:52 +00:00
|
|
|
kevp->flags = EV_ERROR;
|
|
|
|
kevp->data = error;
|
2005-03-01 17:45:55 +00:00
|
|
|
(void) kevent_copyout(&eventlist,
|
|
|
|
eventseg, kevp, 1);
|
|
|
|
nevents--;
|
2000-04-16 19:02:08 +00:00
|
|
|
nerrors++;
|
|
|
|
} else {
|
2000-11-18 21:01:04 +00:00
|
|
|
goto done;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-01 17:45:55 +00:00
|
|
|
nchanges -= n;
|
|
|
|
changelist += n;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
if (nerrors) {
|
2004-08-15 06:24:42 +00:00
|
|
|
td->td_retval[0] = nerrors;
|
2000-11-18 21:01:04 +00:00
|
|
|
error = 0;
|
|
|
|
goto done;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-01 17:45:55 +00:00
|
|
|
error = kqueue_scan(kq, nevents, eventlist, eventseg, timeout,
|
2004-08-15 06:24:42 +00:00
|
|
|
keva, td);
|
2000-11-18 21:01:04 +00:00
|
|
|
done:
|
2004-08-15 06:24:42 +00:00
|
|
|
kqueue_release(kq, 0);
|
|
|
|
done_norel:
|
2000-11-18 21:01:04 +00:00
|
|
|
if (fp != NULL)
|
2001-09-12 08:38:13 +00:00
|
|
|
fdrop(fp, td);
|
2000-04-16 19:02:08 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-12-29 07:13:47 +00:00
|
|
|
int
|
|
|
|
kqueue_add_filteropts(int filt, struct filterops *filtops)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
|
|
|
|
printf(
|
|
|
|
"trying to add a filterop that is out of range: %d is beyond %d\n",
|
|
|
|
~filt, EVFILT_SYSCOUNT);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
mtx_lock(&filterops_lock);
|
|
|
|
if (sysfilt_ops[~filt].for_fop != &null_filtops &&
|
|
|
|
sysfilt_ops[~filt].for_fop != NULL)
|
|
|
|
error = EEXIST;
|
|
|
|
else {
|
|
|
|
sysfilt_ops[~filt].for_fop = filtops;
|
|
|
|
sysfilt_ops[~filt].for_refcnt = 0;
|
|
|
|
}
|
|
|
|
mtx_unlock(&filterops_lock);
|
2001-12-29 07:13:47 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kqueue_del_filteropts(int filt)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
int error;
|
2001-12-29 07:13:47 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
error = 0;
|
|
|
|
if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
mtx_lock(&filterops_lock);
|
|
|
|
if (sysfilt_ops[~filt].for_fop == &null_filtops ||
|
|
|
|
sysfilt_ops[~filt].for_fop == NULL)
|
|
|
|
error = EINVAL;
|
|
|
|
else if (sysfilt_ops[~filt].for_refcnt != 0)
|
|
|
|
error = EBUSY;
|
|
|
|
else {
|
|
|
|
sysfilt_ops[~filt].for_fop = &null_filtops;
|
|
|
|
sysfilt_ops[~filt].for_refcnt = 0;
|
|
|
|
}
|
|
|
|
mtx_unlock(&filterops_lock);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct filterops *
|
|
|
|
kqueue_fo_find(int filt)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mtx_lock(&filterops_lock);
|
|
|
|
sysfilt_ops[~filt].for_refcnt++;
|
|
|
|
if (sysfilt_ops[~filt].for_fop == NULL)
|
|
|
|
sysfilt_ops[~filt].for_fop = &null_filtops;
|
|
|
|
mtx_unlock(&filterops_lock);
|
|
|
|
|
|
|
|
return sysfilt_ops[~filt].for_fop;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_fo_release(int filt)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtx_lock(&filterops_lock);
|
|
|
|
KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
|
|
|
|
("filter object refcount not valid on release"));
|
|
|
|
sysfilt_ops[~filt].for_refcnt--;
|
|
|
|
mtx_unlock(&filterops_lock);
|
2001-12-29 07:13:47 +00:00
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/*
|
|
|
|
* A ref to kq (obtained via kqueue_aquire) should be held. waitok will
|
|
|
|
* influence if memory allocation should wait. Make sure it is 0 if you
|
|
|
|
* hold any mutexes.
|
|
|
|
*/
|
2000-04-16 19:02:08 +00:00
|
|
|
int
|
2004-08-15 06:24:42 +00:00
|
|
|
kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct filedesc *fdp;
|
2000-04-16 19:02:08 +00:00
|
|
|
struct filterops *fops;
|
2004-08-15 06:24:42 +00:00
|
|
|
struct file *fp;
|
|
|
|
struct knote *kn, *tkn;
|
|
|
|
int error, filt, event;
|
|
|
|
int haskqglobal;
|
|
|
|
int fd;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
fdp = NULL;
|
|
|
|
fp = NULL;
|
|
|
|
kn = NULL;
|
|
|
|
error = 0;
|
|
|
|
haskqglobal = 0;
|
|
|
|
|
|
|
|
filt = kev->filter;
|
|
|
|
fops = kqueue_fo_find(filt);
|
|
|
|
if (fops == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
tkn = knote_alloc(waitok); /* prevent waiting with locks */
|
|
|
|
|
|
|
|
findkn:
|
2000-04-16 19:02:08 +00:00
|
|
|
if (fops->f_isfd) {
|
2004-08-15 06:24:42 +00:00
|
|
|
KASSERT(td != NULL, ("td is NULL"));
|
|
|
|
fdp = td->td_proc->p_fd;
|
|
|
|
FILEDESC_LOCK(fdp);
|
2000-08-07 16:45:42 +00:00
|
|
|
/* validate descriptor */
|
2004-08-15 06:24:42 +00:00
|
|
|
fd = kev->ident;
|
|
|
|
if (fd < 0 || fd >= fdp->fd_nfiles ||
|
|
|
|
(fp = fdp->fd_ofiles[fd]) == NULL) {
|
2002-01-13 11:58:06 +00:00
|
|
|
FILEDESC_UNLOCK(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
error = EBADF;
|
|
|
|
goto done;
|
2002-01-13 11:58:06 +00:00
|
|
|
}
|
2000-11-18 21:01:04 +00:00
|
|
|
fhold(fp);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
|
|
|
|
kev->ident, 0) != 0) {
|
|
|
|
/* unlock and try again */
|
|
|
|
FILEDESC_UNLOCK(fdp);
|
|
|
|
fdrop(fp, td);
|
|
|
|
fp = NULL;
|
|
|
|
error = kqueue_expand(kq, fops, kev->ident, waitok);
|
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
goto findkn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp->f_type == DTYPE_KQUEUE) {
|
|
|
|
/*
|
|
|
|
* if we add some inteligence about what we are doing,
|
|
|
|
* we should be able to support events on ourselves.
|
|
|
|
* We need to know when we are doing this to prevent
|
|
|
|
* getting both the knlist lock and the kq lock since
|
|
|
|
* they are the same thing.
|
|
|
|
*/
|
|
|
|
if (fp->f_data == kq) {
|
|
|
|
FILEDESC_UNLOCK(fdp);
|
|
|
|
error = EINVAL;
|
|
|
|
goto done_noglobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
|
|
|
|
}
|
|
|
|
|
2004-11-16 14:41:31 +00:00
|
|
|
FILEDESC_UNLOCK(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (kev->ident < kq->kq_knlistsize) {
|
|
|
|
SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
|
|
|
|
if (kev->filter == kn->kn_filter)
|
2000-04-16 19:02:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((kev->flags & EV_ADD) == EV_ADD)
|
|
|
|
kqueue_expand(kq, fops, kev->ident, waitok);
|
|
|
|
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (kq->kq_knhashmask != 0) {
|
2000-04-16 19:02:08 +00:00
|
|
|
struct klist *list;
|
2004-08-13 07:38:58 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
list = &kq->kq_knhash[
|
|
|
|
KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
|
2000-04-16 19:02:08 +00:00
|
|
|
SLIST_FOREACH(kn, list, kn_link)
|
|
|
|
if (kev->ident == kn->kn_id &&
|
|
|
|
kev->filter == kn->kn_filter)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
/* knote is in the process of changing, wait for it to stablize. */
|
|
|
|
if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
|
|
|
|
if (fp != NULL) {
|
|
|
|
fdrop(fp, td);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
|
|
|
kq->kq_state |= KQ_FLUXWAIT;
|
|
|
|
msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
|
|
|
|
goto findkn;
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2000-11-18 21:01:04 +00:00
|
|
|
if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_UNLOCK(kq);
|
2000-11-18 21:01:04 +00:00
|
|
|
error = ENOENT;
|
|
|
|
goto done;
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* kn now contains the matching knote, or NULL if no match
|
|
|
|
*/
|
|
|
|
if (kev->flags & EV_ADD) {
|
|
|
|
if (kn == NULL) {
|
2004-08-15 06:24:42 +00:00
|
|
|
kn = tkn;
|
|
|
|
tkn = NULL;
|
2000-11-18 21:01:04 +00:00
|
|
|
if (kn == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
kn->kn_fp = fp;
|
|
|
|
kn->kn_kq = kq;
|
|
|
|
kn->kn_fop = fops;
|
2000-11-18 21:01:04 +00:00
|
|
|
/*
|
2004-08-15 06:24:42 +00:00
|
|
|
* apply reference counts to knote structure, and
|
2000-11-18 21:01:04 +00:00
|
|
|
* do not release it at the end of this routine.
|
|
|
|
*/
|
2004-08-15 06:24:42 +00:00
|
|
|
fops = NULL;
|
2000-11-18 21:01:04 +00:00
|
|
|
fp = NULL;
|
|
|
|
|
2000-06-22 18:39:31 +00:00
|
|
|
kn->kn_sfflags = kev->fflags;
|
|
|
|
kn->kn_sdata = kev->data;
|
|
|
|
kev->fflags = 0;
|
|
|
|
kev->data = 0;
|
|
|
|
kn->kn_kevent = *kev;
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status = KN_INFLUX|KN_DETACHED;
|
2000-06-22 18:39:31 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
error = knote_attach(kn, kq);
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
if (error != 0) {
|
|
|
|
tkn = kn;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = kn->kn_fop->f_attach(kn)) != 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
knote_drop(kn, td);
|
2000-04-16 19:02:08 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
KN_LIST_LOCK(kn);
|
2000-06-22 18:39:31 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The user may change some filter values after the
|
2004-08-12 18:06:21 +00:00
|
|
|
* initial EV_ADD, but doing so will not reset any
|
2002-10-29 20:51:44 +00:00
|
|
|
* filter which has already been triggered.
|
2000-06-22 18:39:31 +00:00
|
|
|
*/
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
KN_LIST_LOCK(kn);
|
2000-06-22 18:39:31 +00:00
|
|
|
kn->kn_sfflags = kev->fflags;
|
|
|
|
kn->kn_sdata = kev->data;
|
|
|
|
kn->kn_kevent.udata = kev->udata;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2000-06-22 18:39:31 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
/*
|
|
|
|
* We can get here with kn->kn_knlist == NULL.
|
|
|
|
* This can happen when the initial attach event decides that
|
|
|
|
* the event is "completed" already. i.e. filt_procattach
|
|
|
|
* is called on a zombie process. It will call filt_proc
|
|
|
|
* which will remove it from the list, and NULL kn_knlist.
|
|
|
|
*/
|
|
|
|
event = kn->kn_fop->f_event(kn, 0);
|
|
|
|
KN_LIST_UNLOCK(kn);
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (event)
|
|
|
|
KNOTE_ACTIVATE(kn, 1);
|
|
|
|
kn->kn_status &= ~KN_INFLUX;
|
2000-04-16 19:02:08 +00:00
|
|
|
} else if (kev->flags & EV_DELETE) {
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
2004-09-06 19:02:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
kn->kn_fop->f_detach(kn);
|
2001-09-12 08:38:13 +00:00
|
|
|
knote_drop(kn, td);
|
2000-04-16 19:02:08 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((kev->flags & EV_DISABLE) &&
|
|
|
|
((kn->kn_status & KN_DISABLED) == 0)) {
|
|
|
|
kn->kn_status |= KN_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
|
|
|
|
kn->kn_status &= ~KN_DISABLED;
|
|
|
|
if ((kn->kn_status & KN_ACTIVE) &&
|
|
|
|
((kn->kn_status & KN_QUEUED) == 0))
|
|
|
|
knote_enqueue(kn);
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_UNLOCK_FLUX(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
done:
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
|
|
|
done_noglobal:
|
2000-11-18 21:01:04 +00:00
|
|
|
if (fp != NULL)
|
2001-09-12 08:38:13 +00:00
|
|
|
fdrop(fp, td);
|
2004-08-15 06:24:42 +00:00
|
|
|
if (tkn != NULL)
|
|
|
|
knote_free(tkn);
|
|
|
|
if (fops != NULL)
|
|
|
|
kqueue_fo_release(filt);
|
2000-04-16 19:02:08 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-15 06:24:42 +00:00
|
|
|
kqueue_aquire(struct file *fp, struct kqueue **kqp)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct kqueue *kq;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
|
|
|
|
FILE_LOCK(fp);
|
|
|
|
do {
|
|
|
|
kq = fp->f_data;
|
|
|
|
if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
|
|
|
|
error = EBADF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*kqp = kq;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
error = EBADF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
kq->kq_refcnt++;
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
} while (0);
|
|
|
|
FILE_UNLOCK(fp);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_release(struct kqueue *kq, int locked)
|
|
|
|
{
|
|
|
|
if (locked)
|
|
|
|
KQ_OWNED(kq);
|
|
|
|
else
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
kq->kq_refcnt--;
|
|
|
|
if (kq->kq_refcnt == 1)
|
|
|
|
wakeup(&kq->kq_refcnt);
|
|
|
|
if (!locked)
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_schedtask(struct kqueue *kq)
|
|
|
|
{
|
|
|
|
|
|
|
|
KQ_OWNED(kq);
|
|
|
|
KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
|
|
|
|
("scheduling kqueue task while draining"));
|
|
|
|
|
|
|
|
if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
|
|
|
|
taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
|
|
|
|
kq->kq_state |= KQ_TASKSCHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand the kq to make sure we have storage for fops/ident pair.
|
|
|
|
*
|
|
|
|
* Return 0 on success (or no work necessary), return errno on failure.
|
|
|
|
*
|
|
|
|
* Not calling hashinit w/ waitok (proper malloc flag) should be safe.
|
|
|
|
* If kqueue_register is called from a non-fd context, there usually/should
|
|
|
|
* be no locks held.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
|
|
|
|
int waitok)
|
|
|
|
{
|
|
|
|
struct klist *list, *tmp_knhash;
|
|
|
|
u_long tmp_knhashmask;
|
|
|
|
int size;
|
|
|
|
int fd;
|
|
|
|
int mflag = waitok ? M_WAITOK : M_NOWAIT;
|
|
|
|
|
|
|
|
KQ_NOTOWNED(kq);
|
|
|
|
|
|
|
|
if (fops->f_isfd) {
|
|
|
|
fd = ident;
|
|
|
|
if (kq->kq_knlistsize <= fd) {
|
|
|
|
size = kq->kq_knlistsize;
|
|
|
|
while (size <= fd)
|
|
|
|
size += KQEXTENT;
|
|
|
|
MALLOC(list, struct klist *,
|
|
|
|
size * sizeof list, M_KQUEUE, mflag);
|
|
|
|
if (list == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (kq->kq_knlistsize > fd) {
|
|
|
|
FREE(list, M_KQUEUE);
|
|
|
|
list = NULL;
|
|
|
|
} else {
|
|
|
|
if (kq->kq_knlist != NULL) {
|
|
|
|
bcopy(kq->kq_knlist, list,
|
|
|
|
kq->kq_knlistsize * sizeof list);
|
|
|
|
FREE(kq->kq_knlist, M_KQUEUE);
|
|
|
|
kq->kq_knlist = NULL;
|
|
|
|
}
|
|
|
|
bzero((caddr_t)list +
|
|
|
|
kq->kq_knlistsize * sizeof list,
|
|
|
|
(size - kq->kq_knlistsize) * sizeof list);
|
|
|
|
kq->kq_knlistsize = size;
|
|
|
|
kq->kq_knlist = list;
|
|
|
|
}
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (kq->kq_knhashmask == 0) {
|
|
|
|
tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
|
|
|
|
&tmp_knhashmask);
|
|
|
|
if (tmp_knhash == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (kq->kq_knhashmask == 0) {
|
|
|
|
kq->kq_knhash = tmp_knhash;
|
|
|
|
kq->kq_knhashmask = tmp_knhashmask;
|
|
|
|
} else {
|
|
|
|
free(tmp_knhash, M_KQUEUE);
|
|
|
|
}
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KQ_NOTOWNED(kq);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_task(void *arg, int pending)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2002-01-13 11:58:06 +00:00
|
|
|
struct kqueue *kq;
|
2004-08-15 06:24:42 +00:00
|
|
|
int haskqglobal;
|
|
|
|
|
|
|
|
haskqglobal = 0;
|
|
|
|
kq = arg;
|
|
|
|
|
|
|
|
KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
|
|
|
|
KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
|
|
|
|
|
|
|
|
kq->kq_state &= ~KQ_TASKSCHED;
|
|
|
|
if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
|
|
|
|
wakeup(&kq->kq_state);
|
|
|
|
}
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan, update kn_data (if not ONESHOT), and copyout triggered events.
|
|
|
|
* We treat KN_MARKER knotes as if they are INFLUX.
|
|
|
|
*/
|
|
|
|
static int
|
2005-03-01 17:45:55 +00:00
|
|
|
kqueue_scan(struct kqueue *kq, int maxevents, struct kevent *eventlist,
|
|
|
|
enum uio_seg eventseg, const struct timespec *tsp, struct kevent *keva,
|
|
|
|
struct thread *td)
|
2004-08-15 06:24:42 +00:00
|
|
|
{
|
2000-04-16 19:02:08 +00:00
|
|
|
struct kevent *kevp;
|
|
|
|
struct timeval atv, rtv, ttv;
|
2004-08-16 03:08:38 +00:00
|
|
|
struct knote *kn, *marker;
|
2004-08-15 06:24:42 +00:00
|
|
|
int count, timeout, nkev, error;
|
|
|
|
int haskqglobal;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
count = maxevents;
|
2004-08-15 06:24:42 +00:00
|
|
|
nkev = 0;
|
|
|
|
error = 0;
|
|
|
|
haskqglobal = 0;
|
|
|
|
|
|
|
|
if (maxevents == 0)
|
|
|
|
goto done_nl;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2000-08-07 16:45:42 +00:00
|
|
|
if (tsp != NULL) {
|
2000-04-16 19:02:08 +00:00
|
|
|
TIMESPEC_TO_TIMEVAL(&atv, tsp);
|
2000-08-07 16:45:42 +00:00
|
|
|
if (itimerfix(&atv)) {
|
2000-04-16 19:02:08 +00:00
|
|
|
error = EINVAL;
|
2004-08-15 06:24:42 +00:00
|
|
|
goto done_nl;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2000-08-07 16:45:42 +00:00
|
|
|
if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
|
|
|
|
timeout = -1;
|
2004-08-12 18:06:21 +00:00
|
|
|
else
|
2000-08-07 16:45:42 +00:00
|
|
|
timeout = atv.tv_sec > 24 * 60 * 60 ?
|
|
|
|
24 * 60 * 60 * hz : tvtohz(&atv);
|
|
|
|
getmicrouptime(&rtv);
|
|
|
|
timevaladd(&atv, &rtv);
|
|
|
|
} else {
|
|
|
|
atv.tv_sec = 0;
|
|
|
|
atv.tv_usec = 0;
|
2000-04-16 19:02:08 +00:00
|
|
|
timeout = 0;
|
|
|
|
}
|
2004-08-16 03:08:38 +00:00
|
|
|
marker = knote_alloc(1);
|
|
|
|
if (marker == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto done_nl;
|
|
|
|
}
|
|
|
|
marker->kn_status = KN_MARKER;
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_LOCK(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
goto start;
|
|
|
|
|
|
|
|
retry:
|
2000-08-07 16:45:42 +00:00
|
|
|
if (atv.tv_sec || atv.tv_usec) {
|
2000-04-16 19:02:08 +00:00
|
|
|
getmicrouptime(&rtv);
|
|
|
|
if (timevalcmp(&rtv, &atv, >=))
|
|
|
|
goto done;
|
|
|
|
ttv = atv;
|
|
|
|
timevalsub(&ttv, &rtv);
|
|
|
|
timeout = ttv.tv_sec > 24 * 60 * 60 ?
|
|
|
|
24 * 60 * 60 * hz : tvtohz(&ttv);
|
|
|
|
}
|
|
|
|
|
|
|
|
start:
|
2004-08-15 06:24:42 +00:00
|
|
|
kevp = keva;
|
2000-04-16 19:02:08 +00:00
|
|
|
if (kq->kq_count == 0) {
|
2004-08-12 18:06:21 +00:00
|
|
|
if (timeout < 0) {
|
2000-08-07 16:45:42 +00:00
|
|
|
error = EWOULDBLOCK;
|
|
|
|
} else {
|
2004-09-14 18:38:16 +00:00
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
2000-08-07 16:45:42 +00:00
|
|
|
kq->kq_state |= KQ_SLEEP;
|
2004-08-15 06:24:42 +00:00
|
|
|
error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
|
|
|
|
"kqread", timeout);
|
2000-08-07 16:45:42 +00:00
|
|
|
}
|
2000-08-01 04:27:50 +00:00
|
|
|
if (error == 0)
|
2000-04-16 19:02:08 +00:00
|
|
|
goto retry;
|
2000-08-01 04:27:50 +00:00
|
|
|
/* don't restart after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
error = EINTR;
|
|
|
|
else if (error == EWOULDBLOCK)
|
2000-04-16 19:02:08 +00:00
|
|
|
error = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2004-08-16 03:08:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
|
2000-04-16 19:02:08 +00:00
|
|
|
while (count) {
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_OWNED(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
kn = TAILQ_FIRST(&kq->kq_head);
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2004-08-16 03:08:38 +00:00
|
|
|
if ((kn->kn_status == KN_MARKER && kn != marker) ||
|
2004-08-15 06:24:42 +00:00
|
|
|
(kn->kn_status & KN_INFLUX) == KN_INFLUX) {
|
2004-09-14 18:38:16 +00:00
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
2004-08-15 06:24:42 +00:00
|
|
|
kq->kq_state |= KQ_FLUXWAIT;
|
|
|
|
error = msleep(kq, &kq->kq_lock, PSOCK,
|
|
|
|
"kqflxwt", 0);
|
|
|
|
continue;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
|
|
|
|
if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
|
2000-04-16 19:02:08 +00:00
|
|
|
kn->kn_status &= ~KN_QUEUED;
|
|
|
|
kq->kq_count--;
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-16 03:08:38 +00:00
|
|
|
if (kn == marker) {
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_FLUX_WAKEUP(kq);
|
|
|
|
if (count == maxevents)
|
|
|
|
goto retry;
|
|
|
|
goto done;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
KASSERT((kn->kn_status & KN_INFLUX) == 0,
|
|
|
|
("KN_INFLUX set when not suppose to be"));
|
|
|
|
|
|
|
|
if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
|
2000-04-16 19:02:08 +00:00
|
|
|
kn->kn_status &= ~KN_QUEUED;
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status |= KN_INFLUX;
|
2000-04-16 19:02:08 +00:00
|
|
|
kq->kq_count--;
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
/*
|
|
|
|
* We don't need to lock the list since we've marked
|
|
|
|
* it _INFLUX.
|
|
|
|
*/
|
|
|
|
*kevp = kn->kn_kevent;
|
2004-09-06 19:02:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
kn->kn_fop->f_detach(kn);
|
2001-09-12 08:38:13 +00:00
|
|
|
knote_drop(kn, td);
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_LOCK(kq);
|
|
|
|
kn = NULL;
|
2000-04-16 19:02:08 +00:00
|
|
|
} else {
|
2004-08-15 06:24:42 +00:00
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
|
|
|
|
KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
|
|
|
|
KN_LIST_LOCK(kn);
|
|
|
|
if (kn->kn_fop->f_event(kn, 0) == 0) {
|
|
|
|
KN_LIST_UNLOCK(kn);
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
kn->kn_status &=
|
|
|
|
~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
|
|
|
|
kq->kq_count--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*kevp = kn->kn_kevent;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (kn->kn_flags & EV_CLEAR) {
|
|
|
|
kn->kn_data = 0;
|
|
|
|
kn->kn_fflags = 0;
|
|
|
|
kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
|
|
|
|
kq->kq_count--;
|
|
|
|
} else
|
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
|
|
|
|
KN_LIST_UNLOCK(kn);
|
|
|
|
kn->kn_status &= ~(KN_INFLUX);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
/* we are returning a copy to the user */
|
|
|
|
kevp++;
|
|
|
|
nkev++;
|
2000-04-16 19:02:08 +00:00
|
|
|
count--;
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2000-04-16 19:02:08 +00:00
|
|
|
if (nkev == KQ_NEVENTS) {
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_UNLOCK_FLUX(kq);
|
2005-03-01 17:45:55 +00:00
|
|
|
error = kevent_copyout(&eventlist, eventseg, keva,
|
|
|
|
nkev);
|
2000-04-16 19:02:08 +00:00
|
|
|
nkev = 0;
|
2004-08-15 06:24:42 +00:00
|
|
|
kevp = keva;
|
|
|
|
KQ_LOCK(kq);
|
2000-05-04 20:19:17 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-16 03:08:38 +00:00
|
|
|
TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
|
2000-04-16 19:02:08 +00:00
|
|
|
done:
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_OWNED(kq);
|
|
|
|
KQ_UNLOCK_FLUX(kq);
|
|
|
|
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
|
2004-08-16 03:08:38 +00:00
|
|
|
knote_free(marker);
|
2004-08-15 06:24:42 +00:00
|
|
|
done_nl:
|
|
|
|
KQ_NOTOWNED(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
if (nkev != 0)
|
2005-03-01 17:45:55 +00:00
|
|
|
error = kevent_copyout(&eventlist, eventseg, keva, nkev);
|
2004-08-15 06:24:42 +00:00
|
|
|
td->td_retval[0] = maxevents - count;
|
2000-04-16 19:02:08 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* This could be expanded to call kqueue_scan, if desired.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
In order to better support flexible and extensible access control,
make a series of modifications to the credential arguments relating
to file read and write operations to cliarfy which credential is
used for what:
- Change fo_read() and fo_write() to accept "active_cred" instead of
"cred", and change the semantics of consumers of fo_read() and
fo_write() to pass the active credential of the thread requesting
an operation rather than the cached file cred. The cached file
cred is still available in fo_read() and fo_write() consumers
via fp->f_cred. These changes largely in sys_generic.c.
For each implementation of fo_read() and fo_write(), update cred
usage to reflect this change and maintain current semantics:
- badfo_readwrite() unchanged
- kqueue_read/write() unchanged
pipe_read/write() now authorize MAC using active_cred rather
than td->td_ucred
- soo_read/write() unchanged
- vn_read/write() now authorize MAC using active_cred but
VOP_READ/WRITE() with fp->f_cred
Modify vn_rdwr() to accept two credential arguments instead of a
single credential: active_cred and file_cred. Use active_cred
for MAC authorization, and select a credential for use in
VOP_READ/WRITE() based on whether file_cred is NULL or not. If
file_cred is provided, authorize the VOP using that cred,
otherwise the active credential, matching current semantics.
Modify current vn_rdwr() consumers to pass a file_cred if used
in the context of a struct file, and to always pass active_cred.
When vn_rdwr() is used without a file_cred, pass NOCRED.
These changes should maintain current semantics for read/write,
but avoid a redundant passing of fp->f_cred, as well as making
it more clear what the origin of each credential is in file
descriptor read/write operations.
Follow-up commits will make similar changes to other file descriptor
operations, and modify the MAC framework to pass both credentials
to MAC policy modules so they can implement either semantic for
revocation.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-15 20:55:08 +00:00
|
|
|
kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
|
2001-09-12 08:38:13 +00:00
|
|
|
int flags, struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
In order to better support flexible and extensible access control,
make a series of modifications to the credential arguments relating
to file read and write operations to cliarfy which credential is
used for what:
- Change fo_read() and fo_write() to accept "active_cred" instead of
"cred", and change the semantics of consumers of fo_read() and
fo_write() to pass the active credential of the thread requesting
an operation rather than the cached file cred. The cached file
cred is still available in fo_read() and fo_write() consumers
via fp->f_cred. These changes largely in sys_generic.c.
For each implementation of fo_read() and fo_write(), update cred
usage to reflect this change and maintain current semantics:
- badfo_readwrite() unchanged
- kqueue_read/write() unchanged
pipe_read/write() now authorize MAC using active_cred rather
than td->td_ucred
- soo_read/write() unchanged
- vn_read/write() now authorize MAC using active_cred but
VOP_READ/WRITE() with fp->f_cred
Modify vn_rdwr() to accept two credential arguments instead of a
single credential: active_cred and file_cred. Use active_cred
for MAC authorization, and select a credential for use in
VOP_READ/WRITE() based on whether file_cred is NULL or not. If
file_cred is provided, authorize the VOP using that cred,
otherwise the active credential, matching current semantics.
Modify current vn_rdwr() consumers to pass a file_cred if used
in the context of a struct file, and to always pass active_cred.
When vn_rdwr() is used without a file_cred, pass NOCRED.
These changes should maintain current semantics for read/write,
but avoid a redundant passing of fp->f_cred, as well as making
it more clear what the origin of each credential is in file
descriptor read/write operations.
Follow-up commits will make similar changes to other file descriptor
operations, and modify the MAC framework to pass both credentials
to MAC policy modules so they can implement either semantic for
revocation.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-15 20:55:08 +00:00
|
|
|
kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
|
2001-09-12 08:38:13 +00:00
|
|
|
int flags, struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
2004-07-14 07:02:03 +00:00
|
|
|
kqueue_ioctl(struct file *fp, u_long cmd, void *data,
|
2002-08-17 02:36:16 +00:00
|
|
|
struct ucred *active_cred, struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-07-15 03:49:52 +00:00
|
|
|
/*
|
|
|
|
* Enabling sigio causes two major problems:
|
|
|
|
* 1) infinite recursion:
|
|
|
|
* Synopsys: kevent is being used to track signals and have FIOASYNC
|
|
|
|
* set. On receipt of a signal this will cause a kqueue to recurse
|
|
|
|
* into itself over and over. Sending the sigio causes the kqueue
|
|
|
|
* to become ready, which in turn posts sigio again, forever.
|
|
|
|
* Solution: this can be solved by setting a flag in the kqueue that
|
|
|
|
* we have a SIGIO in progress.
|
|
|
|
* 2) locking problems:
|
|
|
|
* Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
|
|
|
|
* us above the proc and pgrp locks.
|
|
|
|
* Solution: Post a signal using an async mechanism, being sure to
|
|
|
|
* record a generation count in the delivery so that we do not deliver
|
|
|
|
* a signal to the wrong process.
|
|
|
|
*
|
|
|
|
* Note, these two mechanisms are somewhat mutually exclusive!
|
|
|
|
*/
|
|
|
|
#if 0
|
2004-07-14 07:02:03 +00:00
|
|
|
struct kqueue *kq;
|
|
|
|
|
|
|
|
kq = fp->f_data;
|
|
|
|
switch (cmd) {
|
|
|
|
case FIOASYNC:
|
|
|
|
if (*(int *)data) {
|
|
|
|
kq->kq_state |= KQ_ASYNC;
|
|
|
|
} else {
|
|
|
|
kq->kq_state &= ~KQ_ASYNC;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case FIOSETOWN:
|
|
|
|
return (fsetown(*(int *)data, &kq->kq_sigio));
|
|
|
|
|
|
|
|
case FIOGETOWN:
|
|
|
|
*(int *)data = fgetown(&kq->kq_sigio);
|
|
|
|
return (0);
|
|
|
|
}
|
2004-07-15 03:49:52 +00:00
|
|
|
#endif
|
2004-07-14 07:02:03 +00:00
|
|
|
|
2000-04-16 19:02:08 +00:00
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
Make similar changes to fo_stat() and fo_poll() as made earlier to
fo_read() and fo_write(): explicitly use the cred argument to fo_poll()
as "active_cred" using the passed file descriptor's f_cred reference
to provide access to the file credential. Add an active_cred
argument to fo_stat() so that implementers have access to the active
credential as well as the file credential. Generally modify callers
of fo_stat() to pass in td->td_ucred rather than fp->f_cred, which
was redundantly provided via the fp argument. This set of modifications
also permits threads to perform these operations on behalf of another
thread without modifying their credential.
Trickle this change down into fo_stat/poll() implementations:
- badfo_poll(), badfo_stat(): modify/add arguments.
- kqueue_poll(), kqueue_stat(): modify arguments.
- pipe_poll(), pipe_stat(): modify/add arguments, pass active_cred to
MAC checks rather than td->td_ucred.
- soo_poll(), soo_stat(): modify/add arguments, pass fp->f_cred rather
than cred to pru_sopoll() to maintain current semantics.
- sopoll(): moidfy arguments.
- vn_poll(), vn_statfile(): modify/add arguments, pass new arguments
to vn_stat(). Pass active_cred to MAC and fp->f_cred to VOP_POLL()
to maintian current semantics.
- vn_close(): rename cred to file_cred to reflect reality while I'm here.
- vn_stat(): Add active_cred and file_cred arguments to vn_stat()
and consumers so that this distinction is maintained at the VFS
as well as 'struct file' layer. Pass active_cred instead of
td->td_ucred to MAC and to VOP_GETATTR() to maintain current semantics.
- fifofs: modify the creation of a "filetemp" so that the file
credential is properly initialized and can be used in the socket
code if desired. Pass ap->a_td->td_ucred as the active
credential to soo_poll(). If we teach the vnop interface about
the distinction between file and active credentials, we would use
the active credential here.
Note that current inconsistent passing of active_cred vs. file_cred to
VOP's is maintained. It's not clear why GETATTR would be authorized
using active_cred while POLL would be authorized using file_cred at
the file system level.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-16 12:52:03 +00:00
|
|
|
kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
|
2002-08-16 14:12:40 +00:00
|
|
|
struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2002-01-13 11:58:06 +00:00
|
|
|
struct kqueue *kq;
|
2000-04-16 19:02:08 +00:00
|
|
|
int revents = 0;
|
2004-08-15 06:24:42 +00:00
|
|
|
int error;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((error = kqueue_aquire(fp, &kq)))
|
|
|
|
return POLLERR;
|
|
|
|
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
if (kq->kq_count) {
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
2000-04-16 19:02:08 +00:00
|
|
|
} else {
|
2004-08-15 06:24:42 +00:00
|
|
|
selrecord(td, &kq->kq_sel);
|
2000-04-16 19:02:08 +00:00
|
|
|
kq->kq_state |= KQ_SEL;
|
|
|
|
}
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
kqueue_release(kq, 1);
|
|
|
|
KQ_UNLOCK(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
return (revents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
Make similar changes to fo_stat() and fo_poll() as made earlier to
fo_read() and fo_write(): explicitly use the cred argument to fo_poll()
as "active_cred" using the passed file descriptor's f_cred reference
to provide access to the file credential. Add an active_cred
argument to fo_stat() so that implementers have access to the active
credential as well as the file credential. Generally modify callers
of fo_stat() to pass in td->td_ucred rather than fp->f_cred, which
was redundantly provided via the fp argument. This set of modifications
also permits threads to perform these operations on behalf of another
thread without modifying their credential.
Trickle this change down into fo_stat/poll() implementations:
- badfo_poll(), badfo_stat(): modify/add arguments.
- kqueue_poll(), kqueue_stat(): modify arguments.
- pipe_poll(), pipe_stat(): modify/add arguments, pass active_cred to
MAC checks rather than td->td_ucred.
- soo_poll(), soo_stat(): modify/add arguments, pass fp->f_cred rather
than cred to pru_sopoll() to maintain current semantics.
- sopoll(): moidfy arguments.
- vn_poll(), vn_statfile(): modify/add arguments, pass new arguments
to vn_stat(). Pass active_cred to MAC and fp->f_cred to VOP_POLL()
to maintian current semantics.
- vn_close(): rename cred to file_cred to reflect reality while I'm here.
- vn_stat(): Add active_cred and file_cred arguments to vn_stat()
and consumers so that this distinction is maintained at the VFS
as well as 'struct file' layer. Pass active_cred instead of
td->td_ucred to MAC and to VOP_GETATTR() to maintain current semantics.
- fifofs: modify the creation of a "filetemp" so that the file
credential is properly initialized and can be used in the socket
code if desired. Pass ap->a_td->td_ucred as the active
credential to soo_poll(). If we teach the vnop interface about
the distinction between file and active credentials, we would use
the active credential here.
Note that current inconsistent passing of active_cred vs. file_cred to
VOP's is maintained. It's not clear why GETATTR would be authorized
using active_cred while POLL would be authorized using file_cred at
the file system level.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-16 12:52:03 +00:00
|
|
|
kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
|
2002-08-16 14:12:40 +00:00
|
|
|
struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
return (ENXIO);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
kqueue_close(struct file *fp, struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct kqueue *kq = fp->f_data;
|
2004-08-15 06:24:42 +00:00
|
|
|
struct filedesc *fdp;
|
|
|
|
struct knote *kn;
|
2000-04-16 19:02:08 +00:00
|
|
|
int i;
|
2004-08-15 06:24:42 +00:00
|
|
|
int error;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((error = kqueue_aquire(fp, &kq)))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
|
|
|
|
KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
|
|
|
|
("kqueue already closing"));
|
|
|
|
kq->kq_state |= KQ_CLOSING;
|
|
|
|
if (kq->kq_refcnt > 1)
|
|
|
|
msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
|
|
|
|
|
|
|
|
KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
|
|
|
|
fdp = kq->kq_fdp;
|
|
|
|
|
|
|
|
KASSERT(knlist_empty(&kq->kq_sel.si_note),
|
|
|
|
("kqueue's knlist not empty"));
|
|
|
|
|
|
|
|
for (i = 0; i < kq->kq_knlistsize; i++) {
|
|
|
|
while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
|
|
|
|
KASSERT((kn->kn_status & KN_INFLUX) == 0,
|
|
|
|
("KN_INFLUX set when not suppose to be"));
|
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
2004-09-06 19:02:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
kn->kn_fop->f_detach(kn);
|
2004-08-15 06:24:42 +00:00
|
|
|
knote_drop(kn, td);
|
|
|
|
KQ_LOCK(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
if (kq->kq_knhashmask != 0) {
|
|
|
|
for (i = 0; i <= kq->kq_knhashmask; i++) {
|
|
|
|
while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
|
|
|
|
KASSERT((kn->kn_status & KN_INFLUX) == 0,
|
|
|
|
("KN_INFLUX set when not suppose to be"));
|
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
2004-09-06 19:02:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
kn->kn_fop->f_detach(kn);
|
2004-08-15 06:24:42 +00:00
|
|
|
knote_drop(kn, td);
|
|
|
|
KQ_LOCK(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
|
|
|
|
kq->kq_state |= KQ_TASKDRAIN;
|
|
|
|
msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
|
2004-02-20 04:00:48 +00:00
|
|
|
kq->kq_state &= ~KQ_SEL;
|
|
|
|
selwakeuppri(&kq->kq_sel, PSOCK);
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
|
2004-11-13 11:53:02 +00:00
|
|
|
FILEDESC_LOCK_FAST(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
|
2004-11-13 11:53:02 +00:00
|
|
|
FILEDESC_UNLOCK_FAST(fdp);
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
knlist_destroy(&kq->kq_sel.si_note);
|
|
|
|
mtx_destroy(&kq->kq_lock);
|
|
|
|
kq->kq_fdp = NULL;
|
|
|
|
|
|
|
|
if (kq->kq_knhash != NULL)
|
|
|
|
free(kq->kq_knhash, M_KQUEUE);
|
|
|
|
if (kq->kq_knlist != NULL)
|
|
|
|
free(kq->kq_knlist, M_KQUEUE);
|
|
|
|
|
2004-07-14 07:02:03 +00:00
|
|
|
funsetown(&kq->kq_sigio);
|
2001-09-29 17:48:39 +00:00
|
|
|
free(kq, M_KQUEUE);
|
2003-01-13 00:33:17 +00:00
|
|
|
fp->f_data = NULL;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_wakeup(struct kqueue *kq)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_OWNED(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
|
2000-04-16 19:02:08 +00:00
|
|
|
kq->kq_state &= ~KQ_SLEEP;
|
|
|
|
wakeup(kq);
|
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
|
2000-04-16 19:02:08 +00:00
|
|
|
kq->kq_state &= ~KQ_SEL;
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&kq->kq_sel, PSOCK);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
2004-08-15 06:24:42 +00:00
|
|
|
if (!knlist_empty(&kq->kq_sel.si_note))
|
|
|
|
kqueue_schedtask(kq);
|
|
|
|
if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
|
2004-07-14 07:02:03 +00:00
|
|
|
pgsigio(&kq->kq_sigio, SIGIO, 0);
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-15 06:24:42 +00:00
|
|
|
* Walk down a list of knotes, activating them if their event has triggered.
|
|
|
|
*
|
|
|
|
* There is a possibility to optimize in the case of one kq watching another.
|
|
|
|
* Instead of scheduling a task to wake it up, you could pass enough state
|
|
|
|
* down the chain to make up the parent kqueue. Make this code functional
|
|
|
|
* first.
|
2000-04-16 19:02:08 +00:00
|
|
|
*/
|
|
|
|
void
|
2004-08-15 06:24:42 +00:00
|
|
|
knote(struct knlist *list, long hint, int islocked)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct kqueue *kq;
|
2000-04-16 19:02:08 +00:00
|
|
|
struct knote *kn;
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if (list == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtx_assert(list->kl_lock, islocked ? MA_OWNED : MA_NOTOWNED);
|
|
|
|
if (!islocked)
|
|
|
|
mtx_lock(list->kl_lock);
|
|
|
|
/*
|
|
|
|
* If we unlock the list lock (and set KN_INFLUX), we can eliminate
|
|
|
|
* the kqueue scheduling, but this will introduce four
|
|
|
|
* lock/unlock's for each knote to test. If we do, continue to use
|
|
|
|
* SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
|
|
|
|
* only safe if you want to remove the current item, which we are
|
|
|
|
* not doing.
|
|
|
|
*/
|
|
|
|
SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
|
|
|
|
kq = kn->kn_kq;
|
|
|
|
if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
|
|
|
|
kn->kn_status |= KN_HASKQLOCK;
|
|
|
|
if (kn->kn_fop->f_event(kn, hint))
|
|
|
|
KNOTE_ACTIVATE(kn, 1);
|
|
|
|
kn->kn_status &= ~KN_HASKQLOCK;
|
|
|
|
}
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
}
|
|
|
|
kq = NULL;
|
|
|
|
}
|
|
|
|
if (!islocked)
|
|
|
|
mtx_unlock(list->kl_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add a knote to a knlist
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
knlist_add(struct knlist *knl, struct knote *kn, int islocked)
|
|
|
|
{
|
|
|
|
mtx_assert(knl->kl_lock, islocked ? MA_OWNED : MA_NOTOWNED);
|
|
|
|
KQ_NOTOWNED(kn->kn_kq);
|
|
|
|
KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
|
|
|
|
(KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
|
|
|
|
if (!islocked)
|
|
|
|
mtx_lock(knl->kl_lock);
|
|
|
|
SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
|
|
|
|
if (!islocked)
|
|
|
|
mtx_unlock(knl->kl_lock);
|
|
|
|
KQ_LOCK(kn->kn_kq);
|
|
|
|
kn->kn_knlist = knl;
|
|
|
|
kn->kn_status &= ~KN_DETACHED;
|
|
|
|
KQ_UNLOCK(kn->kn_kq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
|
|
|
|
{
|
|
|
|
KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
|
|
|
|
mtx_assert(knl->kl_lock, knlislocked ? MA_OWNED : MA_NOTOWNED);
|
|
|
|
mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
|
|
|
|
if (!kqislocked)
|
|
|
|
KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
|
|
|
|
("knlist_remove called w/o knote being KN_INFLUX or already removed"));
|
|
|
|
if (!knlislocked)
|
|
|
|
mtx_lock(knl->kl_lock);
|
|
|
|
SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
|
|
|
|
kn->kn_knlist = NULL;
|
|
|
|
if (!knlislocked)
|
|
|
|
mtx_unlock(knl->kl_lock);
|
|
|
|
if (!kqislocked)
|
|
|
|
KQ_LOCK(kn->kn_kq);
|
|
|
|
kn->kn_status |= KN_DETACHED;
|
|
|
|
if (!kqislocked)
|
|
|
|
KQ_UNLOCK(kn->kn_kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove all knotes from a specified klist
|
|
|
|
*/
|
|
|
|
void
|
2004-08-15 06:24:42 +00:00
|
|
|
knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
|
|
|
|
{
|
|
|
|
|
|
|
|
knlist_remove_kq(knl, kn, islocked, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove knote from a specified klist while in f_event handler.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
knlist_remove_inevent(struct knlist *knl, struct knote *kn)
|
|
|
|
{
|
|
|
|
|
|
|
|
knlist_remove_kq(knl, kn, 1,
|
|
|
|
(kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
knlist_empty(struct knlist *knl)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_assert(knl->kl_lock, MA_OWNED);
|
|
|
|
return SLIST_EMPTY(&knl->kl_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mtx knlist_lock;
|
|
|
|
MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
|
|
|
|
MTX_DEF);
|
|
|
|
|
|
|
|
void
|
|
|
|
knlist_init(struct knlist *knl, struct mtx *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mtx == NULL)
|
|
|
|
knl->kl_lock = &knlist_lock;
|
|
|
|
else
|
|
|
|
knl->kl_lock = mtx;
|
|
|
|
|
|
|
|
SLIST_INIT(&knl->kl_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
knlist_destroy(struct knlist *knl)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
/*
|
|
|
|
* if we run across this error, we need to find the offending
|
|
|
|
* driver and have it call knlist_clear.
|
|
|
|
*/
|
|
|
|
if (!SLIST_EMPTY(&knl->kl_list))
|
|
|
|
printf("WARNING: destroying knlist w/ knotes on it!\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
knl->kl_lock = NULL;
|
|
|
|
SLIST_INIT(&knl->kl_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Even if we are locked, we may need to drop the lock to allow any influx
|
|
|
|
* knotes time to "settle".
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
knlist_clear(struct knlist *knl, int islocked)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
|
|
|
struct knote *kn;
|
2004-08-15 06:24:42 +00:00
|
|
|
struct kqueue *kq;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if (islocked)
|
|
|
|
mtx_assert(knl->kl_lock, MA_OWNED);
|
|
|
|
else {
|
|
|
|
mtx_assert(knl->kl_lock, MA_NOTOWNED);
|
|
|
|
again: /* need to reaquire lock since we have dropped it */
|
|
|
|
mtx_lock(knl->kl_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(kn, &knl->kl_list, kn_selnext) {
|
|
|
|
kq = kn->kn_kq;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
if ((kn->kn_status & KN_INFLUX) &&
|
|
|
|
(kn->kn_status & KN_DETACHED) != KN_DETACHED) {
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Make sure cleared knotes disappear soon */
|
|
|
|
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
|
|
|
|
knlist_remove_kq(knl, kn, 1, 1);
|
|
|
|
KQ_UNLOCK(kq);
|
|
|
|
kq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SLIST_EMPTY(&knl->kl_list)) {
|
|
|
|
/* there are still KN_INFLUX remaining */
|
|
|
|
kn = SLIST_FIRST(&knl->kl_list);
|
|
|
|
kq = kn->kn_kq;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
KASSERT(kn->kn_status & KN_INFLUX,
|
|
|
|
("knote removed w/o list lock"));
|
|
|
|
mtx_unlock(knl->kl_lock);
|
|
|
|
kq->kq_state |= KQ_FLUXWAIT;
|
|
|
|
msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
|
|
|
|
kq = NULL;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_INIT(&knl->kl_list);
|
|
|
|
|
|
|
|
if (islocked)
|
|
|
|
mtx_assert(knl->kl_lock, MA_OWNED);
|
|
|
|
else {
|
|
|
|
mtx_unlock(knl->kl_lock);
|
|
|
|
mtx_assert(knl->kl_lock, MA_NOTOWNED);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove all knotes referencing a specified fd
|
2004-08-15 06:24:42 +00:00
|
|
|
* must be called with FILEDESC lock. This prevents a race where a new fd
|
|
|
|
* comes along and occupies the entry and we attach a knote to the fd.
|
2000-04-16 19:02:08 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
knote_fdclose(struct thread *td, int fd)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct filedesc *fdp = td->td_proc->p_fd;
|
2004-08-15 06:24:42 +00:00
|
|
|
struct kqueue *kq;
|
|
|
|
struct knote *kn;
|
|
|
|
int influx;
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We shouldn't have to worry about new kevents appearing on fd
|
|
|
|
* since filedesc is locked.
|
|
|
|
*/
|
|
|
|
SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
|
|
|
|
again:
|
|
|
|
influx = 0;
|
|
|
|
while (kq->kq_knlistsize > fd &&
|
|
|
|
(kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
|
|
|
|
if (kn->kn_status & KN_INFLUX) {
|
|
|
|
/* someone else might be waiting on our knote */
|
|
|
|
if (influx)
|
|
|
|
wakeup(kq);
|
|
|
|
kq->kq_state |= KQ_FLUXWAIT;
|
|
|
|
msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
kn->kn_status |= KN_INFLUX;
|
|
|
|
KQ_UNLOCK(kq);
|
2004-09-06 19:02:42 +00:00
|
|
|
if (!(kn->kn_status & KN_DETACHED))
|
|
|
|
kn->kn_fop->f_detach(kn);
|
2004-08-15 06:24:42 +00:00
|
|
|
knote_drop(kn, td);
|
|
|
|
influx = 1;
|
|
|
|
KQ_LOCK(kq);
|
|
|
|
}
|
|
|
|
KQ_UNLOCK_FLUX(kq);
|
|
|
|
}
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
static int
|
|
|
|
knote_attach(struct knote *kn, struct kqueue *kq)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct klist *list;
|
2002-01-13 11:58:06 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
|
|
|
|
KQ_OWNED(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
if (kn->kn_fop->f_isfd) {
|
|
|
|
if (kn->kn_id >= kq->kq_knlistsize)
|
|
|
|
return ENOMEM;
|
|
|
|
list = &kq->kq_knlist[kn->kn_id];
|
|
|
|
} else {
|
|
|
|
if (kq->kq_knhash == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_INSERT_HEAD(list, kn, kn_link);
|
2004-08-15 06:24:42 +00:00
|
|
|
|
|
|
|
return 0;
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-15 06:24:42 +00:00
|
|
|
* knote must already have been detatched using the f_detach method.
|
|
|
|
* no lock need to be held, it is assumed that the KN_INFLUX flag is set
|
|
|
|
* to prevent other removal.
|
2000-04-16 19:02:08 +00:00
|
|
|
*/
|
|
|
|
static void
|
2001-09-12 08:38:13 +00:00
|
|
|
knote_drop(struct knote *kn, struct thread *td)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
struct kqueue *kq;
|
2000-04-16 19:02:08 +00:00
|
|
|
struct klist *list;
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
kq = kn->kn_kq;
|
|
|
|
|
|
|
|
KQ_NOTOWNED(kq);
|
|
|
|
KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
|
|
|
|
("knote_drop called without KN_INFLUX set in kn_status"));
|
|
|
|
|
|
|
|
KQ_LOCK(kq);
|
2000-04-16 19:02:08 +00:00
|
|
|
if (kn->kn_fop->f_isfd)
|
2004-08-15 06:24:42 +00:00
|
|
|
list = &kq->kq_knlist[kn->kn_id];
|
2000-04-16 19:02:08 +00:00
|
|
|
else
|
2004-08-15 06:24:42 +00:00
|
|
|
list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
|
2000-04-16 19:02:08 +00:00
|
|
|
|
2000-05-26 02:09:24 +00:00
|
|
|
SLIST_REMOVE(list, kn, knote, kn_link);
|
2000-04-16 19:02:08 +00:00
|
|
|
if (kn->kn_status & KN_QUEUED)
|
|
|
|
knote_dequeue(kn);
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_UNLOCK_FLUX(kq);
|
|
|
|
|
|
|
|
if (kn->kn_fop->f_isfd) {
|
|
|
|
fdrop(kn->kn_fp, td);
|
|
|
|
kn->kn_fp = NULL;
|
|
|
|
}
|
|
|
|
kqueue_fo_release(kn->kn_kevent.filter);
|
|
|
|
kn->kn_fop = NULL;
|
2000-04-16 19:02:08 +00:00
|
|
|
knote_free(kn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
knote_enqueue(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct kqueue *kq = kn->kn_kq;
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_OWNED(kn->kn_kq);
|
2000-05-04 20:19:17 +00:00
|
|
|
KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
|
|
|
|
|
2004-08-12 18:06:21 +00:00
|
|
|
TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
|
2000-04-16 19:02:08 +00:00
|
|
|
kn->kn_status |= KN_QUEUED;
|
|
|
|
kq->kq_count++;
|
|
|
|
kqueue_wakeup(kq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
knote_dequeue(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct kqueue *kq = kn->kn_kq;
|
|
|
|
|
2004-08-15 06:24:42 +00:00
|
|
|
KQ_OWNED(kn->kn_kq);
|
2000-05-04 20:19:17 +00:00
|
|
|
KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
|
|
|
|
|
2004-08-12 18:06:21 +00:00
|
|
|
TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
|
2000-04-16 19:02:08 +00:00
|
|
|
kn->kn_status &= ~KN_QUEUED;
|
|
|
|
kq->kq_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
knote_init(void)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
|
2002-03-20 04:09:59 +00:00
|
|
|
knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
|
|
|
|
NULL, NULL, UMA_ALIGN_PTR, 0);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
|
|
|
|
|
|
|
|
static struct knote *
|
2004-08-15 06:24:42 +00:00
|
|
|
knote_alloc(int waitok)
|
2000-04-16 19:02:08 +00:00
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
return ((struct knote *)uma_zalloc(knote_zone,
|
|
|
|
(waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
knote_free(struct knote *kn)
|
|
|
|
{
|
2004-08-15 06:24:42 +00:00
|
|
|
if (kn != NULL)
|
|
|
|
uma_zfree(knote_zone, kn);
|
2000-04-16 19:02:08 +00:00
|
|
|
}
|