Skip to content

Commit

Permalink
play: wire up stdin to qemu to make serial work
Browse files Browse the repository at this point in the history
With this change:

1. gokrazy’s serial port starts working in qemu,
   i.e. you can press Enter and get a shell prompt.

2. qemu’s monitor key bindings start working,
   i.e. you don’t quit using Ctrl+C, but by using Ctrl+a x
  • Loading branch information
stapelberg authored and damdo committed Jul 1, 2024
1 parent 0eed327 commit 9d52fee
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions cmd/gom/cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,41 +137,27 @@ func (r *playImplConfig) play(ctx context.Context, _ []string, _, _ io.Writer) e
qemuRun := exec.CommandContext(ctx, playImpl.baseCmd, qemuArgs...)

// Pipe Stderr and Stdout to the OSes ones.
qemuRun.Stdin = os.Stdin
qemuRun.Stderr = os.Stderr
qemuRun.Stdout = os.Stdout

log.Println("about to start qemu with config:")
fmt.Println(fmtQemuConfig(qemuRun.Args))

log.Println("starting qemu:")
go func() {
if err := qemuRun.Start(); err != nil {
log.Fatalln(fmt.Errorf("%v: %w", qemuRun.Args, err))
}
}()
if err := qemuRun.Start(); err != nil {
log.Fatalln(fmt.Errorf("%v: %w", qemuRun.Args, err))
}

// Block until a SIGINT/SIGTEM signal happens (e.g. CTRL+C).
<-ctx.Done()
if err := qemuRun.Wait(); err != nil {
log.Println(fmt.Errorf("qemu.Wait(): %v", err)) //nolint:goerr113
}

// Cleanup the various temp/generated files used.
if err := os.RemoveAll(baseDir); err != nil {
log.Println(fmt.Errorf("error cleaning up temporary directory: %w", err))
}

// If a signal was sent, unblock the main thread and
// kill the qemu process.
if err := qemuRun.Process.Signal(os.Interrupt); err != nil {
log.Print(fmt.Errorf("failed to terminate the qemu process cleanly: %w", err))

if err := qemuRun.Process.Kill(); err != nil {
log.Fatal(fmt.Errorf("failed to kill the qemu process,"+
"a qemu process might have been leaked"+
"you can try and kill it manually: %w", err))
}
}

log.Println("qemu exited cleanly, shutting down")

return nil
}

Expand Down

0 comments on commit 9d52fee

Please sign in to comment.