xref: /freebsd/sys/kern/kern_event.c (revision 64db83a8ab2d1f72a9b2174b39d2ef42b5b0580c)
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon <jlemon@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/proc.h>
33 #include <sys/malloc.h>
34 #include <sys/unistd.h>
35 #include <sys/file.h>
36 #include <sys/fcntl.h>
37 #include <sys/select.h>
38 #include <sys/queue.h>
39 #include <sys/event.h>
40 #include <sys/eventvar.h>
41 #include <sys/poll.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/stat.h>
46 #include <sys/sysproto.h>
47 #include <sys/uio.h>
48 
49 #include <vm/vm_zone.h>
50 
51 static int 	filt_nullattach(struct knote *kn);
52 static int 	filt_rwtypattach(struct knote *kn);
53 static int	filt_kqattach(struct knote *kn);
54 static void	filt_kqdetach(struct knote *kn);
55 static int	filt_kqueue(struct knote *kn, long hint);
56 static int	filt_procattach(struct knote *kn);
57 static void	filt_procdetach(struct knote *kn);
58 static int	filt_proc(struct knote *kn, long hint);
59 
60 static int 	kqueue_create(struct kqueue **kqp);
61 static int	kqueue_scan(struct file *fp, int maxevents,
62 		    struct kevent *ulistp, struct timespec *timeout,
63 		    struct proc *p);
64 static int 	kqueue_read(struct file *fp, struct uio *uio,
65 		    struct ucred *cred, int flags, struct proc *p);
66 static int	kqueue_write(struct file *fp, struct uio *uio,
67 		    struct ucred *cred, int flags, struct proc *p);
68 static int	kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
69 		    struct proc *p);
70 static int 	kqueue_poll(struct file *fp, int events, struct ucred *cred,
71 		    struct proc *p);
72 static int 	kqueue_stat(struct file *fp, struct stat *st, struct proc *p);
73 static int 	kqueue_close(struct file *fp, struct proc *p);
74 static void 	kqueue_wakeup(struct kqueue *kq);
75 
76 static void 	knote_attach(struct knote *kn, struct filedesc *fdp);
77 static void 	knote_drop(struct knote *kn, struct proc *p);
78 static void 	knote_enqueue(struct knote *kn);
79 static void 	knote_dequeue(struct knote *kn);
80 static void 	knote_init(void);
81 static struct 	knote *knote_alloc(void);
82 static void 	knote_free(struct knote *kn);
83 
84 static vm_zone_t	knote_zone;
85 
86 #define KNOTE_ACTIVATE(kn) do { 					\
87 	kn->kn_status |= KN_ACTIVE;					\
88 	if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)		\
89 		knote_enqueue(kn);					\
90 } while(0)
91 
92 #define	KN_HASHSIZE		64		/* XXX should be tunable */
93 #define KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))
94 
95 static struct fileops kqueueops = {
96 	kqueue_read,
97 	kqueue_write,
98 	kqueue_ioctl,
99 	kqueue_poll,
100 	kqueue_stat,
101 	kqueue_close
102 };
103 
104 extern struct filterops so_rwfiltops[];
105 extern struct filterops fifo_rwfiltops[];
106 extern struct filterops pipe_rwfiltops[];
107 extern struct filterops vn_rwfiltops[];
108 
109 static struct filterops kq_rwfiltops[] = {
110     { 1, filt_kqattach, filt_kqdetach, filt_kqueue },
111     { 1, filt_nullattach, NULL, NULL },
112 };
113 
114 extern struct filterops aio_filtops;
115 extern struct filterops sig_filtops;
116 extern struct filterops vn_filtops;
117 
118 static struct filterops rwtype_filtops =
119 	{ 1, filt_rwtypattach, NULL, NULL };
120 static struct filterops proc_filtops =
121 	{ 0, filt_procattach, filt_procdetach, filt_proc };
122 
123 /*
124  * XXX
125  * These must match the order of defines in <sys/file.h>
126  */
127 static struct filterops *rwtypfilt_sw[] = {
128 	NULL,				/* 0 */
129 	vn_rwfiltops,			/* DTYPE_VNODE */
130 	so_rwfiltops,			/* DTYPE_SOCKET */
131 	pipe_rwfiltops,			/* DTYPE_PIPE */
132 	fifo_rwfiltops,			/* DTYPE_FIFO */
133 	kq_rwfiltops,			/* DTYPE_KQUEUE */
134 };
135 
136 /*
137  * table for for all system-defined filters.
138  */
139 static struct filterops *sysfilt_ops[] = {
140 	&rwtype_filtops,		/* EVFILT_READ */
141 	&rwtype_filtops,		/* EVFILT_WRITE */
142 	&aio_filtops,			/* EVFILT_AIO */
143 	&vn_filtops,			/* EVFILT_VNODE */
144 	&proc_filtops,			/* EVFILT_PROC */
145 	&sig_filtops,			/* EVFILT_SIGNAL */
146 };
147 
148 static int
149 filt_nullattach(struct knote *kn)
150 {
151 	return (ENXIO);
152 }
153 
154 /*
155  * file-type specific attach routine for read/write filters
156  */
157 static int
158 filt_rwtypattach(struct knote *kn)
159 {
160 	struct filterops *fops;
161 
162 	fops = rwtypfilt_sw[kn->kn_fp->f_type];
163 	if (fops == NULL)
164 		return (EINVAL);
165 	kn->kn_fop = &fops[~kn->kn_filter];	/* convert to 0-base index */
166 	return (kn->kn_fop->f_attach(kn));
167 }
168 
169 static int
170 filt_kqattach(struct knote *kn)
171 {
172 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
173 
174 	SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
175 	return (0);
176 }
177 
178 static void
179 filt_kqdetach(struct knote *kn)
180 {
181 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
182 
183 	SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
184 }
185 
186 /*ARGSUSED*/
187 static int
188 filt_kqueue(struct knote *kn, long hint)
189 {
190 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
191 
192 	kn->kn_data = kq->kq_count;
193 	return (kn->kn_data > 0);
194 }
195 
196 static int
197 filt_procattach(struct knote *kn)
198 {
199 	struct proc *p;
200 
201 	p = pfind(kn->kn_id);
202 	if (p == NULL)
203 		return (ESRCH);
204 	if (! PRISON_CHECK(curproc, p))
205 		return (EACCES);
206 
207 	kn->kn_ptr.p_proc = p;
208 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
209 
210 	/*
211 	 * internal flag indicating registration done by kernel
212 	 */
213 	if (kn->kn_flags & EV_FLAG1) {
214 		kn->kn_data = kn->kn_sdata;		/* ppid */
215 		kn->kn_fflags = NOTE_CHILD;
216 		kn->kn_flags &= ~EV_FLAG1;
217 	}
218 
219 	/* XXX lock the proc here while adding to the list? */
220 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
221 
222 	return (0);
223 }
224 
225 /*
226  * The knote may be attached to a different process, which may exit,
227  * leaving nothing for the knote to be attached to.  So when the process
228  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
229  * it will be deleted when read out.  However, as part of the knote deletion,
230  * this routine is called, so a check is needed to avoid actually performing
231  * a detach, because the original process does not exist any more.
232  */
233 static void
234 filt_procdetach(struct knote *kn)
235 {
236 	struct proc *p = kn->kn_ptr.p_proc;
237 
238 	if (kn->kn_status & KN_DETACHED)
239 		return;
240 
241 	/* XXX locking?  this might modify another process. */
242 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
243 }
244 
245 static int
246 filt_proc(struct knote *kn, long hint)
247 {
248 	u_int event;
249 
250 	/*
251 	 * mask off extra data
252 	 */
253 	event = (u_int)hint & NOTE_PCTRLMASK;
254 
255 	/*
256 	 * if the user is interested in this event, record it.
257 	 */
258 	if (kn->kn_sfflags & event)
259 		kn->kn_fflags |= event;
260 
261 	/*
262 	 * process is gone, so flag the event as finished.
263 	 */
264 	if (event == NOTE_EXIT) {
265 		kn->kn_status |= KN_DETACHED;
266 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
267 		return (1);
268 	}
269 
270 	/*
271 	 * process forked, and user wants to track the new process,
272 	 * so attach a new knote to it, and immediately report an
273 	 * event with the parent's pid.
274 	 */
275 	if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
276 		struct kevent kev;
277 		int error;
278 
279 		/*
280 		 * register knote with new process.
281 		 */
282 		kev.ident = hint & NOTE_PDATAMASK;	/* pid */
283 		kev.filter = kn->kn_filter;
284 		kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
285 		kev.fflags = kn->kn_sfflags;
286 		kev.data = kn->kn_id;			/* parent */
287 		error = kqueue_register(kn->kn_kq, &kev, NULL);
288 		if (error)
289 			kn->kn_fflags |= NOTE_TRACKERR;
290 	}
291 
292 	return (kn->kn_fflags != 0);
293 }
294 
295 static int
296 kqueue_create(struct kqueue **kqp)
297 {
298 	struct kqueue *kq;
299 
300 	kq = malloc(sizeof(struct kqueue), M_TEMP, M_WAITOK);
301 	if (kq == NULL)
302 		return (EAGAIN);
303 	bzero(kq, sizeof(*kq));
304 	TAILQ_INIT(&kq->kq_head);
305 
306 	*kqp = kq;
307 	return (0);
308 }
309 
310 int
311 kqueue(struct proc *p, struct kqueue_args *uap)
312 {
313         struct filedesc *fdp = p->p_fd;
314         struct kqueue *kq;
315         struct file *fp;
316         int fd, error;
317 
318         error = falloc(p, &fp, &fd);
319         if (error)
320                 return (error);
321         fp->f_flag = FREAD | FWRITE;
322         fp->f_type = DTYPE_KQUEUE;
323         fp->f_ops = &kqueueops;
324         error = kqueue_create(&kq);
325         if (error) {
326                 fdp->fd_ofiles[fd] = 0;
327                 ffree(fp);
328         } else {
329                 fp->f_data = (caddr_t)kq;
330                 p->p_retval[0] = fd;
331         }
332 	fdp->fd_knlistsize = 0;		/* mark this fdesc as having a kq */
333 	kq->kq_fdp = fdp;
334         return (error);
335 }
336 
337 #ifndef _SYS_SYSPROTO_H_
338 struct kevent_args {
339 	int	fd;
340 	int	nchanges;
341 	struct	kevent **changelist;
342 	int	nevents;
343 	struct	kevent *eventlist;
344 	struct	timespec *timeout;
345 };
346 #endif
347 int
348 kevent(struct proc *p, struct kevent_args *uap)
349 {
350         struct filedesc* fdp = p->p_fd;
351 	struct kevent kev;
352 	struct kqueue *kq;
353         struct file *fp;
354 	struct timespec ts;
355 	int i, n, nerrors, error;
356 
357         if (((u_int)uap->fd) >= fdp->fd_nfiles ||
358             (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
359 	    (fp->f_type != DTYPE_KQUEUE))
360 		return (EBADF);
361 
362 	if (uap->timeout != NULL) {
363 		error = copyin((caddr_t)uap->timeout, (caddr_t)&ts,
364 		    sizeof(ts));
365 		if (error)
366 			return error;
367 		uap->timeout = &ts;
368 	}
369 
370 	kq = (struct kqueue *)fp->f_data;
371 	nerrors = 0;
372 
373 	while (uap->nchanges > 0) {
374 		n = uap->nchanges > KQ_NEVENTS ? KQ_NEVENTS : uap->nchanges;
375 		error = copyin((caddr_t)uap->changelist, (caddr_t)kq->kq_kevp,
376 		    n * sizeof(struct kevent *));
377 		if (error)
378 			return (error);
379 		for (i = 0; i < n; i++) {
380 			error = copyin((caddr_t)kq->kq_kevp[i],
381 			    (caddr_t)&kev, sizeof(kev));
382 			if (error)
383 				return (error);
384 			kev.flags &= ~EV_SYSFLAGS;
385 			error = kqueue_register(kq, &kev, p);
386 			if (error) {
387 				if (uap->nevents != 0) {
388 					kev.flags = EV_ERROR;
389 					kev.data = error;
390 					(void) copyout((caddr_t)&kev,
391 					    (caddr_t)uap->eventlist,
392 					    sizeof(kev));
393 					uap->eventlist++;
394 					uap->nevents--;
395 					nerrors++;
396 				} else {
397 					return (error);
398 				}
399 			}
400 		}
401 		uap->nchanges -= n;
402 		uap->changelist += n;
403 	}
404 	if (nerrors) {
405         	p->p_retval[0] = nerrors;
406 		return (0);
407 	}
408 
409 	error = kqueue_scan(fp, uap->nevents, uap->eventlist, uap->timeout, p);
410 	return (error);
411 }
412 
413 int
414 kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p)
415 {
416 	struct filedesc *fdp = kq->kq_fdp;
417 	struct filterops *fops;
418 	struct file *fp = NULL;
419 	struct knote *kn = NULL;
420 	int s, error = 0;
421 
422 	if (kev->filter < 0) {
423 		if (kev->filter + EVFILT_SYSCOUNT < 0)
424 			return (EINVAL);
425 		fops = sysfilt_ops[~kev->filter];	/* to 0-base index */
426 	} else {
427 		/*
428 		 * XXX
429 		 * filter attach routine is responsible for insuring that
430 		 * the identifier can be attached to it.
431 		 */
432 		printf("unknown filter: %d\n", kev->filter);
433 		return (EINVAL);
434 	}
435 
436 	if (fops->f_isfd) {
437 		/* validate descriptor; ignore invalid descriptors */
438 		if ((u_int)kev->ident >= fdp->fd_nfiles ||
439 		    (fp = fdp->fd_ofiles[kev->ident]) == NULL)
440 			return (0);
441 
442 		if (kev->ident < fdp->fd_knlistsize) {
443 			SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
444 				if (kq == kn->kn_kq &&
445 				    kev->filter == kn->kn_filter)
446 					break;
447 		}
448 	} else {
449 		if (fdp->fd_knhashmask != 0) {
450 			struct klist *list;
451 
452 			list = &fdp->fd_knhash[
453 			    KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
454 			SLIST_FOREACH(kn, list, kn_link)
455 				if (kev->ident == kn->kn_id &&
456 				    kq == kn->kn_kq &&
457 				    kev->filter == kn->kn_filter)
458 					break;
459 		}
460 	}
461 
462 	if (kn == NULL && ((kev->flags & EV_ADD) == 0))
463 		goto done;
464 
465 	/*
466 	 * kn now contains the matching knote, or NULL if no match
467 	 */
468 	if (kev->flags & EV_ADD) {
469 		int attach = 0;
470 
471 		if (kn == NULL) {
472 			kn = knote_alloc();
473 			if (kn == NULL)
474 				return (ENOMEM);
475 			if (fp != NULL)
476 				fhold(fp);
477 			kn->kn_fp = fp;
478 			kn->kn_kq = kq;
479 			kn->kn_fop = fops;
480 			attach = 1;
481 		}
482 		kn->kn_sfflags = kev->fflags;
483 		kn->kn_sdata = kev->data;
484 		kev->fflags = 0;
485 		kev->data = 0;
486 		kn->kn_kevent = *kev;
487 
488 		if (attach) {
489 			knote_attach(kn, fdp);
490 			if ((error = fops->f_attach(kn)) != 0) {
491 				knote_drop(kn, p);
492 				goto done;
493 			}
494 		}
495 		s = splhigh();
496 		if (kn->kn_fop->f_event(kn, 0))
497 			KNOTE_ACTIVATE(kn);
498 		splx(s);
499 	} else if (kev->flags & EV_DELETE) {
500 		kn->kn_fop->f_detach(kn);
501 		knote_drop(kn, p);
502 		goto done;
503 	}
504 
505 	if ((kev->flags & EV_DISABLE) &&
506 	    ((kn->kn_status & KN_DISABLED) == 0)) {
507 		s = splhigh();
508 		kn->kn_status |= KN_DISABLED;
509 		splx(s);
510 	}
511 
512 	if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
513 		s = splhigh();
514 		kn->kn_status &= ~KN_DISABLED;
515 		if ((kn->kn_status & KN_ACTIVE) &&
516 		    ((kn->kn_status & KN_QUEUED) == 0))
517 			knote_enqueue(kn);
518 		splx(s);
519 	}
520 
521 done:
522 	return (error);
523 }
524 
525 static int
526 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
527 	struct timespec *tsp, struct proc *p)
528 {
529 	struct kqueue *kq = (struct kqueue *)fp->f_data;
530 	struct kevent *kevp;
531 	struct timeval atv, rtv, ttv;
532 	struct knote *kn, marker;
533 	int s, count, timeout, nkev = 0, error = 0;
534 
535 	count = maxevents;
536 	if (count == 0)
537 		goto done;
538 
539         if (tsp != NULL) {
540 		TIMESPEC_TO_TIMEVAL(&atv, tsp);
541                 if (itimerfix(&atv)) {
542 			error = EINVAL;
543 			goto done;
544 		}
545 		timeout = atv.tv_sec > 24 * 60 * 60 ?
546 			24 * 60 * 60 * hz : tvtohz(&atv);
547                 getmicrouptime(&rtv);
548                 timevaladd(&atv, &rtv);
549         } else {
550                 atv.tv_sec = 0;
551 		timeout = 0;
552 	}
553 	goto start;
554 
555 retry:
556 	if (atv.tv_sec) {
557 		getmicrouptime(&rtv);
558 		if (timevalcmp(&rtv, &atv, >=))
559 			goto done;
560 		ttv = atv;
561 		timevalsub(&ttv, &rtv);
562 		timeout = ttv.tv_sec > 24 * 60 * 60 ?
563 			24 * 60 * 60 * hz : tvtohz(&ttv);
564 	}
565 
566 start:
567 	kevp = kq->kq_kev;
568 	s = splhigh();
569 	if (kq->kq_count == 0) {
570 		kq->kq_state |= KQ_SLEEP;
571 		error = tsleep(kq, PSOCK | PCATCH, "kqread", timeout);
572 		splx(s);
573 		if (error == 0)
574 			goto retry;
575 		/* don't restart after signals... */
576 		if (error == ERESTART)
577 			error = EINTR;
578 		else if (error == EWOULDBLOCK)
579 			error = 0;
580 		goto done;
581 	}
582 
583 	TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
584 	while (count) {
585 		kn = TAILQ_FIRST(&kq->kq_head);
586 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
587 		if (kn == &marker) {
588 			splx(s);
589 			if (count == maxevents)
590 				goto retry;
591 			goto done;
592 		}
593 		if (kn->kn_status & KN_DISABLED) {
594 			kn->kn_status &= ~KN_QUEUED;
595 			kq->kq_count--;
596 			continue;
597 		}
598 		if ((kn->kn_flags & EV_ONESHOT) == 0 &&
599 		    kn->kn_fop->f_event(kn, 0) == 0) {
600 			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
601 			kq->kq_count--;
602 			continue;
603 		}
604 		*kevp = kn->kn_kevent;
605 		kevp++;
606 		nkev++;
607 		if (kn->kn_flags & EV_ONESHOT) {
608 			kn->kn_status &= ~KN_QUEUED;
609 			kq->kq_count--;
610 			splx(s);
611 			kn->kn_fop->f_detach(kn);
612 			knote_drop(kn, p);
613 			s = splhigh();
614 		} else if (kn->kn_flags & EV_CLEAR) {
615 			kn->kn_data = 0;
616 			kn->kn_fflags = 0;
617 			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
618 			kq->kq_count--;
619 		} else {
620 			TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
621 		}
622 		count--;
623 		if (nkev == KQ_NEVENTS) {
624 			splx(s);
625 			error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
626 			    sizeof(struct kevent) * nkev);
627 			ulistp += nkev;
628 			nkev = 0;
629 			kevp = kq->kq_kev;
630 			s = splhigh();
631 			if (error)
632 				break;
633 		}
634 	}
635 	TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
636 	splx(s);
637 done:
638 	if (nkev != 0)
639 		error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
640 		    sizeof(struct kevent) * nkev);
641         p->p_retval[0] = maxevents - count;
642 	return (error);
643 }
644 
645 /*
646  * XXX
647  * This could be expanded to call kqueue_scan, if desired.
648  */
649 /*ARGSUSED*/
650 static int
651 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred,
652 	int flags, struct proc *p)
653 {
654 	return (ENXIO);
655 }
656 
657 /*ARGSUSED*/
658 static int
659 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred,
660 	 int flags, struct proc *p)
661 {
662 	return (ENXIO);
663 }
664 
665 /*ARGSUSED*/
666 static int
667 kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p)
668 {
669 	return (ENOTTY);
670 }
671 
672 /*ARGSUSED*/
673 static int
674 kqueue_poll(struct file *fp, int events, struct ucred *cred, struct proc *p)
675 {
676 	struct kqueue *kq = (struct kqueue *)fp->f_data;
677 	int revents = 0;
678 	int s = splnet();
679 
680         if (events & (POLLIN | POLLRDNORM)) {
681                 if (kq->kq_count) {
682                         revents |= events & (POLLIN | POLLRDNORM);
683 		} else {
684                         selrecord(p, &kq->kq_sel);
685 			kq->kq_state |= KQ_SEL;
686 		}
687 	}
688 	splx(s);
689 	return (revents);
690 }
691 
692 /*ARGSUSED*/
693 static int
694 kqueue_stat(struct file *fp, struct stat *st, struct proc *p)
695 {
696 	struct kqueue *kq = (struct kqueue *)fp->f_data;
697 
698 	bzero((void *)st, sizeof(*st));
699 	st->st_size = kq->kq_count;
700 	st->st_blksize = sizeof(struct kevent);
701 	return (0);
702 }
703 
704 /*ARGSUSED*/
705 static int
706 kqueue_close(struct file *fp, struct proc *p)
707 {
708 	struct kqueue *kq = (struct kqueue *)fp->f_data;
709 	struct filedesc *fdp = p->p_fd;
710 	struct knote **knp, *kn, *kn0;
711 	int i;
712 
713 	for (i = 0; i < fdp->fd_knlistsize; i++) {
714 		knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
715 		kn = *knp;
716 		while (kn != NULL) {
717 			kn0 = SLIST_NEXT(kn, kn_link);
718 			if (kq == kn->kn_kq) {
719 				kn->kn_fop->f_detach(kn);
720 				fdrop(kn->kn_fp, p);
721 				knote_free(kn);
722 				*knp = kn0;
723 			} else {
724 				knp = &SLIST_NEXT(kn, kn_link);
725 			}
726 			kn = kn0;
727 		}
728 	}
729 	if (fdp->fd_knhashmask != 0) {
730 		for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
731 			knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
732 			kn = *knp;
733 			while (kn != NULL) {
734 				kn0 = SLIST_NEXT(kn, kn_link);
735 				if (kq == kn->kn_kq) {
736 					kn->kn_fop->f_detach(kn);
737 		/* XXX non-fd release of kn->kn_ptr */
738 					knote_free(kn);
739 					*knp = kn0;
740 				} else {
741 					knp = &SLIST_NEXT(kn, kn_link);
742 				}
743 				kn = kn0;
744 			}
745 		}
746 	}
747 	free(kq, M_TEMP);
748 	fp->f_data = NULL;
749 
750 	return (0);
751 }
752 
753 static void
754 kqueue_wakeup(struct kqueue *kq)
755 {
756 
757 	if (kq->kq_state & KQ_SLEEP) {
758 		kq->kq_state &= ~KQ_SLEEP;
759 		wakeup(kq);
760 	}
761 	if (kq->kq_state & KQ_SEL) {
762 		kq->kq_state &= ~KQ_SEL;
763 		selwakeup(&kq->kq_sel);
764 	}
765 	KNOTE(&kq->kq_sel.si_note, 0);
766 }
767 
768 /*
769  * walk down a list of knotes, activating them if their event has triggered.
770  */
771 void
772 knote(struct klist *list, long hint)
773 {
774 	struct knote *kn;
775 
776 	SLIST_FOREACH(kn, list, kn_selnext)
777 		if (kn->kn_fop->f_event(kn, hint))
778 			KNOTE_ACTIVATE(kn);
779 }
780 
781 /*
782  * remove all knotes from a specified klist
783  */
784 void
785 knote_remove(struct proc *p, struct klist *list)
786 {
787 	struct knote *kn;
788 
789 	while ((kn = SLIST_FIRST(list)) != NULL) {
790 		kn->kn_fop->f_detach(kn);
791 		knote_drop(kn, p);
792 	}
793 }
794 
795 /*
796  * remove all knotes referencing a specified fd
797  */
798 void
799 knote_fdclose(struct proc *p, int fd)
800 {
801 	struct filedesc *fdp = p->p_fd;
802 	struct klist *list = &fdp->fd_knlist[fd];
803 
804 	knote_remove(p, list);
805 }
806 
807 static void
808 knote_attach(struct knote *kn, struct filedesc *fdp)
809 {
810 	struct klist *list;
811 	int size;
812 
813 	if (! kn->kn_fop->f_isfd) {
814 		if (fdp->fd_knhashmask == 0)
815 			fdp->fd_knhash = hashinit(KN_HASHSIZE, M_TEMP,
816 			    &fdp->fd_knhashmask);
817 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
818 		goto done;
819 	}
820 
821 	if (fdp->fd_knlistsize <= kn->kn_id) {
822 		size = fdp->fd_knlistsize;
823 		while (size <= kn->kn_id)
824 			size += KQEXTENT;
825 		MALLOC(list, struct klist *,
826 		    size * sizeof(struct klist *), M_TEMP, M_WAITOK);
827 		bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
828 		    fdp->fd_knlistsize * sizeof(struct klist *));
829 		bzero((caddr_t)list +
830 		    fdp->fd_knlistsize * sizeof(struct klist *),
831 		    (size - fdp->fd_knlistsize) * sizeof(struct klist *));
832 		if (fdp->fd_knlist != NULL)
833 			FREE(fdp->fd_knlist, M_TEMP);
834 		fdp->fd_knlistsize = size;
835 		fdp->fd_knlist = list;
836 	}
837 	list = &fdp->fd_knlist[kn->kn_id];
838 done:
839 	SLIST_INSERT_HEAD(list, kn, kn_link);
840 	kn->kn_status = 0;
841 }
842 
843 /*
844  * should be called at spl == 0, since we don't want to hold spl
845  * while calling fdrop and free.
846  */
847 static void
848 knote_drop(struct knote *kn, struct proc *p)
849 {
850         struct filedesc *fdp = p->p_fd;
851 	struct klist *list;
852 
853 	if (kn->kn_fop->f_isfd)
854 		list = &fdp->fd_knlist[kn->kn_id];
855 	else
856 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
857 
858 	SLIST_REMOVE(list, kn, knote, kn_link);
859 	if (kn->kn_status & KN_QUEUED)
860 		knote_dequeue(kn);
861 	if (kn->kn_fop->f_isfd)
862 		fdrop(kn->kn_fp, p);
863 	knote_free(kn);
864 }
865 
866 
867 static void
868 knote_enqueue(struct knote *kn)
869 {
870 	struct kqueue *kq = kn->kn_kq;
871 	int s = splhigh();
872 
873 	KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
874 
875 	TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
876 	kn->kn_status |= KN_QUEUED;
877 	kq->kq_count++;
878 	splx(s);
879 	kqueue_wakeup(kq);
880 }
881 
882 static void
883 knote_dequeue(struct knote *kn)
884 {
885 	struct kqueue *kq = kn->kn_kq;
886 	int s = splhigh();
887 
888 	KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
889 
890 	TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
891 	kn->kn_status &= ~KN_QUEUED;
892 	kq->kq_count--;
893 	splx(s);
894 }
895 
896 static void
897 knote_init(void)
898 {
899 	knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
900 }
901 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
902 
903 static struct knote *
904 knote_alloc(void)
905 {
906 	return ((struct knote *)zalloc(knote_zone));
907 }
908 
909 static void
910 knote_free(struct knote *kn)
911 {
912 	zfree(knote_zone, kn);
913 }
914