console_unix.go 3.5 KB

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