pty_linux.go 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // In case of error after this point, make sure we close the ptmx fd.
  14. defer func() {
  15. if err != nil {
  16. _ = p.Close() // Best effort.
  17. }
  18. }()
  19. sname, err := ptsname(p)
  20. if err != nil {
  21. return nil, nil, err
  22. }
  23. if err := unlockpt(p); err != nil {
  24. return nil, nil, err
  25. }
  26. t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
  27. if err != nil {
  28. return nil, nil, err
  29. }
  30. return p, t, nil
  31. }
  32. func ptsname(f *os.File) (string, error) {
  33. var n _C_uint
  34. err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
  35. if err != nil {
  36. return "", err
  37. }
  38. return "/dev/pts/" + strconv.Itoa(int(n)), nil
  39. }
  40. func unlockpt(f *os.File) error {
  41. var u _C_int
  42. // use TIOCSPTLCK with a pointer to zero to clear the lock
  43. return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
  44. }