pty_darwin.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //go:build darwin
  2. // +build darwin
  3. package pty
  4. import (
  5. "errors"
  6. "os"
  7. "syscall"
  8. "unsafe"
  9. )
  10. func open() (pty, tty *os.File, err error) {
  11. pFD, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_CLOEXEC, 0)
  12. if err != nil {
  13. return nil, nil, err
  14. }
  15. p := os.NewFile(uintptr(pFD), "/dev/ptmx")
  16. // In case of error after this point, make sure we close the ptmx fd.
  17. defer func() {
  18. if err != nil {
  19. _ = p.Close() // Best effort.
  20. }
  21. }()
  22. sname, err := ptsname(p)
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. if err := grantpt(p); err != nil {
  27. return nil, nil, err
  28. }
  29. if err := unlockpt(p); err != nil {
  30. return nil, nil, err
  31. }
  32. t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
  33. if err != nil {
  34. return nil, nil, err
  35. }
  36. return p, t, nil
  37. }
  38. func ptsname(f *os.File) (string, error) {
  39. n := make([]byte, _IOC_PARM_LEN(syscall.TIOCPTYGNAME))
  40. err := ioctl(f.Fd(), syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0])))
  41. if err != nil {
  42. return "", err
  43. }
  44. for i, c := range n {
  45. if c == 0 {
  46. return string(n[:i]), nil
  47. }
  48. }
  49. return "", errors.New("TIOCPTYGNAME string not NUL-terminated")
  50. }
  51. func grantpt(f *os.File) error {
  52. return ioctl(f.Fd(), syscall.TIOCPTYGRANT, 0)
  53. }
  54. func unlockpt(f *os.File) error {
  55. return ioctl(f.Fd(), syscall.TIOCPTYUNLK, 0)
  56. }