sys_generic.c revision 1.26 1 /* $NetBSD: sys_generic.c,v 1.26 1996/06/13 05:08:47 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/filedesc.h>
46 #include <sys/ioctl.h>
47 #include <sys/file.h>
48 #include <sys/proc.h>
49 #include <sys/socketvar.h>
50 #include <sys/signalvar.h>
51 #include <sys/uio.h>
52 #include <sys/kernel.h>
53 #include <sys/stat.h>
54 #include <sys/malloc.h>
55 #ifdef KTRACE
56 #include <sys/ktrace.h>
57 #endif
58
59 #include <sys/mount.h>
60 #include <sys/syscallargs.h>
61
62 int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
63 int seltrue __P((dev_t, int, struct proc *));
64
65 /*
66 * Read system call.
67 */
68 /* ARGSUSED */
69 int
70 sys_read(p, v, retval)
71 struct proc *p;
72 void *v;
73 register_t *retval;
74 {
75 register struct sys_read_args /* {
76 syscallarg(int) fd;
77 syscallarg(char *) buf;
78 syscallarg(u_int) nbyte;
79 } */ *uap = v;
80 register struct file *fp;
81 register struct filedesc *fdp = p->p_fd;
82 struct uio auio;
83 struct iovec aiov;
84 long cnt, error = 0;
85 #ifdef KTRACE
86 struct iovec ktriov;
87 #endif
88
89 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
90 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
91 (fp->f_flag & FREAD) == 0)
92 return (EBADF);
93 aiov.iov_base = (caddr_t)SCARG(uap, buf);
94 aiov.iov_len = SCARG(uap, nbyte);
95 auio.uio_iov = &aiov;
96 auio.uio_iovcnt = 1;
97 auio.uio_resid = SCARG(uap, nbyte);
98 auio.uio_rw = UIO_READ;
99 auio.uio_segflg = UIO_USERSPACE;
100 auio.uio_procp = p;
101 if (auio.uio_resid < 0)
102 return EINVAL;
103 #ifdef KTRACE
104 /*
105 * if tracing, save a copy of iovec
106 */
107 if (KTRPOINT(p, KTR_GENIO))
108 ktriov = aiov;
109 #endif
110 cnt = SCARG(uap, nbyte);
111 error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
112 if (error)
113 if (auio.uio_resid != cnt && (error == ERESTART ||
114 error == EINTR || error == EWOULDBLOCK))
115 error = 0;
116 cnt -= auio.uio_resid;
117 #ifdef KTRACE
118 if (KTRPOINT(p, KTR_GENIO) && error == 0)
119 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, &ktriov,
120 cnt, error);
121 #endif
122 *retval = cnt;
123 return (error);
124 }
125
126 /*
127 * Scatter read system call.
128 */
129 int
130 sys_readv(p, v, retval)
131 struct proc *p;
132 void *v;
133 register_t *retval;
134 {
135 register struct sys_readv_args /* {
136 syscallarg(int) fd;
137 syscallarg(struct iovec *) iovp;
138 syscallarg(u_int) iovcnt;
139 } */ *uap = v;
140 register struct file *fp;
141 register struct filedesc *fdp = p->p_fd;
142 struct uio auio;
143 register struct iovec *iov;
144 struct iovec *needfree;
145 struct iovec aiov[UIO_SMALLIOV];
146 long i, cnt, error = 0;
147 u_int iovlen;
148 #ifdef KTRACE
149 struct iovec *ktriov = NULL;
150 #endif
151
152 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
153 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
154 (fp->f_flag & FREAD) == 0)
155 return (EBADF);
156 /* note: can't use iovlen until iovcnt is validated */
157 iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
158 if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
159 if (SCARG(uap, iovcnt) > UIO_MAXIOV)
160 return (EINVAL);
161 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
162 needfree = iov;
163 } else {
164 iov = aiov;
165 needfree = NULL;
166 }
167 auio.uio_iov = iov;
168 auio.uio_iovcnt = SCARG(uap, iovcnt);
169 auio.uio_rw = UIO_READ;
170 auio.uio_segflg = UIO_USERSPACE;
171 auio.uio_procp = p;
172 error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
173 if (error)
174 goto done;
175 auio.uio_resid = 0;
176 for (i = 0; i < SCARG(uap, iovcnt); i++) {
177 #if 0
178 /* Cannot happen iov_len is unsigned */
179 if (iov->iov_len < 0) {
180 error = EINVAL;
181 goto done;
182 }
183 #endif
184 auio.uio_resid += iov->iov_len;
185 if (auio.uio_resid < 0) {
186 error = EINVAL;
187 goto done;
188 }
189 iov++;
190 }
191 #ifdef KTRACE
192 /*
193 * if tracing, save a copy of iovec
194 */
195 if (KTRPOINT(p, KTR_GENIO)) {
196 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
197 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
198 }
199 #endif
200 cnt = auio.uio_resid;
201 error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
202 if (error)
203 if (auio.uio_resid != cnt && (error == ERESTART ||
204 error == EINTR || error == EWOULDBLOCK))
205 error = 0;
206 cnt -= auio.uio_resid;
207 #ifdef KTRACE
208 if (ktriov != NULL) {
209 if (error == 0)
210 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, ktriov,
211 cnt, error);
212 FREE(ktriov, M_TEMP);
213 }
214 #endif
215 *retval = cnt;
216 done:
217 if (needfree)
218 FREE(needfree, M_IOV);
219 return (error);
220 }
221
222 /*
223 * Write system call
224 */
225 int
226 sys_write(p, v, retval)
227 struct proc *p;
228 void *v;
229 register_t *retval;
230 {
231 register struct sys_write_args /* {
232 syscallarg(int) fd;
233 syscallarg(char *) buf;
234 syscallarg(u_int) nbyte;
235 } */ *uap = v;
236 register struct file *fp;
237 register struct filedesc *fdp = p->p_fd;
238 struct uio auio;
239 struct iovec aiov;
240 long cnt, error = 0;
241 #ifdef KTRACE
242 struct iovec ktriov;
243 #endif
244
245 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
246 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
247 (fp->f_flag & FWRITE) == 0)
248 return (EBADF);
249 aiov.iov_base = (caddr_t)SCARG(uap, buf);
250 aiov.iov_len = SCARG(uap, nbyte);
251 auio.uio_iov = &aiov;
252 auio.uio_iovcnt = 1;
253 auio.uio_resid = SCARG(uap, nbyte);
254 auio.uio_rw = UIO_WRITE;
255 auio.uio_segflg = UIO_USERSPACE;
256 auio.uio_procp = p;
257 if (auio.uio_resid < 0)
258 return EINVAL;
259 #ifdef KTRACE
260 /*
261 * if tracing, save a copy of iovec
262 */
263 if (KTRPOINT(p, KTR_GENIO))
264 ktriov = aiov;
265 #endif
266 cnt = SCARG(uap, nbyte);
267 error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
268 if (error) {
269 if (auio.uio_resid != cnt && (error == ERESTART ||
270 error == EINTR || error == EWOULDBLOCK))
271 error = 0;
272 if (error == EPIPE)
273 psignal(p, SIGPIPE);
274 }
275 cnt -= auio.uio_resid;
276 #ifdef KTRACE
277 if (KTRPOINT(p, KTR_GENIO) && error == 0)
278 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
279 &ktriov, cnt, error);
280 #endif
281 *retval = cnt;
282 return (error);
283 }
284
285 /*
286 * Gather write system call
287 */
288 int
289 sys_writev(p, v, retval)
290 struct proc *p;
291 void *v;
292 register_t *retval;
293 {
294 register struct sys_writev_args /* {
295 syscallarg(int) fd;
296 syscallarg(struct iovec *) iovp;
297 syscallarg(u_int) iovcnt;
298 } */ *uap = v;
299 register struct file *fp;
300 register struct filedesc *fdp = p->p_fd;
301 struct uio auio;
302 register struct iovec *iov;
303 struct iovec *needfree;
304 struct iovec aiov[UIO_SMALLIOV];
305 long i, cnt, error = 0;
306 u_int iovlen;
307 #ifdef KTRACE
308 struct iovec *ktriov = NULL;
309 #endif
310
311 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
312 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
313 (fp->f_flag & FWRITE) == 0)
314 return (EBADF);
315 /* note: can't use iovlen until iovcnt is validated */
316 iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
317 if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
318 if (SCARG(uap, iovcnt) > UIO_MAXIOV)
319 return (EINVAL);
320 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
321 needfree = iov;
322 } else {
323 iov = aiov;
324 needfree = NULL;
325 }
326 auio.uio_iov = iov;
327 auio.uio_iovcnt = SCARG(uap, iovcnt);
328 auio.uio_rw = UIO_WRITE;
329 auio.uio_segflg = UIO_USERSPACE;
330 auio.uio_procp = p;
331 error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
332 if (error)
333 goto done;
334 auio.uio_resid = 0;
335 for (i = 0; i < SCARG(uap, iovcnt); i++) {
336 #if 0
337 /* Cannot happen iov_len is unsigned */
338 if (iov->iov_len < 0) {
339 error = EINVAL;
340 goto done;
341 }
342 #endif
343 auio.uio_resid += iov->iov_len;
344 if (auio.uio_resid < 0) {
345 error = EINVAL;
346 goto done;
347 }
348 iov++;
349 }
350 #ifdef KTRACE
351 /*
352 * if tracing, save a copy of iovec
353 */
354 if (KTRPOINT(p, KTR_GENIO)) {
355 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
356 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
357 }
358 #endif
359 cnt = auio.uio_resid;
360 error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
361 if (error) {
362 if (auio.uio_resid != cnt && (error == ERESTART ||
363 error == EINTR || error == EWOULDBLOCK))
364 error = 0;
365 if (error == EPIPE)
366 psignal(p, SIGPIPE);
367 }
368 cnt -= auio.uio_resid;
369 #ifdef KTRACE
370 if (ktriov != NULL) {
371 if (error == 0)
372 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
373 ktriov, cnt, error);
374 FREE(ktriov, M_TEMP);
375 }
376 #endif
377 *retval = cnt;
378 done:
379 if (needfree)
380 FREE(needfree, M_IOV);
381 return (error);
382 }
383
384 /*
385 * Ioctl system call
386 */
387 /* ARGSUSED */
388 int
389 sys_ioctl(p, v, retval)
390 struct proc *p;
391 void *v;
392 register_t *retval;
393 {
394 register struct sys_ioctl_args /* {
395 syscallarg(int) fd;
396 syscallarg(u_long) com;
397 syscallarg(caddr_t) data;
398 } */ *uap = v;
399 register struct file *fp;
400 register struct filedesc *fdp;
401 register u_long com;
402 register int error;
403 register u_int size;
404 caddr_t data, memp;
405 int tmp;
406 #define STK_PARAMS 128
407 char stkbuf[STK_PARAMS];
408
409 fdp = p->p_fd;
410 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
411 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
412 return (EBADF);
413
414 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
415 return (EBADF);
416
417 switch (com = SCARG(uap, com)) {
418 case FIONCLEX:
419 fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
420 return (0);
421 case FIOCLEX:
422 fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
423 return (0);
424 }
425
426 /*
427 * Interpret high order word to find amount of data to be
428 * copied to/from the user's address space.
429 */
430 size = IOCPARM_LEN(com);
431 if (size > IOCPARM_MAX)
432 return (ENOTTY);
433 memp = NULL;
434 if (size > sizeof (stkbuf)) {
435 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
436 data = memp;
437 } else
438 data = stkbuf;
439 if (com&IOC_IN) {
440 if (size) {
441 error = copyin(SCARG(uap, data), data, (u_int)size);
442 if (error) {
443 if (memp)
444 free(memp, M_IOCTLOPS);
445 return (error);
446 }
447 } else
448 *(caddr_t *)data = SCARG(uap, data);
449 } else if ((com&IOC_OUT) && size)
450 /*
451 * Zero the buffer so the user always
452 * gets back something deterministic.
453 */
454 bzero(data, size);
455 else if (com&IOC_VOID)
456 *(caddr_t *)data = SCARG(uap, data);
457
458 switch (com) {
459
460 case FIONBIO:
461 if ((tmp = *(int *)data) != 0)
462 fp->f_flag |= FNONBLOCK;
463 else
464 fp->f_flag &= ~FNONBLOCK;
465 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
466 break;
467
468 case FIOASYNC:
469 if ((tmp = *(int *)data) != 0)
470 fp->f_flag |= FASYNC;
471 else
472 fp->f_flag &= ~FASYNC;
473 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
474 break;
475
476 case FIOSETOWN:
477 tmp = *(int *)data;
478 if (fp->f_type == DTYPE_SOCKET) {
479 ((struct socket *)fp->f_data)->so_pgid = tmp;
480 error = 0;
481 break;
482 }
483 if (tmp <= 0) {
484 tmp = -tmp;
485 } else {
486 struct proc *p1 = pfind(tmp);
487 if (p1 == 0) {
488 error = ESRCH;
489 break;
490 }
491 tmp = p1->p_pgrp->pg_id;
492 }
493 error = (*fp->f_ops->fo_ioctl)
494 (fp, TIOCSPGRP, (caddr_t)&tmp, p);
495 break;
496
497 case FIOGETOWN:
498 if (fp->f_type == DTYPE_SOCKET) {
499 error = 0;
500 *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
501 break;
502 }
503 error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
504 *(int *)data = -*(int *)data;
505 break;
506
507 default:
508 error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
509 /*
510 * Copy any data to user, size was
511 * already set and checked above.
512 */
513 if (error == 0 && (com&IOC_OUT) && size)
514 error = copyout(data, SCARG(uap, data), (u_int)size);
515 break;
516 }
517 if (memp)
518 free(memp, M_IOCTLOPS);
519 return (error);
520 }
521
522 int selwait, nselcoll;
523
524 /*
525 * Select system call.
526 */
527 int
528 sys_select(p, v, retval)
529 register struct proc *p;
530 void *v;
531 register_t *retval;
532 {
533 register struct sys_select_args /* {
534 syscallarg(u_int) nd;
535 syscallarg(fd_set *) in;
536 syscallarg(fd_set *) ou;
537 syscallarg(fd_set *) ex;
538 syscallarg(struct timeval *) tv;
539 } */ *uap = v;
540 caddr_t bits;
541 char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
542 struct timeval atv;
543 int s, ncoll, error = 0, timo;
544 u_int ni;
545
546 if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
547 /* forgiving; slightly wrong */
548 SCARG(uap, nd) = p->p_fd->fd_nfiles;
549 }
550 ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
551 if (SCARG(uap, nd) > FD_SETSIZE)
552 bits = malloc(ni * 6, M_TEMP, M_WAITOK);
553 else
554 bits = smallbits;
555
556 #define getbits(name, x) \
557 if (SCARG(uap, name)) { \
558 error = copyin((caddr_t)SCARG(uap, name), bits + ni * x, ni); \
559 if (error) \
560 goto done; \
561 } else \
562 bzero(bits + ni * x, ni);
563 getbits(in, 0);
564 getbits(ou, 1);
565 getbits(ex, 2);
566 #undef getbits
567
568 if (SCARG(uap, tv)) {
569 error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
570 sizeof (atv));
571 if (error)
572 goto done;
573 if (itimerfix(&atv)) {
574 error = EINVAL;
575 goto done;
576 }
577 s = splclock();
578 timeradd(&atv, &time, &atv);
579 timo = hzto(&atv);
580 /*
581 * Avoid inadvertently sleeping forever.
582 */
583 if (timo == 0)
584 timo = 1;
585 splx(s);
586 } else
587 timo = 0;
588 retry:
589 ncoll = nselcoll;
590 p->p_flag |= P_SELECT;
591 error = selscan(p, (fd_mask *)(bits + ni * 0),
592 (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
593 if (error || *retval)
594 goto done;
595 s = splhigh();
596 /* this should be timercmp(&time, &atv, >=) */
597 if (SCARG(uap, tv) && (time.tv_sec > atv.tv_sec ||
598 (time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec))) {
599 splx(s);
600 goto done;
601 }
602 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
603 splx(s);
604 goto retry;
605 }
606 p->p_flag &= ~P_SELECT;
607 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
608 splx(s);
609 if (error == 0)
610 goto retry;
611 done:
612 p->p_flag &= ~P_SELECT;
613 /* select is not restarted after signals... */
614 if (error == ERESTART)
615 error = EINTR;
616 if (error == EWOULDBLOCK)
617 error = 0;
618 #define putbits(name, x) \
619 if (SCARG(uap, name) && (error2 = copyout(bits + ni * x, \
620 (caddr_t)SCARG(uap, name), ni))) \
621 error = error2;
622 if (error == 0) {
623 int error2;
624
625 putbits(in, 3);
626 putbits(ou, 4);
627 putbits(ex, 5);
628 #undef putbits
629 }
630 if (SCARG(uap, nd) > FD_SETSIZE)
631 free(bits, M_TEMP);
632 return (error);
633 }
634
635 int
636 selscan(p, ibitp, obitp, nfd, retval)
637 struct proc *p;
638 fd_mask *ibitp, *obitp;
639 int nfd;
640 register_t *retval;
641 {
642 register struct filedesc *fdp = p->p_fd;
643 register int msk, i, j, fd;
644 register fd_mask ibits, obits;
645 struct file *fp;
646 int n = 0;
647 static int flag[3] = { FREAD, FWRITE, 0 };
648
649 for (msk = 0; msk < 3; msk++) {
650 for (i = 0; i < nfd; i += NFDBITS) {
651 ibits = *ibitp++;
652 obits = 0;
653 while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
654 ibits &= ~(1 << j);
655 fp = fdp->fd_ofiles[fd];
656 if (fp == NULL)
657 return (EBADF);
658 if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
659 obits |= (1 << j);
660 n++;
661 }
662 }
663 *obitp++ = obits;
664 }
665 }
666 *retval = n;
667 return (0);
668 }
669
670 /*ARGSUSED*/
671 int
672 seltrue(dev, flag, p)
673 dev_t dev;
674 int flag;
675 struct proc *p;
676 {
677
678 return (1);
679 }
680
681 /*
682 * Record a select request.
683 */
684 void
685 selrecord(selector, sip)
686 struct proc *selector;
687 struct selinfo *sip;
688 {
689 struct proc *p;
690 pid_t mypid;
691
692 mypid = selector->p_pid;
693 if (sip->si_pid == mypid)
694 return;
695 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
696 p->p_wchan == (caddr_t)&selwait)
697 sip->si_flags |= SI_COLL;
698 else
699 sip->si_pid = mypid;
700 }
701
702 /*
703 * Do a wakeup when a selectable event occurs.
704 */
705 void
706 selwakeup(sip)
707 register struct selinfo *sip;
708 {
709 register struct proc *p;
710 int s;
711
712 if (sip->si_pid == 0)
713 return;
714 if (sip->si_flags & SI_COLL) {
715 nselcoll++;
716 sip->si_flags &= ~SI_COLL;
717 wakeup((caddr_t)&selwait);
718 }
719 p = pfind(sip->si_pid);
720 sip->si_pid = 0;
721 if (p != NULL) {
722 s = splhigh();
723 if (p->p_wchan == (caddr_t)&selwait) {
724 if (p->p_stat == SSLEEP)
725 setrunnable(p);
726 else
727 unsleep(p);
728 } else if (p->p_flag & P_SELECT)
729 p->p_flag &= ~P_SELECT;
730 splx(s);
731 }
732 }
733