Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.35 retrieving revision 1.44.6.1 diff -u -p -r1.35 -r1.44.6.1 --- src/sys/kern/uipc_socket.c 1998/06/25 23:41:20 1.35 +++ src/sys/kern/uipc_socket.c 1999/06/28 06:36:53 1.44.6.1 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket.c,v 1.35 1998/06/25 23:41:20 thorpej Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.44.6.1 1999/06/28 06:36:53 itojun Exp $ */ /* * Copyright (c) 1982, 1986, 1988, 1990, 1993 @@ -50,6 +50,21 @@ #include #include #include +#include + +struct pool socket_pool; + +void +soinit() +{ + + pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, + "sockpl", 0, NULL, NULL, M_SOCKET); +} + +#ifdef KEY +#include +#endif /* * Socket operation routines. @@ -70,6 +85,7 @@ socreate(dom, aso, type, proto) register struct protosw *prp; register struct socket *so; register int error; + int s; if (proto) prp = pffindproto(dom, proto, type); @@ -79,19 +95,23 @@ socreate(dom, aso, type, proto) return (EPROTONOSUPPORT); if (prp->pr_type != type) return (EPROTOTYPE); - MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT); - bzero((caddr_t)so, sizeof(*so)); + s = splsoftnet(); + so = pool_get(&socket_pool, PR_WAITOK); + memset((caddr_t)so, 0, sizeof(*so)); TAILQ_INIT(&so->so_q0); TAILQ_INIT(&so->so_q); so->so_type = type; so->so_proto = prp; so->so_send = sosend; so->so_receive = soreceive; + if (p != 0) + so->so_uid = p->p_ucred->cr_uid; error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, (struct mbuf *)(long)proto, (struct mbuf *)0, p); if (error) { so->so_state |= SS_NOFDREF; sofree(so); + splx(s); return (error); } #ifdef COMPAT_SUNOS @@ -101,6 +121,7 @@ socreate(dom, aso, type, proto) so->so_options |= SO_BROADCAST; } #endif + splx(s); *aso = so; return (0); } @@ -147,16 +168,30 @@ sofree(so) register struct socket *so; { - if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) + if (so->so_pcb +#ifdef MAPPED_ADDR_ENABLED + /* + * MAPPED_ADDR implementation spec: + * Check so_pcb2 despite the ip6_mapped_addr value. + * Because the sysctl value may be changed to 0 + * after connection establishment. + */ + || so->so_pcb2 +#endif /* MAPPED_ADDR_ENABLED */ + || (so->so_state & SS_NOFDREF) == 0) return; if (so->so_head) { - if (!soqremque(so, 0) && !soqremque(so, 1)) - panic("sofree dq"); - so->so_head = 0; + /* + * We must not decommission a socket that's on the accept(2) + * queue. If we do, then accept(2) may hang after select(2) + * indicated that the listening socket was ready. + */ + if (!soqremque(so, 0)) + return; } sbrelease(&so->so_snd); sorflush(so); - FREE(so, M_SOCKET); + pool_put(&socket_pool, so); } /* @@ -168,16 +203,25 @@ int soclose(so) register struct socket *so; { + struct socket *so2; int s = splsoftnet(); /* conservative */ int error = 0; if (so->so_options & SO_ACCEPTCONN) { - while (so->so_q0.tqh_first) - (void) soabort(so->so_q0.tqh_first); - while (so->so_q.tqh_first) - (void) soabort(so->so_q.tqh_first); + while ((so2 = so->so_q0.tqh_first) != 0) { + (void) soqremque(so2, 0); + (void) soabort(so2); + } + while ((so2 = so->so_q.tqh_first) != 0) { + (void) soqremque(so2, 1); + (void) soabort(so2); + } } - if (so->so_pcb == 0) + if (so->so_pcb == 0 +#ifdef MAPPED_ADDR_ENABLED + && so->so_pcb2 == 0 +#endif /* MAPPED_ADDR_ENABLED */ + ) goto discard; if (so->so_state & SS_ISCONNECTED) { if ((so->so_state & SS_ISDISCONNECTING) == 0) { @@ -199,7 +243,11 @@ soclose(so) } } drop: - if (so->so_pcb) { + if (so->so_pcb +#ifdef MAPPED_ADDR_ENABLED + || so->so_pcb2 +#endif /* MAPPED_ADDR_ENABLED */ + ) { int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); @@ -238,8 +286,11 @@ soaccept(so, nam) if ((so->so_state & SS_NOFDREF) == 0) panic("soaccept: !NOFDREF"); so->so_state &= ~SS_NOFDREF; - error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, (struct mbuf *)0, - nam, (struct mbuf *)0, (struct proc *)0); + if ((so->so_state & SS_ISDISCONNECTED) == 0) + error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, + (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0); + else + error = 0; splx(s); return (error); } @@ -465,6 +516,8 @@ nopages: } while (space > 0 && atomic); if (dontroute) so->so_options |= SO_DONTROUTE; + if (resid > 0) + so->so_state |= SS_MORETOCOME; s = splsoftnet(); /* XXX */ error = (*so->so_proto->pr_usrreq)(so, (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, @@ -472,6 +525,8 @@ nopages: splx(s); if (dontroute) so->so_options &= ~SO_DONTROUTE; + if (resid > 0) + so->so_state &= ~SS_MORETOCOME; clen = 0; control = 0; top = 0; @@ -789,7 +844,13 @@ dontblock: if ((flags & MSG_PEEK) == 0) { if (m == 0) so->so_rcv.sb_mb = nextrecord; - if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) + if (pr->pr_flags & PR_WANTRCVD && + (so->so_pcb +#ifdef MAPPED_ADDR_ENABLED + || so->so_pcb2 +#endif /* MAPPED_ADDR_ENABLED */ + ) + ) (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, (struct mbuf *)(long)flags, (struct mbuf *)0, (struct proc *)0); @@ -842,7 +903,7 @@ sorflush(so) socantrcvmore(so); sbunlock(sb); asb = *sb; - bzero((caddr_t)sb, sizeof (*sb)); + memset((caddr_t)sb, 0, sizeof(*sb)); splx(s); if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) (*pr->pr_domain->dom_dispose)(asb.sb_mb); @@ -867,7 +928,7 @@ sosetopt(so, level, optname, m0) switch (optname) { case SO_LINGER: - if (m == NULL || m->m_len != sizeof (struct linger)) { + if (m == NULL || m->m_len != sizeof(struct linger)) { error = EINVAL; goto bad; } @@ -883,7 +944,7 @@ sosetopt(so, level, optname, m0) case SO_REUSEPORT: case SO_OOBINLINE: case SO_TIMESTAMP: - if (m == NULL || m->m_len < sizeof (int)) { + if (m == NULL || m->m_len < sizeof(int)) { error = EINVAL; goto bad; } @@ -900,7 +961,7 @@ sosetopt(so, level, optname, m0) { int optval; - if (m == NULL || m->m_len < sizeof (int)) { + if (m == NULL || m->m_len < sizeof(int)) { error = EINVAL; goto bad; } @@ -951,7 +1012,7 @@ sosetopt(so, level, optname, m0) struct timeval *tv; short val; - if (m == NULL || m->m_len < sizeof (*tv)) { + if (m == NULL || m->m_len < sizeof(*tv)) { error = EINVAL; goto bad; } @@ -1006,12 +1067,12 @@ sogetopt(so, level, optname, mp) return (ENOPROTOOPT); } else { m = m_get(M_WAIT, MT_SOOPTS); - m->m_len = sizeof (int); + m->m_len = sizeof(int); switch (optname) { case SO_LINGER: - m->m_len = sizeof (struct linger); + m->m_len = sizeof(struct linger); mtod(m, struct linger *)->l_onoff = so->so_options & SO_LINGER; mtod(m, struct linger *)->l_linger = so->so_linger;