Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide correct pty/tty file paths on OpenBSD #148

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pty
import (
"bytes"
"io"
"os"
"testing"
)

Expand Down Expand Up @@ -65,6 +66,44 @@ func TestName(t *testing.T) {
}
}

// TestOpenByName ensures that the name associated with the tty is valid
// and can be opened and used if passed by file name (rather than passing
// the existing open file descriptor).
func TestOpenByName(t *testing.T) {
creack marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

pty, tty, err := Open()
if err != nil {
t.Fatal(err)
}
defer pty.Close()
defer tty.Close()

ttyFile, err := os.OpenFile(tty.Name(), os.O_RDWR, 0600)
if err != nil {
t.Fatalf("Failed to open tty file: %v", err)
}
defer ttyFile.Close()

// Ensure we can write to the newly opened tty file and read on the pty.
text := []byte("ping")
n, err := ttyFile.Write(text)
if err != nil {
t.Errorf("Unexpected error from Write: %s", err)
}
if n != len(text) {
t.Errorf("Unexpected count returned from Write, got %d expected %d", n, len(text))
}

buffer := make([]byte, len(text))
if err := readBytes(pty, buffer); err != nil {
t.Errorf("Unexpected error from readBytes: %s", err)
}
if !bytes.Equal(text, buffer) {
t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, text)
}
}

func TestGetsize(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 13 additions & 2 deletions pty_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import (
"unsafe"
)

func cInt8ToString(in []int8) string {
var s []byte
for _, v := range in {
if v == 0 {
break
}
s = append(s, byte(v))
}
return string(s)
}

func open() (pty, tty *os.File, err error) {
/*
* from ptm(4):
Expand All @@ -29,8 +40,8 @@ func open() (pty, tty *os.File, err error) {
return nil, nil, err
}

pty = os.NewFile(uintptr(ptm.Cfd), "/dev/ptm")
tty = os.NewFile(uintptr(ptm.Sfd), "/dev/ptm")
pty = os.NewFile(uintptr(ptm.Cfd), cInt8ToString(ptm.Cn[:]))
creack marked this conversation as resolved.
Show resolved Hide resolved
tty = os.NewFile(uintptr(ptm.Sfd), cInt8ToString(ptm.Sn[:]))

return pty, tty, nil
}