simple-init

A set of init binaries for linux
git clone git://depsterr.com/git/simple-init
Log | Files | Refs | README | LICENSE

commit 8fee2fbce661e614d9a9b5151beea2242dac235c
parent 67a14bc30fd6746d589f6e619cc11006bab6b57f
Author: depsterr <depsterr@protonmail.com>
Date:   Sun, 31 May 2020 14:12:36 +0200

created boot.c

Diffstat:
Ainitscripts/boot.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minitscripts/config.h | 5+++--
Minitscripts/util.c | 38+++++++++++++++++++++++++++++++-------
Minitscripts/util.h | 14+++++++++++++-
4 files changed, 105 insertions(+), 10 deletions(-)

diff --git a/initscripts/boot.c b/initscripts/boot.c @@ -0,0 +1,58 @@ +#include <unistd.h> + +#include "config.h" +#include "util.h" + +int main() { + log("Welcome to slinux"); + + log("Mounting pseudo filesystems"); + + mount("nosuid,noexec,nodev", "proc", "proc", "/proc" ); + mount("nosuid,noexec,nodev", "sysfs", "sys", "/sys" ); + mount("mode=0755,nosuid,nodev", "tmpfs", "run", "/run" ); + mount("mode=0755,nosuid", "devtmpfs", "dev", "/dev" ); + + make_path("/dev/runit" ); + make_path("/run/user" ); + make_path("/run/lock" ); + make_path("/run/log" ); + make_path("/dev/pts" ); + make_path("/dev/shm" ); + + mount("mode=0620,gid=5,nosuid,noexec", "devpts", "devpts", "/dev/pts" ); + mount("mode=1777,nosuid,nodev", "tmpfs", "shm", "/dev/shm" ); + + /* dest, link */ + symlink("/proc/self/fd", "/dev/fd" ); + symlink("fd/0", "/dev/stdin" ); + symlink("fd/1", "/dev/stdout" ); + symlink("fd/2", "/dev/stderr" ); + + log("Starting device manager"); + + log("Starting udev"); + spawn_process({ "udevd", "-d", 0}); + spawn_process({ "udevadm", "trigger", "-c", "add", "-t", "subsystems", 0}); + spawn_process({ "udevadm", "trigger", "-c", "add", "-t", "devices", 0}); + spawn_process({ "udevadm", "settle", 0}); + + log("Mounting filesystems"); + spawn_process({ "mount", "-a", 0}); + + log("Enabling swap"); + spawn_process({ "swapon", "-a", 0}); + + log("Seeding random"); + load_random_seed(); + + log("Setting up loopback"); + spawn_process({ "ip", "link", "set", "up", "dev", "lo", 0}); + + log("Killing device managers"); + + log("Boot completed, starting post boot services"); + for(int n = 0; post_boot_services[n]; n++) { + spawn_process(post_boot_services[n]); + } +} diff --git a/initscripts/config.h b/initscripts/config.h @@ -1,5 +1,6 @@ /* Services to run post boot */ static char *const post_boot_services[] = { - { "/bin/getty/", "38400", "tty1", 0 } - { "/bin/getty/", "38400", "tty2", 0 } + { "/bin/getty/", "38400", "tty1", 0 }, + { "/bin/getty/", "38400", "tty2", 0 }, + 0 }; diff --git a/initscripts/util.c b/initscripts/util.c @@ -15,12 +15,38 @@ void spawn_process(char* argv[]) { } } +int make_path(char* path) { + char ret = 0; + char* dirpath = 0; + for (int n = 1, path[n] != '\0' && ret == 0; n++) { + if (path[n] == '/') { + dirpath = realloc(dirpath, n + 1); + strncpy(dirpath, path, n); + dirpath[n + 1] = '\0'; + ret = make_dir(dirpath, 0755); + } + } + return ret; +} + +static int make_dir(char* path, mode_t mode) { + struct stat st; + if (stat(path, &st) != 0) { + if (mkdir(path,mode) != 0) + return -1; + } else if (!S_ISDIR(st.st_mode)) { + return -1; + } + + return 0; +} + void log(char* msg) { printf("\033[31;1m=>\033[m %s\n", msg); printk("init: %s\n", msg); } -unsigned char is_mounted(char* path) { +int is_mounted(char* path) { if (access(path, F_OK) == -1) return 0; @@ -55,9 +81,7 @@ void mount(char* options, char* type, char* device, char* mountpoint) { } void save_random_seed() { - if (access("/var/init", F_OK) == -1) { - mkdir("/var/init", 0755); - } + make_path("/var/lib/init"); int fdesc = open("/dev/urandom", O_RDONLY); char random_buffer[512]; @@ -68,7 +92,7 @@ void save_random_seed() { return; - fdesc = open("/var/init/random-seed", O_CREAT|O_WRONLY); + fdesc = open("/var/lib/init/random-seed", O_CREAT|O_WRONLY); if (fdesc < 0) return; @@ -77,11 +101,11 @@ void save_random_seed() { } void load_random_seed() { - if (access("/var/init/random-seed", F_OK) == -1) { + if (access("/var/lib/init/random-seed", F_OK) == -1) { return; } - int fdesc = open("/var/init/random-seed", O_RDONLY); + int fdesc = open("/var/lib/init/random-seed", O_RDONLY); char random_buffer[512]; if (fdesc < 0) diff --git a/initscripts/util.h b/initscripts/util.h @@ -1 +1,13 @@ -void spawn_process(char *const argv[]); +void spawn_process(char* argv[]); + +int make_path(char* path); + +void log(char* msg); + +int char is_mounted(char* path); + +void mount(char* options, char* type, char* device, char* mountpoint); + +void save_random_seed(); + +void load_random_seed();