diff --git a/fs/vfs.c b/fs/vfs.c index 0745c83..f0be809 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -16,7 +16,7 @@ typedef struct fs *(*vfs_init_t)(struct block_device *); ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count) { /* TODO: implement */ - if ((file->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_CHARDEV) + if (file->type == FTYPE_CHARDEV) return -EBADF; if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) @@ -28,7 +28,7 @@ ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count) ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) { /* TODO: implement */ - if ((file->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_CHARDEV) + if (file->type == FTYPE_CHARDEV) return -EBADF; /* TODO: implement */ @@ -56,12 +56,16 @@ struct file *vfs_open(const char *pathname, int *err) /* allocate a file */ struct file *file = slab_alloc(file_slab); if (!file) { - kprintf(LOG_ERROR "Failed to allocate file descriptor\n"); + kprintf(LOG_ERROR "Failed to allocate file struct\n"); return NULL; } memset(file, 0, sizeof(struct file)); + /* terrible temporary hack */ + if (pathname == NULL && (uintptr_t)err == 0x4) + return file; + /* find the file */ int result = rootfs->ops->open(rootfs, &file->vnode, pathname); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 2df9f25..5bd31a6 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -28,7 +28,7 @@ #define O_TRUNC 0x0010 #define O_APPEND 0x0020 -#define FD_TYPE_CHARDEV 0x0001 +#define FTYPE_CHARDEV 0x0001 typedef uint32_t ino_t; typedef uint8_t ftype_t; diff --git a/include/kernel/mem.h b/include/kernel/mem.h index 7294604..955a3c8 100644 --- a/include/kernel/mem.h +++ b/include/kernel/mem.h @@ -4,7 +4,6 @@ #include #include -#include #define BBMAP_FREE 0 #define BBMAP_SPLIT 1 @@ -33,6 +32,8 @@ genrw(ds); #undef genrw +struct proc; + struct page { /* I would rather refer to this as a struct */ union { struct { diff --git a/sched/proc.c b/sched/proc.c index 84c017f..156dcea 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -16,6 +16,7 @@ static struct proc kernel_procs[256] = { 0 }; void _return_to_user(struct procregs *regs, paddr_t cr3); extern uintptr_t kstacks[256]; +extern slab_t *fd_slab; struct procregs *proc_current_regs() { @@ -84,11 +85,20 @@ void proc_term(pid_t pid) rbt_delete(proc_tree, proc_node); - struct rbnode *map_node = rbt_minimum(proc->page_map.root); - while (map_node) { - struct rbnode *next = rbt_successor(map_node); - proc_munmap(proc, map_node->key); - map_node = next; + struct rbnode *node = rbt_minimum(proc->page_map.root); + while (node) { + struct rbnode *next = rbt_successor(node); + proc_munmap(proc, node->key); + node = next; + } + + node = rbt_minimum(proc->fd_map.root); + while (node) { + struct rbnode *next = rbt_successor(node); + struct file_descriptor *fdesc = (void *)node->value; + vfs_close(fdesc->file); + slab_free(fd_slab, fdesc); + node = next; } rbt_destroy(&proc->page_map); @@ -136,10 +146,12 @@ void schedule() static struct file_descriptor *proc_insert_charfd(struct proc *proc, int fdno, int flags) { struct file_descriptor *fd = fd_special(); + fd->file = vfs_open(NULL, (void *)0x4); /* FIXME LATER */ fd->file->vnode = (struct vnode){ 0 }; fd->pos = 0; fd->fd = fdno; fd->flags = flags; + fd->file->type = FTYPE_CHARDEV; fd->file->vnode.flags = VFS_VNO_CHARDEV; fd->buf = NULL; fd->buf_len = 0; diff --git a/sched/syscall.c b/sched/syscall.c index 2dc65c7..82577d2 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -12,7 +12,7 @@ typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5); syscall_t syscall_table[SYSCALL_MAX]; -static slab_t *fd_slab; +slab_t *fd_slab; struct file_descriptor *fd_special() {