From ad0302ab8213e35070e50beba03d8da5c57f4fc4 Mon Sep 17 00:00:00 2001 From: uxtbd Date: Tue, 18 Aug 2026 10:41:53 -0300 Subject: 4suite: move to shinobi --- .clang-format | 5 +++++ .gitignore | 1 + 4init/4init.c | 9 ++++----- 4init/Makefile | 21 -------------------- 4init/build.ninja | 41 +++++++++++++++++++++++++++++++++++++++ 4init/config.h | 11 +++-------- 4rc/4rc | Bin 0 -> 17872 bytes 4rc/4rc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4rc/README.md | 5 +++++ 4rc/build.ninja | 41 +++++++++++++++++++++++++++++++++++++++ 4rc/config.h | 4 ++++ 4rc/config.mk | 5 +++++ format.sh | 2 ++ 13 files changed, 167 insertions(+), 34 deletions(-) create mode 100644 .clang-format delete mode 100644 4init/Makefile create mode 100644 4init/build.ninja create mode 100755 4rc/4rc create mode 100644 4rc/4rc.c create mode 100644 4rc/README.md create mode 100644 4rc/build.ninja create mode 100644 4rc/config.h create mode 100644 4rc/config.mk create mode 100755 format.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..4097bc5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: LLVM +Language: Cpp +IndentWidth: 4 +TabWidth: 4 +UseTab: Always diff --git a/.gitignore b/.gitignore index 5761abc..d39616b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.o +*.ninja_log diff --git a/4init/4init.c b/4init/4init.c index 50807b6..14cfbba 100644 --- a/4init/4init.c +++ b/4init/4init.c @@ -8,14 +8,13 @@ #include "config.h" -static void reap(void) -{ +static void reap(void) { int status; - while (waitpid(-1, &status, WNOHANG) > 0) {} + while (waitpid(-1, &status, WNOHANG) > 0) { + } } -int main(void) -{ +int main(void) { int sigfd = -1; pid_t pid = -1; sigset_t set; diff --git a/4init/Makefile b/4init/Makefile deleted file mode 100644 index e56e105..0000000 --- a/4init/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -include config.mk - -BIN = 4init -OBJ = 4init.o - -all: $(BIN) - -$(BIN): $(OBJ) - $(CC) $(LDFLAGS) -o $@ $(OBJ) - -$(OBJ): config.h - -install: all - mkdir -pv $(DESTDIR)$(PREFIX)/bin - cp -fv $(BIN) $(DESTDIR)$(PREFIX)/bin - -uninstall: all - rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN) - -clean: all - rm -f 4init.o 4init diff --git a/4init/build.ninja b/4init/build.ninja new file mode 100644 index 0000000..41f1d7d --- /dev/null +++ b/4init/build.ninja @@ -0,0 +1,41 @@ +# this file was generated from a makefile by shinobi +build __shin_always_build__: phony + +build all: phony 4init + +rule r1 + command = musl-gcc -s -static -o 4init 4init.o + generator = 1 + +build 4init: r1 4init.o + description = build 4init + +rule r2 + command = musl-gcc -std=c99 -Wextra -Wall -Os -fhardened -c 4init.c + generator = 1 + +build 4init.o: r2 4init.c config.h + description = build 4init.o + +rule r3 + command = sh -c 'mkdir -pv /usr/local/bin' && sh -c 'cp -fv 4init /usr/local/bin' + generator = 1 + +build install: r3 + description = build install + +rule r4 + command = rm -f /usr/local/bin/4init + generator = 1 + +build uninstall: r4 + description = build uninstall + +rule r5 + command = rm -f 4init.o 4init + generator = 1 + +build clean: r5 + description = build clean + +default all diff --git a/4init/config.h b/4init/config.h index 6c36ccf..3798447 100644 --- a/4init/config.h +++ b/4init/config.h @@ -3,15 +3,10 @@ static const char *rc_path = "/usr/bin/4rc"; -static char *const rc_argv[] = { - "4rc", - NULL -}; +static char *const rc_argv[] = {"4rc", NULL}; static char *const rc_envp[] = { - "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/sbin", - "TERM=linux", - NULL -}; + "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/sbin", "TERM=linux", + NULL}; #endif // CONFIG_H diff --git a/4rc/4rc b/4rc/4rc new file mode 100755 index 0000000..1df6d60 Binary files /dev/null and b/4rc/4rc differ diff --git a/4rc/4rc.c b/4rc/4rc.c new file mode 100644 index 0000000..68a5d9f --- /dev/null +++ b/4rc/4rc.c @@ -0,0 +1,56 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include + +void create_vfs(void) { + mkdir("/proc", 0755); + mkdir("/sys", 0755); + mkdir("/dev", 0755); + mkdir("/run", 0755); +} + +void mount_vfs(void) { + if (mount("proc", "/proc", "proc", + MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0) { + perror("mount /proc"); + } + + if (mount("sys", "/sys", "sysfs", + MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0) { + perror("mount /sys"); + } + + if (mount("dev", "/dev", "devtmpfs", MS_NOSUID | MS_RELATIME, "mode=755") != + 0) { + perror("mount /dev"); + } + + if (mount("run", "/run", "tmpfs", MS_NOSUID | MS_NODEV | MS_RELATIME, + "mode=755") != 0) { + perror("mount /run"); + } + + mkdir("/dev/pts", 0755); + mkdir("/dev/shm", 1777); + + if (mount("devpts", "/dev/pts", "devpts", + MS_NOSUID | MS_NOEXEC | MS_RELATIME, "gid=5,mode=620") != 0) { + perror("mount /dev/pts"); + } + + if (mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_RELATIME, + "mode=1777") != 0) { + perror("mount /dev/shm"); + } +} + +int main(void) { + create_vfs(); + mount_vfs(); + + return 0; +} diff --git a/4rc/README.md b/4rc/README.md new file mode 100644 index 0000000..44fbde5 --- /dev/null +++ b/4rc/README.md @@ -0,0 +1,5 @@ +# 4init: minimal but functional init + +It's only purpose is to manage processes, it by defaults calls 4rc, but you can change that in `config.h`, it's started as PID 1, and if it crashes, the kernel will panic, read the article in Wikipedia if you want to know more. + +Inspired by [rofl0r](https://gist.github.com/rofl0r/6168719)'s minimal init. \ No newline at end of file diff --git a/4rc/build.ninja b/4rc/build.ninja new file mode 100644 index 0000000..cce3f53 --- /dev/null +++ b/4rc/build.ninja @@ -0,0 +1,41 @@ +# this file was generated from a makefile by shinobi +build __shin_always_build__: phony + +build all: phony 4rc + +rule r1 + command = musl-gcc -s -static -o 4rc 4rc.o + generator = 1 + +build 4rc: r1 4rc.o + description = build 4rc + +rule r2 + command = musl-gcc -std=c99 -Wextra -Wall -Os -fhardened -c -o 4rc.o 4rc.c + generator = 1 + +build 4rc.o: r2 4rc.c config.h + description = build 4rc.o + +rule r3 + command = sh -c 'mkdir -pv /usr/local/bin' && sh -c 'cp -fv 4rc /usr/local/bin' + generator = 1 + +build install: r3 + description = build install + +rule r4 + command = rm -f /usr/local/bin/4rc + generator = 1 + +build uninstall: r4 + description = build uninstall + +rule r5 + command = rm -f 4rc.o 4rc + generator = 1 + +build clean: r5 + description = build clean + +default all diff --git a/4rc/config.h b/4rc/config.h new file mode 100644 index 0000000..6b3aeb1 --- /dev/null +++ b/4rc/config.h @@ -0,0 +1,4 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#endif // CONFIG_H diff --git a/4rc/config.mk b/4rc/config.mk new file mode 100644 index 0000000..ab7e752 --- /dev/null +++ b/4rc/config.mk @@ -0,0 +1,5 @@ +PREFIX = /usr/local + +CC = musl-gcc +CFLAGS = -std=c99 -Wextra -Wall -Os -fhardened +LDFLAGS = -s -static diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..44c1f0c --- /dev/null +++ b/format.sh @@ -0,0 +1,2 @@ +#/bin/sh +clang-format **/*.c **/*.h -i -- cgit v1.3.1