From 86ba12520832a17557814303516e27d26e753d89 Mon Sep 17 00:00:00 2001 From: Bradford Morgan White Date: Sun, 8 Feb 2026 12:01:20 -0500 Subject: [PATCH] modified version of felker's init --- Makefile | 19 +++++++++++++++++++ init.c | 24 ++++++++++++++++++++++++ rc | 30 ++++++++++++++++++++++++++++++ reboot | 8 ++++++++ respawn.c | 26 ++++++++++++++++++++++++++ shutdown | 8 ++++++++ 6 files changed, 115 insertions(+) create mode 100644 Makefile create mode 100644 init.c create mode 100644 rc create mode 100644 reboot create mode 100644 respawn.c create mode 100644 shutdown diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d9edcfb --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=%.o) + +CFLAGS = -O2 -static +LDFLAGS = -s + +all: init respawn + +clean: + rm -f init respawn $(OBJS) + +$(OBJS): + +%: %.o + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< diff --git a/init.c b/init.c new file mode 100644 index 0000000..1681871 --- /dev/null +++ b/init.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int main() +{ + if (getpid() != 1) return 1; + + sigset_t set, old; + sigfillset(&set); + sigprocmask(SIG_BLOCK, &set, &old); + + posix_spawnattr_t attr; + posix_spawnattr_init(&attr); + posix_spawnattr_setsigmask(&attr, &old); + posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID|POSIX_SPAWN_SETSIGMASK); + + posix_spawn(0, "/etc/rc", 0, &attr, + ((char *[]){ "rc", 0 }), + ((char *[]){ 0 })); + + for (;;) wait(0); +} diff --git a/rc b/rc new file mode 100644 index 0000000..9c35a66 --- /dev/null +++ b/rc @@ -0,0 +1,30 @@ +#!/bin/sh +# /etc/rc — this file will be called directly by /sbin/init +# it is safest to use absolute paths here because +# /etc/profile is not yet loaded +/bin/dmesg -n 1 +/bin/echo "Mounting required filesystems" +/bin/mount -t sysfs sysfs /sys +/bin/mount -t proc proc /proc +/bin/echo "Mounting fstab filesystems" +/bin/mount -a +/bin/echo "Remounting root as read/write" +/bin/mount / -o remount,rw +/bin/echo "Mounting /dev/shm" +[ -d /dev/shm ] || /bin/mkdir /dev/shm +/bin/mount -t tmpfs tmpfs /dev/shm +/bin/echo "Mounting /dev/pts" +[ -d /dev/pts ] || /bin/mkdir /dev/pts +/bin/mount -t devpts devpts /dev/pts +/bin/echo "Mounting /tmp" +/bin/mount -t tmpfs tmpfs /tmp +/bin/echo "Starting mdev" +/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug +/sbin/mdev -s +/bin/echo "Executing init scripts" +for FILE in /etc/rc.d/*; do + [ -x /etc/rc.d/$FILE ] && /etc/rc.d/$FILE start +done +echo "System booted with $(/bin/cat /proc/cmdline)" +/bin/echo "Spawning TTY" +/sbin/respawn /bin/busybox getty 38400 /dev/tty0 diff --git a/reboot b/reboot new file mode 100644 index 0000000..c2820a7 --- /dev/null +++ b/reboot @@ -0,0 +1,8 @@ +#!/bin/sh +# /sbin/reboot +for FILE in /etc/rc.d/*; do + [ -x /etc/rc.d/$FILE ] && /etc/rc.d/$FILE stop +done +sync +mount / -o remount,ro +echo b >/proc/sysrq-trigger diff --git a/respawn.c b/respawn.c new file mode 100644 index 0000000..77e0cc5 --- /dev/null +++ b/respawn.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + sigset_t set, old; + sigfillset(&set); + sigprocmask(SIG_BLOCK, &set, &old); + + posix_spawnattr_t attr; + posix_spawnattr_init(&attr); + posix_spawnattr_setsigmask(&attr, &old); + posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID|POSIX_SPAWN_SETSIGMASK); + + for (;;) { + extern char **environ; + pid_t pid; + if (posix_spawn(&pid, argv[1], 0, &attr, argv+1, environ)) + nanosleep(&(struct timespec){.tv_nsec=100000000}, 0); + waitpid(pid, 0, 0); + } +} diff --git a/shutdown b/shutdown new file mode 100644 index 0000000..d187ab3 --- /dev/null +++ b/shutdown @@ -0,0 +1,8 @@ +#!/bin/sh +# /sbin/shutdown +for FILE in /etc/rc.d/*; do + [ -x /etc/rc.d/$FILE ] && /etc/rc.d/$FILE stop +done +sync +mount / -o remount,ro +echo o >/proc/sysrq-trigger