feat: Add proper device handling
This commit is contained in:
parent
543e1cdd14
commit
8c6d9e4ab6
3 changed files with 14 additions and 9 deletions
|
@ -7,4 +7,3 @@ Drop-in replacement for busybox init (kind of)
|
||||||
### TODO
|
### TODO
|
||||||
|
|
||||||
- askfirst handling
|
- askfirst handling
|
||||||
- other devices than /dev/console
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
15
process.go
15
process.go
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue