From 7a98e998c5cda484611d19854649ab8535f503d4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 14 Feb 2023 17:09:59 +0100 Subject: add simplet init system --- Makefile | 15 +++++++++++---- build_config/linux.config | 4 ++-- config/env | 1 + config/init.hooks/devfs | 3 +++ config/init.hooks/procfs | 3 +++ config/init.hooks/shell | 6 ++++++ config/init.hooks/start | 8 ++++++++ config/init.hooks/stop | 2 ++ config/init.hooks/sysfs | 3 +++ core/for.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ core/init | 5 ----- core/mount.c | 2 +- core/unmount.c | 12 ++++++++++++ lib/exec/exec.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/exec/exec.h | 6 ++++++ lib/sys/unmount.h | 11 +++++++++++ smash/exec.c | 39 +++------------------------------------ smash/exec.h | 2 +- smash/main.c | 2 +- utils/init | 4 ++++ 20 files changed, 161 insertions(+), 50 deletions(-) create mode 100755 config/init.hooks/devfs create mode 100755 config/init.hooks/procfs create mode 100755 config/init.hooks/shell create mode 100755 config/init.hooks/start create mode 100755 config/init.hooks/stop create mode 100755 config/init.hooks/sysfs create mode 100644 core/for.c delete mode 100755 core/init create mode 100644 core/unmount.c create mode 100644 lib/exec/exec.c create mode 100644 lib/exec/exec.h create mode 100644 lib/sys/unmount.h create mode 100755 utils/init diff --git a/Makefile b/Makefile index d90bcc9..1d26ad3 100644 --- a/Makefile +++ b/Makefile @@ -34,13 +34,20 @@ target: initramfs: target core smash @echo Installing Initramfs - @mkdir -p ${INITRAMFS}/system/config ${INITRAMFS}/system/manual ${INITRAMFS}/system/core ${INITRAMFS}/system/utils ${INITRAMFS}/system/scripts ${INITRAMFS}/system/devices + @mkdir -p ${INITRAMFS}/system/config \ + ${INITRAMFS}/system/manual \ + ${INITRAMFS}/system/core \ + ${INITRAMFS}/system/utils \ + ${INITRAMFS}/system/scripts \ + ${INITRAMFS}/system/devices \ + ${INITRAMFS}/system/info \ + ${INITRAMFS}/system/process @cp core/objects/* ${INITRAMFS}/system/core/ - @cp core/init ${INITRAMFS}/system/core/ + @cp utils/init ${INITRAMFS}/system/core/ @cp smash/smash ${INITRAMFS}/system/core/ @cp utils/* ${INITRAMFS}/system/utils/ @cp man/* ${INITRAMFS}/system/manual/ - @cp config/* ${INITRAMFS}/system/config/ + @cp -r config/* ${INITRAMFS}/system/config/ @echo Building Initramfs @cd ${INITRAMFS} && find . | cpio -o --quiet --format=newc > ../initramfs.img @@ -52,7 +59,7 @@ ${TARGET}/kernel: @mv linux-${KERNEL_VERSION} ${TARGET}/kernel-src @cp build_config/linux.config ${TARGET}/kernel-src/.config @echo Building Kernel - @$(MAKE) -C ${TARGET}/kernel-src + $(MAKE) -C ${TARGET}/kernel-src @cp ${TARGET}/kernel-src/arch/x86_64/boot/bzImage ${TARGET}/kernel ${TARGET}/syslinux: diff --git a/build_config/linux.config b/build_config/linux.config index da8ec37..96eaddd 100644 --- a/build_config/linux.config +++ b/build_config/linux.config @@ -1432,9 +1432,9 @@ CONFIG_PCCARD_NONSTATIC=y # CONFIG_AUXILIARY_BUS=y # CONFIG_UEVENT_HELPER is not set -CONFIG_DEVTMPFS=n +CONFIG_DEVTMPFS=y CONFIG_DEVTMPFS_MOUNT=n -CONFIG_DEVTMPFS_SAFE=n +CONFIG_DEVTMPFS_SAFE=y CONFIG_STANDALONE=y CONFIG_PREVENT_FIRMWARE_BUILD=y diff --git a/config/env b/config/env index fd9169d..001b63e 100644 --- a/config/env +++ b/config/env @@ -1,2 +1,3 @@ PATH=/system/core:/system/utils:/system/scripts MANUALS=/system/manual/ +HOME=/ diff --git a/config/init.hooks/devfs b/config/init.hooks/devfs new file mode 100755 index 0000000..77ec1cf --- /dev/null +++ b/config/init.hooks/devfs @@ -0,0 +1,3 @@ +#!/system/core/smash + +mount devtmpfs none /system/devices diff --git a/config/init.hooks/procfs b/config/init.hooks/procfs new file mode 100755 index 0000000..2376461 --- /dev/null +++ b/config/init.hooks/procfs @@ -0,0 +1,3 @@ +#!/system/core/smash + +mount proc none /system/process diff --git a/config/init.hooks/shell b/config/init.hooks/shell new file mode 100755 index 0000000..be975f4 --- /dev/null +++ b/config/init.hooks/shell @@ -0,0 +1,6 @@ +#!/system/core/smash +cd $[HOME] + +clear +read /system/config/banner | mdv +smash diff --git a/config/init.hooks/start b/config/init.hooks/start new file mode 100755 index 0000000..49ed1af --- /dev/null +++ b/config/init.hooks/start @@ -0,0 +1,8 @@ +#!/system/core/smash + +cd /system/config/init.hooks/ + +./devfs +./sysfs +./procfs +./shell diff --git a/config/init.hooks/stop b/config/init.hooks/stop new file mode 100755 index 0000000..159fdcb --- /dev/null +++ b/config/init.hooks/stop @@ -0,0 +1,2 @@ +#!/system/core/smash +poweroff diff --git a/config/init.hooks/sysfs b/config/init.hooks/sysfs new file mode 100755 index 0000000..f54d57c --- /dev/null +++ b/config/init.hooks/sysfs @@ -0,0 +1,3 @@ +#!/system/core/smash + +mount sysfs none /system/info diff --git a/core/for.c b/core/for.c new file mode 100644 index 0000000..d576bf8 --- /dev/null +++ b/core/for.c @@ -0,0 +1,45 @@ +#include "../lib/exec/exec.h" +#include "../lib/sys/fork.h" +#include "../lib/sys/wait4.h" +#include "../lib/io/io.h" +#include "../lib/sys/types.h" +#include "../lib/cstr/cstr.h" + +char line[BUFSIZ]; + + +void replace_args(char **argv, char *rep) +{ + for (; *argv; ++argv) { + if (cstr_compare(*argv, "%") == 0) { + *argv = rep; + } + } +} + + +int main(int argc, char **argv) +{ + if (argc <= 1) { + wff(STDERR_FD, "for command [args...] % [args...]") + return -1; + } + + i64 size; + int pid; + + while ((size = get_next_line_from_fd(STDIN_FD, line)) >= 0) { + + pid = fork(); + if (pid == 0) { + replace_args(argv + 1, line); + exec((const char**)(argv + 1)); + wff(STDERR_FD, "command not found: %s", argv[1]); + return -1; + } + + wait4(pid, 0, 0); + } + + return 0; +} diff --git a/core/init b/core/init deleted file mode 100755 index 5802b36..0000000 --- a/core/init +++ /dev/null @@ -1,5 +0,0 @@ -#!/system/core/smash -/system/core/clear -/system/core/read /system/config/banner | /system/core/mdv -/system/core/env -f /system/config/env /system/core/smash -/system/core/powerctl poweroff diff --git a/core/mount.c b/core/mount.c index a7a4527..3bbf52b 100644 --- a/core/mount.c +++ b/core/mount.c @@ -12,7 +12,7 @@ int main(int argc, char **argv) int err; if ((err = mount(argv[2], argv[3], argv[1], 0, 0))) { - wff(STDERR_FD, "error: %s", errstr[-err]); + wff(STDERR_FD, "error: %s\n", errstr[-err]); return err; } diff --git a/core/unmount.c b/core/unmount.c new file mode 100644 index 0000000..81103d9 --- /dev/null +++ b/core/unmount.c @@ -0,0 +1,12 @@ +#include "../lib/io/io.h" +#include "../lib/sys/unmount.h" + +int main(int argc, char **argv) +{ + if (argc != 2) { + wff(STDERR_FD, "unmount \n"); + return -1; + } + + return unmount(argv[1], 9); +} diff --git a/lib/exec/exec.c b/lib/exec/exec.c new file mode 100644 index 0000000..42fd2c1 --- /dev/null +++ b/lib/exec/exec.c @@ -0,0 +1,38 @@ +#include "exec.h" +#include "../sys/execve.h" +#include "../env/env.h" +#include "../cstr/cstr.h" +#include "../malloc/malloc.h" + +int exec(const char **argv) +{ + const char *path = getenv("PATH"); + const char *end; + char *filename; + + while (*path) { + end = path; + + while (*end && *end != ':') ++end; + + filename = malloc((end - path) + cstr_length(argv[0]) + 2); + + for (int i = 0; i < end - path; ++i) + filename[i] = path[i]; + + filename[end - path] = '/'; + + for (int i = 0; i < cstr_length(argv[0]) + 1; ++i) + filename[(end - path) + i + 1] = argv[0][i]; + + execve(filename, argv, getenvp()); + free(filename); + + path = end; + + if (*path == ':') + ++path; + } + + return execve(argv[0], argv, getenvp()); +} diff --git a/lib/exec/exec.h b/lib/exec/exec.h new file mode 100644 index 0000000..fe7e811 --- /dev/null +++ b/lib/exec/exec.h @@ -0,0 +1,6 @@ +#ifndef EXEC_H +#define EXEC_H + +int exec(const char **argv); + +#endif diff --git a/lib/sys/unmount.h b/lib/sys/unmount.h new file mode 100644 index 0000000..1331e65 --- /dev/null +++ b/lib/sys/unmount.h @@ -0,0 +1,11 @@ +#ifndef UNMOUNT_H +#define UNMOUNT_H + +#include "syscalls.h" + +int unmount(char *pathname, int flags) +{ + return syscall(UMOUNT2, pathname, flags); +} + +#endif diff --git a/smash/exec.c b/smash/exec.c index 779fccd..8c20126 100644 --- a/smash/exec.c +++ b/smash/exec.c @@ -3,7 +3,6 @@ #include "builtin.h" #include "variable.h" -#include "../lib/sys/execve.h" #include "../lib/sys/fork.h" #include "../lib/sys/wait4.h" #include "../lib/sys/pipe.h" @@ -15,6 +14,7 @@ #include "../lib/env/env.h" #include "../lib/malloc/malloc.h" #include "../lib/list/list.h" +#include "../lib/exec/exec.h" void print_call(char **argv) { @@ -25,40 +25,7 @@ void print_call(char **argv) } } -void __execenv(const char **argv) -{ - const char *path = getenv("PATH"); - const char *end; - char *filename; - - while (*path) { - end = path; - - while (*end && *end != ':') ++end; - - filename = malloc((end - path) + cstr_length(argv[0]) + 2); - - for (int i = 0; i < end - path; ++i) - filename[i] = path[i]; - - filename[end - path] = '/'; - - for (int i = 0; i < cstr_length(argv[0]) + 1; ++i) - filename[(end - path) + i + 1] = argv[0][i]; - - execve(filename, argv, getenvp()); - free(filename); - - path = end; - - if (*path == ':') - ++path; - } - - execve(argv[0], argv, getenvp()); -} - -void exec(char *line) +void execute_line(char *line) { expression_list_t *exps = new_expression_from_line(line); expression_list_t *exp = exps; @@ -96,7 +63,7 @@ void exec(char *line) close(pipefd[1][PIPE_OUT]); close(pipefd[1][PIPE_IN]); - __execenv((const char **)argv); + exec((const char **)argv); wff(STDERR_FD, "command not found: %s\n", argv[0]); exit(-1); } diff --git a/smash/exec.h b/smash/exec.h index 74780b0..83f826f 100644 --- a/smash/exec.h +++ b/smash/exec.h @@ -1,6 +1,6 @@ #ifndef EXEC_H #define EXEC_H -void exec(char *line); +void execute_line(char *line); #endif diff --git a/smash/main.c b/smash/main.c index cf5f47f..efc7bd1 100644 --- a/smash/main.c +++ b/smash/main.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) line_length = cstr_length(line_exec); if (line_length > 0) { - exec(line_exec); + execute_line(line_exec); } cleanup_variable_line_context(); diff --git a/utils/init b/utils/init new file mode 100755 index 0000000..04dfeed --- /dev/null +++ b/utils/init @@ -0,0 +1,4 @@ +#!/system/core/smash + +/system/core/env -f /system/config/env /system/config/init.hooks/start +/system/core/env -f /system/config/env /system/config/init.hooks/stop -- cgit v1.2.3-70-g09d2