static int my_open(const char *file) { lock_acquire(&filesys_lock); if(file == NULL){ lock_release(&filesys_lock); return -1; } if(!address_valid(file)){ lock_release(&filesys_lock); my_exit(-1); } struct file *fp = filesys_open(file); int fd = thread_current()->fd; if(!fp){ lock_release(&filesys_lock); // printf("file open error\n"); return -1; } struct Elf32_Ehdr ehdr; if (!(file_read (fp, &ehdr, sizeof ehdr) != sizeof ehdr || memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) || ehdr.e_type != 2 || ehdr.e_machine != 3 || ehdr.e_version != 1 || ehdr.e_phentsize != sizeof (struct Elf32_Phdr) || ehdr.e_phnum > 1024)) { file_deny_write (fp); } file_seek(fp, 0); struct process_file *pf = malloc(sizeof(struct process_file)); pf->file = fp; pf->fd = fd; (thread_current()->fd)++; list_push_back(&thread_current()->file_list, &pf->elem); lock_release(&filesys_lock); return fd; }
/* Opens specified file. Returns its file descriptor. Same file opened multiple times returns different file descriptors. A process has an independent set of file descriptors (files open in that process). fd = 0 is STDIN_FILENO, fd = 1 is STDOUT_FILENO - These are never returned here. If file could not be opened, -1 is returned. */ static int sys_open(const char *file) { check_mem_ptr(file); if (file == NULL) sys_exit(ERROR); lock_acquire(&secure_file); struct file *fl = filesys_open(file); if (fl == NULL) { lock_release(&secure_file); return FD_ERROR; } struct thread *t = thread_current(); /* Freed in sys_close(). */ struct proc_file *f = malloc(sizeof(struct proc_file)); if (f == NULL) sys_exit(FD_ERROR); list_push_front(&t->files, &f->file_elem); f->file = fl; /* If file is currently being run as an executable in this process, we must not be able to write to it. */ if (is_executable(file)) file_deny_write(f->file); int file_descriptor = t->next_file_descriptor; f->fd = file_descriptor; /* Increment next_file_descriptor so that the next file to be opened has a different file descriptor. */ t->next_file_descriptor++; lock_release(&secure_file); return file_descriptor; }