2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
40 * $DragonFly: src/sys/kern/sys_generic.c,v 1.21 2005/06/06 15:02:28 dillon Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
53 #include <sys/signalvar.h>
54 #include <sys/socketvar.h>
56 #include <sys/kernel.h>
57 #include <sys/kern_syscall.h>
58 #include <sys/malloc.h>
59 #include <sys/mapped_ioctl.h>
61 #include <sys/queue.h>
62 #include <sys/resourcevar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
67 #include <sys/ktrace.h>
70 #include <vm/vm_page.h>
71 #include <sys/file2.h>
73 #include <machine/limits.h>
75 static MALLOC_DEFINE(M_IOCTLOPS
, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP
, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT
, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV
, "iov", "large iov's");
80 static int pollscan (struct proc
*, struct pollfd
*, u_int
, int *);
81 static int selscan (struct proc
*, fd_mask
**, fd_mask
**,
91 if (((u_int
)fd
) >= fdp
->fd_nfiles
||
92 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
93 (fp
->f_flag
& flag
) == 0) {
104 read(struct read_args
*uap
)
106 struct thread
*td
= curthread
;
111 aiov
.iov_base
= uap
->buf
;
112 aiov
.iov_len
= uap
->nbyte
;
113 auio
.uio_iov
= &aiov
;
115 auio
.uio_offset
= -1;
116 auio
.uio_resid
= uap
->nbyte
;
117 auio
.uio_rw
= UIO_READ
;
118 auio
.uio_segflg
= UIO_USERSPACE
;
121 error
= kern_readv(uap
->fd
, &auio
, 0, &uap
->sysmsg_result
);
130 pread(struct pread_args
*uap
)
132 struct thread
*td
= curthread
;
137 aiov
.iov_base
= uap
->buf
;
138 aiov
.iov_len
= uap
->nbyte
;
139 auio
.uio_iov
= &aiov
;
141 auio
.uio_offset
= uap
->offset
;
142 auio
.uio_resid
= uap
->nbyte
;
143 auio
.uio_rw
= UIO_READ
;
144 auio
.uio_segflg
= UIO_USERSPACE
;
147 error
= kern_readv(uap
->fd
, &auio
, FOF_OFFSET
, &uap
->sysmsg_result
);
153 readv(struct readv_args
*uap
)
155 struct thread
*td
= curthread
;
157 struct iovec aiov
[UIO_SMALLIOV
], *iov
= NULL
;
160 error
= iovec_copyin(uap
->iovp
, &iov
, aiov
, uap
->iovcnt
,
165 auio
.uio_iovcnt
= uap
->iovcnt
;
166 auio
.uio_offset
= -1;
167 auio
.uio_rw
= UIO_READ
;
168 auio
.uio_segflg
= UIO_USERSPACE
;
171 error
= kern_readv(uap
->fd
, &auio
, 0, &uap
->sysmsg_result
);
173 iovec_free(&iov
, aiov
);
178 kern_readv(int fd
, struct uio
*auio
, int flags
, int *res
)
180 struct thread
*td
= curthread
;
181 struct proc
*p
= td
->td_proc
;
183 struct filedesc
*fdp
= p
->p_fd
;
186 struct iovec
*ktriov
= NULL
;
192 fp
= holdfp(fdp
, fd
, FREAD
);
195 if (flags
& FOF_OFFSET
&& fp
->f_type
!= DTYPE_VNODE
) {
199 if (auio
->uio_resid
< 0) {
205 * if tracing, save a copy of iovec
207 if (KTRPOINT(td
, KTR_GENIO
)) {
208 int iovlen
= auio
->uio_iovcnt
* sizeof(struct iovec
);
210 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
211 bcopy((caddr_t
)auio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
215 len
= auio
->uio_resid
;
216 error
= fo_read(fp
, auio
, fp
->f_cred
, flags
, td
);
218 if (auio
->uio_resid
!= len
&& (error
== ERESTART
||
219 error
== EINTR
|| error
== EWOULDBLOCK
))
223 if (ktriov
!= NULL
) {
225 ktruio
.uio_iov
= ktriov
;
226 ktruio
.uio_resid
= len
- auio
->uio_resid
;
227 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, &ktruio
, error
);
229 FREE(ktriov
, M_TEMP
);
233 *res
= len
- auio
->uio_resid
;
243 write(struct write_args
*uap
)
245 struct thread
*td
= curthread
;
250 aiov
.iov_base
= (void *)(uintptr_t)uap
->buf
;
251 aiov
.iov_len
= uap
->nbyte
;
252 auio
.uio_iov
= &aiov
;
254 auio
.uio_offset
= -1;
255 auio
.uio_resid
= uap
->nbyte
;
256 auio
.uio_rw
= UIO_WRITE
;
257 auio
.uio_segflg
= UIO_USERSPACE
;
260 error
= kern_writev(uap
->fd
, &auio
, 0, &uap
->sysmsg_result
);
269 pwrite(struct pwrite_args
*uap
)
271 struct thread
*td
= curthread
;
276 aiov
.iov_base
= (void *)(uintptr_t)uap
->buf
;
277 aiov
.iov_len
= uap
->nbyte
;
278 auio
.uio_iov
= &aiov
;
280 auio
.uio_offset
= uap
->offset
;
281 auio
.uio_resid
= uap
->nbyte
;
282 auio
.uio_rw
= UIO_WRITE
;
283 auio
.uio_segflg
= UIO_USERSPACE
;
286 error
= kern_writev(uap
->fd
, &auio
, FOF_OFFSET
, &uap
->sysmsg_result
);
292 writev(struct writev_args
*uap
)
294 struct thread
*td
= curthread
;
296 struct iovec aiov
[UIO_SMALLIOV
], *iov
= NULL
;
299 error
= iovec_copyin(uap
->iovp
, &iov
, aiov
, uap
->iovcnt
,
304 auio
.uio_iovcnt
= uap
->iovcnt
;
305 auio
.uio_offset
= -1;
306 auio
.uio_rw
= UIO_WRITE
;
307 auio
.uio_segflg
= UIO_USERSPACE
;
310 error
= kern_writev(uap
->fd
, &auio
, 0, &uap
->sysmsg_result
);
312 iovec_free(&iov
, aiov
);
317 * Gather write system call
320 kern_writev(int fd
, struct uio
*auio
, int flags
, int *res
)
322 struct thread
*td
= curthread
;
323 struct proc
*p
= td
->td_proc
;
325 struct filedesc
*fdp
= p
->p_fd
;
328 struct iovec
*ktriov
= NULL
;
334 fp
= holdfp(fdp
, fd
, FWRITE
);
337 if ((flags
& FOF_OFFSET
) && fp
->f_type
!= DTYPE_VNODE
) {
341 if (auio
->uio_resid
< 0) {
347 * if tracing, save a copy of iovec and uio
349 if (KTRPOINT(td
, KTR_GENIO
)) {
350 int iovlen
= auio
->uio_iovcnt
* sizeof(struct iovec
);
352 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
353 bcopy((caddr_t
)auio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
357 len
= auio
->uio_resid
;
358 if (fp
->f_type
== DTYPE_VNODE
)
360 error
= fo_write(fp
, auio
, fp
->f_cred
, flags
, td
);
362 if (auio
->uio_resid
!= len
&& (error
== ERESTART
||
363 error
== EINTR
|| error
== EWOULDBLOCK
))
369 if (ktriov
!= NULL
) {
371 ktruio
.uio_iov
= ktriov
;
372 ktruio
.uio_resid
= len
- auio
->uio_resid
;
373 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, &ktruio
, error
);
375 FREE(ktriov
, M_TEMP
);
379 *res
= len
- auio
->uio_resid
;
390 ioctl(struct ioctl_args
*uap
)
392 return(mapped_ioctl(uap
->fd
, uap
->com
, uap
->data
, NULL
));
395 struct ioctl_map_entry
{
397 struct ioctl_map_range
*cmd_ranges
;
398 LIST_ENTRY(ioctl_map_entry
) entries
;
402 * The true heart of all ioctl syscall handlers (native, emulation).
403 * If map != NULL, it will be searched for a matching entry for com,
404 * and appropriate conversions/conversion functions will be utilized.
407 mapped_ioctl(int fd
, u_long com
, caddr_t uspc_data
, struct ioctl_map
*map
)
409 struct thread
*td
= curthread
;
410 struct proc
*p
= td
->td_proc
;
412 struct filedesc
*fdp
;
413 struct ioctl_map_range
*iomc
= NULL
;
419 #define STK_PARAMS 128
421 char stkbuf
[STK_PARAMS
];
427 if ((u_int
)fd
>= fdp
->fd_nfiles
||
428 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
)
431 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0)
434 if (map
!= NULL
) { /* obey translation map */
436 struct ioctl_map_entry
*e
;
438 maskcmd
= com
& map
->mask
;
440 LIST_FOREACH(e
, &map
->mapping
, entries
) {
441 for (iomc
= e
->cmd_ranges
; iomc
->start
!= 0 ||
442 iomc
->maptocmd
!= 0 || iomc
->wrapfunc
!= NULL
||
443 iomc
->mapfunc
!= NULL
;
445 if (maskcmd
>= iomc
->start
&&
446 maskcmd
<= iomc
->end
)
450 /* Did we find a match? */
451 if (iomc
->start
!= 0 || iomc
->maptocmd
!= 0 ||
452 iomc
->wrapfunc
!= NULL
|| iomc
->mapfunc
!= NULL
)
457 (iomc
->start
== 0 && iomc
->maptocmd
== 0
458 && iomc
->wrapfunc
== NULL
&& iomc
->mapfunc
== NULL
)) {
459 printf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
460 map
->sys
, fd
, maskcmd
,
461 (int)((maskcmd
>> 8) & 0xff),
462 (int)(maskcmd
& 0xff));
467 * If it's a non-range one to one mapping, maptocmd should be
468 * correct. If it's a ranged one to one mapping, we pass the
469 * original value of com, and for a range mapped to a different
470 * range, we always need a mapping function to translate the
471 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
473 if (iomc
->start
== iomc
->end
&& iomc
->maptocmd
== iomc
->maptoend
) {
474 com
= iomc
->maptocmd
;
475 } else if (iomc
->start
== iomc
->maptocmd
&& iomc
->end
== iomc
->maptoend
) {
476 if (iomc
->mapfunc
!= NULL
)
477 com
= iomc
->mapfunc(iomc
->start
, iomc
->end
,
478 iomc
->start
, iomc
->end
,
481 if (iomc
->mapfunc
!= NULL
) {
482 com
= iomc
->mapfunc(iomc
->start
, iomc
->end
,
483 iomc
->maptocmd
, iomc
->maptoend
,
486 printf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
487 map
->sys
, fd
, maskcmd
,
488 (int)((maskcmd
>> 8) & 0xff),
489 (int)(maskcmd
& 0xff));
497 fdp
->fd_ofileflags
[fd
] &= ~UF_EXCLOSE
;
500 fdp
->fd_ofileflags
[fd
] |= UF_EXCLOSE
;
505 * Interpret high order word to find amount of data to be
506 * copied to/from the user's address space.
508 size
= IOCPARM_LEN(com
);
509 if (size
> IOCPARM_MAX
)
515 if (size
> sizeof (ubuf
.stkbuf
)) {
516 memp
= malloc(size
, M_IOCTLOPS
, M_WAITOK
);
521 if ((com
& IOC_IN
) != 0) {
523 error
= copyin(uspc_data
, data
, (u_int
)size
);
526 free(memp
, M_IOCTLOPS
);
531 *(caddr_t
*)data
= uspc_data
;
533 } else if ((com
& IOC_OUT
) != 0 && size
) {
535 * Zero the buffer so the user always
536 * gets back something deterministic.
539 } else if ((com
& IOC_VOID
) != 0) {
540 *(caddr_t
*)data
= uspc_data
;
546 if ((tmp
= *(int *)data
))
547 fp
->f_flag
|= FNONBLOCK
;
549 fp
->f_flag
&= ~FNONBLOCK
;
550 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, td
);
554 if ((tmp
= *(int *)data
))
555 fp
->f_flag
|= FASYNC
;
557 fp
->f_flag
&= ~FASYNC
;
558 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, td
);
563 * If there is a override function,
564 * call it instead of directly routing the call
566 if (map
!= NULL
&& iomc
->wrapfunc
!= NULL
)
567 error
= iomc
->wrapfunc(fp
, com
, ocom
, data
, td
);
569 error
= fo_ioctl(fp
, com
, data
, td
);
571 * Copy any data to user, size was
572 * already set and checked above.
574 if (error
== 0 && (com
& IOC_OUT
) != 0 && size
!= 0)
575 error
= copyout(data
, uspc_data
, (u_int
)size
);
579 free(memp
, M_IOCTLOPS
);
585 mapped_ioctl_register_handler(struct ioctl_map_handler
*he
)
587 struct ioctl_map_entry
*ne
;
589 KKASSERT(he
!= NULL
&& he
->map
!= NULL
&& he
->cmd_ranges
!= NULL
&&
590 he
->subsys
!= NULL
&& *he
->subsys
!= '\0');
592 ne
= malloc(sizeof(struct ioctl_map_entry
), M_IOCTLMAP
, M_WAITOK
);
594 ne
->subsys
= he
->subsys
;
595 ne
->cmd_ranges
= he
->cmd_ranges
;
597 LIST_INSERT_HEAD(&he
->map
->mapping
, ne
, entries
);
603 mapped_ioctl_unregister_handler(struct ioctl_map_handler
*he
)
605 struct ioctl_map_entry
*ne
;
607 KKASSERT(he
!= NULL
&& he
->map
!= NULL
&& he
->cmd_ranges
!= NULL
);
609 LIST_FOREACH(ne
, &he
->map
->mapping
, entries
) {
610 if (ne
->cmd_ranges
!= he
->cmd_ranges
)
612 LIST_REMOVE(ne
, entries
);
613 free(ne
, M_IOCTLMAP
);
619 static int nselcoll
; /* Select collisions since boot */
621 SYSCTL_INT(_kern
, OID_AUTO
, nselcoll
, CTLFLAG_RD
, &nselcoll
, 0, "");
624 * Select system call.
627 select(struct select_args
*uap
)
629 struct proc
*p
= curproc
;
632 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
633 * infds with the new FD_SETSIZE of 1024, and more than enough for
634 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
637 fd_mask s_selbits
[howmany(2048, NFDBITS
)];
638 fd_mask
*ibits
[3], *obits
[3], *selbits
, *sbp
;
639 struct timeval atv
, rtv
, ttv
;
640 int ncoll
, error
, timo
;
641 u_int nbufbytes
, ncpbytes
, nfdbits
;
645 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
646 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
649 * Allocate just enough bits for the non-null fd_sets. Use the
650 * preallocated auto buffer if possible.
652 nfdbits
= roundup(uap
->nd
, NFDBITS
);
653 ncpbytes
= nfdbits
/ NBBY
;
656 nbufbytes
+= 2 * ncpbytes
;
658 nbufbytes
+= 2 * ncpbytes
;
660 nbufbytes
+= 2 * ncpbytes
;
661 if (nbufbytes
<= sizeof s_selbits
)
662 selbits
= &s_selbits
[0];
664 selbits
= malloc(nbufbytes
, M_SELECT
, M_WAITOK
);
667 * Assign pointers into the bit buffers and fetch the input bits.
668 * Put the output buffers together so that they can be bzeroed
672 #define getbits(name, x) \
674 if (uap->name == NULL) \
677 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
679 sbp += ncpbytes / sizeof *sbp; \
680 error = copyin(uap->name, ibits[x], ncpbytes); \
690 bzero(selbits
, nbufbytes
/ 2);
693 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
,
697 if (itimerfix(&atv
)) {
701 getmicrouptime(&rtv
);
702 timevaladd(&atv
, &rtv
);
710 p
->p_flag
|= P_SELECT
;
711 error
= selscan(p
, ibits
, obits
, uap
->nd
, &uap
->sysmsg_result
);
712 if (error
|| uap
->sysmsg_result
)
714 if (atv
.tv_sec
|| atv
.tv_usec
) {
715 getmicrouptime(&rtv
);
716 if (timevalcmp(&rtv
, &atv
, >=))
719 timevalsub(&ttv
, &rtv
);
720 timo
= ttv
.tv_sec
> 24 * 60 * 60 ?
721 24 * 60 * 60 * hz
: tvtohz_high(&ttv
);
724 if ((p
->p_flag
& P_SELECT
) == 0 || nselcoll
!= ncoll
) {
728 p
->p_flag
&= ~P_SELECT
;
730 error
= tsleep((caddr_t
)&selwait
, PCATCH
, "select", timo
);
736 p
->p_flag
&= ~P_SELECT
;
737 /* select is not restarted after signals... */
738 if (error
== ERESTART
)
740 if (error
== EWOULDBLOCK
)
742 #define putbits(name, x) \
743 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
753 if (selbits
!= &s_selbits
[0])
754 free(selbits
, M_SELECT
);
759 selscan(struct proc
*p
, fd_mask
**ibits
, fd_mask
**obits
, int nfd
, int *res
)
761 struct thread
*td
= p
->p_thread
;
762 struct filedesc
*fdp
= p
->p_fd
;
767 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
768 static int flag
[3] = { POLLRDNORM
, POLLWRNORM
, POLLRDBAND
};
770 for (msk
= 0; msk
< 3; msk
++) {
771 if (ibits
[msk
] == NULL
)
773 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
774 bits
= ibits
[msk
][i
/NFDBITS
];
775 /* ffs(int mask) not portable, fd_mask is long */
776 for (fd
= i
; bits
&& fd
< nfd
; fd
++, bits
>>= 1) {
779 fp
= fdp
->fd_ofiles
[fd
];
782 if (fo_poll(fp
, flag
[msk
], fp
->f_cred
, td
)) {
783 obits
[msk
][(fd
)/NFDBITS
] |=
784 ((fd_mask
)1 << ((fd
) % NFDBITS
));
798 poll(struct poll_args
*uap
)
801 struct pollfd smallbits
[32];
802 struct timeval atv
, rtv
, ttv
;
803 int ncoll
, error
= 0, timo
;
806 struct proc
*p
= curproc
;
810 * This is kinda bogus. We have fd limits, but that is not
811 * really related to the size of the pollfd array. Make sure
812 * we let the process use at least FD_SETSIZE entries and at
813 * least enough for the current limits. We want to be reasonably
814 * safe, but not overly restrictive.
816 if (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& nfds
> FD_SETSIZE
)
818 ni
= nfds
* sizeof(struct pollfd
);
819 if (ni
> sizeof(smallbits
))
820 bits
= malloc(ni
, M_TEMP
, M_WAITOK
);
823 error
= copyin(uap
->fds
, bits
, ni
);
826 if (uap
->timeout
!= INFTIM
) {
827 atv
.tv_sec
= uap
->timeout
/ 1000;
828 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
829 if (itimerfix(&atv
)) {
833 getmicrouptime(&rtv
);
834 timevaladd(&atv
, &rtv
);
842 p
->p_flag
|= P_SELECT
;
843 error
= pollscan(p
, bits
, nfds
, &uap
->sysmsg_result
);
844 if (error
|| uap
->sysmsg_result
)
846 if (atv
.tv_sec
|| atv
.tv_usec
) {
847 getmicrouptime(&rtv
);
848 if (timevalcmp(&rtv
, &atv
, >=))
851 timevalsub(&ttv
, &rtv
);
852 timo
= ttv
.tv_sec
> 24 * 60 * 60 ?
853 24 * 60 * 60 * hz
: tvtohz_high(&ttv
);
856 if ((p
->p_flag
& P_SELECT
) == 0 || nselcoll
!= ncoll
) {
860 p
->p_flag
&= ~P_SELECT
;
861 error
= tsleep((caddr_t
)&selwait
, PCATCH
, "poll", timo
);
866 p
->p_flag
&= ~P_SELECT
;
867 /* poll is not restarted after signals... */
868 if (error
== ERESTART
)
870 if (error
== EWOULDBLOCK
)
873 error
= copyout(bits
, uap
->fds
, ni
);
878 if (ni
> sizeof(smallbits
))
884 pollscan(struct proc
*p
, struct pollfd
*fds
, u_int nfd
, int *res
)
886 struct thread
*td
= p
->p_thread
;
887 struct filedesc
*fdp
= p
->p_fd
;
892 for (i
= 0; i
< nfd
; i
++, fds
++) {
893 if (fds
->fd
>= fdp
->fd_nfiles
) {
894 fds
->revents
= POLLNVAL
;
896 } else if (fds
->fd
< 0) {
899 fp
= fdp
->fd_ofiles
[fds
->fd
];
901 fds
->revents
= POLLNVAL
;
905 * Note: backend also returns POLLHUP and
906 * POLLERR if appropriate.
908 fds
->revents
= fo_poll(fp
, fds
->events
,
910 if (fds
->revents
!= 0)
920 * OpenBSD poll system call.
921 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
924 openbsd_poll(struct openbsd_poll_args
*uap
)
926 return (poll((struct poll_args
*)uap
));
931 seltrue(dev_t dev
, int events
, struct thread
*td
)
933 return (events
& (POLLIN
| POLLOUT
| POLLRDNORM
| POLLWRNORM
));
937 * Record a select request. A global wait must be used since a process/thread
938 * might go away after recording its request.
941 selrecord(struct thread
*selector
, struct selinfo
*sip
)
946 if ((p
= selector
->td_proc
) == NULL
)
947 panic("selrecord: thread needs a process");
950 if (sip
->si_pid
== mypid
)
952 if (sip
->si_pid
&& (p
= pfind(sip
->si_pid
)) &&
953 p
->p_wchan
== (caddr_t
)&selwait
) {
954 sip
->si_flags
|= SI_COLL
;
961 * Do a wakeup when a selectable event occurs.
964 selwakeup(struct selinfo
*sip
)
968 if (sip
->si_pid
== 0)
970 if (sip
->si_flags
& SI_COLL
) {
972 sip
->si_flags
&= ~SI_COLL
;
973 wakeup((caddr_t
)&selwait
); /* YYY fixable */
975 p
= pfind(sip
->si_pid
);
979 if (p
->p_wchan
== (caddr_t
)&selwait
) {
980 if (p
->p_stat
== SSLEEP
)
983 unsleep(p
->p_thread
);
984 } else if (p
->p_flag
& P_SELECT
)
985 p
->p_flag
&= ~P_SELECT
;