ec_rxstart doesn't eists
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 808b95a..1debca7 100644 (file)
@@ -4,7 +4,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)kern_descrip.c      7.25 (Berkeley) %G%
+ *     @(#)kern_descrip.c      7.28 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -26,6 +26,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.
@@ -231,11 +233,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:
@@ -427,7 +431,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.
@@ -437,29 +440,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)
@@ -467,6 +479,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.
  */
@@ -551,7 +584,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;
@@ -565,7 +598,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;
@@ -577,11 +610,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);
 }
 
@@ -618,6 +656,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)
@@ -626,6 +665,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));