proc: hack in special file descriptors

The previos commit broke executions of all program files due to a NULL
memory access when inserting special file descriptors. This will be
replaced when some sort of /dev fs is implemented
This commit is contained in:
Michael Shaw 2024-01-24 18:49:06 -05:00
commit e2ea205129
5 changed files with 28 additions and 11 deletions

View file

@ -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);

View file

@ -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;

View file

@ -4,7 +4,6 @@
#include <kernel/common.h>
#include <kernel/rbtree.h>
#include <kernel/proc.h>
#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 {

View file

@ -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;

View file

@ -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()
{