2006-09-06 20:19:11 +02:00
|
|
|
#include "types.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "defs.h"
|
2006-09-06 20:19:11 +02:00
|
|
|
#include "param.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "stat.h"
|
2006-09-06 20:19:11 +02:00
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "fs.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
|
2007-08-28 01:53:17 +02:00
|
|
|
argfd(int n, 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;
|
|
|
|
|
2007-08-28 01:53:17 +02:00
|
|
|
if(argint(n, &fd) < 0)
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
2009-08-31 08:02:08 +02:00
|
|
|
if(fd < 0 || fd >= NOFILE || (f=proc->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++){
|
2009-08-31 08:02:08 +02:00
|
|
|
if(proc->ofile[fd] == 0){
|
|
|
|
proc->ofile[fd] = f;
|
2006-09-07 16:13:26 +02:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-07-12 04:26:01 +02:00
|
|
|
int
|
|
|
|
sys_dup(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if(argfd(0, 0, &f) < 0)
|
|
|
|
return -1;
|
|
|
|
if((fd=fdalloc(f)) < 0)
|
|
|
|
return -1;
|
|
|
|
filedup(f);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
int
|
2007-08-22 08:01:32 +02:00
|
|
|
sys_read(void)
|
2006-09-06 20:19:11 +02:00
|
|
|
{
|
2007-08-22 08:01:32 +02:00
|
|
|
struct file *f;
|
|
|
|
int n;
|
2007-09-27 07:13:10 +02:00
|
|
|
char *p;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2007-09-27 07:13:10 +02:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2007-09-27 07:13:10 +02:00
|
|
|
return fileread(f, p, n);
|
2006-09-07 16:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_write(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
int n;
|
2007-09-27 07:13:10 +02:00
|
|
|
char *p;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2007-09-27 07:13:10 +02:00
|
|
|
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
|
2006-09-07 16:13:26 +02:00
|
|
|
return -1;
|
2007-09-27 07:13:10 +02:00
|
|
|
return filewrite(f, p, 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;
|
2009-08-31 08:02:08 +02:00
|
|
|
proc->ofile[fd] = 0;
|
2006-09-07 16:13:26 +02:00
|
|
|
fileclose(f);
|
2006-09-06 20:19:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:17:54 +02:00
|
|
|
int
|
|
|
|
sys_fstat(void)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
struct stat *st;
|
|
|
|
|
|
|
|
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
|
|
|
|
return -1;
|
|
|
|
return filestat(f, st);
|
|
|
|
}
|
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
// Create the path new as a link to the same inode as old.
|
|
|
|
int
|
|
|
|
sys_link(void)
|
|
|
|
{
|
|
|
|
char name[DIRSIZ], *new, *old;
|
|
|
|
struct inode *dp, *ip;
|
|
|
|
|
|
|
|
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
|
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
if((ip = namei(old)) == 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
if(ip->type == T_DIR){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ip->nlink++;
|
|
|
|
iupdate(ip);
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlock(ip);
|
|
|
|
|
|
|
|
if((dp = nameiparent(new, name)) == 0)
|
2009-05-31 04:07:51 +02:00
|
|
|
goto bad;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(dp);
|
2009-05-31 04:07:51 +02:00
|
|
|
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
|
|
|
|
iunlockput(dp);
|
2007-08-24 22:54:23 +02:00
|
|
|
goto bad;
|
2009-05-31 04:07:51 +02:00
|
|
|
}
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(dp);
|
|
|
|
iput(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
return 0;
|
2007-08-24 22:54:23 +02:00
|
|
|
|
|
|
|
bad:
|
|
|
|
ilock(ip);
|
|
|
|
ip->nlink--;
|
|
|
|
iupdate(ip);
|
|
|
|
iunlockput(ip);
|
|
|
|
return -1;
|
2007-08-22 08:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is the directory dp empty except for "." and ".." ?
|
|
|
|
static int
|
|
|
|
isdirempty(struct inode *dp)
|
|
|
|
{
|
|
|
|
int off;
|
|
|
|
struct dirent de;
|
|
|
|
|
|
|
|
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
|
|
|
|
if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
|
|
|
|
panic("isdirempty: readi");
|
|
|
|
if(de.inum != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:17:54 +02:00
|
|
|
//PAGEBREAK!
|
2007-08-22 08:01:32 +02:00
|
|
|
int
|
|
|
|
sys_unlink(void)
|
|
|
|
{
|
|
|
|
struct inode *ip, *dp;
|
|
|
|
struct dirent de;
|
|
|
|
char name[DIRSIZ], *path;
|
|
|
|
uint off;
|
|
|
|
|
|
|
|
if(argstr(0, &path) < 0)
|
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
if((dp = nameiparent(path, name)) == 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
|
|
|
|
// Cannot unlink "." or "..".
|
|
|
|
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
if((ip = dirlookup(dp, name, &off)) == 0){
|
|
|
|
iunlockput(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
|
|
|
|
if(ip->nlink < 1)
|
|
|
|
panic("unlink: nlink < 1");
|
|
|
|
if(ip->type == T_DIR && !isdirempty(ip)){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
|
|
|
iunlockput(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&de, 0, sizeof(de));
|
|
|
|
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
|
|
|
|
panic("unlink: writei");
|
2008-10-17 14:42:13 +02:00
|
|
|
if(ip->type == T_DIR){
|
|
|
|
dp->nlink--;
|
|
|
|
iupdate(dp);
|
|
|
|
}
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(dp);
|
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
ip->nlink--;
|
|
|
|
iupdate(ip);
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode*
|
2009-05-31 04:07:51 +02:00
|
|
|
create(char *path, short type, short major, short minor)
|
2007-08-22 08:01:32 +02:00
|
|
|
{
|
|
|
|
uint off;
|
|
|
|
struct inode *ip, *dp;
|
|
|
|
char name[DIRSIZ];
|
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
if((dp = nameiparent(path, name)) == 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return 0;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
|
2009-05-31 04:07:51 +02:00
|
|
|
if((ip = dirlookup(dp, name, &off)) != 0){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(dp);
|
|
|
|
ilock(ip);
|
2009-07-12 04:26:01 +02:00
|
|
|
if(type == T_FILE && ip->type == T_FILE)
|
|
|
|
return ip;
|
|
|
|
iunlockput(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-07-12 04:26:01 +02:00
|
|
|
|
|
|
|
if((ip = ialloc(dp->dev, type)) == 0)
|
|
|
|
panic("create: ialloc");
|
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
ip->major = major;
|
|
|
|
ip->minor = minor;
|
2007-08-27 18:06:15 +02:00
|
|
|
ip->nlink = 1;
|
2007-08-22 08:01:32 +02:00
|
|
|
iupdate(ip);
|
|
|
|
|
|
|
|
if(type == T_DIR){ // Create . and .. entries.
|
|
|
|
dp->nlink++; // for ".."
|
|
|
|
iupdate(dp);
|
|
|
|
// No ip->nlink++ for ".": avoid cyclic ref count.
|
|
|
|
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
|
2007-08-27 16:39:50 +02:00
|
|
|
panic("create dots");
|
2007-08-22 08:01:32 +02:00
|
|
|
}
|
2008-10-16 17:18:49 +02:00
|
|
|
|
2009-05-31 04:07:51 +02:00
|
|
|
if(dirlink(dp, name, ip->inum) < 0)
|
|
|
|
panic("create: dirlink");
|
2008-10-16 17:18:49 +02:00
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(dp);
|
2007-08-22 08:01:32 +02:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
int
|
|
|
|
sys_open(void)
|
|
|
|
{
|
2007-08-21 21:22:08 +02:00
|
|
|
char *path;
|
|
|
|
int fd, omode;
|
2006-09-07 16:13:26 +02:00
|
|
|
struct file *f;
|
2007-08-21 21:22:08 +02:00
|
|
|
struct inode *ip;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
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;
|
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
if(omode & O_CREATE){
|
2009-05-31 04:07:51 +02:00
|
|
|
if((ip = create(path, T_FILE, 0, 0)) == 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
2007-08-28 20:37:41 +02:00
|
|
|
} else {
|
2007-08-24 22:54:23 +02:00
|
|
|
if((ip = namei(path)) == 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2009-05-31 04:07:51 +02:00
|
|
|
if(ip->type == T_DIR && omode != O_RDONLY){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
|
|
|
|
if(f)
|
|
|
|
fileclose(f);
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlock(ip);
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
f->type = FD_INODE;
|
2007-08-24 22:54:23 +02:00
|
|
|
f->ip = ip;
|
2007-08-22 08:01:32 +02:00
|
|
|
f->off = 0;
|
2007-08-27 16:39:50 +02:00
|
|
|
f->readable = !(omode & O_WRONLY);
|
|
|
|
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
|
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
|
|
|
}
|
|
|
|
|
2009-07-12 04:26:01 +02:00
|
|
|
int
|
|
|
|
sys_mkdir(void)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct inode *ip;
|
|
|
|
|
|
|
|
if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0)
|
|
|
|
return -1;
|
|
|
|
iunlockput(ip);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-06 20:19:11 +02:00
|
|
|
int
|
|
|
|
sys_mknod(void)
|
|
|
|
{
|
2007-08-22 08:01:32 +02:00
|
|
|
struct inode *ip;
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
|
|
|
int len;
|
2007-08-24 22:59:43 +02:00
|
|
|
int major, minor;
|
2006-09-07 16:13:26 +02:00
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
if((len=argstr(0, &path)) < 0 ||
|
|
|
|
argint(1, &major) < 0 ||
|
|
|
|
argint(2, &minor) < 0 ||
|
2009-05-31 04:07:51 +02:00
|
|
|
(ip = create(path, T_DEV, major, minor)) == 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2006-09-07 16:13:26 +02:00
|
|
|
return 0;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_chdir(void)
|
|
|
|
{
|
2006-09-07 16:13:26 +02:00
|
|
|
char *path;
|
2007-08-22 08:01:32 +02:00
|
|
|
struct inode *ip;
|
2006-09-06 20:19:11 +02:00
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
if(argstr(0, &path) < 0 || (ip = namei(path)) == 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2007-08-28 20:32:08 +02:00
|
|
|
if(ip->type != T_DIR){
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlockput(ip);
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlock(ip);
|
2009-08-31 08:02:08 +02:00
|
|
|
iput(proc->cwd);
|
|
|
|
proc->cwd = ip;
|
2006-09-06 20:19:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_exec(void)
|
|
|
|
{
|
2007-08-22 08:01:32 +02:00
|
|
|
char *path, *argv[20];
|
2007-08-21 21:22:08 +02:00
|
|
|
int i;
|
|
|
|
uint uargv, uarg;
|
2006-09-07 16:13:26 +02:00
|
|
|
|
2007-08-21 21:22:08 +02:00
|
|
|
if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0)
|
2006-09-06 20:19:11 +02:00
|
|
|
return -1;
|
2007-08-22 08:01:32 +02:00
|
|
|
memset(argv, 0, sizeof(argv));
|
2007-08-21 21:22:08 +02:00
|
|
|
for(i=0;; i++){
|
2007-08-22 08:01:32 +02:00
|
|
|
if(i >= NELEM(argv))
|
2007-08-21 21:22:08 +02:00
|
|
|
return -1;
|
2009-08-31 08:02:08 +02:00
|
|
|
if(fetchint(proc, uargv+4*i, (int*)&uarg) < 0)
|
2007-08-21 21:22:08 +02:00
|
|
|
return -1;
|
|
|
|
if(uarg == 0){
|
|
|
|
argv[i] = 0;
|
2006-09-06 20:19:11 +02:00
|
|
|
break;
|
2007-08-21 21:22:08 +02:00
|
|
|
}
|
2009-08-31 08:02:08 +02:00
|
|
|
if(fetchstr(proc, uarg, &argv[i]) < 0)
|
2007-08-21 21:22:08 +02:00
|
|
|
return -1;
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
2007-08-21 21:22:08 +02:00
|
|
|
return exec(path, argv);
|
2006-09-06 20:19:11 +02:00
|
|
|
}
|
2007-08-21 21:22:08 +02:00
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
int
|
|
|
|
sys_pipe(void)
|
|
|
|
{
|
|
|
|
int *fd;
|
|
|
|
struct file *rf, *wf;
|
|
|
|
int fd0, fd1;
|
|
|
|
|
|
|
|
if(argptr(0, (void*)&fd, 2*sizeof(fd[0])) < 0)
|
|
|
|
return -1;
|
2007-08-28 06:22:35 +02:00
|
|
|
if(pipealloc(&rf, &wf) < 0)
|
2007-08-22 08:01:32 +02:00
|
|
|
return -1;
|
|
|
|
fd0 = -1;
|
|
|
|
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
|
|
|
|
if(fd0 >= 0)
|
2009-08-31 08:02:08 +02:00
|
|
|
proc->ofile[fd0] = 0;
|
2007-08-22 08:01:32 +02:00
|
|
|
fileclose(rf);
|
|
|
|
fileclose(wf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd[0] = fd0;
|
|
|
|
fd[1] = fd1;
|
|
|
|
return 0;
|
|
|
|
}
|