From d7767be24bdcc0101c2ba08c03600b9f3c608663 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 26 Jul 2022 22:54:28 +0200 Subject: user/shell: automatically execute binaries in /bin/ --- src/user/app/init/main.c | 3 +-- src/user/app/init/shell.c | 39 ++++++++++++++++++++++++++++----------- src/user/bootstrap/main.c | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) (limited to 'src') 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 #include #include +#include #include #include #include @@ -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); -- cgit v1.2.3