pty_openbsd.go 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build openbsd
  2. // +build openbsd
  3. package pty
  4. import (
  5. "os"
  6. "syscall"
  7. "unsafe"
  8. )
  9. func cInt8ToString(in []int8) string {
  10. var s []byte
  11. for _, v := range in {
  12. if v == 0 {
  13. break
  14. }
  15. s = append(s, byte(v))
  16. }
  17. return string(s)
  18. }
  19. func open() (pty, tty *os.File, err error) {
  20. /*
  21. * from ptm(4):
  22. * The PTMGET command allocates a free pseudo terminal, changes its
  23. * ownership to the caller, revokes the access privileges for all previous
  24. * users, opens the file descriptors for the pty and tty devices and
  25. * returns them to the caller in struct ptmget.
  26. */
  27. p, err := os.OpenFile("/dev/ptm", os.O_RDWR|syscall.O_CLOEXEC, 0)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. defer p.Close()
  32. var ptm ptmget
  33. if err := ioctl(p.Fd(), uintptr(ioctl_PTMGET), uintptr(unsafe.Pointer(&ptm))); err != nil {
  34. return nil, nil, err
  35. }
  36. pty = os.NewFile(uintptr(ptm.Cfd), cInt8ToString(ptm.Cn[:]))
  37. tty = os.NewFile(uintptr(ptm.Sfd), cInt8ToString(ptm.Sn[:]))
  38. return pty, tty, nil
  39. }