From c92cfd95be35df604345c96974b92665d2881acd Mon Sep 17 00:00:00 2001 From: jewelcodes Date: Sat, 5 Oct 2024 23:39:16 -0400 Subject: [PATCH] procfs: implemented stat() --- fs/procfs/src/include/procfs/procfs.h | 2 ++ fs/procfs/src/io.c | 25 +++++++++++++++++++++++++ fs/procfs/src/main.c | 16 +++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/fs/procfs/src/include/procfs/procfs.h b/fs/procfs/src/include/procfs/procfs.h index b9f906e..cdc1d1b 100644 --- a/fs/procfs/src/include/procfs/procfs.h +++ b/fs/procfs/src/include/procfs/procfs.h @@ -34,6 +34,8 @@ #define RESOLVE_DIRECTORY 0x10000 +extern SysInfoResponse *sysinfo; + void procfsMount(MountCommand *); void procfsStat(StatCommand *); void procfsOpen(OpenCommand *); diff --git a/fs/procfs/src/io.c b/fs/procfs/src/io.c index 4490867..f9a8ebc 100644 --- a/fs/procfs/src/io.c +++ b/fs/procfs/src/io.c @@ -11,6 +11,7 @@ #include #include #include +#include void procfsOpen(OpenCommand *ocmd) { pid_t pid; @@ -37,4 +38,28 @@ void procfsOpen(OpenCommand *ocmd) { ocmd->header.header.status = 0; luxSendDependency(ocmd); +} + +void procfsStat(StatCommand *scmd) { + scmd->header.header.response = 1; + scmd->header.header.length = sizeof(StatCommand); + + pid_t pid; + int res = resolve(scmd->path, &pid); + if(res < 0) { + scmd->header.header.status = -ENOENT; + luxSendDependency(scmd); + return; + } + + scmd->header.header.status = 0; + + memset(&scmd->buffer, 0, sizeof(struct stat)); + scmd->buffer.st_mode = S_IRUSR | S_IRGRP | S_IROTH; + if(res & RESOLVE_DIRECTORY) scmd->buffer.st_mode |= S_IFDIR; + + if(res == RESOLVE_KERNEL) scmd->buffer.st_size = strlen(sysinfo->kernel); + else scmd->buffer.st_size = 8; + + luxSendDependency(scmd); } \ No newline at end of file diff --git a/fs/procfs/src/main.c b/fs/procfs/src/main.c index 6b1fabf..b37d26c 100644 --- a/fs/procfs/src/main.c +++ b/fs/procfs/src/main.c @@ -11,16 +11,25 @@ #include #include +SysInfoResponse *sysinfo; + int main() { luxInit("procfs"); while(luxConnectDependency("vfs")); // file system driver SyscallHeader *req = calloc(1, SERVER_MAX_SIZE); SyscallHeader *res = calloc(1, SERVER_MAX_SIZE); + sysinfo = calloc(1, sizeof(SysInfoResponse)); + + if(!req || !res || !sysinfo) { + luxLog(KPRINT_LEVEL_ERROR, "unable to allocate memory for procfs server\n"); + return -1; + } - if(!req || !res) { - luxLog(KPRINT_LEVEL_ERROR, "unable to allocate memory for procfs messages\n"); - exit(-1); + // request kernel info + if(luxSysinfo(sysinfo)) { + luxLog(KPRINT_LEVEL_ERROR, "failed to acquire kernel sysinfo\n"); + return -1; } // notify the vfs that this is a file system driver @@ -54,6 +63,7 @@ int main() { switch(req->header.command) { case COMMAND_MOUNT: procfsMount((MountCommand *) req); break; case COMMAND_OPEN: procfsOpen((OpenCommand *) req); break; + case COMMAND_STAT: procfsStat((StatCommand *) req); break; default: luxLogf(KPRINT_LEVEL_WARNING, "unimplemented command 0x%X, dropping message...\n", req->header.command); }