From 4051c3a3c78e23cd366609d7ebdcc994bc1f08b2 Mon Sep 17 00:00:00 2001 From: mpi Date: Wed, 6 May 2015 08:52:17 +0000 Subject: [PATCH] Pass a thread pointer instead of its file descriptor table to getsock(9). Diff from Vitaliy Makkoveev. Manpage tweak and ok millert@ --- share/man/man9/file.9 | 29 ++++++++++++++++++----------- sys/compat/linux/linux_socket.c | 4 ++-- sys/kern/subr_log.c | 4 ++-- sys/kern/uipc_socket.c | 4 ++-- sys/kern/uipc_syscalls.c | 28 ++++++++++++++-------------- sys/nfs/nfs_syscalls.c | 4 ++-- sys/sys/filedesc.h | 4 ++-- 7 files changed, 42 insertions(+), 35 deletions(-) diff --git a/share/man/man9/file.9 b/share/man/man9/file.9 index 7ff402aa4b6..0fdad047692 100644 --- a/share/man/man9/file.9 +++ b/share/man/man9/file.9 @@ -1,4 +1,4 @@ -.\" $OpenBSD: file.9,v 1.13 2015/04/30 11:46:16 millert Exp $ +.\" $OpenBSD: file.9,v 1.14 2015/05/06 08:52:17 mpi Exp $ .\" .\" Copyright (c) 2002 Artur Grabowski .\" All rights reserved. @@ -22,7 +22,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: April 30 2015 $ +.Dd $Mdocdate: May 6 2015 $ .Dt FILE 9 .Os .Sh NAME @@ -42,7 +42,7 @@ .Ft struct file * .Fn fd_getfile "struct filedesc *fdp" "int fd" .Ft int -.Fn getsock "struct filedesc *fdp" "int fd" "struct file **fpp" +.Fn getsock "struct proc *p" "int fd" "struct file **fpp" .In sys/file.h .In sys/filedesc.h .In sys/vnode.h @@ -74,21 +74,28 @@ recommended to make complicated kernel APIs that require it. .Pp The files are extracted from the file descriptor table using the functions -.Fn fd_getfile , -.Fn getvnode +.Fn fd_getfile and -.Fn getsock . +.Fn getvnode . .Fn fd_getfile performs all necessary checks to see if the file descriptor number is within the range of file descriptor table, and if the descriptor is valid. -.Fn getsock -and .Fn getvnode -are special cases that besides doing +is a special case that besides doing +.Fn fd_getfile +also checks if the descriptor is a vnode, returns the proper +errno on error and increases the use count with +.Fn FREF . +.Pp +The files are extracted from the process context using the +function +.Fn getsock . +.Fn getsock +is a special case that besides doing .Fn fd_getfile -also check if the descriptor is a vnode or socket, return the proper -errno on error and increase the use count with +also checks if the descriptor is a socket, returns the proper +errno on error and increases the use count with .Fn FREF . .Sh CONCURRENT ACCESS Since multiple processes can share the same file descriptor table, diff --git a/sys/compat/linux/linux_socket.c b/sys/compat/linux/linux_socket.c index 1c16aab6a5b..1777f1fd43b 100644 --- a/sys/compat/linux/linux_socket.c +++ b/sys/compat/linux/linux_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: linux_socket.c,v 1.60 2015/01/30 23:38:49 millert Exp $ */ +/* $OpenBSD: linux_socket.c,v 1.61 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: linux_socket.c,v 1.14 1996/04/05 00:01:50 christos Exp $ */ /* @@ -937,7 +937,7 @@ linux_setsockopt(p, v, retval) if ((error = copyin((caddr_t) uap, (caddr_t) &lsa, sizeof lsa))) return error; - if ((error = getsock(p->p_fd, lsa.s, &fp)) != 0) + if ((error = getsock(p, lsa.s, &fp)) != 0) return error; level = linux_to_bsd_sopt_level(lsa.level); diff --git a/sys/kern/subr_log.c b/sys/kern/subr_log.c index b8c257b7ec0..db8252eb209 100644 --- a/sys/kern/subr_log.c +++ b/sys/kern/subr_log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr_log.c,v 1.29 2015/03/14 03:38:50 jsg Exp $ */ +/* $OpenBSD: subr_log.c,v 1.30 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $ */ /* @@ -334,7 +334,7 @@ logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p) case LIOCSFD: if ((error = suser(p, 0)) != 0) return (error); - if ((error = getsock(p->p_fd, *(int *)data, &fp)) != 0) + if ((error = getsock(p, *(int *)data, &fp)) != 0) return (error); if (syslogf) FRELE(syslogf, p); diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c index 08d8e061a8b..b37d55d8f34 100644 --- a/sys/kern/uipc_socket.c +++ b/sys/kern/uipc_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_socket.c,v 1.137 2015/03/14 03:38:51 jsg Exp $ */ +/* $OpenBSD: uipc_socket.c,v 1.138 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $ */ /* @@ -1076,7 +1076,7 @@ sosplice(struct socket *so, int fd, off_t max, struct timeval *tv) return (EINVAL); /* Find sosp, the drain socket where data will be spliced into. */ - if ((error = getsock(curproc->p_fd, fd, &fp)) != 0) + if ((error = getsock(curproc, fd, &fp)) != 0) return (error); sosp = fp->f_data; if (sosp->so_sp == NULL) diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c index 6795fd42f78..25f48e51347 100644 --- a/sys/kern/uipc_syscalls.c +++ b/sys/kern/uipc_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_syscalls.c,v 1.100 2015/03/14 03:38:51 jsg Exp $ */ +/* $OpenBSD: uipc_syscalls.c,v 1.101 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: uipc_syscalls.c,v 1.19 1996/02/09 19:00:48 christos Exp $ */ /* @@ -120,7 +120,7 @@ sys_bind(struct proc *p, void *v, register_t *retval) struct mbuf *nam; int error; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen), MT_SONAME); @@ -147,7 +147,7 @@ sys_listen(struct proc *p, void *v, register_t *retval) struct file *fp; int error; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); error = solisten(fp->f_data, SCARG(uap, backlog)); FRELE(fp, p); @@ -198,7 +198,7 @@ doaccept(struct proc *p, int sock, struct sockaddr *name, socklen_t *anamelen, if (name && (error = copyin(anamelen, &namelen, sizeof (namelen)))) return (error); - if ((error = getsock(fdp, sock, &fp)) != 0) + if ((error = getsock(p, sock, &fp)) != 0) return (error); headfp = fp; s = splsoftnet(); @@ -315,7 +315,7 @@ sys_connect(struct proc *p, void *v, register_t *retval) struct mbuf *nam = NULL; int error, s; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); so = fp->f_data; if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { @@ -518,7 +518,7 @@ sendit(struct proc *p, int s, struct msghdr *mp, int flags, register_t *retsize) to = NULL; - if ((error = getsock(p->p_fd, s, &fp)) != 0) + if ((error = getsock(p, s, &fp)) != 0) return (error); auio.uio_iov = mp->msg_iov; auio.uio_iovcnt = mp->msg_iovlen; @@ -684,7 +684,7 @@ recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp, int iovlen = 0; #endif - if ((error = getsock(p->p_fd, s, &fp)) != 0) + if ((error = getsock(p, s, &fp)) != 0) return (error); auio.uio_iov = mp->msg_iov; auio.uio_iovcnt = mp->msg_iovlen; @@ -803,7 +803,7 @@ sys_shutdown(struct proc *p, void *v, register_t *retval) struct file *fp; int error; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); error = soshutdown(fp->f_data, SCARG(uap, how)); FRELE(fp, p); @@ -825,7 +825,7 @@ sys_setsockopt(struct proc *p, void *v, register_t *retval) struct mbuf *m = NULL; int error; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); if (SCARG(uap, valsize) > MCLBYTES) { error = EINVAL; @@ -876,7 +876,7 @@ sys_getsockopt(struct proc *p, void *v, register_t *retval) socklen_t valsize; int error; - if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, s), &fp)) != 0) return (error); if (SCARG(uap, val)) { error = copyin(SCARG(uap, avalsize), @@ -920,7 +920,7 @@ sys_getsockname(struct proc *p, void *v, register_t *retval) socklen_t len; int error; - if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, fdes), &fp)) != 0) return (error); error = copyin(SCARG(uap, alen), &len, sizeof (len)); if (error) @@ -956,7 +956,7 @@ sys_getpeername(struct proc *p, void *v, register_t *retval) socklen_t len; int error; - if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0) + if ((error = getsock(p, SCARG(uap, fdes), &fp)) != 0) return (error); so = fp->f_data; if ((so->so_state & SS_ISCONNECTED) == 0) { @@ -1016,11 +1016,11 @@ sockargs(struct mbuf **mp, const void *buf, size_t buflen, int type) } int -getsock(struct filedesc *fdp, int fdes, struct file **fpp) +getsock(struct proc *p, int fdes, struct file **fpp) { struct file *fp; - if ((fp = fd_getfile(fdp, fdes)) == NULL) + if ((fp = fd_getfile(p->p_fd, fdes)) == NULL) return (EBADF); if (fp->f_type != DTYPE_SOCKET) return (ENOTSOCK); diff --git a/sys/nfs/nfs_syscalls.c b/sys/nfs/nfs_syscalls.c index 2f05d36d9d6..26db90feed0 100644 --- a/sys/nfs/nfs_syscalls.c +++ b/sys/nfs/nfs_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_syscalls.c,v 1.99 2015/03/14 03:38:52 jsg Exp $ */ +/* $OpenBSD: nfs_syscalls.c,v 1.100 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: nfs_syscalls.c,v 1.19 1996/02/18 11:53:52 fvdl Exp $ */ /* @@ -166,7 +166,7 @@ sys_nfssvc(struct proc *p, void *v, register_t *retval) if (error) return (error); - error = getsock(p->p_fd, nfsdarg.sock, &fp); + error = getsock(p, nfsdarg.sock, &fp); if (error) return (error); diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index 2016441326b..148c8bc0182 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: filedesc.h,v 1.29 2015/04/30 09:20:51 mpi Exp $ */ +/* $OpenBSD: filedesc.h,v 1.30 2015/05/06 08:52:17 mpi Exp $ */ /* $NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $ */ /* @@ -136,7 +136,7 @@ struct file *fd_getfile(struct filedesc *, int); struct file *fd_getfile_mode(struct filedesc *, int, int); int closef(struct file *, struct proc *); -int getsock(struct filedesc *, int, struct file **); +int getsock(struct proc *, int, struct file **); #define fdplock(fdp) rw_enter_write(&(fdp)->fd_lock) #define fdpunlock(fdp) rw_exit_write(&(fdp)->fd_lock) -- 2.20.1