kern_event.c revision 1.122 1 1.122 thorpej /* $NetBSD: kern_event.c,v 1.122 2021/09/26 03:12:50 thorpej Exp $ */
2 1.49 ad
3 1.49 ad /*-
4 1.64 ad * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.49 ad * All rights reserved.
6 1.49 ad *
7 1.64 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.64 ad * by Andrew Doran.
9 1.64 ad *
10 1.49 ad * Redistribution and use in source and binary forms, with or without
11 1.49 ad * modification, are permitted provided that the following conditions
12 1.49 ad * are met:
13 1.49 ad * 1. Redistributions of source code must retain the above copyright
14 1.49 ad * notice, this list of conditions and the following disclaimer.
15 1.49 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.49 ad * notice, this list of conditions and the following disclaimer in the
17 1.49 ad * documentation and/or other materials provided with the distribution.
18 1.49 ad *
19 1.49 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.49 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.49 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.49 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.49 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.49 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.49 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.49 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.49 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.49 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.49 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.49 ad */
31 1.28 kardel
32 1.1 lukem /*-
33 1.1 lukem * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon (at) FreeBSD.org>
34 1.108 christos * Copyright (c) 2009 Apple, Inc
35 1.1 lukem * All rights reserved.
36 1.1 lukem *
37 1.1 lukem * Redistribution and use in source and binary forms, with or without
38 1.1 lukem * modification, are permitted provided that the following conditions
39 1.1 lukem * are met:
40 1.1 lukem * 1. Redistributions of source code must retain the above copyright
41 1.1 lukem * notice, this list of conditions and the following disclaimer.
42 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 lukem * notice, this list of conditions and the following disclaimer in the
44 1.1 lukem * documentation and/or other materials provided with the distribution.
45 1.1 lukem *
46 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1 lukem * SUCH DAMAGE.
57 1.1 lukem *
58 1.49 ad * FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp
59 1.1 lukem */
60 1.14 jdolecek
61 1.14 jdolecek #include <sys/cdefs.h>
62 1.122 thorpej __KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.122 2021/09/26 03:12:50 thorpej Exp $");
63 1.1 lukem
64 1.1 lukem #include <sys/param.h>
65 1.1 lukem #include <sys/systm.h>
66 1.1 lukem #include <sys/kernel.h>
67 1.86 christos #include <sys/wait.h>
68 1.1 lukem #include <sys/proc.h>
69 1.1 lukem #include <sys/file.h>
70 1.3 jdolecek #include <sys/select.h>
71 1.1 lukem #include <sys/queue.h>
72 1.1 lukem #include <sys/event.h>
73 1.1 lukem #include <sys/eventvar.h>
74 1.1 lukem #include <sys/poll.h>
75 1.49 ad #include <sys/kmem.h>
76 1.1 lukem #include <sys/stat.h>
77 1.3 jdolecek #include <sys/filedesc.h>
78 1.3 jdolecek #include <sys/syscallargs.h>
79 1.27 elad #include <sys/kauth.h>
80 1.40 ad #include <sys/conf.h>
81 1.49 ad #include <sys/atomic.h>
82 1.1 lukem
83 1.49 ad static int kqueue_scan(file_t *, size_t, struct kevent *,
84 1.49 ad const struct timespec *, register_t *,
85 1.49 ad const struct kevent_ops *, struct kevent *,
86 1.49 ad size_t);
87 1.49 ad static int kqueue_ioctl(file_t *, u_long, void *);
88 1.49 ad static int kqueue_fcntl(file_t *, u_int, void *);
89 1.49 ad static int kqueue_poll(file_t *, int);
90 1.49 ad static int kqueue_kqfilter(file_t *, struct knote *);
91 1.49 ad static int kqueue_stat(file_t *, struct stat *);
92 1.49 ad static int kqueue_close(file_t *);
93 1.118 jdolecek static void kqueue_restart(file_t *);
94 1.49 ad static int kqueue_register(struct kqueue *, struct kevent *);
95 1.49 ad static void kqueue_doclose(struct kqueue *, struct klist *, int);
96 1.49 ad
97 1.49 ad static void knote_detach(struct knote *, filedesc_t *fdp, bool);
98 1.49 ad static void knote_enqueue(struct knote *);
99 1.49 ad static void knote_activate(struct knote *);
100 1.49 ad
101 1.49 ad static void filt_kqdetach(struct knote *);
102 1.49 ad static int filt_kqueue(struct knote *, long hint);
103 1.49 ad static int filt_procattach(struct knote *);
104 1.49 ad static void filt_procdetach(struct knote *);
105 1.49 ad static int filt_proc(struct knote *, long hint);
106 1.49 ad static int filt_fileattach(struct knote *);
107 1.49 ad static void filt_timerexpire(void *x);
108 1.49 ad static int filt_timerattach(struct knote *);
109 1.49 ad static void filt_timerdetach(struct knote *);
110 1.49 ad static int filt_timer(struct knote *, long hint);
111 1.102 christos static int filt_fsattach(struct knote *kn);
112 1.102 christos static void filt_fsdetach(struct knote *kn);
113 1.102 christos static int filt_fs(struct knote *kn, long hint);
114 1.108 christos static int filt_userattach(struct knote *);
115 1.108 christos static void filt_userdetach(struct knote *);
116 1.108 christos static int filt_user(struct knote *, long hint);
117 1.108 christos static void filt_usertouch(struct knote *, struct kevent *, long type);
118 1.1 lukem
119 1.21 christos static const struct fileops kqueueops = {
120 1.101 christos .fo_name = "kqueue",
121 1.64 ad .fo_read = (void *)enxio,
122 1.64 ad .fo_write = (void *)enxio,
123 1.64 ad .fo_ioctl = kqueue_ioctl,
124 1.64 ad .fo_fcntl = kqueue_fcntl,
125 1.64 ad .fo_poll = kqueue_poll,
126 1.64 ad .fo_stat = kqueue_stat,
127 1.64 ad .fo_close = kqueue_close,
128 1.64 ad .fo_kqfilter = kqueue_kqfilter,
129 1.118 jdolecek .fo_restart = kqueue_restart,
130 1.1 lukem };
131 1.1 lukem
132 1.96 maya static const struct filterops kqread_filtops = {
133 1.121 thorpej .f_flags = FILTEROP_ISFD,
134 1.96 maya .f_attach = NULL,
135 1.96 maya .f_detach = filt_kqdetach,
136 1.96 maya .f_event = filt_kqueue,
137 1.96 maya };
138 1.96 maya
139 1.96 maya static const struct filterops proc_filtops = {
140 1.121 thorpej .f_flags = 0,
141 1.96 maya .f_attach = filt_procattach,
142 1.96 maya .f_detach = filt_procdetach,
143 1.96 maya .f_event = filt_proc,
144 1.96 maya };
145 1.96 maya
146 1.122 thorpej /*
147 1.122 thorpej * file_filtops is not marked MPSAFE because it's going to call
148 1.122 thorpej * fileops::fo_kqfilter(), which might not be. That function,
149 1.122 thorpej * however, will override the knote's filterops, and thus will
150 1.122 thorpej * inherit the MPSAFE-ness of the back-end at that time.
151 1.122 thorpej */
152 1.96 maya static const struct filterops file_filtops = {
153 1.121 thorpej .f_flags = FILTEROP_ISFD,
154 1.96 maya .f_attach = filt_fileattach,
155 1.96 maya .f_detach = NULL,
156 1.96 maya .f_event = NULL,
157 1.96 maya };
158 1.96 maya
159 1.96 maya static const struct filterops timer_filtops = {
160 1.121 thorpej .f_flags = 0,
161 1.96 maya .f_attach = filt_timerattach,
162 1.96 maya .f_detach = filt_timerdetach,
163 1.96 maya .f_event = filt_timer,
164 1.96 maya };
165 1.1 lukem
166 1.102 christos static const struct filterops fs_filtops = {
167 1.121 thorpej .f_flags = 0,
168 1.102 christos .f_attach = filt_fsattach,
169 1.102 christos .f_detach = filt_fsdetach,
170 1.102 christos .f_event = filt_fs,
171 1.102 christos };
172 1.102 christos
173 1.108 christos static const struct filterops user_filtops = {
174 1.121 thorpej .f_flags = 0,
175 1.108 christos .f_attach = filt_userattach,
176 1.108 christos .f_detach = filt_userdetach,
177 1.108 christos .f_event = filt_user,
178 1.108 christos .f_touch = filt_usertouch,
179 1.108 christos };
180 1.108 christos
181 1.49 ad static u_int kq_ncallouts = 0;
182 1.8 jdolecek static int kq_calloutmax = (4 * 1024);
183 1.7 thorpej
184 1.1 lukem #define KN_HASHSIZE 64 /* XXX should be tunable */
185 1.3 jdolecek #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
186 1.1 lukem
187 1.3 jdolecek extern const struct filterops sig_filtops;
188 1.1 lukem
189 1.111 jdolecek #define KQ_FLUX_WAKEUP(kq) cv_broadcast(&kq->kq_cv)
190 1.111 jdolecek
191 1.1 lukem /*
192 1.1 lukem * Table for for all system-defined filters.
193 1.3 jdolecek * These should be listed in the numeric order of the EVFILT_* defines.
194 1.3 jdolecek * If filtops is NULL, the filter isn't implemented in NetBSD.
195 1.3 jdolecek * End of list is when name is NULL.
196 1.93 riastrad *
197 1.49 ad * Note that 'refcnt' is meaningless for built-in filters.
198 1.1 lukem */
199 1.3 jdolecek struct kfilter {
200 1.49 ad const char *name; /* name of filter */
201 1.49 ad uint32_t filter; /* id of filter */
202 1.49 ad unsigned refcnt; /* reference count */
203 1.3 jdolecek const struct filterops *filtops;/* operations for filter */
204 1.49 ad size_t namelen; /* length of name string */
205 1.3 jdolecek };
206 1.3 jdolecek
207 1.49 ad /* System defined filters */
208 1.49 ad static struct kfilter sys_kfilters[] = {
209 1.49 ad { "EVFILT_READ", EVFILT_READ, 0, &file_filtops, 0 },
210 1.49 ad { "EVFILT_WRITE", EVFILT_WRITE, 0, &file_filtops, 0, },
211 1.49 ad { "EVFILT_AIO", EVFILT_AIO, 0, NULL, 0 },
212 1.49 ad { "EVFILT_VNODE", EVFILT_VNODE, 0, &file_filtops, 0 },
213 1.49 ad { "EVFILT_PROC", EVFILT_PROC, 0, &proc_filtops, 0 },
214 1.49 ad { "EVFILT_SIGNAL", EVFILT_SIGNAL, 0, &sig_filtops, 0 },
215 1.49 ad { "EVFILT_TIMER", EVFILT_TIMER, 0, &timer_filtops, 0 },
216 1.102 christos { "EVFILT_FS", EVFILT_FS, 0, &fs_filtops, 0 },
217 1.108 christos { "EVFILT_USER", EVFILT_USER, 0, &user_filtops, 0 },
218 1.49 ad { NULL, 0, 0, NULL, 0 },
219 1.1 lukem };
220 1.1 lukem
221 1.49 ad /* User defined kfilters */
222 1.3 jdolecek static struct kfilter *user_kfilters; /* array */
223 1.3 jdolecek static int user_kfilterc; /* current offset */
224 1.3 jdolecek static int user_kfiltermaxc; /* max size so far */
225 1.49 ad static size_t user_kfiltersz; /* size of allocated memory */
226 1.49 ad
227 1.95 riastrad /*
228 1.95 riastrad * Global Locks.
229 1.95 riastrad *
230 1.95 riastrad * Lock order:
231 1.95 riastrad *
232 1.95 riastrad * kqueue_filter_lock
233 1.95 riastrad * -> kn_kq->kq_fdp->fd_lock
234 1.95 riastrad * -> object lock (e.g., device driver lock, kqueue_misc_lock, &c.)
235 1.95 riastrad * -> kn_kq->kq_lock
236 1.95 riastrad *
237 1.95 riastrad * Locking rules:
238 1.95 riastrad *
239 1.95 riastrad * f_attach: fdp->fd_lock, KERNEL_LOCK
240 1.95 riastrad * f_detach: fdp->fd_lock, KERNEL_LOCK
241 1.95 riastrad * f_event(!NOTE_SUBMIT) via kevent: fdp->fd_lock, _no_ object lock
242 1.95 riastrad * f_event via knote: whatever caller guarantees
243 1.95 riastrad * Typically, f_event(NOTE_SUBMIT) via knote: object lock
244 1.95 riastrad * f_event(!NOTE_SUBMIT) via knote: nothing,
245 1.95 riastrad * acquires/releases object lock inside.
246 1.95 riastrad */
247 1.49 ad static krwlock_t kqueue_filter_lock; /* lock on filter lists */
248 1.49 ad static kmutex_t kqueue_misc_lock; /* miscellaneous */
249 1.49 ad
250 1.122 thorpej static int
251 1.122 thorpej filter_attach(struct knote *kn)
252 1.122 thorpej {
253 1.122 thorpej int rv;
254 1.122 thorpej
255 1.122 thorpej KASSERT(kn->kn_fop != NULL);
256 1.122 thorpej KASSERT(kn->kn_fop->f_attach != NULL);
257 1.122 thorpej
258 1.122 thorpej /*
259 1.122 thorpej * N.B. that kn->kn_fop may change as the result of calling
260 1.122 thorpej * f_attach().
261 1.122 thorpej */
262 1.122 thorpej if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
263 1.122 thorpej rv = kn->kn_fop->f_attach(kn);
264 1.122 thorpej } else {
265 1.122 thorpej KERNEL_LOCK(1, NULL);
266 1.122 thorpej rv = kn->kn_fop->f_attach(kn);
267 1.122 thorpej KERNEL_UNLOCK_ONE(NULL);
268 1.122 thorpej }
269 1.122 thorpej
270 1.122 thorpej return rv;
271 1.122 thorpej }
272 1.122 thorpej
273 1.122 thorpej static void
274 1.122 thorpej filter_detach(struct knote *kn)
275 1.122 thorpej {
276 1.122 thorpej KASSERT(kn->kn_fop != NULL);
277 1.122 thorpej KASSERT(kn->kn_fop->f_detach != NULL);
278 1.122 thorpej
279 1.122 thorpej if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
280 1.122 thorpej kn->kn_fop->f_detach(kn);
281 1.122 thorpej } else {
282 1.122 thorpej KERNEL_LOCK(1, NULL);
283 1.122 thorpej kn->kn_fop->f_detach(kn);
284 1.122 thorpej KERNEL_UNLOCK_ONE(NULL);
285 1.122 thorpej }
286 1.122 thorpej }
287 1.122 thorpej
288 1.122 thorpej static int
289 1.122 thorpej filter_event(struct knote *kn, long hint)
290 1.122 thorpej {
291 1.122 thorpej int rv;
292 1.122 thorpej
293 1.122 thorpej KASSERT(kn->kn_fop != NULL);
294 1.122 thorpej KASSERT(kn->kn_fop->f_event != NULL);
295 1.122 thorpej
296 1.122 thorpej if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
297 1.122 thorpej rv = kn->kn_fop->f_event(kn, hint);
298 1.122 thorpej } else {
299 1.122 thorpej KERNEL_LOCK(1, NULL);
300 1.122 thorpej rv = kn->kn_fop->f_event(kn, hint);
301 1.122 thorpej KERNEL_UNLOCK_ONE(NULL);
302 1.122 thorpej }
303 1.122 thorpej
304 1.122 thorpej return rv;
305 1.122 thorpej }
306 1.122 thorpej
307 1.122 thorpej static void
308 1.122 thorpej filter_touch(struct knote *kn, struct kevent *kev, long type)
309 1.122 thorpej {
310 1.122 thorpej kn->kn_fop->f_touch(kn, kev, type);
311 1.122 thorpej }
312 1.122 thorpej
313 1.66 elad static kauth_listener_t kqueue_listener;
314 1.66 elad
315 1.66 elad static int
316 1.66 elad kqueue_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
317 1.66 elad void *arg0, void *arg1, void *arg2, void *arg3)
318 1.66 elad {
319 1.66 elad struct proc *p;
320 1.66 elad int result;
321 1.66 elad
322 1.66 elad result = KAUTH_RESULT_DEFER;
323 1.66 elad p = arg0;
324 1.66 elad
325 1.66 elad if (action != KAUTH_PROCESS_KEVENT_FILTER)
326 1.66 elad return result;
327 1.66 elad
328 1.66 elad if ((kauth_cred_getuid(p->p_cred) != kauth_cred_getuid(cred) ||
329 1.66 elad ISSET(p->p_flag, PK_SUGID)))
330 1.66 elad return result;
331 1.66 elad
332 1.66 elad result = KAUTH_RESULT_ALLOW;
333 1.66 elad
334 1.66 elad return result;
335 1.66 elad }
336 1.66 elad
337 1.49 ad /*
338 1.49 ad * Initialize the kqueue subsystem.
339 1.49 ad */
340 1.49 ad void
341 1.49 ad kqueue_init(void)
342 1.49 ad {
343 1.49 ad
344 1.49 ad rw_init(&kqueue_filter_lock);
345 1.49 ad mutex_init(&kqueue_misc_lock, MUTEX_DEFAULT, IPL_NONE);
346 1.66 elad
347 1.66 elad kqueue_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
348 1.66 elad kqueue_listener_cb, NULL);
349 1.49 ad }
350 1.3 jdolecek
351 1.3 jdolecek /*
352 1.3 jdolecek * Find kfilter entry by name, or NULL if not found.
353 1.3 jdolecek */
354 1.49 ad static struct kfilter *
355 1.3 jdolecek kfilter_byname_sys(const char *name)
356 1.3 jdolecek {
357 1.3 jdolecek int i;
358 1.3 jdolecek
359 1.49 ad KASSERT(rw_lock_held(&kqueue_filter_lock));
360 1.49 ad
361 1.3 jdolecek for (i = 0; sys_kfilters[i].name != NULL; i++) {
362 1.3 jdolecek if (strcmp(name, sys_kfilters[i].name) == 0)
363 1.49 ad return &sys_kfilters[i];
364 1.3 jdolecek }
365 1.49 ad return NULL;
366 1.3 jdolecek }
367 1.3 jdolecek
368 1.3 jdolecek static struct kfilter *
369 1.3 jdolecek kfilter_byname_user(const char *name)
370 1.3 jdolecek {
371 1.3 jdolecek int i;
372 1.3 jdolecek
373 1.49 ad KASSERT(rw_lock_held(&kqueue_filter_lock));
374 1.49 ad
375 1.31 seanb /* user filter slots have a NULL name if previously deregistered */
376 1.31 seanb for (i = 0; i < user_kfilterc ; i++) {
377 1.31 seanb if (user_kfilters[i].name != NULL &&
378 1.3 jdolecek strcmp(name, user_kfilters[i].name) == 0)
379 1.49 ad return &user_kfilters[i];
380 1.3 jdolecek }
381 1.49 ad return NULL;
382 1.3 jdolecek }
383 1.3 jdolecek
384 1.49 ad static struct kfilter *
385 1.3 jdolecek kfilter_byname(const char *name)
386 1.3 jdolecek {
387 1.49 ad struct kfilter *kfilter;
388 1.49 ad
389 1.49 ad KASSERT(rw_lock_held(&kqueue_filter_lock));
390 1.3 jdolecek
391 1.3 jdolecek if ((kfilter = kfilter_byname_sys(name)) != NULL)
392 1.49 ad return kfilter;
393 1.3 jdolecek
394 1.49 ad return kfilter_byname_user(name);
395 1.3 jdolecek }
396 1.3 jdolecek
397 1.3 jdolecek /*
398 1.3 jdolecek * Find kfilter entry by filter id, or NULL if not found.
399 1.3 jdolecek * Assumes entries are indexed in filter id order, for speed.
400 1.3 jdolecek */
401 1.49 ad static struct kfilter *
402 1.3 jdolecek kfilter_byfilter(uint32_t filter)
403 1.3 jdolecek {
404 1.49 ad struct kfilter *kfilter;
405 1.49 ad
406 1.49 ad KASSERT(rw_lock_held(&kqueue_filter_lock));
407 1.3 jdolecek
408 1.3 jdolecek if (filter < EVFILT_SYSCOUNT) /* it's a system filter */
409 1.3 jdolecek kfilter = &sys_kfilters[filter];
410 1.3 jdolecek else if (user_kfilters != NULL &&
411 1.3 jdolecek filter < EVFILT_SYSCOUNT + user_kfilterc)
412 1.3 jdolecek /* it's a user filter */
413 1.3 jdolecek kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
414 1.3 jdolecek else
415 1.3 jdolecek return (NULL); /* out of range */
416 1.3 jdolecek KASSERT(kfilter->filter == filter); /* sanity check! */
417 1.3 jdolecek return (kfilter);
418 1.3 jdolecek }
419 1.3 jdolecek
420 1.3 jdolecek /*
421 1.3 jdolecek * Register a new kfilter. Stores the entry in user_kfilters.
422 1.3 jdolecek * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
423 1.3 jdolecek * If retfilter != NULL, the new filterid is returned in it.
424 1.3 jdolecek */
425 1.3 jdolecek int
426 1.3 jdolecek kfilter_register(const char *name, const struct filterops *filtops,
427 1.49 ad int *retfilter)
428 1.1 lukem {
429 1.3 jdolecek struct kfilter *kfilter;
430 1.49 ad size_t len;
431 1.31 seanb int i;
432 1.3 jdolecek
433 1.3 jdolecek if (name == NULL || name[0] == '\0' || filtops == NULL)
434 1.3 jdolecek return (EINVAL); /* invalid args */
435 1.49 ad
436 1.49 ad rw_enter(&kqueue_filter_lock, RW_WRITER);
437 1.49 ad if (kfilter_byname(name) != NULL) {
438 1.49 ad rw_exit(&kqueue_filter_lock);
439 1.3 jdolecek return (EEXIST); /* already exists */
440 1.49 ad }
441 1.49 ad if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT) {
442 1.49 ad rw_exit(&kqueue_filter_lock);
443 1.3 jdolecek return (EINVAL); /* too many */
444 1.49 ad }
445 1.3 jdolecek
446 1.31 seanb for (i = 0; i < user_kfilterc; i++) {
447 1.31 seanb kfilter = &user_kfilters[i];
448 1.31 seanb if (kfilter->name == NULL) {
449 1.31 seanb /* Previously deregistered slot. Reuse. */
450 1.31 seanb goto reuse;
451 1.31 seanb }
452 1.31 seanb }
453 1.31 seanb
454 1.3 jdolecek /* check if need to grow user_kfilters */
455 1.3 jdolecek if (user_kfilterc + 1 > user_kfiltermaxc) {
456 1.49 ad /* Grow in KFILTER_EXTENT chunks. */
457 1.3 jdolecek user_kfiltermaxc += KFILTER_EXTENT;
458 1.69 dsl len = user_kfiltermaxc * sizeof(*kfilter);
459 1.49 ad kfilter = kmem_alloc(len, KM_SLEEP);
460 1.49 ad memset((char *)kfilter + user_kfiltersz, 0, len - user_kfiltersz);
461 1.49 ad if (user_kfilters != NULL) {
462 1.49 ad memcpy(kfilter, user_kfilters, user_kfiltersz);
463 1.49 ad kmem_free(user_kfilters, user_kfiltersz);
464 1.49 ad }
465 1.49 ad user_kfiltersz = len;
466 1.3 jdolecek user_kfilters = kfilter;
467 1.3 jdolecek }
468 1.31 seanb /* Adding new slot */
469 1.31 seanb kfilter = &user_kfilters[user_kfilterc++];
470 1.31 seanb reuse:
471 1.97 christos kfilter->name = kmem_strdupsize(name, &kfilter->namelen, KM_SLEEP);
472 1.3 jdolecek
473 1.31 seanb kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
474 1.3 jdolecek
475 1.49 ad kfilter->filtops = kmem_alloc(sizeof(*filtops), KM_SLEEP);
476 1.49 ad memcpy(__UNCONST(kfilter->filtops), filtops, sizeof(*filtops));
477 1.3 jdolecek
478 1.3 jdolecek if (retfilter != NULL)
479 1.31 seanb *retfilter = kfilter->filter;
480 1.49 ad rw_exit(&kqueue_filter_lock);
481 1.49 ad
482 1.3 jdolecek return (0);
483 1.1 lukem }
484 1.1 lukem
485 1.3 jdolecek /*
486 1.3 jdolecek * Unregister a kfilter previously registered with kfilter_register.
487 1.3 jdolecek * This retains the filter id, but clears the name and frees filtops (filter
488 1.3 jdolecek * operations), so that the number isn't reused during a boot.
489 1.3 jdolecek * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
490 1.3 jdolecek */
491 1.3 jdolecek int
492 1.3 jdolecek kfilter_unregister(const char *name)
493 1.1 lukem {
494 1.3 jdolecek struct kfilter *kfilter;
495 1.3 jdolecek
496 1.3 jdolecek if (name == NULL || name[0] == '\0')
497 1.3 jdolecek return (EINVAL); /* invalid name */
498 1.3 jdolecek
499 1.49 ad rw_enter(&kqueue_filter_lock, RW_WRITER);
500 1.49 ad if (kfilter_byname_sys(name) != NULL) {
501 1.49 ad rw_exit(&kqueue_filter_lock);
502 1.3 jdolecek return (EINVAL); /* can't detach system filters */
503 1.49 ad }
504 1.1 lukem
505 1.3 jdolecek kfilter = kfilter_byname_user(name);
506 1.49 ad if (kfilter == NULL) {
507 1.49 ad rw_exit(&kqueue_filter_lock);
508 1.3 jdolecek return (ENOENT);
509 1.49 ad }
510 1.49 ad if (kfilter->refcnt != 0) {
511 1.49 ad rw_exit(&kqueue_filter_lock);
512 1.49 ad return (EBUSY);
513 1.49 ad }
514 1.1 lukem
515 1.49 ad /* Cast away const (but we know it's safe. */
516 1.49 ad kmem_free(__UNCONST(kfilter->name), kfilter->namelen);
517 1.31 seanb kfilter->name = NULL; /* mark as `not implemented' */
518 1.31 seanb
519 1.3 jdolecek if (kfilter->filtops != NULL) {
520 1.49 ad /* Cast away const (but we know it's safe. */
521 1.49 ad kmem_free(__UNCONST(kfilter->filtops),
522 1.49 ad sizeof(*kfilter->filtops));
523 1.3 jdolecek kfilter->filtops = NULL; /* mark as `not implemented' */
524 1.3 jdolecek }
525 1.49 ad rw_exit(&kqueue_filter_lock);
526 1.49 ad
527 1.1 lukem return (0);
528 1.1 lukem }
529 1.1 lukem
530 1.3 jdolecek
531 1.3 jdolecek /*
532 1.3 jdolecek * Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
533 1.49 ad * descriptors. Calls fileops kqfilter method for given file descriptor.
534 1.3 jdolecek */
535 1.3 jdolecek static int
536 1.3 jdolecek filt_fileattach(struct knote *kn)
537 1.3 jdolecek {
538 1.49 ad file_t *fp;
539 1.49 ad
540 1.49 ad fp = kn->kn_obj;
541 1.3 jdolecek
542 1.49 ad return (*fp->f_ops->fo_kqfilter)(fp, kn);
543 1.3 jdolecek }
544 1.3 jdolecek
545 1.3 jdolecek /*
546 1.3 jdolecek * Filter detach method for EVFILT_READ on kqueue descriptor.
547 1.3 jdolecek */
548 1.1 lukem static void
549 1.1 lukem filt_kqdetach(struct knote *kn)
550 1.1 lukem {
551 1.3 jdolecek struct kqueue *kq;
552 1.1 lukem
553 1.82 matt kq = ((file_t *)kn->kn_obj)->f_kqueue;
554 1.49 ad
555 1.49 ad mutex_spin_enter(&kq->kq_lock);
556 1.109 thorpej selremove_knote(&kq->kq_sel, kn);
557 1.49 ad mutex_spin_exit(&kq->kq_lock);
558 1.1 lukem }
559 1.1 lukem
560 1.3 jdolecek /*
561 1.3 jdolecek * Filter event method for EVFILT_READ on kqueue descriptor.
562 1.3 jdolecek */
563 1.1 lukem /*ARGSUSED*/
564 1.1 lukem static int
565 1.33 yamt filt_kqueue(struct knote *kn, long hint)
566 1.1 lukem {
567 1.3 jdolecek struct kqueue *kq;
568 1.49 ad int rv;
569 1.49 ad
570 1.82 matt kq = ((file_t *)kn->kn_obj)->f_kqueue;
571 1.1 lukem
572 1.49 ad if (hint != NOTE_SUBMIT)
573 1.49 ad mutex_spin_enter(&kq->kq_lock);
574 1.118 jdolecek kn->kn_data = KQ_COUNT(kq);
575 1.49 ad rv = (kn->kn_data > 0);
576 1.49 ad if (hint != NOTE_SUBMIT)
577 1.49 ad mutex_spin_exit(&kq->kq_lock);
578 1.49 ad
579 1.49 ad return rv;
580 1.1 lukem }
581 1.1 lukem
582 1.3 jdolecek /*
583 1.3 jdolecek * Filter attach method for EVFILT_PROC.
584 1.3 jdolecek */
585 1.1 lukem static int
586 1.1 lukem filt_procattach(struct knote *kn)
587 1.1 lukem {
588 1.78 pooka struct proc *p;
589 1.30 ad struct lwp *curl;
590 1.30 ad
591 1.30 ad curl = curlwp;
592 1.1 lukem
593 1.107 ad mutex_enter(&proc_lock);
594 1.77 joerg if (kn->kn_flags & EV_FLAG1) {
595 1.77 joerg /*
596 1.77 joerg * NOTE_TRACK attaches to the child process too early
597 1.77 joerg * for proc_find, so do a raw look up and check the state
598 1.77 joerg * explicitly.
599 1.77 joerg */
600 1.77 joerg p = proc_find_raw(kn->kn_id);
601 1.77 joerg if (p != NULL && p->p_stat != SIDL)
602 1.77 joerg p = NULL;
603 1.77 joerg } else {
604 1.77 joerg p = proc_find(kn->kn_id);
605 1.77 joerg }
606 1.77 joerg
607 1.49 ad if (p == NULL) {
608 1.107 ad mutex_exit(&proc_lock);
609 1.49 ad return ESRCH;
610 1.49 ad }
611 1.3 jdolecek
612 1.3 jdolecek /*
613 1.3 jdolecek * Fail if it's not owned by you, or the last exec gave us
614 1.3 jdolecek * setuid/setgid privs (unless you're root).
615 1.3 jdolecek */
616 1.57 ad mutex_enter(p->p_lock);
617 1.107 ad mutex_exit(&proc_lock);
618 1.120 christos if (kauth_authorize_process(curl->l_cred,
619 1.119 christos KAUTH_PROCESS_KEVENT_FILTER, p, NULL, NULL, NULL) != 0) {
620 1.57 ad mutex_exit(p->p_lock);
621 1.49 ad return EACCES;
622 1.49 ad }
623 1.1 lukem
624 1.49 ad kn->kn_obj = p;
625 1.3 jdolecek kn->kn_flags |= EV_CLEAR; /* automatically set */
626 1.1 lukem
627 1.1 lukem /*
628 1.1 lukem * internal flag indicating registration done by kernel
629 1.1 lukem */
630 1.1 lukem if (kn->kn_flags & EV_FLAG1) {
631 1.3 jdolecek kn->kn_data = kn->kn_sdata; /* ppid */
632 1.1 lukem kn->kn_fflags = NOTE_CHILD;
633 1.1 lukem kn->kn_flags &= ~EV_FLAG1;
634 1.1 lukem }
635 1.1 lukem SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
636 1.57 ad mutex_exit(p->p_lock);
637 1.1 lukem
638 1.49 ad return 0;
639 1.1 lukem }
640 1.1 lukem
641 1.1 lukem /*
642 1.3 jdolecek * Filter detach method for EVFILT_PROC.
643 1.3 jdolecek *
644 1.1 lukem * The knote may be attached to a different process, which may exit,
645 1.1 lukem * leaving nothing for the knote to be attached to. So when the process
646 1.1 lukem * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
647 1.1 lukem * it will be deleted when read out. However, as part of the knote deletion,
648 1.1 lukem * this routine is called, so a check is needed to avoid actually performing
649 1.3 jdolecek * a detach, because the original process might not exist any more.
650 1.1 lukem */
651 1.1 lukem static void
652 1.1 lukem filt_procdetach(struct knote *kn)
653 1.1 lukem {
654 1.3 jdolecek struct proc *p;
655 1.1 lukem
656 1.1 lukem if (kn->kn_status & KN_DETACHED)
657 1.1 lukem return;
658 1.1 lukem
659 1.49 ad p = kn->kn_obj;
660 1.3 jdolecek
661 1.57 ad mutex_enter(p->p_lock);
662 1.1 lukem SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
663 1.57 ad mutex_exit(p->p_lock);
664 1.1 lukem }
665 1.1 lukem
666 1.3 jdolecek /*
667 1.3 jdolecek * Filter event method for EVFILT_PROC.
668 1.3 jdolecek */
669 1.1 lukem static int
670 1.1 lukem filt_proc(struct knote *kn, long hint)
671 1.1 lukem {
672 1.49 ad u_int event, fflag;
673 1.49 ad struct kevent kev;
674 1.49 ad struct kqueue *kq;
675 1.49 ad int error;
676 1.1 lukem
677 1.1 lukem event = (u_int)hint & NOTE_PCTRLMASK;
678 1.49 ad kq = kn->kn_kq;
679 1.49 ad fflag = 0;
680 1.1 lukem
681 1.49 ad /* If the user is interested in this event, record it. */
682 1.1 lukem if (kn->kn_sfflags & event)
683 1.49 ad fflag |= event;
684 1.1 lukem
685 1.1 lukem if (event == NOTE_EXIT) {
686 1.83 christos struct proc *p = kn->kn_obj;
687 1.83 christos
688 1.83 christos if (p != NULL)
689 1.86 christos kn->kn_data = P_WAITSTATUS(p);
690 1.3 jdolecek /*
691 1.49 ad * Process is gone, so flag the event as finished.
692 1.49 ad *
693 1.3 jdolecek * Detach the knote from watched process and mark
694 1.3 jdolecek * it as such. We can't leave this to kqueue_scan(),
695 1.3 jdolecek * since the process might not exist by then. And we
696 1.3 jdolecek * have to do this now, since psignal KNOTE() is called
697 1.3 jdolecek * also for zombies and we might end up reading freed
698 1.3 jdolecek * memory if the kevent would already be picked up
699 1.22 perry * and knote g/c'ed.
700 1.3 jdolecek */
701 1.49 ad filt_procdetach(kn);
702 1.49 ad
703 1.49 ad mutex_spin_enter(&kq->kq_lock);
704 1.1 lukem kn->kn_status |= KN_DETACHED;
705 1.3 jdolecek /* Mark as ONESHOT, so that the knote it g/c'ed when read */
706 1.22 perry kn->kn_flags |= (EV_EOF | EV_ONESHOT);
707 1.49 ad kn->kn_fflags |= fflag;
708 1.49 ad mutex_spin_exit(&kq->kq_lock);
709 1.49 ad
710 1.49 ad return 1;
711 1.1 lukem }
712 1.1 lukem
713 1.49 ad mutex_spin_enter(&kq->kq_lock);
714 1.1 lukem if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
715 1.1 lukem /*
716 1.49 ad * Process forked, and user wants to track the new process,
717 1.49 ad * so attach a new knote to it, and immediately report an
718 1.49 ad * event with the parent's pid. Register knote with new
719 1.49 ad * process.
720 1.1 lukem */
721 1.104 maxv memset(&kev, 0, sizeof(kev));
722 1.1 lukem kev.ident = hint & NOTE_PDATAMASK; /* pid */
723 1.1 lukem kev.filter = kn->kn_filter;
724 1.1 lukem kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
725 1.1 lukem kev.fflags = kn->kn_sfflags;
726 1.1 lukem kev.data = kn->kn_id; /* parent */
727 1.1 lukem kev.udata = kn->kn_kevent.udata; /* preserve udata */
728 1.49 ad mutex_spin_exit(&kq->kq_lock);
729 1.49 ad error = kqueue_register(kq, &kev);
730 1.49 ad mutex_spin_enter(&kq->kq_lock);
731 1.49 ad if (error != 0)
732 1.1 lukem kn->kn_fflags |= NOTE_TRACKERR;
733 1.1 lukem }
734 1.49 ad kn->kn_fflags |= fflag;
735 1.49 ad fflag = kn->kn_fflags;
736 1.49 ad mutex_spin_exit(&kq->kq_lock);
737 1.1 lukem
738 1.49 ad return fflag != 0;
739 1.8 jdolecek }
740 1.8 jdolecek
741 1.8 jdolecek static void
742 1.8 jdolecek filt_timerexpire(void *knx)
743 1.8 jdolecek {
744 1.8 jdolecek struct knote *kn = knx;
745 1.8 jdolecek int tticks;
746 1.8 jdolecek
747 1.49 ad mutex_enter(&kqueue_misc_lock);
748 1.8 jdolecek kn->kn_data++;
749 1.49 ad knote_activate(kn);
750 1.8 jdolecek if ((kn->kn_flags & EV_ONESHOT) == 0) {
751 1.8 jdolecek tticks = mstohz(kn->kn_sdata);
752 1.73 christos if (tticks <= 0)
753 1.73 christos tticks = 1;
754 1.39 ad callout_schedule((callout_t *)kn->kn_hook, tticks);
755 1.8 jdolecek }
756 1.49 ad mutex_exit(&kqueue_misc_lock);
757 1.8 jdolecek }
758 1.8 jdolecek
759 1.8 jdolecek /*
760 1.8 jdolecek * data contains amount of time to sleep, in milliseconds
761 1.22 perry */
762 1.8 jdolecek static int
763 1.8 jdolecek filt_timerattach(struct knote *kn)
764 1.8 jdolecek {
765 1.39 ad callout_t *calloutp;
766 1.49 ad struct kqueue *kq;
767 1.8 jdolecek int tticks;
768 1.8 jdolecek
769 1.8 jdolecek tticks = mstohz(kn->kn_sdata);
770 1.8 jdolecek
771 1.8 jdolecek /* if the supplied value is under our resolution, use 1 tick */
772 1.8 jdolecek if (tticks == 0) {
773 1.8 jdolecek if (kn->kn_sdata == 0)
774 1.49 ad return EINVAL;
775 1.8 jdolecek tticks = 1;
776 1.8 jdolecek }
777 1.8 jdolecek
778 1.49 ad if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
779 1.49 ad (calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
780 1.49 ad atomic_dec_uint(&kq_ncallouts);
781 1.49 ad return ENOMEM;
782 1.49 ad }
783 1.54 ad callout_init(calloutp, CALLOUT_MPSAFE);
784 1.49 ad
785 1.49 ad kq = kn->kn_kq;
786 1.49 ad mutex_spin_enter(&kq->kq_lock);
787 1.8 jdolecek kn->kn_flags |= EV_CLEAR; /* automatically set */
788 1.49 ad kn->kn_hook = calloutp;
789 1.49 ad mutex_spin_exit(&kq->kq_lock);
790 1.49 ad
791 1.8 jdolecek callout_reset(calloutp, tticks, filt_timerexpire, kn);
792 1.8 jdolecek
793 1.8 jdolecek return (0);
794 1.8 jdolecek }
795 1.8 jdolecek
796 1.8 jdolecek static void
797 1.8 jdolecek filt_timerdetach(struct knote *kn)
798 1.8 jdolecek {
799 1.39 ad callout_t *calloutp;
800 1.103 christos struct kqueue *kq = kn->kn_kq;
801 1.103 christos
802 1.103 christos mutex_spin_enter(&kq->kq_lock);
803 1.103 christos /* prevent rescheduling when we expire */
804 1.103 christos kn->kn_flags |= EV_ONESHOT;
805 1.103 christos mutex_spin_exit(&kq->kq_lock);
806 1.8 jdolecek
807 1.39 ad calloutp = (callout_t *)kn->kn_hook;
808 1.55 ad callout_halt(calloutp, NULL);
809 1.39 ad callout_destroy(calloutp);
810 1.49 ad kmem_free(calloutp, sizeof(*calloutp));
811 1.49 ad atomic_dec_uint(&kq_ncallouts);
812 1.8 jdolecek }
813 1.8 jdolecek
814 1.8 jdolecek static int
815 1.33 yamt filt_timer(struct knote *kn, long hint)
816 1.8 jdolecek {
817 1.49 ad int rv;
818 1.49 ad
819 1.49 ad mutex_enter(&kqueue_misc_lock);
820 1.49 ad rv = (kn->kn_data != 0);
821 1.49 ad mutex_exit(&kqueue_misc_lock);
822 1.49 ad
823 1.49 ad return rv;
824 1.1 lukem }
825 1.1 lukem
826 1.3 jdolecek /*
827 1.102 christos * Filter event method for EVFILT_FS.
828 1.102 christos */
829 1.102 christos struct klist fs_klist = SLIST_HEAD_INITIALIZER(&fs_klist);
830 1.102 christos
831 1.102 christos static int
832 1.102 christos filt_fsattach(struct knote *kn)
833 1.102 christos {
834 1.102 christos
835 1.102 christos mutex_enter(&kqueue_misc_lock);
836 1.102 christos kn->kn_flags |= EV_CLEAR;
837 1.102 christos SLIST_INSERT_HEAD(&fs_klist, kn, kn_selnext);
838 1.102 christos mutex_exit(&kqueue_misc_lock);
839 1.102 christos
840 1.102 christos return 0;
841 1.102 christos }
842 1.102 christos
843 1.102 christos static void
844 1.102 christos filt_fsdetach(struct knote *kn)
845 1.102 christos {
846 1.102 christos
847 1.102 christos mutex_enter(&kqueue_misc_lock);
848 1.102 christos SLIST_REMOVE(&fs_klist, kn, knote, kn_selnext);
849 1.102 christos mutex_exit(&kqueue_misc_lock);
850 1.102 christos }
851 1.102 christos
852 1.102 christos static int
853 1.102 christos filt_fs(struct knote *kn, long hint)
854 1.102 christos {
855 1.102 christos int rv;
856 1.102 christos
857 1.102 christos mutex_enter(&kqueue_misc_lock);
858 1.102 christos kn->kn_fflags |= hint;
859 1.102 christos rv = (kn->kn_fflags != 0);
860 1.102 christos mutex_exit(&kqueue_misc_lock);
861 1.102 christos
862 1.102 christos return rv;
863 1.102 christos }
864 1.102 christos
865 1.108 christos static int
866 1.108 christos filt_userattach(struct knote *kn)
867 1.108 christos {
868 1.108 christos struct kqueue *kq = kn->kn_kq;
869 1.108 christos
870 1.108 christos /*
871 1.108 christos * EVFILT_USER knotes are not attached to anything in the kernel.
872 1.108 christos */
873 1.108 christos mutex_spin_enter(&kq->kq_lock);
874 1.108 christos kn->kn_hook = NULL;
875 1.108 christos if (kn->kn_fflags & NOTE_TRIGGER)
876 1.108 christos kn->kn_hookid = 1;
877 1.108 christos else
878 1.108 christos kn->kn_hookid = 0;
879 1.108 christos mutex_spin_exit(&kq->kq_lock);
880 1.108 christos return (0);
881 1.108 christos }
882 1.108 christos
883 1.108 christos static void
884 1.108 christos filt_userdetach(struct knote *kn)
885 1.108 christos {
886 1.108 christos
887 1.108 christos /*
888 1.108 christos * EVFILT_USER knotes are not attached to anything in the kernel.
889 1.108 christos */
890 1.108 christos }
891 1.108 christos
892 1.108 christos static int
893 1.108 christos filt_user(struct knote *kn, long hint)
894 1.108 christos {
895 1.108 christos struct kqueue *kq = kn->kn_kq;
896 1.108 christos int hookid;
897 1.108 christos
898 1.108 christos mutex_spin_enter(&kq->kq_lock);
899 1.108 christos hookid = kn->kn_hookid;
900 1.108 christos mutex_spin_exit(&kq->kq_lock);
901 1.108 christos
902 1.108 christos return hookid;
903 1.108 christos }
904 1.108 christos
905 1.108 christos static void
906 1.108 christos filt_usertouch(struct knote *kn, struct kevent *kev, long type)
907 1.108 christos {
908 1.108 christos int ffctrl;
909 1.108 christos
910 1.117 skrll KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
911 1.116 jdolecek
912 1.108 christos switch (type) {
913 1.108 christos case EVENT_REGISTER:
914 1.108 christos if (kev->fflags & NOTE_TRIGGER)
915 1.108 christos kn->kn_hookid = 1;
916 1.108 christos
917 1.108 christos ffctrl = kev->fflags & NOTE_FFCTRLMASK;
918 1.108 christos kev->fflags &= NOTE_FFLAGSMASK;
919 1.108 christos switch (ffctrl) {
920 1.108 christos case NOTE_FFNOP:
921 1.108 christos break;
922 1.108 christos
923 1.108 christos case NOTE_FFAND:
924 1.108 christos kn->kn_sfflags &= kev->fflags;
925 1.108 christos break;
926 1.108 christos
927 1.108 christos case NOTE_FFOR:
928 1.108 christos kn->kn_sfflags |= kev->fflags;
929 1.108 christos break;
930 1.108 christos
931 1.108 christos case NOTE_FFCOPY:
932 1.108 christos kn->kn_sfflags = kev->fflags;
933 1.108 christos break;
934 1.108 christos
935 1.108 christos default:
936 1.108 christos /* XXX Return error? */
937 1.108 christos break;
938 1.108 christos }
939 1.108 christos kn->kn_sdata = kev->data;
940 1.108 christos if (kev->flags & EV_CLEAR) {
941 1.108 christos kn->kn_hookid = 0;
942 1.108 christos kn->kn_data = 0;
943 1.108 christos kn->kn_fflags = 0;
944 1.108 christos }
945 1.108 christos break;
946 1.108 christos
947 1.108 christos case EVENT_PROCESS:
948 1.108 christos *kev = kn->kn_kevent;
949 1.108 christos kev->fflags = kn->kn_sfflags;
950 1.108 christos kev->data = kn->kn_sdata;
951 1.108 christos if (kn->kn_flags & EV_CLEAR) {
952 1.108 christos kn->kn_hookid = 0;
953 1.108 christos kn->kn_data = 0;
954 1.108 christos kn->kn_fflags = 0;
955 1.108 christos }
956 1.108 christos break;
957 1.108 christos
958 1.108 christos default:
959 1.108 christos panic("filt_usertouch() - invalid type (%ld)", type);
960 1.108 christos break;
961 1.108 christos }
962 1.108 christos }
963 1.108 christos
964 1.102 christos /*
965 1.3 jdolecek * filt_seltrue:
966 1.3 jdolecek *
967 1.3 jdolecek * This filter "event" routine simulates seltrue().
968 1.3 jdolecek */
969 1.1 lukem int
970 1.33 yamt filt_seltrue(struct knote *kn, long hint)
971 1.1 lukem {
972 1.1 lukem
973 1.3 jdolecek /*
974 1.3 jdolecek * We don't know how much data can be read/written,
975 1.3 jdolecek * but we know that it *can* be. This is about as
976 1.3 jdolecek * good as select/poll does as well.
977 1.3 jdolecek */
978 1.3 jdolecek kn->kn_data = 0;
979 1.3 jdolecek return (1);
980 1.3 jdolecek }
981 1.3 jdolecek
982 1.3 jdolecek /*
983 1.3 jdolecek * This provides full kqfilter entry for device switch tables, which
984 1.3 jdolecek * has same effect as filter using filt_seltrue() as filter method.
985 1.3 jdolecek */
986 1.3 jdolecek static void
987 1.33 yamt filt_seltruedetach(struct knote *kn)
988 1.3 jdolecek {
989 1.3 jdolecek /* Nothing to do */
990 1.3 jdolecek }
991 1.3 jdolecek
992 1.96 maya const struct filterops seltrue_filtops = {
993 1.121 thorpej .f_flags = FILTEROP_ISFD,
994 1.96 maya .f_attach = NULL,
995 1.96 maya .f_detach = filt_seltruedetach,
996 1.96 maya .f_event = filt_seltrue,
997 1.96 maya };
998 1.3 jdolecek
999 1.3 jdolecek int
1000 1.33 yamt seltrue_kqfilter(dev_t dev, struct knote *kn)
1001 1.3 jdolecek {
1002 1.3 jdolecek switch (kn->kn_filter) {
1003 1.3 jdolecek case EVFILT_READ:
1004 1.3 jdolecek case EVFILT_WRITE:
1005 1.3 jdolecek kn->kn_fop = &seltrue_filtops;
1006 1.3 jdolecek break;
1007 1.3 jdolecek default:
1008 1.43 pooka return (EINVAL);
1009 1.3 jdolecek }
1010 1.3 jdolecek
1011 1.3 jdolecek /* Nothing more to do */
1012 1.3 jdolecek return (0);
1013 1.3 jdolecek }
1014 1.3 jdolecek
1015 1.3 jdolecek /*
1016 1.3 jdolecek * kqueue(2) system call.
1017 1.3 jdolecek */
1018 1.72 christos static int
1019 1.72 christos kqueue1(struct lwp *l, int flags, register_t *retval)
1020 1.3 jdolecek {
1021 1.49 ad struct kqueue *kq;
1022 1.49 ad file_t *fp;
1023 1.49 ad int fd, error;
1024 1.3 jdolecek
1025 1.49 ad if ((error = fd_allocfile(&fp, &fd)) != 0)
1026 1.49 ad return error;
1027 1.75 christos fp->f_flag = FREAD | FWRITE | (flags & (FNONBLOCK|FNOSIGPIPE));
1028 1.1 lukem fp->f_type = DTYPE_KQUEUE;
1029 1.1 lukem fp->f_ops = &kqueueops;
1030 1.49 ad kq = kmem_zalloc(sizeof(*kq), KM_SLEEP);
1031 1.49 ad mutex_init(&kq->kq_lock, MUTEX_DEFAULT, IPL_SCHED);
1032 1.49 ad cv_init(&kq->kq_cv, "kqueue");
1033 1.49 ad selinit(&kq->kq_sel);
1034 1.1 lukem TAILQ_INIT(&kq->kq_head);
1035 1.82 matt fp->f_kqueue = kq;
1036 1.3 jdolecek *retval = fd;
1037 1.49 ad kq->kq_fdp = curlwp->l_fd;
1038 1.72 christos fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
1039 1.49 ad fd_affix(curproc, fp, fd);
1040 1.49 ad return error;
1041 1.1 lukem }
1042 1.1 lukem
1043 1.3 jdolecek /*
1044 1.72 christos * kqueue(2) system call.
1045 1.72 christos */
1046 1.72 christos int
1047 1.72 christos sys_kqueue(struct lwp *l, const void *v, register_t *retval)
1048 1.72 christos {
1049 1.72 christos return kqueue1(l, 0, retval);
1050 1.72 christos }
1051 1.72 christos
1052 1.72 christos int
1053 1.72 christos sys_kqueue1(struct lwp *l, const struct sys_kqueue1_args *uap,
1054 1.72 christos register_t *retval)
1055 1.72 christos {
1056 1.72 christos /* {
1057 1.72 christos syscallarg(int) flags;
1058 1.72 christos } */
1059 1.72 christos return kqueue1(l, SCARG(uap, flags), retval);
1060 1.72 christos }
1061 1.72 christos
1062 1.72 christos /*
1063 1.3 jdolecek * kevent(2) system call.
1064 1.3 jdolecek */
1065 1.61 christos int
1066 1.81 matt kevent_fetch_changes(void *ctx, const struct kevent *changelist,
1067 1.61 christos struct kevent *changes, size_t index, int n)
1068 1.24 cube {
1069 1.49 ad
1070 1.24 cube return copyin(changelist + index, changes, n * sizeof(*changes));
1071 1.24 cube }
1072 1.24 cube
1073 1.61 christos int
1074 1.81 matt kevent_put_events(void *ctx, struct kevent *events,
1075 1.61 christos struct kevent *eventlist, size_t index, int n)
1076 1.24 cube {
1077 1.49 ad
1078 1.24 cube return copyout(events, eventlist + index, n * sizeof(*events));
1079 1.24 cube }
1080 1.24 cube
1081 1.24 cube static const struct kevent_ops kevent_native_ops = {
1082 1.60 gmcgarry .keo_private = NULL,
1083 1.60 gmcgarry .keo_fetch_timeout = copyin,
1084 1.60 gmcgarry .keo_fetch_changes = kevent_fetch_changes,
1085 1.60 gmcgarry .keo_put_events = kevent_put_events,
1086 1.24 cube };
1087 1.24 cube
1088 1.1 lukem int
1089 1.61 christos sys___kevent50(struct lwp *l, const struct sys___kevent50_args *uap,
1090 1.61 christos register_t *retval)
1091 1.1 lukem {
1092 1.44 dsl /* {
1093 1.3 jdolecek syscallarg(int) fd;
1094 1.3 jdolecek syscallarg(const struct kevent *) changelist;
1095 1.3 jdolecek syscallarg(size_t) nchanges;
1096 1.3 jdolecek syscallarg(struct kevent *) eventlist;
1097 1.3 jdolecek syscallarg(size_t) nevents;
1098 1.3 jdolecek syscallarg(const struct timespec *) timeout;
1099 1.44 dsl } */
1100 1.24 cube
1101 1.49 ad return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
1102 1.24 cube SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
1103 1.24 cube SCARG(uap, timeout), &kevent_native_ops);
1104 1.24 cube }
1105 1.24 cube
1106 1.24 cube int
1107 1.49 ad kevent1(register_t *retval, int fd,
1108 1.49 ad const struct kevent *changelist, size_t nchanges,
1109 1.49 ad struct kevent *eventlist, size_t nevents,
1110 1.49 ad const struct timespec *timeout,
1111 1.49 ad const struct kevent_ops *keops)
1112 1.24 cube {
1113 1.49 ad struct kevent *kevp;
1114 1.49 ad struct kqueue *kq;
1115 1.3 jdolecek struct timespec ts;
1116 1.49 ad size_t i, n, ichange;
1117 1.49 ad int nerrors, error;
1118 1.80 maxv struct kevent kevbuf[KQ_NEVENTS]; /* approx 300 bytes on 64-bit */
1119 1.49 ad file_t *fp;
1120 1.3 jdolecek
1121 1.3 jdolecek /* check that we're dealing with a kq */
1122 1.49 ad fp = fd_getfile(fd);
1123 1.10 pk if (fp == NULL)
1124 1.1 lukem return (EBADF);
1125 1.10 pk
1126 1.10 pk if (fp->f_type != DTYPE_KQUEUE) {
1127 1.49 ad fd_putfile(fd);
1128 1.10 pk return (EBADF);
1129 1.10 pk }
1130 1.1 lukem
1131 1.24 cube if (timeout != NULL) {
1132 1.24 cube error = (*keops->keo_fetch_timeout)(timeout, &ts, sizeof(ts));
1133 1.1 lukem if (error)
1134 1.1 lukem goto done;
1135 1.24 cube timeout = &ts;
1136 1.1 lukem }
1137 1.1 lukem
1138 1.82 matt kq = fp->f_kqueue;
1139 1.1 lukem nerrors = 0;
1140 1.24 cube ichange = 0;
1141 1.1 lukem
1142 1.3 jdolecek /* traverse list of events to register */
1143 1.24 cube while (nchanges > 0) {
1144 1.49 ad n = MIN(nchanges, __arraycount(kevbuf));
1145 1.24 cube error = (*keops->keo_fetch_changes)(keops->keo_private,
1146 1.49 ad changelist, kevbuf, ichange, n);
1147 1.1 lukem if (error)
1148 1.1 lukem goto done;
1149 1.1 lukem for (i = 0; i < n; i++) {
1150 1.49 ad kevp = &kevbuf[i];
1151 1.1 lukem kevp->flags &= ~EV_SYSFLAGS;
1152 1.3 jdolecek /* register each knote */
1153 1.49 ad error = kqueue_register(kq, kevp);
1154 1.89 abhinav if (!error && !(kevp->flags & EV_RECEIPT))
1155 1.89 abhinav continue;
1156 1.89 abhinav if (nevents == 0)
1157 1.89 abhinav goto done;
1158 1.89 abhinav kevp->flags = EV_ERROR;
1159 1.89 abhinav kevp->data = error;
1160 1.89 abhinav error = (*keops->keo_put_events)
1161 1.89 abhinav (keops->keo_private, kevp,
1162 1.89 abhinav eventlist, nerrors, 1);
1163 1.89 abhinav if (error)
1164 1.89 abhinav goto done;
1165 1.89 abhinav nevents--;
1166 1.89 abhinav nerrors++;
1167 1.1 lukem }
1168 1.24 cube nchanges -= n; /* update the results */
1169 1.24 cube ichange += n;
1170 1.1 lukem }
1171 1.1 lukem if (nerrors) {
1172 1.3 jdolecek *retval = nerrors;
1173 1.1 lukem error = 0;
1174 1.1 lukem goto done;
1175 1.1 lukem }
1176 1.1 lukem
1177 1.3 jdolecek /* actually scan through the events */
1178 1.49 ad error = kqueue_scan(fp, nevents, eventlist, timeout, retval, keops,
1179 1.49 ad kevbuf, __arraycount(kevbuf));
1180 1.3 jdolecek done:
1181 1.49 ad fd_putfile(fd);
1182 1.1 lukem return (error);
1183 1.1 lukem }
1184 1.1 lukem
1185 1.3 jdolecek /*
1186 1.3 jdolecek * Register a given kevent kev onto the kqueue
1187 1.3 jdolecek */
1188 1.49 ad static int
1189 1.49 ad kqueue_register(struct kqueue *kq, struct kevent *kev)
1190 1.1 lukem {
1191 1.49 ad struct kfilter *kfilter;
1192 1.49 ad filedesc_t *fdp;
1193 1.49 ad file_t *fp;
1194 1.49 ad fdfile_t *ff;
1195 1.49 ad struct knote *kn, *newkn;
1196 1.49 ad struct klist *list;
1197 1.49 ad int error, fd, rv;
1198 1.3 jdolecek
1199 1.3 jdolecek fdp = kq->kq_fdp;
1200 1.3 jdolecek fp = NULL;
1201 1.3 jdolecek kn = NULL;
1202 1.3 jdolecek error = 0;
1203 1.49 ad fd = 0;
1204 1.49 ad
1205 1.49 ad newkn = kmem_zalloc(sizeof(*newkn), KM_SLEEP);
1206 1.49 ad
1207 1.49 ad rw_enter(&kqueue_filter_lock, RW_READER);
1208 1.3 jdolecek kfilter = kfilter_byfilter(kev->filter);
1209 1.3 jdolecek if (kfilter == NULL || kfilter->filtops == NULL) {
1210 1.3 jdolecek /* filter not found nor implemented */
1211 1.49 ad rw_exit(&kqueue_filter_lock);
1212 1.49 ad kmem_free(newkn, sizeof(*newkn));
1213 1.1 lukem return (EINVAL);
1214 1.1 lukem }
1215 1.1 lukem
1216 1.3 jdolecek /* search if knote already exists */
1217 1.121 thorpej if (kfilter->filtops->f_flags & FILTEROP_ISFD) {
1218 1.3 jdolecek /* monitoring a file descriptor */
1219 1.87 christos /* validate descriptor */
1220 1.88 christos if (kev->ident > INT_MAX
1221 1.88 christos || (fp = fd_getfile(fd = kev->ident)) == NULL) {
1222 1.49 ad rw_exit(&kqueue_filter_lock);
1223 1.49 ad kmem_free(newkn, sizeof(*newkn));
1224 1.49 ad return EBADF;
1225 1.49 ad }
1226 1.74 rmind mutex_enter(&fdp->fd_lock);
1227 1.65 ad ff = fdp->fd_dt->dt_ff[fd];
1228 1.98 christos if (ff->ff_refcnt & FR_CLOSING) {
1229 1.98 christos error = EBADF;
1230 1.98 christos goto doneunlock;
1231 1.98 christos }
1232 1.49 ad if (fd <= fdp->fd_lastkqfile) {
1233 1.49 ad SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
1234 1.1 lukem if (kq == kn->kn_kq &&
1235 1.1 lukem kev->filter == kn->kn_filter)
1236 1.1 lukem break;
1237 1.49 ad }
1238 1.1 lukem }
1239 1.1 lukem } else {
1240 1.3 jdolecek /*
1241 1.3 jdolecek * not monitoring a file descriptor, so
1242 1.3 jdolecek * lookup knotes in internal hash table
1243 1.3 jdolecek */
1244 1.74 rmind mutex_enter(&fdp->fd_lock);
1245 1.1 lukem if (fdp->fd_knhashmask != 0) {
1246 1.1 lukem list = &fdp->fd_knhash[
1247 1.1 lukem KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
1248 1.49 ad SLIST_FOREACH(kn, list, kn_link) {
1249 1.1 lukem if (kev->ident == kn->kn_id &&
1250 1.1 lukem kq == kn->kn_kq &&
1251 1.1 lukem kev->filter == kn->kn_filter)
1252 1.1 lukem break;
1253 1.49 ad }
1254 1.1 lukem }
1255 1.1 lukem }
1256 1.1 lukem
1257 1.1 lukem /*
1258 1.1 lukem * kn now contains the matching knote, or NULL if no match
1259 1.1 lukem */
1260 1.108 christos if (kn == NULL) {
1261 1.108 christos if (kev->flags & EV_ADD) {
1262 1.3 jdolecek /* create new knote */
1263 1.49 ad kn = newkn;
1264 1.49 ad newkn = NULL;
1265 1.49 ad kn->kn_obj = fp;
1266 1.79 christos kn->kn_id = kev->ident;
1267 1.1 lukem kn->kn_kq = kq;
1268 1.3 jdolecek kn->kn_fop = kfilter->filtops;
1269 1.49 ad kn->kn_kfilter = kfilter;
1270 1.49 ad kn->kn_sfflags = kev->fflags;
1271 1.49 ad kn->kn_sdata = kev->data;
1272 1.49 ad kev->fflags = 0;
1273 1.49 ad kev->data = 0;
1274 1.49 ad kn->kn_kevent = *kev;
1275 1.1 lukem
1276 1.85 christos KASSERT(kn->kn_fop != NULL);
1277 1.1 lukem /*
1278 1.1 lukem * apply reference count to knote structure, and
1279 1.1 lukem * do not release it at the end of this routine.
1280 1.1 lukem */
1281 1.1 lukem fp = NULL;
1282 1.1 lukem
1283 1.121 thorpej if (!(kn->kn_fop->f_flags & FILTEROP_ISFD)) {
1284 1.49 ad /*
1285 1.49 ad * If knote is not on an fd, store on
1286 1.49 ad * internal hash table.
1287 1.49 ad */
1288 1.49 ad if (fdp->fd_knhashmask == 0) {
1289 1.49 ad /* XXXAD can block with fd_lock held */
1290 1.49 ad fdp->fd_knhash = hashinit(KN_HASHSIZE,
1291 1.59 ad HASH_LIST, true,
1292 1.49 ad &fdp->fd_knhashmask);
1293 1.49 ad }
1294 1.49 ad list = &fdp->fd_knhash[KN_HASH(kn->kn_id,
1295 1.49 ad fdp->fd_knhashmask)];
1296 1.49 ad } else {
1297 1.49 ad /* Otherwise, knote is on an fd. */
1298 1.49 ad list = (struct klist *)
1299 1.65 ad &fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
1300 1.49 ad if ((int)kn->kn_id > fdp->fd_lastkqfile)
1301 1.49 ad fdp->fd_lastkqfile = kn->kn_id;
1302 1.49 ad }
1303 1.49 ad SLIST_INSERT_HEAD(list, kn, kn_link);
1304 1.1 lukem
1305 1.122 thorpej /*
1306 1.122 thorpej * N.B. kn->kn_fop may change as the result
1307 1.122 thorpej * of filter_attach()!
1308 1.122 thorpej */
1309 1.122 thorpej error = filter_attach(kn);
1310 1.49 ad if (error != 0) {
1311 1.100 christos #ifdef DEBUG
1312 1.105 christos struct proc *p = curlwp->l_proc;
1313 1.101 christos const file_t *ft = kn->kn_obj;
1314 1.105 christos printf("%s: %s[%d]: event type %d not "
1315 1.105 christos "supported for file type %d/%s "
1316 1.105 christos "(error %d)\n", __func__,
1317 1.105 christos p->p_comm, p->p_pid,
1318 1.101 christos kn->kn_filter, ft ? ft->f_type : -1,
1319 1.101 christos ft ? ft->f_ops->fo_name : "?", error);
1320 1.100 christos #endif
1321 1.100 christos
1322 1.49 ad /* knote_detach() drops fdp->fd_lock */
1323 1.49 ad knote_detach(kn, fdp, false);
1324 1.1 lukem goto done;
1325 1.1 lukem }
1326 1.49 ad atomic_inc_uint(&kfilter->refcnt);
1327 1.108 christos goto done_ev_add;
1328 1.1 lukem } else {
1329 1.108 christos /* No matching knote and the EV_ADD flag is not set. */
1330 1.108 christos error = ENOENT;
1331 1.108 christos goto doneunlock;
1332 1.1 lukem }
1333 1.108 christos }
1334 1.108 christos
1335 1.108 christos if (kev->flags & EV_DELETE) {
1336 1.108 christos /* knote_detach() drops fdp->fd_lock */
1337 1.108 christos knote_detach(kn, fdp, true);
1338 1.108 christos goto done;
1339 1.108 christos }
1340 1.108 christos
1341 1.108 christos /*
1342 1.108 christos * The user may change some filter values after the
1343 1.108 christos * initial EV_ADD, but doing so will not reset any
1344 1.108 christos * filter which have already been triggered.
1345 1.108 christos */
1346 1.108 christos kn->kn_kevent.udata = kev->udata;
1347 1.108 christos KASSERT(kn->kn_fop != NULL);
1348 1.121 thorpej if (!(kn->kn_fop->f_flags & FILTEROP_ISFD) &&
1349 1.121 thorpej kn->kn_fop->f_touch != NULL) {
1350 1.116 jdolecek mutex_spin_enter(&kq->kq_lock);
1351 1.122 thorpej filter_touch(kn, kev, EVENT_REGISTER);
1352 1.116 jdolecek mutex_spin_exit(&kq->kq_lock);
1353 1.49 ad } else {
1354 1.108 christos kn->kn_sfflags = kev->fflags;
1355 1.108 christos kn->kn_sdata = kev->data;
1356 1.1 lukem }
1357 1.1 lukem
1358 1.108 christos /*
1359 1.108 christos * We can get here if we are trying to attach
1360 1.108 christos * an event to a file descriptor that does not
1361 1.108 christos * support events, and the attach routine is
1362 1.108 christos * broken and does not return an error.
1363 1.108 christos */
1364 1.108 christos done_ev_add:
1365 1.122 thorpej rv = filter_event(kn, 0);
1366 1.108 christos if (rv)
1367 1.108 christos knote_activate(kn);
1368 1.108 christos
1369 1.3 jdolecek /* disable knote */
1370 1.49 ad if ((kev->flags & EV_DISABLE)) {
1371 1.49 ad mutex_spin_enter(&kq->kq_lock);
1372 1.49 ad if ((kn->kn_status & KN_DISABLED) == 0)
1373 1.49 ad kn->kn_status |= KN_DISABLED;
1374 1.49 ad mutex_spin_exit(&kq->kq_lock);
1375 1.1 lukem }
1376 1.1 lukem
1377 1.3 jdolecek /* enable knote */
1378 1.49 ad if ((kev->flags & EV_ENABLE)) {
1379 1.49 ad knote_enqueue(kn);
1380 1.1 lukem }
1381 1.98 christos doneunlock:
1382 1.49 ad mutex_exit(&fdp->fd_lock);
1383 1.3 jdolecek done:
1384 1.49 ad rw_exit(&kqueue_filter_lock);
1385 1.49 ad if (newkn != NULL)
1386 1.49 ad kmem_free(newkn, sizeof(*newkn));
1387 1.1 lukem if (fp != NULL)
1388 1.49 ad fd_putfile(fd);
1389 1.1 lukem return (error);
1390 1.1 lukem }
1391 1.1 lukem
1392 1.52 yamt #if defined(DEBUG)
1393 1.94 christos #define KN_FMT(buf, kn) \
1394 1.94 christos (snprintb((buf), sizeof(buf), __KN_FLAG_BITS, (kn)->kn_status), buf)
1395 1.94 christos
1396 1.52 yamt static void
1397 1.94 christos kqueue_check(const char *func, size_t line, const struct kqueue *kq)
1398 1.52 yamt {
1399 1.52 yamt const struct knote *kn;
1400 1.118 jdolecek u_int count;
1401 1.52 yamt int nmarker;
1402 1.94 christos char buf[128];
1403 1.52 yamt
1404 1.52 yamt KASSERT(mutex_owned(&kq->kq_lock));
1405 1.118 jdolecek KASSERT(KQ_COUNT(kq) < UINT_MAX / 2);
1406 1.52 yamt
1407 1.52 yamt count = 0;
1408 1.52 yamt nmarker = 0;
1409 1.52 yamt TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
1410 1.52 yamt if ((kn->kn_status & (KN_MARKER | KN_QUEUED)) == 0) {
1411 1.94 christos panic("%s,%zu: kq=%p kn=%p !(MARKER|QUEUED) %s",
1412 1.94 christos func, line, kq, kn, KN_FMT(buf, kn));
1413 1.52 yamt }
1414 1.52 yamt if ((kn->kn_status & KN_MARKER) == 0) {
1415 1.52 yamt if (kn->kn_kq != kq) {
1416 1.94 christos panic("%s,%zu: kq=%p kn(%p) != kn->kq(%p): %s",
1417 1.94 christos func, line, kq, kn, kn->kn_kq,
1418 1.94 christos KN_FMT(buf, kn));
1419 1.52 yamt }
1420 1.52 yamt if ((kn->kn_status & KN_ACTIVE) == 0) {
1421 1.94 christos panic("%s,%zu: kq=%p kn=%p: !ACTIVE %s",
1422 1.94 christos func, line, kq, kn, KN_FMT(buf, kn));
1423 1.52 yamt }
1424 1.52 yamt count++;
1425 1.118 jdolecek if (count > KQ_COUNT(kq)) {
1426 1.112 jdolecek panic("%s,%zu: kq=%p kq->kq_count(%d) != "
1427 1.112 jdolecek "count(%d), nmarker=%d",
1428 1.118 jdolecek func, line, kq, KQ_COUNT(kq), count,
1429 1.112 jdolecek nmarker);
1430 1.52 yamt }
1431 1.52 yamt } else {
1432 1.52 yamt nmarker++;
1433 1.52 yamt }
1434 1.52 yamt }
1435 1.52 yamt }
1436 1.94 christos #define kq_check(a) kqueue_check(__func__, __LINE__, (a))
1437 1.52 yamt #else /* defined(DEBUG) */
1438 1.52 yamt #define kq_check(a) /* nothing */
1439 1.52 yamt #endif /* defined(DEBUG) */
1440 1.52 yamt
1441 1.118 jdolecek static void
1442 1.118 jdolecek kqueue_restart(file_t *fp)
1443 1.118 jdolecek {
1444 1.118 jdolecek struct kqueue *kq = fp->f_kqueue;
1445 1.118 jdolecek KASSERT(kq != NULL);
1446 1.118 jdolecek
1447 1.118 jdolecek mutex_spin_enter(&kq->kq_lock);
1448 1.118 jdolecek kq->kq_count |= KQ_RESTART;
1449 1.118 jdolecek cv_broadcast(&kq->kq_cv);
1450 1.118 jdolecek mutex_spin_exit(&kq->kq_lock);
1451 1.118 jdolecek }
1452 1.118 jdolecek
1453 1.3 jdolecek /*
1454 1.3 jdolecek * Scan through the list of events on fp (for a maximum of maxevents),
1455 1.3 jdolecek * returning the results in to ulistp. Timeout is determined by tsp; if
1456 1.3 jdolecek * NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
1457 1.3 jdolecek * as appropriate.
1458 1.3 jdolecek */
1459 1.1 lukem static int
1460 1.49 ad kqueue_scan(file_t *fp, size_t maxevents, struct kevent *ulistp,
1461 1.49 ad const struct timespec *tsp, register_t *retval,
1462 1.49 ad const struct kevent_ops *keops, struct kevent *kevbuf,
1463 1.49 ad size_t kevcnt)
1464 1.1 lukem {
1465 1.3 jdolecek struct kqueue *kq;
1466 1.3 jdolecek struct kevent *kevp;
1467 1.62 christos struct timespec ats, sleepts;
1468 1.85 christos struct knote *kn, *marker, morker;
1469 1.24 cube size_t count, nkev, nevents;
1470 1.111 jdolecek int timeout, error, touch, rv, influx;
1471 1.49 ad filedesc_t *fdp;
1472 1.1 lukem
1473 1.49 ad fdp = curlwp->l_fd;
1474 1.82 matt kq = fp->f_kqueue;
1475 1.1 lukem count = maxevents;
1476 1.24 cube nkev = nevents = error = 0;
1477 1.49 ad if (count == 0) {
1478 1.49 ad *retval = 0;
1479 1.49 ad return 0;
1480 1.49 ad }
1481 1.1 lukem
1482 1.9 jdolecek if (tsp) { /* timeout supplied */
1483 1.63 christos ats = *tsp;
1484 1.62 christos if (inittimeleft(&ats, &sleepts) == -1) {
1485 1.49 ad *retval = maxevents;
1486 1.49 ad return EINVAL;
1487 1.1 lukem }
1488 1.62 christos timeout = tstohz(&ats);
1489 1.9 jdolecek if (timeout <= 0)
1490 1.29 kardel timeout = -1; /* do poll */
1491 1.1 lukem } else {
1492 1.9 jdolecek /* no timeout, wait forever */
1493 1.1 lukem timeout = 0;
1494 1.93 riastrad }
1495 1.1 lukem
1496 1.85 christos memset(&morker, 0, sizeof(morker));
1497 1.85 christos marker = &morker;
1498 1.49 ad marker->kn_status = KN_MARKER;
1499 1.49 ad mutex_spin_enter(&kq->kq_lock);
1500 1.3 jdolecek retry:
1501 1.49 ad kevp = kevbuf;
1502 1.118 jdolecek if (KQ_COUNT(kq) == 0) {
1503 1.49 ad if (timeout >= 0) {
1504 1.49 ad error = cv_timedwait_sig(&kq->kq_cv,
1505 1.49 ad &kq->kq_lock, timeout);
1506 1.49 ad if (error == 0) {
1507 1.118 jdolecek if (KQ_COUNT(kq) == 0 &&
1508 1.118 jdolecek (kq->kq_count & KQ_RESTART)) {
1509 1.118 jdolecek /* return to clear file reference */
1510 1.118 jdolecek error = ERESTART;
1511 1.118 jdolecek } else if (tsp == NULL || (timeout =
1512 1.118 jdolecek gettimeleft(&ats, &sleepts)) > 0) {
1513 1.49 ad goto retry;
1514 1.118 jdolecek }
1515 1.49 ad } else {
1516 1.49 ad /* don't restart after signals... */
1517 1.49 ad if (error == ERESTART)
1518 1.49 ad error = EINTR;
1519 1.49 ad if (error == EWOULDBLOCK)
1520 1.49 ad error = 0;
1521 1.49 ad }
1522 1.1 lukem }
1523 1.92 christos mutex_spin_exit(&kq->kq_lock);
1524 1.110 jdolecek goto done;
1525 1.110 jdolecek }
1526 1.110 jdolecek
1527 1.110 jdolecek /* mark end of knote list */
1528 1.110 jdolecek TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1529 1.111 jdolecek influx = 0;
1530 1.1 lukem
1531 1.110 jdolecek /*
1532 1.110 jdolecek * Acquire the fdp->fd_lock interlock to avoid races with
1533 1.110 jdolecek * file creation/destruction from other threads.
1534 1.110 jdolecek */
1535 1.111 jdolecek relock:
1536 1.110 jdolecek mutex_spin_exit(&kq->kq_lock);
1537 1.110 jdolecek mutex_enter(&fdp->fd_lock);
1538 1.110 jdolecek mutex_spin_enter(&kq->kq_lock);
1539 1.92 christos
1540 1.110 jdolecek while (count != 0) {
1541 1.110 jdolecek kn = TAILQ_FIRST(&kq->kq_head); /* get next knote */
1542 1.111 jdolecek
1543 1.111 jdolecek if ((kn->kn_status & KN_MARKER) != 0 && kn != marker) {
1544 1.111 jdolecek if (influx) {
1545 1.111 jdolecek influx = 0;
1546 1.111 jdolecek KQ_FLUX_WAKEUP(kq);
1547 1.111 jdolecek }
1548 1.111 jdolecek mutex_exit(&fdp->fd_lock);
1549 1.114 jdolecek (void)cv_wait(&kq->kq_cv, &kq->kq_lock);
1550 1.111 jdolecek goto relock;
1551 1.111 jdolecek }
1552 1.111 jdolecek
1553 1.111 jdolecek TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1554 1.111 jdolecek if (kn == marker) {
1555 1.111 jdolecek /* it's our marker, stop */
1556 1.111 jdolecek KQ_FLUX_WAKEUP(kq);
1557 1.111 jdolecek if (count == maxevents) {
1558 1.110 jdolecek mutex_exit(&fdp->fd_lock);
1559 1.110 jdolecek goto retry;
1560 1.49 ad }
1561 1.111 jdolecek break;
1562 1.110 jdolecek }
1563 1.111 jdolecek KASSERT((kn->kn_status & KN_BUSY) == 0);
1564 1.111 jdolecek
1565 1.110 jdolecek kq_check(kq);
1566 1.115 jdolecek kn->kn_status &= ~KN_QUEUED;
1567 1.110 jdolecek kn->kn_status |= KN_BUSY;
1568 1.110 jdolecek kq_check(kq);
1569 1.110 jdolecek if (kn->kn_status & KN_DISABLED) {
1570 1.115 jdolecek kn->kn_status &= ~KN_BUSY;
1571 1.111 jdolecek kq->kq_count--;
1572 1.110 jdolecek /* don't want disabled events */
1573 1.110 jdolecek continue;
1574 1.110 jdolecek }
1575 1.110 jdolecek if ((kn->kn_flags & EV_ONESHOT) == 0) {
1576 1.110 jdolecek mutex_spin_exit(&kq->kq_lock);
1577 1.110 jdolecek KASSERT(mutex_owned(&fdp->fd_lock));
1578 1.122 thorpej rv = filter_event(kn, 0);
1579 1.110 jdolecek mutex_spin_enter(&kq->kq_lock);
1580 1.115 jdolecek /* Re-poll if note was re-enqueued. */
1581 1.115 jdolecek if ((kn->kn_status & KN_QUEUED) != 0) {
1582 1.115 jdolecek kn->kn_status &= ~KN_BUSY;
1583 1.115 jdolecek /* Re-enqueue raised kq_count, lower it again */
1584 1.115 jdolecek kq->kq_count--;
1585 1.115 jdolecek influx = 1;
1586 1.115 jdolecek continue;
1587 1.115 jdolecek }
1588 1.110 jdolecek if (rv == 0) {
1589 1.110 jdolecek /*
1590 1.110 jdolecek * non-ONESHOT event that hasn't
1591 1.110 jdolecek * triggered again, so de-queue.
1592 1.110 jdolecek */
1593 1.115 jdolecek kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
1594 1.111 jdolecek kq->kq_count--;
1595 1.111 jdolecek influx = 1;
1596 1.110 jdolecek continue;
1597 1.49 ad }
1598 1.110 jdolecek }
1599 1.110 jdolecek KASSERT(kn->kn_fop != NULL);
1600 1.121 thorpej touch = (!(kn->kn_fop->f_flags & FILTEROP_ISFD) &&
1601 1.110 jdolecek kn->kn_fop->f_touch != NULL);
1602 1.110 jdolecek /* XXXAD should be got from f_event if !oneshot. */
1603 1.110 jdolecek if (touch) {
1604 1.122 thorpej filter_touch(kn, kevp, EVENT_PROCESS);
1605 1.110 jdolecek } else {
1606 1.110 jdolecek *kevp = kn->kn_kevent;
1607 1.110 jdolecek }
1608 1.110 jdolecek kevp++;
1609 1.110 jdolecek nkev++;
1610 1.111 jdolecek influx = 1;
1611 1.110 jdolecek if (kn->kn_flags & EV_ONESHOT) {
1612 1.110 jdolecek /* delete ONESHOT events after retrieval */
1613 1.115 jdolecek kn->kn_status &= ~KN_BUSY;
1614 1.111 jdolecek kq->kq_count--;
1615 1.110 jdolecek mutex_spin_exit(&kq->kq_lock);
1616 1.110 jdolecek knote_detach(kn, fdp, true);
1617 1.110 jdolecek mutex_enter(&fdp->fd_lock);
1618 1.110 jdolecek mutex_spin_enter(&kq->kq_lock);
1619 1.110 jdolecek } else if (kn->kn_flags & EV_CLEAR) {
1620 1.110 jdolecek /* clear state after retrieval */
1621 1.110 jdolecek kn->kn_data = 0;
1622 1.110 jdolecek kn->kn_fflags = 0;
1623 1.110 jdolecek /*
1624 1.110 jdolecek * Manually clear knotes who weren't
1625 1.110 jdolecek * 'touch'ed.
1626 1.110 jdolecek */
1627 1.110 jdolecek if (touch == 0) {
1628 1.49 ad kn->kn_data = 0;
1629 1.49 ad kn->kn_fflags = 0;
1630 1.49 ad }
1631 1.115 jdolecek kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
1632 1.111 jdolecek kq->kq_count--;
1633 1.110 jdolecek } else if (kn->kn_flags & EV_DISPATCH) {
1634 1.110 jdolecek kn->kn_status |= KN_DISABLED;
1635 1.115 jdolecek kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
1636 1.111 jdolecek kq->kq_count--;
1637 1.110 jdolecek } else {
1638 1.110 jdolecek /* add event back on list */
1639 1.110 jdolecek kq_check(kq);
1640 1.115 jdolecek kn->kn_status |= KN_QUEUED;
1641 1.110 jdolecek kn->kn_status &= ~KN_BUSY;
1642 1.110 jdolecek TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1643 1.110 jdolecek kq_check(kq);
1644 1.110 jdolecek }
1645 1.111 jdolecek
1646 1.110 jdolecek if (nkev == kevcnt) {
1647 1.110 jdolecek /* do copyouts in kevcnt chunks */
1648 1.111 jdolecek influx = 0;
1649 1.111 jdolecek KQ_FLUX_WAKEUP(kq);
1650 1.110 jdolecek mutex_spin_exit(&kq->kq_lock);
1651 1.110 jdolecek mutex_exit(&fdp->fd_lock);
1652 1.110 jdolecek error = (*keops->keo_put_events)
1653 1.110 jdolecek (keops->keo_private,
1654 1.110 jdolecek kevbuf, ulistp, nevents, nkev);
1655 1.110 jdolecek mutex_enter(&fdp->fd_lock);
1656 1.110 jdolecek mutex_spin_enter(&kq->kq_lock);
1657 1.110 jdolecek nevents += nkev;
1658 1.110 jdolecek nkev = 0;
1659 1.110 jdolecek kevp = kevbuf;
1660 1.110 jdolecek }
1661 1.110 jdolecek count--;
1662 1.110 jdolecek if (error != 0 || count == 0) {
1663 1.110 jdolecek /* remove marker */
1664 1.110 jdolecek TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1665 1.110 jdolecek break;
1666 1.1 lukem }
1667 1.1 lukem }
1668 1.111 jdolecek KQ_FLUX_WAKEUP(kq);
1669 1.110 jdolecek mutex_spin_exit(&kq->kq_lock);
1670 1.110 jdolecek mutex_exit(&fdp->fd_lock);
1671 1.110 jdolecek
1672 1.110 jdolecek done:
1673 1.49 ad if (nkev != 0) {
1674 1.3 jdolecek /* copyout remaining events */
1675 1.24 cube error = (*keops->keo_put_events)(keops->keo_private,
1676 1.49 ad kevbuf, ulistp, nevents, nkev);
1677 1.49 ad }
1678 1.3 jdolecek *retval = maxevents - count;
1679 1.3 jdolecek
1680 1.49 ad return error;
1681 1.1 lukem }
1682 1.1 lukem
1683 1.1 lukem /*
1684 1.49 ad * fileops ioctl method for a kqueue descriptor.
1685 1.3 jdolecek *
1686 1.3 jdolecek * Two ioctls are currently supported. They both use struct kfilter_mapping:
1687 1.3 jdolecek * KFILTER_BYNAME find name for filter, and return result in
1688 1.3 jdolecek * name, which is of size len.
1689 1.3 jdolecek * KFILTER_BYFILTER find filter for name. len is ignored.
1690 1.3 jdolecek */
1691 1.1 lukem /*ARGSUSED*/
1692 1.1 lukem static int
1693 1.49 ad kqueue_ioctl(file_t *fp, u_long com, void *data)
1694 1.1 lukem {
1695 1.3 jdolecek struct kfilter_mapping *km;
1696 1.3 jdolecek const struct kfilter *kfilter;
1697 1.3 jdolecek char *name;
1698 1.3 jdolecek int error;
1699 1.3 jdolecek
1700 1.49 ad km = data;
1701 1.3 jdolecek error = 0;
1702 1.49 ad name = kmem_alloc(KFILTER_MAXNAME, KM_SLEEP);
1703 1.3 jdolecek
1704 1.3 jdolecek switch (com) {
1705 1.3 jdolecek case KFILTER_BYFILTER: /* convert filter -> name */
1706 1.49 ad rw_enter(&kqueue_filter_lock, RW_READER);
1707 1.3 jdolecek kfilter = kfilter_byfilter(km->filter);
1708 1.49 ad if (kfilter != NULL) {
1709 1.49 ad strlcpy(name, kfilter->name, KFILTER_MAXNAME);
1710 1.49 ad rw_exit(&kqueue_filter_lock);
1711 1.49 ad error = copyoutstr(name, km->name, km->len, NULL);
1712 1.49 ad } else {
1713 1.49 ad rw_exit(&kqueue_filter_lock);
1714 1.3 jdolecek error = ENOENT;
1715 1.49 ad }
1716 1.3 jdolecek break;
1717 1.3 jdolecek
1718 1.3 jdolecek case KFILTER_BYNAME: /* convert name -> filter */
1719 1.3 jdolecek error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
1720 1.3 jdolecek if (error) {
1721 1.3 jdolecek break;
1722 1.3 jdolecek }
1723 1.49 ad rw_enter(&kqueue_filter_lock, RW_READER);
1724 1.3 jdolecek kfilter = kfilter_byname(name);
1725 1.3 jdolecek if (kfilter != NULL)
1726 1.3 jdolecek km->filter = kfilter->filter;
1727 1.3 jdolecek else
1728 1.3 jdolecek error = ENOENT;
1729 1.49 ad rw_exit(&kqueue_filter_lock);
1730 1.3 jdolecek break;
1731 1.3 jdolecek
1732 1.3 jdolecek default:
1733 1.3 jdolecek error = ENOTTY;
1734 1.49 ad break;
1735 1.3 jdolecek
1736 1.3 jdolecek }
1737 1.49 ad kmem_free(name, KFILTER_MAXNAME);
1738 1.3 jdolecek return (error);
1739 1.3 jdolecek }
1740 1.3 jdolecek
1741 1.3 jdolecek /*
1742 1.49 ad * fileops fcntl method for a kqueue descriptor.
1743 1.3 jdolecek */
1744 1.3 jdolecek static int
1745 1.49 ad kqueue_fcntl(file_t *fp, u_int com, void *data)
1746 1.3 jdolecek {
1747 1.3 jdolecek
1748 1.1 lukem return (ENOTTY);
1749 1.1 lukem }
1750 1.1 lukem
1751 1.3 jdolecek /*
1752 1.49 ad * fileops poll method for a kqueue descriptor.
1753 1.3 jdolecek * Determine if kqueue has events pending.
1754 1.3 jdolecek */
1755 1.1 lukem static int
1756 1.49 ad kqueue_poll(file_t *fp, int events)
1757 1.1 lukem {
1758 1.3 jdolecek struct kqueue *kq;
1759 1.3 jdolecek int revents;
1760 1.3 jdolecek
1761 1.82 matt kq = fp->f_kqueue;
1762 1.49 ad
1763 1.3 jdolecek revents = 0;
1764 1.3 jdolecek if (events & (POLLIN | POLLRDNORM)) {
1765 1.49 ad mutex_spin_enter(&kq->kq_lock);
1766 1.118 jdolecek if (KQ_COUNT(kq) != 0) {
1767 1.3 jdolecek revents |= events & (POLLIN | POLLRDNORM);
1768 1.1 lukem } else {
1769 1.49 ad selrecord(curlwp, &kq->kq_sel);
1770 1.1 lukem }
1771 1.52 yamt kq_check(kq);
1772 1.49 ad mutex_spin_exit(&kq->kq_lock);
1773 1.1 lukem }
1774 1.49 ad
1775 1.49 ad return revents;
1776 1.1 lukem }
1777 1.1 lukem
1778 1.3 jdolecek /*
1779 1.49 ad * fileops stat method for a kqueue descriptor.
1780 1.3 jdolecek * Returns dummy info, with st_size being number of events pending.
1781 1.3 jdolecek */
1782 1.1 lukem static int
1783 1.49 ad kqueue_stat(file_t *fp, struct stat *st)
1784 1.1 lukem {
1785 1.49 ad struct kqueue *kq;
1786 1.49 ad
1787 1.82 matt kq = fp->f_kqueue;
1788 1.1 lukem
1789 1.49 ad memset(st, 0, sizeof(*st));
1790 1.118 jdolecek st->st_size = KQ_COUNT(kq);
1791 1.1 lukem st->st_blksize = sizeof(struct kevent);
1792 1.1 lukem st->st_mode = S_IFIFO;
1793 1.49 ad
1794 1.49 ad return 0;
1795 1.49 ad }
1796 1.49 ad
1797 1.49 ad static void
1798 1.49 ad kqueue_doclose(struct kqueue *kq, struct klist *list, int fd)
1799 1.49 ad {
1800 1.49 ad struct knote *kn;
1801 1.49 ad filedesc_t *fdp;
1802 1.49 ad
1803 1.49 ad fdp = kq->kq_fdp;
1804 1.49 ad
1805 1.49 ad KASSERT(mutex_owned(&fdp->fd_lock));
1806 1.49 ad
1807 1.49 ad for (kn = SLIST_FIRST(list); kn != NULL;) {
1808 1.49 ad if (kq != kn->kn_kq) {
1809 1.49 ad kn = SLIST_NEXT(kn, kn_link);
1810 1.49 ad continue;
1811 1.49 ad }
1812 1.49 ad knote_detach(kn, fdp, true);
1813 1.49 ad mutex_enter(&fdp->fd_lock);
1814 1.49 ad kn = SLIST_FIRST(list);
1815 1.49 ad }
1816 1.1 lukem }
1817 1.1 lukem
1818 1.49 ad
1819 1.3 jdolecek /*
1820 1.49 ad * fileops close method for a kqueue descriptor.
1821 1.3 jdolecek */
1822 1.1 lukem static int
1823 1.49 ad kqueue_close(file_t *fp)
1824 1.1 lukem {
1825 1.49 ad struct kqueue *kq;
1826 1.49 ad filedesc_t *fdp;
1827 1.49 ad fdfile_t *ff;
1828 1.49 ad int i;
1829 1.49 ad
1830 1.82 matt kq = fp->f_kqueue;
1831 1.82 matt fp->f_kqueue = NULL;
1832 1.79 christos fp->f_type = 0;
1833 1.49 ad fdp = curlwp->l_fd;
1834 1.1 lukem
1835 1.49 ad mutex_enter(&fdp->fd_lock);
1836 1.49 ad for (i = 0; i <= fdp->fd_lastkqfile; i++) {
1837 1.65 ad if ((ff = fdp->fd_dt->dt_ff[i]) == NULL)
1838 1.49 ad continue;
1839 1.49 ad kqueue_doclose(kq, (struct klist *)&ff->ff_knlist, i);
1840 1.1 lukem }
1841 1.1 lukem if (fdp->fd_knhashmask != 0) {
1842 1.1 lukem for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
1843 1.49 ad kqueue_doclose(kq, &fdp->fd_knhash[i], -1);
1844 1.1 lukem }
1845 1.1 lukem }
1846 1.49 ad mutex_exit(&fdp->fd_lock);
1847 1.49 ad
1848 1.118 jdolecek KASSERT(KQ_COUNT(kq) == 0);
1849 1.49 ad mutex_destroy(&kq->kq_lock);
1850 1.49 ad cv_destroy(&kq->kq_cv);
1851 1.48 rmind seldestroy(&kq->kq_sel);
1852 1.49 ad kmem_free(kq, sizeof(*kq));
1853 1.1 lukem
1854 1.1 lukem return (0);
1855 1.1 lukem }
1856 1.1 lukem
1857 1.3 jdolecek /*
1858 1.3 jdolecek * struct fileops kqfilter method for a kqueue descriptor.
1859 1.3 jdolecek * Event triggered when monitored kqueue changes.
1860 1.3 jdolecek */
1861 1.3 jdolecek static int
1862 1.49 ad kqueue_kqfilter(file_t *fp, struct knote *kn)
1863 1.3 jdolecek {
1864 1.3 jdolecek struct kqueue *kq;
1865 1.49 ad
1866 1.82 matt kq = ((file_t *)kn->kn_obj)->f_kqueue;
1867 1.49 ad
1868 1.49 ad KASSERT(fp == kn->kn_obj);
1869 1.3 jdolecek
1870 1.3 jdolecek if (kn->kn_filter != EVFILT_READ)
1871 1.49 ad return 1;
1872 1.49 ad
1873 1.3 jdolecek kn->kn_fop = &kqread_filtops;
1874 1.49 ad mutex_enter(&kq->kq_lock);
1875 1.109 thorpej selrecord_knote(&kq->kq_sel, kn);
1876 1.49 ad mutex_exit(&kq->kq_lock);
1877 1.49 ad
1878 1.49 ad return 0;
1879 1.3 jdolecek }
1880 1.3 jdolecek
1881 1.3 jdolecek
1882 1.3 jdolecek /*
1883 1.49 ad * Walk down a list of knotes, activating them if their event has
1884 1.49 ad * triggered. The caller's object lock (e.g. device driver lock)
1885 1.49 ad * must be held.
1886 1.1 lukem */
1887 1.1 lukem void
1888 1.1 lukem knote(struct klist *list, long hint)
1889 1.1 lukem {
1890 1.71 drochner struct knote *kn, *tmpkn;
1891 1.1 lukem
1892 1.71 drochner SLIST_FOREACH_SAFE(kn, list, kn_selnext, tmpkn) {
1893 1.85 christos KASSERT(kn->kn_fop != NULL);
1894 1.84 christos KASSERT(kn->kn_fop->f_event != NULL);
1895 1.49 ad if ((*kn->kn_fop->f_event)(kn, hint))
1896 1.49 ad knote_activate(kn);
1897 1.49 ad }
1898 1.1 lukem }
1899 1.1 lukem
1900 1.1 lukem /*
1901 1.49 ad * Remove all knotes referencing a specified fd
1902 1.1 lukem */
1903 1.1 lukem void
1904 1.49 ad knote_fdclose(int fd)
1905 1.1 lukem {
1906 1.49 ad struct klist *list;
1907 1.1 lukem struct knote *kn;
1908 1.49 ad filedesc_t *fdp;
1909 1.1 lukem
1910 1.49 ad fdp = curlwp->l_fd;
1911 1.106 riastrad mutex_enter(&fdp->fd_lock);
1912 1.65 ad list = (struct klist *)&fdp->fd_dt->dt_ff[fd]->ff_knlist;
1913 1.1 lukem while ((kn = SLIST_FIRST(list)) != NULL) {
1914 1.49 ad knote_detach(kn, fdp, true);
1915 1.49 ad mutex_enter(&fdp->fd_lock);
1916 1.1 lukem }
1917 1.49 ad mutex_exit(&fdp->fd_lock);
1918 1.1 lukem }
1919 1.1 lukem
1920 1.1 lukem /*
1921 1.49 ad * Drop knote. Called with fdp->fd_lock held, and will drop before
1922 1.49 ad * returning.
1923 1.3 jdolecek */
1924 1.1 lukem static void
1925 1.49 ad knote_detach(struct knote *kn, filedesc_t *fdp, bool dofop)
1926 1.1 lukem {
1927 1.49 ad struct klist *list;
1928 1.53 ad struct kqueue *kq;
1929 1.53 ad
1930 1.53 ad kq = kn->kn_kq;
1931 1.1 lukem
1932 1.49 ad KASSERT((kn->kn_status & KN_MARKER) == 0);
1933 1.49 ad KASSERT(mutex_owned(&fdp->fd_lock));
1934 1.3 jdolecek
1935 1.85 christos KASSERT(kn->kn_fop != NULL);
1936 1.53 ad /* Remove from monitored object. */
1937 1.49 ad if (dofop) {
1938 1.122 thorpej filter_detach(kn);
1939 1.1 lukem }
1940 1.3 jdolecek
1941 1.53 ad /* Remove from descriptor table. */
1942 1.121 thorpej if (kn->kn_fop->f_flags & FILTEROP_ISFD)
1943 1.65 ad list = (struct klist *)&fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
1944 1.1 lukem else
1945 1.1 lukem list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
1946 1.1 lukem
1947 1.1 lukem SLIST_REMOVE(list, kn, knote, kn_link);
1948 1.53 ad
1949 1.53 ad /* Remove from kqueue. */
1950 1.85 christos again:
1951 1.53 ad mutex_spin_enter(&kq->kq_lock);
1952 1.53 ad if ((kn->kn_status & KN_QUEUED) != 0) {
1953 1.53 ad kq_check(kq);
1954 1.85 christos kq->kq_count--;
1955 1.53 ad TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1956 1.53 ad kn->kn_status &= ~KN_QUEUED;
1957 1.53 ad kq_check(kq);
1958 1.85 christos } else if (kn->kn_status & KN_BUSY) {
1959 1.85 christos mutex_spin_exit(&kq->kq_lock);
1960 1.85 christos goto again;
1961 1.53 ad }
1962 1.53 ad mutex_spin_exit(&kq->kq_lock);
1963 1.53 ad
1964 1.49 ad mutex_exit(&fdp->fd_lock);
1965 1.121 thorpej if (kn->kn_fop->f_flags & FILTEROP_ISFD)
1966 1.49 ad fd_putfile(kn->kn_id);
1967 1.49 ad atomic_dec_uint(&kn->kn_kfilter->refcnt);
1968 1.49 ad kmem_free(kn, sizeof(*kn));
1969 1.1 lukem }
1970 1.1 lukem
1971 1.3 jdolecek /*
1972 1.3 jdolecek * Queue new event for knote.
1973 1.3 jdolecek */
1974 1.1 lukem static void
1975 1.1 lukem knote_enqueue(struct knote *kn)
1976 1.1 lukem {
1977 1.49 ad struct kqueue *kq;
1978 1.49 ad
1979 1.49 ad KASSERT((kn->kn_status & KN_MARKER) == 0);
1980 1.1 lukem
1981 1.3 jdolecek kq = kn->kn_kq;
1982 1.1 lukem
1983 1.49 ad mutex_spin_enter(&kq->kq_lock);
1984 1.49 ad if ((kn->kn_status & KN_DISABLED) != 0) {
1985 1.49 ad kn->kn_status &= ~KN_DISABLED;
1986 1.49 ad }
1987 1.49 ad if ((kn->kn_status & (KN_ACTIVE | KN_QUEUED)) == KN_ACTIVE) {
1988 1.52 yamt kq_check(kq);
1989 1.85 christos kn->kn_status |= KN_QUEUED;
1990 1.49 ad TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1991 1.49 ad kq->kq_count++;
1992 1.52 yamt kq_check(kq);
1993 1.49 ad cv_broadcast(&kq->kq_cv);
1994 1.49 ad selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
1995 1.49 ad }
1996 1.49 ad mutex_spin_exit(&kq->kq_lock);
1997 1.1 lukem }
1998 1.49 ad /*
1999 1.49 ad * Queue new event for knote.
2000 1.49 ad */
2001 1.49 ad static void
2002 1.49 ad knote_activate(struct knote *kn)
2003 1.49 ad {
2004 1.49 ad struct kqueue *kq;
2005 1.49 ad
2006 1.49 ad KASSERT((kn->kn_status & KN_MARKER) == 0);
2007 1.1 lukem
2008 1.3 jdolecek kq = kn->kn_kq;
2009 1.12 pk
2010 1.49 ad mutex_spin_enter(&kq->kq_lock);
2011 1.49 ad kn->kn_status |= KN_ACTIVE;
2012 1.49 ad if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) {
2013 1.52 yamt kq_check(kq);
2014 1.85 christos kn->kn_status |= KN_QUEUED;
2015 1.49 ad TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2016 1.49 ad kq->kq_count++;
2017 1.52 yamt kq_check(kq);
2018 1.49 ad cv_broadcast(&kq->kq_cv);
2019 1.49 ad selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
2020 1.49 ad }
2021 1.49 ad mutex_spin_exit(&kq->kq_lock);
2022 1.1 lukem }
2023