Module Name: src Committed By: rmind Date: Sat May 17 23:27:59 UTC 2014
Modified Files: src/sys/kern: uipc_socket.c uipc_syscalls.c src/sys/net: if_gre.c src/sys/sys: mbuf.h socketvar.h Log Message: - fsocreate: set SS_NBIO before the file descriptor is affixed as there is a theoretical race condition (hard to trigger, though); remove the LWP parameter and clean up the code a little. - Sprinkle few comments. - Remove M_SOOPTS while here. To generate a diff of this commit: cvs rdiff -u -r1.221 -r1.222 src/sys/kern/uipc_socket.c cvs rdiff -u -r1.168 -r1.169 src/sys/kern/uipc_syscalls.c cvs rdiff -u -r1.153 -r1.154 src/sys/net/if_gre.c cvs rdiff -u -r1.154 -r1.155 src/sys/sys/mbuf.h cvs rdiff -u -r1.132 -r1.133 src/sys/sys/socketvar.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/uipc_socket.c diff -u src/sys/kern/uipc_socket.c:1.221 src/sys/kern/uipc_socket.c:1.222 --- src/sys/kern/uipc_socket.c:1.221 Tue Feb 25 18:30:11 2014 +++ src/sys/kern/uipc_socket.c Sat May 17 23:27:59 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket.c,v 1.221 2014/02/25 18:30:11 pooka Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.222 2014/05/17 23:27:59 rmind Exp $ */ /*- * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -62,8 +62,16 @@ * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 */ +/* + * Socket operation routines. + * + * These routines are called by the routines in sys_socket.c or from a + * system process, and implement the semantics of socket operations by + * switching out to the protocol specific routines. + */ + #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.221 2014/02/25 18:30:11 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.222 2014/05/17 23:27:59 rmind Exp $"); #include "opt_compat_netbsd.h" #include "opt_sock_counters.h" @@ -103,7 +111,6 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include <uvm/uvm_loan.h> #include <uvm/uvm_page.h> -MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); MALLOC_DEFINE(M_SONAME, "soname", "socket name"); extern const struct fileops socketops; @@ -479,13 +486,11 @@ soinit1(void) } /* - * Socket operation routines. - * These routines are called by the routines in - * sys_socket.c or from a system process, and - * implement the semantics of socket operations by - * switching out to the protocol specific routines. - */ -/*ARGSUSED*/ + * socreate: create a new socket of the specified type and the protocol. + * + * => Caller may specify another socket for lock sharing (must not be held). + * => Returns the new socket without lock held. +*/ int socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, struct socket *lockso) @@ -547,6 +552,7 @@ socreate(int dom, struct socket **aso, i (struct mbuf *)(long)proto, NULL, l); KASSERT(solocked(so)); if (error != 0) { + KASSERT(so->so_pcb == NULL); so->so_state |= SS_NOFDREF; sofree(so); return error; @@ -557,38 +563,46 @@ socreate(int dom, struct socket **aso, i return 0; } -/* On success, write file descriptor to fdout and return zero. On - * failure, return non-zero; *fdout will be undefined. +/* + * fsocreate: create a socket and a file descriptor associated with it. + * + * => On success, write file descriptor to fdout and return zero. + * => On failure, return non-zero; *fdout will be undefined. */ int -fsocreate(int domain, struct socket **sop, int type, int protocol, - struct lwp *l, int *fdout) +fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout) { - struct socket *so; - struct file *fp; - int fd, error; - int flags = type & SOCK_FLAGS_MASK; + lwp_t *l = curlwp; + int error, fd, flags; + struct socket *so; + struct file *fp; - type &= ~SOCK_FLAGS_MASK; - if ((error = fd_allocfile(&fp, &fd)) != 0) + if ((error = fd_allocfile(&fp, &fd)) != 0) { return error; + } + flags = type & SOCK_FLAGS_MASK; fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); fp->f_type = DTYPE_SOCKET; fp->f_ops = &socketops; - error = socreate(domain, &so, type, protocol, l, NULL); - if (error != 0) { + + type &= ~SOCK_FLAGS_MASK; + error = socreate(domain, &so, type, proto, l, NULL); + if (error) { fd_abort(curproc, fp, fd); - } else { - if (sop != NULL) - *sop = so; - fp->f_data = so; - fd_affix(curproc, fp, fd); - *fdout = fd; - if (flags & SOCK_NONBLOCK) - so->so_state |= SS_NBIO; + return error; } + if (flags & SOCK_NONBLOCK) { + so->so_state |= SS_NBIO; + } + fp->f_data = so; + fd_affix(curproc, fp, fd); + + if (sop != NULL) { + *sop = so; + } + *fdout = fd; return error; } @@ -624,8 +638,8 @@ solisten(struct socket *so, int backlog, solock(so); if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) != 0) { - sounlock(so); - return (EINVAL); + sounlock(so); + return EINVAL; } error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, l); @@ -685,18 +699,15 @@ sofree(struct socket *so) } /* - * Close a socket on last file table reference removal. - * Initiate disconnect if connected. - * Free socket when disconnect complete. + * soclose: close a socket on last file table reference removal. + * Initiate disconnect if connected. Free socket when disconnect complete. */ int soclose(struct socket *so) { - struct socket *so2; - int error; - int error2; + struct socket *so2; + int error = 0; - error = 0; solock(so); if (so->so_options & SO_ACCEPTCONN) { for (;;) { @@ -719,7 +730,7 @@ soclose(struct socket *so) break; } } - if (so->so_pcb == 0) + if (so->so_pcb == NULL) goto discard; if (so->so_state & SS_ISCONNECTED) { if ((so->so_state & SS_ISDISCONNECTING) == 0) { @@ -740,18 +751,17 @@ soclose(struct socket *so) } drop: if (so->so_pcb) { - error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, + int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, NULL, NULL, NULL, NULL); if (error == 0) error = error2; } discard: - if (so->so_state & SS_NOFDREF) - panic("soclose: NOFDREF"); + KASSERT((so->so_state & SS_NOFDREF) == 0); kauth_cred_free(so->so_cred); so->so_state |= SS_NOFDREF; sofree(so); - return (error); + return error; } /* @@ -781,13 +791,11 @@ soabort(struct socket *so) int soaccept(struct socket *so, struct mbuf *nam) { - int error; + int error; KASSERT(solocked(so)); + KASSERT((so->so_state & SS_NOFDREF) != 0); - error = 0; - if ((so->so_state & SS_NOFDREF) == 0) - panic("soaccept: !NOFDREF"); so->so_state &= ~SS_NOFDREF; if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) @@ -796,18 +804,18 @@ soaccept(struct socket *so, struct mbuf else error = ECONNABORTED; - return (error); + return error; } int soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) { - int error; + int error; KASSERT(solocked(so)); if (so->so_options & SO_ACCEPTCONN) - return (EOPNOTSUPP); + return EOPNOTSUPP; /* * If protocol is connection-based, can only connect once. * Otherwise, if connected, try to disconnect first. @@ -821,19 +829,17 @@ soconnect(struct socket *so, struct mbuf else error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, NULL, nam, NULL, l); - return (error); + + return error; } int soconnect2(struct socket *so1, struct socket *so2) { - int error; - KASSERT(solocked2(so1, so2)); - error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, + return (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL); - return (error); } int Index: src/sys/kern/uipc_syscalls.c diff -u src/sys/kern/uipc_syscalls.c:1.168 src/sys/kern/uipc_syscalls.c:1.169 --- src/sys/kern/uipc_syscalls.c:1.168 Sat May 17 21:48:48 2014 +++ src/sys/kern/uipc_syscalls.c Sat May 17 23:27:59 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_syscalls.c,v 1.168 2014/05/17 21:48:48 rmind Exp $ */ +/* $NetBSD: uipc_syscalls.c,v 1.169 2014/05/17 23:27:59 rmind Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -61,7 +61,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.168 2014/05/17 21:48:48 rmind Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.169 2014/05/17 23:27:59 rmind Exp $"); #include "opt_pipe.h" @@ -103,7 +103,7 @@ sys___socket30(struct lwp *l, const stru int fd, error; error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type), - SCARG(uap, protocol), l, &fd); + SCARG(uap, protocol), &fd); if (error == 0) { *retval = fd; } Index: src/sys/net/if_gre.c diff -u src/sys/net/if_gre.c:1.153 src/sys/net/if_gre.c:1.154 --- src/sys/net/if_gre.c:1.153 Thu Nov 7 21:44:48 2013 +++ src/sys/net/if_gre.c Sat May 17 23:27:59 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: if_gre.c,v 1.153 2013/11/07 21:44:48 christos Exp $ */ +/* $NetBSD: if_gre.c,v 1.154 2014/05/17 23:27:59 rmind Exp $ */ /* * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc. @@ -45,7 +45,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.153 2013/11/07 21:44:48 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.154 2014/05/17 23:27:59 rmind Exp $"); #include "opt_atalk.h" #include "opt_gre.h" @@ -422,7 +422,7 @@ gre_socreate(struct gre_softc *sc, const GRE_DPRINTF(sc, "enter\n"); af = sp->sp_src.ss_family; - rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, curlwp, &fd); + rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd); if (rc != 0) { GRE_DPRINTF(sc, "fsocreate failed\n"); return rc; Index: src/sys/sys/mbuf.h diff -u src/sys/sys/mbuf.h:1.154 src/sys/sys/mbuf.h:1.155 --- src/sys/sys/mbuf.h:1.154 Thu Mar 20 00:29:57 2014 +++ src/sys/sys/mbuf.h Sat May 17 23:27:59 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: mbuf.h,v 1.154 2014/03/20 00:29:57 pooka Exp $ */ +/* $NetBSD: mbuf.h,v 1.155 2014/05/17 23:27:59 rmind Exp $ */ /*- * Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc. @@ -818,7 +818,6 @@ extern struct mowner revoked_mowner; MALLOC_DECLARE(M_MBUF); MALLOC_DECLARE(M_SONAME); -MALLOC_DECLARE(M_SOOPTS); struct mbuf *m_copym(struct mbuf *, int, int, int); struct mbuf *m_copypacket(struct mbuf *, int); Index: src/sys/sys/socketvar.h diff -u src/sys/sys/socketvar.h:1.132 src/sys/sys/socketvar.h:1.133 --- src/sys/sys/socketvar.h:1.132 Sat May 17 22:52:36 2014 +++ src/sys/sys/socketvar.h Sat May 17 23:27:59 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: socketvar.h,v 1.132 2014/05/17 22:52:36 rmind Exp $ */ +/* $NetBSD: socketvar.h,v 1.133 2014/05/17 23:27:59 rmind Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -295,7 +295,7 @@ int soconnect(struct socket *, struct mb int soconnect2(struct socket *, struct socket *); int socreate(int, struct socket **, int, int, struct lwp *, struct socket *); -int fsocreate(int, struct socket **, int, int, struct lwp *, int *); +int fsocreate(int, struct socket **, int, int, int *); int sodisconnect(struct socket *); void sofree(struct socket *); int sogetopt(struct socket *, struct sockopt *);