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
+ }
0 commit comments