The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_event.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: kern_event.c,v 1.19 2004/02/14 11:56:28 jdolecek Exp $ */
    2 /*-
    3  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp $
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.19 2004/02/14 11:56:28 jdolecek Exp $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/kernel.h>
   36 #include <sys/proc.h>
   37 #include <sys/malloc.h> 
   38 #include <sys/unistd.h>
   39 #include <sys/file.h>
   40 #include <sys/fcntl.h>
   41 #include <sys/select.h>
   42 #include <sys/queue.h>
   43 #include <sys/event.h>
   44 #include <sys/eventvar.h>
   45 #include <sys/poll.h>
   46 #include <sys/pool.h>
   47 #include <sys/protosw.h>
   48 #include <sys/socket.h>
   49 #include <sys/socketvar.h>
   50 #include <sys/stat.h>
   51 #include <sys/uio.h>
   52 #include <sys/mount.h>
   53 #include <sys/filedesc.h>
   54 #include <sys/sa.h>
   55 #include <sys/syscallargs.h>
   56 
   57 static int      kqueue_scan(struct file *fp, size_t maxevents,
   58                     struct kevent *ulistp, const struct timespec *timeout,
   59                     struct proc *p, register_t *retval);
   60 static void     kqueue_wakeup(struct kqueue *kq);
   61 
   62 static int      kqueue_read(struct file *fp, off_t *offset, struct uio *uio,
   63                     struct ucred *cred, int flags);
   64 static int      kqueue_write(struct file *fp, off_t *offset, struct uio *uio,
   65                     struct ucred *cred, int flags);
   66 static int      kqueue_ioctl(struct file *fp, u_long com, void *data,
   67                     struct proc *p);
   68 static int      kqueue_fcntl(struct file *fp, u_int com, void *data,
   69                     struct proc *p);
   70 static int      kqueue_poll(struct file *fp, int events, struct proc *p);
   71 static int      kqueue_kqfilter(struct file *fp, struct knote *kn);
   72 static int      kqueue_stat(struct file *fp, struct stat *sp, struct proc *p);
   73 static int      kqueue_close(struct file *fp, struct proc *p);
   74 
   75 static struct fileops kqueueops = {
   76         kqueue_read, kqueue_write, kqueue_ioctl, kqueue_fcntl, kqueue_poll,
   77         kqueue_stat, kqueue_close, kqueue_kqfilter
   78 };
   79 
   80 static void     knote_attach(struct knote *kn, struct filedesc *fdp);
   81 static void     knote_drop(struct knote *kn, struct proc *p,
   82                     struct filedesc *fdp);
   83 static void     knote_enqueue(struct knote *kn);
   84 static void     knote_dequeue(struct knote *kn);
   85 
   86 static void     filt_kqdetach(struct knote *kn);
   87 static int      filt_kqueue(struct knote *kn, long hint);
   88 static int      filt_procattach(struct knote *kn);
   89 static void     filt_procdetach(struct knote *kn);
   90 static int      filt_proc(struct knote *kn, long hint);
   91 static int      filt_fileattach(struct knote *kn);
   92 static void     filt_timerexpire(void *knx);
   93 static int      filt_timerattach(struct knote *kn);
   94 static void     filt_timerdetach(struct knote *kn);
   95 static int      filt_timer(struct knote *kn, long hint);
   96 
   97 static const struct filterops kqread_filtops =
   98         { 1, NULL, filt_kqdetach, filt_kqueue };
   99 static const struct filterops proc_filtops =
  100         { 0, filt_procattach, filt_procdetach, filt_proc };
  101 static const struct filterops file_filtops =
  102         { 1, filt_fileattach, NULL, NULL };
  103 static struct filterops timer_filtops =
  104         { 0, filt_timerattach, filt_timerdetach, filt_timer };
  105 
  106 struct pool     kqueue_pool;
  107 struct pool     knote_pool;
  108 static int      kq_ncallouts = 0;
  109 static int      kq_calloutmax = (4 * 1024);
  110 
  111 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
  112 
  113 #define KNOTE_ACTIVATE(kn)                                              \
  114 do {                                                                    \
  115         kn->kn_status |= KN_ACTIVE;                                     \
  116         if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)           \
  117                 knote_enqueue(kn);                                      \
  118 } while(0)
  119 
  120 #define KN_HASHSIZE             64              /* XXX should be tunable */
  121 #define KN_HASH(val, mask)      (((val) ^ (val >> 8)) & (mask))
  122 
  123 extern const struct filterops sig_filtops;
  124 
  125 /*
  126  * Table for for all system-defined filters.
  127  * These should be listed in the numeric order of the EVFILT_* defines.
  128  * If filtops is NULL, the filter isn't implemented in NetBSD.
  129  * End of list is when name is NULL.
  130  */
  131 struct kfilter {
  132         const char       *name;         /* name of filter */
  133         uint32_t          filter;       /* id of filter */
  134         const struct filterops *filtops;/* operations for filter */
  135 };
  136 
  137                 /* System defined filters */
  138 static const struct kfilter sys_kfilters[] = {
  139         { "EVFILT_READ",        EVFILT_READ,    &file_filtops },
  140         { "EVFILT_WRITE",       EVFILT_WRITE,   &file_filtops },
  141         { "EVFILT_AIO",         EVFILT_AIO,     NULL },
  142         { "EVFILT_VNODE",       EVFILT_VNODE,   &file_filtops },
  143         { "EVFILT_PROC",        EVFILT_PROC,    &proc_filtops },
  144         { "EVFILT_SIGNAL",      EVFILT_SIGNAL,  &sig_filtops },
  145         { "EVFILT_TIMER",       EVFILT_TIMER,   &timer_filtops },
  146         { NULL,                 0,              NULL }, /* end of list */ 
  147 };
  148 
  149                 /* User defined kfilters */
  150 static struct kfilter   *user_kfilters;         /* array */
  151 static int              user_kfilterc;          /* current offset */
  152 static int              user_kfiltermaxc;       /* max size so far */
  153 
  154 /*
  155  * kqueue_init:
  156  *
  157  *      Initialize the kqueue/knote facility.
  158  */
  159 void
  160 kqueue_init(void)
  161 {
  162 
  163         pool_init(&kqueue_pool, sizeof(struct kqueue), 0, 0, 0, "kqueuepl",
  164             NULL);
  165         pool_init(&knote_pool, sizeof(struct knote), 0, 0, 0, "knotepl",
  166             NULL);
  167 }
  168 
  169 /*
  170  * Find kfilter entry by name, or NULL if not found.
  171  */
  172 static const struct kfilter *
  173 kfilter_byname_sys(const char *name)
  174 {
  175         int i;
  176 
  177         for (i = 0; sys_kfilters[i].name != NULL; i++) {
  178                 if (strcmp(name, sys_kfilters[i].name) == 0)
  179                         return (&sys_kfilters[i]);
  180         }
  181         return (NULL);
  182 }
  183 
  184 static struct kfilter *
  185 kfilter_byname_user(const char *name)
  186 {
  187         int i;
  188 
  189         /* user_kfilters[] could be NULL if no filters were registered */
  190         if (!user_kfilters)
  191                 return (NULL);
  192 
  193         for (i = 0; user_kfilters[i].name != NULL; i++) {
  194                 if (user_kfilters[i].name != '\0' &&
  195                     strcmp(name, user_kfilters[i].name) == 0)
  196                         return (&user_kfilters[i]);
  197         }
  198         return (NULL);
  199 }
  200 
  201 static const struct kfilter *
  202 kfilter_byname(const char *name)
  203 {
  204         const struct kfilter *kfilter;
  205 
  206         if ((kfilter = kfilter_byname_sys(name)) != NULL)
  207                 return (kfilter);
  208 
  209         return (kfilter_byname_user(name));
  210 }
  211 
  212 /*
  213  * Find kfilter entry by filter id, or NULL if not found.
  214  * Assumes entries are indexed in filter id order, for speed.
  215  */
  216 static const struct kfilter *
  217 kfilter_byfilter(uint32_t filter)
  218 {
  219         const struct kfilter *kfilter;
  220 
  221         if (filter < EVFILT_SYSCOUNT)   /* it's a system filter */
  222                 kfilter = &sys_kfilters[filter];
  223         else if (user_kfilters != NULL &&
  224             filter < EVFILT_SYSCOUNT + user_kfilterc)
  225                                         /* it's a user filter */
  226                 kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
  227         else
  228                 return (NULL);          /* out of range */
  229         KASSERT(kfilter->filter == filter);     /* sanity check! */
  230         return (kfilter);
  231 }
  232 
  233 /*
  234  * Register a new kfilter. Stores the entry in user_kfilters.
  235  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
  236  * If retfilter != NULL, the new filterid is returned in it.
  237  */
  238 int
  239 kfilter_register(const char *name, const struct filterops *filtops,
  240     int *retfilter)
  241 {
  242         struct kfilter *kfilter;
  243         void *space;
  244         int len;
  245 
  246         if (name == NULL || name[0] == '\0' || filtops == NULL)
  247                 return (EINVAL);        /* invalid args */
  248         if (kfilter_byname(name) != NULL)
  249                 return (EEXIST);        /* already exists */
  250         if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT)
  251                 return (EINVAL);        /* too many */
  252 
  253         /* check if need to grow user_kfilters */
  254         if (user_kfilterc + 1 > user_kfiltermaxc) {
  255                 /*
  256                  * Grow in KFILTER_EXTENT chunks. Use malloc(9), because we
  257                  * want to traverse user_kfilters as an array.
  258                  */
  259                 user_kfiltermaxc += KFILTER_EXTENT;
  260                 kfilter = malloc(user_kfiltermaxc * sizeof(struct filter *),
  261                     M_KEVENT, M_WAITOK);
  262 
  263                 /* copy existing user_kfilters */
  264                 if (user_kfilters != NULL)
  265                         memcpy((caddr_t)kfilter, (caddr_t)user_kfilters,
  266                             user_kfilterc * sizeof(struct kfilter *));
  267                                         /* zero new sections */
  268                 memset((caddr_t)kfilter +
  269                     user_kfilterc * sizeof(struct kfilter *), 0,
  270                     (user_kfiltermaxc - user_kfilterc) *
  271                     sizeof(struct kfilter *));
  272                                         /* switch to new kfilter */
  273                 if (user_kfilters != NULL)
  274                         free(user_kfilters, M_KEVENT);
  275                 user_kfilters = kfilter;
  276         }
  277         len = strlen(name) + 1;         /* copy name */
  278         space = malloc(len, M_KEVENT, M_WAITOK);
  279         memcpy(space, name, len);
  280         user_kfilters[user_kfilterc].name = space;
  281 
  282         user_kfilters[user_kfilterc].filter = user_kfilterc + EVFILT_SYSCOUNT;
  283 
  284         len = sizeof(struct filterops); /* copy filtops */
  285         space = malloc(len, M_KEVENT, M_WAITOK);
  286         memcpy(space, filtops, len);
  287         user_kfilters[user_kfilterc].filtops = space;
  288 
  289         if (retfilter != NULL)
  290                 *retfilter = user_kfilters[user_kfilterc].filter;
  291         user_kfilterc++;                /* finally, increment count */
  292         return (0);
  293 }
  294 
  295 /*
  296  * Unregister a kfilter previously registered with kfilter_register.
  297  * This retains the filter id, but clears the name and frees filtops (filter
  298  * operations), so that the number isn't reused during a boot.
  299  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
  300  */
  301 int
  302 kfilter_unregister(const char *name)
  303 {
  304         struct kfilter *kfilter;
  305 
  306         if (name == NULL || name[0] == '\0')
  307                 return (EINVAL);        /* invalid name */
  308 
  309         if (kfilter_byname_sys(name) != NULL)
  310                 return (EINVAL);        /* can't detach system filters */
  311 
  312         kfilter = kfilter_byname_user(name);
  313         if (kfilter == NULL)            /* not found */
  314                 return (ENOENT);
  315 
  316         if (kfilter->name[0] != '\0') {
  317                 /* XXX Cast away const (but we know it's safe. */
  318                 free((void *) kfilter->name, M_KEVENT);
  319                 kfilter->name = "";     /* mark as `not implemented' */
  320         }
  321         if (kfilter->filtops != NULL) {
  322                 /* XXX Cast away const (but we know it's safe. */
  323                 free((void *) kfilter->filtops, M_KEVENT);
  324                 kfilter->filtops = NULL; /* mark as `not implemented' */
  325         }
  326         return (0);
  327 }
  328 
  329 
  330 /*
  331  * Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
  332  * descriptors. Calls struct fileops kqfilter method for given file descriptor.
  333  */
  334 static int
  335 filt_fileattach(struct knote *kn)
  336 {
  337         struct file *fp;
  338 
  339         fp = kn->kn_fp;
  340         return ((*fp->f_ops->fo_kqfilter)(fp, kn));
  341 }
  342 
  343 /*
  344  * Filter detach method for EVFILT_READ on kqueue descriptor.
  345  */
  346 static void
  347 filt_kqdetach(struct knote *kn)
  348 {
  349         struct kqueue *kq;
  350 
  351         kq = (struct kqueue *)kn->kn_fp->f_data;
  352         SLIST_REMOVE(&kq->kq_sel.sel_klist, kn, knote, kn_selnext);
  353 }
  354 
  355 /*
  356  * Filter event method for EVFILT_READ on kqueue descriptor.
  357  */
  358 /*ARGSUSED*/
  359 static int
  360 filt_kqueue(struct knote *kn, long hint)
  361 {
  362         struct kqueue *kq;
  363 
  364         kq = (struct kqueue *)kn->kn_fp->f_data;
  365         kn->kn_data = kq->kq_count;
  366         return (kn->kn_data > 0);
  367 }
  368 
  369 /*
  370  * Filter attach method for EVFILT_PROC.
  371  */
  372 static int
  373 filt_procattach(struct knote *kn)
  374 {
  375         struct proc *p;
  376 
  377         p = pfind(kn->kn_id);
  378         if (p == NULL)
  379                 return (ESRCH);
  380 
  381         /*
  382          * Fail if it's not owned by you, or the last exec gave us
  383          * setuid/setgid privs (unless you're root).
  384          */
  385         if ((p->p_cred->p_ruid != curproc->p_cred->p_ruid ||
  386                 (p->p_flag & P_SUGID))
  387             && suser(curproc->p_ucred, &curproc->p_acflag) != 0)
  388                 return (EACCES);
  389 
  390         kn->kn_ptr.p_proc = p;
  391         kn->kn_flags |= EV_CLEAR;       /* automatically set */
  392 
  393         /*
  394          * internal flag indicating registration done by kernel
  395          */
  396         if (kn->kn_flags & EV_FLAG1) {
  397                 kn->kn_data = kn->kn_sdata;     /* ppid */
  398                 kn->kn_fflags = NOTE_CHILD;
  399                 kn->kn_flags &= ~EV_FLAG1;
  400         }
  401 
  402         /* XXXSMP lock the process? */
  403         SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
  404 
  405         return (0);
  406 }
  407 
  408 /*
  409  * Filter detach method for EVFILT_PROC.
  410  *
  411  * The knote may be attached to a different process, which may exit,
  412  * leaving nothing for the knote to be attached to.  So when the process
  413  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
  414  * it will be deleted when read out.  However, as part of the knote deletion,
  415  * this routine is called, so a check is needed to avoid actually performing
  416  * a detach, because the original process might not exist any more.
  417  */
  418 static void
  419 filt_procdetach(struct knote *kn)
  420 {
  421         struct proc *p;
  422 
  423         if (kn->kn_status & KN_DETACHED)
  424                 return;
  425 
  426         p = kn->kn_ptr.p_proc;
  427         KASSERT(p->p_stat == SZOMB || pfind(kn->kn_id) == p);
  428 
  429         /* XXXSMP lock the process? */
  430         SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
  431 }
  432 
  433 /*
  434  * Filter event method for EVFILT_PROC.
  435  */
  436 static int
  437 filt_proc(struct knote *kn, long hint)
  438 {
  439         u_int event;
  440 
  441         /*
  442          * mask off extra data
  443          */
  444         event = (u_int)hint & NOTE_PCTRLMASK;
  445 
  446         /*
  447          * if the user is interested in this event, record it.
  448          */
  449         if (kn->kn_sfflags & event)
  450                 kn->kn_fflags |= event;
  451 
  452         /*
  453          * process is gone, so flag the event as finished.
  454          */
  455         if (event == NOTE_EXIT) {
  456                 /*
  457                  * Detach the knote from watched process and mark
  458                  * it as such. We can't leave this to kqueue_scan(),
  459                  * since the process might not exist by then. And we
  460                  * have to do this now, since psignal KNOTE() is called
  461                  * also for zombies and we might end up reading freed
  462                  * memory if the kevent would already be picked up
  463                  * and knote g/c'ed. 
  464                  */
  465                 kn->kn_fop->f_detach(kn);
  466                 kn->kn_status |= KN_DETACHED;
  467 
  468                 /* Mark as ONESHOT, so that the knote it g/c'ed when read */
  469                 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 
  470                 return (1);
  471         }
  472 
  473         /*
  474          * process forked, and user wants to track the new process,
  475          * so attach a new knote to it, and immediately report an
  476          * event with the parent's pid.
  477          */
  478         if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
  479                 struct kevent kev;
  480                 int error;
  481 
  482                 /*
  483                  * register knote with new process.
  484                  */
  485                 kev.ident = hint & NOTE_PDATAMASK;      /* pid */
  486                 kev.filter = kn->kn_filter;
  487                 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
  488                 kev.fflags = kn->kn_sfflags;
  489                 kev.data = kn->kn_id;                   /* parent */
  490                 kev.udata = kn->kn_kevent.udata;        /* preserve udata */
  491                 error = kqueue_register(kn->kn_kq, &kev, NULL);
  492                 if (error)
  493                         kn->kn_fflags |= NOTE_TRACKERR;
  494         }
  495 
  496         return (kn->kn_fflags != 0);
  497 }
  498 
  499 static void
  500 filt_timerexpire(void *knx)
  501 {
  502         struct knote *kn = knx;
  503         int tticks;
  504 
  505         kn->kn_data++;
  506         KNOTE_ACTIVATE(kn);
  507 
  508         if ((kn->kn_flags & EV_ONESHOT) == 0) {
  509                 tticks = mstohz(kn->kn_sdata);
  510                 callout_schedule((struct callout *)kn->kn_hook, tticks);
  511         }
  512 }
  513 
  514 /*
  515  * data contains amount of time to sleep, in milliseconds
  516  */ 
  517 static int
  518 filt_timerattach(struct knote *kn)
  519 {
  520         struct callout *calloutp;
  521         int tticks;
  522 
  523         if (kq_ncallouts >= kq_calloutmax)
  524                 return (ENOMEM);
  525         kq_ncallouts++;
  526 
  527         tticks = mstohz(kn->kn_sdata);
  528 
  529         /* if the supplied value is under our resolution, use 1 tick */
  530         if (tticks == 0) {
  531                 if (kn->kn_sdata == 0)
  532                         return (EINVAL);
  533                 tticks = 1;
  534         }
  535 
  536         kn->kn_flags |= EV_CLEAR;               /* automatically set */
  537         MALLOC(calloutp, struct callout *, sizeof(*calloutp),
  538             M_KEVENT, 0);
  539         callout_init(calloutp);
  540         callout_reset(calloutp, tticks, filt_timerexpire, kn);
  541         kn->kn_hook = calloutp;
  542 
  543         return (0);
  544 }
  545 
  546 static void
  547 filt_timerdetach(struct knote *kn)
  548 {
  549         struct callout *calloutp;
  550 
  551         calloutp = (struct callout *)kn->kn_hook;
  552         callout_stop(calloutp);
  553         FREE(calloutp, M_KEVENT);
  554         kq_ncallouts--;
  555 }
  556 
  557 static int
  558 filt_timer(struct knote *kn, long hint)
  559 {
  560         return (kn->kn_data != 0);
  561 }
  562 
  563 /*
  564  * filt_seltrue:
  565  *
  566  *      This filter "event" routine simulates seltrue().
  567  */
  568 int
  569 filt_seltrue(struct knote *kn, long hint)
  570 {
  571 
  572         /*
  573          * We don't know how much data can be read/written,
  574          * but we know that it *can* be.  This is about as
  575          * good as select/poll does as well.
  576          */
  577         kn->kn_data = 0;
  578         return (1);
  579 }
  580 
  581 /*
  582  * This provides full kqfilter entry for device switch tables, which
  583  * has same effect as filter using filt_seltrue() as filter method.
  584  */
  585 static void
  586 filt_seltruedetach(struct knote *kn)
  587 {
  588         /* Nothing to do */
  589 }
  590 
  591 static const struct filterops seltrue_filtops =
  592         { 1, NULL, filt_seltruedetach, filt_seltrue };
  593 
  594 int
  595 seltrue_kqfilter(dev_t dev, struct knote *kn)
  596 {
  597         switch (kn->kn_filter) {
  598         case EVFILT_READ:
  599         case EVFILT_WRITE:
  600                 kn->kn_fop = &seltrue_filtops;
  601                 break;
  602         default:
  603                 return (1);
  604         }
  605 
  606         /* Nothing more to do */
  607         return (0);
  608 }
  609 
  610 /*
  611  * kqueue(2) system call.
  612  */
  613 int
  614 sys_kqueue(struct lwp *l, void *v, register_t *retval)
  615 {
  616         struct filedesc *fdp;
  617         struct kqueue   *kq;
  618         struct file     *fp;
  619         struct proc     *p;
  620         int             fd, error;
  621 
  622         p = l->l_proc;
  623         fdp = p->p_fd;
  624         error = falloc(p, &fp, &fd);    /* setup a new file descriptor */
  625         if (error)
  626                 return (error);
  627         fp->f_flag = FREAD | FWRITE;
  628         fp->f_type = DTYPE_KQUEUE;
  629         fp->f_ops = &kqueueops;
  630         kq = pool_get(&kqueue_pool, PR_WAITOK);
  631         memset((char *)kq, 0, sizeof(struct kqueue));
  632         simple_lock_init(&kq->kq_lock);
  633         TAILQ_INIT(&kq->kq_head);
  634         fp->f_data = (caddr_t)kq;       /* store the kqueue with the fp */
  635         *retval = fd;
  636         if (fdp->fd_knlistsize < 0)
  637                 fdp->fd_knlistsize = 0; /* this process has a kq */
  638         kq->kq_fdp = fdp;
  639         FILE_SET_MATURE(fp);
  640         FILE_UNUSE(fp, p);              /* falloc() does FILE_USE() */
  641         return (error);
  642 }
  643 
  644 /*
  645  * kevent(2) system call.
  646  */
  647 int
  648 sys_kevent(struct lwp *l, void *v, register_t *retval)
  649 {
  650         struct sys_kevent_args /* {
  651                 syscallarg(int) fd;
  652                 syscallarg(const struct kevent *) changelist;
  653                 syscallarg(size_t) nchanges;
  654                 syscallarg(struct kevent *) eventlist;
  655                 syscallarg(size_t) nevents;
  656                 syscallarg(const struct timespec *) timeout;
  657         } */ *uap = v;
  658         struct kevent   *kevp;
  659         struct kqueue   *kq;
  660         struct file     *fp;
  661         struct timespec ts;
  662         struct proc     *p;
  663         size_t          i, n;
  664         int             nerrors, error;
  665 
  666         p = l->l_proc;
  667         /* check that we're dealing with a kq */
  668         fp = fd_getfile(p->p_fd, SCARG(uap, fd));
  669         if (fp == NULL)
  670                 return (EBADF);
  671 
  672         if (fp->f_type != DTYPE_KQUEUE) {
  673                 simple_unlock(&fp->f_slock);
  674                 return (EBADF);
  675         }
  676 
  677         FILE_USE(fp);
  678 
  679         if (SCARG(uap, timeout) != NULL) {
  680                 error = copyin(SCARG(uap, timeout), &ts, sizeof(ts));
  681                 if (error)
  682                         goto done;
  683                 SCARG(uap, timeout) = &ts;
  684         }
  685 
  686         kq = (struct kqueue *)fp->f_data;
  687         nerrors = 0;
  688 
  689         /* traverse list of events to register */
  690         while (SCARG(uap, nchanges) > 0) {
  691                 /* copyin a maximum of KQ_EVENTS at each pass */
  692                 n = MIN(SCARG(uap, nchanges), KQ_NEVENTS);
  693                 error = copyin(SCARG(uap, changelist), kq->kq_kev,
  694                     n * sizeof(struct kevent));
  695                 if (error)
  696                         goto done;
  697                 for (i = 0; i < n; i++) {
  698                         kevp = &kq->kq_kev[i];
  699                         kevp->flags &= ~EV_SYSFLAGS;
  700                         /* register each knote */
  701                         error = kqueue_register(kq, kevp, p);
  702                         if (error) {
  703                                 if (SCARG(uap, nevents) != 0) {
  704                                         kevp->flags = EV_ERROR;
  705                                         kevp->data = error;
  706                                         error = copyout((caddr_t)kevp,
  707                                             (caddr_t)SCARG(uap, eventlist),
  708                                             sizeof(*kevp));
  709                                         if (error)
  710                                                 goto done;
  711                                         SCARG(uap, eventlist)++;
  712                                         SCARG(uap, nevents)--;
  713                                         nerrors++;
  714                                 } else {
  715                                         goto done;
  716                                 }
  717                         }
  718                 }
  719                 SCARG(uap, nchanges) -= n;      /* update the results */
  720                 SCARG(uap, changelist) += n;
  721         }
  722         if (nerrors) {
  723                 *retval = nerrors;
  724                 error = 0;
  725                 goto done;
  726         }
  727 
  728         /* actually scan through the events */
  729         error = kqueue_scan(fp, SCARG(uap, nevents), SCARG(uap, eventlist),
  730             SCARG(uap, timeout), p, retval);
  731  done:
  732         FILE_UNUSE(fp, p);
  733         return (error);
  734 }
  735 
  736 /*
  737  * Register a given kevent kev onto the kqueue
  738  */
  739 int
  740 kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p)
  741 {
  742         const struct kfilter *kfilter;
  743         struct filedesc *fdp;
  744         struct file     *fp;
  745         struct knote    *kn;
  746         int             s, error;
  747 
  748         fdp = kq->kq_fdp;
  749         fp = NULL;
  750         kn = NULL;
  751         error = 0;
  752         kfilter = kfilter_byfilter(kev->filter);
  753         if (kfilter == NULL || kfilter->filtops == NULL) {
  754                 /* filter not found nor implemented */
  755                 return (EINVAL);
  756         }
  757 
  758         /* search if knote already exists */
  759         if (kfilter->filtops->f_isfd) {
  760                 /* monitoring a file descriptor */
  761                 if ((fp = fd_getfile(fdp, kev->ident)) == NULL)
  762                         return (EBADF); /* validate descriptor */
  763                 FILE_USE(fp);
  764 
  765                 if (kev->ident < fdp->fd_knlistsize) {
  766                         SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
  767                                 if (kq == kn->kn_kq &&
  768                                     kev->filter == kn->kn_filter)
  769                                         break;
  770                 }
  771         } else {
  772                 /*
  773                  * not monitoring a file descriptor, so
  774                  * lookup knotes in internal hash table
  775                  */
  776                 if (fdp->fd_knhashmask != 0) {
  777                         struct klist *list;
  778                         
  779                         list = &fdp->fd_knhash[
  780                             KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
  781                         SLIST_FOREACH(kn, list, kn_link)
  782                                 if (kev->ident == kn->kn_id &&
  783                                     kq == kn->kn_kq &&
  784                                     kev->filter == kn->kn_filter)
  785                                         break;
  786                 }
  787         }
  788 
  789         if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
  790                 error = ENOENT;         /* filter not found */
  791                 goto done;
  792         }
  793 
  794         /*
  795          * kn now contains the matching knote, or NULL if no match
  796          */
  797         if (kev->flags & EV_ADD) {
  798                 /* add knote */
  799 
  800                 if (kn == NULL) {
  801                         /* create new knote */
  802                         kn = pool_get(&knote_pool, PR_WAITOK);
  803                         if (kn == NULL) {
  804                                 error = ENOMEM;
  805                                 goto done;
  806                         }
  807                         kn->kn_fp = fp;
  808                         kn->kn_kq = kq;
  809                         kn->kn_fop = kfilter->filtops;
  810 
  811                         /*
  812                          * apply reference count to knote structure, and
  813                          * do not release it at the end of this routine.
  814                          */
  815                         fp = NULL;
  816 
  817                         kn->kn_sfflags = kev->fflags;
  818                         kn->kn_sdata = kev->data;
  819                         kev->fflags = 0;
  820                         kev->data = 0;
  821                         kn->kn_kevent = *kev;
  822 
  823                         knote_attach(kn, fdp);
  824                         if ((error = kfilter->filtops->f_attach(kn)) != 0) {
  825                                 knote_drop(kn, p, fdp);
  826                                 goto done;
  827                         }
  828                 } else {
  829                         /* modify existing knote */
  830 
  831                         /*
  832                          * The user may change some filter values after the
  833                          * initial EV_ADD, but doing so will not reset any 
  834                          * filter which have already been triggered.
  835                          */
  836                         kn->kn_sfflags = kev->fflags;
  837                         kn->kn_sdata = kev->data;
  838                         kn->kn_kevent.udata = kev->udata;
  839                 }
  840 
  841                 s = splsched();
  842                 if (kn->kn_fop->f_event(kn, 0))
  843                         KNOTE_ACTIVATE(kn);
  844                 splx(s);
  845 
  846         } else if (kev->flags & EV_DELETE) {    /* delete knote */
  847                 kn->kn_fop->f_detach(kn);
  848                 knote_drop(kn, p, fdp);
  849                 goto done;
  850         }
  851 
  852         /* disable knote */
  853         if ((kev->flags & EV_DISABLE) &&
  854             ((kn->kn_status & KN_DISABLED) == 0)) {
  855                 s = splsched();
  856                 kn->kn_status |= KN_DISABLED;
  857                 splx(s);
  858         }
  859 
  860         /* enable knote */
  861         if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
  862                 s = splsched();
  863                 kn->kn_status &= ~KN_DISABLED;
  864                 if ((kn->kn_status & KN_ACTIVE) &&
  865                     ((kn->kn_status & KN_QUEUED) == 0))
  866                         knote_enqueue(kn);
  867                 splx(s);
  868         }
  869 
  870  done:
  871         if (fp != NULL)
  872                 FILE_UNUSE(fp, p);
  873         return (error);
  874 }
  875 
  876 /*
  877  * Scan through the list of events on fp (for a maximum of maxevents),
  878  * returning the results in to ulistp. Timeout is determined by tsp; if
  879  * NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
  880  * as appropriate.
  881  */
  882 static int
  883 kqueue_scan(struct file *fp, size_t maxevents, struct kevent *ulistp,
  884         const struct timespec *tsp, struct proc *p, register_t *retval)
  885 {
  886         struct kqueue   *kq;
  887         struct kevent   *kevp;
  888         struct timeval  atv;
  889         struct knote    *kn, *marker=NULL;
  890         size_t          count, nkev;
  891         int             s, timeout, error;
  892 
  893         kq = (struct kqueue *)fp->f_data;
  894         count = maxevents;
  895         nkev = error = 0;
  896         if (count == 0)
  897                 goto done;
  898 
  899         if (tsp) {                              /* timeout supplied */
  900                 TIMESPEC_TO_TIMEVAL(&atv, tsp);
  901                 if (itimerfix(&atv)) {
  902                         error = EINVAL;
  903                         goto done;
  904                 }
  905                 s = splclock();
  906                 timeradd(&atv, &time, &atv);    /* calc. time to wait until */
  907                 splx(s);
  908                 timeout = hzto(&atv);
  909                 if (timeout <= 0)
  910                         timeout = -1;           /* do poll */
  911         } else {
  912                 /* no timeout, wait forever */
  913                 timeout = 0;
  914         }
  915 
  916         MALLOC(marker, struct knote *, sizeof(*marker), M_KEVENT, M_WAITOK);
  917         memset(marker, 0, sizeof(*marker));
  918 
  919         goto start;
  920 
  921  retry:
  922         if (tsp) {
  923                 /*
  924                  * We have to recalculate the timeout on every retry.
  925                  */
  926                 timeout = hzto(&atv);
  927                 if (timeout <= 0)
  928                         goto done;
  929         }
  930 
  931  start:
  932         kevp = kq->kq_kev;
  933         s = splsched();
  934         simple_lock(&kq->kq_lock);
  935         if (kq->kq_count == 0) {
  936                 if (timeout < 0) { 
  937                         error = EWOULDBLOCK;
  938                         simple_unlock(&kq->kq_lock);
  939                 } else {
  940                         kq->kq_state |= KQ_SLEEP;
  941                         error = ltsleep(kq, PSOCK | PCATCH | PNORELOCK,
  942                                         "kqread", timeout, &kq->kq_lock);
  943                 }
  944                 splx(s);
  945                 if (error == 0)
  946                         goto retry;
  947                 /* don't restart after signals... */
  948                 if (error == ERESTART)
  949                         error = EINTR;
  950                 else if (error == EWOULDBLOCK)
  951                         error = 0;
  952                 goto done;
  953         }
  954 
  955         /* mark end of knote list */
  956         TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe); 
  957         simple_unlock(&kq->kq_lock);
  958 
  959         while (count) {                         /* while user wants data ... */
  960                 simple_lock(&kq->kq_lock);
  961                 kn = TAILQ_FIRST(&kq->kq_head); /* get next knote */
  962                 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 
  963                 if (kn == marker) {             /* if it's our marker, stop */
  964                         /* What if it's some else's marker? */
  965                         simple_unlock(&kq->kq_lock);
  966                         splx(s);
  967                         if (count == maxevents)
  968                                 goto retry;
  969                         goto done;
  970                 }
  971                 kq->kq_count--;
  972                 simple_unlock(&kq->kq_lock);
  973 
  974                 if (kn->kn_status & KN_DISABLED) {
  975                         /* don't want disabled events */
  976                         kn->kn_status &= ~KN_QUEUED;
  977                         continue;
  978                 }
  979                 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
  980                     kn->kn_fop->f_event(kn, 0) == 0) {
  981                         /*
  982                          * non-ONESHOT event that hasn't
  983                          * triggered again, so de-queue.
  984                          */
  985                         kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
  986                         continue;
  987                 }
  988                 *kevp = kn->kn_kevent;
  989                 kevp++;
  990                 nkev++;
  991                 if (kn->kn_flags & EV_ONESHOT) {
  992                         /* delete ONESHOT events after retrieval */
  993                         kn->kn_status &= ~KN_QUEUED;
  994                         splx(s);
  995                         kn->kn_fop->f_detach(kn);
  996                         knote_drop(kn, p, p->p_fd);
  997                         s = splsched();
  998                 } else if (kn->kn_flags & EV_CLEAR) {
  999                         /* clear state after retrieval */
 1000                         kn->kn_data = 0;
 1001                         kn->kn_fflags = 0;
 1002                         kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
 1003                 } else {
 1004                         /* add event back on list */
 1005                         simple_lock(&kq->kq_lock);
 1006                         TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 
 1007                         kq->kq_count++;
 1008                         simple_unlock(&kq->kq_lock);
 1009                 }
 1010                 count--;
 1011                 if (nkev == KQ_NEVENTS) {
 1012                         /* do copyouts in KQ_NEVENTS chunks */
 1013                         splx(s);
 1014                         error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
 1015                             sizeof(struct kevent) * nkev);
 1016                         ulistp += nkev;
 1017                         nkev = 0;
 1018                         kevp = kq->kq_kev;
 1019                         s = splsched();
 1020                         if (error)
 1021                                 break;
 1022                 }
 1023         }
 1024 
 1025         /* remove marker */
 1026         simple_lock(&kq->kq_lock);
 1027         TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe); 
 1028         simple_unlock(&kq->kq_lock);
 1029         splx(s);
 1030  done:
 1031         if (marker)
 1032                 FREE(marker, M_KEVENT);
 1033 
 1034         if (nkev != 0) {
 1035                 /* copyout remaining events */
 1036                 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
 1037                     sizeof(struct kevent) * nkev);
 1038         }
 1039         *retval = maxevents - count;
 1040 
 1041         return (error);
 1042 }
 1043 
 1044 /*
 1045  * struct fileops read method for a kqueue descriptor.
 1046  * Not implemented.
 1047  * XXX: This could be expanded to call kqueue_scan, if desired.
 1048  */
 1049 /*ARGSUSED*/
 1050 static int
 1051 kqueue_read(struct file *fp, off_t *offset, struct uio *uio,
 1052         struct ucred *cred, int flags)
 1053 {
 1054 
 1055         return (ENXIO);
 1056 }
 1057 
 1058 /*
 1059  * struct fileops write method for a kqueue descriptor.
 1060  * Not implemented.
 1061  */
 1062 /*ARGSUSED*/
 1063 static int
 1064 kqueue_write(struct file *fp, off_t *offset, struct uio *uio,
 1065         struct ucred *cred, int flags)
 1066 {
 1067 
 1068         return (ENXIO);
 1069 }
 1070 
 1071 /*
 1072  * struct fileops ioctl method for a kqueue descriptor.
 1073  *
 1074  * Two ioctls are currently supported. They both use struct kfilter_mapping:
 1075  *      KFILTER_BYNAME          find name for filter, and return result in
 1076  *                              name, which is of size len.
 1077  *      KFILTER_BYFILTER        find filter for name. len is ignored.
 1078  */
 1079 /*ARGSUSED*/
 1080 static int
 1081 kqueue_ioctl(struct file *fp, u_long com, void *data, struct proc *p)
 1082 {
 1083         struct kfilter_mapping  *km;
 1084         const struct kfilter    *kfilter;
 1085         char                    *name;
 1086         int                     error;
 1087 
 1088         km = (struct kfilter_mapping *)data; 
 1089         error = 0;
 1090 
 1091         switch (com) {
 1092         case KFILTER_BYFILTER:  /* convert filter -> name */
 1093                 kfilter = kfilter_byfilter(km->filter);
 1094                 if (kfilter != NULL)
 1095                         error = copyoutstr(kfilter->name, km->name, km->len,
 1096                             NULL);
 1097                 else
 1098                         error = ENOENT;
 1099                 break;
 1100 
 1101         case KFILTER_BYNAME:    /* convert name -> filter */
 1102                 MALLOC(name, char *, KFILTER_MAXNAME, M_KEVENT, M_WAITOK);
 1103                 error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
 1104                 if (error) {
 1105                         FREE(name, M_KEVENT);
 1106                         break;
 1107                 }
 1108                 kfilter = kfilter_byname(name);
 1109                 if (kfilter != NULL)
 1110                         km->filter = kfilter->filter;
 1111                 else
 1112                         error = ENOENT;
 1113                 FREE(name, M_KEVENT);
 1114                 break;
 1115 
 1116         default:
 1117                 error = ENOTTY;
 1118 
 1119         }
 1120         return (error);
 1121 }
 1122 
 1123 /*
 1124  * struct fileops fcntl method for a kqueue descriptor.
 1125  * Not implemented.
 1126  */
 1127 /*ARGSUSED*/
 1128 static int
 1129 kqueue_fcntl(struct file *fp, u_int com, void *data, struct proc *p)
 1130 {
 1131 
 1132         return (ENOTTY);
 1133 }
 1134 
 1135 /*
 1136  * struct fileops poll method for a kqueue descriptor.
 1137  * Determine if kqueue has events pending.
 1138  */
 1139 static int
 1140 kqueue_poll(struct file *fp, int events, struct proc *p)
 1141 {
 1142         struct kqueue   *kq;
 1143         int             revents;
 1144 
 1145         kq = (struct kqueue *)fp->f_data;
 1146         revents = 0;
 1147         if (events & (POLLIN | POLLRDNORM)) {
 1148                 if (kq->kq_count) {
 1149                         revents |= events & (POLLIN | POLLRDNORM);
 1150                 } else {
 1151                         selrecord(p, &kq->kq_sel);
 1152                 }
 1153         }
 1154         return (revents);
 1155 }
 1156 
 1157 /*
 1158  * struct fileops stat method for a kqueue descriptor.
 1159  * Returns dummy info, with st_size being number of events pending.
 1160  */
 1161 static int
 1162 kqueue_stat(struct file *fp, struct stat *st, struct proc *p)
 1163 {
 1164         struct kqueue   *kq;
 1165 
 1166         kq = (struct kqueue *)fp->f_data;
 1167         memset((void *)st, 0, sizeof(*st));
 1168         st->st_size = kq->kq_count;
 1169         st->st_blksize = sizeof(struct kevent);
 1170         st->st_mode = S_IFIFO;
 1171         return (0);
 1172 }
 1173 
 1174 /*
 1175  * struct fileops close method for a kqueue descriptor.
 1176  * Cleans up kqueue.
 1177  */
 1178 static int
 1179 kqueue_close(struct file *fp, struct proc *p)
 1180 {
 1181         struct kqueue   *kq;
 1182         struct filedesc *fdp;
 1183         struct knote    **knp, *kn, *kn0;
 1184         int             i;
 1185 
 1186         kq = (struct kqueue *)fp->f_data;
 1187         fdp = p->p_fd;
 1188         for (i = 0; i < fdp->fd_knlistsize; i++) {
 1189                 knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
 1190                 kn = *knp;
 1191                 while (kn != NULL) {
 1192                         kn0 = SLIST_NEXT(kn, kn_link);
 1193                         if (kq == kn->kn_kq) {
 1194                                 kn->kn_fop->f_detach(kn);
 1195                                 FILE_UNUSE(kn->kn_fp, p);
 1196                                 pool_put(&knote_pool, kn);
 1197                                 *knp = kn0;
 1198                         } else {
 1199                                 knp = &SLIST_NEXT(kn, kn_link);
 1200                         }
 1201                         kn = kn0;
 1202                 }
 1203         }
 1204         if (fdp->fd_knhashmask != 0) {
 1205                 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
 1206                         knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
 1207                         kn = *knp;
 1208                         while (kn != NULL) {
 1209                                 kn0 = SLIST_NEXT(kn, kn_link);
 1210                                 if (kq == kn->kn_kq) {
 1211                                         kn->kn_fop->f_detach(kn);
 1212                                         /* XXX non-fd release of kn->kn_ptr */
 1213                                         pool_put(&knote_pool, kn);
 1214                                         *knp = kn0;
 1215                                 } else {
 1216                                         knp = &SLIST_NEXT(kn, kn_link);
 1217                                 }
 1218                                 kn = kn0;
 1219                         }
 1220                 }
 1221         }
 1222         pool_put(&kqueue_pool, kq);
 1223         fp->f_data = NULL;
 1224 
 1225         return (0);
 1226 }
 1227 
 1228 /*
 1229  * wakeup a kqueue
 1230  */
 1231 static void
 1232 kqueue_wakeup(struct kqueue *kq)
 1233 {
 1234         int s;
 1235 
 1236         s = splsched();
 1237         simple_lock(&kq->kq_lock);
 1238         if (kq->kq_state & KQ_SLEEP) {          /* if currently sleeping ...  */
 1239                 kq->kq_state &= ~KQ_SLEEP;
 1240                 wakeup(kq);                     /* ... wakeup */
 1241         }
 1242 
 1243         /* Notify select/poll and kevent. */
 1244         selnotify(&kq->kq_sel, 0);
 1245         simple_unlock(&kq->kq_lock);
 1246         splx(s);
 1247 }
 1248 
 1249 /*
 1250  * struct fileops kqfilter method for a kqueue descriptor.
 1251  * Event triggered when monitored kqueue changes.
 1252  */
 1253 /*ARGSUSED*/
 1254 static int
 1255 kqueue_kqfilter(struct file *fp, struct knote *kn)
 1256 {
 1257         struct kqueue *kq;
 1258 
 1259         KASSERT(fp == kn->kn_fp);
 1260         kq = (struct kqueue *)kn->kn_fp->f_data;
 1261         if (kn->kn_filter != EVFILT_READ)
 1262                 return (1);
 1263         kn->kn_fop = &kqread_filtops;
 1264         SLIST_INSERT_HEAD(&kq->kq_sel.sel_klist, kn, kn_selnext);
 1265         return (0);
 1266 }
 1267 
 1268 
 1269 /*
 1270  * Walk down a list of knotes, activating them if their event has triggered.
 1271  */
 1272 void
 1273 knote(struct klist *list, long hint)
 1274 {
 1275         struct knote *kn;
 1276 
 1277         SLIST_FOREACH(kn, list, kn_selnext)
 1278                 if (kn->kn_fop->f_event(kn, hint))
 1279                         KNOTE_ACTIVATE(kn);
 1280 }
 1281 
 1282 /*
 1283  * Remove all knotes from a specified klist
 1284  */
 1285 void
 1286 knote_remove(struct proc *p, struct klist *list)
 1287 {
 1288         struct knote *kn;
 1289 
 1290         while ((kn = SLIST_FIRST(list)) != NULL) {
 1291                 kn->kn_fop->f_detach(kn);
 1292                 knote_drop(kn, p, p->p_fd);
 1293         }
 1294 }
 1295 
 1296 /*
 1297  * Remove all knotes referencing a specified fd
 1298  */
 1299 void
 1300 knote_fdclose(struct proc *p, int fd)
 1301 {
 1302         struct filedesc *fdp;
 1303         struct klist    *list;
 1304 
 1305         fdp = p->p_fd;
 1306         list = &fdp->fd_knlist[fd];
 1307         knote_remove(p, list);
 1308 }
 1309 
 1310 /*
 1311  * Attach a new knote to a file descriptor
 1312  */
 1313 static void
 1314 knote_attach(struct knote *kn, struct filedesc *fdp)
 1315 {
 1316         struct klist    *list;
 1317         int             size;
 1318 
 1319         if (! kn->kn_fop->f_isfd) {
 1320                 /* if knote is not on an fd, store on internal hash table */
 1321                 if (fdp->fd_knhashmask == 0)
 1322                         fdp->fd_knhash = hashinit(KN_HASHSIZE, HASH_LIST,
 1323                             M_KEVENT, M_WAITOK, &fdp->fd_knhashmask);
 1324                 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
 1325                 goto done;
 1326         }
 1327 
 1328         /*
 1329          * otherwise, knote is on an fd.
 1330          * knotes are stored in fd_knlist indexed by kn->kn_id.
 1331          */
 1332         if (fdp->fd_knlistsize <= kn->kn_id) {
 1333                 /* expand list, it's too small */
 1334                 size = fdp->fd_knlistsize;
 1335                 while (size <= kn->kn_id) {
 1336                         /* grow in KQ_EXTENT chunks */
 1337                         size += KQ_EXTENT;
 1338                 }
 1339                 list = malloc(size * sizeof(struct klist *), M_KEVENT,M_WAITOK);
 1340                 if (fdp->fd_knlist) {
 1341                         /* copy existing knlist */
 1342                         memcpy((caddr_t)list, (caddr_t)fdp->fd_knlist,
 1343                             fdp->fd_knlistsize * sizeof(struct klist *));
 1344                 }
 1345                 /*
 1346                  * Zero new memory. Stylistically, SLIST_INIT() should be
 1347                  * used here, but that does same thing as the memset() anyway.
 1348                  */
 1349                 memset(&list[fdp->fd_knlistsize], 0,
 1350                     (size - fdp->fd_knlistsize) * sizeof(struct klist *));
 1351 
 1352                 /* switch to new knlist */
 1353                 if (fdp->fd_knlist != NULL)
 1354                         free(fdp->fd_knlist, M_KEVENT);
 1355                 fdp->fd_knlistsize = size;
 1356                 fdp->fd_knlist = list;
 1357         }
 1358 
 1359         /* get list head for this fd */
 1360         list = &fdp->fd_knlist[kn->kn_id];
 1361  done:
 1362         /* add new knote */
 1363         SLIST_INSERT_HEAD(list, kn, kn_link);
 1364         kn->kn_status = 0;
 1365 }
 1366 
 1367 /*
 1368  * Drop knote.
 1369  * Should be called at spl == 0, since we don't want to hold spl
 1370  * while calling FILE_UNUSE and free.
 1371  */
 1372 static void
 1373 knote_drop(struct knote *kn, struct proc *p, struct filedesc *fdp)
 1374 {
 1375         struct klist    *list;
 1376 
 1377         if (kn->kn_fop->f_isfd)
 1378                 list = &fdp->fd_knlist[kn->kn_id];
 1379         else
 1380                 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
 1381 
 1382         SLIST_REMOVE(list, kn, knote, kn_link);
 1383         if (kn->kn_status & KN_QUEUED)
 1384                 knote_dequeue(kn);
 1385         if (kn->kn_fop->f_isfd)
 1386                 FILE_UNUSE(kn->kn_fp, p);
 1387         pool_put(&knote_pool, kn);
 1388 }
 1389 
 1390 
 1391 /*
 1392  * Queue new event for knote.
 1393  */
 1394 static void
 1395 knote_enqueue(struct knote *kn)
 1396 {
 1397         struct kqueue   *kq;
 1398         int             s;
 1399 
 1400         kq = kn->kn_kq;
 1401         KASSERT((kn->kn_status & KN_QUEUED) == 0);
 1402 
 1403         s = splsched();
 1404         simple_lock(&kq->kq_lock);
 1405         TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 
 1406         kn->kn_status |= KN_QUEUED;
 1407         kq->kq_count++;
 1408         simple_unlock(&kq->kq_lock);
 1409         splx(s);
 1410         kqueue_wakeup(kq);
 1411 }
 1412 
 1413 /*
 1414  * Dequeue event for knote.
 1415  */
 1416 static void
 1417 knote_dequeue(struct knote *kn)
 1418 {
 1419         struct kqueue   *kq;
 1420         int             s;
 1421 
 1422         KASSERT(kn->kn_status & KN_QUEUED);
 1423         kq = kn->kn_kq;
 1424 
 1425         s = splsched();
 1426         simple_lock(&kq->kq_lock);
 1427         TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 
 1428         kn->kn_status &= ~KN_QUEUED;
 1429         kq->kq_count--;
 1430         simple_unlock(&kq->kq_lock);
 1431         splx(s);
 1432 }

Cache object: fb2a90088f6ec6f0825bd1d3514e3b9e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.