simple-init

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

commit 95005f5e4db9a5fdfdb8fd735200db656f92c132
parent 80b8ba70af3794cc5bbdf1d856b5d7f5a073ad8b
Author: depsterr <depsterr@protonmail.com>
Date:   Sun, 31 May 2020 17:26:43 +0200

updated wait for children method

Diffstat:
Minitscripts/init.c | 8++++----
Ainitscripts/shutdown.c | 24++++++++++++++++++++++++
Minitscripts/util.c | 14++++++++++++++
Minitscripts/util.h | 2++
4 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/initscripts/init.c b/initscripts/init.c @@ -13,7 +13,7 @@ int main() { 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; + wait_for_children(); mount("mode=0755,nosuid", "devtmpfs", "dev", "/dev" ); mkdir("/dev/runit", 0755); @@ -23,7 +23,7 @@ int main() { mkdir("/dev/pts" , 0755); mkdir("/dev/shm" , 0755); - WAIT_FOR_CHILDREN; + wait_for_children(); mount("mode=0620,gid=5,nosuid,noexec", "devpts", "devpts", "/dev/pts" ); mount("mode=1777,nosuid,nodev", "tmpfs", "shm", "/dev/shm" ); @@ -42,10 +42,10 @@ 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; + wait_for_children(); spawn_process(udevcmd2); spawn_process(udevcmd3); - WAIT_FOR_CHILDREN; + wait_for_children(); spawn_process(udevcmd4); log_message("Mounting filesystems"); diff --git a/initscripts/shutdown.c b/initscripts/shutdown.c @@ -0,0 +1,24 @@ +#include <sys/reboot.h> +#include <unistd.h> + +#include "util.h" + +int main(int argc, char* argv[]) { + if (argc < 2) + exit(1); + + save_random_seed(); + + sleep(2); + + char* swapcmd[] = { "swapoff", "-a", 0}; + char* umountcmd[] = { "mount", "-rat", "nosysfs,noproc,nodevtmpfs,notmpfs", 0}; + spawn_process(swapcmd); + spawn_process(umountcmd); + + if(argv[1][0] == 'p') { + reboot(RB_POWER_OFF); + } else { + reboot(RB_AUTOBOOT); + } +} diff --git a/initscripts/util.c b/initscripts/util.c @@ -8,6 +8,20 @@ #include <mntent.h> #include <stdio.h> +void wait_for_children() { + int status = 0; + while(wait(&status) > 0) { + if (WEXITSTATUS(status) != 0) { + perror("Child exited with non 0 exit code.\nDropping to shell\n"); + char* shellcmd[] = { "/bin/sh", 0 }; + int pid; + pid = spawn_process(shellcmd); + wait(pid, &status, 0); + break; + } + } +} + int spawn_process(char** argv) { int child_pid; if ((child_pid = fork()) == 0) { diff --git a/initscripts/util.h b/initscripts/util.h @@ -3,6 +3,8 @@ int status = 0; #define WAIT_FOR_CHILDREN while(wait(&status) > 0) +void wait_for_children(); + int spawn_process(char** argv); void make_path(char* path);