/* kern_descrip.c 5.25 83/06/12 */ #include "../h/param.h" #include "../h/systm.h" #include "../h/dir.h" #include "../h/user.h" #include "../h/kernel.h" #include "../h/inode.h" #include "../h/proc.h" #include "../h/conf.h" #include "../h/file.h" #include "../h/socket.h" #include "../h/socketvar.h" #include "../h/mount.h" #include "../h/stat.h" #include "../h/ioctl.h" /* * Descriptor management. */ /* * TODO: * increase NOFILE * eliminate u.u_error side effects */ /* * System calls on descriptors. */ getdtablesize() { u.u_r.r_val1 = NOFILE; } getdopt() { } setdopt() { } dup() { register struct a { int i; } *uap = (struct a *) u.u_ap; struct file *fp; int j; if (uap->i &~ 077) { uap->i &= 077; dup2(); return; } /* XXX */ fp = getf(uap->i); if (fp == 0) return; j = ufalloc(0); if (j < 0) return; dupit(j, fp, u.u_pofile[uap->i]); } dup2() { register struct a { int i, j; } *uap = (struct a *) u.u_ap; register struct file *fp; fp = getf(uap->i); if (fp == 0) return; if (uap->j < 0 || uap->j >= NOFILE) { u.u_error = EBADF; return; } u.u_r.r_val1 = uap->j; if (uap->i == uap->j) return; if (u.u_ofile[uap->j]) { if (u.u_pofile[uap->j] & UF_MAPPED) munmapfd(uap->j); closef(u.u_ofile[uap->j], u.u_pofile[uap->j]); if (u.u_error) return; /* u.u_ofile[uap->j] = 0; */ /* u.u_pofile[uap->j] = 0; */ } dupit(uap->j, fp, u.u_pofile[uap->i]); } dupit(fd, fp, lockflags) int fd; register struct file *fp; register int lockflags; { u.u_ofile[fd] = fp; u.u_pofile[fd] = lockflags; fp->f_count++; /* THIS DOESN'T BELONG HERE */ if (lockflags&UF_SHLOCK) ((struct inode *)fp->f_data)->i_shlockc++; if (lockflags&UF_EXLOCK) ((struct inode *)fp->f_data)->i_exlockc++; /* END DOESN'T BELONG */ } /* * The file control system call. */ fcntl() { register struct file *fp; register struct a { int fdes; int cmd; int arg; } *uap; register i; register char *pop; uap = (struct a *)u.u_ap; fp = getf(uap->fdes); if (fp == NULL) return; pop = &u.u_pofile[uap->fdes]; switch(uap->cmd) { case 0: i = uap->arg; if (i < 0 || i > NOFILE) { u.u_error = EINVAL; return; } if ((i = ufalloc(i)) < 0) return; dupit(i, fp, *pop); break; case 1: u.u_r.r_val1 = *pop & 1; break; case 2: *pop = (*pop &~ 1) | (uap->arg & 1); break; case 3: u.u_r.r_val1 = fp->f_flag+FOPEN; break; case 4: fp->f_flag &= FCNTLCANT; fp->f_flag |= (uap->arg-FOPEN) &~ FCNTLCANT; u.u_error = fset(fp, FNDELAY, fp->f_flag & FNDELAY); if (u.u_error) break; u.u_error = fset(fp, FASYNC, fp->f_flag & FASYNC); if (u.u_error) (void) fset(fp, FNDELAY, 0); break; case 5: u.u_error = fsetown(fp, uap->arg); break; case 6: u.u_error = fgetown(fp, &u.u_r.r_val1); break; default: u.u_error = EINVAL; } } fset(fp, bit, value) struct file *fp; int bit, value; { if (value) fp->f_flag |= bit; else fp->f_flag &= ~bit; return (fioctl(fp, (int)(bit == FNDELAY ? FIONBIO : FIOASYNC), (caddr_t)&value)); } fgetown(fp, valuep) struct file *fp; int *valuep; { int error; switch (fp->f_type) { case DTYPE_SOCKET: *valuep = ((struct socket *)fp->f_data)->so_pgrp; return (0); default: error = fioctl(fp, (int)TIOCGPGRP, (caddr_t)valuep); *valuep = -*valuep; return (error); } } fsetown(fp, value) struct file *fp; int value; { if (fp->f_type == DTYPE_SOCKET) { ((struct socket *)fp->f_data)->so_pgrp = value; return (0); } if (value > 0) { struct proc *p = pfind(value); if (p == 0) return (EINVAL); value = p->p_pgrp; } else value = -value; return (fioctl(fp, (int)TIOCSPGRP, (caddr_t)&value)); } fioctl(fp, cmd, value) struct file *fp; int cmd; caddr_t value; { return ((*fp->f_ops->fo_ioctl)(fp, cmd, value)); } close() { register struct a { int i; } *uap = (struct a *)u.u_ap; register struct file *fp; register u_char *pf; fp = getf(uap->i); if (fp == 0) return; pf = (u_char *)&u.u_pofile[uap->i]; if (*pf & UF_MAPPED) munmapfd(uap->i); if (*pf & (UF_SHLOCK|UF_EXLOCK)) unlockf(fp, pf); closef(fp); /* WHAT IF u.u_error ? */ u.u_ofile[uap->i] = NULL; *pf = 0; } fstat() { register struct file *fp; register struct a { int fdes; struct stat *sb; } *uap; struct stat ub; uap = (struct a *)u.u_ap; fp = getf(uap->fdes); if (fp == 0) return; switch (fp->f_type) { case DTYPE_INODE: u.u_error = ino_stat((struct inode *)fp->f_data, &ub); break; case DTYPE_SOCKET: u.u_error = soo_stat((struct socket *)fp->f_data, &ub); break; default: panic("fstat"); /*NOTREACHED*/ } if (u.u_error == 0) u.u_error = copyout(&ub, uap->sb, sizeof (ub)); } /* * Allocate a user file descriptor. */ ufalloc(i) register int i; { for (; i < NOFILE; i++) if (u.u_ofile[i] == NULL) { u.u_r.r_val1 = i; u.u_pofile[i] = 0; return (i); } u.u_error = EMFILE; return (-1); } ufavail() { register int i, avail = 0; for (i = 0; i < NOFILE; i++) if (u.u_ofile[i] == NULL) avail++; return (avail); } struct file *lastf; /* * Allocate a user file descriptor * and a file structure. * Initialize the descriptor * to point at the file structure. */ struct file * falloc() { register struct file *fp; register i; i = ufalloc(0); if (i < 0) return (NULL); 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"); u.u_error = ENFILE; return (NULL); slot: u.u_ofile[i] = fp; fp->f_count = 1; fp->f_data = 0; fp->f_offset = 0; lastf = fp + 1; return (fp); } /* * Convert a user supplied file descriptor into a pointer * to a file structure. Only task is to check range of the descriptor. * Critical paths should use the GETF macro. */ struct file * getf(f) register int f; { register struct file *fp; if ((unsigned)f >= NOFILE || (fp = u.u_ofile[f]) == NULL) { u.u_error = EBADF; return (NULL); } return (fp); } /* * Internal form of close. * Decrement reference count on file structure. * If last reference not going away, but no more * references except in message queues, run a * garbage collect. This would better be done by * forcing a gc() to happen sometime soon, rather * than running one each time. */ closef(fp) register struct file *fp; { if (fp == NULL) return; if (fp->f_count > 1) { fp->f_count--; if (fp->f_count == fp->f_msgcount) unp_gc(); return; } (*fp->f_ops->fo_close)(fp); fp->f_count = 0; } /* * Apply an advisory lock on a file descriptor. */ flock() { register struct a { int fd; int how; } *uap = (struct a *)u.u_ap; register struct file *fp; register u_char *pf; int cmd; fp = getf(uap->fd); if (fp == NULL) return; cmd = uap->how; pf = (u_char *)&u.u_pofile[uap->fd]; if (cmd & LOCK_UN) { unlockf(fp, pf); return; } /* * No reason to write lock a file we've already * write locked, similarly with a read lock. */ if ((*pf & UF_EXLOCK) && (cmd & LOCK_EX) || (*pf & UF_SHLOCK) && (cmd & LOCK_SH)) return; switch (fp->f_type) { case DTYPE_INODE: u.u_error = ino_lock((struct inode *)fp->f_data, pf, cmd); break; case DTYPE_SOCKET: u.u_error = soo_lock((struct socket *)fp->f_data, pf, cmd); break; default: panic("lockf"); } } unlockf(fp, pf) register struct file *fp; register u_char *pf; { if ((*pf & (UF_SHLOCK|UF_EXLOCK)) == 0) return; switch (fp->f_type) { case DTYPE_INODE: ino_unlock((struct inode *)fp->f_data, *pf); break; case DTYPE_SOCKET: soo_unlock((struct socket *)fp->f_data, *pf); break; default: panic("unlockf"); } *pf &= ~(UF_SHLOCK|UF_EXLOCK); }