Files
creack.pty/pty_openbsd.go
Vitaly Potyarkin 291695431f Do not set file descriptor into blocking mode
(*os.File).Fd() implicitly sets file descriptor into blocking mode [1], [2]
We avoid that by calling (*os.File).SyscallConn instead.
This method was added in go1.12 (released on 2019-02-25)

[1]: https://pkg.go.dev/os#File.Fd
[2]: https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/os/file_unix.go;l=80-87
2023-04-28 09:24:09 +00:00

48 lines
987 B
Go

//go:build openbsd
// +build openbsd
package pty
import (
"os"
"syscall"
"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):
* The PTMGET command allocates a free pseudo terminal, changes its
* ownership to the caller, revokes the access privileges for all previous
* users, opens the file descriptors for the pty and tty devices and
* returns them to the caller in struct ptmget.
*/
p, err := os.OpenFile("/dev/ptm", os.O_RDWR|syscall.O_CLOEXEC, 0)
if err != nil {
return nil, nil, err
}
defer p.Close()
var ptm ptmget
if err := ioctl(p, uintptr(ioctl_PTMGET), uintptr(unsafe.Pointer(&ptm))); err != nil {
return nil, nil, err
}
pty = os.NewFile(uintptr(ptm.Cfd), cInt8ToString(ptm.Cn[:]))
tty = os.NewFile(uintptr(ptm.Sfd), cInt8ToString(ptm.Sn[:]))
return pty, tty, nil
}