]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_generic.c
b8c6e975e59f2e6a92cca0afe10967e6b38c726f
[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 if (selthread) {
724 /* thread_deallocate(selthread); */
725 act_deallocate(getact_thread(selthread));
726 }
727 /* do I need act reference ??? */
728 /* thread_reference(sip->si_thread); */
729 act_reference(getact_thread(sip->si_thread));
730 }
731
732 return;
733 }
734
735 void
736 selwakeup(sip)
737 register struct selinfo *sip;
738 {
739 register thread_t the_thread = (thread_t)sip->si_thread;
740 int oldpri;
741 struct proc *p;
742 thread_act_t th_act;
743
744 if (the_thread == 0)
745 return;
746
747 if (sip->si_flags & SI_COLL) {
748 nselcoll++;
749 sip->si_flags &= ~SI_COLL;
750 wakeup((caddr_t)&selwait);
751 }
752
753 oldpri = splhigh();
754
755 th_act = (thread_act_t)getact_thread(the_thread);
756
757 if (is_thread_active(the_thread)) {
758 if (get_thread_waitevent(the_thread) == &selwait)
759 clear_wait(the_thread, THREAD_AWAKENED);
760 if (p = current_proc())
761 p->p_flag &= ~P_SELECT;
762 }
763
764 /* th_act = (thread_act_t)getact_thread(the_thread); */
765
766 act_deallocate(th_act);
767
768 sip->si_thread = 0;
769
770 splx(oldpri);
771 }
772
773 void
774 selthreadclear(sip)
775 register struct selinfo *sip;
776 {
777 thread_act_t th_act;
778
779 if (sip->si_thread) {
780 th_act = (thread_act_t)getact_thread(sip->si_thread);
781 act_deallocate(th_act);
782 }
783 }
784
785
786 extern struct eventqelt *evprocdeque(struct proc *p, struct eventqelt *eqp);
787
788 /*
789 * called upon socket close. deque and free all events for
790 * the socket
791 */
792 evsofree(struct socket *sp)
793 {
794 struct eventqelt *eqp, *next;
795
796 if (sp == NULL) return;
797
798 for (eqp = sp->so_evlist.tqh_first; eqp != NULL; eqp = next) {
799 next = eqp->ee_slist.tqe_next;
800 evprocdeque(eqp->ee_proc, eqp); // remove from proc q if there
801 TAILQ_REMOVE(&sp->so_evlist, eqp, ee_slist); // remove from socket q
802 FREE(eqp, M_TEMP);
803 }
804 }
805
806
807 #define DBG_EVENT 0x10
808
809 #define DBG_POST 0x10
810 #define DBG_WATCH 0x11
811 #define DBG_WAIT 0x12
812 #define DBG_MOD 0x13
813 #define DBG_EWAKEUP 0x14
814 #define DBG_ENQUEUE 0x15
815 #define DBG_DEQUEUE 0x16
816
817 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
818 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
819 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
820 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
821 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
822 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
823 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
824
825
826 /*
827 * enque this event if it's not already queued. wakeup
828 the proc if we do queue this event to it.
829 */
830 evprocenque(struct eventqelt *eqp)
831 {
832 struct proc *p;
833
834 assert(eqp);
835 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, eqp, eqp->ee_flags, eqp->ee_eventmask,0,0);
836 if (eqp->ee_flags & EV_QUEUED) {
837 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
838 return;
839 }
840 eqp->ee_flags |= EV_QUEUED;
841 eqp->ee_eventmask = 0; // disarm
842 p = eqp->ee_proc;
843 TAILQ_INSERT_TAIL(&p->p_evlist, eqp, ee_plist);
844 KERNEL_DEBUG(DBG_MISC_EWAKEUP,0,0,0,eqp,0);
845 wakeup(&p->p_evlist);
846 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
847 }
848
849 /*
850 * given either a sockbuf or a socket run down the
851 * event list and queue ready events found
852 */
853 postevent(struct socket *sp, struct sockbuf *sb, int event)
854 {
855 int mask;
856 struct eventqelt *evq;
857 register struct tcpcb *tp;
858
859 if (sb) sp = sb->sb_so;
860 if (!sp || sp->so_evlist.tqh_first == NULL) return;
861
862 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,0,0);
863
864 for (evq = sp->so_evlist.tqh_first;
865 evq != NULL; evq = evq->ee_slist.tqe_next) {
866
867 mask = 0;
868
869 /* ready for reading:
870 - byte cnt >= receive low water mark
871 - read-half of conn closed
872 - conn pending for listening sock
873 - socket error pending
874
875 ready for writing
876 - byte cnt avail >= send low water mark
877 - write half of conn closed
878 - socket error pending
879 - non-blocking conn completed successfully
880
881 exception pending
882 - out of band data
883 - sock at out of band mark
884
885 */
886 switch (event & EV_DMASK) {
887
888 case EV_RWBYTES:
889 case EV_OOB:
890 case EV_RWBYTES|EV_OOB:
891 if (event & EV_OOB) {
892 if ((evq->ee_eventmask & EV_EX)) {
893 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK))) {
894 mask |= EV_EX|EV_OOB;
895 }
896 }
897 }
898 if (event & EV_RWBYTES) {
899 if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
900 if ((sp->so_type == SOCK_STREAM) && (sp->so_error == ECONNREFUSED) ||
901 (sp->so_error == ECONNRESET)) {
902 if ((sp->so_pcb == 0) ||
903 !(tp = sototcpcb(sp)) ||
904 (tp->t_state == TCPS_CLOSED)) {
905 mask |= EV_RE|EV_RESET;
906 break;
907 }
908 }
909 if (sp->so_state & SS_CANTRCVMORE) {
910 mask |= EV_RE|EV_FIN;
911 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
912 break;
913 }
914 mask |= EV_RE;
915 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
916 }
917
918 if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
919 if ((sp->so_type == SOCK_STREAM) &&(sp->so_error == ECONNREFUSED) ||
920 (sp->so_error == ECONNRESET)) {
921 if ((sp->so_pcb == 0) ||
922 !(tp = sototcpcb(sp)) ||
923 (tp->t_state == TCPS_CLOSED)) {
924 mask |= EV_WR|EV_RESET;
925 break;
926 }
927 }
928 mask |= EV_WR;
929 evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
930 }
931 }
932 break;
933
934 case EV_RCONN:
935 if ((evq->ee_eventmask & EV_RE)) {
936 evq->ee_req.er_rcnt = sp->so_qlen + 1; // incl this one
937 mask |= EV_RE|EV_RCONN;
938 }
939 break;
940
941 case EV_WCONN:
942 if ((evq->ee_eventmask & EV_WR)) {
943 mask |= EV_WR|EV_WCONN;
944 }
945 break;
946
947 case EV_RCLOSED:
948 if ((evq->ee_eventmask & EV_RE)) {
949 mask |= EV_RE|EV_RCLOSED;
950 }
951 break;
952
953 case EV_WCLOSED:
954 if ((evq->ee_eventmask & EV_WR)) {
955 mask |= EV_WR|EV_WCLOSED;
956 }
957 break;
958
959 case EV_FIN:
960 if (evq->ee_eventmask & EV_RE) {
961 mask |= EV_RE|EV_FIN;
962 }
963 break;
964
965 case EV_RESET:
966 case EV_TIMEOUT:
967 if (evq->ee_eventmask & EV_RE) {
968 mask |= EV_RE | event;
969 }
970 if (evq->ee_eventmask & EV_WR) {
971 mask |= EV_WR | event;
972 }
973 break;
974
975 default:
976 return;
977 } /* switch */
978
979 if (mask) {
980 evq->ee_req.er_eventbits |= mask;
981 KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask,0,0);
982 evprocenque(evq);
983 }
984 }
985 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,0,0);
986 }
987
988 /*
989 * remove and return the first event (eqp=NULL) or a specific
990 * event, or return NULL if no events found
991 */
992 struct eventqelt *
993 evprocdeque(struct proc *p, struct eventqelt *eqp)
994 {
995
996 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_START,p,eqp,0,0,0);
997
998 if (eqp && ((eqp->ee_flags & EV_QUEUED) == NULL)) {
999 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,0,0,0,0,0);
1000 return(NULL);
1001 }
1002 if (p->p_evlist.tqh_first == NULL) {
1003 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,0,0,0,0,0);
1004 return(NULL);
1005 }
1006 if (eqp == NULL) { // remove first
1007 eqp = p->p_evlist.tqh_first;
1008 }
1009 TAILQ_REMOVE(&p->p_evlist, eqp, ee_plist);
1010 eqp->ee_flags &= ~EV_QUEUED;
1011 KERNEL_DEBUG(DBG_MISC_DEQUEUE|DBG_FUNC_END,eqp,0,0,0,0);
1012 return(eqp);
1013 }
1014
1015 struct evwatch_args {
1016 struct eventreq *u_req;
1017 int u_eventmask;
1018 };
1019
1020
1021 /*
1022 * watchevent system call. user passes us an event to watch
1023 * for. we malloc an event object, initialize it, and queue
1024 * it to the open socket. when the event occurs, postevent()
1025 * will enque it back to our proc where we can retrieve it
1026 * via waitevent().
1027 *
1028 * should this prevent duplicate events on same socket?
1029 */
1030 int
1031 watchevent(p, uap, retval)
1032 struct proc *p;
1033 struct evwatch_args *uap;
1034 register_t *retval;
1035 {
1036 struct eventqelt *eqp = (struct eventqelt *)0;
1037 struct eventqelt *np;
1038 struct eventreq *erp;
1039 struct file *fp;
1040 struct socket *sp;
1041 int error;
1042
1043 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
1044
1045 // get a qelt and fill with users req
1046 MALLOC(eqp, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
1047 if (!eqp) panic("can't MALLOC eqp");
1048 erp = &eqp->ee_req;
1049 // get users request pkt
1050 if (error = copyin((caddr_t)uap->u_req, (caddr_t)erp,
1051 sizeof(struct eventreq))) {
1052 FREE(eqp, M_TEMP);
1053 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
1054 return(error);
1055 }
1056 KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,eqp,0,0);
1057 // validate, freeing qelt if errors
1058 error = 0;
1059 if (erp->er_type != EV_FD) {
1060 error = EINVAL;
1061 } else if (erp->er_handle < 0) {
1062 error = EBADF;
1063 } else if (erp->er_handle > p->p_fd->fd_nfiles) {
1064 error = EBADF;
1065 } else if ((fp = *fdfile(p, erp->er_handle)) == NULL) {
1066 error = EBADF;
1067 } else if (fp->f_type != DTYPE_SOCKET) {
1068 error = EINVAL;
1069 }
1070 if (error) {
1071 FREE(eqp,M_TEMP);
1072 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
1073 return(error);
1074 }
1075
1076 erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
1077 eqp->ee_proc = p;
1078 eqp->ee_eventmask = uap->u_eventmask & EV_MASK;
1079 eqp->ee_flags = 0;
1080
1081 sp = (struct socket *)fp->f_data;
1082 assert(sp != NULL);
1083
1084 // only allow one watch per file per proc
1085 for (np = sp->so_evlist.tqh_first; np != NULL; np = np->ee_slist.tqe_next) {
1086 if (np->ee_proc == p) {
1087 FREE(eqp,M_TEMP);
1088 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
1089 return(EINVAL);
1090 }
1091 }
1092
1093 TAILQ_INSERT_TAIL(&sp->so_evlist, eqp, ee_slist);
1094 postevent(sp, 0, EV_RWBYTES); // catch existing events
1095 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
1096 return(0);
1097 }
1098
1099 struct evwait_args {
1100 struct eventreq *u_req;
1101 struct timeval *tv;
1102 };
1103
1104 /*
1105 * waitevent system call.
1106 * grabs the next waiting event for this proc and returns
1107 * it. if no events, user can request to sleep with timeout
1108 * or poll mode (tv=NULL);
1109 */
1110 int
1111 waitevent(p, uap, retval)
1112 struct proc *p;
1113 struct evwait_args *uap;
1114 register_t *retval;
1115 {
1116 int error = 0;
1117 struct eventqelt *eqp;
1118 int timo;
1119 struct timeval atv;
1120 int s;
1121
1122 if (uap->tv) {
1123 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
1124 sizeof (atv));
1125 if (error)
1126 return(error);
1127 if (itimerfix(&atv)) {
1128 error = EINVAL;
1129 return(error);
1130 }
1131 s = splhigh();
1132 timeradd(&atv, &time, &atv);
1133 timo = hzto(&atv);
1134 splx(s);
1135 } else
1136 timo = 0;
1137
1138 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
1139
1140 retry:
1141 s = splhigh();
1142 if ((eqp = evprocdeque(p,NULL)) != NULL) {
1143 splx(s);
1144 error = copyout((caddr_t)&eqp->ee_req, (caddr_t)uap->u_req,
1145 sizeof(struct eventreq));
1146 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
1147 eqp->ee_req.er_handle,eqp->ee_req.er_eventbits,eqp,0);
1148 return(error);
1149 } else {
1150 if (uap->tv && (timo == 0)) {
1151 splx(s);
1152 *retval = 1; // poll failed
1153 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
1154 return(error);
1155 }
1156
1157 KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
1158 error = tsleep(&p->p_evlist, PSOCK | PCATCH, "waitevent", timo);
1159 KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
1160 splx(s);
1161 if (error == 0)
1162 goto retry;
1163 if (error == ERESTART)
1164 error = EINTR;
1165 if (error == EWOULDBLOCK) {
1166 *retval = 1;
1167 error = 0;
1168 }
1169 }
1170 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
1171 return(error);
1172 }
1173
1174 struct modwatch_args {
1175 struct eventreq *u_req;
1176 int u_eventmask;
1177 };
1178
1179 /*
1180 * modwatch system call. user passes in event to modify.
1181 * if we find it we reset the event bits and que/deque event
1182 * it needed.
1183 */
1184 int
1185 modwatch(p, uap, retval)
1186 struct proc *p;
1187 struct modwatch_args *uap;
1188 register_t *retval;
1189 {
1190 struct eventreq er;
1191 struct eventreq *erp = &er;
1192 struct eventqelt *evq;
1193 int error;
1194 struct file *fp;
1195 struct socket *sp;
1196 int flag;
1197
1198 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
1199
1200 // get users request pkt
1201 if (error = copyin((caddr_t)uap->u_req, (caddr_t)erp,
1202 sizeof(struct eventreq))) return(error);
1203
1204 if (erp->er_type != EV_FD) return(EINVAL);
1205 if (erp->er_handle < 0) return(EBADF);
1206 if (erp->er_handle > p->p_fd->fd_nfiles) return(EBADF);
1207 if ((fp = *fdfile(p, erp->er_handle)) == NULL)
1208 return(EBADF);
1209 if (fp->f_type != DTYPE_SOCKET) return(EINVAL); // for now must be sock
1210 sp = (struct socket *)fp->f_data;
1211 assert(sp != NULL);
1212
1213
1214 // locate event if possible
1215 for (evq = sp->so_evlist.tqh_first;
1216 evq != NULL; evq = evq->ee_slist.tqe_next) {
1217 if (evq->ee_proc == p) break;
1218 }
1219
1220 if (evq == NULL) {
1221 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
1222 return(EINVAL);
1223 }
1224 KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
1225
1226 if (uap->u_eventmask == EV_RM) {
1227 evprocdeque(p, evq);
1228 TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist);
1229 FREE(evq, M_TEMP);
1230 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
1231 return(0);
1232 }
1233
1234 switch (uap->u_eventmask & EV_MASK) {
1235
1236 case 0:
1237 flag = 0;
1238 break;
1239
1240 case EV_RE:
1241 case EV_WR:
1242 case EV_RE|EV_WR:
1243 flag = EV_RWBYTES;
1244 break;
1245
1246 case EV_EX:
1247 flag = EV_OOB;
1248 break;
1249
1250 case EV_EX|EV_RE:
1251 case EV_EX|EV_WR:
1252 case EV_EX|EV_RE|EV_WR:
1253 flag = EV_OOB|EV_RWBYTES;
1254 break;
1255
1256 default:
1257 return(EINVAL);
1258 }
1259
1260 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
1261 evprocdeque(p, evq);
1262 evq->ee_req.er_eventbits = 0;
1263 postevent(sp, 0, flag);
1264 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,sp,flag,0);
1265 return(0);
1266 }