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