pty_session.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package session
  15. import (
  16. "os"
  17. "os/exec"
  18. "os/signal"
  19. "syscall"
  20. "github.com/creack/pty"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. type Pty struct {
  25. Session *SSession
  26. Cmd *exec.Cmd
  27. Pty *os.File
  28. sizeCh chan os.Signal
  29. size *pty.Winsize
  30. OriginSize *pty.Winsize
  31. Exit bool
  32. }
  33. func NewPty(session *SSession) (p *Pty, err error) {
  34. cmd := session.GetCommand()
  35. p = &Pty{
  36. Session: session,
  37. Cmd: cmd,
  38. Exit: false,
  39. Pty: nil,
  40. }
  41. log.Debugf("[session %s] Start command: %#v", session.Id, cmd)
  42. if cmd != nil {
  43. p.Pty, err = pty.Start(p.Cmd)
  44. if err != nil {
  45. log.Errorf("start cmd error: %v", err)
  46. p.Cmd = nil
  47. return
  48. }
  49. }
  50. p.sizeCh = make(chan os.Signal, 1)
  51. p.size = &pty.Winsize{}
  52. p.startResizeMonitor()
  53. signal.Notify(p.sizeCh, syscall.SIGWINCH) // Initail resize
  54. return
  55. }
  56. func (p *Pty) IsInShellMode() bool {
  57. if p.Cmd == nil || p.Cmd.Process == nil {
  58. return false
  59. }
  60. return true
  61. }
  62. func (p *Pty) Read() ([]byte, error) {
  63. if !p.IsInShellMode() {
  64. return nil, errors.Error("not in shell mode")
  65. }
  66. buf := make([]byte, 1024)
  67. n, err := p.Pty.Read(buf)
  68. if err != nil {
  69. return nil, errors.Wrap(err, "Pty.Read")
  70. }
  71. return buf[0:n], nil
  72. }
  73. func (p *Pty) startResizeMonitor() {
  74. go func() {
  75. for range p.sizeCh {
  76. if p.Pty != nil {
  77. if err := pty.Setsize(p.Pty, p.size); err != nil {
  78. log.Errorf("Resize pty error: %v", err)
  79. } else {
  80. log.Debugf("Resize pty to %#v, cmd: %#v", p.size, p.Cmd)
  81. }
  82. }
  83. p.OriginSize = p.size
  84. }
  85. }()
  86. }
  87. func (p *Pty) Resize(size *pty.Winsize) {
  88. p.size = size
  89. p.sizeCh <- syscall.SIGWINCH
  90. }
  91. func (p *Pty) Stop() (err error) {
  92. var errs []error
  93. defer func() {
  94. p.Cmd, p.Pty = nil, nil
  95. }()
  96. defer func() {
  97. err = errors.NewAggregate(errs)
  98. }()
  99. // LOCK required
  100. defer func() {
  101. if err := p.Session.Close(); err != nil {
  102. errs = append(errs, err)
  103. }
  104. }()
  105. defer func() {
  106. if p.Cmd != nil && p.Cmd.Process != nil {
  107. log.Debugf("[%s] stop cmd: %s", p.Session.Id, p.Cmd.String())
  108. err := p.Cmd.Process.Signal(os.Kill)
  109. if err != nil {
  110. errs = append(errs, err)
  111. log.Errorf("Kill command process error: %v", err)
  112. return
  113. }
  114. err = p.Cmd.Wait()
  115. if err != nil {
  116. errs = append(errs, err)
  117. log.Errorf("Wait command error: %v", err)
  118. return
  119. }
  120. }
  121. }()
  122. defer func() {
  123. if p.Pty != nil {
  124. err := p.Pty.Close()
  125. if err != nil {
  126. log.Errorf("Close PTY error: %v", err)
  127. errs = append(errs, err)
  128. return
  129. }
  130. }
  131. }()
  132. return
  133. }