simple-init

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

commit 022b42a648b720691a8af892f0d7f5a16da1f152
parent 0c45bc524bc7627318b5f2a1ec7fae08f668fea8
Author: depsterr <depsterr@protonmail.com>
Date:   Sun, 31 May 2020 15:00:59 +0200

now compiles rc.init

Diffstat:
Ainitscripts/.gitignore | 2++
Minitscripts/config.h | 2+-
Minitscripts/init.c | 45++++++++++++++++++++++++++-------------------
Minitscripts/util.c | 55++++++++++++++++++++++++++++++-------------------------
Minitscripts/util.h | 6+++---
5 files changed, 62 insertions(+), 48 deletions(-)

diff --git a/initscripts/.gitignore b/initscripts/.gitignore @@ -0,0 +1,2 @@ +rc.init +rc.shutdown diff --git a/initscripts/config.h b/initscripts/config.h @@ -1,5 +1,5 @@ /* Services to run post boot */ -static char *const post_boot_services[] = { +char* ttys[][4] = { { "/bin/getty/", "38400", "tty1", 0 }, { "/bin/getty/", "38400", "tty2", 0 }, 0 diff --git a/initscripts/init.c b/initscripts/init.c @@ -4,9 +4,9 @@ #include "util.h" int main() { - log("Welcome to slinux"); + log_message("Welcome to slinux"); - log("Mounting pseudo filesystems"); + log_message("Mounting pseudo filesystems"); mount("nosuid,noexec,nodev", "proc", "proc", "/proc" ); mount("nosuid,noexec,nodev", "sysfs", "sys", "/sys" ); @@ -29,30 +29,37 @@ int main() { symlink("fd/1", "/dev/stdout" ); symlink("fd/2", "/dev/stderr" ); - log("Starting device manager"); + log_message("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_message("Starting udev"); + char* udevcmd1[] = { "udevd", "-d", 0}; + char* udevcmd2[] = { "udevadm", "trigger", "-c", "add", "-t", "subsystems", 0}; + char* udevcmd3[] = { udevcmd2[0], udevcmd2[1], udevcmd2[2], udevcmd2[3], udevcmd2[4], "devices", 0}; + char* udevcmd4[] = { udevcmd2[0], "settle", 0}; + spawn_process(udevcmd1); + spawn_process(udevcmd2); + spawn_process(udevcmd3); + spawn_process(udevcmd4); - log("Mounting filesystems"); - spawn_process({ "mount", "-a", 0}); + log_message("Mounting filesystems"); + char* mountcmd[] = { "mount", "-a", 0}; + spawn_process(mountcmd); - log("Enabling swap"); - spawn_process({ "swapon", "-a", 0}); + log_message("Enabling swap"); + char* swapcmd[] = { "spawon", mountcmd[1], 0}; + spawn_process(swapcmd); - log("Seeding random"); + log_message("Seeding random"); load_random_seed(); - log("Setting up loopback"); - spawn_process({ "ip", "link", "set", "up", "dev", "lo", 0}); + log_message("Setting up loopback"); + char* netloopcmd[] = { "ip", "link", "set", "up", "dev", "lo", 0}; + spawn_process(netloopcmd); - log("Killing device managers"); + log_message("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]); + log_message("Boot completed, starting ttys"); + for(int n = 0; ttys[n]; n++) { + spawn_process(ttys[n]); } } diff --git a/initscripts/util.c b/initscripts/util.c @@ -1,12 +1,14 @@ #include <sys/types.h> #include <sys/stat.h> +#include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> +#include <mntent.h> #include <stdio.h> -void spawn_process(char* argv[]) { +void spawn_process(char** argv) { if (fork() == 0) { setsid(); execvp(argv[0], argv); @@ -15,10 +17,22 @@ void spawn_process(char* argv[]) { } } +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) { char ret = 0; char* dirpath = 0; - for (int n = 1, path[n] != '\0' && ret == 0; n++) { + for (int n = 1; path[n] != '\0' && ret == 0; n++) { if (path[n] == '/') { dirpath = realloc(dirpath, n + 1); strncpy(dirpath, path, n); @@ -29,21 +43,13 @@ int make_path(char* path) { 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) { +void log_message(char* msg) { printf("\033[31;1m=>\033[m %s\n", msg); - printk("init: %s\n", msg); + if(access("/dev/kmsg", F_OK|W_OK) != -1) { + FILE* fp = fopen("/dev/kmsg", "w"); + fprintf(fp, "init: %s\n", msg); + fclose(fp); + } } int is_mounted(char* path) { @@ -62,7 +68,7 @@ int is_mounted(char* path) { return 0; while (NULL != (ent = getmntent(aFile))) { - if(!strcmp(path, ent->mnt_dir) + if(!strcmp(path, ent->mnt_dir)) return 1; } endmntent(aFile); @@ -71,13 +77,13 @@ int is_mounted(char* path) { void mount(char* options, char* type, char* device, char* mountpoint) { if (is_mounted(mountpoint)) { - char newopts[256] = "remount," + char newopts[256] = "remount,"; strcat(newopts,options); options = newopts; } - char *const mountcmd[] = { "/bin/mount", "-o", options, "-t", type, device, mountpoint, 0 }; - spawn(mountcmd); + char* mountcmd[] = { "/bin/mount", "-o", options, "-t", type, device, mountpoint, 0 }; + spawn_process(mountcmd); } void save_random_seed() { @@ -88,15 +94,14 @@ void save_random_seed() { if (fdesc < 0) return; - else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0) + else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0)) return; - fdesc = open("/var/lib/init/random-seed", O_CREAT|O_WRONLY); if (fdesc < 0) return; - else + else write(fdesc, random_buffer, sizeof(random_buffer)); } @@ -110,13 +115,13 @@ void load_random_seed() { if (fdesc < 0) return; - else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0) + else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0)) return; fdesc = open("/dev/urandom", O_WRONLY); if (fdesc < 0) return; - else + else write(fdesc, random_buffer, sizeof(random_buffer)); } diff --git a/initscripts/util.h b/initscripts/util.h @@ -1,10 +1,10 @@ -void spawn_process(char* argv[]); +void spawn_process(char** argv); int make_path(char* path); -void log(char* msg); +void log_message (char* msg); -int char is_mounted(char* path); +int is_mounted(char* path); void mount(char* options, char* type, char* device, char* mountpoint);