diff options
| author | Hunter Kvalevog <hunter@kvog.sh> | 2026-02-06 21:28:34 -0600 |
|---|---|---|
| committer | Hunter Kvalevog <hunter@kvog.sh> | 2026-02-06 21:28:34 -0600 |
| commit | c2075e339ce7a46f96a5c63c409a2cc8e4b6b3cf (patch) | |
| tree | 73e9dda90fd9c90f955398085d91a031f2943289 | |
| parent | c41d3de8eeb16773cd326f8ea6cfbf575ce08b75 (diff) | |
rl: Beef up init
| -rwxr-xr-x | rushmore-linux/rl.sh | 1 | ||||
| -rw-r--r-- | rushmore-linux/rlinit/rlinit.c | 76 |
2 files changed, 64 insertions, 13 deletions
diff --git a/rushmore-linux/rl.sh b/rushmore-linux/rl.sh index 2f7ecae..39614bc 100755 --- a/rushmore-linux/rl.sh +++ b/rushmore-linux/rl.sh @@ -23,5 +23,4 @@ echo "Building initramfs" (cd "$ROOTFS_DIR" && find . -print0 | cpio --null -ov --format=newc) > "$BUILD_DIR/initramfs.cpio" echo "Running QEMU (do CTRL+A then X to quit)" -sleep 1 qemu-system-x86_64 -kernel linux/arch/x86/boot/bzImage -initrd "$BUILD_DIR/initramfs.cpio" -append "console=ttyS0" -nographic
\ No newline at end of file diff --git a/rushmore-linux/rlinit/rlinit.c b/rushmore-linux/rlinit/rlinit.c index 21cf2af..4ab06db 100644 --- a/rushmore-linux/rlinit/rlinit.c +++ b/rushmore-linux/rlinit/rlinit.c @@ -1,24 +1,76 @@ +#include <errno.h> +#include <fcntl.h> #include <unistd.h> #include <sys/mount.h> +#include <sys/uio.h> #include <sys/wait.h> +static void write_str(int fd, const char* str) +{ + write(fd, str, __builtin_strlen(str)); +} + +static void error(const char* message) +{ + // Attempt to spew to kernel log + int kmfd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); + if (kmfd >= 0) { + write_str(kmfd, message); + close(kmfd); + } + + _exit(1); // Kernel panic +} +#define ERROR(message) error("[rlinit] " message) + +static bool mount_required(const char* src, const char* tgt, const char* fs) +{ + // EBUSY means already mounted, which is ok + return mount(src, tgt, fs, 0, 0) >= 0 || errno == EBUSY; +} + int main(void) { - mount("proc", "/proc", "proc", 0, 0); - mount("sysfs", "/sys", "sysfs", 0, 0); - mount("devtmpfs", "/dev", "devtmpfs", 0, 0); +#if 0 + // First, spew a message to tty to signal we at least made it this far + { + const char* hello = "rlinit: Initialization started\n"; + write(1, hello, __builtin_strlen(hello)); + } +#endif - write(1, "rlinit: starting rlwsd\n", 23); + // Mount /dev first, as /dev/kmsg is useful for debugging errors + // @@ include errno in error messages + if (!mount_required("devtmpfs", "/dev", "devtmpfs")) { + ERROR("Failed to mount /dev\n"); + } + if (!mount_required("proc", "/proc", "proc")) { + ERROR("Failed to mount /proc\n"); + } + if (!mount_required("sysfs", "/sys", "sysfs")) { + ERROR("Failed to mount /sys\n"); + } - pid_t pid = fork(); - if (pid == 0) { - char *argv[] = { (char*)"/rlwsd", 0 }; - execv(argv[0], argv); - _exit(127); + // Write to kernel log + { + int kmfd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); + if (kmfd >= 0) { + const char* hello = "<6>Rushmore Linux - Nothing is beyond our reach.\n"; + write(kmfd, hello, __builtin_strlen(hello)); + close(kmfd); + } } - int st = 0; - waitpid(pid, &st, 0); - write(1, "rlinit: rlwsd exited\n", 21); + // pid_t pid = fork(); + // if (pid == 0) { + // char *argv[] = { (char*)"/rlwsd", 0 }; + // execv(argv[0], argv); + // _exit(127); + // } + + // int st = 0; + // waitpid(pid, &st, 0); + // write(1, "rlinit: rlwsd exited\n", 21); + for (;;) pause(); } |