2006-09-06 20:19:11 +02:00
|
|
|
#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 20:40:28 +02:00
|
|
|
#include "file.h"
|
2006-09-06 20:19:11 +02:00
|
|
|
#include "fcntl.h"
|
|
|
|
|
2006-09-07 16:13:26 +02: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-06 20:19:11 +02:00
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
int fd;
|
|
|
|
struct file *f;
|
|
|
|
|
|
|
|
if(argint(argno, &fd) < 0)
|
|
|
|
return -1;
|
2007-08-09 19:32:40 +02:00
|
|
|
if(fd < 0 || fd >= NOFILE || (f=cp->ofile[fd]) == 0)
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
|
|
|
if(pfd)
|
|
|
|
*pfd = fd;
|
|
|
|
if(pf)
|
|
|
|
*pf = f;
|
2006-09-06 20:19:11 +02:00
|
|
|
return 0;
|
2006-09-07 16:13:26 +02:00
|
|
|
}
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02: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 19:32:40 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
for(fd = 0; fd < NOFILE; fd++){
|
2007-08-09 19:32:40 +02:00
|
|
|
if(cp->ofile[fd] == 0){
|
|
|
|
cp->ofile[fd] = f;
|
2006-09-07 16:13:26 +02:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-09-07 16:13:26 +02:00
|
|
|
sys_pipe(void)
|
2006-09-06 20:19:11 +02:00
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
int *fd;
|
2007-08-10 19:17:42 +02:00
|
|
|
struct file *rf, *wf;
|
2006-09-07 16:13:26 +02:00
|
|
|
int fd0, fd1;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argptr(0, (void*)&fd, 2*sizeof fd[0]) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
if(pipe_alloc(&rf, &wf) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
fd0 = -1;
|
|
|
|
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
|
|
|
|
if(fd0 >= 0)
|
2007-08-09 19:32:40 +02:00
|
|
|
cp->ofile[fd0] = 0;
|
2006-09-07 16:13:26 +02:00
|
|
|
fileclose(rf);
|
|
|
|
fileclose(wf);
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
}
|
2006-09-07 16:38:56 +02:00
|
|
|
fd[0] = fd0;
|
|
|
|
fd[1] = fd1;
|
2006-09-07 16:13:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_write(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
int n;
|
|
|
|
char *cp;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
|
|
|
|
return -1;
|
|
|
|
return filewrite(f, cp, n);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_read(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
struct file *f;
|
|
|
|
int n;
|
|
|
|
char *cp;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
return fileread(f, cp, n);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_close(void)
|
|
|
|
{
|
|
|
|
int fd;
|
2006-09-07 16:13:26 +02:00
|
|
|
struct file *f;
|
|
|
|
|
|
|
|
if(argfd(0, &fd, &f) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2007-08-10 18:37:27 +02:00
|
|
|
cp->ofile[fd] = 0;
|
2006-09-07 16:13:26 +02:00
|
|
|
fileclose(f);
|
2006-09-06 20:19:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_open(void)
|
|
|
|
{
|
|
|
|
struct inode *ip, *dp;
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
|
|
|
int omode;
|
|
|
|
int fd;
|
|
|
|
struct file *f;
|
2006-09-06 20:19:11 +02:00
|
|
|
char *last;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(omode & O_CREATE){
|
|
|
|
dp = namei(path, NAMEI_CREATE, 0, &last, &ip);
|
2006-09-06 20:19:11 +02:00
|
|
|
if(dp){
|
|
|
|
ip = mknod1(dp, last, T_FILE, 0, 0);
|
|
|
|
iput(dp);
|
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
|
|
|
} else if(ip == 0){
|
|
|
|
return -1;
|
|
|
|
} else if(ip->type == T_DIR){
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2006-09-07 16:13:26 +02:00
|
|
|
ip = namei(path, NAMEI_LOOKUP, 0, 0, 0);
|
2006-09-06 20:19:11 +02:00
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-07 16:13:26 +02:00
|
|
|
if(ip->type == T_DIR && ((omode & O_RDWR) || (omode & O_WRONLY))){
|
2006-09-06 20:19:11 +02:00
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if((f = filealloc()) == 0){
|
2006-09-06 20:19:11 +02:00
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-07 16:13:26 +02:00
|
|
|
if((fd = fdalloc(f)) < 0){
|
2006-09-06 20:19:11 +02:00
|
|
|
iput(ip);
|
2006-09-07 16:13:26 +02:00
|
|
|
fileclose(f);
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iunlock(ip);
|
2006-09-07 16:13:26 +02: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;
|
2006-09-06 20:19:11 +02:00
|
|
|
} else {
|
2006-09-07 16:13:26 +02:00
|
|
|
f->readable = 1;
|
|
|
|
f->writable = 0;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
2006-09-07 16:13:26 +02:00
|
|
|
f->ip = ip;
|
|
|
|
f->off = 0;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
return fd;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_mknod(void)
|
|
|
|
{
|
|
|
|
struct inode *nip;
|
2006-09-07 16:13:26 +02: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)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(len >= DIRSIZ)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if((nip = mknod(path, type, major, minor)) == 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
iput(nip);
|
|
|
|
return 0;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_mkdir(void)
|
|
|
|
{
|
|
|
|
struct inode *nip;
|
|
|
|
struct inode *dp;
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
2006-09-06 20:19:11 +02:00
|
|
|
struct dirent de;
|
|
|
|
char *last;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argstr(0, &path) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
dp = namei(path, NAMEI_CREATE, 0, &last, 0);
|
2006-09-06 20:19:11 +02:00
|
|
|
if(dp == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
nip = mknod1(dp, last, T_DIR, 0, 0);
|
|
|
|
if(nip == 0){
|
|
|
|
iput(dp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-07 17:29:54 +02:00
|
|
|
dp->nlink++;
|
2006-09-06 20:19:11 +02:00
|
|
|
iupdate(dp);
|
|
|
|
|
|
|
|
memset(de.name, '\0', DIRSIZ);
|
|
|
|
de.name[0] = '.';
|
|
|
|
de.inum = nip->inum;
|
|
|
|
writei(nip, (char*) &de, 0, sizeof(de));
|
|
|
|
|
|
|
|
de.inum = dp->inum;
|
|
|
|
de.name[1] = '.';
|
|
|
|
writei(nip, (char*) &de, sizeof(de), sizeof(de));
|
|
|
|
|
|
|
|
iput(dp);
|
|
|
|
iput(nip);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_chdir(void)
|
|
|
|
{
|
|
|
|
struct inode *ip;
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if(argstr(0, &path) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2006-09-07 16:13:26 +02:00
|
|
|
if((ip = namei(path, NAMEI_LOOKUP, 0, 0, 0)) == 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
|
2007-08-09 19:53:03 +02:00
|
|
|
if(ip == cp->cwd) {
|
2006-09-06 20:19:11 +02:00
|
|
|
iput(ip);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ip->type != T_DIR) {
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-08-09 19:32:40 +02:00
|
|
|
idecref(cp->cwd);
|
|
|
|
cp->cwd = ip;
|
|
|
|
iunlock(cp->cwd);
|
2006-09-06 20:19:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_unlink(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
if(argstr(0, &path) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
return unlink(path);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_fstat(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
struct file *f;
|
|
|
|
struct stat *st;
|
|
|
|
|
|
|
|
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof *st) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
return filestat(f, st);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_dup(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
struct file *f;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if(argfd(0, 0, &f) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
if((fd=fdalloc(f)) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
fileincref(f);
|
|
|
|
return fd;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_link(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
char *old, *new;
|
|
|
|
|
|
|
|
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
return link(old, new);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_exec(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
uint sz=0, ap, sp, p1, p2;
|
2006-09-06 20:19:11 +02:00
|
|
|
int i, nargs, argbytes, len;
|
|
|
|
struct inode *ip;
|
|
|
|
struct elfhdr elf;
|
|
|
|
struct proghdr ph;
|
|
|
|
char *mem = 0;
|
2007-08-08 10:38:11 +02:00
|
|
|
char *path, *s, *last;
|
2006-09-07 16:13:26 +02:00
|
|
|
uint argv;
|
|
|
|
|
|
|
|
if(argstr(0, &path) < 0 || argint(1, (int*)&argv) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2006-09-07 16:13:26 +02:00
|
|
|
|
|
|
|
ip = namei(path, NAMEI_LOOKUP, 0, 0, 0);
|
2006-09-06 20:19:11 +02:00
|
|
|
if(ip == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if(elf.magic != ELF_MAGIC)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
sz = 0;
|
|
|
|
for(i = 0; i < elf.phnum; i++){
|
|
|
|
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
|
|
|
|
sizeof(ph)) != sizeof(ph))
|
|
|
|
goto bad;
|
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
|
|
|
if(ph.memsz < ph.filesz)
|
|
|
|
goto bad;
|
|
|
|
sz += ph.memsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz += 4096 - (sz % 4096);
|
|
|
|
sz += 4096;
|
|
|
|
|
|
|
|
mem = kalloc(sz);
|
|
|
|
if(mem == 0)
|
|
|
|
goto bad;
|
|
|
|
memset(mem, 0, sz);
|
|
|
|
|
|
|
|
nargs = 0;
|
|
|
|
argbytes = 0;
|
2006-09-07 16:13:26 +02:00
|
|
|
for(i = 0;; i++){
|
|
|
|
if(fetchint(cp, argv + 4*i, (int*)&ap) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
goto bad;
|
|
|
|
if(ap == 0)
|
|
|
|
break;
|
2006-09-07 16:13:26 +02:00
|
|
|
len = fetchstr(cp, ap, &s);
|
2006-09-06 20:19:11 +02:00
|
|
|
if(len < 0)
|
|
|
|
goto bad;
|
|
|
|
nargs++;
|
|
|
|
argbytes += len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// argn\0
|
|
|
|
// ...
|
|
|
|
// arg0\0
|
|
|
|
// 0
|
|
|
|
// ptr to argn
|
|
|
|
// ...
|
|
|
|
// 12: ptr to arg0
|
|
|
|
// 8: argv (points to ptr to arg0)
|
|
|
|
// 4: argc
|
|
|
|
// 0: fake return pc
|
|
|
|
sp = sz - argbytes - (nargs+1)*4 - 4 - 4 - 4;
|
|
|
|
*(uint*)(mem + sp) = 0xffffffff;
|
|
|
|
*(uint*)(mem + sp + 4) = nargs;
|
|
|
|
*(uint*)(mem + sp + 8) = (uint)(sp + 12);
|
|
|
|
|
|
|
|
p1 = sp + 12;
|
|
|
|
p2 = sp + 12 + (nargs + 1) * 4;
|
|
|
|
for(i = 0; i < nargs; i++){
|
2006-09-07 16:13:26 +02:00
|
|
|
fetchint(cp, argv + 4*i, (int*)&ap);
|
|
|
|
len = fetchstr(cp, ap, &s);
|
|
|
|
memmove(mem + p2, s, len + 1);
|
2006-09-06 20:19:11 +02:00
|
|
|
*(uint*)(mem + p1) = p2;
|
|
|
|
p1 += 4;
|
|
|
|
p2 += len + 1;
|
|
|
|
}
|
|
|
|
*(uint*)(mem + p1) = 0;
|
|
|
|
|
2007-08-08 10:38:11 +02:00
|
|
|
// Save name for debugging.
|
|
|
|
for(last=s=path; *s; s++)
|
|
|
|
if(*s == '/')
|
|
|
|
last = s+1;
|
|
|
|
safestrcpy(cp->name, last, sizeof cp->name);
|
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
// commit to the new image.
|
|
|
|
kfree(cp->mem, cp->sz);
|
|
|
|
cp->sz = sz;
|
|
|
|
cp->mem = mem;
|
|
|
|
mem = 0;
|
|
|
|
|
|
|
|
for(i = 0; i < elf.phnum; i++){
|
|
|
|
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
|
|
|
|
sizeof(ph)) != sizeof(ph))
|
|
|
|
goto bad2;
|
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
|
|
|
if(ph.va + ph.memsz > sz)
|
|
|
|
goto bad2;
|
|
|
|
if(readi(ip, cp->mem + ph.va, ph.offset, ph.filesz) != ph.filesz)
|
|
|
|
goto bad2;
|
|
|
|
memset(cp->mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz);
|
|
|
|
}
|
|
|
|
|
|
|
|
iput(ip);
|
2007-08-08 10:38:11 +02:00
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
cp->tf->eip = elf.entry;
|
|
|
|
cp->tf->esp = sp;
|
|
|
|
setupsegs(cp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if(mem)
|
|
|
|
kfree(mem, sz);
|
|
|
|
iput(ip);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bad2:
|
|
|
|
iput(ip);
|
|
|
|
proc_exit();
|
|
|
|
return 0;
|
|
|
|
}
|