]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_generic.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / sys_generic.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
62 */
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/filedesc.h>
67 #include <sys/ioctl.h>
68 #include <sys/file_internal.h>
69 #include <sys/proc_internal.h>
70 #include <sys/socketvar.h>
71 #if KTRACE
72 #include <sys/uio_internal.h>
73 #else
74 #include <sys/uio.h>
75 #endif
76 #include <sys/kernel.h>
77 #include <sys/stat.h>
78 #include <sys/malloc.h>
79 #include <sys/sysproto.h>
80
81 #include <sys/mount_internal.h>
82 #include <sys/protosw.h>
83 #include <sys/ev.h>
84 #include <sys/user.h>
85 #include <sys/kdebug.h>
86 #include <sys/poll.h>
87 #include <sys/event.h>
88 #include <sys/eventvar.h>
89
90 #include <mach/mach_types.h>
91 #include <kern/kern_types.h>
92 #include <kern/assert.h>
93 #include <kern/kalloc.h>
94 #include <kern/thread.h>
95 #include <kern/clock.h>
96
97 #include <sys/mbuf.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/errno.h>
101 #include <sys/syscall.h>
102 #include <sys/pipe.h>
103
104 #include <bsm/audit_kernel.h>
105
106 #include <net/if.h>
107 #include <net/route.h>
108
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/ip.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/ip6.h>
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcpip.h>
121 #include <netinet/tcp_debug.h>
122 /* for wait queue based select */
123 #include <kern/wait_queue.h>
124 #include <kern/kalloc.h>
125 #if KTRACE
126 #include <sys/ktrace.h>
127 #endif
128 #include <sys/vnode_internal.h>
129
130 int rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
131 int wr_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
132 extern void *get_bsduthreadarg(thread_t);
133 extern int *get_bsduthreadrval(thread_t);
134
135 __private_extern__ int dofileread(struct proc *p, struct fileproc *fp, int fd,
136 user_addr_t bufp, user_size_t nbyte,
137 off_t offset, int flags, user_ssize_t *retval);
138 __private_extern__ int dofilewrite(struct proc *p, struct fileproc *fp, int fd,
139 user_addr_t bufp, user_size_t nbyte,
140 off_t offset, int flags, user_ssize_t *retval);
141 __private_extern__ int preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_vnode);
142 __private_extern__ void donefileread(struct proc *p, struct fileproc *fp_ret, int fd);
143
144 #if NETAT
145 extern int appletalk_inited;
146 #endif /* NETAT */
147
148 #define f_flag f_fglob->fg_flag
149 #define f_type f_fglob->fg_type
150 #define f_msgcount f_fglob->fg_msgcount
151 #define f_cred f_fglob->fg_cred
152 #define f_ops f_fglob->fg_ops
153 #define f_offset f_fglob->fg_offset
154 #define f_data f_fglob->fg_data
155 /*
156 * Read system call.
157 */
158 int
159 read(p, uap, retval)
160 struct proc *p;
161 register struct read_args *uap;
162 user_ssize_t *retval;
163 {
164 struct fileproc *fp;
165 int error;
166 int fd = uap->fd;
167
168 if ( (error = preparefileread(p, &fp, fd, 0)) )
169 return (error);
170
171 error = dofileread(p, fp, uap->fd, uap->cbuf, uap->nbyte,
172 (off_t)-1, 0, retval);
173
174 donefileread(p, fp, fd);
175
176 return (error);
177 }
178
179 /*
180 * Pread system call
181 */
182 int
183 pread(p, uap, retval)
184 struct proc *p;
185 register struct pread_args *uap;
186 user_ssize_t *retval;
187 {
188 struct fileproc *fp;
189 int fd = uap->fd;
190 int error;
191
192 if ( (error = preparefileread(p, &fp, fd, 1)) )
193 return (error);
194
195 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
196 uap->offset, FOF_OFFSET, retval);
197
198 donefileread(p, fp, fd);
199
200 if (!error)
201 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pread) | DBG_FUNC_NONE),
202 uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
203
204 return (error);
205 }
206
207 /*
208 * Code common for read and pread
209 */
210
211 void
212 donefileread(struct proc *p, struct fileproc *fp, int fd)
213 {
214 proc_fdlock(p);
215
216 fp->f_flags &= ~FP_INCHRREAD;
217
218 fp_drop(p, fd, fp, 1);
219 proc_fdunlock(p);
220 }
221
222 int
223 preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_pread)
224 {
225 vnode_t vp;
226 int error;
227 struct fileproc *fp;
228
229 proc_fdlock(p);
230
231 error = fp_lookup(p, fd, &fp, 1);
232
233 if (error) {
234 proc_fdunlock(p);
235 return (error);
236 }
237 if ((fp->f_flag & FREAD) == 0) {
238 error = EBADF;
239 goto out;
240 }
241 if (check_for_pread && (fp->f_type != DTYPE_VNODE)) {
242 error = ESPIPE;
243 goto out;
244 }
245 if (fp->f_type == DTYPE_VNODE) {
246 vp = (struct vnode *)fp->f_fglob->fg_data;
247
248 if (vp->v_type == VCHR)
249 fp->f_flags |= FP_INCHRREAD;
250 }
251
252 *fp_ret = fp;
253
254 proc_fdunlock(p);
255 return (0);
256
257 out:
258 fp_drop(p, fd, fp, 1);
259 proc_fdunlock(p);
260 return (error);
261 }
262
263
264 __private_extern__ int
265 dofileread(p, fp, fd, bufp, nbyte, offset, flags, retval)
266 struct proc *p;
267 struct fileproc *fp;
268 int fd, flags;
269 user_addr_t bufp;
270 user_size_t nbyte;
271 off_t offset;
272 user_ssize_t *retval;
273 {
274 uio_t auio;
275 user_ssize_t bytecnt;
276 long error = 0;
277 char uio_buf[ UIO_SIZEOF(1) ];
278 #if KTRACE
279 uio_t ktruio = NULL;
280 char ktr_uio_buf[ UIO_SIZEOF(1) ];
281 int didktr = 0;
282 #endif
283
284 // LP64todo - do we want to raise this?
285 if (nbyte > INT_MAX)
286 return (EINVAL);
287
288 if (IS_64BIT_PROCESS(p)) {
289 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ,
290 &uio_buf[0], sizeof(uio_buf));
291 } else {
292 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ,
293 &uio_buf[0], sizeof(uio_buf));
294 }
295 uio_addiov(auio, bufp, nbyte);
296
297 #if KTRACE
298 /*
299 * if tracing, save a copy of iovec
300 */
301 if (KTRPOINT(p, KTR_GENIO)) {
302 didktr = 1;
303
304 if (IS_64BIT_PROCESS(p)) {
305 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ,
306 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
307 } else {
308 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ,
309 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
310 }
311 uio_addiov(ktruio, bufp, nbyte);
312 }
313 #endif
314 bytecnt = nbyte;
315
316 if ((error = fo_read(fp, auio, fp->f_cred, flags, p))) {
317 if (uio_resid(auio) != bytecnt && (error == ERESTART ||
318 error == EINTR || error == EWOULDBLOCK))
319 error = 0;
320 }
321 bytecnt -= uio_resid(auio);
322 #if KTRACE
323 if (didktr && error == 0) {
324 uio_setresid(ktruio, bytecnt);
325 ktrgenio(p->p_tracep, fd, UIO_READ, ktruio, error);
326 }
327 #endif
328
329 *retval = bytecnt;
330
331 return (error);
332 }
333
334 /*
335 * Scatter read system call.
336 */
337 int
338 readv(p, uap, retval)
339 struct proc *p;
340 register struct readv_args *uap;
341 user_ssize_t *retval;
342 {
343 uio_t auio = NULL;
344 int error;
345 int size_of_iovec;
346 struct user_iovec *iovp;
347
348 /* Verify range bedfore calling uio_create() */
349 if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
350 return (EINVAL);
351
352 /* allocate a uio large enough to hold the number of iovecs passed */
353 auio = uio_create(uap->iovcnt, 0,
354 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
355 UIO_READ);
356
357 /* get location of iovecs within the uio. then copyin the iovecs from
358 * user space.
359 */
360 iovp = uio_iovsaddr(auio);
361 if (iovp == NULL) {
362 error = ENOMEM;
363 goto ExitThisRoutine;
364 }
365 size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
366 error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
367 if (error) {
368 goto ExitThisRoutine;
369 }
370
371 /* finalize uio_t for use and do the IO
372 */
373 uio_calculateresid(auio);
374 error = rd_uio(p, uap->fd, auio, retval);
375
376 ExitThisRoutine:
377 if (auio != NULL) {
378 uio_free(auio);
379 }
380 return (error);
381 }
382
383 /*
384 * Write system call
385 */
386 int
387 write(p, uap, retval)
388 struct proc *p;
389 register struct write_args *uap;
390 user_ssize_t *retval;
391 {
392 struct fileproc *fp;
393 int error;
394 int fd = uap->fd;
395
396 error = fp_lookup(p,fd,&fp,0);
397 if (error)
398 return(error);
399 if ((fp->f_flag & FWRITE) == 0) {
400 error = EBADF;
401 } else {
402 error = dofilewrite(p, fp, uap->fd, uap->cbuf, uap->nbyte,
403 (off_t)-1, 0, retval);
404 }
405 if (error == 0)
406 fp_drop_written(p, fd, fp);
407 else
408 fp_drop(p, fd, fp, 0);
409 return(error);
410 }
411
412 /*
413 * pwrite system call
414 */
415 int
416 pwrite(p, uap, retval)
417 struct proc *p;
418 register struct pwrite_args *uap;
419 user_ssize_t *retval;
420 {
421 struct fileproc *fp;
422 int error;
423 int fd = uap->fd;
424
425 error = fp_lookup(p,fd,&fp,0);
426 if (error)
427 return(error);
428
429 if ((fp->f_flag & FWRITE) == 0) {
430 error = EBADF;
431 } else {
432 if (fp->f_type != DTYPE_VNODE) {
433 error = ESPIPE;
434 } else {
435 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
436 uap->offset, FOF_OFFSET, retval);
437 }
438 }
439 if (error == 0)
440 fp_drop_written(p, fd, fp);
441 else
442 fp_drop(p, fd, fp, 0);
443
444 if (!error)
445 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pwrite) | DBG_FUNC_NONE),
446 uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
447
448 return(error);
449 }
450
451 __private_extern__ int
452 dofilewrite(p, fp, fd, bufp, nbyte, offset, flags, retval)
453 struct proc *p;
454 struct fileproc *fp;
455 int fd, flags;
456 user_addr_t bufp;
457 user_size_t nbyte;
458 off_t offset;
459 user_ssize_t *retval;
460 {
461 uio_t auio;
462 long error = 0;
463 user_ssize_t bytecnt;
464 char uio_buf[ UIO_SIZEOF(1) ];
465 #if KTRACE
466 uio_t ktruio;
467 int didktr = 0;
468 char ktr_uio_buf[ UIO_SIZEOF(1) ];
469 #endif
470
471 // LP64todo - do we want to raise this?
472 if (nbyte > INT_MAX)
473 return (EINVAL);
474
475 if (IS_64BIT_PROCESS(p)) {
476 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE,
477 &uio_buf[0], sizeof(uio_buf));
478 } else {
479 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE,
480 &uio_buf[0], sizeof(uio_buf));
481 }
482 uio_addiov(auio, bufp, nbyte);
483
484 #if KTRACE
485 /*
486 * if tracing, save a copy of iovec and uio
487 */
488 if (KTRPOINT(p, KTR_GENIO)) {
489 didktr = 1;
490
491 if (IS_64BIT_PROCESS(p)) {
492 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE,
493 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
494 } else {
495 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE,
496 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
497 }
498 uio_addiov(ktruio, bufp, nbyte);
499 }
500 #endif
501 bytecnt = nbyte;
502 if ((error = fo_write(fp, auio, fp->f_cred, flags, p))) {
503 if (uio_resid(auio) != bytecnt && (error == ERESTART ||
504 error == EINTR || error == EWOULDBLOCK))
505 error = 0;
506 /* The socket layer handles SIGPIPE */
507 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
508 psignal(p, SIGPIPE);
509 }
510 bytecnt -= uio_resid(auio);
511 #if KTRACE
512 if (didktr && error == 0) {
513 uio_setresid(ktruio, bytecnt);
514 ktrgenio(p->p_tracep, fd, UIO_WRITE, ktruio, error);
515 }
516 #endif
517 *retval = bytecnt;
518
519 return (error);
520 }
521
522 /*
523 * Gather write system call
524 */
525 int
526 writev(p, uap, retval)
527 struct proc *p;
528 register struct writev_args *uap;
529 user_ssize_t *retval;
530 {
531 uio_t auio = NULL;
532 int error;
533 int size_of_iovec;
534 struct user_iovec *iovp;
535
536 /* Verify range bedfore calling uio_create() */
537 if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
538 return (EINVAL);
539
540 /* allocate a uio large enough to hold the number of iovecs passed */
541 auio = uio_create(uap->iovcnt, 0,
542 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
543 UIO_WRITE);
544
545 /* get location of iovecs within the uio. then copyin the iovecs from
546 * user space.
547 */
548 iovp = uio_iovsaddr(auio);
549 if (iovp == NULL) {
550 error = ENOMEM;
551 goto ExitThisRoutine;
552 }
553 size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
554 error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
555 if (error) {
556 goto ExitThisRoutine;
557 }
558
559 /* finalize uio_t for use and do the IO
560 */
561 uio_calculateresid(auio);
562 error = wr_uio(p, uap->fd, auio, retval);
563
564 ExitThisRoutine:
565 if (auio != NULL) {
566 uio_free(auio);
567 }
568 return (error);
569 }
570
571
572 int
573 wr_uio(p, fdes, uio, retval)
574 struct proc *p;
575 int fdes;
576 register uio_t uio;
577 user_ssize_t *retval;
578 {
579 struct fileproc *fp;
580 int error;
581 user_ssize_t count;
582 #if KTRACE
583 struct iovec_64 *ktriov = NULL;
584 struct uio ktruio;
585 int didktr = 0;
586 u_int iovlen;
587 #endif
588
589 error = fp_lookup(p,fdes,&fp,0);
590 if (error)
591 return(error);
592
593 if ((fp->f_flag & FWRITE) == 0) {
594 error = EBADF;
595 goto out;
596 }
597 count = uio_resid(uio);
598 #if KTRACE
599 /*
600 * if tracing, save a copy of iovec
601 */
602 if (KTRPOINT(p, KTR_GENIO)) {
603 iovlen = uio->uio_iovcnt *
604 (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
605 MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
606 if (ktriov != NULL) {
607 bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
608 ktruio = *uio;
609 didktr = 1;
610 }
611 }
612 #endif
613 error = fo_write(fp, uio, fp->f_cred, 0, p);
614 if (error) {
615 if (uio_resid(uio) != count && (error == ERESTART ||
616 error == EINTR || error == EWOULDBLOCK))
617 error = 0;
618 /* The socket layer handles SIGPIPE */
619 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
620 psignal(p, SIGPIPE);
621 }
622 *retval = count - uio_resid(uio);
623
624 #if KTRACE
625 if (didktr) {
626 if (error == 0) {
627 ktruio.uio_iovs.iov64p = ktriov;
628 uio_setresid(&ktruio, *retval);
629 ktrgenio(p->p_tracep, fdes, UIO_WRITE, &ktruio, error);
630 }
631 FREE(ktriov, M_TEMP);
632 }
633 #endif
634
635 out:
636 if ( (error == 0) )
637 fp_drop_written(p, fdes, fp);
638 else
639 fp_drop(p, fdes, fp, 0);
640 return(error);
641 }
642
643
644 int
645 rd_uio(p, fdes, uio, retval)
646 struct proc *p;
647 int fdes;
648 register uio_t uio;
649 user_ssize_t *retval;
650 {
651 struct fileproc *fp;
652 int error;
653 user_ssize_t count;
654 #if KTRACE
655 struct iovec_64 *ktriov = NULL;
656 struct uio ktruio;
657 int didktr = 0;
658 u_int iovlen;
659 #endif
660
661 if ( (error = preparefileread(p, &fp, fdes, 0)) )
662 return (error);
663
664 count = uio_resid(uio);
665 #if KTRACE
666 /*
667 * if tracing, save a copy of iovec
668 */
669 if (KTRPOINT(p, KTR_GENIO)) {
670 iovlen = uio->uio_iovcnt *
671 (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
672 MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
673 if (ktriov != NULL) {
674 bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
675 ktruio = *uio;
676 didktr = 1;
677 }
678 }
679 #endif
680 error = fo_read(fp, uio, fp->f_cred, 0, p);
681
682 if (error) {
683 if (uio_resid(uio) != count && (error == ERESTART ||
684 error == EINTR || error == EWOULDBLOCK))
685 error = 0;
686 }
687 *retval = count - uio_resid(uio);
688
689 #if KTRACE
690 if (didktr) {
691 if (error == 0) {
692 ktruio.uio_iovs.iov64p = ktriov;
693 uio_setresid(&ktruio, *retval);
694 ktrgenio(p->p_tracep, fdes, UIO_READ, &ktruio, error);
695 }
696 FREE(ktriov, M_TEMP);
697 }
698 #endif
699 donefileread(p, fp, fdes);
700
701 return (error);
702 }
703
704 /*
705 * Ioctl system call
706 *
707 */
708 int
709 ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retval)
710 {
711 struct fileproc *fp;
712 register u_long com;
713 int error = 0;
714 register u_int size;
715 caddr_t datap, memp;
716 boolean_t is64bit;
717 int tmp;
718 #define STK_PARAMS 128
719 char stkbuf[STK_PARAMS];
720 int fd = uap->fd;
721
722 AUDIT_ARG(fd, uap->fd);
723 AUDIT_ARG(cmd, CAST_DOWN(int, uap->com)); /* LP64todo: uap->com is a user-land long */
724 AUDIT_ARG(addr, uap->data);
725
726 is64bit = proc_is64bit(p);
727
728 proc_fdlock(p);
729 error = fp_lookup(p,fd,&fp,1);
730 if (error) {
731 proc_fdunlock(p);
732 return(error);
733 }
734
735 AUDIT_ARG(file, p, fp);
736
737 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
738 error = EBADF;
739 goto out;
740 }
741
742 #if NETAT
743 /*
744 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
745 * while implementing an ATioctl system call
746 */
747 {
748 if (appletalk_inited && ((uap->com & 0x0000FFFF) == 0xff99)) {
749 u_long fixed_command;
750 #ifdef APPLETALK_DEBUG
751 kprintf("ioctl: special AppleTalk \n");
752 #endif
753 datap = &stkbuf[0];
754 *(user_addr_t *)datap = uap->data;
755 fixed_command = _IOW(0, 0xff99, uap->data);
756 error = fo_ioctl(fp, fixed_command, datap, p);
757 goto out;
758 }
759 }
760
761 #endif /* NETAT */
762
763
764 switch (com = uap->com) {
765 case FIONCLEX:
766 *fdflags(p, uap->fd) &= ~UF_EXCLOSE;
767 error =0;
768 goto out;
769 case FIOCLEX:
770 *fdflags(p, uap->fd) |= UF_EXCLOSE;
771 error =0;
772 goto out;
773 }
774
775 /*
776 * Interpret high order word to find amount of data to be
777 * copied to/from the user's address space.
778 */
779 size = IOCPARM_LEN(com);
780 if (size > IOCPARM_MAX) {
781 error = ENOTTY;
782 goto out;
783 }
784 memp = NULL;
785 if (size > sizeof (stkbuf)) {
786 proc_fdunlock(p);
787 if ((memp = (caddr_t)kalloc(size)) == 0) {
788 proc_fdlock(p);
789 error = ENOMEM;
790 goto out;
791 }
792 proc_fdlock(p);
793 datap = memp;
794 } else
795 datap = &stkbuf[0];
796 if (com&IOC_IN) {
797 if (size) {
798 proc_fdunlock(p);
799 error = copyin(uap->data, datap, size);
800 if (error) {
801 if (memp)
802 kfree(memp, size);
803 proc_fdlock(p);
804 goto out;
805 }
806 proc_fdlock(p);
807 } else {
808 /* XXX - IOC_IN and no size? we should proably return an error here!! */
809 if (is64bit) {
810 *(user_addr_t *)datap = uap->data;
811 }
812 else {
813 *(uint32_t *)datap = (uint32_t)uap->data;
814 }
815 }
816 } else if ((com&IOC_OUT) && size)
817 /*
818 * Zero the buffer so the user always
819 * gets back something deterministic.
820 */
821 bzero(datap, size);
822 else if (com&IOC_VOID) {
823 /* XXX - this is odd since IOC_VOID means no parameters */
824 if (is64bit) {
825 *(user_addr_t *)datap = uap->data;
826 }
827 else {
828 *(uint32_t *)datap = (uint32_t)uap->data;
829 }
830 }
831
832 switch (com) {
833
834 case FIONBIO:
835 if ( (tmp = *(int *)datap) )
836 fp->f_flag |= FNONBLOCK;
837 else
838 fp->f_flag &= ~FNONBLOCK;
839 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
840 break;
841
842 case FIOASYNC:
843 if ( (tmp = *(int *)datap) )
844 fp->f_flag |= FASYNC;
845 else
846 fp->f_flag &= ~FASYNC;
847 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
848 break;
849
850 case FIOSETOWN:
851 tmp = *(int *)datap;
852 if (fp->f_type == DTYPE_SOCKET) {
853 ((struct socket *)fp->f_data)->so_pgid = tmp;
854 error = 0;
855 break;
856 }
857 if (fp->f_type == DTYPE_PIPE) {
858 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
859 break;
860 }
861 if (tmp <= 0) {
862 tmp = -tmp;
863 } else {
864 struct proc *p1 = pfind(tmp);
865 if (p1 == 0) {
866 error = ESRCH;
867 break;
868 }
869 tmp = p1->p_pgrp->pg_id;
870 }
871 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
872 break;
873
874 case FIOGETOWN:
875 if (fp->f_type == DTYPE_SOCKET) {
876 error = 0;
877 *(int *)datap = ((struct socket *)fp->f_data)->so_pgid;
878 break;
879 }
880 error = fo_ioctl(fp, TIOCGPGRP, datap, p);
881 *(int *)datap = -*(int *)datap;
882 break;
883
884 default:
885 error = fo_ioctl(fp, com, datap, p);
886 /*
887 * Copy any data to user, size was
888 * already set and checked above.
889 */
890 if (error == 0 && (com&IOC_OUT) && size)
891 error = copyout(datap, uap->data, (u_int)size);
892 break;
893 }
894 proc_fdunlock(p);
895 if (memp)
896 kfree(memp, size);
897 proc_fdlock(p);
898 out:
899 fp_drop(p, fd, fp, 1);
900 proc_fdunlock(p);
901 return(error);
902 }
903
904 int selwait, nselcoll;
905 #define SEL_FIRSTPASS 1
906 #define SEL_SECONDPASS 2
907 extern int selcontinue(int error);
908 extern int selprocess(int error, int sel_pass);
909 static int selscan(struct proc *p, struct _select * sel,
910 int nfd, register_t *retval, int sel_pass, wait_queue_sub_t wqsub);
911 static int selcount(struct proc *p, u_int32_t *ibits, u_int32_t *obits,
912 int nfd, int * count);
913 static int seldrop(struct proc *p, u_int32_t *ibits, int nfd);
914 extern uint64_t tvtoabstime(struct timeval *tvp);
915
916 /*
917 * Select system call.
918 */
919 int
920 select(struct proc *p, struct select_args *uap, register_t *retval)
921 {
922 int error = 0;
923 u_int ni, nw, size;
924 thread_t th_act;
925 struct uthread *uth;
926 struct _select *sel;
927 int needzerofill = 1;
928 int count = 0;
929
930 th_act = current_thread();
931 uth = get_bsdthread_info(th_act);
932 sel = &uth->uu_select;
933 retval = (int *)get_bsduthreadrval(th_act);
934 *retval = 0;
935
936 if (uap->nd < 0) {
937 return (EINVAL);
938 }
939
940 if (uap->nd > p->p_fd->fd_nfiles)
941 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
942
943 nw = howmany(uap->nd, NFDBITS);
944 ni = nw * sizeof(fd_mask);
945
946 /*
947 * if this is the first select by the thread
948 * allocate the space for bits.
949 */
950 if (sel->nbytes == 0) {
951 sel->nbytes = 3 * ni;
952 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
953 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
954 if ((sel->ibits == NULL) || (sel->obits == NULL))
955 panic("select out of memory");
956 needzerofill = 0;
957 }
958
959 /*
960 * if the previously allocated space for the bits
961 * is smaller than what is requested. Reallocate.
962 */
963 if (sel->nbytes < (3 * ni)) {
964 sel->nbytes = (3 * ni);
965 FREE(sel->ibits, M_TEMP);
966 FREE(sel->obits, M_TEMP);
967 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
968 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
969 if ((sel->ibits == NULL) || (sel->obits == NULL))
970 panic("select out of memory");
971 needzerofill = 0;
972 }
973
974 if (needzerofill) {
975 bzero((caddr_t)sel->ibits, sel->nbytes);
976 bzero((caddr_t)sel->obits, sel->nbytes);
977 }
978
979 /*
980 * get the bits from the user address space
981 */
982 #define getbits(name, x) \
983 do { \
984 if (uap->name && (error = copyin(uap->name, \
985 (caddr_t)&sel->ibits[(x) * nw], ni))) \
986 goto continuation; \
987 } while (0)
988
989 getbits(in, 0);
990 getbits(ou, 1);
991 getbits(ex, 2);
992 #undef getbits
993
994 if (uap->tv) {
995 struct timeval atv;
996 if (IS_64BIT_PROCESS(p)) {
997 struct user_timeval atv64;
998 error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
999 /* Loses resolution - assume timeout < 68 years */
1000 atv.tv_sec = atv64.tv_sec;
1001 atv.tv_usec = atv64.tv_usec;
1002 } else {
1003 error = copyin(uap->tv, (caddr_t)&atv, sizeof(atv));
1004 }
1005 if (error)
1006 goto continuation;
1007 if (itimerfix(&atv)) {
1008 error = EINVAL;
1009 goto continuation;
1010 }
1011
1012 clock_absolutetime_interval_to_deadline(
1013 tvtoabstime(&atv), &sel->abstime);
1014 }
1015 else
1016 sel->abstime = 0;
1017
1018 if ( (error = selcount(p, sel->ibits, sel->obits, uap->nd, &count)) ) {
1019 goto continuation;
1020 }
1021
1022 sel->count = count;
1023 size = SIZEOF_WAITQUEUE_SET + (count * SIZEOF_WAITQUEUE_LINK);
1024 if (sel->allocsize) {
1025 if (sel->wqset == 0)
1026 panic("select: wql memory smashed");
1027 /* needed for the select now */
1028 if (size > sel->allocsize) {
1029 kfree(sel->wqset, sel->allocsize);
1030 sel->allocsize = size;
1031 sel->wqset = (wait_queue_set_t)kalloc(size);
1032 if (sel->wqset == (wait_queue_set_t)NULL)
1033 panic("failed to allocate memory for waitqueue\n");
1034 }
1035 } else {
1036 sel->count = count;
1037 sel->allocsize = size;
1038 sel->wqset = (wait_queue_set_t)kalloc(sel->allocsize);
1039 if (sel->wqset == (wait_queue_set_t)NULL)
1040 panic("failed to allocate memory for waitqueue\n");
1041 }
1042 bzero(sel->wqset, size);
1043 sel->wql = (char *)sel->wqset + SIZEOF_WAITQUEUE_SET;
1044 wait_queue_set_init(sel->wqset, (SYNC_POLICY_FIFO | SYNC_POLICY_PREPOST));
1045
1046 continuation:
1047 return selprocess(error, SEL_FIRSTPASS);
1048 }
1049
1050 int
1051 selcontinue(int error)
1052 {
1053 return selprocess(error, SEL_SECONDPASS);
1054 }
1055
1056 int
1057 selprocess(int error, int sel_pass)
1058 {
1059 int ncoll;
1060 u_int ni, nw;
1061 thread_t th_act;
1062 struct uthread *uth;
1063 struct proc *p;
1064 struct select_args *uap;
1065 int *retval;
1066 struct _select *sel;
1067 int unwind = 1;
1068 int prepost = 0;
1069 int somewakeup = 0;
1070 int doretry = 0;
1071 wait_result_t wait_result;
1072
1073 p = current_proc();
1074 th_act = current_thread();
1075 uap = (struct select_args *)get_bsduthreadarg(th_act);
1076 retval = (int *)get_bsduthreadrval(th_act);
1077 uth = get_bsdthread_info(th_act);
1078 sel = &uth->uu_select;
1079
1080 /* if it is first pass wait queue is not setup yet */
1081 if ((error != 0) && (sel_pass == SEL_FIRSTPASS))
1082 unwind = 0;
1083 if (sel->count == 0)
1084 unwind = 0;
1085 retry:
1086 if (error != 0) {
1087 goto done;
1088 }
1089
1090 ncoll = nselcoll;
1091 p->p_flag |= P_SELECT;
1092 /* skip scans if the select is just for timeouts */
1093 if (sel->count) {
1094 if (sel_pass == SEL_FIRSTPASS)
1095 wait_queue_sub_clearrefs(sel->wqset);
1096
1097 error = selscan(p, sel, uap->nd, retval, sel_pass, sel->wqset);
1098 if (error || *retval) {
1099 goto done;
1100 }
1101 if (prepost) {
1102 /* if the select of log, then we canwakeup and discover some one
1103 * else already read the data; go toselct again if time permits
1104 */
1105 prepost = 0;
1106 doretry = 1;
1107 }
1108 if (somewakeup) {
1109 somewakeup = 0;
1110 doretry = 1;
1111 }
1112 }
1113
1114 if (uap->tv) {
1115 uint64_t now;
1116
1117 clock_get_uptime(&now);
1118 if (now >= sel->abstime)
1119 goto done;
1120 }
1121
1122 if (doretry) {
1123 /* cleanup obits and try again */
1124 doretry = 0;
1125 sel_pass = SEL_FIRSTPASS;
1126 goto retry;
1127 }
1128
1129 /*
1130 * To effect a poll, the timeout argument should be
1131 * non-nil, pointing to a zero-valued timeval structure.
1132 */
1133 if (uap->tv && sel->abstime == 0) {
1134 goto done;
1135 }
1136
1137 /* No spurious wakeups due to colls,no need to check for them */
1138 if ((sel_pass == SEL_SECONDPASS) || ((p->p_flag & P_SELECT) == 0)) {
1139 sel_pass = SEL_FIRSTPASS;
1140 goto retry;
1141 }
1142
1143 p->p_flag &= ~P_SELECT;
1144
1145 /* if the select is just for timeout skip check */
1146 if (sel->count &&(sel_pass == SEL_SECONDPASS))
1147 panic("selprocess: 2nd pass assertwaiting");
1148
1149 /* Wait Queue Subordinate has waitqueue as first element */
1150 wait_result = wait_queue_assert_wait((wait_queue_t)sel->wqset,
1151 &selwait, THREAD_ABORTSAFE, sel->abstime);
1152 if (wait_result != THREAD_AWAKENED) {
1153 /* there are no preposted events */
1154 error = tsleep1(NULL, PSOCK | PCATCH,
1155 "select", 0, selcontinue);
1156 } else {
1157 prepost = 1;
1158 error = 0;
1159 }
1160
1161 sel_pass = SEL_SECONDPASS;
1162 if (error == 0) {
1163 if (!prepost)
1164 somewakeup =1;
1165 goto retry;
1166 }
1167 done:
1168 if (unwind) {
1169 wait_subqueue_unlink_all(sel->wqset);
1170 seldrop(p, sel->ibits, uap->nd);
1171 }
1172 p->p_flag &= ~P_SELECT;
1173 /* select is not restarted after signals... */
1174 if (error == ERESTART)
1175 error = EINTR;
1176 if (error == EWOULDBLOCK)
1177 error = 0;
1178 nw = howmany(uap->nd, NFDBITS);
1179 ni = nw * sizeof(fd_mask);
1180
1181 #define putbits(name, x) \
1182 do { \
1183 if (uap->name && (error2 = \
1184 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1185 error = error2; \
1186 } while (0)
1187
1188 if (error == 0) {
1189 int error2;
1190
1191 putbits(in, 0);
1192 putbits(ou, 1);
1193 putbits(ex, 2);
1194 #undef putbits
1195 }
1196 return(error);
1197 }
1198
1199 static int
1200 selscan(p, sel, nfd, retval, sel_pass, wqsub)
1201 struct proc *p;
1202 struct _select *sel;
1203 int nfd;
1204 register_t *retval;
1205 int sel_pass;
1206 wait_queue_sub_t wqsub;
1207 {
1208 register struct filedesc *fdp = p->p_fd;
1209 register int msk, i, j, fd;
1210 register u_int32_t bits;
1211 struct fileproc *fp;
1212 int n = 0;
1213 int nc = 0;
1214 static int flag[3] = { FREAD, FWRITE, 0 };
1215 u_int32_t *iptr, *optr;
1216 u_int nw;
1217 u_int32_t *ibits, *obits;
1218 char * wql;
1219 char * wql_ptr;
1220
1221 /*
1222 * Problems when reboot; due to MacOSX signal probs
1223 * in Beaker1C ; verify that the p->p_fd is valid
1224 */
1225 if (fdp == NULL) {
1226 *retval=0;
1227 return(EIO);
1228 }
1229 ibits = sel->ibits;
1230 obits = sel->obits;
1231 wql = sel->wql;
1232
1233 nw = howmany(nfd, NFDBITS);
1234
1235 nc = 0;
1236 proc_fdlock(p);
1237
1238 if (sel->count) {
1239 for (msk = 0; msk < 3; msk++) {
1240 iptr = (u_int32_t *)&ibits[msk * nw];
1241 optr = (u_int32_t *)&obits[msk * nw];
1242
1243 for (i = 0; i < nfd; i += NFDBITS) {
1244 bits = iptr[i/NFDBITS];
1245
1246 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1247 bits &= ~(1 << j);
1248 fp = fdp->fd_ofiles[fd];
1249
1250 if (fp == NULL ||
1251 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1252 proc_fdunlock(p);
1253 return(EBADF);
1254 }
1255 if (sel_pass == SEL_SECONDPASS) {
1256 wql_ptr = (char *)0;
1257 fp->f_flags &= ~FP_INSELECT;
1258 fp->f_waddr = (void *)0;
1259 } else {
1260 wql_ptr = (wql + nc * SIZEOF_WAITQUEUE_LINK);
1261 fp->f_flags |= FP_INSELECT;
1262 fp->f_waddr = (void *)wqsub;
1263 }
1264 if (fp->f_ops && fo_select(fp, flag[msk], wql_ptr, p)) {
1265 optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
1266 n++;
1267 }
1268 nc++;
1269 }
1270 }
1271 }
1272 }
1273 proc_fdunlock(p);
1274 *retval = n;
1275 return (0);
1276 }
1277
1278 static int poll_callback(struct kqueue *, struct kevent *, void *);
1279
1280 struct poll_continue_args {
1281 user_addr_t pca_fds;
1282 u_int pca_nfds;
1283 u_int pca_rfds;
1284 };
1285
1286 int
1287 poll(struct proc *p, struct poll_args *uap, register_t *retval)
1288 {
1289 struct poll_continue_args *cont;
1290 struct pollfd *fds;
1291 struct kqueue *kq;
1292 struct timeval atv;
1293 int ncoll, error = 0;
1294 u_int nfds = uap->nfds;
1295 u_int rfds = 0;
1296 u_int i;
1297 size_t ni;
1298
1299 /*
1300 * This is kinda bogus. We have fd limits, but that is not
1301 * really related to the size of the pollfd array. Make sure
1302 * we let the process use at least FD_SETSIZE entries and at
1303 * least enough for the current limits. We want to be reasonably
1304 * safe, but not overly restrictive.
1305 */
1306 if (nfds > OPEN_MAX ||
1307 (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE))
1308 return (EINVAL);
1309
1310 kq = kqueue_alloc(p);
1311 if (kq == NULL)
1312 return (EAGAIN);
1313
1314 ni = nfds * sizeof(struct pollfd) + sizeof(struct poll_continue_args);
1315 MALLOC(cont, struct poll_continue_args *, ni, M_TEMP, M_WAITOK);
1316 if (NULL == cont) {
1317 error = EAGAIN;
1318 goto out;
1319 }
1320
1321 fds = (struct pollfd *)&cont[1];
1322 error = copyin(uap->fds, fds, nfds * sizeof(struct pollfd));
1323 if (error)
1324 goto out;
1325
1326 if (uap->timeout != -1) {
1327 struct timeval rtv;
1328
1329 atv.tv_sec = uap->timeout / 1000;
1330 atv.tv_usec = (uap->timeout % 1000) * 1000;
1331 if (itimerfix(&atv)) {
1332 error = EINVAL;
1333 goto out;
1334 }
1335 getmicrouptime(&rtv);
1336 timevaladd(&atv, &rtv);
1337 } else {
1338 atv.tv_sec = 0;
1339 atv.tv_usec = 0;
1340 }
1341
1342 /* JMM - all this P_SELECT stuff is bogus */
1343 ncoll = nselcoll;
1344 p->p_flag |= P_SELECT;
1345
1346 for (i = 0; i < nfds; i++) {
1347 short events = fds[i].events;
1348 struct kevent kev;
1349 int kerror = 0;
1350
1351 /* per spec, ignore fd values below zero */
1352 if (fds[i].fd < 0) {
1353 fds[i].revents = 0;
1354 continue;
1355 }
1356
1357 /* convert the poll event into a kqueue kevent */
1358 kev.ident = fds[i].fd;
1359 kev.flags = EV_ADD | EV_ONESHOT | EV_POLL;
1360 kev.fflags = NOTE_LOWAT;
1361 kev.data = 1; /* efficiency be damned: any data should trigger */
1362 kev.udata = CAST_USER_ADDR_T(&fds[i]);
1363
1364 /* Handle input events */
1365 if (events & ( POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND )) {
1366 kev.filter = EVFILT_READ;
1367 if (!(events & ( POLLIN | POLLRDNORM )))
1368 kev.flags |= EV_OOBAND;
1369 kerror = kevent_register(kq, &kev, p);
1370 }
1371
1372 /* Handle output events */
1373 if (kerror == 0 &&
1374 events & ( POLLOUT | POLLWRNORM | POLLWRBAND )) {
1375 kev.filter = EVFILT_WRITE;
1376 kerror = kevent_register(kq, &kev, p);
1377 }
1378
1379 /* Handle BSD extension vnode events */
1380 if (kerror == 0 &&
1381 events & ( POLLEXTEND | POLLATTRIB | POLLNLINK | POLLWRITE )) {
1382 kev.filter = EVFILT_VNODE;
1383 kev.fflags = 0;
1384 if (events & POLLEXTEND)
1385 kev.fflags |= NOTE_EXTEND;
1386 if (events & POLLATTRIB)
1387 kev.fflags |= NOTE_ATTRIB;
1388 if (events & POLLNLINK)
1389 kev.fflags |= NOTE_LINK;
1390 if (events & POLLWRITE)
1391 kev.fflags |= NOTE_WRITE;
1392 kerror = kevent_register(kq, &kev, p);
1393 }
1394
1395 if (kerror != 0) {
1396 fds[i].revents = POLLNVAL;
1397 rfds++;
1398 } else
1399 fds[i].revents = 0;
1400 }
1401
1402 /* Did we have any trouble registering? */
1403 if (rfds > 0)
1404 goto done;
1405
1406 /* scan for, and possibly wait for, the kevents to trigger */
1407 cont->pca_fds = uap->fds;
1408 cont->pca_nfds = nfds;
1409 cont->pca_rfds = rfds;
1410 error = kevent_scan(kq, poll_callback, NULL, cont, &atv, p);
1411 rfds = cont->pca_rfds;
1412
1413 done:
1414 p->p_flag &= ~P_SELECT;
1415 /* poll is not restarted after signals... */
1416 if (error == ERESTART)
1417 error = EINTR;
1418 if (error == EWOULDBLOCK)
1419 error = 0;
1420 if (error == 0) {
1421 error = copyout(fds, uap->fds, nfds * sizeof(struct pollfd));
1422 *retval = rfds;
1423 }
1424
1425 out:
1426 if (NULL != cont)
1427 FREE(cont, M_TEMP);
1428
1429 kqueue_dealloc(kq, p);
1430 return (error);
1431 }
1432
1433 static int
1434 poll_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data)
1435 {
1436 struct poll_continue_args *cont = (struct poll_continue_args *)data;
1437 struct pollfd *fds = CAST_DOWN(struct pollfd *, kevp->udata);
1438 short mask;
1439
1440 /* convert the results back into revents */
1441 if (kevp->flags & EV_EOF)
1442 fds->revents |= POLLHUP;
1443 if (kevp->flags & EV_ERROR)
1444 fds->revents |= POLLERR;
1445 cont->pca_rfds++;
1446
1447 switch (kevp->filter) {
1448 case EVFILT_READ:
1449 if (fds->revents & POLLHUP)
1450 mask = (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND );
1451 else {
1452 mask = 0;
1453 if (kevp->data != 0)
1454 mask |= (POLLIN | POLLRDNORM );
1455 if (kevp->flags & EV_OOBAND)
1456 mask |= ( POLLPRI | POLLRDBAND );
1457 }
1458 fds->revents |= (fds->events & mask);
1459 break;
1460
1461 case EVFILT_WRITE:
1462 if (!(fds->revents & POLLHUP))
1463 fds->revents |= (fds->events & ( POLLOUT | POLLWRNORM | POLLWRBAND ));
1464 break;
1465
1466 case EVFILT_PROC:
1467 if (kevp->fflags & NOTE_EXTEND)
1468 fds->revents |= (fds->events & POLLEXTEND);
1469 if (kevp->fflags & NOTE_ATTRIB)
1470 fds->revents |= (fds->events & POLLATTRIB);
1471 if (kevp->fflags & NOTE_LINK)
1472 fds->revents |= (fds->events & POLLNLINK);
1473 if (kevp->fflags & NOTE_WRITE)
1474 fds->revents |= (fds->events & POLLWRITE);
1475 break;
1476 }
1477 return 0;
1478 }
1479
1480 int
1481 seltrue(__unused dev_t dev, __unused int flag, __unused struct proc *p)
1482 {
1483
1484 return (1);
1485 }
1486
1487 static int
1488 selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
1489 int nfd, int *count)
1490 {
1491 register struct filedesc *fdp = p->p_fd;
1492 register int msk, i, j, fd;
1493 register u_int32_t bits;
1494 struct fileproc *fp;
1495 int n = 0;
1496 u_int32_t *iptr;
1497 u_int nw;
1498 int error=0;
1499 int dropcount;
1500
1501 /*
1502 * Problems when reboot; due to MacOSX signal probs
1503 * in Beaker1C ; verify that the p->p_fd is valid
1504 */
1505 if (fdp == NULL) {
1506 *count=0;
1507 return(EIO);
1508 }
1509 nw = howmany(nfd, NFDBITS);
1510
1511 proc_fdlock(p);
1512 for (msk = 0; msk < 3; msk++) {
1513 iptr = (u_int32_t *)&ibits[msk * nw];
1514 for (i = 0; i < nfd; i += NFDBITS) {
1515 bits = iptr[i/NFDBITS];
1516 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1517 bits &= ~(1 << j);
1518 fp = fdp->fd_ofiles[fd];
1519 if (fp == NULL ||
1520 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1521 *count=0;
1522 error = EBADF;
1523 goto bad;
1524 }
1525 fp->f_iocount++;
1526 n++;
1527 }
1528 }
1529 }
1530 proc_fdunlock(p);
1531
1532 *count = n;
1533 return (0);
1534 bad:
1535 dropcount = 0;
1536
1537 if (n== 0)
1538 goto out;
1539 /* undo the iocounts */
1540 for (msk = 0; msk < 3; msk++) {
1541 iptr = (u_int32_t *)&ibits[msk * nw];
1542 for (i = 0; i < nfd; i += NFDBITS) {
1543 bits = iptr[i/NFDBITS];
1544 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1545 bits &= ~(1 << j);
1546 fp = fdp->fd_ofiles[fd];
1547 if (dropcount >= n)
1548 goto out;
1549 fp->f_iocount--;
1550
1551 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1552 p->p_fpdrainwait = 0;
1553 wakeup(&p->p_fpdrainwait);
1554 }
1555 dropcount++;
1556 }
1557 }
1558 }
1559 out:
1560 proc_fdunlock(p);
1561 return(error);
1562 }
1563
1564 static int
1565 seldrop(p, ibits, nfd)
1566 struct proc *p;
1567 u_int32_t *ibits;
1568 int nfd;
1569 {
1570 register struct filedesc *fdp = p->p_fd;
1571 register int msk, i, j, fd;
1572 register u_int32_t bits;
1573 struct fileproc *fp;
1574 int n = 0;
1575 u_int32_t *iptr;
1576 u_int nw;
1577
1578 /*
1579 * Problems when reboot; due to MacOSX signal probs
1580 * in Beaker1C ; verify that the p->p_fd is valid
1581 */
1582 if (fdp == NULL) {
1583 return(EIO);
1584 }
1585
1586 nw = howmany(nfd, NFDBITS);
1587
1588
1589 proc_fdlock(p);
1590 for (msk = 0; msk < 3; msk++) {
1591 iptr = (u_int32_t *)&ibits[msk * nw];
1592 for (i = 0; i < nfd; i += NFDBITS) {
1593 bits = iptr[i/NFDBITS];
1594 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1595 bits &= ~(1 << j);
1596 fp = fdp->fd_ofiles[fd];
1597 if (fp == NULL
1598 #if 0
1599 /* if you are here then it is being closed */
1600 || (fdp->fd_ofileflags[fd] & UF_RESERVED)
1601 #endif
1602 ) {
1603 proc_fdunlock(p);
1604 return(EBADF);
1605 }
1606 n++;
1607 fp->f_iocount--;
1608 fp->f_flags &= ~FP_INSELECT;
1609
1610 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1611 p->p_fpdrainwait = 0;
1612 wakeup(&p->p_fpdrainwait);
1613 }
1614 }
1615 }
1616 }
1617 proc_fdunlock(p);
1618 return (0);
1619 }
1620
1621 /*
1622 * Record a select request.
1623 */
1624 void
1625 selrecord(__unused struct proc *selector, struct selinfo *sip, void * p_wql)
1626 {
1627 thread_t cur_act = current_thread();
1628 struct uthread * ut = get_bsdthread_info(cur_act);
1629
1630 /* need to look at collisions */
1631
1632 if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
1633 return;
1634 }
1635
1636 /*do not record if this is second pass of select */
1637 if((p_wql == (void *)0)) {
1638 return;
1639 }
1640
1641 if ((sip->si_flags & SI_INITED) == 0) {
1642 wait_queue_init(&sip->si_wait_queue, SYNC_POLICY_FIFO);
1643 sip->si_flags |= SI_INITED;
1644 sip->si_flags &= ~SI_CLEAR;
1645 }
1646
1647 if (sip->si_flags & SI_RECORDED) {
1648 sip->si_flags |= SI_COLL;
1649 } else
1650 sip->si_flags &= ~SI_COLL;
1651
1652 sip->si_flags |= SI_RECORDED;
1653 if (!wait_queue_member(&sip->si_wait_queue, ut->uu_select.wqset))
1654 wait_queue_link_noalloc(&sip->si_wait_queue, ut->uu_select.wqset,
1655 (wait_queue_link_t)p_wql);
1656
1657 return;
1658 }
1659
1660 void
1661 selwakeup(sip)
1662 register struct selinfo *sip;
1663 {
1664
1665 if ((sip->si_flags & SI_INITED) == 0) {
1666 return;
1667 }
1668
1669 if (sip->si_flags & SI_COLL) {
1670 nselcoll++;
1671 sip->si_flags &= ~SI_COLL;
1672 #if 0
1673 /* will not support */
1674 //wakeup((caddr_t)&selwait);
1675 #endif
1676 }
1677
1678 if (sip->si_flags & SI_RECORDED) {
1679 wait_queue_wakeup_all(&sip->si_wait_queue, &selwait, THREAD_AWAKENED);
1680 sip->si_flags &= ~SI_RECORDED;
1681 }
1682
1683 }
1684
1685 void
1686 selthreadclear(sip)
1687 register struct selinfo *sip;
1688 {
1689
1690 if ((sip->si_flags & SI_INITED) == 0) {
1691 return;
1692 }
1693 if (sip->si_flags & SI_RECORDED) {
1694 selwakeup(sip);
1695 sip->si_flags &= ~(SI_RECORDED | SI_COLL);
1696 }
1697 sip->si_flags |= SI_CLEAR;
1698 wait_queue_unlinkall_nofree(&sip->si_wait_queue);
1699 }
1700
1701
1702
1703
1704 #define DBG_EVENT 0x10
1705
1706 #define DBG_POST 0x10
1707 #define DBG_WATCH 0x11
1708 #define DBG_WAIT 0x12
1709 #define DBG_MOD 0x13
1710 #define DBG_EWAKEUP 0x14
1711 #define DBG_ENQUEUE 0x15
1712 #define DBG_DEQUEUE 0x16
1713
1714 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1715 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1716 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1717 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1718 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1719 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1720 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1721
1722
1723 #define EVPROCDEQUE(p, evq) do { \
1724 proc_lock(p); \
1725 if (evq->ee_flags & EV_QUEUED) { \
1726 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1727 evq->ee_flags &= ~EV_QUEUED; \
1728 } \
1729 proc_unlock(p); \
1730 } while (0);
1731
1732
1733 /*
1734 * called upon socket close. deque and free all events for
1735 * the socket... socket must be locked by caller.
1736 */
1737 void
1738 evsofree(struct socket *sp)
1739 {
1740 struct eventqelt *evq, *next;
1741 proc_t p;
1742
1743 if (sp == NULL)
1744 return;
1745
1746 for (evq = sp->so_evlist.tqh_first; evq != NULL; evq = next) {
1747 next = evq->ee_slist.tqe_next;
1748 p = evq->ee_proc;
1749
1750 if (evq->ee_flags & EV_QUEUED) {
1751 EVPROCDEQUE(p, evq);
1752 }
1753 TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist); // remove from socket q
1754 FREE(evq, M_TEMP);
1755 }
1756 }
1757
1758
1759 /*
1760 * called upon pipe close. deque and free all events for
1761 * the pipe... pipe must be locked by caller
1762 */
1763 void
1764 evpipefree(struct pipe *cpipe)
1765 {
1766 struct eventqelt *evq, *next;
1767 proc_t p;
1768
1769 for (evq = cpipe->pipe_evlist.tqh_first; evq != NULL; evq = next) {
1770 next = evq->ee_slist.tqe_next;
1771 p = evq->ee_proc;
1772
1773 EVPROCDEQUE(p, evq);
1774
1775 TAILQ_REMOVE(&cpipe->pipe_evlist, evq, ee_slist); // remove from pipe q
1776 FREE(evq, M_TEMP);
1777 }
1778 }
1779
1780
1781 /*
1782 * enqueue this event if it's not already queued. wakeup
1783 * the proc if we do queue this event to it...
1784 * entered with proc lock held... we drop it before
1785 * doing the wakeup and return in that state
1786 */
1787 static void
1788 evprocenque(struct eventqelt *evq)
1789 {
1790 proc_t p;
1791
1792 assert(evq);
1793 p = evq->ee_proc;
1794
1795 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, evq, evq->ee_flags, evq->ee_eventmask,0,0);
1796
1797 proc_lock(p);
1798
1799 if (evq->ee_flags & EV_QUEUED) {
1800 proc_unlock(p);
1801
1802 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1803 return;
1804 }
1805 evq->ee_flags |= EV_QUEUED;
1806
1807 TAILQ_INSERT_TAIL(&p->p_evlist, evq, ee_plist);
1808
1809 proc_unlock(p);
1810
1811 wakeup(&p->p_evlist);
1812
1813 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1814 }
1815
1816
1817 /*
1818 * pipe lock must be taken by the caller
1819 */
1820 void
1821 postpipeevent(struct pipe *pipep, int event)
1822 {
1823 int mask;
1824 struct eventqelt *evq;
1825
1826 if (pipep == NULL)
1827 return;
1828 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,1,0);
1829
1830 for (evq = pipep->pipe_evlist.tqh_first;
1831 evq != NULL; evq = evq->ee_slist.tqe_next) {
1832
1833 if (evq->ee_eventmask == 0)
1834 continue;
1835 mask = 0;
1836
1837 switch (event & (EV_RWBYTES | EV_RCLOSED | EV_WCLOSED)) {
1838
1839 case EV_RWBYTES:
1840 if ((evq->ee_eventmask & EV_RE) && pipep->pipe_buffer.cnt) {
1841 mask |= EV_RE;
1842 evq->ee_req.er_rcnt = pipep->pipe_buffer.cnt;
1843 }
1844 if ((evq->ee_eventmask & EV_WR) &&
1845 (pipep->pipe_buffer.size - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
1846
1847 if (pipep->pipe_state & PIPE_EOF) {
1848 mask |= EV_WR|EV_RESET;
1849 break;
1850 }
1851 mask |= EV_WR;
1852 evq->ee_req.er_wcnt = pipep->pipe_buffer.size - pipep->pipe_buffer.cnt;
1853 }
1854 break;
1855
1856 case EV_WCLOSED:
1857 case EV_RCLOSED:
1858 if ((evq->ee_eventmask & EV_RE)) {
1859 mask |= EV_RE|EV_RCLOSED;
1860 }
1861 if ((evq->ee_eventmask & EV_WR)) {
1862 mask |= EV_WR|EV_WCLOSED;
1863 }
1864 break;
1865
1866 default:
1867 return;
1868 }
1869 if (mask) {
1870 /*
1871 * disarm... postevents are nops until this event is 'read' via
1872 * waitevent and then re-armed via modwatch
1873 */
1874 evq->ee_eventmask = 0;
1875
1876 /*
1877 * since events are disarmed until after the waitevent
1878 * the ee_req.er_xxxx fields can't change once we've
1879 * inserted this event into the proc queue...
1880 * therefore, the waitevent will see a 'consistent'
1881 * snapshot of the event, even though it won't hold
1882 * the pipe lock, and we're updating the event outside
1883 * of the proc lock, which it will hold
1884 */
1885 evq->ee_req.er_eventbits |= mask;
1886
1887 KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask, 1,0);
1888
1889 evprocenque(evq);
1890 }
1891 }
1892 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,1,0);
1893 }
1894
1895
1896 /*
1897 * given either a sockbuf or a socket run down the
1898 * event list and queue ready events found...
1899 * the socket must be locked by the caller
1900 */
1901 void
1902 postevent(struct socket *sp, struct sockbuf *sb, int event)
1903 {
1904 int mask;
1905 struct eventqelt *evq;
1906 struct tcpcb *tp;
1907
1908 if (sb)
1909 sp = sb->sb_so;
1910 if (sp == NULL)
1911 return;
1912
1913 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, (int)sp, event, 0, 0, 0);
1914
1915 for (evq = sp->so_evlist.tqh_first;
1916 evq != NULL; evq = evq->ee_slist.tqe_next) {
1917
1918 if (evq->ee_eventmask == 0)
1919 continue;
1920 mask = 0;
1921
1922 /* ready for reading:
1923 - byte cnt >= receive low water mark
1924 - read-half of conn closed
1925 - conn pending for listening sock
1926 - socket error pending
1927
1928 ready for writing
1929 - byte cnt avail >= send low water mark
1930 - write half of conn closed
1931 - socket error pending
1932 - non-blocking conn completed successfully
1933
1934 exception pending
1935 - out of band data
1936 - sock at out of band mark
1937 */
1938
1939 switch (event & EV_DMASK) {
1940
1941 case EV_OOB:
1942 if ((evq->ee_eventmask & EV_EX)) {
1943 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
1944 mask |= EV_EX|EV_OOB;
1945 }
1946 break;
1947
1948 case EV_RWBYTES|EV_OOB:
1949 if ((evq->ee_eventmask & EV_EX)) {
1950 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
1951 mask |= EV_EX|EV_OOB;
1952 }
1953 /*
1954 * fall into the next case
1955 */
1956 case EV_RWBYTES:
1957 if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
1958 if (sp->so_error) {
1959 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1960 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1961 (tp->t_state == TCPS_CLOSED)) {
1962 mask |= EV_RE|EV_RESET;
1963 break;
1964 }
1965 }
1966 }
1967 mask |= EV_RE;
1968 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
1969
1970 if (sp->so_state & SS_CANTRCVMORE) {
1971 mask |= EV_FIN;
1972 break;
1973 }
1974 }
1975 if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
1976 if (sp->so_error) {
1977 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1978 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1979 (tp->t_state == TCPS_CLOSED)) {
1980 mask |= EV_WR|EV_RESET;
1981 break;
1982 }
1983 }
1984 }
1985 mask |= EV_WR;
1986 evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
1987 }
1988 break;
1989
1990 case EV_RCONN:
1991 if ((evq->ee_eventmask & EV_RE)) {
1992 mask |= EV_RE|EV_RCONN;
1993 evq->ee_req.er_rcnt = sp->so_qlen + 1; // incl this one
1994 }
1995 break;
1996
1997 case EV_WCONN:
1998 if ((evq->ee_eventmask & EV_WR)) {
1999 mask |= EV_WR|EV_WCONN;
2000 }
2001 break;
2002
2003 case EV_RCLOSED:
2004 if ((evq->ee_eventmask & EV_RE)) {
2005 mask |= EV_RE|EV_RCLOSED;
2006 }
2007 break;
2008
2009 case EV_WCLOSED:
2010 if ((evq->ee_eventmask & EV_WR)) {
2011 mask |= EV_WR|EV_WCLOSED;
2012 }
2013 break;
2014
2015 case EV_FIN:
2016 if (evq->ee_eventmask & EV_RE) {
2017 mask |= EV_RE|EV_FIN;
2018 }
2019 break;
2020
2021 case EV_RESET:
2022 case EV_TIMEOUT:
2023 if (evq->ee_eventmask & EV_RE) {
2024 mask |= EV_RE | event;
2025 }
2026 if (evq->ee_eventmask & EV_WR) {
2027 mask |= EV_WR | event;
2028 }
2029 break;
2030
2031 default:
2032 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, -1, 0, 0, 0);
2033 return;
2034 } /* switch */
2035
2036 KERNEL_DEBUG(DBG_MISC_POST, (int)evq, evq->ee_eventmask, evq->ee_req.er_eventbits, mask, 0);
2037
2038 if (mask) {
2039 /*
2040 * disarm... postevents are nops until this event is 'read' via
2041 * waitevent and then re-armed via modwatch
2042 */
2043 evq->ee_eventmask = 0;
2044
2045 /*
2046 * since events are disarmed until after the waitevent
2047 * the ee_req.er_xxxx fields can't change once we've
2048 * inserted this event into the proc queue...
2049 * since waitevent can't see this event until we
2050 * enqueue it, waitevent will see a 'consistent'
2051 * snapshot of the event, even though it won't hold
2052 * the socket lock, and we're updating the event outside
2053 * of the proc lock, which it will hold
2054 */
2055 evq->ee_req.er_eventbits |= mask;
2056
2057 evprocenque(evq);
2058 }
2059 }
2060 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, 0, 0, 0, 0);
2061 }
2062
2063
2064 /*
2065 * watchevent system call. user passes us an event to watch
2066 * for. we malloc an event object, initialize it, and queue
2067 * it to the open socket. when the event occurs, postevent()
2068 * will enque it back to our proc where we can retrieve it
2069 * via waitevent().
2070 *
2071 * should this prevent duplicate events on same socket?
2072 */
2073 int
2074 watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
2075 {
2076 struct eventqelt *evq = (struct eventqelt *)0;
2077 struct eventqelt *np = NULL;
2078 struct eventreq *erp;
2079 struct fileproc *fp = NULL;
2080 int error;
2081
2082 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
2083
2084 // get a qelt and fill with users req
2085 MALLOC(evq, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
2086
2087 if (evq == NULL)
2088 panic("can't MALLOC evq");
2089 erp = &evq->ee_req;
2090
2091 // get users request pkt
2092 if ( (error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2093 sizeof(struct eventreq))) ) {
2094 FREE(evq, M_TEMP);
2095
2096 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2097 return(error);
2098 }
2099 KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,evq,0,0);
2100
2101 // validate, freeing qelt if errors
2102 error = 0;
2103 proc_fdlock(p);
2104
2105 if (erp->er_type != EV_FD) {
2106 error = EINVAL;
2107 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2108 error = EBADF;
2109 } else if (fp->f_type == DTYPE_SOCKET) {
2110 socket_lock((struct socket *)fp->f_data, 1);
2111 np = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2112 } else if (fp->f_type == DTYPE_PIPE) {
2113 PIPE_LOCK((struct pipe *)fp->f_data);
2114 np = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2115 } else {
2116 fp_drop(p, erp->er_handle, fp, 1);
2117 error = EINVAL;
2118 }
2119 proc_fdunlock(p);
2120
2121 if (error) {
2122 FREE(evq, M_TEMP);
2123
2124 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2125 return(error);
2126 }
2127
2128 /*
2129 * only allow one watch per file per proc
2130 */
2131 for ( ; np != NULL; np = np->ee_slist.tqe_next) {
2132 if (np->ee_proc == p) {
2133 if (fp->f_type == DTYPE_SOCKET)
2134 socket_unlock((struct socket *)fp->f_data, 1);
2135 else
2136 PIPE_UNLOCK((struct pipe *)fp->f_data);
2137 fp_drop(p, erp->er_handle, fp, 0);
2138 FREE(evq, M_TEMP);
2139
2140 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2141 return(EINVAL);
2142 }
2143 }
2144 erp->er_ecnt = erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
2145 evq->ee_proc = p;
2146 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2147 evq->ee_flags = 0;
2148
2149 if (fp->f_type == DTYPE_SOCKET) {
2150 TAILQ_INSERT_TAIL(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2151 postevent((struct socket *)fp->f_data, 0, EV_RWBYTES); // catch existing events
2152
2153 socket_unlock((struct socket *)fp->f_data, 1);
2154 } else {
2155 TAILQ_INSERT_TAIL(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2156 postpipeevent((struct pipe *)fp->f_data, EV_RWBYTES);
2157
2158 PIPE_UNLOCK((struct pipe *)fp->f_data);
2159 }
2160 fp_drop_event(p, erp->er_handle, fp);
2161
2162 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
2163 return(0);
2164 }
2165
2166
2167
2168 /*
2169 * waitevent system call.
2170 * grabs the next waiting event for this proc and returns
2171 * it. if no events, user can request to sleep with timeout
2172 * or poll mode (tv=NULL);
2173 */
2174 int
2175 waitevent(proc_t p, struct waitevent_args *uap, int *retval)
2176 {
2177 int error = 0;
2178 struct eventqelt *evq;
2179 struct eventreq er;
2180 uint64_t abstime, interval;
2181
2182 if (uap->tv) {
2183 struct timeval atv;
2184
2185 error = copyin(CAST_USER_ADDR_T(uap->tv), (caddr_t)&atv, sizeof (atv));
2186 if (error)
2187 return(error);
2188 if (itimerfix(&atv)) {
2189 error = EINVAL;
2190 return(error);
2191 }
2192 interval = tvtoabstime(&atv);
2193 } else
2194 interval = 0;
2195
2196 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
2197
2198 proc_lock(p);
2199 retry:
2200 if ((evq = p->p_evlist.tqh_first) != NULL) {
2201 /*
2202 * found one... make a local copy while it's still on the queue
2203 * to prevent it from changing while in the midst of copying
2204 * don't want to hold the proc lock across a copyout because
2205 * it might block on a page fault at the target in user space
2206 */
2207 bcopy((caddr_t)&evq->ee_req, (caddr_t)&er, sizeof (struct eventreq));
2208
2209 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist);
2210
2211 evq->ee_flags &= ~EV_QUEUED;
2212
2213 proc_unlock(p);
2214
2215 error = copyout((caddr_t)&er, CAST_USER_ADDR_T(uap->u_req), sizeof(struct eventreq));
2216
2217 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
2218 evq->ee_req.er_handle,evq->ee_req.er_eventbits,evq,0);
2219 return (error);
2220 }
2221 else {
2222 if (uap->tv && interval == 0) {
2223 proc_unlock(p);
2224 *retval = 1; // poll failed
2225
2226 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
2227 return (error);
2228 }
2229 if (interval != 0)
2230 clock_absolutetime_interval_to_deadline(interval, &abstime);
2231 else
2232 abstime = 0;
2233
2234 KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
2235
2236 error = msleep1(&p->p_evlist, &p->p_mlock, (PSOCK | PCATCH), "waitevent", abstime);
2237
2238 KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
2239
2240 if (error == 0)
2241 goto retry;
2242 if (error == ERESTART)
2243 error = EINTR;
2244 if (error == EWOULDBLOCK) {
2245 *retval = 1;
2246 error = 0;
2247 }
2248 }
2249 proc_unlock(p);
2250
2251 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
2252 return (error);
2253 }
2254
2255
2256 /*
2257 * modwatch system call. user passes in event to modify.
2258 * if we find it we reset the event bits and que/deque event
2259 * it needed.
2260 */
2261 int
2262 modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
2263 {
2264 struct eventreq er;
2265 struct eventreq *erp = &er;
2266 struct eventqelt *evq;
2267 int error;
2268 struct fileproc *fp;
2269 int flag;
2270
2271 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
2272
2273 /*
2274 * get user's request pkt
2275 */
2276 if ((error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2277 sizeof(struct eventreq)))) {
2278 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2279 return(error);
2280 }
2281 proc_fdlock(p);
2282
2283 if (erp->er_type != EV_FD) {
2284 error = EINVAL;
2285 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2286 error = EBADF;
2287 } else if (fp->f_type == DTYPE_SOCKET) {
2288 socket_lock((struct socket *)fp->f_data, 1);
2289 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2290 } else if (fp->f_type == DTYPE_PIPE) {
2291 PIPE_LOCK((struct pipe *)fp->f_data);
2292 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2293 } else {
2294 fp_drop(p, erp->er_handle, fp, 1);
2295 error = EINVAL;
2296 }
2297
2298 if (error) {
2299 proc_fdunlock(p);
2300 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2301 return(error);
2302 }
2303
2304 if ((uap->u_eventmask == EV_RM) && (fp->f_flags & FP_WAITEVENT)) {
2305 fp->f_flags &= ~FP_WAITEVENT;
2306 }
2307 proc_fdunlock(p);
2308
2309 // locate event if possible
2310 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2311 if (evq->ee_proc == p)
2312 break;
2313 }
2314 if (evq == NULL) {
2315 if (fp->f_type == DTYPE_SOCKET)
2316 socket_unlock((struct socket *)fp->f_data, 1);
2317 else
2318 PIPE_UNLOCK((struct pipe *)fp->f_data);
2319 fp_drop(p, erp->er_handle, fp, 0);
2320 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
2321 return(EINVAL);
2322 }
2323 KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
2324
2325 if (uap->u_eventmask == EV_RM) {
2326 EVPROCDEQUE(p, evq);
2327
2328 if (fp->f_type == DTYPE_SOCKET) {
2329 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2330 socket_unlock((struct socket *)fp->f_data, 1);
2331 } else {
2332 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2333 PIPE_UNLOCK((struct pipe *)fp->f_data);
2334 }
2335 fp_drop(p, erp->er_handle, fp, 0);
2336 FREE(evq, M_TEMP);
2337 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
2338 return(0);
2339 }
2340 switch (uap->u_eventmask & EV_MASK) {
2341
2342 case 0:
2343 flag = 0;
2344 break;
2345
2346 case EV_RE:
2347 case EV_WR:
2348 case EV_RE|EV_WR:
2349 flag = EV_RWBYTES;
2350 break;
2351
2352 case EV_EX:
2353 flag = EV_OOB;
2354 break;
2355
2356 case EV_EX|EV_RE:
2357 case EV_EX|EV_WR:
2358 case EV_EX|EV_RE|EV_WR:
2359 flag = EV_OOB|EV_RWBYTES;
2360 break;
2361
2362 default:
2363 if (fp->f_type == DTYPE_SOCKET)
2364 socket_unlock((struct socket *)fp->f_data, 1);
2365 else
2366 PIPE_UNLOCK((struct pipe *)fp->f_data);
2367 fp_drop(p, erp->er_handle, fp, 0);
2368 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2369 return(EINVAL);
2370 }
2371 /*
2372 * since we're holding the socket/pipe lock, the event
2373 * cannot go from the unqueued state to the queued state
2374 * however, it can go from the queued state to the unqueued state
2375 * since that direction is protected by the proc_lock...
2376 * so do a quick check for EV_QUEUED w/o holding the proc lock
2377 * since by far the common case will be NOT EV_QUEUED, this saves
2378 * us taking the proc_lock the majority of the time
2379 */
2380 if (evq->ee_flags & EV_QUEUED) {
2381 /*
2382 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2383 */
2384 EVPROCDEQUE(p, evq);
2385 }
2386 /*
2387 * while the event is off the proc queue and
2388 * we're holding the socket/pipe lock
2389 * it's safe to update these fields...
2390 */
2391 evq->ee_req.er_eventbits = 0;
2392 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2393
2394 if (fp->f_type == DTYPE_SOCKET) {
2395 postevent((struct socket *)fp->f_data, 0, flag);
2396 socket_unlock((struct socket *)fp->f_data, 1);
2397 }
2398 else {
2399 postpipeevent((struct pipe *)fp->f_data, flag);
2400 PIPE_UNLOCK((struct pipe *)fp->f_data);
2401 }
2402 fp_drop(p, erp->er_handle, fp, 0);
2403 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,fp->f_data,flag,0);
2404 return(0);
2405 }
2406
2407 /* this routine is called from the close of fd with proc_fdlock held */
2408 int
2409 waitevent_close(struct proc *p, struct fileproc *fp)
2410 {
2411 struct eventqelt *evq;
2412
2413
2414 fp->f_flags &= ~FP_WAITEVENT;
2415
2416 if (fp->f_type == DTYPE_SOCKET) {
2417 socket_lock((struct socket *)fp->f_data, 1);
2418 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2419 }
2420 else if (fp->f_type == DTYPE_PIPE) {
2421 PIPE_LOCK((struct pipe *)fp->f_data);
2422 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2423 }
2424 else {
2425 return(EINVAL);
2426 }
2427 proc_fdunlock(p);
2428
2429
2430 // locate event if possible
2431 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2432 if (evq->ee_proc == p)
2433 break;
2434 }
2435 if (evq == NULL) {
2436 if (fp->f_type == DTYPE_SOCKET)
2437 socket_unlock((struct socket *)fp->f_data, 1);
2438 else
2439 PIPE_UNLOCK((struct pipe *)fp->f_data);
2440
2441 proc_fdlock(p);
2442
2443 return(EINVAL);
2444 }
2445 EVPROCDEQUE(p, evq);
2446
2447 if (fp->f_type == DTYPE_SOCKET) {
2448 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2449 socket_unlock((struct socket *)fp->f_data, 1);
2450 } else {
2451 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2452 PIPE_UNLOCK((struct pipe *)fp->f_data);
2453 }
2454 FREE(evq, M_TEMP);
2455
2456 proc_fdlock(p);
2457
2458 return(0);
2459 }
2460