pty_solaris.go 3.3 KB

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