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