pty_solaris.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package pty
  2. /* based on:
  3. http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libc/port/gen/pt.c
  4. */
  5. import (
  6. "errors"
  7. "golang.org/x/sys/unix"
  8. "os"
  9. "strconv"
  10. "syscall"
  11. "unsafe"
  12. )
  13. const NODEV = ^uint64(0)
  14. func open() (pty, tty *os.File, err error) {
  15. masterfd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|unix.O_NOCTTY, 0)
  16. //masterfd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_CLOEXEC|unix.O_NOCTTY, 0)
  17. if err != nil {
  18. return nil, nil, err
  19. }
  20. p := os.NewFile(uintptr(masterfd), "/dev/ptmx")
  21. sname, err := ptsname(p)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. err = grantpt(p)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. err = unlockpt(p)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. slavefd, err := syscall.Open(sname, os.O_RDWR|unix.O_NOCTTY, 0)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. t := os.NewFile(uintptr(slavefd), sname)
  38. // pushing terminal driver STREAMS modules as per pts(7)
  39. for _, mod := range([]string{"ptem", "ldterm", "ttcompat"}) {
  40. err = streams_push(t, mod)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. }
  45. return p, t, nil
  46. }
  47. func minor(x uint64) uint64 {
  48. return x & 0377
  49. }
  50. func ptsdev(fd uintptr) uint64 {
  51. istr := strioctl{ISPTM, 0, 0, nil}
  52. err := ioctl(fd, I_STR, uintptr(unsafe.Pointer(&istr)))
  53. if err != nil {
  54. return NODEV
  55. }
  56. var status unix.Stat_t
  57. err = unix.Fstat(int(fd), &status)
  58. if err != nil {
  59. return NODEV
  60. }
  61. return uint64(minor(status.Rdev))
  62. }
  63. func ptsname(f *os.File) (string, error) {
  64. dev := ptsdev(f.Fd())
  65. if dev == NODEV {
  66. return "", errors.New("not a master pty")
  67. }
  68. fn := "/dev/pts/" + strconv.FormatInt(int64(dev), 10)
  69. // access(2) creates the slave device (if the pty exists)
  70. // F_OK == 0 (unistd.h)
  71. err := unix.Access(fn, 0)
  72. if err != nil {
  73. return "", err
  74. }
  75. return fn, nil
  76. }
  77. type pt_own struct {
  78. pto_ruid int32
  79. pto_rgid int32
  80. }
  81. func grantpt(f *os.File) error {
  82. if ptsdev(f.Fd()) == NODEV {
  83. return errors.New("not a master pty")
  84. }
  85. var pto pt_own
  86. pto.pto_ruid = int32(os.Getuid())
  87. // XXX should first attempt to get gid of DEFAULT_TTY_GROUP="tty"
  88. pto.pto_rgid = int32(os.Getgid())
  89. var istr strioctl
  90. istr.ic_cmd = OWNERPT
  91. istr.ic_timout = 0
  92. istr.ic_len = int32(unsafe.Sizeof(istr))
  93. istr.ic_dp = unsafe.Pointer(&pto)
  94. err := ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr)))
  95. if err != nil {
  96. return errors.New("access denied")
  97. }
  98. return nil
  99. }
  100. func unlockpt(f *os.File) error {
  101. istr := strioctl{UNLKPT, 0, 0, nil}
  102. return ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr)))
  103. }
  104. // push STREAMS modules if not already done so
  105. func streams_push(f *os.File, mod string) error {
  106. var err error
  107. buf := []byte(mod)
  108. // XXX I_FIND is not returning an error when the module
  109. // is already pushed even though truss reports a return
  110. // value of 1. A bug in the Go Solaris syscall interface?
  111. // XXX without this we are at risk of the issue
  112. // https://www.illumos.org/issues/9042
  113. // but since we are not using libc or XPG4.2, we should not be
  114. // double-pushing modules
  115. err = ioctl(f.Fd(), I_FIND, uintptr(unsafe.Pointer(&buf[0])))
  116. if err != nil {
  117. return nil
  118. }
  119. err = ioctl(f.Fd(), I_PUSH, uintptr(unsafe.Pointer(&buf[0])))
  120. return err
  121. }