diff options
author | dzwdz | 2022-07-26 22:54:28 +0200 |
---|---|---|
committer | dzwdz | 2022-07-26 22:54:28 +0200 |
commit | d7767be24bdcc0101c2ba08c03600b9f3c608663 (patch) | |
tree | 2ce6cb96fafad4814ad73c15ec32b702d3882280 | |
parent | 4aee2f759ab7bf2ad9534941afef0195040ba5db (diff) |
user/shell: automatically execute binaries in /bin/
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/user/app/init/main.c | 3 | ||||
-rw-r--r-- | src/user/app/init/shell.c | 39 | ||||
-rw-r--r-- | src/user/bootstrap/main.c | 2 |
4 files changed, 32 insertions, 16 deletions
@@ -87,7 +87,7 @@ out/hdd: define userbin_template = -out/initrd/$(1).elf: src/user/linker.ld \ +out/initrd/bin/$(1): src/user/linker.ld \ $(call from_sources, src/user/app/$(1)/) \ $(call from_sources, src/user/lib/) \ $(call from_sources, src/shared/) @@ -102,7 +102,7 @@ out/initrd/%: initrd/% @cp $< $@ out/initrd.tar: $(patsubst %,out/%,$(shell find initrd/ -type f)) \ - $(patsubst %,out/initrd/%.elf,$(USERBINS)) + $(patsubst %,out/initrd/bin/%,$(USERBINS)) @cd out/initrd; tar chf ../initrd.tar * diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c index 788fd78..4637703 100644 --- a/src/user/app/init/main.c +++ b/src/user/app/init/main.c @@ -18,8 +18,7 @@ int main(void) { MOUNT("/tmp/", tmpfs_drv()); MOUNT("/keyboard", ps2_drv()); MOUNT("/vga_tty", ansiterm_drv()); - - MOUNT("/bind/", fs_passthru(NULL)); + MOUNT("/bin/", fs_passthru("/init/bin")); if (fork()) { /* (used to) expose a bug in the kernel diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c index 85e56e1..048b713 100644 --- a/src/user/app/init/shell.c +++ b/src/user/app/init/shell.c @@ -3,6 +3,7 @@ #include <camellia/syscalls.h> #include <stdbool.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> #include <user/lib/elfload.h> @@ -137,8 +138,12 @@ void shell_loop(void) { readline(buf, 256); if (feof(stdin)) exit(0); - redir = strtrim(strsplit(buf, '>')); + cmd = strtrim(buf); + if (!*cmd) continue; + + redir = strtrim(strsplit(cmd, '>')); + cmd = strtrim(cmd); args = strtrim(strsplit(cmd, 0)); /* "special" commands that can't be handled in a subprocess */ @@ -162,15 +167,6 @@ void shell_loop(void) { if (!strcmp(cmd, "echo")) { printf("%s\n", args); - } else if (!strcmp(cmd, "exec")) { - FILE *file = fopen(args, "r"); - if (!file) { - printf("couldn't open file\n"); - } else { - elf_execf(file); - fclose(file); - printf("elf_execf failed\n"); - } } else if (!strcmp(cmd, "cat")) { cmd_cat_ls(args, false); } else if (!strcmp(cmd, "ls")) { @@ -194,7 +190,28 @@ void shell_loop(void) { } else if (!strcmp(cmd, "stress")) { stress_all(); } else { - printf("unknown command :(\n"); + char *binname = cmd; + if (*cmd != '/') { + size_t cmdlen = strlen(cmd); + binname = malloc(cmdlen); + if (!binname) { + printf("sh: out of memory.\n"); + exit(1); + } + memcpy(binname, "/bin/", 5); + memcpy(binname + 5, cmd, cmdlen + 1); + } + + FILE *file = fopen(binname, "r"); + if (!file) { + printf("unknown command: %s\n", cmd); + } else { + elf_execf(file); + fclose(file); + printf("couldn't execute %s\n", binname); + } + if (binname != cmd) + free(binname); } exit(0); } else { diff --git a/src/user/bootstrap/main.c b/src/user/bootstrap/main.c index 089fd68..9c08023 100644 --- a/src/user/bootstrap/main.c +++ b/src/user/bootstrap/main.c @@ -24,7 +24,7 @@ void _start(void) { MOUNT("/init/", tar_driver(&_initrd)); - void *init = tar_find("init.elf", 8, &_initrd, ~0) + 512; + void *init = tar_find("bin/init", 8, &_initrd, ~0) + 512; if (init) { _klogf("execing init.elf"); elf_exec(init); |