pty_linux.go 866 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package pty
  2. import (
  3. "os"
  4. "strconv"
  5. "syscall"
  6. "unsafe"
  7. )
  8. func open() (pty, tty *os.File, err error) {
  9. p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
  10. if err != nil {
  11. return nil, nil, err
  12. }
  13. sname, err := ptsname(p)
  14. if err != nil {
  15. return nil, nil, err
  16. }
  17. err = unlockpt(p)
  18. if err != nil {
  19. return nil, nil, err
  20. }
  21. t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. return p, t, nil
  26. }
  27. func ptsname(f *os.File) (string, error) {
  28. var n _C_uint
  29. err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
  30. if err != nil {
  31. return "", err
  32. }
  33. return "/dev/pts/" + strconv.Itoa(int(n)), nil
  34. }
  35. func unlockpt(f *os.File) error {
  36. var u _C_int
  37. // use TIOCSPTLCK with a zero valued arg to clear the slave pty lock
  38. return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
  39. }