init/inittab.go

93 lines
1.4 KiB
Go
Raw Normal View History

2021-11-29 01:18:43 +00:00
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,
}
2021-11-29 21:12:08 +00:00
type InitTabEntry struct {
2021-11-29 21:11:48 +00:00
Device string
2021-11-29 01:18:43 +00:00
Action Action
Process string
}
2021-11-29 21:12:08 +00:00
type InitTab []InitTabEntry
func (i InitTab) Entries(action Action) InitTab {
var res InitTab
for _, entry := range i {
if entry.Action == action {
res = append(res, entry)
}
}
return res
}
var DefaultInitTab = InitTab{
2021-11-29 01:18:43 +00:00
{"", 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"},
}
2021-11-29 21:12:08 +00:00
func ParseInitTab(reader io.Reader) InitTab {
var res InitTab
2021-11-29 01:18:43 +00:00
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
}
2021-11-29 21:12:08 +00:00
res = append(res, InitTabEntry{
2021-11-29 21:11:48 +00:00
Device: tokens[0],
2021-11-29 01:18:43 +00:00
Action: action,
Process: tokens[3],
})
}
return res
}