This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from raravena80/master
signal: terminal: Changes to support build in Darwin
- Loading branch information
Showing
5 changed files
with
143 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2017 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
termiosIFlagRawTermInvMask = (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON) | ||
termiosOFlagRawTermInvMask = unix.OPOST | ||
termiosLFlagRawTermInvMask = (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN) | ||
termiosCFlagRawTermInvMask = unix.PARENB | ||
termiosCFlagRawTermMask = unix.CS8 | ||
termiosCcVMinRawTermVal = 1 | ||
termiosCcVTimeRawTermVal = 0 | ||
) | ||
|
||
func setupTerminal(fd int) (*unix.Termios, error) { | ||
termios, err := unix.IoctlGetTermios(fd, unix.TIOCGETA) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var savedTermios unix.Termios | ||
savedTermios = *termios | ||
|
||
// Set the terminal in raw mode | ||
termios.Iflag &^= termiosIFlagRawTermInvMask | ||
termios.Oflag &^= termiosOFlagRawTermInvMask | ||
termios.Lflag &^= termiosLFlagRawTermInvMask | ||
termios.Cflag &^= termiosCFlagRawTermInvMask | ||
termios.Cflag |= termiosCFlagRawTermMask | ||
termios.Cc[unix.VMIN] = termiosCcVMinRawTermVal | ||
termios.Cc[unix.VTIME] = termiosCcVTimeRawTermVal | ||
|
||
if err := unix.IoctlSetTermios(fd, unix.TIOCSETA, termios); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &savedTermios, nil | ||
} | ||
|
||
func restoreTerminal(fd int, termios *unix.Termios) error { | ||
return unix.IoctlSetTermios(fd, unix.TIOCSETA, termios) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2017 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
RawModeErr = "should be properly set in raw mode" | ||
FlagRawModeErr = "flag " + RawModeErr | ||
ValueRawModeErr = "value " + RawModeErr | ||
) | ||
|
||
func newTestTerminal(t *testing.T) (*os.File, error) { | ||
if os.Getuid() != 0 { | ||
t.Skip("Skipping this test: Requires to be root") | ||
return nil, nil | ||
} | ||
|
||
return os.OpenFile("/dev/tty", os.O_RDWR, os.ModeDevice) | ||
} | ||
|
||
func TestSetupTerminalOnNonTerminalFailure(t *testing.T) { | ||
file, err := ioutil.TempFile("", "tmp") | ||
assert.Nil(t, err, "Failed to create temporary file") | ||
defer file.Close() | ||
|
||
_, err = setupTerminal(int(file.Fd())) | ||
assert.NotNil(t, err, "Should fail because the file is not a terminal") | ||
} | ||
|
||
func TestSetupTerminalSuccess(t *testing.T) { | ||
file, err := newTestTerminal(t) | ||
|
||
if perr, ok := err.(*os.PathError); ok { | ||
switch perr.Err.(syscall.Errno) { | ||
case syscall.ENXIO: | ||
t.Skip("Skipping this test: Failed to open tty, make sure test is running in a tty") | ||
default: | ||
t.Fatalf("could not open tty %s", err) | ||
} | ||
} | ||
|
||
assert.Nil(t, err, "Failed to create terminal") | ||
defer file.Close() | ||
|
||
savedTermios, err := setupTerminal(int(file.Fd())) | ||
assert.Nil(t, err, "Should not fail because the file is a terminal") | ||
|
||
termios, err := unix.IoctlGetTermios(int(file.Fd()), unix.TIOCGETA) | ||
assert.Nil(t, err, "Failed to get terminal information") | ||
assert.True(t, (termios.Iflag&termiosIFlagRawTermInvMask) == 0, "Termios I %s", FlagRawModeErr) | ||
assert.True(t, (termios.Oflag&termiosOFlagRawTermInvMask) == 0, "Termios O %s", FlagRawModeErr) | ||
assert.True(t, (termios.Lflag&termiosLFlagRawTermInvMask) == 0, "Termios L %s", FlagRawModeErr) | ||
assert.True(t, (termios.Cflag&termiosCFlagRawTermInvMask) == 0, "Termios C %s", FlagRawModeErr) | ||
assert.True(t, (termios.Cflag&termiosCFlagRawTermMask) == termiosCFlagRawTermMask, "Termios C %s", FlagRawModeErr) | ||
assert.True(t, termios.Cc[unix.VMIN] == termiosCcVMinRawTermVal, "Termios CC VMIN %s", ValueRawModeErr) | ||
assert.True(t, termios.Cc[unix.VTIME] == termiosCcVTimeRawTermVal, "Termios CC VTIME %s", ValueRawModeErr) | ||
|
||
err = restoreTerminal(int(file.Fd()), savedTermios) | ||
assert.Nil(t, err, "Terminal should be properly restored") | ||
} |