]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_generic.c
xnu-792.6.61.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 short mask;
1438
1439 /* convert the results back into revents */
1440 if (kevp->flags & EV_EOF)
1441 fds->revents |= POLLHUP;
1442 if (kevp->flags & EV_ERROR)
1443 fds->revents |= POLLERR;
1444 cont->pca_rfds++;
1445
1446 switch (kevp->filter) {
1447 case EVFILT_READ:
1448 if (fds->revents & POLLHUP)
1449 mask = (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND );
1450 else {
1451 mask = 0;
1452 if (kevp->data != 0)
1453 mask |= (POLLIN | POLLRDNORM );
1454 if (kevp->flags & EV_OOBAND)
1455 mask |= ( POLLPRI | POLLRDBAND );
1456 }
1457 fds->revents |= (fds->events & mask);
1458 break;
1459
1460 case EVFILT_WRITE:
1461 if (!(fds->revents & POLLHUP))
1462 fds->revents |= (fds->events & ( POLLOUT | POLLWRNORM | POLLWRBAND ));
1463 break;
1464
1465 case EVFILT_PROC:
1466 if (kevp->fflags & NOTE_EXTEND)
1467 fds->revents |= (fds->events & POLLEXTEND);
1468 if (kevp->fflags & NOTE_ATTRIB)
1469 fds->revents |= (fds->events & POLLATTRIB);
1470 if (kevp->fflags & NOTE_LINK)
1471 fds->revents |= (fds->events & POLLNLINK);
1472 if (kevp->fflags & NOTE_WRITE)
1473 fds->revents |= (fds->events & POLLWRITE);
1474 break;
1475 }
1476 return 0;
1477 }
1478
1479 int
1480 seltrue(__unused dev_t dev, __unused int flag, __unused struct proc *p)
1481 {
1482
1483 return (1);
1484 }
1485
1486 static int
1487 selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
1488 int nfd, int *count)
1489 {
1490 register struct filedesc *fdp = p->p_fd;
1491 register int msk, i, j, fd;
1492 register u_int32_t bits;
1493 struct fileproc *fp;
1494 int n = 0;
1495 u_int32_t *iptr;
1496 u_int nw;
1497 int error=0;
1498 int dropcount;
1499
1500 /*
1501 * Problems when reboot; due to MacOSX signal probs
1502 * in Beaker1C ; verify that the p->p_fd is valid
1503 */
1504 if (fdp == NULL) {
1505 *count=0;
1506 return(EIO);
1507 }
1508 nw = howmany(nfd, NFDBITS);
1509
1510 proc_fdlock(p);
1511 for (msk = 0; msk < 3; msk++) {
1512 iptr = (u_int32_t *)&ibits[msk * nw];
1513 for (i = 0; i < nfd; i += NFDBITS) {
1514 bits = iptr[i/NFDBITS];
1515 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1516 bits &= ~(1 << j);
1517 fp = fdp->fd_ofiles[fd];
1518 if (fp == NULL ||
1519 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1520 *count=0;
1521 error = EBADF;
1522 goto bad;
1523 }
1524 fp->f_iocount++;
1525 n++;
1526 }
1527 }
1528 }
1529 proc_fdunlock(p);
1530
1531 *count = n;
1532 return (0);
1533 bad:
1534 dropcount = 0;
1535
1536 if (n== 0)
1537 goto out;
1538 /* undo the iocounts */
1539 for (msk = 0; msk < 3; msk++) {
1540 iptr = (u_int32_t *)&ibits[msk * nw];
1541 for (i = 0; i < nfd; i += NFDBITS) {
1542 bits = iptr[i/NFDBITS];
1543 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1544 bits &= ~(1 << j);
1545 fp = fdp->fd_ofiles[fd];
1546 if (dropcount >= n)
1547 goto out;
1548 fp->f_iocount--;
1549
1550 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1551 p->p_fpdrainwait = 0;
1552 wakeup(&p->p_fpdrainwait);
1553 }
1554 dropcount++;
1555 }
1556 }
1557 }
1558 out:
1559 proc_fdunlock(p);
1560 return(error);
1561 }
1562
1563 static int
1564 seldrop(p, ibits, nfd)
1565 struct proc *p;
1566 u_int32_t *ibits;
1567 int nfd;
1568 {
1569 register struct filedesc *fdp = p->p_fd;
1570 register int msk, i, j, fd;
1571 register u_int32_t bits;
1572 struct fileproc *fp;
1573 int n = 0;
1574 u_int32_t *iptr;
1575 u_int nw;
1576
1577 /*
1578 * Problems when reboot; due to MacOSX signal probs
1579 * in Beaker1C ; verify that the p->p_fd is valid
1580 */
1581 if (fdp == NULL) {
1582 return(EIO);
1583 }
1584
1585 nw = howmany(nfd, NFDBITS);
1586
1587
1588 proc_fdlock(p);
1589 for (msk = 0; msk < 3; msk++) {
1590 iptr = (u_int32_t *)&ibits[msk * nw];
1591 for (i = 0; i < nfd; i += NFDBITS) {
1592 bits = iptr[i/NFDBITS];
1593 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1594 bits &= ~(1 << j);
1595 fp = fdp->fd_ofiles[fd];
1596 if (fp == NULL
1597 #if 0
1598 /* if you are here then it is being closed */
1599 || (fdp->fd_ofileflags[fd] & UF_RESERVED)
1600 #endif
1601 ) {
1602 proc_fdunlock(p);
1603 return(EBADF);
1604 }
1605 n++;
1606 fp->f_iocount--;
1607 fp->f_flags &= ~FP_INSELECT;
1608
1609 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1610 p->p_fpdrainwait = 0;
1611 wakeup(&p->p_fpdrainwait);
1612 }
1613 }
1614 }
1615 }
1616 proc_fdunlock(p);
1617 return (0);
1618 }
1619
1620 /*
1621 * Record a select request.
1622 */
1623 void
1624 selrecord(__unused struct proc *selector, struct selinfo *sip, void * p_wql)
1625 {
1626 thread_t cur_act = current_thread();
1627 struct uthread * ut = get_bsdthread_info(cur_act);
1628
1629 /* need to look at collisions */
1630
1631 if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
1632 return;
1633 }
1634
1635 /*do not record if this is second pass of select */
1636 if((p_wql == (void *)0)) {
1637 return;
1638 }
1639
1640 if ((sip->si_flags & SI_INITED) == 0) {
1641 wait_queue_init(&sip->si_wait_queue, SYNC_POLICY_FIFO);
1642 sip->si_flags |= SI_INITED;
1643 sip->si_flags &= ~SI_CLEAR;
1644 }
1645
1646 if (sip->si_flags & SI_RECORDED) {
1647 sip->si_flags |= SI_COLL;
1648 } else
1649 sip->si_flags &= ~SI_COLL;
1650
1651 sip->si_flags |= SI_RECORDED;
1652 if (!wait_queue_member(&sip->si_wait_queue, ut->uu_select.wqset))
1653 wait_queue_link_noalloc(&sip->si_wait_queue, ut->uu_select.wqset,
1654 (wait_queue_link_t)p_wql);
1655
1656 return;
1657 }
1658
1659 void
1660 selwakeup(sip)
1661 register struct selinfo *sip;
1662 {
1663
1664 if ((sip->si_flags & SI_INITED) == 0) {
1665 return;
1666 }
1667
1668 if (sip->si_flags & SI_COLL) {
1669 nselcoll++;
1670 sip->si_flags &= ~SI_COLL;
1671 #if 0
1672 /* will not support */
1673 //wakeup((caddr_t)&selwait);
1674 #endif
1675 }
1676
1677 if (sip->si_flags & SI_RECORDED) {
1678 wait_queue_wakeup_all(&sip->si_wait_queue, &selwait, THREAD_AWAKENED);
1679 sip->si_flags &= ~SI_RECORDED;
1680 }
1681
1682 }
1683
1684 void
1685 selthreadclear(sip)
1686 register struct selinfo *sip;
1687 {
1688
1689 if ((sip->si_flags & SI_INITED) == 0) {
1690 return;
1691 }
1692 if (sip->si_flags & SI_RECORDED) {
1693 selwakeup(sip);
1694 sip->si_flags &= ~(SI_RECORDED | SI_COLL);
1695 }
1696 sip->si_flags |= SI_CLEAR;
1697 wait_queue_unlinkall_nofree(&sip->si_wait_queue);
1698 }
1699
1700
1701
1702
1703 #define DBG_EVENT 0x10
1704
1705 #define DBG_POST 0x10
1706 #define DBG_WATCH 0x11
1707 #define DBG_WAIT 0x12
1708 #define DBG_MOD 0x13
1709 #define DBG_EWAKEUP 0x14
1710 #define DBG_ENQUEUE 0x15
1711 #define DBG_DEQUEUE 0x16
1712
1713 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1714 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1715 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1716 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1717 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1718 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1719 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1720
1721
1722 #define EVPROCDEQUE(p, evq) do { \
1723 proc_lock(p); \
1724 if (evq->ee_flags & EV_QUEUED) { \
1725 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1726 evq->ee_flags &= ~EV_QUEUED; \
1727 } \
1728 proc_unlock(p); \
1729 } while (0);
1730
1731
1732 /*
1733 * called upon socket close. deque and free all events for
1734 * the socket... socket must be locked by caller.
1735 */
1736 void
1737 evsofree(struct socket *sp)
1738 {
1739 struct eventqelt *evq, *next;
1740 proc_t p;
1741
1742 if (sp == NULL)
1743 return;
1744
1745 for (evq = sp->so_evlist.tqh_first; evq != NULL; evq = next) {
1746 next = evq->ee_slist.tqe_next;
1747 p = evq->ee_proc;
1748
1749 if (evq->ee_flags & EV_QUEUED) {
1750 EVPROCDEQUE(p, evq);
1751 }
1752 TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist); // remove from socket q
1753 FREE(evq, M_TEMP);
1754 }
1755 }
1756
1757
1758 /*
1759 * called upon pipe close. deque and free all events for
1760 * the pipe... pipe must be locked by caller
1761 */
1762 void
1763 evpipefree(struct pipe *cpipe)
1764 {
1765 struct eventqelt *evq, *next;
1766 proc_t p;
1767
1768 for (evq = cpipe->pipe_evlist.tqh_first; evq != NULL; evq = next) {
1769 next = evq->ee_slist.tqe_next;
1770 p = evq->ee_proc;
1771
1772 EVPROCDEQUE(p, evq);
1773
1774 TAILQ_REMOVE(&cpipe->pipe_evlist, evq, ee_slist); // remove from pipe q
1775 FREE(evq, M_TEMP);
1776 }
1777 }
1778
1779
1780 /*
1781 * enqueue this event if it's not already queued. wakeup
1782 * the proc if we do queue this event to it...
1783 * entered with proc lock held... we drop it before
1784 * doing the wakeup and return in that state
1785 */
1786 static void
1787 evprocenque(struct eventqelt *evq)
1788 {
1789 proc_t p;
1790
1791 assert(evq);
1792 p = evq->ee_proc;
1793
1794 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, evq, evq->ee_flags, evq->ee_eventmask,0,0);
1795
1796 proc_lock(p);
1797
1798 if (evq->ee_flags & EV_QUEUED) {
1799 proc_unlock(p);
1800
1801 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1802 return;
1803 }
1804 evq->ee_flags |= EV_QUEUED;
1805
1806 TAILQ_INSERT_TAIL(&p->p_evlist, evq, ee_plist);
1807
1808 proc_unlock(p);
1809
1810 wakeup(&p->p_evlist);
1811
1812 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1813 }
1814
1815
1816 /*
1817 * pipe lock must be taken by the caller
1818 */
1819 void
1820 postpipeevent(struct pipe *pipep, int event)
1821 {
1822 int mask;
1823 struct eventqelt *evq;
1824
1825 if (pipep == NULL)
1826 return;
1827 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,1,0);
1828
1829 for (evq = pipep->pipe_evlist.tqh_first;
1830 evq != NULL; evq = evq->ee_slist.tqe_next) {
1831
1832 if (evq->ee_eventmask == 0)
1833 continue;
1834 mask = 0;
1835
1836 switch (event & (EV_RWBYTES | EV_RCLOSED | EV_WCLOSED)) {
1837
1838 case EV_RWBYTES:
1839 if ((evq->ee_eventmask & EV_RE) && pipep->pipe_buffer.cnt) {
1840 mask |= EV_RE;
1841 evq->ee_req.er_rcnt = pipep->pipe_buffer.cnt;
1842 }
1843 if ((evq->ee_eventmask & EV_WR) &&
1844 (pipep->pipe_buffer.size - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
1845
1846 if (pipep->pipe_state & PIPE_EOF) {
1847 mask |= EV_WR|EV_RESET;
1848 break;
1849 }
1850 mask |= EV_WR;
1851 evq->ee_req.er_wcnt = pipep->pipe_buffer.size - pipep->pipe_buffer.cnt;
1852 }
1853 break;
1854
1855 case EV_WCLOSED:
1856 case EV_RCLOSED:
1857 if ((evq->ee_eventmask & EV_RE)) {
1858 mask |= EV_RE|EV_RCLOSED;
1859 }
1860 if ((evq->ee_eventmask & EV_WR)) {
1861 mask |= EV_WR|EV_WCLOSED;
1862 }
1863 break;
1864
1865 default:
1866 return;
1867 }
1868 if (mask) {
1869 /*
1870 * disarm... postevents are nops until this event is 'read' via
1871 * waitevent and then re-armed via modwatch
1872 */
1873 evq->ee_eventmask = 0;
1874
1875 /*
1876 * since events are disarmed until after the waitevent
1877 * the ee_req.er_xxxx fields can't change once we've
1878 * inserted this event into the proc queue...
1879 * therefore, the waitevent will see a 'consistent'
1880 * snapshot of the event, even though it won't hold
1881 * the pipe lock, and we're updating the event outside
1882 * of the proc lock, which it will hold
1883 */
1884 evq->ee_req.er_eventbits |= mask;
1885
1886 KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask, 1,0);
1887
1888 evprocenque(evq);
1889 }
1890 }
1891 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,1,0);
1892 }
1893
1894
1895 /*
1896 * given either a sockbuf or a socket run down the
1897 * event list and queue ready events found...
1898 * the socket must be locked by the caller
1899 */
1900 void
1901 postevent(struct socket *sp, struct sockbuf *sb, int event)
1902 {
1903 int mask;
1904 struct eventqelt *evq;
1905 struct tcpcb *tp;
1906
1907 if (sb)
1908 sp = sb->sb_so;
1909 if (sp == NULL)
1910 return;
1911
1912 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, (int)sp, event, 0, 0, 0);
1913
1914 for (evq = sp->so_evlist.tqh_first;
1915 evq != NULL; evq = evq->ee_slist.tqe_next) {
1916
1917 if (evq->ee_eventmask == 0)
1918 continue;
1919 mask = 0;
1920
1921 /* ready for reading:
1922 - byte cnt >= receive low water mark
1923 - read-half of conn closed
1924 - conn pending for listening sock
1925 - socket error pending
1926
1927 ready for writing
1928 - byte cnt avail >= send low water mark
1929 - write half of conn closed
1930 - socket error pending
1931 - non-blocking conn completed successfully
1932
1933 exception pending
1934 - out of band data
1935 - sock at out of band mark
1936 */
1937
1938 switch (event & EV_DMASK) {
1939
1940 case 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 break;
1946
1947 case EV_RWBYTES|EV_OOB:
1948 if ((evq->ee_eventmask & EV_EX)) {
1949 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
1950 mask |= EV_EX|EV_OOB;
1951 }
1952 /*
1953 * fall into the next case
1954 */
1955 case EV_RWBYTES:
1956 if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
1957 if (sp->so_error) {
1958 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1959 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1960 (tp->t_state == TCPS_CLOSED)) {
1961 mask |= EV_RE|EV_RESET;
1962 break;
1963 }
1964 }
1965 }
1966 mask |= EV_RE;
1967 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
1968
1969 if (sp->so_state & SS_CANTRCVMORE) {
1970 mask |= EV_FIN;
1971 break;
1972 }
1973 }
1974 if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
1975 if (sp->so_error) {
1976 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1977 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1978 (tp->t_state == TCPS_CLOSED)) {
1979 mask |= EV_WR|EV_RESET;
1980 break;
1981 }
1982 }
1983 }
1984 mask |= EV_WR;
1985 evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
1986 }
1987 break;
1988
1989 case EV_RCONN:
1990 if ((evq->ee_eventmask & EV_RE)) {
1991 mask |= EV_RE|EV_RCONN;
1992 evq->ee_req.er_rcnt = sp->so_qlen + 1; // incl this one
1993 }
1994 break;
1995
1996 case EV_WCONN:
1997 if ((evq->ee_eventmask & EV_WR)) {
1998 mask |= EV_WR|EV_WCONN;
1999 }
2000 break;
2001
2002 case EV_RCLOSED:
2003 if ((evq->ee_eventmask & EV_RE)) {
2004 mask |= EV_RE|EV_RCLOSED;
2005 }
2006 break;
2007
2008 case EV_WCLOSED:
2009 if ((evq->ee_eventmask & EV_WR)) {
2010 mask |= EV_WR|EV_WCLOSED;
2011 }
2012 break;
2013
2014 case EV_FIN:
2015 if (evq->ee_eventmask & EV_RE) {
2016 mask |= EV_RE|EV_FIN;
2017 }
2018 break;
2019
2020 case EV_RESET:
2021 case EV_TIMEOUT:
2022 if (evq->ee_eventmask & EV_RE) {
2023 mask |= EV_RE | event;
2024 }
2025 if (evq->ee_eventmask & EV_WR) {
2026 mask |= EV_WR | event;
2027 }
2028 break;
2029
2030 default:
2031 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, -1, 0, 0, 0);
2032 return;
2033 } /* switch */
2034
2035 KERNEL_DEBUG(DBG_MISC_POST, (int)evq, evq->ee_eventmask, evq->ee_req.er_eventbits, mask, 0);
2036
2037 if (mask) {
2038 /*
2039 * disarm... postevents are nops until this event is 'read' via
2040 * waitevent and then re-armed via modwatch
2041 */
2042 evq->ee_eventmask = 0;
2043
2044 /*
2045 * since events are disarmed until after the waitevent
2046 * the ee_req.er_xxxx fields can't change once we've
2047 * inserted this event into the proc queue...
2048 * since waitevent can't see this event until we
2049 * enqueue it, waitevent will see a 'consistent'
2050 * snapshot of the event, even though it won't hold
2051 * the socket lock, and we're updating the event outside
2052 * of the proc lock, which it will hold
2053 */
2054 evq->ee_req.er_eventbits |= mask;
2055
2056 evprocenque(evq);
2057 }
2058 }
2059 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, 0, 0, 0, 0);
2060 }
2061
2062
2063 /*
2064 * watchevent system call. user passes us an event to watch
2065 * for. we malloc an event object, initialize it, and queue
2066 * it to the open socket. when the event occurs, postevent()
2067 * will enque it back to our proc where we can retrieve it
2068 * via waitevent().
2069 *
2070 * should this prevent duplicate events on same socket?
2071 */
2072 int
2073 watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
2074 {
2075 struct eventqelt *evq = (struct eventqelt *)0;
2076 struct eventqelt *np = NULL;
2077 struct eventreq *erp;
2078 struct fileproc *fp = NULL;
2079 int error;
2080
2081 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
2082
2083 // get a qelt and fill with users req
2084 MALLOC(evq, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
2085
2086 if (evq == NULL)
2087 panic("can't MALLOC evq");
2088 erp = &evq->ee_req;
2089
2090 // get users request pkt
2091 if ( (error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2092 sizeof(struct eventreq))) ) {
2093 FREE(evq, M_TEMP);
2094
2095 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2096 return(error);
2097 }
2098 KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,evq,0,0);
2099
2100 // validate, freeing qelt if errors
2101 error = 0;
2102 proc_fdlock(p);
2103
2104 if (erp->er_type != EV_FD) {
2105 error = EINVAL;
2106 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2107 error = EBADF;
2108 } else if (fp->f_type == DTYPE_SOCKET) {
2109 socket_lock((struct socket *)fp->f_data, 1);
2110 np = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2111 } else if (fp->f_type == DTYPE_PIPE) {
2112 PIPE_LOCK((struct pipe *)fp->f_data);
2113 np = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2114 } else {
2115 fp_drop(p, erp->er_handle, fp, 1);
2116 error = EINVAL;
2117 }
2118 proc_fdunlock(p);
2119
2120 if (error) {
2121 FREE(evq, M_TEMP);
2122
2123 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2124 return(error);
2125 }
2126
2127 /*
2128 * only allow one watch per file per proc
2129 */
2130 for ( ; np != NULL; np = np->ee_slist.tqe_next) {
2131 if (np->ee_proc == p) {
2132 if (fp->f_type == DTYPE_SOCKET)
2133 socket_unlock((struct socket *)fp->f_data, 1);
2134 else
2135 PIPE_UNLOCK((struct pipe *)fp->f_data);
2136 fp_drop(p, erp->er_handle, fp, 0);
2137 FREE(evq, M_TEMP);
2138
2139 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2140 return(EINVAL);
2141 }
2142 }
2143 erp->er_ecnt = erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
2144 evq->ee_proc = p;
2145 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2146 evq->ee_flags = 0;
2147
2148 if (fp->f_type == DTYPE_SOCKET) {
2149 TAILQ_INSERT_TAIL(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2150 postevent((struct socket *)fp->f_data, 0, EV_RWBYTES); // catch existing events
2151
2152 socket_unlock((struct socket *)fp->f_data, 1);
2153 } else {
2154 TAILQ_INSERT_TAIL(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2155 postpipeevent((struct pipe *)fp->f_data, EV_RWBYTES);
2156
2157 PIPE_UNLOCK((struct pipe *)fp->f_data);
2158 }
2159 fp_drop_event(p, erp->er_handle, fp);
2160
2161 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
2162 return(0);
2163 }
2164
2165
2166
2167 /*
2168 * waitevent system call.
2169 * grabs the next waiting event for this proc and returns
2170 * it. if no events, user can request to sleep with timeout
2171 * or poll mode (tv=NULL);
2172 */
2173 int
2174 waitevent(proc_t p, struct waitevent_args *uap, int *retval)
2175 {
2176 int error = 0;
2177 struct eventqelt *evq;
2178 struct eventreq er;
2179 uint64_t abstime, interval;
2180
2181 if (uap->tv) {
2182 struct timeval atv;
2183
2184 error = copyin(CAST_USER_ADDR_T(uap->tv), (caddr_t)&atv, sizeof (atv));
2185 if (error)
2186 return(error);
2187 if (itimerfix(&atv)) {
2188 error = EINVAL;
2189 return(error);
2190 }
2191 interval = tvtoabstime(&atv);
2192 } else
2193 interval = 0;
2194
2195 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
2196
2197 proc_lock(p);
2198 retry:
2199 if ((evq = p->p_evlist.tqh_first) != NULL) {
2200 /*
2201 * found one... make a local copy while it's still on the queue
2202 * to prevent it from changing while in the midst of copying
2203 * don't want to hold the proc lock across a copyout because
2204 * it might block on a page fault at the target in user space
2205 */
2206 bcopy((caddr_t)&evq->ee_req, (caddr_t)&er, sizeof (struct eventreq));
2207
2208 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist);
2209
2210 evq->ee_flags &= ~EV_QUEUED;
2211
2212 proc_unlock(p);
2213
2214 error = copyout((caddr_t)&er, CAST_USER_ADDR_T(uap->u_req), sizeof(struct eventreq));
2215
2216 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
2217 evq->ee_req.er_handle,evq->ee_req.er_eventbits,evq,0);
2218 return (error);
2219 }
2220 else {
2221 if (uap->tv && interval == 0) {
2222 proc_unlock(p);
2223 *retval = 1; // poll failed
2224
2225 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
2226 return (error);
2227 }
2228 if (interval != 0)
2229 clock_absolutetime_interval_to_deadline(interval, &abstime);
2230 else
2231 abstime = 0;
2232
2233 KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
2234
2235 error = msleep1(&p->p_evlist, &p->p_mlock, (PSOCK | PCATCH), "waitevent", abstime);
2236
2237 KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
2238
2239 if (error == 0)
2240 goto retry;
2241 if (error == ERESTART)
2242 error = EINTR;
2243 if (error == EWOULDBLOCK) {
2244 *retval = 1;
2245 error = 0;
2246 }
2247 }
2248 proc_unlock(p);
2249
2250 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
2251 return (error);
2252 }
2253
2254
2255 /*
2256 * modwatch system call. user passes in event to modify.
2257 * if we find it we reset the event bits and que/deque event
2258 * it needed.
2259 */
2260 int
2261 modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
2262 {
2263 struct eventreq er;
2264 struct eventreq *erp = &er;
2265 struct eventqelt *evq;
2266 int error;
2267 struct fileproc *fp;
2268 int flag;
2269
2270 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
2271
2272 /*
2273 * get user's request pkt
2274 */
2275 if ((error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2276 sizeof(struct eventreq)))) {
2277 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2278 return(error);
2279 }
2280 proc_fdlock(p);
2281
2282 if (erp->er_type != EV_FD) {
2283 error = EINVAL;
2284 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2285 error = EBADF;
2286 } else if (fp->f_type == DTYPE_SOCKET) {
2287 socket_lock((struct socket *)fp->f_data, 1);
2288 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2289 } else if (fp->f_type == DTYPE_PIPE) {
2290 PIPE_LOCK((struct pipe *)fp->f_data);
2291 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2292 } else {
2293 fp_drop(p, erp->er_handle, fp, 1);
2294 error = EINVAL;
2295 }
2296
2297 if (error) {
2298 proc_fdunlock(p);
2299 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2300 return(error);
2301 }
2302
2303 if ((uap->u_eventmask == EV_RM) && (fp->f_flags & FP_WAITEVENT)) {
2304 fp->f_flags &= ~FP_WAITEVENT;
2305 }
2306 proc_fdunlock(p);
2307
2308 // locate event if possible
2309 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2310 if (evq->ee_proc == p)
2311 break;
2312 }
2313 if (evq == NULL) {
2314 if (fp->f_type == DTYPE_SOCKET)
2315 socket_unlock((struct socket *)fp->f_data, 1);
2316 else
2317 PIPE_UNLOCK((struct pipe *)fp->f_data);
2318 fp_drop(p, erp->er_handle, fp, 0);
2319 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
2320 return(EINVAL);
2321 }
2322 KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
2323
2324 if (uap->u_eventmask == EV_RM) {
2325 EVPROCDEQUE(p, evq);
2326
2327 if (fp->f_type == DTYPE_SOCKET) {
2328 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2329 socket_unlock((struct socket *)fp->f_data, 1);
2330 } else {
2331 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2332 PIPE_UNLOCK((struct pipe *)fp->f_data);
2333 }
2334 fp_drop(p, erp->er_handle, fp, 0);
2335 FREE(evq, M_TEMP);
2336 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
2337 return(0);
2338 }
2339 switch (uap->u_eventmask & EV_MASK) {
2340
2341 case 0:
2342 flag = 0;
2343 break;
2344
2345 case EV_RE:
2346 case EV_WR:
2347 case EV_RE|EV_WR:
2348 flag = EV_RWBYTES;
2349 break;
2350
2351 case EV_EX:
2352 flag = EV_OOB;
2353 break;
2354
2355 case EV_EX|EV_RE:
2356 case EV_EX|EV_WR:
2357 case EV_EX|EV_RE|EV_WR:
2358 flag = EV_OOB|EV_RWBYTES;
2359 break;
2360
2361 default:
2362 if (fp->f_type == DTYPE_SOCKET)
2363 socket_unlock((struct socket *)fp->f_data, 1);
2364 else
2365 PIPE_UNLOCK((struct pipe *)fp->f_data);
2366 fp_drop(p, erp->er_handle, fp, 0);
2367 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2368 return(EINVAL);
2369 }
2370 /*
2371 * since we're holding the socket/pipe lock, the event
2372 * cannot go from the unqueued state to the queued state
2373 * however, it can go from the queued state to the unqueued state
2374 * since that direction is protected by the proc_lock...
2375 * so do a quick check for EV_QUEUED w/o holding the proc lock
2376 * since by far the common case will be NOT EV_QUEUED, this saves
2377 * us taking the proc_lock the majority of the time
2378 */
2379 if (evq->ee_flags & EV_QUEUED) {
2380 /*
2381 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2382 */
2383 EVPROCDEQUE(p, evq);
2384 }
2385 /*
2386 * while the event is off the proc queue and
2387 * we're holding the socket/pipe lock
2388 * it's safe to update these fields...
2389 */
2390 evq->ee_req.er_eventbits = 0;
2391 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2392
2393 if (fp->f_type == DTYPE_SOCKET) {
2394 postevent((struct socket *)fp->f_data, 0, flag);
2395 socket_unlock((struct socket *)fp->f_data, 1);
2396 }
2397 else {
2398 postpipeevent((struct pipe *)fp->f_data, flag);
2399 PIPE_UNLOCK((struct pipe *)fp->f_data);
2400 }
2401 fp_drop(p, erp->er_handle, fp, 0);
2402 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,fp->f_data,flag,0);
2403 return(0);
2404 }
2405
2406 /* this routine is called from the close of fd with proc_fdlock held */
2407 int
2408 waitevent_close(struct proc *p, struct fileproc *fp)
2409 {
2410 struct eventqelt *evq;
2411
2412
2413 fp->f_flags &= ~FP_WAITEVENT;
2414
2415 if (fp->f_type == DTYPE_SOCKET) {
2416 socket_lock((struct socket *)fp->f_data, 1);
2417 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2418 }
2419 else if (fp->f_type == DTYPE_PIPE) {
2420 PIPE_LOCK((struct pipe *)fp->f_data);
2421 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2422 }
2423 else {
2424 return(EINVAL);
2425 }
2426 proc_fdunlock(p);
2427
2428
2429 // locate event if possible
2430 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2431 if (evq->ee_proc == p)
2432 break;
2433 }
2434 if (evq == NULL) {
2435 if (fp->f_type == DTYPE_SOCKET)
2436 socket_unlock((struct socket *)fp->f_data, 1);
2437 else
2438 PIPE_UNLOCK((struct pipe *)fp->f_data);
2439
2440 proc_fdlock(p);
2441
2442 return(EINVAL);
2443 }
2444 EVPROCDEQUE(p, evq);
2445
2446 if (fp->f_type == DTYPE_SOCKET) {
2447 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2448 socket_unlock((struct socket *)fp->f_data, 1);
2449 } else {
2450 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2451 PIPE_UNLOCK((struct pipe *)fp->f_data);
2452 }
2453 FREE(evq, M_TEMP);
2454
2455 proc_fdlock(p);
2456
2457 return(0);
2458 }
2459