simple-init

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

commit 2634009f02f7a1cd3d4e47191f03fa9e576c71cd
parent c50ce4274481ac1a203c19de86235256126d8ecb
Author: depsterr <depsterr@protonmail.com>
Date:   Thu,  2 Jul 2020 18:06:56 +0200

updated boot.c to use mount syscall instead of command (except for mount
-a)

Diffstat:
M.gitignore | 2+-
Dboot.c | 83-------------------------------------------------------------------------------
Mconfig.mk | 2+-
Minit.c | 72+++++++++++++++++++++++++++++++++++++++++-------------------------------
Mutil.c | 35-----------------------------------
Mutil.h | 8--------
6 files changed, 43 insertions(+), 159 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,2 @@ -rc.init +rc.boot rc.shutdown diff --git a/boot.c b/boot.c @@ -1,83 +0,0 @@ -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> - -#include "util.h" - -int main() { - - log_message("Welcome to linux"); - - log_message("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" ); - wait_for_children(); - mount("mode=0755,nosuid", "devtmpfs", "dev", "/dev" ); - - mkdir("/run/user" , 0755); - mkdir("/run/lock" , 0755); - mkdir("/run/log" , 0755); - mkdir("/dev/pts" , 0755); - mkdir("/dev/shm" , 0755); - - wait_for_children(); - 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" ); - - /* Call modprobe stuff here */ - - log_message("Starting device manager"); - - log_message("Starting udev"); - char* udevcmd1[] = { "udevd", "-d", 0}; - char* udevcmd2[] = { "udevadm", "trigger", "-c", "add", "-t", "subsystems", 0}; - /* avoid storing duplicate strings twice. */ - char* udevcmd3[] = { udevcmd2[0], udevcmd2[1], udevcmd2[2], udevcmd2[3], udevcmd2[4], "devices", 0}; - char* udevcmd4[] = { udevcmd2[0], "settle", 0}; - spawn_process(udevcmd1); - wait_for_children(); - spawn_process(udevcmd2); - spawn_process(udevcmd3); - wait_for_children(); - spawn_process(udevcmd4); - - log_message("Mounting filesystems"); - char* mountcmd1[] = { "mount", "-o", "remount,rw", "/", 0}; - char* mountcmd2[] = { mountcmd1[0], "-a", 0}; - spawn_process(mountcmd1); - spawn_process(mountcmd2); - - log_message("Enabling swap"); - char* swapcmd[] = { "swapon", mountcmd2[1], 0}; - spawn_process(swapcmd); - - log_message("Seeding random"); - load_random_seed(); - - log_message("Setting up loopback"); - char* netloopcmd[] = { "ip", "link", "set", "up", "dev", "lo", 0}; - spawn_process(netloopcmd); - - log_message("Killing device managers"); - char* udevcmd5[] = { udevcmd2[0], "control", "--exit", 0}; - wait_for_children(); - spawn_process(udevcmd5); - - /* Startup services here */ - - wait_for_children(); - log_message("Boot completed, starting ttys"); - char* tty1[] = { "/bin/getty", "38400", "tty1", 0 }; - char* tty2[] = { "/bin/getty", "38400", "tty2", 0 }; - respawn_process(tty1); - respawn_process(tty2); -} diff --git a/config.mk b/config.mk @@ -1,6 +1,6 @@ # Change this if you want to install to a custom path. DESTDIR?=/usr/lib/init -CC=cc +CC=gcc INIT=rc.boot SHUTDOWN=rc.shutdown diff --git a/init.c b/init.c @@ -1,46 +1,50 @@ #include <sys/types.h> -#include <sys/syscall.h> +#include <sys/mount.h> +#include <sys/swap.h> #include <sys/stat.h> -#include <fcntl.h> #include <unistd.h> +#include <mntent.h> +#include <stdio.h> #include "util.h" int main() { + log_message("Welcome to linux"); + log_message("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" ); - wait_for_children(); - mount("mode=0755,nosuid", "devtmpfs", "dev", "/dev" ); + mount("proc", "/proc", "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV, 0); + mount("sys", "/sys", "sysfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, 0); + mount("run", "/run", "tmpfs", MS_NOSUID | MS_NODEV, "mode=0755"); + mount("dev", "/dev", "devtmpfs", MS_NOSUID, "mode=0755"); - mkdir("/run/user" , 0755); - mkdir("/run/lock" , 0755); - mkdir("/run/log" , 0755); - mkdir("/dev/pts" , 0755); - mkdir("/dev/shm" , 0755); - - wait_for_children(); - mount("mode=0620,gid=5,nosuid,noexec", "devpts", "devpts", "/dev/pts" ); - mount("mode=1777,nosuid,nodev", "tmpfs", "shm", "/dev/shm" ); + mkdir("/run/user", 0755); + mkdir("/run/lock", 0755); + mkdir("/run/log", 0755); + mkdir("/dev/pts", 0755); + mkdir("/dev/shm", 0755); + mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, "mode=0620,gid=5"); + mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NODEV, "mode=1777"); /* dest, link */ - symlink("/proc/self/fd", "/dev/fd" ); - symlink("fd/0", "/dev/stdin" ); - symlink("fd/1", "/dev/stdout" ); - symlink("fd/2", "/dev/stderr" ); + symlink("/proc/self/fd", "/dev/fd"); + symlink("fd/0", "/dev/stdin"); + symlink("fd/1", "/dev/stdout"); + symlink("fd/2", "/dev/stderr"); + + /* Call modprobe stuff here */ + /* char* modprobe[] = { "modprobe", "module", 0 }; */ + /* spawn(modprobe) */ log_message("Starting device manager"); log_message("Starting udev"); char* udevcmd1[] = { "udevd", "-d", 0}; char* udevcmd2[] = { "udevadm", "trigger", "-c", "add", "-t", "subsystems", 0}; - /* avoid storing duplicate strings twice. */ - char* udevcmd3[] = { udevcmd2[0], udevcmd2[1], udevcmd2[2], udevcmd2[3], udevcmd2[4], "devices", 0}; - char* udevcmd4[] = { udevcmd2[0], "settle", 0}; + char* udevcmd3[] = { "udevadm", "trigger", "-c", "add", "-t", "devices", 0}; + char* udevcmd4[] = { "udevadm", "settle", 0}; spawn_process(udevcmd1); wait_for_children(); spawn_process(udevcmd2); @@ -49,14 +53,14 @@ int main() { spawn_process(udevcmd4); log_message("Mounting filesystems"); - char* mountcmd1[] = { "mount", "-o", "remount,rw", "/", 0}; - char* mountcmd2[] = { mountcmd1[0], "-a", 0}; - spawn_process(mountcmd1); - spawn_process(mountcmd2); + mount("/", "/", "ext4", MS_REMOUNT, 0); + char* mountall[] = { "mount", "-a", 0}; + spawn_process(mountall); log_message("Enabling swap"); - char* swapcmd[] = { "swapon", mountcmd2[1], 0}; - spawn_process(swapcmd); + + char* swapon[] = { "swapon", "-a", 0}; + spawn_process(swapon); log_message("Seeding random"); load_random_seed(); @@ -70,9 +74,15 @@ int main() { wait_for_children(); spawn_process(udevcmd5); + /* Startup services here */ + + /* char* startup[] = { "dhcpcd", 0 }; */ + /* spawn(startup) */ + + wait_for_children(); log_message("Boot completed, starting ttys"); - char* tty1[] = { "/usr/bin/getty", "38400", "tty1", 0 }; - char* tty2[] = { "/usr/bin/getty", "38400", "tty2", 0 }; + char* tty1[] = { "/bin/getty", "38400", "tty1", 0 }; + char* tty2[] = { "/bin/getty", "38400", "tty2", 0 }; respawn_process(tty1); respawn_process(tty2); } diff --git a/util.c b/util.c @@ -65,41 +65,6 @@ void make_path(char* path) { } } -int is_mounted(char* path) { - if (access(path, F_OK) == -1) - return 0; - - if (access("/proc/mounts", F_OK) == -1) - return 0; - - struct mntent *ent; - FILE *aFile; - - aFile = setmntent("/proc/mounts", "r"); - - if (aFile == NULL) - return 0; - - while (NULL != (ent = getmntent(aFile))) { - if(!strcmp(path, ent->mnt_dir)) - return 1; - } - endmntent(aFile); - return 0; -} - -void mount(char* options, char* type, char* device, char* mountpoint) { - if (is_mounted(mountpoint)) { - char newopts[128] = "remount,"; - strcat(newopts,options); - options = newopts; - } - - char* mountcmd[] = { "/bin/mount", "-o", options, "-t", type, device, mountpoint, 0 }; - /* Could use the mount syscall, but I'm lazy, this allows for parellel mounting AND I'm lazy */ - spawn_process(mountcmd); -} - void save_random_seed() { make_path("/var/lib/init"); diff --git a/util.h b/util.h @@ -16,14 +16,6 @@ void make_path(char* path); /* Logs message to user and kmsg/dmesg */ void log_message (char* msg); -/* Checks if a mountpoint is mounted, is not perfect and will not - work for all cases (however the uses in the unmodified program - are of course safe. */ -int is_mounted(char* path); - -/* like mount command */ -void mount(char* options, char* type, char* device, char* mountpoint); - /* save a random seed */ void save_random_seed();