xref: /freebsd/sys/kern/kern_event.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 		if (kn->kn_fop->f_event(kn, 0))
496 			KNOTE_ACTIVATE(kn);
497 	} else if (kev->flags & EV_DELETE) {
498 		kn->kn_fop->f_detach(kn);
499 		knote_drop(kn, p);
500 		goto done;
501 	}
502 
503 	if ((kev->flags & EV_DISABLE) &&
504 	    ((kn->kn_status & KN_DISABLED) == 0)) {
505 		s = splhigh();
506 		kn->kn_status |= KN_DISABLED;
507 		splx(s);
508 	}
509 
510 	if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
511 		s = splhigh();
512 		kn->kn_status &= ~KN_DISABLED;
513 		if ((kn->kn_status & KN_ACTIVE) &&
514 		    ((kn->kn_status & KN_QUEUED) == 0))
515 			knote_enqueue(kn);
516 		splx(s);
517 	}
518 
519 done:
520 	return (error);
521 }
522 
523 static int
524 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
525 	struct timespec *tsp, struct proc *p)
526 {
527 	struct kqueue *kq = (struct kqueue *)fp->f_data;
528 	struct kevent *kevp;
529 	struct timeval atv, rtv, ttv;
530 	struct knote *kn, marker;
531 	int s, count, timeout, nkev = 0, error = 0;
532 
533 	count = maxevents;
534 	if (count == 0)
535 		goto done;
536 
537         if (tsp != NULL) {
538 		TIMESPEC_TO_TIMEVAL(&atv, tsp);
539                 if (itimerfix(&atv)) {
540 			error = EINVAL;
541 			goto done;
542 		}
543 		timeout = atv.tv_sec > 24 * 60 * 60 ?
544 			24 * 60 * 60 * hz : tvtohz(&atv);
545                 getmicrouptime(&rtv);
546                 timevaladd(&atv, &rtv);
547         } else {
548                 atv.tv_sec = 0;
549 		timeout = 0;
550 	}
551 	goto start;
552 
553 retry:
554 	if (atv.tv_sec) {
555 		getmicrouptime(&rtv);
556 		if (timevalcmp(&rtv, &atv, >=))
557 			goto done;
558 		ttv = atv;
559 		timevalsub(&ttv, &rtv);
560 		timeout = ttv.tv_sec > 24 * 60 * 60 ?
561 			24 * 60 * 60 * hz : tvtohz(&ttv);
562 	}
563 
564 start:
565 	kevp = kq->kq_kev;
566 	s = splhigh();
567 	if (kq->kq_count == 0) {
568 		kq->kq_state |= KQ_SLEEP;
569 		error = tsleep(kq, PSOCK | PCATCH, "kqread", timeout);
570 		splx(s);
571 		if (error == 0)
572 			goto retry;
573 		/* don't restart after signals... */
574 		if (error == ERESTART)
575 			error = EINTR;
576 		else if (error == EWOULDBLOCK)
577 			error = 0;
578 		goto done;
579 	}
580 
581 	TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
582 	while (count) {
583 		kn = TAILQ_FIRST(&kq->kq_head);
584 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
585 		if (kn == &marker) {
586 			splx(s);
587 			if (count == maxevents)
588 				goto retry;
589 			goto done;
590 		}
591 		if (kn->kn_status & KN_DISABLED) {
592 			kn->kn_status &= ~KN_QUEUED;
593 			kq->kq_count--;
594 			continue;
595 		}
596 		if ((kn->kn_flags & EV_ONESHOT) == 0 &&
597 		    kn->kn_fop->f_event(kn, 0) == 0) {
598 			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
599 			kq->kq_count--;
600 			continue;
601 		}
602 		*kevp = kn->kn_kevent;
603 		kevp++;
604 		nkev++;
605 		if (kn->kn_flags & EV_ONESHOT) {
606 			kn->kn_status &= ~KN_QUEUED;
607 			kq->kq_count--;
608 			splx(s);
609 			kn->kn_fop->f_detach(kn);
610 			knote_drop(kn, p);
611 			s = splhigh();
612 		} else if (kn->kn_flags & EV_CLEAR) {
613 			kn->kn_data = 0;
614 			kn->kn_fflags = 0;
615 			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
616 			kq->kq_count--;
617 		} else {
618 			TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
619 		}
620 		count--;
621 		if (nkev == KQ_NEVENTS) {
622 			splx(s);
623 			error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
624 			    sizeof(struct kevent) * nkev);
625 			if (error)
626 				break;
627 			ulistp += nkev;
628 			nkev = 0;
629 			kevp = kq->kq_kev;
630 			s = splhigh();
631 		}
632 	}
633 	TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
634 	splx(s);
635 done:
636 	if (nkev != 0)
637 		error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
638 		    sizeof(struct kevent) * nkev);
639         p->p_retval[0] = maxevents - count;
640 	return (error);
641 }
642 
643 /*
644  * XXX
645  * This could be expanded to call kqueue_scan, if desired.
646  */
647 /*ARGSUSED*/
648 static int
649 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred,
650 	int flags, struct proc *p)
651 {
652 	return (ENXIO);
653 }
654 
655 /*ARGSUSED*/
656 static int
657 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred,
658 	 int flags, struct proc *p)
659 {
660 	return (ENXIO);
661 }
662 
663 /*ARGSUSED*/
664 static int
665 kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p)
666 {
667 	return (ENOTTY);
668 }
669 
670 /*ARGSUSED*/
671 static int
672 kqueue_poll(struct file *fp, int events, struct ucred *cred, struct proc *p)
673 {
674 	struct kqueue *kq = (struct kqueue *)fp->f_data;
675 	int revents = 0;
676 	int s = splnet();
677 
678         if (events & (POLLIN | POLLRDNORM)) {
679                 if (kq->kq_count) {
680                         revents |= events & (POLLIN | POLLRDNORM);
681 		} else {
682                         selrecord(p, &kq->kq_sel);
683 			kq->kq_state |= KQ_SEL;
684 		}
685 	}
686 	splx(s);
687 	return (revents);
688 }
689 
690 /*ARGSUSED*/
691 static int
692 kqueue_stat(struct file *fp, struct stat *st, struct proc *p)
693 {
694 	struct kqueue *kq = (struct kqueue *)fp->f_data;
695 
696 	bzero((void *)st, sizeof(*st));
697 	st->st_size = kq->kq_count;
698 	st->st_blksize = sizeof(struct kevent);
699 	return (0);
700 }
701 
702 /*ARGSUSED*/
703 static int
704 kqueue_close(struct file *fp, struct proc *p)
705 {
706 	struct kqueue *kq = (struct kqueue *)fp->f_data;
707 	struct filedesc *fdp = p->p_fd;
708 	struct knote **knp, *kn, *kn0;
709 	int i;
710 
711 	for (i = 0; i < fdp->fd_knlistsize; i++) {
712 		knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
713 		kn = *knp;
714 		while (kn != NULL) {
715 			kn0 = SLIST_NEXT(kn, kn_link);
716 			if (kq == kn->kn_kq) {
717 				kn->kn_fop->f_detach(kn);
718 				fdrop(kn->kn_fp, p);
719 				knote_free(kn);
720 				*knp = kn0;
721 			} else {
722 				knp = &SLIST_NEXT(kn, kn_link);
723 			}
724 			kn = kn0;
725 		}
726 	}
727 	if (fdp->fd_knhashmask != 0) {
728 		for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
729 			knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
730 			kn = *knp;
731 			while (kn != NULL) {
732 				kn0 = SLIST_NEXT(kn, kn_link);
733 				if (kq == kn->kn_kq) {
734 					kn->kn_fop->f_detach(kn);
735 		/* XXX non-fd release of kn->kn_ptr */
736 					knote_free(kn);
737 					*knp = kn0;
738 				} else {
739 					knp = &SLIST_NEXT(kn, kn_link);
740 				}
741 				kn = kn0;
742 			}
743 		}
744 	}
745 	free(kq, M_TEMP);
746 	fp->f_data = NULL;
747 
748 	return (0);
749 }
750 
751 static void
752 kqueue_wakeup(struct kqueue *kq)
753 {
754 
755 	if (kq->kq_state & KQ_SLEEP) {
756 		kq->kq_state &= ~KQ_SLEEP;
757 		wakeup(kq);
758 	}
759 	if (kq->kq_state & KQ_SEL) {
760 		kq->kq_state &= ~KQ_SEL;
761 		selwakeup(&kq->kq_sel);
762 	}
763 	KNOTE(&kq->kq_sel.si_note, 0);
764 }
765 
766 /*
767  * walk down a list of knotes, activating them if their event has triggered.
768  */
769 void
770 knote(struct klist *list, long hint)
771 {
772 	struct knote *kn;
773 
774 	SLIST_FOREACH(kn, list, kn_selnext)
775 		if (kn->kn_fop->f_event(kn, hint))
776 			KNOTE_ACTIVATE(kn);
777 }
778 
779 /*
780  * remove all knotes from a specified klist
781  */
782 void
783 knote_remove(struct proc *p, struct klist *list)
784 {
785 	struct knote *kn;
786 
787 	while ((kn = SLIST_FIRST(list)) != NULL) {
788 		kn->kn_fop->f_detach(kn);
789 		knote_drop(kn, p);
790 	}
791 }
792 
793 /*
794  * remove all knotes referencing a specified fd
795  */
796 void
797 knote_fdclose(struct proc *p, int fd)
798 {
799 	struct filedesc *fdp = p->p_fd;
800 	struct klist *list = &fdp->fd_knlist[fd];
801 
802 	knote_remove(p, list);
803 }
804 
805 static void
806 knote_attach(struct knote *kn, struct filedesc *fdp)
807 {
808 	struct klist *list;
809 	int size;
810 
811 	if (! kn->kn_fop->f_isfd) {
812 		if (fdp->fd_knhashmask == 0)
813 			fdp->fd_knhash = hashinit(KN_HASHSIZE, M_TEMP,
814 			    &fdp->fd_knhashmask);
815 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
816 		goto done;
817 	}
818 
819 	if (fdp->fd_knlistsize <= kn->kn_id) {
820 		size = fdp->fd_knlistsize;
821 		while (size <= kn->kn_id)
822 			size += KQEXTENT;
823 		MALLOC(list, struct klist *,
824 		    size * sizeof(struct klist *), M_TEMP, M_WAITOK);
825 		bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
826 		    fdp->fd_knlistsize * sizeof(struct klist *));
827 		bzero((caddr_t)list +
828 		    fdp->fd_knlistsize * sizeof(struct klist *),
829 		    (size - fdp->fd_knlistsize) * sizeof(struct klist *));
830 		if (fdp->fd_knlist != NULL)
831 			FREE(fdp->fd_knlist, M_TEMP);
832 		fdp->fd_knlistsize = size;
833 		fdp->fd_knlist = list;
834 	}
835 	list = &fdp->fd_knlist[kn->kn_id];
836 done:
837 	SLIST_INSERT_HEAD(list, kn, kn_link);
838 	kn->kn_status = 0;
839 }
840 
841 /*
842  * should be called at spl == 0, since we don't want to hold spl
843  * while calling fdrop and free.
844  */
845 static void
846 knote_drop(struct knote *kn, struct proc *p)
847 {
848         struct filedesc *fdp = p->p_fd;
849 	struct klist *list;
850 
851 	if (kn->kn_fop->f_isfd)
852 		list = &fdp->fd_knlist[kn->kn_id];
853 	else
854 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
855 
856 	SLIST_REMOVE(list, kn, knote, kn_link);
857 	if (kn->kn_status & KN_QUEUED)
858 		knote_dequeue(kn);
859 	if (kn->kn_fop->f_isfd)
860 		fdrop(kn->kn_fp, p);
861 	knote_free(kn);
862 }
863 
864 
865 static void
866 knote_enqueue(struct knote *kn)
867 {
868 	struct kqueue *kq = kn->kn_kq;
869 	int s = splhigh();
870 
871 	TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
872 	kn->kn_status |= KN_QUEUED;
873 	kq->kq_count++;
874 	splx(s);
875 	kqueue_wakeup(kq);
876 }
877 
878 static void
879 knote_dequeue(struct knote *kn)
880 {
881 	struct kqueue *kq = kn->kn_kq;
882 	int s = splhigh();
883 
884 	TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
885 	kn->kn_status &= ~KN_QUEUED;
886 	kq->kq_count--;
887 	splx(s);
888 }
889 
890 static void
891 knote_init(void)
892 {
893 	knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
894 }
895 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
896 
897 static struct knote *
898 knote_alloc(void)
899 {
900 	return ((struct knote *)zalloc(knote_zone));
901 }
902 
903 static void
904 knote_free(struct knote *kn)
905 {
906 	zfree(knote_zone, kn);
907 }
908