-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinittab.go
75 lines (65 loc) · 1.47 KB
/
inittab.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
package main
import (
"os"
"os/exec"
"runtime"
"strings"
)
func ParseInitTab(path string) []string {
file, err := os.Open(path)
if err != nil {
FatalError("an error occured while opening inittab file", err)
}
var inittab string
stat, _ := file.Stat()
data := make([]byte, stat.Size())
length, err := file.Read(data)
if err != nil {
FatalError("an error occured while reading inittab file", err)
os.Exit(2)
}
inittab = string(data[:length])
itLines := strings.Split(inittab, "\n")
return itLines
}
func ExecSysInit(script string) bool {
cmd := exec.Command(config["shell"].(string), script)
err := cmd.Start()
if err != nil {
Warning("Failed to run sysinit script", err)
return false
}
return true
}
func InitInittab() {
var inittab []string
switch runtime.GOOS {
case "linux", "freebsd":
inittab = ParseInitTab("/etc/inittab")
case "windows":
inittab = ParseInitTab("C:\\pinit\\inittab")
default:
FatalError("unsupported operating system", nil)
os.Exit(3)
}
for i := 0; i < len(inittab); i++ {
if inittab[i] == "" {
continue
}
args := strings.SplitN(inittab[i], ":", 4)
switch args[2] {
case "sysinit":
ExecSysInit(args[3])
case "respawn":
subargs := strings.Split(args[3], " ")
exec := subargs[0]
go StartRespawnProcess(exec, subargs[1:])
case "shutdown":
subargs := strings.Split(args[3], " ")
cmd := Command{
cmd: subargs[0],
args: subargs[1:] }
shutdownCmds = append(shutdownCmds, cmd)
}
}
}