static int filemap_alloc(int fd, struct file **file_store) { struct file *file = get_filemap(); if (fd == NO_FD) { for (fd = 0; fd < FS_STRUCT_NENTRY; fd++, file++) { if (file->status == FD_NONE) { goto found; } } return -E_MAX_OPEN; } else { if (testfd(fd)) { file += fd; if (file->status == FD_NONE) { goto found; } return -E_BUSY; } return -E_INVAL; } found: assert(fopen_count(file) == 0); file->status = FD_INIT, file->node = NULL; *file_store = file; return 0; }
static inline int fd2file(int fd, struct file **file_store) { if (testfd(fd)) { struct file *file = get_filemap() + fd; if (file->status == FD_OPENED && file->fd == fd) { *file_store = file; return 0; } } return -E_INVAL; }
struct file* fd2file_onfs(int fd, struct fs_struct *fs_struct) { if(testfd(fd)) { assert(fs_struct != NULL && fs_count(fs_struct) > 0); struct file *file = fs_struct->filemap + fd; if((file->status == FD_OPENED || file->status == FD_CLOSED) && file->fd == fd) { return file; } } else { panic("testfd() failed"); } return NULL; }
int main(void) { int fd = dup(1); assert(fd >= 0); testfd("stdin", 0); testfd("stdout", 1); testfd("dup: stdout", fd); int size = 1024, len = 0; char buf[size]; len += snprintf(buf + len, size - len, "Hello world!!.\n"); len += snprintf(buf + len, size - len, "I am process %d.\n", getpid()); write(fd, buf, len); int ret; while ((ret = dup(fd)) >= 0) /* do nothing */ ; close(fd); len = snprintf(buf, size, "FAIL: T.T\n"); write(fd, buf, len); cprintf("dup fd ok.\n"); int pid; if ((pid = fork()) == 0) { sleep(10); len = snprintf(buf, size, "fork fd ok.\n"); ret = write(1, buf, len); assert(ret == len); exit(0); } assert(pid > 0); assert(waitpid(pid, &ret) == 0 && ret == 0); cprintf("fwrite_test pass.\n"); return 0; }
struct file* fd2file_onfs(int fd, struct fs_struct *fs_struct) { struct file_desc_table *desc_table = fs_get_desc_table(fs_struct); if(testfd(fd)) { assert(fs_struct != NULL && fs_count(fs_struct) > 0); struct file *file = file_desc_table_get_file(desc_table, fd); return file; /*if((file->status == FD_OPENED || file->status == FD_CLOSED) && file->fd == fd) { return file; }*/ } else { panic("testfd() failed %d", fd); } return NULL; }
int main(int argc, char *argv[]) { int i; struct uemux mux; parse_options(argc, argv); fclose(stdin); pkb_init_pools(1); ue_init(&mux, &estdmm); for (i = 3; i < 3 + g_nfd; ++i) { testfd(i); ue_io_new(&mux, UE_RD, i, readpkt, NULL); } ue_run(&mux); ue_fini(&mux); return 0; }