Skip to content

Commit

Permalink
virtio: Simplify setRawMode
Browse files Browse the repository at this point in the history
This uses more helper APIs from github.com/pkg/term/termios, and makes
the code closer to Apple's recommendations in
https://developer.apple.com/documentation/virtualization/running_linux_in_a_virtual_machine?language=objc#:~:text=Configure%20the%20Serial%20Port%20Device%20for%20Standard%20In%20and%20Out
This also removes direct use of `syscall` in pkg/vf/virtio.go

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau authored and evidolob committed Feb 14, 2025
1 parent 1cefce1 commit 33b62fd
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pkg/vf/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/crc-org/vfkit/pkg/config"
"github.com/onsi/gocleanup"
Expand Down Expand Up @@ -232,21 +231,19 @@ func unixFd(fd uintptr) int {
// https://developer.apple.com/documentation/virtualization/running_linux_in_a_virtual_machine#3880009
func setRawMode(f *os.File) error {
// Get settings for terminal
attr, _ := unix.IoctlGetTermios(unixFd(f.Fd()), unix.TIOCGETA)
var attr unix.Termios
err := termios.Tcgetattr(f.Fd(), &attr)
if err != nil {
return err
}

// Put stdin into raw mode, disabling local echo, input canonicalization,
// and CR-NL mapping.
attr.Iflag &^= syscall.ICRNL
attr.Lflag &^= syscall.ICANON | syscall.ECHO

// Set minimum characters when reading = 1 char
attr.Cc[syscall.VMIN] = 1

// set timeout when reading as non-canonical mode
attr.Cc[syscall.VTIME] = 0
attr.Iflag &^= unix.ICRNL
attr.Lflag &^= unix.ICANON | unix.ECHO

// reflects the changed settings
return unix.IoctlSetTermios(unixFd(f.Fd()), unix.TIOCSETA, attr)
return termios.Tcsetattr(f.Fd(), termios.TCSANOW, &attr)
}

func (dev *VirtioSerial) toVz() (*vz.VirtioConsoleDeviceSerialPortConfiguration, error) {
Expand Down

0 comments on commit 33b62fd

Please sign in to comment.