From 9d52fee355ebdabdf0388fc05d304c33b46b5472 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 29 Jun 2024 16:43:02 +0200 Subject: [PATCH] play: wire up stdin to qemu to make serial work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cmd/gom/cmd/play.go | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/cmd/gom/cmd/play.go b/cmd/gom/cmd/play.go index 8c3f4c6..81f472b 100644 --- a/cmd/gom/cmd/play.go +++ b/cmd/gom/cmd/play.go @@ -137,6 +137,7 @@ 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 @@ -144,34 +145,19 @@ func (r *playImplConfig) play(ctx context.Context, _ []string, _, _ io.Writer) e 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 }