shutdown; clear suid; get/set hostname; nbio in state not options fixes
[unix-history] / usr / src / sys / kern / uipc_syscalls.c
index a0ddc1d..13cae1a 100644 (file)
@@ -1,4 +1,4 @@
-/*     uipc_syscalls.c 4.1     81/11/10        */
+/*     uipc_syscalls.c 4.15    82/03/15        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -9,13 +9,11 @@
 #include "../h/inode.h"
 #include "../h/buf.h"
 #include "../h/mbuf.h"
 #include "../h/inode.h"
 #include "../h/buf.h"
 #include "../h/mbuf.h"
-#include "../h/protocol.h"
 #include "../h/protosw.h"
 #include "../h/socket.h"
 #include "../h/socketvar.h"
 #include "../h/protosw.h"
 #include "../h/socket.h"
 #include "../h/socketvar.h"
-#include "../h/inaddr.h"
-#include "../net/inet.h"
-#include "../net/inet_systm.h"
+#include "../net/in.h"
+#include "../net/in_systm.h"
 
 /*
  * Socket system call interface.
 
 /*
  * Socket system call interface.
  * These routines interface the socket routines to UNIX,
  * isolating the system interface from the socket-protocol interface.
  *
  * These routines interface the socket routines to UNIX,
  * isolating the system interface from the socket-protocol interface.
  *
- * DO SPLICE STUFF
- * DO PIPE STUFF
- * DO PORTALS
- * DO ASSOCIATIONS
- * DO NEWFD STUFF
+ * TODO:
+ *     SO_INTNOTIFY
  */
 
  */
 
+static struct sockproto localproto = { PF_UNIX, 0 };
 /*
 /*
- * Socket system call interface.  Copy in arguments
+ * Pipe system call interface.
+ */
+spipe()
+{
+       register struct file *rf, *wf;
+       struct socket *rso, *wso;
+       int r;
+COUNT(SPIPE);
+       
+       u.u_error = socreate(&rso, SOCK_STREAM,
+           &localproto, (struct sockaddr *)0, 0);
+       if (u.u_error)
+               return;
+       u.u_error = socreate(&wso, SOCK_STREAM,
+           &localproto, (struct sockaddr *)0, 0);
+       if (u.u_error)
+               goto free;
+       rf = falloc();
+       if (rf == NULL)
+               goto free2;
+       r = u.u_r.r_val1;
+       rf->f_flag = FREAD|FSOCKET;
+       rf->f_socket = rso;
+       wf = falloc();
+       if (wf == NULL)
+               goto free3;
+       wf->f_flag = FWRITE|FSOCKET;
+       wf->f_socket = wso;
+       u.u_r.r_val2 = u.u_r.r_val1;
+       u.u_r.r_val1 = r;
+       if (piconnect(wso, rso) == 0)
+               goto free4;
+       return;
+free4:
+       wf->f_count = 0;
+       u.u_ofile[u.u_r.r_val1] = 0;
+free3:
+       rf->f_count = 0;
+       u.u_ofile[r] = 0;
+free2:
+       wso->so_state |= SS_USERGONE;
+       sofree(wso);
+free:
+       rso->so_state |= SS_USERGONE;
+       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
  * creation routine.
  */
  * set up file descriptor and call internal socket
  * creation routine.
  */
@@ -39,22 +122,26 @@ ssocket()
 {
        register struct a {
                int     type;
 {
        register struct a {
                int     type;
-               struct  in_addr *ain;
+               struct  sockproto *asp;
+               struct  sockaddr *asa;
                int     options;
        } *uap = (struct a *)u.u_ap;
                int     options;
        } *uap = (struct a *)u.u_ap;
-       struct in_addr in;
-       struct socket *so0;
-       register struct socket *so;
+       struct sockproto sp;
+       struct sockaddr sa;
+       struct socket *so;
        register struct file *fp;
        register struct file *fp;
+COUNT(SSOCKET);
 
        if ((fp = falloc()) == NULL)
                return;
        fp->f_flag = FSOCKET|FREAD|FWRITE;
 
        if ((fp = falloc()) == NULL)
                return;
        fp->f_flag = FSOCKET|FREAD|FWRITE;
-       if (copyin((caddr_t)uap->ain, &in, sizeof (in))) {
+       if (uap->asp && copyin((caddr_t)uap->asp, (caddr_t)&sp, sizeof (sp)) ||
+           uap->asa && copyin((caddr_t)uap->asa, (caddr_t)&sa, sizeof (sa))) {
                u.u_error = EFAULT;
                return;
        }
                u.u_error = EFAULT;
                return;
        }
-       u.u_error = socket(&so0, uap->type, &in, uap->options);
+       u.u_error = socreate(&so, uap->type,
+           uap->asp ? &sp : 0, uap->asa ? &sa : 0, uap->options);
        if (u.u_error)
                goto bad;
        fp->f_socket = so;
        if (u.u_error)
                goto bad;
        fp->f_socket = so;
@@ -65,219 +152,75 @@ bad:
 }
 
 /*
 }
 
 /*
- * Pipe system call interface.
- */
-spipe()
-{
-
-}
-
-static struct in_addr portalproto = { PF_PORTAL, /* rest don't care */ };
-/*
- * Portal system call interface.
- *
- * This call creates a portal.
- * All the difficulty here is in dealing with errors.
- * A long sequence of steps is necessary:
- *     1. a socket must be allocated
- *     2. the server name length must be determined
- *     3. the protal must be entered into the file system
- *     4. the portal type and server must be entered into the portals' file
- *     5. a file descriptor referencing the socket+inode must be returned
- * If any errors occur in this process we must back it all out.
+ * Accept system call interface.
  */
  */
-sportal()
+saccept()
 {
        register struct a {
 {
        register struct a {
-               caddr_t name;
-               int     mode;
-               caddr_t server;
-               int     kind;
-       } *ap = (struct a *)u.u_ap;
+               int     fdes;
+               struct  sockaddr *asa;
+       } *uap = (struct a *)u.u_ap;
+       struct sockaddr sa;
+       register struct file *fp;
        struct socket *so;
        struct socket *so;
-       struct inode *ip;
-       struct file *fp;
-       int err, len;
-       char ch;
+       int s;
+COUNT(SACCEPT);
 
 
-       /*
-        * Allocate the socket for the portal.
-        */
-       u.u_error = socket(&so, SOCK_STREAM, &portalproto, SO_NEWFDONCONN);
-       if (u.u_error)
+       if (uap->asa && useracc((caddr_t)uap->asa, sizeof (sa), B_WRITE)==0) {
+               u.u_error = EFAULT;
                return;
                return;
-
-       /*
-        * Check that server name fis in a file system buffer.
-        * This to simplify the creation of the portal service process.
-        */
-       if (ap->server) {
-               u.u_dirp = ap->server;
-               for (len = 0; len < BSIZE-2; len++) {
-                       register c = uchar();
-                       if (c < 0)
-                               goto bad;
-                       if (c == 0)
-                               break;
-               }
-               if (len == BSIZE - 2) {
-                       u.u_error = EINVAL;
-                       goto bad;
-               }
        }
        }
-
-       /*
-        * Make sure that nothing with the portal's name exists.
-        */
-       u.u_dirp = ap->name;
-       ip = namei(uchar, 1);
-       if (ip != NULL) {
-               iput(ip);
-               u.u_error = EEXIST;
-       }
-       if (u.u_error)
-               goto bad;
-
-       /*
-        * Make a node in the file system for the portal.
-        */
-       ip = maknode((ap->mode & 0x7777) | IFPORTAL);
-       if (ip == NULL)
-               goto bad;
-
-       /*
-        * Make the first character of the contents of the
-        * portal be the portal type and the rest of the portal be
-        * the pathname of the server (if one was given).
-        */
-       ch = (char)ap->kind;
-       u.u_base = (caddr_t)&ch;
-       u.u_count = 1;
-       u.u_offset = 0;
-       u.u_segflg = 1;
-       writei(ip);
-       if (ap->server) {
-               u.u_base = ap->server;
-               u.u_count = len;
-               u.u_segflg = 0;
-               writei(ip);
+       fp = getf(uap->fdes);
+       if (fp == 0)
+               return;
+       if ((fp->f_flag & FSOCKET) == 0) {
+               u.u_error = ENOTSOCK;
+               return;
        }
        }
-       if (u.u_error)
-               goto bad2;
-       
-       /*
-        * Allocate a file descriptor and make it reference both
-        * the inode representing the portal and the call director
-        * socket for the portal.
-        */
-       fp = falloc();
-       if (fp == NULL)
-               goto bad2;
-       fp->f_flags = FPORTAL|FSOCKET;
-       fp->f_inode = ip;
-       fp->f_socket = s;
-
-       /*
-        * Make the in-core inode reference the socket.
-        */
-       ip->i_socket = s;
-       prele(ip);
-       return;
-bad2:
-       err = u.u_error;
-       iput(ip);
-       u.u_dirp = ap->name;
-       unlink();
-       u.u_error = err;
-bad:
-       sofree(s);
-}
-
-/*
- * Close a socket on last file table reference removal.
- * Initiate disconnect if connected.
- * Free socket when disconnect complete.
- */
-skclose(so)
-       register struct socket *so;
-{
-       int s = splnet();               /* conservative */
-
-       if (so->so_pcb == 0)
-               goto discard;
-       if (so->so_state & SS_ISCONNECTED) {
-               u.u_error = disconnect(so, 0);
-               if (u.u_error) {
-                       splx(s);
-                       return;
-               }
-               if ((so->so_state & SS_ISDISCONNECTING) &&
-                   (so->so_options & SO_NBIO)) {
-                       u.u_error = EINPROGRESS;
-                       splx(s);
-                       return;
-               }
-               while (so->so_state & SS_ISCONNECTED)
-                       sleep((caddr_t)&so->so_timeo, PZERO+1);
+       s = splnet();
+       so = fp->f_socket;
+       if ((so->so_state & SS_NBIO) &&
+           (so->so_state & SS_CONNAWAITING) == 0) {
+               u.u_error = EWOULDBLOCK;
+               splx(s);
+               return;
        }
        }
-       u.u_error = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 0, 0);
-discard:
-       sofree(so);
-       splx(s);
-}
-
-/*
- * Select a socket.
- */
-soselect(so, flag)
-       register struct socket *so;
-       int flag;
-{
-       register struct proc *p;
-
-       if (soreadable(so))
-               return (1);
-       if ((p = so->so_rcv.sb_sel) && p->p_wchan == (caddr_t)select)
-               so->so_rcv.sb_flags |= SB_COLL;
-       else
-               so->so_rcv.sb_sel = u.u_procp;
-       return (0);
-}
-
-/*
- * Wakeup read sleep/select'ers.
- */
-sowakeup(so)
-       struct socket *so;
-{
-
-       if (so->so_rcv.sb_sel && soreadable(so)) {
-               selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL);
-               so->so_rcv.sb_sel = 0;
-               so->so_rcv.sb_flags &= ~SB_COLL;
+       while ((so->so_state & SS_CONNAWAITING) == 0 && so->so_error == 0)
+               sleep((caddr_t)&so->so_timeo, PZERO+1);
+       if (so->so_error) {
+               u.u_error = so->so_error;
+               splx(s);
+               return;
        }
        }
-       if (so->so_rcv.sb_flags & SB_WAIT) {
-               so->so_rcv.sb_flags &= ~SB_WAIT;
-               wakeup((caddr_t)&so->so_rcv.sb_cc);
+       u.u_error = soaccept(so, &sa);
+       if (u.u_error) {
+               splx(s);
+               return;
        }
        }
+       if (uap->asa)
+               (void) copyout((caddr_t)&sa, (caddr_t)uap->asa, sizeof (sa));
+       /* deal with new file descriptor case */
+       /* u.u_r.r_val1 = ... */
+       splx(s);
 }
 
 /*
  * Connect socket to foreign peer; system call
 }
 
 /*
  * Connect socket to foreign peer; system call
- * interface.  Copy in arguments and call internal routine.
+ * interface.  Copy sa arguments and call internal routine.
  */
 sconnect()
 {
        register struct a {
  */
 sconnect()
 {
        register struct a {
-               int fdes;
-               struct in_addr *a;
+               int     fdes;
+               struct  sockaddr *a;
        } *uap = (struct a *)u.u_ap;
        } *uap = (struct a *)u.u_ap;
-       in_addr in;
+       struct sockaddr sa;
        register struct file *fp;
        register struct socket *so;
        int s;
        register struct file *fp;
        register struct socket *so;
        int s;
+COUNT(SCONNECT);
 
 
-       if (copyin((caddr_t)uap->a, &in, sizeof (in))) {
+       if (copyin((caddr_t)uap->a, (caddr_t)&sa, sizeof (sa))) {
                u.u_error = EFAULT;
                return;
        }
                u.u_error = EFAULT;
                return;
        }
@@ -289,35 +232,41 @@ sconnect()
                return;
        }
        so = fp->f_socket;
                return;
        }
        so = fp->f_socket;
-       u.u_error = connect(so, &in);
+       u.u_error = soconnect(so, &sa);
        if (u.u_error)
                return;
        s = splnet();
        if (u.u_error)
                return;
        s = splnet();
-       if ((so->so_options & SO_NBIO) && (so->so_state & SS_ISCONN) == 0) {
+       if ((so->so_state & SS_NBIO) &&
+           (so->so_state & SS_ISCONNECTING)) {
                u.u_error = EINPROGRESS;
                u.u_error = EINPROGRESS;
+               splx(s);
                return;
        }
                return;
        }
-       while ((so->so_state & (SS_ISCONN|SS_ISCONNING)) == SS_ISCONNING)
+       while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0)
                sleep((caddr_t)&so->so_timeo, PZERO+1);
        u.u_error = so->so_error;
                sleep((caddr_t)&so->so_timeo, PZERO+1);
        u.u_error = so->so_error;
+       so->so_error = 0;
+       splx(s);
 }
 
 /*
  * Disconnect socket from foreign peer; system call
 }
 
 /*
  * Disconnect socket from foreign peer; system call
- * interface.  Copy in arguments and call internal routine.
+ * interface.  Copy sa arguments and call internal routine.
  */
 sdisconnect()
 {
        register struct a {
                int     fdes;
  */
 sdisconnect()
 {
        register struct a {
                int     fdes;
-               in_addr  *addr;
+               struct  sockaddr *asa;
        } *uap = (struct a *)u.u_ap;
        } *uap = (struct a *)u.u_ap;
-       in_addr in;
+       struct sockaddr sa;
        register struct file *fp;
        register struct file *fp;
+       register struct socket *so;
        int s;
        int s;
+COUNT(SDISCONNECT);
 
 
-       if (uap->addr &&
-           copyin((caddr_t)uap->addr, (caddr_t)&in, sizeof (in))) {
+       if (uap->asa &&
+           copyin((caddr_t)uap->asa, (caddr_t)&sa, sizeof (sa))) {
                u.u_error = EFAULT;
                return;
        }
                u.u_error = EFAULT;
                return;
        }
@@ -328,15 +277,21 @@ sdisconnect()
                u.u_error = ENOTSOCK;
                return;
        }
                u.u_error = ENOTSOCK;
                return;
        }
-       u.u_error = disconnect(fp->f_socket, uap->addr ? &in : 0);
+       so = fp->f_socket;
+       u.u_error = sodisconnect(so, uap->asa ? &sa : 0);
        if (u.u_error)
                return;
        s = splnet();
        if (u.u_error)
                return;
        s = splnet();
-       if ((so->so_options&SO_NBIO) && (so->so_state&SS_ISCONNECTED))
-               return (EINPROGRESS);
-       while ((so)->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTING) == SS_ISDISCONNECTING)
+       if ((so->so_state&SS_NBIO) && (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;
                sleep((caddr_t)&so->so_timeo, PZERO+1);
        u.u_error = so->so_error;
+       so->so_error = 0;
+       splx(s);
 }
 
 /*
 }
 
 /*
@@ -346,12 +301,13 @@ ssend()
 {
        register struct a {
                int     fdes;
 {
        register struct a {
                int     fdes;
-               in_addr *ain;
+               struct  sockaddr *asa;
                caddr_t cbuf;
                caddr_t cbuf;
-               int     count;
+               unsigned count;
        } *uap = (struct a *)u.u_ap;
        register struct file *fp;
        } *uap = (struct a *)u.u_ap;
        register struct file *fp;
-       struct in_addr in;
+       struct sockaddr sa;
+COUNT(SSEND);
 
        fp = getf(uap->fdes);
        if (fp == 0)
 
        fp = getf(uap->fdes);
        if (fp == 0)
@@ -360,18 +316,78 @@ ssend()
                u.u_error = ENOTSOCK;
                return;
        }
                u.u_error = ENOTSOCK;
                return;
        }
-       if (uap->count < 0) {
-               u.u_error = EINVAL;
+       u.u_base = uap->cbuf;
+       u.u_count = uap->count;
+       u.u_segflg = 0;
+       if (useracc(uap->cbuf, uap->count, B_READ) == 0 ||
+           uap->asa && copyin((caddr_t)uap->asa, (caddr_t)&sa, sizeof (sa))) {
+               u.u_error = EFAULT;
+               return;
+       }
+       u.u_error = sosend(fp->f_socket, uap->asa ? &sa : 0);
+       u.u_r.r_val1 = uap->count - u.u_count;
+}
+
+/*
+ * Receive data on socket.
+ */
+sreceive()
+{
+       register struct a {
+               int     fdes;
+               struct  sockaddr *asa;
+               caddr_t cbuf;
+               u_int   count;
+       } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
+       struct sockaddr sa;
+COUNT(SRECEIVE);
+
+       fp = getf(uap->fdes);
+       if (fp == 0)
+               return;
+       if ((fp->f_flag & FSOCKET) == 0) {
+               u.u_error = ENOTSOCK;
                return;
        }
        u.u_base = uap->cbuf;
        u.u_count = uap->count;
        u.u_segflg = 0;
                return;
        }
        u.u_base = uap->cbuf;
        u.u_count = uap->count;
        u.u_segflg = 0;
-       if (useracc(u.u_base, u.u_count, B_READ) == 0 ||
-           uap->ain && copyin((caddr_t)uap->ain, (caddr_t)&in, sizeof (in))) {
+       if (useracc(uap->cbuf, uap->count, B_WRITE) == 0 ||
+           uap->asa && copyin((caddr_t)uap->asa, (caddr_t)&sa, sizeof (sa))) {
                u.u_error = EFAULT;
                return;
        }
                u.u_error = EFAULT;
                return;
        }
-       u.u_error = send(fp->f_socket, uap->ain ? &in : 0);
+       u.u_error = soreceive(fp->f_socket, uap->asa ? &sa : 0);
+       if (u.u_error)
+               return;
+       if (uap->asa)
+               (void) copyout((caddr_t)&sa, (caddr_t)uap->asa, sizeof (sa));
+       u.u_r.r_val1 = uap->count - u.u_count;
 }
 
 }
 
+/*
+ * Get socket address.
+ */
+ssocketaddr()
+{
+       register struct a {
+               int     fdes;
+               struct  sockaddr *asa;
+       } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
+COUNT(SSOCKETADDR);
+
+       fp = getf(uap->fdes);
+       if (fp == 0)
+               return;
+       if ((fp->f_flag & FSOCKET) == 0) {
+               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;
+               return;
+       }
+}