Skip to content

Commit

Permalink
procfs: implemented stat()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Oct 6, 2024
1 parent 8a4f836 commit c92cfd9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions fs/procfs/src/include/procfs/procfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#define RESOLVE_DIRECTORY 0x10000

extern SysInfoResponse *sysinfo;

void procfsMount(MountCommand *);
void procfsStat(StatCommand *);
void procfsOpen(OpenCommand *);
Expand Down
25 changes: 25 additions & 0 deletions fs/procfs/src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdlib.h>
#include <fnctl.h>
#include <errno.h>
#include <sys/stat.h>

void procfsOpen(OpenCommand *ocmd) {
pid_t pid;
Expand All @@ -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);
}
16 changes: 13 additions & 3 deletions fs/procfs/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
#include <string.h>
#include <stdlib.h>

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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c92cfd9

Please sign in to comment.