BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 988ce1e..26c8c54 100644 (file)
@@ -2,9 +2,35 @@
  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)kern_descrip.c      7.23 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)kern_descrip.c      7.28 (Berkeley) 6/25/91
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -26,6 +52,8 @@
 /*
  * Descriptor management.
  */
 /*
  * Descriptor management.
  */
+struct file *filehead; /* head of list of open files */
+int nfiles;            /* actual number of open files */
 
 /*
  * System calls on descriptors.
 
 /*
  * System calls on descriptors.
@@ -173,21 +201,15 @@ fcntl(p, uap, retval)
        case F_SETFL:
                fp->f_flag &= ~FCNTLFLAGS;
                fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
        case F_SETFL:
                fp->f_flag &= ~FCNTLFLAGS;
                fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
-               if (tmp = (fp->f_flag & FNDELAY))
-                       fp->f_flag |= FNDELAY;
-               else
-                       fp->f_flag &= ~FNDELAY;
+               tmp = fp->f_flag & FNONBLOCK;
                error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                if (error)
                        return (error);
                error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                if (error)
                        return (error);
-               if (tmp = (fp->f_flag & FASYNC))
-                       fp->f_flag |= FASYNC;
-               else
-                       fp->f_flag &= ~FASYNC;
+               tmp = fp->f_flag & FASYNC;
                error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
                if (!error)
                        return (0);
                error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
                if (!error)
                        return (0);
-               fp->f_flag &= ~FNDELAY;
+               fp->f_flag &= ~FNONBLOCK;
                tmp = 0;
                (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                return (error);
                tmp = 0;
                (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                return (error);
@@ -237,11 +259,13 @@ fcntl(p, uap, retval)
                case F_RDLCK:
                        if ((fp->f_flag & FREAD) == 0)
                                return (EBADF);
                case F_RDLCK:
                        if ((fp->f_flag & FREAD) == 0)
                                return (EBADF);
+                       p->p_flag |= SADVLCK;
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_WRLCK:
                        if ((fp->f_flag & FWRITE) == 0)
                                return (EBADF);
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_WRLCK:
                        if ((fp->f_flag & FWRITE) == 0)
                                return (EBADF);
+                       p->p_flag |= SADVLCK;
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_UNLCK:
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_UNLCK:
@@ -433,7 +457,6 @@ fdavail(p, n)
        return (0);
 }
 
        return (0);
 }
 
-struct file *lastf;
 /*
  * Create a new open file structure and allocate
  * a file decriptor for the process that refers to it.
 /*
  * Create a new open file structure and allocate
  * a file decriptor for the process that refers to it.
@@ -443,29 +466,38 @@ falloc(p, resultfp, resultfd)
        struct file **resultfp;
        int *resultfd;
 {
        struct file **resultfp;
        int *resultfd;
 {
-       register struct file *fp;
+       register struct file *fp, *fq, **fpp;
        int error, i;
 
        if (error = fdalloc(p, 0, &i))
                return (error);
        int error, i;
 
        if (error = fdalloc(p, 0, &i))
                return (error);
-       if (lastf == 0)
-               lastf = file;
-       for (fp = lastf; fp < fileNFILE; fp++)
-               if (fp->f_count == 0)
-                       goto slot;
-       for (fp = file; fp < lastf; fp++)
-               if (fp->f_count == 0)
-                       goto slot;
-       tablefull("file");
-       return (ENFILE);
-slot:
+       if (nfiles >= maxfiles) {
+               tablefull("file");
+               return (ENFILE);
+       }
+       /*
+        * Allocate a new file descriptor.
+        * If the process has file descriptor zero open, add to the list
+        * of open files at that point, otherwise put it at the front of
+        * the list of open files.
+        */
+       nfiles++;
+       MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
+       if (fq = p->p_fd->fd_ofiles[0])
+               fpp = &fq->f_filef;
+       else
+               fpp = &filehead;
        p->p_fd->fd_ofiles[i] = fp;
        p->p_fd->fd_ofiles[i] = fp;
+       if (fq = *fpp)
+               fq->f_fileb = &fp->f_filef;
+       fp->f_filef = fq;
+       fp->f_fileb = fpp;
+       *fpp = fp;
        fp->f_count = 1;
        fp->f_count = 1;
-       fp->f_data = 0;
+       fp->f_msgcount = 0;
        fp->f_offset = 0;
        fp->f_cred = p->p_ucred;
        crhold(fp->f_cred);
        fp->f_offset = 0;
        fp->f_cred = p->p_ucred;
        crhold(fp->f_cred);
-       lastf = fp + 1;
        if (resultfp)
                *resultfp = fp;
        if (resultfd)
        if (resultfp)
                *resultfp = fp;
        if (resultfd)
@@ -473,6 +505,27 @@ slot:
        return (0);
 }
 
        return (0);
 }
 
+/*
+ * Free a file descriptor.
+ */
+ffree(fp)
+       register struct file *fp;
+{
+       register struct file *fq;
+
+       if (fq = fp->f_filef)
+               fq->f_fileb = fp->f_fileb;
+       *fp->f_fileb = fq;
+       crfree(fp->f_cred);
+#ifdef DIAGNOSTIC
+       fp->f_filef = NULL;
+       fp->f_fileb = NULL;
+       fp->f_count = 0;
+#endif
+       nfiles--;
+       FREE(fp, M_FILE);
+}
+
 /*
  * Copy a filedesc structure.
  */
 /*
  * Copy a filedesc structure.
  */
@@ -557,7 +610,7 @@ fdfree(p)
  */
 closef(fp, p)
        register struct file *fp;
  */
 closef(fp, p)
        register struct file *fp;
-       struct proc *p;
+       register struct proc *p;
 {
        struct vnode *vp;
        struct flock lf;
 {
        struct vnode *vp;
        struct flock lf;
@@ -571,7 +624,7 @@ closef(fp, p)
         * a flag in the unlock to free ONLY locks obeying POSIX
         * semantics, and not to free BSD-style file locks.
         */
         * a flag in the unlock to free ONLY locks obeying POSIX
         * semantics, and not to free BSD-style file locks.
         */
-       if (fp->f_type == DTYPE_VNODE) {
+       if ((p->p_flag & SADVLCK) && fp->f_type == DTYPE_VNODE) {
                lf.l_whence = SEEK_SET;
                lf.l_start = 0;
                lf.l_len = 0;
                lf.l_whence = SEEK_SET;
                lf.l_start = 0;
                lf.l_len = 0;
@@ -583,11 +636,16 @@ closef(fp, p)
                return (0);
        if (fp->f_count < 0)
                panic("closef: count < 0");
                return (0);
        if (fp->f_count < 0)
                panic("closef: count < 0");
-       if (fp->f_type == DTYPE_VNODE)
+       if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
+               lf.l_whence = SEEK_SET;
+               lf.l_start = 0;
+               lf.l_len = 0;
+               lf.l_type = F_UNLCK;
+               vp = (struct vnode *)fp->f_data;
                (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
                (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
+       }
        error = (*fp->f_ops->fo_close)(fp, p);
        error = (*fp->f_ops->fo_close)(fp, p);
-       crfree(fp->f_cred);
-       fp->f_count = 0;
+       ffree(fp);
        return (error);
 }
 
        return (error);
 }
 
@@ -624,6 +682,7 @@ flock(p, uap, retval)
        lf.l_len = 0;
        if (uap->how & LOCK_UN) {
                lf.l_type = F_UNLCK;
        lf.l_len = 0;
        if (uap->how & LOCK_UN) {
                lf.l_type = F_UNLCK;
+               fp->f_flag &= ~FHASLOCK;
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
        }
        if (uap->how & LOCK_EX)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
        }
        if (uap->how & LOCK_EX)
@@ -632,6 +691,7 @@ flock(p, uap, retval)
                lf.l_type = F_RDLCK;
        else
                return (EBADF);
                lf.l_type = F_RDLCK;
        else
                return (EBADF);
+       fp->f_flag |= FHASLOCK;
        if (uap->how & LOCK_NB)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
        return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
        if (uap->how & LOCK_NB)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
        return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));