feat: Add basic inittab support

This commit is contained in:
ptrcnull 2021-11-29 02:18:43 +01:00
parent 0496b578c4
commit 1eb77e513e

78
inittab.go Normal file
View file

@ -0,0 +1,78 @@
package main
import (
"bufio"
"io"
"strings"
)
type Action uint8
const (
SysInit Action = iota
Wait
Once
Respawn
AskFirst
Shutdown
Restart
CtrlAltDel
)
var ActionMap = map[string]Action{
"sysinit": SysInit,
"wait": Wait,
"once": Once,
"respawn": Respawn,
"askfirst": AskFirst,
"shutdown": Shutdown,
"restart": Restart,
"ctrlaltdel": CtrlAltDel,
}
type TabEntry struct {
Id string
Action Action
Process string
}
var DefaultInittab = []TabEntry{
{"", SysInit, "/etc/init.d/rcS"},
{"", AskFirst, "/bin/sh"},
{"", CtrlAltDel, "/sbin/reboot"},
{"", Shutdown, "/sbin/swapoff -a"},
{"", Shutdown, "/bin/umount -a -r"},
{"", Restart, "/sbin/init"},
{"tty2", AskFirst, "/bin/sh"},
{"tty3", AskFirst, "/bin/sh"},
{"tty4", AskFirst, "/bin/sh"},
}
func ParseInittab(reader io.Reader) []TabEntry {
var res []TabEntry
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
tokens := strings.Split(line, ":")
if len(tokens) != 4 {
continue
}
action, ok := ActionMap[tokens[2]]
if !ok {
continue
}
res = append(res, TabEntry{
Id: tokens[0],
Action: action,
Process: tokens[3],
})
}
return res
}