console_zos.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // +build zos
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package console
  15. import (
  16. "fmt"
  17. "os"
  18. "golang.org/x/sys/unix"
  19. )
  20. // NewPty creates a new pty pair
  21. // The master is returned as the first console and a string
  22. // with the path to the pty slave is returned as the second
  23. func NewPty() (Console, string, error) {
  24. var f File
  25. var err error
  26. var slave string
  27. for i := 0;; i++ {
  28. ptyp := fmt.Sprintf("/dev/ptyp%04d", i)
  29. f, err = os.OpenFile(ptyp, os.O_RDWR, 0600)
  30. if err == nil {
  31. slave = fmt.Sprintf("/dev/ttyp%04d", i)
  32. break
  33. }
  34. if os.IsNotExist(err) {
  35. return nil, "", err
  36. }
  37. // else probably Resource Busy
  38. }
  39. m, err := newMaster(f)
  40. if err != nil {
  41. return nil, "", err
  42. }
  43. return m, slave, nil
  44. }
  45. type master struct {
  46. f File
  47. original *unix.Termios
  48. }
  49. func (m *master) Read(b []byte) (int, error) {
  50. return m.f.Read(b)
  51. }
  52. func (m *master) Write(b []byte) (int, error) {
  53. return m.f.Write(b)
  54. }
  55. func (m *master) Close() error {
  56. return m.f.Close()
  57. }
  58. func (m *master) Resize(ws WinSize) error {
  59. return tcswinsz(m.f.Fd(), ws)
  60. }
  61. func (m *master) ResizeFrom(c Console) error {
  62. ws, err := c.Size()
  63. if err != nil {
  64. return err
  65. }
  66. return m.Resize(ws)
  67. }
  68. func (m *master) Reset() error {
  69. if m.original == nil {
  70. return nil
  71. }
  72. return tcset(m.f.Fd(), m.original)
  73. }
  74. func (m *master) getCurrent() (unix.Termios, error) {
  75. var termios unix.Termios
  76. if err := tcget(m.f.Fd(), &termios); err != nil {
  77. return unix.Termios{}, err
  78. }
  79. return termios, nil
  80. }
  81. func (m *master) SetRaw() error {
  82. rawState, err := m.getCurrent()
  83. if err != nil {
  84. return err
  85. }
  86. rawState = cfmakeraw(rawState)
  87. rawState.Oflag = rawState.Oflag | unix.OPOST
  88. return tcset(m.f.Fd(), &rawState)
  89. }
  90. func (m *master) DisableEcho() error {
  91. rawState, err := m.getCurrent()
  92. if err != nil {
  93. return err
  94. }
  95. rawState.Lflag = rawState.Lflag &^ unix.ECHO
  96. return tcset(m.f.Fd(), &rawState)
  97. }
  98. func (m *master) Size() (WinSize, error) {
  99. return tcgwinsz(m.f.Fd())
  100. }
  101. func (m *master) Fd() uintptr {
  102. return m.f.Fd()
  103. }
  104. func (m *master) Name() string {
  105. return m.f.Name()
  106. }
  107. // checkConsole checks if the provided file is a console
  108. func checkConsole(f File) error {
  109. var termios unix.Termios
  110. if tcget(f.Fd(), &termios) != nil {
  111. return ErrNotAConsole
  112. }
  113. return nil
  114. }
  115. func newMaster(f File) (Console, error) {
  116. m := &master{
  117. f: f,
  118. }
  119. t, err := m.getCurrent()
  120. if err != nil {
  121. return nil, err
  122. }
  123. m.original = &t
  124. return m, nil
  125. }
  126. // ClearONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
  127. // created by us acts normally. In particular, a not-very-well-known default of
  128. // Linux unix98 ptys is that they have +onlcr by default. While this isn't a
  129. // problem for terminal emulators, because we relay data from the terminal we
  130. // also relay that funky line discipline.
  131. func ClearONLCR(fd uintptr) error {
  132. return setONLCR(fd, false)
  133. }
  134. // SetONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
  135. // created by us acts as intended for a terminal emulator.
  136. func SetONLCR(fd uintptr) error {
  137. return setONLCR(fd, true)
  138. }