morpheus

suckless linux distro
git clone git://git.2f30.org/morpheus
Log | Files | Refs | Submodules | README | LICENSE

commit 46c073db831fed3c8cf3c744f2a9f62aa2cb653c
parent 501beefc45c9d61de8067dafc4d156da3f19f68c
Author: sin <sin@2f30.org>
Date:   Wed,  5 Feb 2014 16:48:33 +0000

Add sinit and remove busybox init

Diffstat:
Mpkgs/busybox | 5-----
Apkgs/sinit | 13+++++++++++++
Astuff/sinit.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 103 insertions(+), 5 deletions(-)

diff --git 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 @@ -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 @@ -0,0 +1,90 @@ +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <string.h> +#include <sys/select.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +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; +}