simple-init

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

commit 721228ee36b20c292d98c518ac66de5b4322348b
parent 5dd4631fdf651690ac9e1d612bf256d4bbffa75a
Author: depsterr <depsterr@protonmail.com>
Date:   Sun, 31 May 2020 15:42:40 +0200

updated stuff

Diffstat:
Minitscripts/init.c | 37++++++++++++++++++++++++++-----------
Minitscripts/util.c | 25+++++++------------------
Minitscripts/util.h | 9+++++++--
3 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/initscripts/init.c b/initscripts/init.c @@ -1,26 +1,35 @@ +#include <sys/types.h> +#include <sys/stat.h> #include <unistd.h> #include "util.h" int main() { + log_message("Welcome to slinux"); log_message("Mounting pseudo filesystems"); mount("nosuid,noexec,nodev", "proc", "proc", "/proc" ); + WAIT_FOR_CHILDREN; mount("nosuid,noexec,nodev", "sysfs", "sys", "/sys" ); + WAIT_FOR_CHILDREN; mount("mode=0755,nosuid,nodev", "tmpfs", "run", "/run" ); + WAIT_FOR_CHILDREN; mount("mode=0755,nosuid", "devtmpfs", "dev", "/dev" ); + WAIT_FOR_CHILDREN; - make_path("/dev/runit" ); - make_path("/run/user" ); - make_path("/run/lock" ); - make_path("/run/log" ); - make_path("/dev/pts" ); - make_path("/dev/shm" ); + mkdir("/dev/runit", 0755); + mkdir("/run/user" , 0755); + mkdir("/run/lock" , 0755); + mkdir("/run/log" , 0755); + mkdir("/dev/pts" , 0755); + mkdir("/dev/shm" , 0755); mount("mode=0620,gid=5,nosuid,noexec", "devpts", "devpts", "/dev/pts" ); mount("mode=1777,nosuid,nodev", "tmpfs", "shm", "/dev/shm" ); + WAIT_FOR_CHILDREN; + /* dest, link */ symlink("/proc/self/fd", "/dev/fd" ); @@ -36,16 +45,22 @@ int main() { 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); + WAIT_FOR_CHILDREN; spawn_process(udevcmd3); + WAIT_FOR_CHILDREN; spawn_process(udevcmd4); log_message("Mounting filesystems"); - char* mountcmd[] = { "mount", "-a", 0}; - spawn_process(mountcmd); + char* mountcmd1[] = { "mount", "-o", "remount,ro", "/", 0}; + char* mountcmd2[] = { mountcmd1[0], "-a", 0}; + spawn_process(mountcmd1); + WAIT_FOR_CHILDREN; + spawn_process(mountcmd2); log_message("Enabling swap"); - char* swapcmd[] = { "spawon", mountcmd[1], 0}; + char* swapcmd[] = { "swapon", mountcmd2[1], 0}; spawn_process(swapcmd); log_message("Seeding random"); @@ -58,8 +73,8 @@ int main() { log_message("Killing device managers"); log_message("Boot completed, starting ttys"); - char* tty1[] = { "/bin/getty/", "38400", "tty1", 0 }; - char* tty2[] = { "/bin/getty/", "38400", "tty2", 0 }; + char* tty1[] = { "/bin/getty", "38400", "tty1", 0 }; + char* tty2[] = { "/bin/getty", "38400", "tty2", 0 }; spawn_process(tty1); spawn_process(tty2); } diff --git a/initscripts/util.c b/initscripts/util.c @@ -8,28 +8,18 @@ #include <mntent.h> #include <stdio.h> -void spawn_process(char** argv) { - if (fork() == 0) { +int spawn_process(char** argv) { + int child_pid; + if ((child_pid = fork()) == 0) { setsid(); execvp(argv[0], argv); - perror("execvp"); + fprintf(stderr, "execvp: %s\n", argv[0]); exit(1); } + return child_pid; } -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; -} - -int make_path(char* path) { +void make_path(char* path) { char ret = 0; char* dirpath = 0; for (int n = 1; path[n] != '\0' && ret == 0; n++) { @@ -37,10 +27,9 @@ int make_path(char* path) { dirpath = realloc(dirpath, n + 1); strncpy(dirpath, path, n); dirpath[n + 1] = '\0'; - ret = make_dir(dirpath, 0755); + mkdir(dirpath, 0755); } } - return ret; } void log_message(char* msg) { diff --git a/initscripts/util.h b/initscripts/util.h @@ -1,6 +1,11 @@ -void spawn_process(char** argv); +#include <sys/wait.h> -int make_path(char* path); +int status = 0; +#define WAIT_FOR_CHILDREN while(wait(&status) > 0) + +int spawn_process(char** argv); + +void make_path(char* path); void log_message (char* msg);