diff options
author | Marvin Borner | 2021-01-11 19:24:45 +0100 |
---|---|---|
committer | Marvin Borner | 2021-01-11 19:24:45 +0100 |
commit | 9ac1eda2988b6c7472a24817f4fd623de28a33f5 (patch) | |
tree | daac0f3f678a2be542eff36ebf144869c957626b /kernel | |
parent | 856b362a4b68d5776ea37dc30a50e7414288d877 (diff) |
Much work..
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/features/fs.c | 23 | ||||
-rw-r--r-- | kernel/features/load.c | 2 | ||||
-rw-r--r-- | kernel/features/proc.c | 72 | ||||
-rw-r--r-- | kernel/features/syscall.c | 4 | ||||
-rw-r--r-- | kernel/inc/proc.h | 3 |
5 files changed, 85 insertions, 19 deletions
diff --git a/kernel/features/fs.c b/kernel/features/fs.c index ab2e236..13ed0c2 100644 --- a/kernel/features/fs.c +++ b/kernel/features/fs.c @@ -121,6 +121,9 @@ u32 vfs_read(const char *path, void *buf, u32 offset, u32 count) if (count == 0 || offset > count) return 0; + while (*path == ' ') + path++; + struct mount_info *m = vfs_find_mount_info(path); assert(m && m->dev && m->dev->vfs && m->dev->vfs->read); @@ -128,15 +131,25 @@ u32 vfs_read(const char *path, void *buf, u32 offset, u32 count) if (len > 1) path += len; - struct device *dev = m->dev; - return dev->vfs->read(path, buf, offset, count, dev); + return m->dev->vfs->read(path, buf, offset, count, m->dev); } u32 vfs_write(const char *path, void *buf, u32 offset, u32 count) { - struct device *dev = vfs_find_dev(path); - assert(dev && dev->vfs && dev->vfs->write); - return dev->vfs->write(path, buf, offset, count, dev); + if (count == 0 || offset > count) + return 0; + + while (*path == ' ') + path++; + + struct mount_info *m = vfs_find_mount_info(path); + assert(m && m->dev && m->dev->vfs && m->dev->vfs->write); + + u32 len = strlen(m->path); + if (len > 1) + path += len; + + return m->dev->vfs->write(path, buf, offset, count, m->dev); } u32 vfs_stat(const char *path, struct stat *buf) diff --git a/kernel/features/load.c b/kernel/features/load.c index 1eb34d3..f7fc1a5 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -22,7 +22,7 @@ int bin_load(char *path, struct proc *proc) proc->regs.ebp = (u32)stack; proc->regs.useresp = (u32)stack; proc->regs.eip = (u32)data; - strcpy(proc->name, path + 1); + strcpy(proc->name, path); return data ? 0 : 1; } diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 499be81..79bc6c8 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -4,12 +4,11 @@ #include <boot.h> #include <cpu.h> #include <fs.h> -#include <interrupts.h> -#include <list.h> #include <load.h> #include <mem.h> #include <print.h> #include <proc.h> +#include <stack.h> #include <str.h> #include <timer.h> @@ -130,7 +129,7 @@ struct proc *proc_make(void) { struct proc *proc = malloc(sizeof(*proc)); proc->pid = current_pid++; - proc->messages = list_new(); + proc->messages = stack_new(); proc->state = PROC_RUNNING; if (current) @@ -139,7 +138,7 @@ struct proc *proc_make(void) return proc; } -u32 procfs_read(const char *path, void *buf, u32 offset, u32 count, struct device *dev) +u32 procfs_write(const char *path, void *buf, u32 offset, u32 count, struct device *dev) { while (*path == '/') path++; @@ -150,18 +149,74 @@ u32 procfs_read(const char *path, void *buf, u32 offset, u32 count, struct devic path++; } + if (!pid && !memcmp(path, "self/", 5)) { + pid = proc_current()->pid; + path += 4; + } + if (pid) { struct proc *p = proc_from_pid(pid); - if (!p) + if (!p || path[0] != '/') return 0; - if (!memcmp(path, "/status", 8)) { - printf("STATUS!\n"); + path++; + if (!memcmp(path, "msg", 4)) { + stack_push_bot(p->messages, buf); + return count; } } printf("%s - off: %d, cnt: %d, buf: %x, dev %x\n", path, offset, count, buf, dev); - return count; + return 0; +} + +u32 procfs_read(const char *path, void *buf, u32 offset, u32 count, struct device *dev) +{ + (void)dev; + + while (*path == '/') + path++; + + int pid = 0; + while (path[0] >= '0' && path[0] <= '9') { + pid = pid * 10 + (path[0] - '0'); + path++; + } + + if (!pid && !memcmp(path, "self/", 5)) { + pid = proc_current()->pid; + path += 4; + } + + if (pid) { + struct proc *p = proc_from_pid(pid); + if (!p || path[0] != '/') + return 0; + + path++; + if (!memcmp(path, "pid", 4)) { + //memcpy(buf, ((u8 *)p->pid) + offset, count); + *(u32 *)buf = p->pid; + return count; + } else if (!memcmp(path, "name", 5)) { + memcpy(buf, p->name + offset, count); + return count; + } else if (!memcmp(path, "status", 7)) { + const char *status = p->state == PROC_RUNNING ? "running" : "sleeping"; + memcpy(buf, status + offset, count); + return count; + } else if (!memcmp(path, "msg", 4)) { + if (stack_empty(p->messages)) { + return 0; + } else { + u8 *msg = stack_pop(p->messages); + memcpy(buf, msg + offset, count); + return count; + } + } + } + + return 0; } extern void proc_jump_userspace(void); @@ -180,6 +235,7 @@ void proc_init(void) struct vfs *vfs = malloc(sizeof(*vfs)); vfs->type = VFS_PROCFS; vfs->read = procfs_read; + vfs->write = procfs_write; struct device *dev = malloc(sizeof(*dev)); dev->name = "proc"; dev->type = DEV_CHAR; diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index bb174a1..dbb7033 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -73,10 +73,6 @@ void syscall_handler(struct regs *r) r->eax = timer_get(); break; } - case SYS_GETPID: { - r->eax = proc_current()->pid; - break; - } case SYS_NET_OPEN: { r->eax = (int)net_open(r->ebx); break; diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index 7a479be..d4146fe 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -6,6 +6,7 @@ #include <def.h> #include <interrupts.h> #include <list.h> +#include <stack.h> #include <sys.h> #define PROC_QUANTUM 42 // Milliseconds or something // TODO @@ -24,7 +25,7 @@ struct proc { struct regs regs; struct regs regs_backup; enum proc_state state; - struct list *messages; + struct stack *messages; }; struct proc *kernel_proc; |