Skip to content

Commit

Permalink
procfs: partial implementation of read()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Oct 6, 2024
1 parent c92cfd9 commit e16e13e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions fs/procfs/src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,76 @@ void procfsStat(StatCommand *scmd) {
else scmd->buffer.st_size = 8;

luxSendDependency(scmd);
}

void procfsRead(RWCommand *rcmd) {
rcmd->header.header.response = 1;
rcmd->header.header.length = sizeof(RWCommand);

pid_t pid;
int file = resolve(rcmd->path, &pid);
if(file < 0) {
rcmd->header.header.status = -ENOENT;
luxSendDependency(rcmd);
return;
}

RWCommand *res = calloc(1, sizeof(RWCommand) + rcmd->length);
if(!res) {
rcmd->header.header.status = -ENOMEM;
rcmd->length = 0;
luxSendDependency(rcmd);
return;
}

memcpy(res, rcmd, sizeof(RWCommand));

uint64_t data;
void *ptr = (void *) &data;
size_t size = 0;

switch(file) {
case RESOLVE_KERNEL:
ptr = sysinfo->kernel;
size = strlen(sysinfo->kernel);
break;
case RESOLVE_MEMSIZE:
luxSysinfo(sysinfo);
data = sysinfo->memorySize;
break;
case RESOLVE_MEMUSAGE:
luxSysinfo(sysinfo);
data = sysinfo->memoryUsage;
break;
case RESOLVE_UPTIME:
luxSysinfo(sysinfo);
data = sysinfo->uptime;
break;
default:
rcmd->header.header.status = -ENOENT;
rcmd->length = 0;
luxSendDependency(rcmd);
free(res);
return;
}

if(rcmd->position >= size) {
rcmd->header.header.status = -EOVERFLOW;
rcmd->length = 0;
luxSendDependency(rcmd);
free(res);
return;
}

size_t truelen;
if((rcmd->position + rcmd->length) > size) truelen = size - rcmd->position;
else truelen = size;

memcpy(res->data, ptr + rcmd->position, truelen);
res->length = truelen;
res->header.header.status = truelen;
res->header.header.length += truelen;
res->position += truelen;
luxSendDependency(res);
free(res);
}
1 change: 1 addition & 0 deletions fs/procfs/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int main() {
case COMMAND_MOUNT: procfsMount((MountCommand *) req); break;
case COMMAND_OPEN: procfsOpen((OpenCommand *) req); break;
case COMMAND_STAT: procfsStat((StatCommand *) req); break;
case COMMAND_READ: procfsRead((RWCommand *) req); break;
default:
luxLogf(KPRINT_LEVEL_WARNING, "unimplemented command 0x%X, dropping message...\n", req->header.command);
}
Expand Down

0 comments on commit e16e13e

Please sign in to comment.