fix to race in accept
[unix-history] / usr / src / sys / kern / uipc_syscalls.c
index 54edbf8..29c2d1d 100644 (file)
@@ -1,4 +1,4 @@
-/*     uipc_syscalls.c 4.13    82/01/13        */
+/*     uipc_syscalls.c 4.18    82/06/12        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -74,45 +74,6 @@ free:
        sofree(rso);
 }
 
        sofree(rso);
 }
 
-/*
- * Splice system call interface.
- */
-ssplice()
-{
-       register struct a {
-               int     fd1;
-               int     fd2;
-       } *ap = (struct a *)u.u_ap;
-       struct file *f1, *f2;
-COUNT(SSPLICE);
-
-       f1 = getf(ap->fd1);
-       if (f1 == NULL)
-               return;
-       f2 = getf(ap->fd2);
-       if (f2 == NULL)
-               return;
-       if (f1 == f2) {
-               u.u_error = EINVAL;
-               return;
-       }
-       if ((f1->f_flag & FSOCKET) == 0 || (f2->f_flag & FSOCKET) == 0) {
-               u.u_error = ENOTSOCK;
-               return;
-       }
-       if (f1->f_count > 1 || f2->f_count > 1) {
-               u.u_error = ETOOMANYREFS;
-               return;
-       }
-       u.u_error = sosplice(f1->f_socket, f2->f_socket);
-       if (u.u_error)
-               return;
-       u.u_ofile[ap->fd1] = 0;
-       u.u_ofile[ap->fd2] = 0;
-       f1->f_count = 0;
-       f2->f_count = 0;
-}
-
 /*
  * Socket system call interface.  Copy sa arguments
  * set up file descriptor and call internal socket
 /*
  * Socket system call interface.  Copy sa arguments
  * set up file descriptor and call internal socket
@@ -179,14 +140,24 @@ COUNT(SACCEPT);
        }
        s = splnet();
        so = fp->f_socket;
        }
        s = splnet();
        so = fp->f_socket;
-       if ((so->so_options & SO_NONBLOCKING) &&
+       if ((so->so_state & SS_NBIO) &&
            (so->so_state & SS_CONNAWAITING) == 0) {
                u.u_error = EWOULDBLOCK;
                splx(s);
                return;
        }
            (so->so_state & SS_CONNAWAITING) == 0) {
                u.u_error = EWOULDBLOCK;
                splx(s);
                return;
        }
-       while ((so->so_state & SS_CONNAWAITING) == 0)
+       while ((so->so_state & SS_CONNAWAITING) == 0 && so->so_error == 0) {
+               if (so->so_state & SS_CANTRCVMORE) {
+                       so->so_error = ECONNABORTED;
+                       break;
+               }
                sleep((caddr_t)&so->so_timeo, PZERO+1);
                sleep((caddr_t)&so->so_timeo, PZERO+1);
+       }
+       if (so->so_error) {
+               u.u_error = so->so_error;
+               splx(s);
+               return;
+       }
        u.u_error = soaccept(so, &sa);
        if (u.u_error) {
                splx(s);
        u.u_error = soaccept(so, &sa);
        if (u.u_error) {
                splx(s);
@@ -231,7 +202,7 @@ COUNT(SCONNECT);
        if (u.u_error)
                return;
        s = splnet();
        if (u.u_error)
                return;
        s = splnet();
-       if ((so->so_options & SO_NONBLOCKING) &&
+       if ((so->so_state & SS_NBIO) &&
            (so->so_state & SS_ISCONNECTING)) {
                u.u_error = EINPROGRESS;
                splx(s);
            (so->so_state & SS_ISCONNECTING)) {
                u.u_error = EINPROGRESS;
                splx(s);
@@ -244,51 +215,6 @@ COUNT(SCONNECT);
        splx(s);
 }
 
        splx(s);
 }
 
-/*
- * Disconnect socket from foreign peer; system call
- * interface.  Copy sa arguments and call internal routine.
- */
-sdisconnect()
-{
-       register struct a {
-               int     fdes;
-               struct  sockaddr *asa;
-       } *uap = (struct a *)u.u_ap;
-       struct sockaddr sa;
-       register struct file *fp;
-       register struct socket *so;
-       int s;
-COUNT(SDISCONNECT);
-
-       if (uap->asa &&
-           copyin((caddr_t)uap->asa, (caddr_t)&sa, sizeof (sa))) {
-               u.u_error = EFAULT;
-               return;
-       }
-       fp = getf(uap->fdes);
-       if (fp == 0)
-               return;
-       if ((fp->f_flag & FSOCKET) == 0) {
-               u.u_error = ENOTSOCK;
-               return;
-       }
-       so = fp->f_socket;
-       u.u_error = sodisconnect(so, uap->asa ? &sa : 0);
-       if (u.u_error)
-               return;
-       s = splnet();
-       if ((so->so_options&SO_NONBLOCKING) && (so->so_state&SS_ISDISCONNECTING)) {
-               u.u_error = EINPROGRESS;
-               splx(s);
-               return;
-       }
-       while ((so->so_state & SS_ISDISCONNECTING) && so->so_error == 0)
-               sleep((caddr_t)&so->so_timeo, PZERO+1);
-       u.u_error = so->so_error;
-       so->so_error = 0;
-       splx(s);
-}
-
 /*
  * Send data on socket.
  */
 /*
  * Send data on socket.
  */
@@ -371,6 +297,8 @@ ssocketaddr()
                struct  sockaddr *asa;
        } *uap = (struct a *)u.u_ap;
        register struct file *fp;
                struct  sockaddr *asa;
        } *uap = (struct a *)u.u_ap;
        register struct file *fp;
+       register struct socket *so;
+       struct sockaddr addr;
 COUNT(SSOCKETADDR);
 
        fp = getf(uap->fdes);
 COUNT(SSOCKETADDR);
 
        fp = getf(uap->fdes);
@@ -380,9 +308,11 @@ COUNT(SSOCKETADDR);
                u.u_error = ENOTSOCK;
                return;
        }
                u.u_error = ENOTSOCK;
                return;
        }
-       if (copyout((caddr_t)&fp->f_socket->so_addr, (caddr_t)uap->asa, 
-           sizeof (struct sockaddr))) {
-               u.u_error = EFAULT;
+       so = fp->f_socket;
+       u.u_error =
+               (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, 0, (caddr_t)&addr);
+       if (u.u_error)
                return;
                return;
-       }
+       if (copyout((caddr_t)&addr, (caddr_t)uap->asa, sizeof (addr)))
+               u.u_error = EFAULT;
 }
 }