Skip to content

Commit 6f4ea2a

Browse files
committed
kthd: read ELF file for exec() and relay response
1 parent e0c6950 commit 6f4ea2a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

kthd/src/exec.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* luxOS - a unix-like operating system
3+
* Omar Elghoul, 2024
4+
*
5+
* kthd: Kernel Thread Helper Daemon
6+
*/
7+
8+
#include <liblux/liblux.h>
9+
#include <sys/stat.h>
10+
#include <unistd.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <fnctl.h>
14+
#include <errno.h>
15+
16+
void exec(ExecCommand *cmd) {
17+
cmd->header.header.response = 1;
18+
cmd->header.header.length = sizeof(ExecCommand);
19+
20+
// open the program to be executed
21+
int fd = open(cmd->path, O_RDONLY);
22+
if(fd < 0) {
23+
cmd->header.header.status = -ENOENT;
24+
luxSendLumen(cmd);
25+
return;
26+
}
27+
28+
// ensure the requesting process has execute permissions
29+
struct stat st;
30+
if(fstat(fd, &st)) {
31+
close(fd);
32+
cmd->header.header.status = -1*errno;
33+
luxSendLumen(cmd);
34+
return;
35+
}
36+
37+
cmd->header.header.status = 0;
38+
if(cmd->header.header.requester == st.st_uid) {
39+
if(!(st.st_mode & S_IXUSR)) cmd->header.header.status = -EPERM;
40+
} else {
41+
if(!(st.st_mode & S_IXOTH)) cmd->header.header.status = -EPERM;
42+
}
43+
44+
if(cmd->header.header.status) {
45+
close(fd);
46+
luxSendLumen(cmd);
47+
return;
48+
}
49+
50+
// now read the file into memory
51+
// allocate a new buffer for this
52+
ExecCommand *res = calloc(1, sizeof(ExecCommand) + st.st_size);
53+
if(!res) {
54+
close(fd);
55+
cmd->header.header.status = -ENOMEM;
56+
luxSendLumen(cmd);
57+
return;
58+
}
59+
60+
memcpy(res, cmd, sizeof(ExecCommand));
61+
62+
if(read(fd, res->elf, st.st_size) != st.st_size) {
63+
close(fd);
64+
free(res);
65+
cmd->header.header.status = -1*errno;
66+
luxSendLumen(cmd);
67+
return;
68+
}
69+
70+
// and relay the response
71+
res->header.header.length += st.st_size;
72+
res->header.header.status = 0;
73+
luxSendLumen(res);
74+
free(res);
75+
}

kthd/src/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <liblux/liblux.h>
99
#include <stdlib.h>
1010

11+
void exec(ExecCommand *);
12+
1113
int main() {
1214
luxInit("kthd");
1315

@@ -38,6 +40,7 @@ int main() {
3840
luxRecvLumen(msg, SERVER_MAX_SIZE, false, false);
3941

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

0 commit comments

Comments
 (0)