Skip to content

Commit 4b2aff5

Browse files
Grates: Add launch scripts
1 parent a309186 commit 4b2aff5

File tree

26 files changed

+348
-607
lines changed

26 files changed

+348
-607
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ clang+llvm-*
99
#Ignore Build artifacts
1010
src/glibc/build/
1111
src/tmp/
12+
13+
.DS_Store

example_grate/README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
This is an example of a grate written with the new APIs. Runs the in-memory file system grate with an example program.
1+
This folder contains example grate implementations, as well as an example runner for grate-cage configurations.
22

33
### Compile
44

5-
`./tools/compile.sh`
5+
`./tools/compile.sh <grate source files>`
66

77
### Run
88

9-
- Compile `runopen.c` into `LIND_ROOT`
10-
- `./tools/run.sh`
9+
- Compile all cages present in the `cage/` folder, and place them into `LIND_ROOT` using the `scripts/lind_compile.sh` command.
10+
- Compile all grates present in the `grates/` folder, and place them into `LIND_ROOT`.
11+
- Run an example grate configuration using `./tools/run.sh`. This will run the following configuration:
12+
13+
```
14+
geteuid_grate:
15+
getegid_grate.wasm:
16+
bash -c "etest & imfs_grate runopen & getgid_grate gidtest"
17+
```
1118

1219
### File Structure
1320

1421
```
15-
imfs_grate.c // Grate source code with wrappers for individual syscalls.
16-
imfs.* // IMFS source code.
17-
runopen.c // Sample cage for testing
22+
grates/
23+
imfs_grate.c // Grate source code with wrappers for individual syscalls.
24+
imfs.* // IMFS source code.
25+
... // Other Grate examples
26+
27+
cages/
28+
mash.c // Barebones, minimal bash source code.
29+
etest.c
30+
runopen.c
31+
... // Example cage source code.
32+
1833
syscalls // List of syscalls along with the arguments required.
1934
2035
tools/

example_grate/cages/etest.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
4+
int main(void) {
5+
int cageid = getpid();
6+
printf("-------ETEST %d ------\n", cageid);
7+
printf("getegid() = %d\n", (int)getegid());
8+
printf("geteuid() = %d\n", (int)geteuid());
9+
printf("----(EXIT) ETEST----\n");
10+
return 0;
11+
}

example_grate/cages/gidtest.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
4+
int main(void) {
5+
int cageid = getpid();
6+
printf("--- GIDTEST %d ---\n", cageid);
7+
printf("getgid() = %d\n", (int)getgid());
8+
printf("--EXIT GIDTEST-\n");
9+
return 0;
10+
}

example_grate/cages/mash.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <ctype.h>
6+
#include <sys/wait.h>
7+
8+
#include <fcntl.h>
9+
10+
#include <dirent.h>
11+
12+
static char *trim(char *s) {
13+
while (isspace(*s)) s++;
14+
if (*s == 0) return s;
15+
char *end = s + strlen(s) - 1;
16+
while (end > s && isspace(*end)) end--;
17+
end[1] = '\0';
18+
return s;
19+
}
20+
21+
// split a string like "cmd arg1 arg2" into argv[]
22+
static char **make_argv(char *cmd) {
23+
int cap = 8, n = 0;
24+
char **argv = malloc(sizeof(char*) * cap);
25+
char *tok = strtok(cmd, " \t");
26+
while (tok) {
27+
if (n + 1 >= cap) {
28+
cap *= 2;
29+
argv = realloc(argv, sizeof(char*) * cap);
30+
}
31+
argv[n++] = tok;
32+
tok = strtok(NULL, " \t");
33+
}
34+
argv[n] = NULL;
35+
return argv;
36+
}
37+
38+
void list_dir(const char *path) {
39+
DIR *d = opendir(path);
40+
struct dirent *entry;
41+
printf("Contents of %s:\n", path);
42+
while ((entry = readdir(d)) != NULL) {
43+
printf(" %s\n", entry->d_name);
44+
}
45+
closedir(d);
46+
printf("\n");
47+
}
48+
49+
int main(int argc, char **argv) {
50+
if (argc < 3 || strcmp(argv[1], "-c") != 0) {
51+
fprintf(stderr, "usage: %s -c \"command\"\n", argv[0]);
52+
return 1;
53+
}
54+
55+
int fd = open("etest.wasm", O_WRONLY, 0);
56+
printf("FD etest: %d\n", fd);
57+
close(fd);
58+
59+
int nchildren=0;
60+
61+
char *cmd = strdup(argv[2]); // full string
62+
char *saveptr;
63+
char *segment = strtok_r(cmd, "&", &saveptr);
64+
65+
while (segment) {
66+
char *t = trim(segment);
67+
if (*t) {
68+
pid_t pid = fork();
69+
if (pid == 0) {
70+
// child: exec command
71+
char *copy = strdup(t);
72+
char **cmd_argv = make_argv(copy);
73+
74+
execv(cmd_argv[0], cmd_argv);
75+
char errbuf[256];
76+
snprintf(errbuf, sizeof(errbuf), "execv(%s)", cmd_argv[0]);
77+
perror(errbuf);
78+
exit(1);
79+
} else if (pid < 0) {
80+
perror("fork");
81+
}else {
82+
nchildren++;
83+
}
84+
// parent: do not wait (background)
85+
}
86+
segment = strtok_r(NULL, "&", &saveptr);
87+
}
88+
89+
// behave like bash when all jobs are background:
90+
// exit immediately, but reap zombies
91+
for (int i = 0; i < nchildren; i++) {
92+
int status;
93+
pid_t w = wait(&status);
94+
if (w > 0) {
95+
printf("[mash] terminated pid %d, status %d\n", w, status);
96+
}
97+
}
98+
99+
return 0;
100+
}

example_grate/cages/runopen.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <unistd.h>
2+
#include <sys/stat.h>
3+
#include <sys/utsname.h>
4+
#include <fcntl.h>
5+
#include <stdio.h>
6+
#include <error.h>
7+
#include <stdlib.h>
8+
9+
int main() {
10+
int cageid = getpid();
11+
printf("\n---- RUNOPEN %d ----\n", cageid);
12+
int fd = open("firstfile", O_CREAT | O_WRONLY, 0755);
13+
printf("FD: %d\n", fd);
14+
close(fd);
15+
printf("\n--EXIT RUNOPEN---\n");
16+
return 0;
17+
}

example_grate/cages/tester.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <unistd.h>
2+
#include <sys/stat.h>
3+
#include <sys/utsname.h>
4+
#include <fcntl.h>
5+
#include <stdio.h>
6+
#include <error.h>
7+
#include <stdlib.h>
8+
9+
int main() {
10+
printf("\n[PROG]: Top\n");
11+
int fd = open("etest.wasm", O_WRONLY, 0755);
12+
printf("\n[PROG]: FD: %d\n", fd);
13+
14+
close(fd);
15+
return 0;
16+
}

example_grate/gratecfg.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <sys/types.h>
3+
#include <sys/stat.h>
4+
#include <string.h>
5+
6+
#include <lind_syscall_num.h>
7+
#include "magic.h"
8+
9+
10+
int grate_syscalls[] = {GETEGID_SYSCALL};
11+
int grate_syscalls_len = 1;
12+
13+
void grate_init() {
14+
printf("EGID init'd\n");
15+
}
16+
17+
void grate_destroy() {
18+
printf("EGID exiting.\n");
19+
}
20+
21+
int getegid_syscall(int cageid) {
22+
return 556;
23+
}
24+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <sys/types.h>
3+
#include <sys/stat.h>
4+
#include <string.h>
5+
6+
#include <lind_syscall_num.h>
7+
#include "magic.h"
8+
9+
int grate_syscalls[] = {GETEUID_SYSCALL};
10+
int grate_syscalls_len = 1;
11+
12+
void grate_init() {
13+
printf("EUID initing...\n");
14+
}
15+
16+
void grate_destroy() {
17+
printf("EUID exiting.\n");
18+
}
19+
20+
int geteuid_syscall(int cageid) {
21+
return 546;
22+
}

0 commit comments

Comments
 (0)