feat: Add spawning processes
This commit is contained in:
parent
e8591c5770
commit
2454b772dc
2 changed files with 80 additions and 1 deletions
42
main.go
42
main.go
|
@ -11,8 +11,48 @@ func main() {
|
|||
inittab = ParseInitTab(file)
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
fmt.Println("close inittab:", err)
|
||||
fmt.Printf("error closing inittab: %s\n", err)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("error reading inittab: %s\n", err)
|
||||
}
|
||||
|
||||
for _, entry := range inittab.Entries(SysInit) {
|
||||
err := Exec(entry)
|
||||
if err != nil {
|
||||
fmt.Printf("error running sysinit \"%s\": %s\n", entry.Process, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, entry := range inittab.Entries(Wait) {
|
||||
err := Exec(entry)
|
||||
if err != nil {
|
||||
fmt.Printf("error running wait \"%s\": %s\n", entry.Process, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, entry := range inittab.Entries(Once) {
|
||||
_, err := Spawn(entry)
|
||||
if err != nil {
|
||||
fmt.Printf("error running once \"%s\": %s\n", entry.Process, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO implement AskFirst handling
|
||||
|
||||
for _, entry := range inittab.Entries(Respawn) {
|
||||
go func(entry InitTabEntry) {
|
||||
for {
|
||||
err := Exec(entry)
|
||||
if err != nil {
|
||||
fmt.Printf("error running respawn \"%s\": %s\n", entry.Process, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}(entry)
|
||||
}
|
||||
|
||||
// TODO implement Shutdown, Restart and CtrlAltDel handling
|
||||
|
||||
select {}
|
||||
}
|
||||
|
|
39
process.go
Normal file
39
process.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Spawn(entry InitTabEntry) (*exec.Cmd, error) {
|
||||
cmdline := strings.Split(entry.Process, " ")
|
||||
cmd := exec.Command(cmdline[0], cmdline[1:]...)
|
||||
|
||||
// TODO: add stdio handling
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func Exec(entry InitTabEntry) error {
|
||||
cmd, err := Spawn(entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("spawn: %w", err)
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
return fmt.Errorf("wait: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in a new issue