xv6/sysfile.c

292 lines
4.4 KiB
C
Raw Normal View History

#include "types.h"
#include "stat.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
#include "spinlock.h"
#include "buf.h"
#include "fs.h"
#include "fsvar.h"
#include "elf.h"
2006-09-06 18:40:28 +00:00
#include "file.h"
#include "fcntl.h"
2006-09-07 14:13:26 +00:00
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
static int
argfd(int argno, int *pfd, struct file **pf)
{
2006-09-07 14:13:26 +00:00
int fd;
struct file *f;
if(argint(argno, &fd) < 0)
return -1;
2007-08-09 17:32:40 +00:00
if(fd < 0 || fd >= NOFILE || (f=cp->ofile[fd]) == 0)
2006-09-07 14:13:26 +00:00
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
2006-09-07 14:13:26 +00:00
}
2006-09-07 14:13:26 +00:00
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
2007-08-09 17:32:40 +00:00
2006-09-07 14:13:26 +00:00
for(fd = 0; fd < NOFILE; fd++){
2007-08-09 17:32:40 +00:00
if(cp->ofile[fd] == 0){
cp->ofile[fd] = f;
2006-09-07 14:13:26 +00:00
return fd;
}
}
return -1;
}
int
2006-09-07 14:13:26 +00:00
sys_pipe(void)
{
2006-09-07 14:13:26 +00:00
int *fd;
2007-08-10 17:17:42 +00:00
struct file *rf, *wf;
2006-09-07 14:13:26 +00:00
int fd0, fd1;
2006-09-07 14:13:26 +00:00
if(argptr(0, (void*)&fd, 2*sizeof fd[0]) < 0)
return -1;
2006-09-07 14:13:26 +00:00
if(pipe_alloc(&rf, &wf) < 0)
return -1;
2006-09-07 14:13:26 +00:00
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
2007-08-09 17:32:40 +00:00
cp->ofile[fd0] = 0;
2006-09-07 14:13:26 +00:00
fileclose(rf);
fileclose(wf);
return -1;
2006-09-07 14:13:26 +00:00
}
2006-09-07 14:38:56 +00:00
fd[0] = fd0;
fd[1] = fd1;
2006-09-07 14:13:26 +00:00
return 0;
}
int
sys_write(void)
{
struct file *f;
int n;
char *cp;
2006-09-07 14:13:26 +00:00
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
return filewrite(f, cp, n);
}
int
sys_read(void)
{
2006-09-07 14:13:26 +00:00
struct file *f;
int n;
char *cp;
2006-09-07 14:13:26 +00:00
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
2006-09-07 14:13:26 +00:00
return fileread(f, cp, n);
}
int
sys_close(void)
{
int fd;
2006-09-07 14:13:26 +00:00
struct file *f;
if(argfd(0, &fd, &f) < 0)
return -1;
2007-08-10 16:37:27 +00:00
cp->ofile[fd] = 0;
2006-09-07 14:13:26 +00:00
fileclose(f);
return 0;
}
int
sys_open(void)
{
char *path;
int fd, omode;
2006-09-07 14:13:26 +00:00
struct file *f;
struct inode *ip;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
return -1;
if(omode & O_CREATE)
ip = create(path);
else
ip = namei(path);
if(ip == 0)
return -1;
2007-08-20 19:37:15 +00:00
if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){
iput(ip);
return -1;
}
2006-09-07 14:13:26 +00:00
if((f = filealloc()) == 0){
iput(ip);
return -1;
}
2006-09-07 14:13:26 +00:00
if((fd = fdalloc(f)) < 0){
iput(ip);
2006-09-07 14:13:26 +00:00
fileclose(f);
return -1;
}
iunlock(ip);
2006-09-07 14:13:26 +00:00
f->type = FD_FILE;
if(omode & O_RDWR) {
f->readable = 1;
f->writable = 1;
} else if(omode & O_WRONLY) {
f->readable = 0;
f->writable = 1;
} else {
2006-09-07 14:13:26 +00:00
f->readable = 1;
f->writable = 0;
}
2006-09-07 14:13:26 +00:00
f->ip = ip;
f->off = 0;
2006-09-07 14:13:26 +00:00
return fd;
}
int
sys_mknod(void)
{
struct inode *nip;
2006-09-07 14:13:26 +00:00
char *path;
int len;
int type, major, minor;
if((len=argstr(0, &path)) < 0 || argint(1, &type) < 0 ||
argint(2, &major) < 0 || argint(3, &minor) < 0)
return -1;
// XXX why this check?
2006-09-07 14:13:26 +00:00
if(len >= DIRSIZ)
return -1;
2006-09-07 14:13:26 +00:00
if((nip = mknod(path, type, major, minor)) == 0)
return -1;
2006-09-07 14:13:26 +00:00
iput(nip);
return 0;
}
int
sys_mkdir(void)
{
char *path;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0)
return -1;
return mkdir(path);
}
int
sys_chdir(void)
{
struct inode *ip;
2006-09-07 14:13:26 +00:00
char *path;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0)
return -1;
2007-08-20 19:37:15 +00:00
if((ip = namei(path)) == 0)
return -1;
if(ip->type != T_DIR) {
iput(ip);
return -1;
}
2007-08-20 19:37:15 +00:00
iunlock(ip);
2007-08-09 17:32:40 +00:00
idecref(cp->cwd);
cp->cwd = ip;
return 0;
}
int
sys_unlink(void)
{
2006-09-07 14:13:26 +00:00
char *path;
if(argstr(0, &path) < 0)
return -1;
2006-09-07 14:13:26 +00:00
return unlink(path);
}
int
sys_fstat(void)
{
2006-09-07 14:13:26 +00:00
struct file *f;
struct stat *st;
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof *st) < 0)
return -1;
2006-09-07 14:13:26 +00:00
return filestat(f, st);
}
int
sys_dup(void)
{
2006-09-07 14:13:26 +00:00
struct file *f;
int fd;
if(argfd(0, 0, &f) < 0)
return -1;
2006-09-07 14:13:26 +00:00
if((fd=fdalloc(f)) < 0)
return -1;
2006-09-07 14:13:26 +00:00
fileincref(f);
return fd;
}
int
sys_link(void)
{
2006-09-07 14:13:26 +00:00
char *old, *new;
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1;
2006-09-07 14:13:26 +00:00
return link(old, new);
}
2007-08-21 20:01:11 +00:00
#define MAXARGS 20
int
sys_exec(void)
{
2007-08-21 20:01:11 +00:00
char *path, *argv[MAXARGS];
int i;
uint uargv, uarg;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0)
return -1;
memset(argv, 0, sizeof argv);
for(i=0;; i++){
2007-08-21 20:01:11 +00:00
if(i >= MAXARGS)
return -1;
if(fetchint(cp, uargv+4*i, (int*)&uarg) < 0)
return -1;
if(uarg == 0){
argv[i] = 0;
break;
}
if(fetchstr(cp, uarg, &argv[i]) < 0)
return -1;
}
return exec(path, argv);
}