Skip to content

Commit

Permalink
First draft
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Gallouët <[email protected]>
  • Loading branch information
angt committed Feb 23, 2020
1 parent 68ccd6a commit 1e5f10c
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
init
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2015-2020, Adrien Gallouët <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DESTDIR ?=

.PHONY: all
all: init

.PHONY: install
install: init
install -s init $(DESTDIR)/

.PHONY: clean
clean:
rm -f init
117 changes: 117 additions & 0 deletions init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mount.h>

static void
spawn(const char *cmd)
{
pid_t pid = fork();

if (pid == (pid_t)-1) {
perror("fork");
return;
}

if (pid == (pid_t)0) {
static sigset_t set;
sigfillset(&set);
sigprocmask(SIG_UNBLOCK, &set, NULL);
setsid();
execl(cmd, cmd, (char *)NULL);
perror("execl");
_exit(1);
}

while (waitpid(-1, NULL, 0) != pid);
}

static void
init_fs(void)
{
chdir("/");

if (mount(NULL, "/", NULL, MS_NOSUID|MS_REMOUNT, NULL))
perror("remount(/)");

mkdir("/dev", 0755);
mkdir("/proc", 0755);
mkdir("/tmp", 01777);
mkdir("/sys", 0755);

if (mount("none", "/dev", "devtmpfs", 0, NULL))
perror("mount(dev)");

if (mount("none", "/proc", "proc", 0, NULL))
perror("mount(proc)");

if (mount("none", "/sys", "sysfs", 0, NULL))
perror("mount(sys)");

if (mount("none", "/sys/fs/cgroup", "cgroup2", 0, NULL))
perror("mount(cgroup2)");

mkdir("/dev/pts", 0755);
mkdir("/dev/shm", 0755);

if (mount("devpts", "/dev/pts", "devpts", 0, "gid=5,mode=0620,ptmxmode=0666"))
perror("mount(devpts)");
}

static void
init_console(void)
{
int fd = open("/dev/console", O_RDWR | O_NONBLOCK | O_NOCTTY);

if (fd < 0) {
perror("open(console)");
return;
}

dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);

if (fd > 2)
close(fd);
}

static void
init_term(void)
{
if (getenv("TERM"))
return;

putenv("TERM=linux");
}

int
main(void)
{
if (getpid() != 1)
return 1;

setsid();

init_fs();
init_console();
init_term();

static sigset_t set;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, NULL);

while (1) {
spawn("/etc/boot");
spawn("/etc/reboot");
sleep(1);
}

return 0;
}

0 comments on commit 1e5f10c

Please sign in to comment.