]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_generic.c
xnu-201.14.tar.gz
[apple/xnu.git] / bsd / kern / sys_generic.c
1 /*
2 * Copyright (c) 2000 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.h>
68 #include <sys/proc.h>
69 #include <sys/socketvar.h>
70 #include <sys/uio.h>
71 #include <sys/kernel.h>
72 #include <sys/stat.h>
73 #include <sys/malloc.h>
74
75 #if KTRACE
76 #include <sys/ktrace.h>
77 #endif
78
79 #include <sys/mount.h>
80 #include <sys/protosw.h>
81 #include <sys/ev.h>
82 #include <sys/user.h>
83 #include <sys/kdebug.h>
84 #include <kern/assert.h>
85 #include <kern/thread_act.h>
86
87 #include <sys/mbuf.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/errno.h>
91
92 #include <net/if.h>
93 #include <net/route.h>
94
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/ip_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/tcp.h>
102 #include <netinet/tcp_fsm.h>
103 #include <netinet/tcp_seq.h>
104 #include <netinet/tcp_timer.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet/tcpip.h>
107 #include <netinet/tcp_debug.h>
108 /* for wait queue based select */
109 #include <kern/wait_queue.h>
110
111 /*
112 * Read system call.
113 */
114 struct read_args {
115 int fd;
116 char *cbuf;
117 u_int nbyte;
118 };
119 /* ARGSUSED */
120 read(p, uap, retval)
121 struct proc *p;
122 register struct read_args *uap;
123 register_t *retval;
124 {
125 struct uio auio;
126 struct iovec aiov;
127
128 aiov.iov_base = (caddr_t)uap->cbuf;
129 aiov.iov_len = uap->nbyte;
130 auio.uio_iov = &aiov;
131 auio.uio_iovcnt = 1;
132 auio.uio_rw = UIO_READ;
133 return (rwuio(p, uap->fd, &auio, UIO_READ, retval));
134 }
135
136 struct readv_args {
137 int fd;
138 struct iovec *iovp;
139 u_int iovcnt;
140 };
141 readv(p, uap, retval)
142 struct proc *p;
143 register struct readv_args *uap;
144 int *retval;
145 {
146 struct uio auio;
147 register struct iovec *iov;
148 int error;
149 struct iovec aiov[UIO_SMALLIOV];
150
151 if (uap->iovcnt > UIO_SMALLIOV) {
152 if (uap->iovcnt > UIO_MAXIOV)
153 return (EINVAL);
154 if ((iov = (struct iovec *)
155 kalloc(sizeof(struct iovec) * (uap->iovcnt))) == 0)
156 return (ENOMEM);
157 } else
158 iov = aiov;
159 auio.uio_iov = iov;
160 auio.uio_iovcnt = uap->iovcnt;
161 auio.uio_rw = UIO_READ;
162 error = copyin((caddr_t)uap->iovp, (caddr_t)iov,
163 uap->iovcnt * sizeof (struct iovec));
164 if (!error)
165 error = rwuio(p, uap->fd, &auio, UIO_READ, retval);
166 if (uap->iovcnt > UIO_SMALLIOV)
167 kfree(iov, sizeof(struct iovec)*uap->iovcnt);
168 return (error);
169 }
170
171 /*
172 * Write system call
173 */
174 struct write_args {
175 int fd;
176 char *cbuf;
177 u_int nbyte;
178 };
179 write(p, uap, retval)
180 struct proc *p;
181 register struct write_args *uap;
182 int *retval;
183 {
184 struct uio auio;
185 struct iovec aiov;
186
187 aiov.iov_base = uap->cbuf;
188 aiov.iov_len = uap->nbyte;
189 auio.uio_iov = &aiov;
190 auio.uio_iovcnt = 1;
191 auio.uio_rw = UIO_WRITE;
192 return (rwuio(p, uap->fd, &auio, UIO_WRITE, retval));
193 }
194
195 struct writev_args {
196 int fd;
197 struct iovec *iovp;
198 u_int iovcnt;
199 };
200 writev(p, uap, retval)
201 struct proc *p;
202 register struct writev_args *uap;
203 int *retval;
204 {
205 struct uio auio;
206 register struct iovec *iov;
207 int error;
208 struct iovec aiov[UIO_SMALLIOV];
209
210 if (uap->iovcnt > UIO_SMALLIOV) {
211 if (uap->iovcnt > UIO_MAXIOV)
212 return (EINVAL);
213 if ((iov = (struct iovec *)
214 kalloc(sizeof(struct iovec) * (uap->iovcnt))) == 0)
215 return (ENOMEM);
216 } else
217 iov = aiov;
218 auio.uio_iov = iov;
219 auio.uio_iovcnt = uap->iovcnt;
220 auio.uio_rw = UIO_WRITE;
221 error = copyin((caddr_t)uap->iovp, (caddr_t)iov,
222 uap->iovcnt * sizeof (struct iovec));
223 if (!error)
224 error = rwuio(p, uap->fd, &auio, UIO_WRITE, retval);
225 if (uap->iovcnt > UIO_SMALLIOV)
226 kfree(iov, sizeof(struct iovec)*uap->iovcnt);
227 return (error);
228 }
229
230 rwuio(p, fdes, uio, rw, retval)
231 struct proc *p;
232 int fdes;
233 register struct uio *uio;
234 enum uio_rw rw;
235 int *retval;
236 {
237 struct file *fp;
238 register struct iovec *iov;
239 int i, count, flag, error;
240
241 if (error = fdgetf(p, fdes, &fp))
242 return (error);
243
244 if ((fp->f_flag&(rw==UIO_READ ? FREAD : FWRITE)) == 0) {
245 return(EBADF);
246 }
247 uio->uio_resid = 0;
248 uio->uio_segflg = UIO_USERSPACE;
249 uio->uio_procp = p;
250 iov = uio->uio_iov;
251 for (i = 0; i < uio->uio_iovcnt; i++) {
252 if (iov->iov_len < 0) {
253 return(EINVAL);
254 }
255 uio->uio_resid += iov->iov_len;
256 if (uio->uio_resid < 0) {
257 return(EINVAL);
258 }
259 iov++;
260 }
261 count = uio->uio_resid;
262 if (rw == UIO_READ) {
263 if (error = (*fp->f_ops->fo_read)(fp, uio, fp->f_cred))
264 if (uio->uio_resid != count && (error == ERESTART ||
265 error == EINTR || error == EWOULDBLOCK))
266 error = 0;
267 } else {
268 if (error = (*fp->f_ops->fo_write)(fp, uio, fp->f_cred)) {
269 if (uio->uio_resid != count && (error == ERESTART ||
270 error == EINTR || error == EWOULDBLOCK))
271 error = 0;
272 if (error == EPIPE)
273 psignal(p, SIGPIPE);
274 }
275 }
276 *retval = count - uio->uio_resid;
277 return(error);
278 }
279
280 /*
281 * Ioctl system call
282 */
283 struct ioctl_args {
284 int fd;
285 u_long com;
286 caddr_t data;
287 };
288 /* ARGSUSED */
289 ioctl(p, uap, retval)
290 struct proc *p;
291 register struct ioctl_args *uap;
292 register_t *retval;
293 {
294 struct file *fp;
295 register u_long com;
296 register int error;
297 register u_int size;
298 caddr_t data, memp;
299 int tmp;
300 #define STK_PARAMS 128
301 char stkbuf[STK_PARAMS];
302
303 if (error = fdgetf(p, uap->fd, &fp))
304 return (error);
305
306 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
307 return (EBADF);
308
309 /*### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
310 * while implementing an ATioctl system call
311 */
312 #if NETAT
313 {
314 extern int appletalk_inited;
315
316 if (appletalk_inited && ((uap->com & 0x0000FFFF) == 0xff99)) {
317 #ifdef APPLETALK_DEBUG
318 kprintf("ioctl: special AppleTalk \n");
319 #endif
320 error = (*fp->f_ops->fo_ioctl)(fp, uap->com, uap->data, p);
321 return(error);
322 }
323 }
324
325 #endif /* NETAT */
326
327
328 switch (com = uap->com) {
329 case FIONCLEX:
330 *fdflags(p, uap->fd) &= ~UF_EXCLOSE;
331 return (0);
332 case FIOCLEX:
333 *fdflags(p, uap->fd) |= UF_EXCLOSE;
334 return (0);
335 }
336
337 /*
338 * Interpret high order word to find amount of data to be
339 * copied to/from the user's address space.
340 */
341 size = IOCPARM_LEN(com);
342 if (size > IOCPARM_MAX)
343 return (ENOTTY);
344 memp = NULL;
345 if (size > sizeof (stkbuf)) {
346 if ((memp = (caddr_t)kalloc(size)) == 0)
347 return(ENOMEM);
348 data = memp;
349 } else
350 data = stkbuf;
351 if (com&IOC_IN) {
352 if (size) {
353 error = copyin(uap->data, data, (u_int)size);
354 if (error) {
355 if (memp)
356 kfree(memp, size);
357 return (error);
358 }
359 } else
360 *(caddr_t *)data = uap->data;
361 } else if ((com&IOC_OUT) && size)
362 /*
363 * Zero the buffer so the user always
364 * gets back something deterministic.
365 */
366 bzero(data, size);
367 else if (com&IOC_VOID)
368 *(caddr_t *)data = uap->data;
369
370 switch (com) {
371
372 case FIONBIO:
373 if (tmp = *(int *)data)
374 fp->f_flag |= FNONBLOCK;
375 else
376 fp->f_flag &= ~FNONBLOCK;
377 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
378 break;
379
380 case FIOASYNC:
381 if (tmp = *(int *)data)
382 fp->f_flag |= FASYNC;
383 else
384 fp->f_flag &= ~FASYNC;
385 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
386 break;
387
388 case FIOSETOWN:
389 tmp = *(int *)data;
390 if (fp->f_type == DTYPE_SOCKET) {
391 ((struct socket *)fp->f_data)->so_pgid = tmp;
392 error = 0;
393 break;
394 }
395 if (tmp <= 0) {
396 tmp = -tmp;
397 } else {
398 struct proc *p1 = pfind(tmp);
399 if (p1 == 0) {
400 error = ESRCH;
401 break;
402 }
403 tmp = p1->p_pgrp->pg_id;
404 }
405 error = (*fp->f_ops->fo_ioctl)
406 (fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
407 break;
408
409 case FIOGETOWN:
410 if (fp->f_type == DTYPE_SOCKET) {
411 error = 0;
412 *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
413 break;
414 }
415 error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
416 *(int *)data = -*(int *)data;
417 break;
418
419 default:
420 error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
421 /*
422 * Copy any data to user, size was
423 * already set and checked above.
424 */
425 if (error == 0 && (com&IOC_OUT) && size)
426 error = copyout(data, uap->data, (u_int)size);
427 break;
428 }
429 if (memp)
430 kfree(memp, size);
431 return (error);
432 }
433
434
435 int selwait, nselcoll;
436 #define SEL_FIRSTPASS 1
437 #define SEL_SECONDPASS 2
438
439 /*
440 * Select system call.
441 */
442 struct select_args {
443 int nd;
444 u_int32_t *in;
445 u_int32_t *ou;
446 u_int32_t *ex;
447 struct timeval *tv;
448 };
449
450 extern int selcontinue(int error);
451 extern int selprocess(int error, int sel_pass);
452 static int selscan( struct proc *p, struct _select * sel,
453 int nfd, register_t *retval, int sel_pass);
454 static int selcount(struct proc *p, u_int32_t *ibits, u_int32_t *obits,
455 int nfd, int * count, int * nfcount);
456
457 select(p, uap, retval)
458 register struct proc *p;
459 register struct select_args *uap;
460 register_t *retval;
461 {
462 int s, error = 0, timo;
463 u_int ni, nw, size;
464 thread_act_t th_act;
465 struct uthread *uth;
466 struct _select *sel;
467 int needzerofill = 1;
468 int kfcount =0;
469 int nfcount = 0;
470 int count = 0;
471
472 th_act = current_act();
473 uth = get_bsdthread_info(th_act);
474 sel = &uth->uu_state.ss_select;
475 retval = (int *)get_bsduthreadrval(th_act);
476 *retval = 0;
477
478 if (uap->nd < 0) {
479 return (EINVAL);
480 }
481
482 if (uap->nd > p->p_fd->fd_nfiles)
483 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
484
485 nw = howmany(uap->nd, NFDBITS);
486 ni = nw * sizeof(fd_mask);
487
488 /*
489 * if this is the first select by the thread
490 * allocate the space for bits.
491 */
492 if (sel->nbytes == 0) {
493 sel->nbytes = 3 * ni;
494 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK);
495 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK);
496 bzero((caddr_t)sel->ibits, sel->nbytes);
497 bzero((caddr_t)sel->obits, sel->nbytes);
498 needzerofill = 0;
499 }
500
501 /*
502 * if the previously allocated space for the bits
503 * is smaller than what is requested. Reallocate.
504 */
505 if (sel->nbytes < (3 * ni)) {
506 sel->nbytes = (3 * ni);
507 FREE(sel->ibits, M_TEMP);
508 FREE(sel->obits, M_TEMP);
509 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK);
510 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK);
511 bzero((caddr_t)sel->ibits, sel->nbytes);
512 bzero((caddr_t)sel->obits, sel->nbytes);
513 needzerofill = 0;
514 }
515
516 if (needzerofill) {
517 bzero((caddr_t)sel->ibits, sel->nbytes);
518 bzero((caddr_t)sel->obits, sel->nbytes);
519 }
520
521 /*
522 * get the bits from the user address space
523 */
524 #define getbits(name, x) \
525 do { \
526 if (uap->name && (error = copyin((caddr_t)uap->name, \
527 (caddr_t)&sel->ibits[(x) * nw], ni))) \
528 goto continuation; \
529 } while (0)
530
531 getbits(in, 0);
532 getbits(ou, 1);
533 getbits(ex, 2);
534 #undef getbits
535
536 if (uap->tv) {
537 error = copyin((caddr_t)uap->tv, (caddr_t)&sel->atv,
538 sizeof (sel->atv));
539 if (error)
540 goto continuation;
541 if (itimerfix(&sel->atv)) {
542 error = EINVAL;
543 goto continuation;
544 }
545
546 timeradd(&sel->atv, &time, &sel->atv);
547 timo = hzto(&sel->atv);
548 } else
549 timo = 0;
550 sel->poll = timo;
551 sel->nfcount = 0;
552 if (error = selcount(p, sel->ibits, sel->obits, uap->nd, &count, &nfcount)) {
553 goto continuation;
554 }
555
556 sel->nfcount = nfcount;
557 sel->count = count;
558 size = SIZEOF_WAITQUEUE_SUB + (count * SIZEOF_WAITQUEUE_LINK);
559 if (sel->allocsize) {
560 if (uth->uu_wqsub == 0)
561 panic("select: wql memory smashed");
562 /* needed for the select now */
563 if (size > sel->allocsize) {
564 kfree(uth->uu_wqsub, sel->allocsize);
565 sel->allocsize = size;
566 uth->uu_wqsub = (wait_queue_sub_t)kalloc(sel->allocsize);
567 if (uth->uu_wqsub == (wait_queue_sub_t)NULL)
568 panic("failed to allocate memory for waitqueue\n");
569 sel->wql = (char *)uth->uu_wqsub + SIZEOF_WAITQUEUE_SUB;
570 }
571 } else {
572 sel->count = count;
573 sel->allocsize = size;
574 uth->uu_wqsub = (wait_queue_sub_t)kalloc(sel->allocsize);
575 if (uth->uu_wqsub == (wait_queue_sub_t)NULL)
576 panic("failed to allocate memory for waitqueue\n");
577 sel->wql = (char *)uth->uu_wqsub + SIZEOF_WAITQUEUE_SUB;
578 }
579 bzero(uth->uu_wqsub, size);
580 wait_queue_sub_init(uth->uu_wqsub, (SYNC_POLICY_FIFO | SYNC_POLICY_PREPOST));
581
582 continuation:
583 selprocess(error, SEL_FIRSTPASS);
584 }
585
586 int
587 selcontinue(int error)
588 {
589 selprocess(error, SEL_SECONDPASS);
590 }
591
592 int
593 selprocess(error, sel_pass)
594 {
595 int s, ncoll, timo;
596 u_int ni, nw;
597 thread_act_t th_act;
598 struct uthread *uth;
599 struct proc *p;
600 struct select_args *uap;
601 int *retval;
602 struct _select *sel;
603 int unwind = 1;
604 int prepost =0;
605 int somewakeup = 0;
606 int doretry = 0;
607
608 p = current_proc();
609 th_act = current_act();
610 uap = (struct select_args *)get_bsduthreadarg(th_act);
611 retval = (int *)get_bsduthreadrval(th_act);
612 uth = get_bsdthread_info(th_act);
613 sel = &uth->uu_state.ss_select;
614
615 /* if it is first pass wait queue is not setup yet */
616 if ((error != 0) && (sel_pass == SEL_FIRSTPASS))
617 unwind = 0;
618 if (sel->count == 0)
619 unwind = 0;
620 retry:
621 if (error != 0) {
622 goto done;
623 }
624
625 ncoll = nselcoll;
626 p->p_flag |= P_SELECT;
627 /* skip scans if the select is just for timeouts */
628 if (sel->count) {
629 if (sel_pass == SEL_FIRSTPASS)
630 wait_queue_sub_clearrefs(uth->uu_wqsub);
631
632 error = selscan(p, sel, uap->nd, retval, sel_pass);
633 if (error || *retval) {
634 goto done;
635 }
636 if (prepost) {
637 /* if the select of log, then we canwakeup and discover some one
638 * else already read the data; go toselct again if time permits
639 */
640 prepost = 0;
641 doretry = 1;
642 }
643 if (somewakeup) {
644 somewakeup = 0;
645 doretry = 1;
646 }
647 }
648
649 /* this should be timercmp(&time, &atv, >=) */
650 if (uap->tv && (time.tv_sec > sel->atv.tv_sec ||
651 time.tv_sec == sel->atv.tv_sec && time.tv_usec >= sel->atv.tv_usec)) {
652 goto done;
653 }
654
655 if (doretry) {
656 /* cleanup obits and try again */
657 doretry = 0;
658 sel_pass = SEL_FIRSTPASS;
659 goto retry;
660 }
661
662 /*
663 * To effect a poll, the timeout argument should be
664 * non-nil, pointing to a zero-valued timeval structure.
665 */
666 timo = sel->poll;
667
668 if (uap->tv && (timo == 0)) {
669 goto done;
670 }
671
672 /* No spurious wakeups due to colls,no need to check for them */
673 if ((sel_pass == SEL_SECONDPASS) || ((p->p_flag & P_SELECT) == 0)) {
674 sel_pass = SEL_FIRSTPASS;
675 goto retry;
676 }
677
678 p->p_flag &= ~P_SELECT;
679
680 /* if the select is just for timeout skip check */
681 if (sel->count &&(sel_pass == SEL_SECONDPASS))
682 panic("selprocess: 2nd pass assertwaiting");
683
684 /* Wait Queue Subordinate has waitqueue as first element */
685 if (wait_queue_assert_wait(uth->uu_wqsub, &selwait, THREAD_ABORTSAFE)) {
686 /* If it is true then there are no preposted events */
687 error = tsleep1((caddr_t)&selwait, PSOCK | PCATCH, "select", timo, selcontinue);
688 } else {
689 prepost = 1;
690 error = 0;
691 }
692
693 sel_pass = SEL_SECONDPASS;
694 if (error == 0) {
695 if (!prepost)
696 somewakeup =1;
697 goto retry;
698 }
699 done:
700 if (unwind)
701 wait_subqueue_unlink_all(uth->uu_wqsub);
702 p->p_flag &= ~P_SELECT;
703 /* select is not restarted after signals... */
704 if (error == ERESTART)
705 error = EINTR;
706 if (error == EWOULDBLOCK)
707 error = 0;
708 nw = howmany(uap->nd, NFDBITS);
709 ni = nw * sizeof(fd_mask);
710
711 #define putbits(name, x) \
712 do { \
713 if (uap->name && (error2 = copyout((caddr_t)&sel->obits[(x) * nw], \
714 (caddr_t)uap->name, ni))) \
715 error = error2; \
716 } while (0)
717
718 if (error == 0) {
719 int error2;
720
721 putbits(in, 0);
722 putbits(ou, 1);
723 putbits(ex, 2);
724 #undef putbits
725 }
726
727 #if defined (__i386__)
728 return(error);
729 #else
730 unix_syscall_return(error);
731 #endif
732 }
733
734 static int
735 selscan(p, sel, nfd, retval, sel_pass)
736 struct proc *p;
737 struct _select *sel;
738 int nfd;
739 register_t *retval;
740 int sel_pass;
741 {
742 register struct filedesc *fdp = p->p_fd;
743 register int msk, i, j, fd;
744 register u_int32_t bits;
745 struct file *fp;
746 int n = 0;
747 int nc = 0;
748 static int flag[3] = { FREAD, FWRITE, 0 };
749 u_int32_t *iptr, *optr;
750 u_int nw;
751 u_int32_t *ibits, *obits;
752 char * wql;
753 int nfunnel = 0;
754 int count, nfcount;
755 char * wql_ptr;
756
757 /*
758 * Problems when reboot; due to MacOSX signal probs
759 * in Beaker1C ; verify that the p->p_fd is valid
760 */
761 if (fdp == NULL) {
762 *retval=0;
763 return(EIO);
764 }
765
766 ibits = sel->ibits;
767 obits = sel->obits;
768 wql = sel->wql;
769
770 count = sel->count;
771 nfcount = sel->nfcount;
772
773 if (nfcount > count)
774 panic("selcount count<nfcount");
775
776 nw = howmany(nfd, NFDBITS);
777
778 nc = 0;
779 if ( nfcount < count) {
780 /* some or all in kernel funnel */
781 for (msk = 0; msk < 3; msk++) {
782 iptr = (u_int32_t *)&ibits[msk * nw];
783 optr = (u_int32_t *)&obits[msk * nw];
784 for (i = 0; i < nfd; i += NFDBITS) {
785 bits = iptr[i/NFDBITS];
786 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
787 bits &= ~(1 << j);
788 fp = fdp->fd_ofiles[fd];
789 if (fp == NULL ||
790 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
791 return(EBADF);
792 }
793 if (sel_pass == SEL_SECONDPASS)
794 wql_ptr = (char *)0;
795 else
796 wql_ptr = (wql+ nc * SIZEOF_WAITQUEUE_LINK);
797 if (fp->f_ops && (fp->f_type != DTYPE_SOCKET)
798 && (*fp->f_ops->fo_select)(fp, flag[msk], wql_ptr, p)) {
799 optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
800 n++;
801 }
802 nc++;
803 }
804 }
805 }
806 }
807
808 if (nfcount) {
809 /* socket file descriptors for scan */
810 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
811
812 nc = 0;
813 for (msk = 0; msk < 3; msk++) {
814 iptr = (u_int32_t *)&ibits[msk * nw];
815 optr = (u_int32_t *)&obits[msk * nw];
816 for (i = 0; i < nfd; i += NFDBITS) {
817 bits = iptr[i/NFDBITS];
818 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
819 bits &= ~(1 << j);
820 fp = fdp->fd_ofiles[fd];
821 if (fp == NULL ||
822 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
823 return(EBADF);
824 }
825 if (sel_pass == SEL_SECONDPASS)
826 wql_ptr = (char *)0;
827 else
828 wql_ptr = (wql+ nc * SIZEOF_WAITQUEUE_LINK);
829 if (fp->f_ops && (fp->f_type == DTYPE_SOCKET) &&
830 (*fp->f_ops->fo_select)(fp, flag[msk], wql_ptr, p)) {
831 optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
832 n++;
833 }
834 nc++;
835 }
836 }
837 }
838 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
839 }
840
841 *retval = n;
842 return (0);
843 }
844
845 /*ARGSUSED*/
846 seltrue(dev, flag, p)
847 dev_t dev;
848 int flag;
849 struct proc *p;
850 {
851
852 return (1);
853 }
854
855 static int
856 selcount(p, ibits, obits, nfd, count, nfcount)
857 struct proc *p;
858 u_int32_t *ibits, *obits;
859 int nfd;
860 int *count;
861 int *nfcount;
862 {
863 register struct filedesc *fdp = p->p_fd;
864 register int msk, i, j, fd;
865 register u_int32_t bits;
866 struct file *fp;
867 int n = 0;
868 int nc = 0;
869 int nfc = 0;
870 static int flag[3] = { FREAD, FWRITE, 0 };
871 u_int32_t *iptr, *fptr, *fbits;
872 u_int nw;
873
874 /*
875 * Problems when reboot; due to MacOSX signal probs
876 * in Beaker1C ; verify that the p->p_fd is valid
877 */
878 if (fdp == NULL) {
879 *count=0;
880 *nfcount=0;
881 return(EIO);
882 }
883
884 nw = howmany(nfd, NFDBITS);
885
886
887 for (msk = 0; msk < 3; msk++) {
888 iptr = (u_int32_t *)&ibits[msk * nw];
889 for (i = 0; i < nfd; i += NFDBITS) {
890 bits = iptr[i/NFDBITS];
891 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
892 bits &= ~(1 << j);
893 fp = fdp->fd_ofiles[fd];
894 if (fp == NULL ||
895 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
896 *count=0;
897 *nfcount=0;
898 return(EBADF);
899 }
900 if (fp->f_type == DTYPE_SOCKET)
901 nfc++;
902 n++;
903 }
904 }
905 }
906 *count = n;
907 *nfcount = nfc;
908 return (0);
909 }
910
911 /*
912 * Record a select request.
913 */
914 void
915 selrecord(selector, sip, p_wql)
916 struct proc *selector;
917 struct selinfo *sip;
918 void * p_wql;
919 {
920 thread_act_t cur_act = current_act();
921 struct uthread * ut = get_bsdthread_info(cur_act);
922
923 /* need to look at collisions */
924
925 if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
926 return;
927 }
928
929 /*do not record if this is second pass of select */
930 if((p_wql == (void *)0)) {
931 return;
932 }
933
934 if ((sip->si_flags & SI_INITED) == 0) {
935 wait_queue_init(&sip->wait_queue, SYNC_POLICY_FIFO);
936 sip->si_flags |= SI_INITED;
937 sip->si_flags &= ~SI_CLEAR;
938 }
939
940 if (sip->si_flags & SI_RECORDED) {
941 sip->si_flags |= SI_COLL;
942 } else
943 sip->si_flags &= ~SI_COLL;
944
945 sip->si_flags |= SI_RECORDED;
946 if (!wait_queue_member(&sip->wait_queue, ut->uu_wqsub))
947 wait_queue_link_noalloc(&sip->wait_queue, ut->uu_wqsub, (wait_queue_link_t)p_wql);
948
949 return;
950 }
951
952 void
953 selwakeup(sip)
954 register struct selinfo *sip;
955 {
956
957 if ((sip->si_flags & SI_INITED) == 0) {
958 return;
959 }
960
961 if (sip->si_flags & SI_COLL) {
962 nselcoll++;
963 sip->si_flags &= ~SI_COLL;
964 #if 0
965 /* will not support */
966 //wakeup((caddr_t)&selwait);
967 #endif
968 }
969
970 if (sip->si_flags & SI_RECORDED) {
971 wait_queue_wakeup_all(&sip->wait_queue, &selwait, THREAD_AWAKENED);
972 sip->si_flags &= ~SI_RECORDED;
973 }
974
975 }
976
977 void
978 selthreadclear(sip)
979 register struct selinfo *sip;
980 {
981
982 if ((sip->si_flags & SI_INITED) == 0) {
983 return;
984 }
985 if (sip->si_flags & SI_RECORDED) {
986 selwakeup(sip);
987 sip->si_flags &= ~(SI_RECORDED | SI_COLL);
988 }
989 sip->si_flags |= SI_CLEAR;
990 wait_queue_unlinkall_nofree(&sip->wait_queue);
991 }
992
993
994 extern struct eventqelt *evprocdeque(struct proc *p, struct eventqelt *eqp);
995
996 /*
997 * called upon socket close. deque and free all events for
998 * the socket
999 */
1000 evsofree(struct socket *sp)
1001 {
1002 struct eventqelt *eqp, *next;
1003
1004 if (sp == NULL) return;
1005
1006 for (eqp = sp->so_evlist.tqh_first; eqp != NULL; eqp = next) {
1007 next = eqp->ee_slist.tqe_next;
1008 evprocdeque(eqp->ee_proc, eqp); // remove from proc q if there
1009 TAILQ_REMOVE(&sp->so_evlist, eqp, ee_slist); // remove from socket q
1010 FREE(eqp, M_TEMP);
1011 }
1012 }
1013
1014
1015 #define DBG_EVENT 0x10
1016
1017 #define DBG_POST 0x10
1018 #define DBG_WATCH 0x11
1019 #define DBG_WAIT 0x12
1020 #define DBG_MOD 0x13
1021 #define DBG_EWAKEUP 0x14
1022 #define DBG_ENQUEUE 0x15
1023 #define DBG_DEQUEUE 0x16
1024
1025 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1026 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1027 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1028 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1029 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1030 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1031 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1032
1033
1034 /*
1035 * enque this event if it's not already queued. wakeup
1036 the proc if we do queue this event to it.
1037 */
1038 evprocenque(struct eventqelt *eqp)
1039 {
1040 struct proc *p;
1041
1042 assert(eqp);
1043 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, eqp, eqp->ee_flags, eqp->ee_eventmask,0,0);
1044 if (eqp->ee_flags & EV_QUEUED) {
1045 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1046 return;
1047 }
1048 eqp->ee_flags |= EV_QUEUED;
1049 eqp->ee_eventmask = 0; // disarm
1050 p = eqp->ee_proc;
1051 TAILQ_INSERT_TAIL(&p->p_evlist, eqp, ee_plist);
1052 KERNEL_DEBUG(DBG_MISC_EWAKEUP,0,0,0,eqp,0);
1053 wakeup(&p->p_evlist);
1054 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1055 }
1056
1057 /*
1058 * given either a sockbuf or a socket run down the
1059 * event list and queue ready events found
1060 */
1061 postevent(struct socket *sp, struct sockbuf *sb, int event)
1062 {
1063 int mask;
1064 struct eventqelt *evq;
1065 register struct tcpcb *tp;
1066
1067 if (sb) sp = sb->sb_so;
1068 if (!sp || sp->so_evlist.tqh_first == NULL) return;
1069
1070 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,0,0);
1071
1072 for (evq = sp->so_evlist.tqh_first;
1073 evq != NULL; evq = evq->ee_slist.tqe_next) {
1074
1075 mask = 0;
1076
1077 /* ready for reading:
1078 - byte cnt >= receive low water mark
1079 - read-half of conn closed
1080 - conn pending for listening sock
1081 - socket error pending
1082
1083 ready for writing
1084 - byte cnt avail >= send low water mark
1085 - write half of conn closed
1086 - socket error pending
1087 - non-blocking conn completed successfully
1088
1089 exception pending
1090 - out of band data
1091 - sock at out of band mark
1092
1093 */
1094 switch (event & EV_DMASK) {
1095
1096 case EV_RWBYTES:
1097 case EV_OOB:
1098 case EV_RWBYTES|EV_OOB:
1099 if (event & EV_OOB) {
1100 if ((evq->ee_eventmask & EV_EX)) {
1101 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK))) {
1102 mask |= EV_EX|EV_OOB;
1103 }
1104 }
1105 }
1106 if (event & EV_RWBYTES) {
1107 if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
1108 if ((sp->so_type == SOCK_STREAM) && (sp->so_error == ECONNREFUSED) ||
1109 (sp->so_error == ECONNRESET)) {
1110 if ((sp->so_pcb == 0) ||
1111 !(tp = sototcpcb(sp)) ||
1112 (tp->t_state == TCPS_CLOSED)) {
1113 mask |= EV_RE|EV_RESET;
1114 break;
1115 }
1116 }
1117 if (sp->so_state & SS_CANTRCVMORE) {
1118 mask |= EV_RE|EV_FIN;
1119 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
1120 break;
1121 }
1122 mask |= EV_RE;
1123 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
1124 }
1125
1126 if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
1127 if ((sp->so_type == SOCK_STREAM) &&(sp->so_error == ECONNREFUSED) ||
1128 (sp->so_error == ECONNRESET)) {
1129 if ((sp->so_pcb == 0) ||
1130 !(tp = sototcpcb(sp)) ||
1131 (tp->t_state == TCPS_CLOSED)) {
1132 mask |= EV_WR|EV_RESET;
1133 break;
1134 }
1135 }
1136 mask |= EV_WR;
1137 evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
1138 }
1139 }
1140 break;
1141
1142 case EV_RCONN:
1143 if ((evq->ee_eventmask & EV_RE)) {
1144 evq->ee_req.er_rcnt = sp->so_qlen + 1; // incl this one
1145 mask |= EV_RE|EV_RCONN;
1146 }
1147 break;
1148
1149 case EV_WCONN:
1150 if ((evq->ee_eventmask & EV_WR)) {
1151 mask |= EV_WR|EV_WCONN;
1152 }
1153 break;
1154
1155 case EV_RCLOSED:
1156 if ((evq->ee_eventmask & EV_RE)) {
1157 mask |= EV_RE|EV_RCLOSED;
1158 }
1159 break;
1160
1161 case EV_WCLOSED:
1162 if ((evq->ee_eventmask & EV_WR)) {
1163 mask |= EV_WR|EV_WCLOSED;
1164 }
1165 break;
1166
1167 case EV_FIN:
1168 if (evq->ee_eventmask & EV_RE) {
1169 mask |= EV_RE|EV_FIN;
1170 }
1171 break;
1172
1173 case EV_RESET:
1174 case EV_TIMEOUT:
1175 if (evq->ee_eventmask & EV_RE) {
1176 mask |= EV_RE | event;
1177 }
1178 if (evq->ee_eventmask & EV_WR) {
1179 mask |= EV_WR | event;
1180 }
1181 break;
1182
1183 default:
1184 return;
1185 } /* switch */
1186
1187 if (mask) {
1188 evq->ee_req.er_eventbits |= mask;
1189 KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask,0,0);
1190 evprocenque(evq);
1191 }
1192 }
1193 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,0,0);
1194 }
1195
1196 /*
1197 * remove and return the first event (eqp=NULL) or a specific
1198 * event, or return NULL if no events found
1199 */
1200 struct eventqelt *
1201 evprocdeque(struct proc *p, struct eventqelt *eqp)
1202 {
1203
1204 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_START,p,eqp,0,0,0);
1205
1206 if (eqp && ((eqp->ee_flags & EV_QUEUED) == NULL)) {
1207 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,0,0,0,0,0);
1208 return(NULL);
1209 }
1210 if (p->p_evlist.tqh_first == NULL) {
1211 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,0,0,0,0,0);
1212 return(NULL);
1213 }
1214 if (eqp == NULL) { // remove first
1215 eqp = p->p_evlist.tqh_first;
1216 }
1217 TAILQ_REMOVE(&p->p_evlist, eqp, ee_plist);
1218 eqp->ee_flags &= ~EV_QUEUED;
1219 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,eqp,0,0,0,0);
1220 return(eqp);
1221 }
1222
1223 struct evwatch_args {
1224 struct eventreq *u_req;
1225 int u_eventmask;
1226 };
1227
1228
1229 /*
1230 * watchevent system call. user passes us an event to watch
1231 * for. we malloc an event object, initialize it, and queue
1232 * it to the open socket. when the event occurs, postevent()
1233 * will enque it back to our proc where we can retrieve it
1234 * via waitevent().
1235 *
1236 * should this prevent duplicate events on same socket?
1237 */
1238 int
1239 watchevent(p, uap, retval)
1240 struct proc *p;
1241 struct evwatch_args *uap;
1242 register_t *retval;
1243 {
1244 struct eventqelt *eqp = (struct eventqelt *)0;
1245 struct eventqelt *np;
1246 struct eventreq *erp;
1247 struct file *fp;
1248 struct socket *sp;
1249 int error;
1250
1251 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
1252
1253 // get a qelt and fill with users req
1254 MALLOC(eqp, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
1255 if (!eqp) panic("can't MALLOC eqp");
1256 erp = &eqp->ee_req;
1257 // get users request pkt
1258 if (error = copyin((caddr_t)uap->u_req, (caddr_t)erp,
1259 sizeof(struct eventreq))) {
1260 FREE(eqp, M_TEMP);
1261 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
1262 return(error);
1263 }
1264 KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,eqp,0,0);
1265 // validate, freeing qelt if errors
1266 error = 0;
1267 if (erp->er_type != EV_FD) {
1268 error = EINVAL;
1269 } else if (erp->er_handle < 0) {
1270 error = EBADF;
1271 } else if (erp->er_handle > p->p_fd->fd_nfiles) {
1272 error = EBADF;
1273 } else if ((fp = *fdfile(p, erp->er_handle)) == NULL) {
1274 error = EBADF;
1275 } else if (fp->f_type != DTYPE_SOCKET) {
1276 error = EINVAL;
1277 }
1278 if (error) {
1279 FREE(eqp,M_TEMP);
1280 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
1281 return(error);
1282 }
1283
1284 erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
1285 eqp->ee_proc = p;
1286 eqp->ee_eventmask = uap->u_eventmask & EV_MASK;
1287 eqp->ee_flags = 0;
1288
1289 sp = (struct socket *)fp->f_data;
1290 assert(sp != NULL);
1291
1292 // only allow one watch per file per proc
1293 for (np = sp->so_evlist.tqh_first; np != NULL; np = np->ee_slist.tqe_next) {
1294 if (np->ee_proc == p) {
1295 FREE(eqp,M_TEMP);
1296 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
1297 return(EINVAL);
1298 }
1299 }
1300
1301 TAILQ_INSERT_TAIL(&sp->so_evlist, eqp, ee_slist);
1302 postevent(sp, 0, EV_RWBYTES); // catch existing events
1303 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
1304 return(0);
1305 }
1306
1307 struct evwait_args {
1308 struct eventreq *u_req;
1309 struct timeval *tv;
1310 };
1311
1312 /*
1313 * waitevent system call.
1314 * grabs the next waiting event for this proc and returns
1315 * it. if no events, user can request to sleep with timeout
1316 * or poll mode (tv=NULL);
1317 */
1318 int
1319 waitevent(p, uap, retval)
1320 struct proc *p;
1321 struct evwait_args *uap;
1322 register_t *retval;
1323 {
1324 int error = 0;
1325 struct eventqelt *eqp;
1326 int timo;
1327 struct timeval atv;
1328 int s;
1329
1330 if (uap->tv) {
1331 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
1332 sizeof (atv));
1333 if (error)
1334 return(error);
1335 if (itimerfix(&atv)) {
1336 error = EINVAL;
1337 return(error);
1338 }
1339 s = splhigh();
1340 timeradd(&atv, &time, &atv);
1341 timo = hzto(&atv);
1342 splx(s);
1343 } else
1344 timo = 0;
1345
1346 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
1347
1348 retry:
1349 s = splhigh();
1350 if ((eqp = evprocdeque(p,NULL)) != NULL) {
1351 splx(s);
1352 error = copyout((caddr_t)&eqp->ee_req, (caddr_t)uap->u_req,
1353 sizeof(struct eventreq));
1354 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
1355 eqp->ee_req.er_handle,eqp->ee_req.er_eventbits,eqp,0);
1356 return(error);
1357 } else {
1358 if (uap->tv && (timo == 0)) {
1359 splx(s);
1360 *retval = 1; // poll failed
1361 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
1362 return(error);
1363 }
1364
1365 KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
1366 error = tsleep(&p->p_evlist, PSOCK | PCATCH, "waitevent", timo);
1367 KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
1368 splx(s);
1369 if (error == 0)
1370 goto retry;
1371 if (error == ERESTART)
1372 error = EINTR;
1373 if (error == EWOULDBLOCK) {
1374 *retval = 1;
1375 error = 0;
1376 }
1377 }
1378 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
1379 return(error);
1380 }
1381
1382 struct modwatch_args {
1383 struct eventreq *u_req;
1384 int u_eventmask;
1385 };
1386
1387 /*
1388 * modwatch system call. user passes in event to modify.
1389 * if we find it we reset the event bits and que/deque event
1390 * it needed.
1391 */
1392 int
1393 modwatch(p, uap, retval)
1394 struct proc *p;
1395 struct modwatch_args *uap;
1396 register_t *retval;
1397 {
1398 struct eventreq er;
1399 struct eventreq *erp = &er;
1400 struct eventqelt *evq;
1401 int error;
1402 struct file *fp;
1403 struct socket *sp;
1404 int flag;
1405
1406 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
1407
1408 // get users request pkt
1409 if (error = copyin((caddr_t)uap->u_req, (caddr_t)erp,
1410 sizeof(struct eventreq))) return(error);
1411
1412 if (erp->er_type != EV_FD) return(EINVAL);
1413 if (erp->er_handle < 0) return(EBADF);
1414 if (erp->er_handle > p->p_fd->fd_nfiles) return(EBADF);
1415 if ((fp = *fdfile(p, erp->er_handle)) == NULL)
1416 return(EBADF);
1417 if (fp->f_type != DTYPE_SOCKET) return(EINVAL); // for now must be sock
1418 sp = (struct socket *)fp->f_data;
1419 assert(sp != NULL);
1420
1421
1422 // locate event if possible
1423 for (evq = sp->so_evlist.tqh_first;
1424 evq != NULL; evq = evq->ee_slist.tqe_next) {
1425 if (evq->ee_proc == p) break;
1426 }
1427
1428 if (evq == NULL) {
1429 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
1430 return(EINVAL);
1431 }
1432 KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
1433
1434 if (uap->u_eventmask == EV_RM) {
1435 evprocdeque(p, evq);
1436 TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist);
1437 FREE(evq, M_TEMP);
1438 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
1439 return(0);
1440 }
1441
1442 switch (uap->u_eventmask & EV_MASK) {
1443
1444 case 0:
1445 flag = 0;
1446 break;
1447
1448 case EV_RE:
1449 case EV_WR:
1450 case EV_RE|EV_WR:
1451 flag = EV_RWBYTES;
1452 break;
1453
1454 case EV_EX:
1455 flag = EV_OOB;
1456 break;
1457
1458 case EV_EX|EV_RE:
1459 case EV_EX|EV_WR:
1460 case EV_EX|EV_RE|EV_WR:
1461 flag = EV_OOB|EV_RWBYTES;
1462 break;
1463
1464 default:
1465 return(EINVAL);
1466 }
1467
1468 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
1469 evprocdeque(p, evq);
1470 evq->ee_req.er_eventbits = 0;
1471 postevent(sp, 0, flag);
1472 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,sp,flag,0);
1473 return(0);
1474 }