From 8a4f8366447c7ff950e83d744cea2c0c1fd60744 Mon Sep 17 00:00:00 2001 From: jewelcodes Date: Sat, 5 Oct 2024 23:33:03 -0400 Subject: [PATCH] procfs: refactored and implemented open() --- fs/procfs/src/include/procfs/procfs.h | 14 ++++++---- fs/procfs/src/io.c | 40 +++++++++++++++++++++++++++ fs/procfs/src/main.c | 3 +- fs/procfs/src/mount.c | 12 ++++---- 4 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 fs/procfs/src/io.c diff --git a/fs/procfs/src/include/procfs/procfs.h b/fs/procfs/src/include/procfs/procfs.h index af20deb..b9f906e 100644 --- a/fs/procfs/src/include/procfs/procfs.h +++ b/fs/procfs/src/include/procfs/procfs.h @@ -32,10 +32,12 @@ #define RESOLVE_PID_CHILDREN (9 | RESOLVE_PID) /* /proc/pid/children */ #define RESOLVE_PID_STAT (10 | RESOLVE_PID) /* /proc/pid/stat */ -void procfsMount(MountCommand *, MountCommand *); -void procfsStat(StatCommand *, StatCommand *); -void procfsOpen(OpenCommand *, OpenCommand *); -void procfsRead(RWCommand *, RWCommand *); -void procfsWrite(RWCommand *, RWCommand *); +#define RESOLVE_DIRECTORY 0x10000 -int procfsResolve(const char *, pid_t *); +void procfsMount(MountCommand *); +void procfsStat(StatCommand *); +void procfsOpen(OpenCommand *); +void procfsRead(RWCommand *); +void procfsWrite(RWCommand *); + +int resolve(const char *, pid_t *); diff --git a/fs/procfs/src/io.c b/fs/procfs/src/io.c new file mode 100644 index 0000000..4490867 --- /dev/null +++ b/fs/procfs/src/io.c @@ -0,0 +1,40 @@ +/* + * luxOS - a unix-like operating system + * Omar Elghoul, 2024 + * + * procfs: Microkernel server implementing the /proc file system + */ + +#include +#include +#include +#include +#include +#include + +void procfsOpen(OpenCommand *ocmd) { + pid_t pid; + + ocmd->header.header.response = 1; + ocmd->header.header.length = sizeof(OpenCommand); + + int res = resolve(ocmd->path, &pid); + if(res < 0) { + ocmd->header.header.status = -ENOENT; + luxSendDependency(ocmd); + return; + } + + if(res & RESOLVE_DIRECTORY) { + ocmd->header.header.status = -EISDIR; + luxSendDependency(ocmd); + return; + } + + if(ocmd->flags & O_WRONLY) + ocmd->header.header.status = -EPERM; + else + ocmd->header.header.status = 0; + + luxSendDependency(ocmd); +} \ No newline at end of file diff --git a/fs/procfs/src/main.c b/fs/procfs/src/main.c index 3adfe4d..6b1fabf 100644 --- a/fs/procfs/src/main.c +++ b/fs/procfs/src/main.c @@ -52,7 +52,8 @@ int main() { luxRecvDependency(req, req->header.length, false, false); switch(req->header.command) { - case COMMAND_MOUNT: procfsMount((MountCommand *) req, (MountCommand *) res); break; + case COMMAND_MOUNT: procfsMount((MountCommand *) req); break; + case COMMAND_OPEN: procfsOpen((OpenCommand *) req); break; default: luxLogf(KPRINT_LEVEL_WARNING, "unimplemented command 0x%X, dropping message...\n", req->header.command); } diff --git a/fs/procfs/src/mount.c b/fs/procfs/src/mount.c index 5affcc4..a9a9c79 100644 --- a/fs/procfs/src/mount.c +++ b/fs/procfs/src/mount.c @@ -10,15 +10,13 @@ /* procfsMount(): mounts the /proc file system * params: req - request buffer - * params: res - response buffer * returns: nothing */ -void procfsMount(MountCommand *req, MountCommand *res) { - memcpy(res, req, sizeof(MountCommand)); - res->header.header.response = 1; - res->header.header.length = sizeof(MountCommand); - res->header.header.status = 0; // success +void procfsMount(MountCommand *req) { + req->header.header.response = 1; + req->header.header.length = sizeof(MountCommand); + req->header.header.status = 0; // success - luxSendDependency(res); + luxSendDependency(req); } \ No newline at end of file