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