int do_open(const char *path, int mode) { int fd; struct file *f; struct inode *ip; if ((mode & O_CREATE) &&!(ip = icreate(path, T_FILE, 0, 0))) return -1; else if (!(ip = namei(path))) return -1; if (ip->type == T_DIR && mode != O_RDONLY) { iput(ip); return -1; } if (!(f = filealloc()) || (fd = fdalloc()) < 0) { if (f) fileclose(f); iput(ip); return -1; } f->type = FD_INODE; f->ip = ip; f->off = 0; f->readable = !(mode & O_WRONLY); f->writable = (mode & O_WRONLY) || (mode & O_RDWR); fs_current->ofile[fd] = f; return fd; }
int sys_dup(struct file *f) { int fd; if(argfd(0, 0, &f) < 0) return -1; if((fd=fdalloc(f)) < 0) return -1; filedup(f); return fd; }
int sys_getcwd() { struct inode *ip = idup(current_proc->cwd); struct file * f = filealloc(); f->in = ip; f->offset = 0; f->type = FD_INODE; f->rw = FD_READABLE; int fd = fdalloc(f); if(fd > 0) return fd; return -1; }
static int sysvipc_install_fd(struct proc *p_from, struct proc *p_to, int fd) { struct filedesc *fdp_from = p_from->p_fd; struct filedesc *fdp_to = p_to->p_fd; struct file *fp; int error, newfd; int flags; /* * Get the file corresponding to fd from the process p_from. */ spin_lock(&fdp_from->fd_spin); if ((unsigned)fd >= fdp_from->fd_nfiles || fdp_from->fd_files[fd].fp == NULL) { spin_unlock(&fdp_from->fd_spin); return (EBADF); } fp = fdp_from->fd_files[fd].fp; flags = fdp_from->fd_files[fd].fileflags; fhold(fp); /* MPSAFE - can be called with a spinlock held */ spin_unlock(&fdp_from->fd_spin); /* * Reserve a fd in the process p_to. */ error = fdalloc(p_to, 1, &newfd); if (error) { fdrop(fp); return (error); } /* * Set fd for the fp file. */ fsetfd(fdp_to, fp, newfd); fdp_to->fd_files[newfd].fileflags = flags; fdrop(fp); return (newfd); }