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