From 46c073db831fed3c8cf3c744f2a9f62aa2cb653c Mon Sep 17 00:00:00 2001 From: sin Date: Wed, 5 Feb 2014 16:48:33 +0000 Subject: [PATCH] Add sinit and remove busybox init --- pkgs/busybox | 5 --- pkgs/sinit | 13 ++++++++ stuff/sinit.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 pkgs/sinit create mode 100644 stuff/sinit.c diff --git a/pkgs/busybox b/pkgs/busybox index 1af452a..fe14991 100644 --- a/pkgs/busybox +++ b/pkgs/busybox @@ -9,9 +9,6 @@ install() { pushd src/ chmod +x busybox cp busybox $root/bin - pushd $root - ln -sf /bin/busybox init - popd pushd $root/bin ln -sf busybox addgroup ln -sf busybox adduser @@ -40,8 +37,6 @@ install() { ln -sf busybox passwd ln -sf busybox ping ln -sf busybox pkill - ln -sf busybox poweroff - ln -sf busybox reboot ln -sf busybox sed ln -sf busybox sysctl ln -sf busybox tac diff --git a/pkgs/sinit b/pkgs/sinit new file mode 100644 index 0000000..9460d95 --- /dev/null +++ b/pkgs/sinit @@ -0,0 +1,13 @@ +fetch() { + cp $top/stuff/sinit.c src +} + +build() { + pushd src + x86_64-linux-musl-gcc -o init sinit.c -static -I$libcroot/include/ncurses -lncurses || return 1 + popd +} + +install() { + cp src/init $root/ +} diff --git a/stuff/sinit.c b/stuff/sinit.c new file mode 100644 index 0000000..bc2cf31 --- /dev/null +++ b/stuff/sinit.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int +spawn(const char *file, char *const argv[]) +{ + pid_t pid; + + pid = fork(); + if (pid < 0) + return pid; + if (pid == 0) { + setsid(); + setpgid(0, 0); + execvp(file, argv); + _exit(errno == ENOENT ? 127 : 126); + } +} + +int +main(void) +{ + const char *fifopath = "/var/run/init.fifo"; + sigset_t set; + pid_t pid; + fd_set rfds; + char buf[BUFSIZ], *p; + int c; + int fd; + int n; + + if (getpid() != 1) return 1; + + sigfillset(&set); + sigprocmask(SIG_BLOCK, &set, 0); + + pid = fork(); + if (pid < 0) return 1; + if (pid > 0) for (;;) wait(&c); + + sigprocmask(SIG_UNBLOCK, &set, 0); + + spawn("/bin/sh", (char *[]){ "sh", "-c", "/etc/rc", NULL }); + + unlink(fifopath); + umask(0); + mkfifo(fifopath, 0600); + fd = open(fifopath, O_RDWR | O_NONBLOCK); + while (1) { + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + n = select(fd + 1, &rfds, NULL, NULL, NULL); + if (n <= 0) + continue; + if (FD_ISSET(fd, &rfds)) { + n = read(fd, buf, sizeof(buf) - 1); + if (n < 0 || n == 0) + continue; + buf[n] = '\0'; + p = strchr(buf, '\n'); + if (p) *p = '\0'; + if (strcmp(buf, "reboot") == 0) { + spawn("/bin/sh", (char *[]){ + "sh", + "-c", + "/etc/rc.shutdown reboot", + NULL + }); + } + if (strcmp(buf, "poweroff") == 0) { + spawn("/bin/sh", (char *[]){ + "sh", + "-c", + "/etc/rc.shutdown poweroff", + NULL + }); + } + } + } + + /* unreached */ + return 0; +}