2023-04-26 20:20:43 -04:00
|
|
|
//go:build openbsd
|
|
|
|
// +build openbsd
|
|
|
|
|
2019-04-19 07:03:37 -04:00
|
|
|
package pty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2024-01-08 04:22:09 -05:00
|
|
|
func cInt8ToString(in []int8) string {
|
|
|
|
var s []byte
|
|
|
|
for _, v := range in {
|
|
|
|
if v == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s = append(s, byte(v))
|
|
|
|
}
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:03:37 -04:00
|
|
|
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
|
2024-01-08 04:22:09 -05:00
|
|
|
if err := ioctl(p, uintptr(ioctl_PTMGET), uintptr(unsafe.Pointer(&ptm))); err != nil {
|
2019-04-19 07:03:37 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-08 04:22:09 -05:00
|
|
|
pty = os.NewFile(uintptr(ptm.Cfd), cInt8ToString(ptm.Cn[:]))
|
|
|
|
tty = os.NewFile(uintptr(ptm.Sfd), cInt8ToString(ptm.Sn[:]))
|
2019-04-19 07:03:37 -04:00
|
|
|
|
|
|
|
return pty, tty, nil
|
|
|
|
}
|