Skip to content

Commit

Permalink
kthd: read ELF file for exec() and relay response
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 30, 2024
1 parent e0c6950 commit 6f4ea2a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
75 changes: 75 additions & 0 deletions kthd/src/exec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* luxOS - a unix-like operating system
* Omar Elghoul, 2024
*
* kthd: Kernel Thread Helper Daemon
*/

#include <liblux/liblux.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fnctl.h>
#include <errno.h>

void exec(ExecCommand *cmd) {
cmd->header.header.response = 1;
cmd->header.header.length = sizeof(ExecCommand);

// open the program to be executed
int fd = open(cmd->path, O_RDONLY);
if(fd < 0) {
cmd->header.header.status = -ENOENT;
luxSendLumen(cmd);
return;
}

// ensure the requesting process has execute permissions
struct stat st;
if(fstat(fd, &st)) {
close(fd);
cmd->header.header.status = -1*errno;
luxSendLumen(cmd);
return;
}

cmd->header.header.status = 0;
if(cmd->header.header.requester == st.st_uid) {
if(!(st.st_mode & S_IXUSR)) cmd->header.header.status = -EPERM;
} else {
if(!(st.st_mode & S_IXOTH)) cmd->header.header.status = -EPERM;
}

if(cmd->header.header.status) {
close(fd);
luxSendLumen(cmd);
return;
}

// now read the file into memory
// allocate a new buffer for this
ExecCommand *res = calloc(1, sizeof(ExecCommand) + st.st_size);
if(!res) {
close(fd);
cmd->header.header.status = -ENOMEM;
luxSendLumen(cmd);
return;
}

memcpy(res, cmd, sizeof(ExecCommand));

if(read(fd, res->elf, st.st_size) != st.st_size) {
close(fd);
free(res);
cmd->header.header.status = -1*errno;
luxSendLumen(cmd);
return;
}

// and relay the response
res->header.header.length += st.st_size;
res->header.header.status = 0;
luxSendLumen(res);
free(res);
}
3 changes: 3 additions & 0 deletions kthd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <liblux/liblux.h>
#include <stdlib.h>

void exec(ExecCommand *);

int main() {
luxInit("kthd");

Expand Down Expand Up @@ -38,6 +40,7 @@ int main() {
luxRecvLumen(msg, SERVER_MAX_SIZE, false, false);

switch(msg->header.command) {
case COMMAND_EXEC: exec((ExecCommand *) msg); break;
default:
luxLogf(KPRINT_LEVEL_WARNING, "unimplemented command 0x%04X, dropping message...\n", msg->header.command);
}
Expand Down

0 comments on commit 6f4ea2a

Please sign in to comment.