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
- askfirst handling
- other devices than /dev/console

View file

@ -2,18 +2,17 @@ package main
import (
"fmt"
"io"
"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 {
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 {
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, " ")
cmd := exec.Command(cmdline[0], cmdline[1:]...)
// TODO: add stdio handling
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdio := os.Stdout
if entry.Device != "" {
dev, err := GetDevice(entry.Device)
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()
if err != nil {