feat: Add proper device handling

This commit is contained in:
ptrcnull 2023-01-05 02:29:23 +01:00
parent 543e1cdd14
commit 8c6d9e4ab6
Signed by: ptrcnull
GPG key ID: 411F7B30801DD9CA
3 changed files with 14 additions and 9 deletions

View file

@ -7,4 +7,3 @@ Drop-in replacement for busybox init (kind of)
### TODO ### TODO
- askfirst handling - askfirst handling
- other devices than /dev/console

View file

@ -2,18 +2,17 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
) )
var devices map[string]io.ReadWriteCloser var devices = map[string]*os.File{}
func GetDevice(name string) (io.ReadWriteCloser, error) { func GetDevice(name string) (*os.File, error) {
if dev, ok := devices[name]; ok { if dev, ok := devices[name]; ok {
return dev, nil return dev, nil
} }
dev, err := os.OpenFile("/dev/" + name, os.O_RDWR, 0644) dev, err := os.OpenFile("/dev/"+name, os.O_RDWR, 0644)
if err != nil { if err != nil {
return nil, fmt.Errorf("open: %w", err) return nil, fmt.Errorf("open: %w", err)
} }

View file

@ -11,10 +11,17 @@ func Spawn(entry InitTabEntry) (*exec.Cmd, error) {
cmdline := strings.Split(entry.Process, " ") cmdline := strings.Split(entry.Process, " ")
cmd := exec.Command(cmdline[0], cmdline[1:]...) cmd := exec.Command(cmdline[0], cmdline[1:]...)
// TODO: add stdio handling stdio := os.Stdout
cmd.Stdin = os.Stdin if entry.Device != "" {
cmd.Stdout = os.Stdout dev, err := GetDevice(entry.Device)
cmd.Stderr = os.Stderr if err != nil {
return nil, fmt.Errorf("open device %s: %w", entry.Device, err)
}
stdio = dev
}
cmd.Stdin = stdio
cmd.Stdout = stdio
cmd.Stderr = stdio
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {