-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
145 lines (129 loc) · 2.95 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
)
const HostnameUser = "/etc/hostname"
const HostnameKernel = "/proc/sys/kernel/hostname"
const EntropyReserve = "/var/entropy"
const EntropyKernel = "/dev/random"
const InitDir = "/etc/init"
const ZeroDir = InitDir + "/0"
const OneDir = InitDir + "/1"
const OneDirOnce = OneDir + "/once"
const OneDirRepeat = OneDir + "/repeat"
const TwoDir = InitDir + "/2"
func system(prog string, args ...string) {
cmd := exec.Command(prog, args...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
}
func mountSystem() {
syscall.Mount("proc", "/proc", "proc", 0x4|0x2|0x8, "")
syscall.Mount("sys", "/sys", "sysfs", 0x4|0x2|0x8, "")
syscall.Mount("run", "/run", "tmpfs", 0x4|0x2, "mode=0755")
syscall.Mount("dev", "/dev", "devtmpfs", 0x2, "mode=0755")
}
func hostname() {
host, err := ioutil.ReadFile(HostnameUser)
if err != nil {
host = []byte("linux")
}
ioutil.WriteFile(HostnameKernel, host, 0644)
}
func RunEachIn(directory string) {
filepath.Walk(directory, func(current string, info os.FileInfo, err error) error {
if info.IsDir() || err != nil {
return nil
}
absolute, _ := filepath.Abs(current)
system(absolute)
return nil
})
}
func StartEachIn(directory string) {
filepath.Walk(directory, func(current string, info os.FileInfo, err error) error {
if info.IsDir() || err != nil {
return nil
}
absolute, _ := filepath.Abs(current)
go system(absolute)
return nil
})
}
func StartLoopEachIn(directory string) {
filepath.Walk(directory, func(current string, info os.FileInfo, err error) error {
if info.IsDir() || err != nil {
return nil
}
absolute, _ := filepath.Abs(current)
go StartLoop(absolute)
return nil
})
}
func StartLoop(absolute string) {
for {
system(absolute)
}
}
func preserveEntropy() {
entropy := make([]byte, 1024)
rand.Read(entropy)
ioutil.WriteFile(EntropyReserve, entropy, 0644)
}
func injectEntropy() {
entropy, err := ioutil.ReadFile(EntropyReserve)
if err != nil {
return
}
ioutil.WriteFile(EntropyKernel, entropy, 0644)
}
func Zero() {
fmt.Println("-- Phase 0: preliminary system setup.")
mountSystem()
injectEntropy()
hostname()
RunEachIn(ZeroDir)
}
func One() {
fmt.Println("-- Phase 1: launching userspace coroutines.")
go StartEachIn(OneDirOnce)
StartLoopEachIn(OneDirRepeat)
}
func Two() {
fmt.Println("-- Phase 2: reboot triggered, shutdown hooks.")
preserveEntropy()
RunEachIn(TwoDir)
syscall.Sync()
}
func main() {
if os.Getpid() != 1 {
fmt.Println("Not run as pid 1.")
os.Exit(1)
}
sigc := make(chan os.Signal, 2)
signal.Notify(sigc, syscall.SIGUSR1, syscall.SIGUSR2)
go func() {
sig := <-sigc
Two()
switch sig {
case syscall.SIGUSR1:
syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
case syscall.SIGUSR2:
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}
}()
Zero()
One()
for {
syscall.Wait4(-1, nil, 0, nil)
}
}