2002-08-26 03:15:21 +04:00
|
|
|
/* $NetBSD: sys_generic.c,v 1.64 2002/08/25 23:15:21 thorpej Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2002-08-26 03:15:21 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.64 2002/08/25 23:15:21 thorpej Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1998-06-26 01:17:15 +04:00
|
|
|
#include "opt_ktrace.h"
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/socketvar.h>
|
1996-02-04 05:15:01 +03:00
|
|
|
#include <sys/signalvar.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/malloc.h>
|
1996-09-07 16:40:22 +04:00
|
|
|
#include <sys/poll.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
1996-06-02 18:48:23 +04:00
|
|
|
int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
|
1996-09-07 16:40:22 +04:00
|
|
|
int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
|
1996-02-04 05:15:01 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* Read system call.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_read(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_read_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(void *) buf;
|
|
|
|
syscallarg(size_t) nbyte;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
int fd;
|
|
|
|
struct file *fp;
|
|
|
|
struct filedesc *fdp;
|
1998-06-30 11:39:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fd = SCARG(uap, fd);
|
|
|
|
fdp = p->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
|
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
|
|
|
return (EBADF);
|
|
|
|
|
|
|
|
if ((fp->f_flag & FREAD) == 0)
|
1998-06-30 11:39:22 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
|
|
|
/* dofileread() will unuse the descriptor for us */
|
1998-06-30 11:39:22 +04:00
|
|
|
return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
|
|
|
|
&fp->f_offset, FOF_UPDATE_OFFSET, retval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
dofileread(struct proc *p, int fd, struct file *fp, void *buf, size_t nbyte,
|
|
|
|
off_t *offset, int flags, register_t *retval)
|
1998-06-30 11:39:22 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
2002-08-26 03:15:21 +04:00
|
|
|
size_t cnt;
|
|
|
|
int error;
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
2001-02-27 00:58:30 +03:00
|
|
|
struct iovec ktriov;
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
2001-02-27 00:58:30 +03:00
|
|
|
error = 0;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-06-30 11:39:22 +04:00
|
|
|
aiov.iov_base = (caddr_t)buf;
|
|
|
|
aiov.iov_len = nbyte;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
1998-06-30 11:39:22 +04:00
|
|
|
auio.uio_resid = nbyte;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_segflg = UIO_USERSPACE;
|
|
|
|
auio.uio_procp = p;
|
1998-07-28 21:58:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads return ssize_t because -1 is returned on error. Therefore
|
|
|
|
* we must restrict the length to SSIZE_MAX to avoid garbage return
|
|
|
|
* values.
|
|
|
|
*/
|
1999-05-06 00:01:01 +04:00
|
|
|
if (auio.uio_resid > SSIZE_MAX) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-07-28 21:58:29 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* if tracing, save a copy of iovec
|
|
|
|
*/
|
|
|
|
if (KTRPOINT(p, KTR_GENIO))
|
|
|
|
ktriov = aiov;
|
|
|
|
#endif
|
1998-06-30 09:33:11 +04:00
|
|
|
cnt = auio.uio_resid;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error)
|
1994-06-29 10:29:24 +04:00
|
|
|
if (auio.uio_resid != cnt && (error == ERESTART ||
|
|
|
|
error == EINTR || error == EWOULDBLOCK))
|
|
|
|
error = 0;
|
|
|
|
cnt -= auio.uio_resid;
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(p, KTR_GENIO) && error == 0)
|
2000-05-27 04:40:29 +04:00
|
|
|
ktrgenio(p, fd, UIO_READ, &ktriov, cnt, error);
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
|
|
|
*retval = cnt;
|
1999-05-06 00:01:01 +04:00
|
|
|
out:
|
|
|
|
FILE_UNUSE(fp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scatter read system call.
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_readv(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_readv_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(const struct iovec *) iovp;
|
|
|
|
syscallarg(int) iovcnt;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
int fd;
|
|
|
|
struct file *fp;
|
|
|
|
struct filedesc *fdp;
|
1998-06-30 11:39:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fd = SCARG(uap, fd);
|
|
|
|
fdp = p->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
|
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
|
|
|
return (EBADF);
|
|
|
|
|
|
|
|
if ((fp->f_flag & FREAD) == 0)
|
1998-06-30 11:39:22 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
|
|
|
/* dofilereadv() will unuse the descriptor for us */
|
1998-06-30 11:39:22 +04:00
|
|
|
return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
|
|
|
|
&fp->f_offset, FOF_UPDATE_OFFSET, retval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
dofilereadv(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
|
|
|
|
int iovcnt, off_t *offset, int flags, register_t *retval)
|
1998-06-30 11:39:22 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
|
2002-08-26 03:15:21 +04:00
|
|
|
int i, error;
|
|
|
|
size_t cnt;
|
2001-02-27 00:58:30 +03:00
|
|
|
u_int iovlen;
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
2001-02-27 00:58:30 +03:00
|
|
|
struct iovec *ktriov;
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
error = 0;
|
|
|
|
#ifdef KTRACE
|
|
|
|
ktriov = NULL;
|
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
/* note: can't use iovlen until iovcnt is validated */
|
1998-08-01 02:50:48 +04:00
|
|
|
iovlen = iovcnt * sizeof(struct iovec);
|
1997-10-15 21:03:52 +04:00
|
|
|
if ((u_int)iovcnt > UIO_SMALLIOV) {
|
1999-05-06 00:01:01 +04:00
|
|
|
if ((u_int)iovcnt > IOV_MAX) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-08-03 00:42:03 +04:00
|
|
|
iov = malloc(iovlen, M_IOV, M_WAITOK);
|
1994-06-29 10:29:24 +04:00
|
|
|
needfree = iov;
|
1998-07-31 19:38:58 +04:00
|
|
|
} else if ((u_int)iovcnt > 0) {
|
1994-06-29 10:29:24 +04:00
|
|
|
iov = aiov;
|
|
|
|
needfree = NULL;
|
1999-05-06 00:01:01 +04:00
|
|
|
} else {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-07-31 19:38:58 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_iov = iov;
|
1997-10-15 21:03:52 +04:00
|
|
|
auio.uio_iovcnt = iovcnt;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_segflg = UIO_USERSPACE;
|
|
|
|
auio.uio_procp = p;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = copyin(iovp, iov, iovlen);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto done;
|
|
|
|
auio.uio_resid = 0;
|
1997-10-15 21:03:52 +04:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_resid += iov->iov_len;
|
1998-07-28 21:58:29 +04:00
|
|
|
/*
|
|
|
|
* Reads return ssize_t because -1 is returned on error.
|
|
|
|
* Therefore we must restrict the length to SSIZE_MAX to
|
|
|
|
* avoid garbage return values.
|
|
|
|
*/
|
|
|
|
if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
|
1994-06-29 10:29:24 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
iov++;
|
|
|
|
}
|
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* if tracing, save a copy of iovec
|
|
|
|
*/
|
|
|
|
if (KTRPOINT(p, KTR_GENIO)) {
|
2000-08-03 00:42:03 +04:00
|
|
|
ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
cnt = auio.uio_resid;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error)
|
1994-06-29 10:29:24 +04:00
|
|
|
if (auio.uio_resid != cnt && (error == ERESTART ||
|
|
|
|
error == EINTR || error == EWOULDBLOCK))
|
|
|
|
error = 0;
|
|
|
|
cnt -= auio.uio_resid;
|
|
|
|
#ifdef KTRACE
|
2001-10-30 16:37:21 +03:00
|
|
|
if (ktriov != NULL) {
|
|
|
|
if (error == 0)
|
2000-05-27 04:40:29 +04:00
|
|
|
ktrgenio(p, fd, UIO_READ, ktriov, cnt, error);
|
2000-08-03 00:42:03 +04:00
|
|
|
free(ktriov, M_TEMP);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*retval = cnt;
|
1999-05-06 00:01:01 +04:00
|
|
|
done:
|
1994-06-29 10:29:24 +04:00
|
|
|
if (needfree)
|
2000-08-03 00:42:03 +04:00
|
|
|
free(needfree, M_IOV);
|
1999-05-06 00:01:01 +04:00
|
|
|
out:
|
|
|
|
FILE_UNUSE(fp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write system call
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_write(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_write_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(const void *) buf;
|
|
|
|
syscallarg(size_t) nbyte;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
int fd;
|
|
|
|
struct file *fp;
|
|
|
|
struct filedesc *fdp;
|
1998-06-30 11:39:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fd = SCARG(uap, fd);
|
|
|
|
fdp = p->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
|
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
|
|
|
return (EBADF);
|
|
|
|
|
|
|
|
if ((fp->f_flag & FWRITE) == 0)
|
1998-06-30 11:39:22 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
|
|
|
/* dofilewrite() will unuse the descriptor for us */
|
1998-06-30 11:39:22 +04:00
|
|
|
return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
|
|
|
|
&fp->f_offset, FOF_UPDATE_OFFSET, retval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
dofilewrite(struct proc *p, int fd, struct file *fp, const void *buf,
|
|
|
|
size_t nbyte, off_t *offset, int flags, register_t *retval)
|
1998-06-30 11:39:22 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
2002-08-26 03:15:21 +04:00
|
|
|
size_t cnt;
|
|
|
|
int error;
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
2001-02-27 00:58:30 +03:00
|
|
|
struct iovec ktriov;
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
error = 0;
|
1998-06-30 11:39:22 +04:00
|
|
|
aiov.iov_base = (caddr_t)buf; /* XXX kills const */
|
|
|
|
aiov.iov_len = nbyte;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
1998-06-30 11:39:22 +04:00
|
|
|
auio.uio_resid = nbyte;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_segflg = UIO_USERSPACE;
|
|
|
|
auio.uio_procp = p;
|
1998-07-28 21:58:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Writes return ssize_t because -1 is returned on error. Therefore
|
|
|
|
* we must restrict the length to SSIZE_MAX to avoid garbage return
|
|
|
|
* values.
|
|
|
|
*/
|
1999-05-06 00:01:01 +04:00
|
|
|
if (auio.uio_resid > SSIZE_MAX) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-07-28 21:58:29 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* if tracing, save a copy of iovec
|
|
|
|
*/
|
|
|
|
if (KTRPOINT(p, KTR_GENIO))
|
|
|
|
ktriov = aiov;
|
|
|
|
#endif
|
1998-06-30 09:33:11 +04:00
|
|
|
cnt = auio.uio_resid;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error) {
|
1994-06-29 10:29:24 +04:00
|
|
|
if (auio.uio_resid != cnt && (error == ERESTART ||
|
|
|
|
error == EINTR || error == EWOULDBLOCK))
|
|
|
|
error = 0;
|
|
|
|
if (error == EPIPE)
|
|
|
|
psignal(p, SIGPIPE);
|
|
|
|
}
|
|
|
|
cnt -= auio.uio_resid;
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(p, KTR_GENIO) && error == 0)
|
2000-05-27 04:40:29 +04:00
|
|
|
ktrgenio(p, fd, UIO_WRITE, &ktriov, cnt, error);
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
|
|
|
*retval = cnt;
|
1999-05-06 00:01:01 +04:00
|
|
|
out:
|
|
|
|
FILE_UNUSE(fp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gather write system call
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_writev(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_writev_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(const struct iovec *) iovp;
|
|
|
|
syscallarg(int) iovcnt;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
int fd;
|
|
|
|
struct file *fp;
|
|
|
|
struct filedesc *fdp;
|
1998-06-30 11:39:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fd = SCARG(uap, fd);
|
|
|
|
fdp = p->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
|
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
|
|
|
return (EBADF);
|
|
|
|
|
|
|
|
if ((fp->f_flag & FWRITE) == 0)
|
1998-06-30 11:39:22 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
|
|
|
/* dofilewritev() will unuse the descriptor for us */
|
1998-06-30 11:39:22 +04:00
|
|
|
return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
|
|
|
|
&fp->f_offset, FOF_UPDATE_OFFSET, retval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
dofilewritev(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
|
|
|
|
int iovcnt, off_t *offset, int flags, register_t *retval)
|
1998-06-30 11:39:22 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
|
2002-08-26 03:15:21 +04:00
|
|
|
int i, error;
|
|
|
|
size_t cnt;
|
2001-02-27 00:58:30 +03:00
|
|
|
u_int iovlen;
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef KTRACE
|
2001-02-27 00:58:30 +03:00
|
|
|
struct iovec *ktriov;
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
error = 0;
|
|
|
|
#ifdef KTRACE
|
|
|
|
ktriov = NULL;
|
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
/* note: can't use iovlen until iovcnt is validated */
|
1998-08-01 02:50:48 +04:00
|
|
|
iovlen = iovcnt * sizeof(struct iovec);
|
1997-10-15 21:03:52 +04:00
|
|
|
if ((u_int)iovcnt > UIO_SMALLIOV) {
|
2002-03-22 21:58:59 +03:00
|
|
|
if ((u_int)iovcnt > IOV_MAX) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-08-03 00:42:03 +04:00
|
|
|
iov = malloc(iovlen, M_IOV, M_WAITOK);
|
1994-06-29 10:29:24 +04:00
|
|
|
needfree = iov;
|
1998-07-31 19:38:58 +04:00
|
|
|
} else if ((u_int)iovcnt > 0) {
|
1994-06-29 10:29:24 +04:00
|
|
|
iov = aiov;
|
|
|
|
needfree = NULL;
|
1999-05-06 00:01:01 +04:00
|
|
|
} else {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-07-31 19:38:58 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_iov = iov;
|
1997-10-15 21:03:52 +04:00
|
|
|
auio.uio_iovcnt = iovcnt;
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_segflg = UIO_USERSPACE;
|
|
|
|
auio.uio_procp = p;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = copyin(iovp, iov, iovlen);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto done;
|
|
|
|
auio.uio_resid = 0;
|
1997-10-15 21:03:52 +04:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
1994-06-29 10:29:24 +04:00
|
|
|
auio.uio_resid += iov->iov_len;
|
1998-07-28 21:58:29 +04:00
|
|
|
/*
|
|
|
|
* Writes return ssize_t because -1 is returned on error.
|
|
|
|
* Therefore we must restrict the length to SSIZE_MAX to
|
|
|
|
* avoid garbage return values.
|
|
|
|
*/
|
|
|
|
if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
|
1994-06-29 10:29:24 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
iov++;
|
|
|
|
}
|
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* if tracing, save a copy of iovec
|
|
|
|
*/
|
|
|
|
if (KTRPOINT(p, KTR_GENIO)) {
|
2000-08-03 00:42:03 +04:00
|
|
|
ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
cnt = auio.uio_resid;
|
1998-06-30 11:39:22 +04:00
|
|
|
error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error) {
|
1994-06-29 10:29:24 +04:00
|
|
|
if (auio.uio_resid != cnt && (error == ERESTART ||
|
|
|
|
error == EINTR || error == EWOULDBLOCK))
|
|
|
|
error = 0;
|
|
|
|
if (error == EPIPE)
|
|
|
|
psignal(p, SIGPIPE);
|
|
|
|
}
|
|
|
|
cnt -= auio.uio_resid;
|
|
|
|
#ifdef KTRACE
|
1997-10-15 21:03:52 +04:00
|
|
|
if (KTRPOINT(p, KTR_GENIO))
|
|
|
|
if (error == 0) {
|
2000-05-27 04:40:29 +04:00
|
|
|
ktrgenio(p, fd, UIO_WRITE, ktriov, cnt, error);
|
2000-08-03 00:42:03 +04:00
|
|
|
free(ktriov, M_TEMP);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*retval = cnt;
|
1999-05-06 00:01:01 +04:00
|
|
|
done:
|
1994-06-29 10:29:24 +04:00
|
|
|
if (needfree)
|
2000-08-03 00:42:03 +04:00
|
|
|
free(needfree, M_IOV);
|
1999-05-06 00:01:01 +04:00
|
|
|
out:
|
|
|
|
FILE_UNUSE(fp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ioctl system call
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_ioctl(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_ioctl_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(u_long) com;
|
|
|
|
syscallarg(caddr_t) data;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
struct file *fp;
|
|
|
|
struct filedesc *fdp;
|
|
|
|
u_long com;
|
|
|
|
int error;
|
|
|
|
u_int size;
|
|
|
|
caddr_t data, memp;
|
|
|
|
int tmp;
|
|
|
|
#define STK_PARAMS 128
|
|
|
|
u_long stkbuf[STK_PARAMS/sizeof(u_long)];
|
|
|
|
|
|
|
|
error = 0;
|
1994-06-29 10:29:24 +04:00
|
|
|
fdp = p->p_fd;
|
2001-06-15 00:32:41 +04:00
|
|
|
|
|
|
|
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
|
1994-06-29 10:29:24 +04:00
|
|
|
return (EBADF);
|
|
|
|
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
|
|
|
|
|
|
|
if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
|
|
|
|
error = EBADF;
|
|
|
|
goto out;
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
switch (com = SCARG(uap, com)) {
|
1994-06-29 10:29:24 +04:00
|
|
|
case FIONCLEX:
|
1994-10-20 07:22:35 +03:00
|
|
|
fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
|
1999-05-06 00:01:01 +04:00
|
|
|
goto out;
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
case FIOCLEX:
|
1994-10-20 07:22:35 +03:00
|
|
|
fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
|
1999-05-06 00:01:01 +04:00
|
|
|
goto out;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpret high order word to find amount of data to be
|
|
|
|
* copied to/from the user's address space.
|
|
|
|
*/
|
|
|
|
size = IOCPARM_LEN(com);
|
1999-05-06 00:01:01 +04:00
|
|
|
if (size > IOCPARM_MAX) {
|
|
|
|
error = ENOTTY;
|
|
|
|
goto out;
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
memp = NULL;
|
1998-08-01 02:50:48 +04:00
|
|
|
if (size > sizeof(stkbuf)) {
|
1994-06-29 10:29:24 +04:00
|
|
|
memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
|
|
|
|
data = memp;
|
|
|
|
} else
|
2000-03-17 03:01:48 +03:00
|
|
|
data = (caddr_t)stkbuf;
|
1994-06-29 10:29:24 +04:00
|
|
|
if (com&IOC_IN) {
|
|
|
|
if (size) {
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyin(SCARG(uap, data), data, size);
|
1994-06-29 10:29:24 +04:00
|
|
|
if (error) {
|
|
|
|
if (memp)
|
|
|
|
free(memp, M_IOCTLOPS);
|
1999-05-06 00:01:01 +04:00
|
|
|
goto out;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
} else
|
1994-10-20 07:22:35 +03:00
|
|
|
*(caddr_t *)data = SCARG(uap, data);
|
1994-06-29 10:29:24 +04:00
|
|
|
} else if ((com&IOC_OUT) && size)
|
|
|
|
/*
|
|
|
|
* Zero the buffer so the user always
|
|
|
|
* gets back something deterministic.
|
|
|
|
*/
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(data, 0, size);
|
1994-06-29 10:29:24 +04:00
|
|
|
else if (com&IOC_VOID)
|
1994-10-20 07:22:35 +03:00
|
|
|
*(caddr_t *)data = SCARG(uap, data);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
switch (com) {
|
|
|
|
|
|
|
|
case FIONBIO:
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((tmp = *(int *)data) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
fp->f_flag |= FNONBLOCK;
|
|
|
|
else
|
|
|
|
fp->f_flag &= ~FNONBLOCK;
|
|
|
|
error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOASYNC:
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((tmp = *(int *)data) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
fp->f_flag |= FASYNC;
|
|
|
|
else
|
|
|
|
fp->f_flag &= ~FASYNC;
|
|
|
|
error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOSETOWN:
|
|
|
|
tmp = *(int *)data;
|
|
|
|
if (fp->f_type == DTYPE_SOCKET) {
|
|
|
|
((struct socket *)fp->f_data)->so_pgid = tmp;
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tmp <= 0) {
|
|
|
|
tmp = -tmp;
|
|
|
|
} else {
|
|
|
|
struct proc *p1 = pfind(tmp);
|
|
|
|
if (p1 == 0) {
|
|
|
|
error = ESRCH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = p1->p_pgrp->pg_id;
|
|
|
|
}
|
|
|
|
error = (*fp->f_ops->fo_ioctl)
|
1996-03-29 03:25:30 +03:00
|
|
|
(fp, TIOCSPGRP, (caddr_t)&tmp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOGETOWN:
|
|
|
|
if (fp->f_type == DTYPE_SOCKET) {
|
|
|
|
error = 0;
|
|
|
|
*(int *)data = ((struct socket *)fp->f_data)->so_pgid;
|
|
|
|
break;
|
|
|
|
}
|
1994-10-31 00:43:03 +03:00
|
|
|
error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
|
2001-05-24 10:52:43 +04:00
|
|
|
if (error == 0)
|
|
|
|
*(int *)data = -*(int *)data;
|
1994-06-29 10:29:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
|
|
|
|
/*
|
|
|
|
* Copy any data to user, size was
|
|
|
|
* already set and checked above.
|
|
|
|
*/
|
|
|
|
if (error == 0 && (com&IOC_OUT) && size)
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyout(data, SCARG(uap, data), size);
|
1994-06-29 10:29:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (memp)
|
|
|
|
free(memp, M_IOCTLOPS);
|
1999-05-06 00:01:01 +04:00
|
|
|
out:
|
|
|
|
FILE_UNUSE(fp, p);
|
2002-03-17 22:40:26 +03:00
|
|
|
switch (error) {
|
|
|
|
case -1:
|
|
|
|
printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
|
|
|
|
"pid=%d comm=%s\n",
|
|
|
|
(com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
|
|
|
|
(char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
|
|
|
|
p->p_pid, p->p_comm);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case EPASSTHROUGH:
|
|
|
|
error = ENOTTY;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
return (error);
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int selwait, nselcoll;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select system call.
|
|
|
|
*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_select(struct proc *p, void *v, register_t *retval)
|
1995-09-20 01:40:36 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_select_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(int) nd;
|
|
|
|
syscallarg(fd_set *) in;
|
|
|
|
syscallarg(fd_set *) ou;
|
|
|
|
syscallarg(fd_set *) ex;
|
|
|
|
syscallarg(struct timeval *) tv;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
caddr_t bits;
|
|
|
|
char smallbits[howmany(FD_SETSIZE, NFDBITS) *
|
|
|
|
sizeof(fd_mask) * 6];
|
|
|
|
struct timeval atv;
|
|
|
|
int s, ncoll, error, timo;
|
|
|
|
size_t ni;
|
|
|
|
|
|
|
|
error = 0;
|
1997-11-05 00:24:14 +03:00
|
|
|
if (SCARG(uap, nd) < 0)
|
|
|
|
return (EINVAL);
|
1994-10-20 07:22:35 +03:00
|
|
|
if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
|
|
|
|
/* forgiving; slightly wrong */
|
|
|
|
SCARG(uap, nd) = p->p_fd->fd_nfiles;
|
|
|
|
}
|
|
|
|
ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
|
1996-09-05 19:32:52 +04:00
|
|
|
if (ni * 6 > sizeof(smallbits))
|
1996-06-02 18:48:23 +04:00
|
|
|
bits = malloc(ni * 6, M_TEMP, M_WAITOK);
|
|
|
|
else
|
1996-06-13 09:08:47 +04:00
|
|
|
bits = smallbits;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
#define getbits(name, x) \
|
|
|
|
if (SCARG(uap, name)) { \
|
|
|
|
error = copyin(SCARG(uap, name), bits + ni * x, ni); \
|
|
|
|
if (error) \
|
|
|
|
goto done; \
|
|
|
|
} else \
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(bits + ni * x, 0, ni);
|
1994-06-29 10:29:24 +04:00
|
|
|
getbits(in, 0);
|
|
|
|
getbits(ou, 1);
|
|
|
|
getbits(ex, 2);
|
|
|
|
#undef getbits
|
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
if (SCARG(uap, tv)) {
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyin(SCARG(uap, tv), (caddr_t)&atv,
|
1998-08-01 02:50:48 +04:00
|
|
|
sizeof(atv));
|
1994-06-29 10:29:24 +04:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
if (itimerfix(&atv)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
s = splclock();
|
1995-03-21 16:33:34 +03:00
|
|
|
timeradd(&atv, &time, &atv);
|
1994-06-29 10:29:24 +04:00
|
|
|
splx(s);
|
|
|
|
} else
|
|
|
|
timo = 0;
|
2001-02-27 00:58:30 +03:00
|
|
|
retry:
|
1994-06-29 10:29:24 +04:00
|
|
|
ncoll = nselcoll;
|
|
|
|
p->p_flag |= P_SELECT;
|
1996-06-02 18:48:23 +04:00
|
|
|
error = selscan(p, (fd_mask *)(bits + ni * 0),
|
|
|
|
(fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
|
1994-06-29 10:29:24 +04:00
|
|
|
if (error || *retval)
|
|
|
|
goto done;
|
2000-07-13 05:32:33 +04:00
|
|
|
if (SCARG(uap, tv)) {
|
|
|
|
/*
|
|
|
|
* We have to recalculate the timeout on every retry.
|
|
|
|
*/
|
|
|
|
timo = hzto(&atv);
|
|
|
|
if (timo <= 0)
|
|
|
|
goto done;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2000-08-21 10:27:59 +04:00
|
|
|
s = splsched();
|
1994-06-29 10:29:24 +04:00
|
|
|
if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
|
|
|
|
splx(s);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
p->p_flag &= ~P_SELECT;
|
|
|
|
error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
|
|
|
|
splx(s);
|
|
|
|
if (error == 0)
|
|
|
|
goto retry;
|
2001-02-27 00:58:30 +03:00
|
|
|
done:
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_flag &= ~P_SELECT;
|
|
|
|
/* select is not restarted after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
error = EINTR;
|
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = 0;
|
|
|
|
if (error == 0) {
|
2001-02-27 00:58:30 +03:00
|
|
|
|
|
|
|
#define putbits(name, x) \
|
|
|
|
if (SCARG(uap, name)) { \
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyout(bits + ni * x, SCARG(uap, name), ni); \
|
2001-02-27 00:58:30 +03:00
|
|
|
if (error) \
|
|
|
|
goto out; \
|
1996-09-05 19:32:52 +04:00
|
|
|
}
|
1996-06-02 18:48:23 +04:00
|
|
|
putbits(in, 3);
|
|
|
|
putbits(ou, 4);
|
|
|
|
putbits(ex, 5);
|
1994-06-29 10:29:24 +04:00
|
|
|
#undef putbits
|
|
|
|
}
|
2001-02-27 00:58:30 +03:00
|
|
|
out:
|
1996-09-05 19:32:52 +04:00
|
|
|
if (ni * 6 > sizeof(smallbits))
|
1996-06-02 18:48:23 +04:00
|
|
|
free(bits, M_TEMP);
|
1994-06-29 10:29:24 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
selscan(struct proc *p, fd_mask *ibitp, fd_mask *obitp, int nfd,
|
|
|
|
register_t *retval)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct filedesc *fdp;
|
|
|
|
int msk, i, j, fd, n;
|
|
|
|
fd_mask ibits, obits;
|
|
|
|
struct file *fp;
|
2002-08-09 00:40:25 +04:00
|
|
|
static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
|
1996-09-07 16:40:22 +04:00
|
|
|
POLLWRNORM | POLLHUP | POLLERR,
|
|
|
|
POLLRDBAND };
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fdp = p->p_fd;
|
|
|
|
n = 0;
|
1994-06-29 10:29:24 +04:00
|
|
|
for (msk = 0; msk < 3; msk++) {
|
|
|
|
for (i = 0; i < nfd; i += NFDBITS) {
|
1996-06-02 18:48:23 +04:00
|
|
|
ibits = *ibitp++;
|
|
|
|
obits = 0;
|
|
|
|
while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
|
|
|
|
ibits &= ~(1 << j);
|
2001-06-15 00:32:41 +04:00
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
1994-06-29 10:29:24 +04:00
|
|
|
return (EBADF);
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
1996-09-07 16:40:22 +04:00
|
|
|
if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
|
1996-06-02 18:48:23 +04:00
|
|
|
obits |= (1 << j);
|
1994-06-29 10:29:24 +04:00
|
|
|
n++;
|
|
|
|
}
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_UNUSE(fp, p);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
1996-06-02 18:48:23 +04:00
|
|
|
*obitp++ = obits;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*retval = n;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1996-09-07 16:40:22 +04:00
|
|
|
/*
|
|
|
|
* Poll system call.
|
|
|
|
*/
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
sys_poll(struct proc *p, void *v, register_t *retval)
|
1996-09-07 16:40:22 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_poll_args /* {
|
2001-02-27 00:58:30 +03:00
|
|
|
syscallarg(struct pollfd *) fds;
|
|
|
|
syscallarg(u_int) nfds;
|
|
|
|
syscallarg(int) timeout;
|
1996-09-07 16:40:22 +04:00
|
|
|
} */ *uap = v;
|
2001-02-27 00:58:30 +03:00
|
|
|
caddr_t bits;
|
|
|
|
char smallbits[32 * sizeof(struct pollfd)];
|
|
|
|
struct timeval atv;
|
|
|
|
int s, ncoll, error, timo;
|
|
|
|
size_t ni;
|
1996-09-07 16:40:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
error = 0;
|
1996-09-07 16:40:22 +04:00
|
|
|
if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
|
|
|
|
/* forgiving; slightly wrong */
|
|
|
|
SCARG(uap, nfds) = p->p_fd->fd_nfiles;
|
|
|
|
}
|
|
|
|
ni = SCARG(uap, nfds) * sizeof(struct pollfd);
|
|
|
|
if (ni > sizeof(smallbits))
|
|
|
|
bits = malloc(ni, M_TEMP, M_WAITOK);
|
|
|
|
else
|
|
|
|
bits = smallbits;
|
|
|
|
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyin(SCARG(uap, fds), bits, ni);
|
1996-09-07 16:40:22 +04:00
|
|
|
if (error)
|
|
|
|
goto done;
|
|
|
|
|
1996-09-08 01:47:23 +04:00
|
|
|
if (SCARG(uap, timeout) != INFTIM) {
|
1996-09-07 16:40:22 +04:00
|
|
|
atv.tv_sec = SCARG(uap, timeout) / 1000;
|
|
|
|
atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
|
|
|
|
if (itimerfix(&atv)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
s = splclock();
|
|
|
|
timeradd(&atv, &time, &atv);
|
|
|
|
splx(s);
|
|
|
|
} else
|
|
|
|
timo = 0;
|
2001-02-27 00:58:30 +03:00
|
|
|
retry:
|
1996-09-07 16:40:22 +04:00
|
|
|
ncoll = nselcoll;
|
|
|
|
p->p_flag |= P_SELECT;
|
|
|
|
error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
|
|
|
|
if (error || *retval)
|
|
|
|
goto done;
|
2000-07-13 05:32:33 +04:00
|
|
|
if (SCARG(uap, timeout) != INFTIM) {
|
|
|
|
/*
|
|
|
|
* We have to recalculate the timeout on every retry.
|
|
|
|
*/
|
|
|
|
timo = hzto(&atv);
|
|
|
|
if (timo <= 0)
|
|
|
|
goto done;
|
1996-09-07 16:40:22 +04:00
|
|
|
}
|
2000-08-21 10:27:59 +04:00
|
|
|
s = splsched();
|
1996-09-07 16:40:22 +04:00
|
|
|
if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
|
|
|
|
splx(s);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
p->p_flag &= ~P_SELECT;
|
|
|
|
error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
|
|
|
|
splx(s);
|
|
|
|
if (error == 0)
|
|
|
|
goto retry;
|
2001-02-27 00:58:30 +03:00
|
|
|
done:
|
1996-09-07 16:40:22 +04:00
|
|
|
p->p_flag &= ~P_SELECT;
|
|
|
|
/* poll is not restarted after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
error = EINTR;
|
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = 0;
|
|
|
|
if (error == 0) {
|
1996-12-22 13:21:06 +03:00
|
|
|
error = copyout(bits, SCARG(uap, fds), ni);
|
1996-09-07 16:40:22 +04:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
2001-02-27 00:58:30 +03:00
|
|
|
out:
|
1996-09-07 16:40:22 +04:00
|
|
|
if (ni > sizeof(smallbits))
|
|
|
|
free(bits, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
pollscan(struct proc *p, struct pollfd *fds, int nfd, register_t *retval)
|
1996-09-07 16:40:22 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct filedesc *fdp;
|
|
|
|
int i, n;
|
|
|
|
struct file *fp;
|
1996-09-07 16:40:22 +04:00
|
|
|
|
2001-02-27 00:58:30 +03:00
|
|
|
fdp = p->p_fd;
|
2001-02-27 07:44:51 +03:00
|
|
|
n = 0;
|
1996-09-07 16:40:22 +04:00
|
|
|
for (i = 0; i < nfd; i++, fds++) {
|
2001-11-14 21:43:58 +03:00
|
|
|
if (fds->fd >= fdp->fd_nfiles) {
|
1996-09-07 16:40:22 +04:00
|
|
|
fds->revents = POLLNVAL;
|
|
|
|
n++;
|
2001-11-14 21:43:58 +03:00
|
|
|
} else if (fds->fd < 0) {
|
|
|
|
fds->revents = 0;
|
1996-09-07 16:40:22 +04:00
|
|
|
} else {
|
2001-06-15 00:32:41 +04:00
|
|
|
if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
|
1997-02-18 07:20:54 +03:00
|
|
|
fds->revents = POLLNVAL;
|
1996-09-07 16:40:22 +04:00
|
|
|
n++;
|
1997-02-18 07:20:54 +03:00
|
|
|
} else {
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_USE(fp);
|
1997-02-18 07:20:54 +03:00
|
|
|
fds->revents = (*fp->f_ops->fo_poll)(fp,
|
|
|
|
fds->events | POLLERR | POLLHUP, p);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
n++;
|
1999-05-06 00:01:01 +04:00
|
|
|
FILE_UNUSE(fp, p);
|
1997-02-18 07:20:54 +03:00
|
|
|
}
|
1996-09-07 16:40:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*retval = n;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*ARGSUSED*/
|
1996-02-04 05:15:01 +03:00
|
|
|
int
|
2001-02-27 00:58:30 +03:00
|
|
|
seltrue(dev_t dev, int events, struct proc *p)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
|
|
|
|
1996-09-07 16:40:22 +04:00
|
|
|
return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record a select request.
|
|
|
|
*/
|
|
|
|
void
|
2001-02-27 00:58:30 +03:00
|
|
|
selrecord(struct proc *selector, struct selinfo *sip)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2001-02-27 00:58:30 +03:00
|
|
|
struct proc *p;
|
|
|
|
pid_t mypid;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
mypid = selector->p_pid;
|
|
|
|
if (sip->si_pid == mypid)
|
|
|
|
return;
|
|
|
|
if (sip->si_pid && (p = pfind(sip->si_pid)) &&
|
|
|
|
p->p_wchan == (caddr_t)&selwait)
|
|
|
|
sip->si_flags |= SI_COLL;
|
2001-06-28 09:45:49 +04:00
|
|
|
else {
|
|
|
|
sip->si_flags &= ~SI_COLL;
|
1994-06-29 10:29:24 +04:00
|
|
|
sip->si_pid = mypid;
|
2001-06-28 09:45:49 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a wakeup when a selectable event occurs.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
selwakeup(sip)
|
2000-03-30 13:27:11 +04:00
|
|
|
struct selinfo *sip;
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct proc *p;
|
1994-06-29 10:29:24 +04:00
|
|
|
int s;
|
|
|
|
|
|
|
|
if (sip->si_pid == 0)
|
|
|
|
return;
|
|
|
|
if (sip->si_flags & SI_COLL) {
|
|
|
|
nselcoll++;
|
|
|
|
sip->si_flags &= ~SI_COLL;
|
|
|
|
wakeup((caddr_t)&selwait);
|
|
|
|
}
|
|
|
|
p = pfind(sip->si_pid);
|
|
|
|
sip->si_pid = 0;
|
|
|
|
if (p != NULL) {
|
2000-08-21 01:50:06 +04:00
|
|
|
SCHED_LOCK(s);
|
1994-06-29 10:29:24 +04:00
|
|
|
if (p->p_wchan == (caddr_t)&selwait) {
|
|
|
|
if (p->p_stat == SSLEEP)
|
|
|
|
setrunnable(p);
|
|
|
|
else
|
|
|
|
unsleep(p);
|
|
|
|
} else if (p->p_flag & P_SELECT)
|
|
|
|
p->p_flag &= ~P_SELECT;
|
2000-08-21 01:50:06 +04:00
|
|
|
SCHED_UNLOCK(s);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
}
|