console_unix.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // +build darwin freebsd linux netbsd openbsd solaris
  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. "golang.org/x/sys/unix"
  17. )
  18. // NewPty creates a new pty pair
  19. // The master is returned as the first console and a string
  20. // with the path to the pty slave is returned as the second
  21. func NewPty() (Console, string, error) {
  22. f, err := openpt()
  23. if err != nil {
  24. return nil, "", err
  25. }
  26. slave, err := ptsname(f)
  27. if err != nil {
  28. return nil, "", err
  29. }
  30. if err := unlockpt(f); err != nil {
  31. return nil, "", err
  32. }
  33. m, err := newMaster(f)
  34. if err != nil {
  35. return nil, "", err
  36. }
  37. return m, slave, nil
  38. }
  39. type master struct {
  40. f File
  41. original *unix.Termios
  42. }
  43. func (m *master) Read(b []byte) (int, error) {
  44. return m.f.Read(b)
  45. }
  46. func (m *master) Write(b []byte) (int, error) {
  47. return m.f.Write(b)
  48. }
  49. func (m *master) Close() error {
  50. return m.f.Close()
  51. }
  52. func (m *master) Resize(ws WinSize) error {
  53. return tcswinsz(m.f.Fd(), ws)
  54. }
  55. func (m *master) ResizeFrom(c Console) error {
  56. ws, err := c.Size()
  57. if err != nil {
  58. return err
  59. }
  60. return m.Resize(ws)
  61. }
  62. func (m *master) Reset() error {
  63. if m.original == nil {
  64. return nil
  65. }
  66. return tcset(m.f.Fd(), m.original)
  67. }
  68. func (m *master) getCurrent() (unix.Termios, error) {
  69. var termios unix.Termios
  70. if err := tcget(m.f.Fd(), &termios); err != nil {
  71. return unix.Termios{}, err
  72. }
  73. return termios, nil
  74. }
  75. func (m *master) SetRaw() error {
  76. rawState, err := m.getCurrent()
  77. if err != nil {
  78. return err
  79. }
  80. rawState = cfmakeraw(rawState)
  81. rawState.Oflag = rawState.Oflag | unix.OPOST
  82. return tcset(m.f.Fd(), &rawState)
  83. }
  84. func (m *master) DisableEcho() error {
  85. rawState, err := m.getCurrent()
  86. if err != nil {
  87. return err
  88. }
  89. rawState.Lflag = rawState.Lflag &^ unix.ECHO
  90. return tcset(m.f.Fd(), &rawState)
  91. }
  92. func (m *master) Size() (WinSize, error) {
  93. return tcgwinsz(m.f.Fd())
  94. }
  95. func (m *master) Fd() uintptr {
  96. return m.f.Fd()
  97. }
  98. func (m *master) Name() string {
  99. return m.f.Name()
  100. }
  101. // checkConsole checks if the provided file is a console
  102. func checkConsole(f File) error {
  103. var termios unix.Termios
  104. if tcget(f.Fd(), &termios) != nil {
  105. return ErrNotAConsole
  106. }
  107. return nil
  108. }
  109. func newMaster(f File) (Console, error) {
  110. m := &master{
  111. f: f,
  112. }
  113. t, err := m.getCurrent()
  114. if err != nil {
  115. return nil, err
  116. }
  117. m.original = &t
  118. return m, nil
  119. }
  120. // ClearONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
  121. // created by us acts normally. In particular, a not-very-well-known default of
  122. // Linux unix98 ptys is that they have +onlcr by default. While this isn't a
  123. // problem for terminal emulators, because we relay data from the terminal we
  124. // also relay that funky line discipline.
  125. func ClearONLCR(fd uintptr) error {
  126. return setONLCR(fd, false)
  127. }
  128. // SetONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
  129. // created by us acts as intended for a terminal emulator.
  130. func SetONLCR(fd uintptr) error {
  131. return setONLCR(fd, true)
  132. }