call ffree to free file structures
SCCS-vsn: sys/kern/kern_descrip.c 7.26
*
* %sccs.include.redist.c%
*
*
* %sccs.include.redist.c%
*
- * @(#)kern_descrip.c 7.25 (Berkeley) %G%
+ * @(#)kern_descrip.c 7.26 (Berkeley) %G%
/*
* 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.
/*
* 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.
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);
p->p_fd->fd_ofiles[i] = fp;
p->p_fd->fd_ofiles[i] = fp;
+ if (fq = p->p_fd->fd_ofiles[0])
+ fpp = &fq->f_filef;
+ else
+ fpp = &filehead;
+ if (fq = *fpp)
+ fq->f_fileb = &fp->f_filef;
+ fp->f_filef = fq;
+ fp->f_fileb = fpp;
+ *fpp = fp;
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);
if (resultfp)
*resultfp = fp;
if (resultfd)
if (resultfp)
*resultfp = fp;
if (resultfd)
+/*
+ * 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.
*/
*/
closef(fp, p)
register struct file *fp;
*/
closef(fp, p)
register struct file *fp;
+ register struct proc *p;
{
struct vnode *vp;
struct flock lf;
{
struct vnode *vp;
struct flock lf;
(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
}
error = (*fp->f_ops->fo_close)(fp, p);
(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
}
error = (*fp->f_ops->fo_close)(fp, p);
- crfree(fp->f_cred);
- fp->f_count = 0;