From a4f9da3a6a86f68e92a54d89deafce022bbdce97 Mon Sep 17 00:00:00 2001 From: CCXXXI Date: Sun, 10 Jan 2021 23:35:12 +0800 Subject: [PATCH] syscall open --- src/userprog/process.c | 3 +++ src/userprog/process.h | 2 ++ src/userprog/syscall.c | 27 +++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/userprog/process.c b/src/userprog/process.c index 4a438e3..170f5e7 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -583,6 +583,9 @@ struct process *process_create(struct thread *t) sema_init(&p->sema_load, 0); sema_init(&p->sema_wait, 0); + list_init(&p->files); + p->fd = 2; + return p; } diff --git a/src/userprog/process.h b/src/userprog/process.h index 8bc0bd2..8b7fd49 100644 --- a/src/userprog/process.h +++ b/src/userprog/process.h @@ -29,6 +29,8 @@ struct process struct process *parent; /* Parent. */ struct semaphore sema_load; /* Parent block on this while loading. */ struct semaphore sema_wait; /* Parent block on this while waiting. */ + struct list files; /* Opening files. */ + int fd; /* Max file descriptor num. */ }; void process_init(void); diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 96875ee..9b3faaa 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -7,6 +7,7 @@ #include "threads/vaddr.h" #include "threads/synch.h" #include "threads/palloc.h" +#include "threads/malloc.h" #include "userprog/pagedir.h" #include "userprog/process.h" #include "devices/shutdown.h" @@ -21,6 +22,13 @@ exit(-1); \ } +static struct open_file +{ + int fd; + struct file *file; + struct list_elem elem; +}; + static void syscall_handler(struct intr_frame *); static bool is_valid_ptr(const void *ptr); static bool is_user_mem(const void *start, size_t size); @@ -350,8 +358,23 @@ static bool remove(const char *file) file position. */ static int open(const char *file) { - // todo - return -1; + USER_ASSERT(is_valid_str(file)); + + lock_acquire(&file_lock); + struct file *f = filesys_open(file); + lock_release(&file_lock); + + if (f == NULL) + return -1; + + struct process *self = thread_current()->process; + + struct open_file *open_file = malloc(sizeof(struct open_file)); + open_file->fd = self->fd++; + open_file->file = f; + list_push_back(&self->files, &open_file->elem); + + return open_file->fd; } /* Returns the size, in bytes, of the file open as FD. */