-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testscript: add ttyin/ttyout commands
- Loading branch information
1 parent
ec11942
commit e748a67
Showing
11 changed files
with
270 additions
and
18 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package pty | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
const Supported = true | ||
|
||
func SetCtty(cmd *exec.Cmd, tty *os.File) { | ||
cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
Setctty: true, | ||
Setsid: true, | ||
Ctty: 3, | ||
} | ||
cmd.ExtraFiles = []*os.File{tty} | ||
} | ||
|
||
func Open() (pty, tty *os.File, err error) { | ||
p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open pty multiplexer: %v", err) | ||
} | ||
defer func() { | ||
if err != nil { | ||
p.Close() | ||
} | ||
}() | ||
|
||
name, err := ptyName(p) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to obtain tty name: %v", err) | ||
} | ||
|
||
if err := ptyGrant(p); err != nil { | ||
return nil, nil, fmt.Errorf("failed to grant pty: %v", err) | ||
} | ||
|
||
if err := ptyUnlock(p); err != nil { | ||
return nil, nil, fmt.Errorf("failed to unlock pty: %v", err) | ||
} | ||
|
||
t, err := os.OpenFile(name, os.O_RDWR|syscall.O_NOCTTY, 0) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to open TTY: %v", err) | ||
} | ||
|
||
return p, t, nil | ||
} | ||
|
||
func ioctl(f *os.File, name string, cmd, ptr uintptr) error { | ||
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), cmd, ptr) | ||
if err != 0 { | ||
return fmt.Errorf("%s ioctl failed: %v", name, err) | ||
} | ||
return nil | ||
} |
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,32 @@ | ||
package pty | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func ptyName(f *os.File) (string, error) { | ||
// Parameter length is encoded in the low 13 bits of the top word. | ||
// See https://github.com/apple/darwin-xnu/blob/2ff845c2e0/bsd/sys/ioccom.h#L69-L77 | ||
const IOCPARM_MASK = 0x1fff | ||
const TIOCPTYGNAME_PARM_LEN = (syscall.TIOCPTYGNAME >> 16) & IOCPARM_MASK | ||
out := make([]byte, TIOCPTYGNAME_PARM_LEN) | ||
|
||
err := ioctl(f, "TIOCPTYGNAME", syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&out[0]))) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
i := bytes.IndexByte(out, 0x00) | ||
return string(out[:i]), nil | ||
} | ||
|
||
func ptyGrant(f *os.File) error { | ||
return ioctl(f, "TIOCPTYGRANT", syscall.TIOCPTYGRANT, 0) | ||
} | ||
|
||
func ptyUnlock(f *os.File) error { | ||
return ioctl(f, "TIOCPTYUNLK", syscall.TIOCPTYUNLK, 0) | ||
} |
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,26 @@ | ||
package pty | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func ptyName(f *os.File) (string, error) { | ||
var out uint | ||
err := ioctl(f, "TIOCGPTN", syscall.TIOCGPTN, uintptr(unsafe.Pointer(&out))) | ||
if err != nil { | ||
return "", err | ||
} | ||
return "/dev/pts/" + strconv.Itoa(int(out)), nil | ||
} | ||
|
||
func ptyGrant(f *os.File) error { | ||
return nil | ||
} | ||
|
||
func ptyUnlock(f *os.File) error { | ||
var zero int | ||
return ioctl(f, "TIOCSPTLCK", syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&zero))) | ||
} |
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,21 @@ | ||
//go:build !linux && !darwin | ||
// +build !linux,!darwin | ||
|
||
package pty | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
const Supported = false | ||
|
||
func SetCtty(cmd *exec.Cmd, tty *os.File) error { | ||
panic("SetCtty called on unsupported platform") | ||
} | ||
|
||
func Open() (pty, tty *os.File, err error) { | ||
return nil, nil, fmt.Errorf("pty unsupported on %s", runtime.GOOS) | ||
} |
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,10 @@ | ||
[!linux] [!darwin] skip | ||
|
||
ttyin secretwords.txt | ||
terminalprompt | ||
ttyout 'magic words' | ||
! stderr . | ||
! stdout . | ||
|
||
-- secretwords.txt -- | ||
SQUEAMISHOSSIFRAGE |
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