Skip to content

Commit 1e5f10c

Browse files
committed
First draft
Signed-off-by: Adrien Gallouët <[email protected]>
1 parent 68ccd6a commit 1e5f10c

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
init

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2015-2020, Adrien Gallouët <[email protected]>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DESTDIR ?=
2+
3+
.PHONY: all
4+
all: init
5+
6+
.PHONY: install
7+
install: init
8+
install -s init $(DESTDIR)/
9+
10+
.PHONY: clean
11+
clean:
12+
rm -f init

init.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#define _GNU_SOURCE
2+
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
8+
#include <sys/stat.h>
9+
#include <sys/wait.h>
10+
#include <sys/mount.h>
11+
12+
static void
13+
spawn(const char *cmd)
14+
{
15+
pid_t pid = fork();
16+
17+
if (pid == (pid_t)-1) {
18+
perror("fork");
19+
return;
20+
}
21+
22+
if (pid == (pid_t)0) {
23+
static sigset_t set;
24+
sigfillset(&set);
25+
sigprocmask(SIG_UNBLOCK, &set, NULL);
26+
setsid();
27+
execl(cmd, cmd, (char *)NULL);
28+
perror("execl");
29+
_exit(1);
30+
}
31+
32+
while (waitpid(-1, NULL, 0) != pid);
33+
}
34+
35+
static void
36+
init_fs(void)
37+
{
38+
chdir("/");
39+
40+
if (mount(NULL, "/", NULL, MS_NOSUID|MS_REMOUNT, NULL))
41+
perror("remount(/)");
42+
43+
mkdir("/dev", 0755);
44+
mkdir("/proc", 0755);
45+
mkdir("/tmp", 01777);
46+
mkdir("/sys", 0755);
47+
48+
if (mount("none", "/dev", "devtmpfs", 0, NULL))
49+
perror("mount(dev)");
50+
51+
if (mount("none", "/proc", "proc", 0, NULL))
52+
perror("mount(proc)");
53+
54+
if (mount("none", "/sys", "sysfs", 0, NULL))
55+
perror("mount(sys)");
56+
57+
if (mount("none", "/sys/fs/cgroup", "cgroup2", 0, NULL))
58+
perror("mount(cgroup2)");
59+
60+
mkdir("/dev/pts", 0755);
61+
mkdir("/dev/shm", 0755);
62+
63+
if (mount("devpts", "/dev/pts", "devpts", 0, "gid=5,mode=0620,ptmxmode=0666"))
64+
perror("mount(devpts)");
65+
}
66+
67+
static void
68+
init_console(void)
69+
{
70+
int fd = open("/dev/console", O_RDWR | O_NONBLOCK | O_NOCTTY);
71+
72+
if (fd < 0) {
73+
perror("open(console)");
74+
return;
75+
}
76+
77+
dup2(fd, 0);
78+
dup2(fd, 1);
79+
dup2(fd, 2);
80+
81+
if (fd > 2)
82+
close(fd);
83+
}
84+
85+
static void
86+
init_term(void)
87+
{
88+
if (getenv("TERM"))
89+
return;
90+
91+
putenv("TERM=linux");
92+
}
93+
94+
int
95+
main(void)
96+
{
97+
if (getpid() != 1)
98+
return 1;
99+
100+
setsid();
101+
102+
init_fs();
103+
init_console();
104+
init_term();
105+
106+
static sigset_t set;
107+
sigfillset(&set);
108+
sigprocmask(SIG_BLOCK, &set, NULL);
109+
110+
while (1) {
111+
spawn("/etc/boot");
112+
spawn("/etc/reboot");
113+
sleep(1);
114+
}
115+
116+
return 0;
117+
}

0 commit comments

Comments
 (0)