Compare commits

...

2 commits

Author SHA1 Message Date
ptrcnull 941d8a814b
docs: Update URL 2023-01-05 02:30:16 +01:00
ptrcnull 8c6d9e4ab6
feat: Add proper device handling 2023-01-05 02:29:23 +01:00
3 changed files with 16 additions and 9 deletions

View file

@ -4,7 +4,8 @@ Experimental implementation of PID 1.
Drop-in replacement for busybox init (kind of) Drop-in replacement for busybox init (kind of)
The canonical URL of this repository is https://git.ptrc.gay/ptrcnull/init
### 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 {