pty_darwin.go 1.2 KB

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