termconsole.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package execdriver
  2. import (
  3. "github.com/dotcloud/docker/pkg/term"
  4. "github.com/kr/pty"
  5. "io"
  6. "os"
  7. "os/exec"
  8. )
  9. func SetTerminal(command *Command, pipes *Pipes) error {
  10. var (
  11. term Terminal
  12. err error
  13. )
  14. if command.Tty {
  15. term, err = NewTtyConsole(command, pipes)
  16. } else {
  17. term, err = NewStdConsole(command, pipes)
  18. }
  19. if err != nil {
  20. return err
  21. }
  22. command.Terminal = term
  23. return nil
  24. }
  25. type TtyConsole struct {
  26. MasterPty *os.File
  27. SlavePty *os.File
  28. }
  29. func NewTtyConsole(command *Command, pipes *Pipes) (*TtyConsole, error) {
  30. ptyMaster, ptySlave, err := pty.Open()
  31. if err != nil {
  32. return nil, err
  33. }
  34. tty := &TtyConsole{
  35. MasterPty: ptyMaster,
  36. SlavePty: ptySlave,
  37. }
  38. if err := tty.AttachPipes(&command.Cmd, pipes); err != nil {
  39. tty.Close()
  40. return nil, err
  41. }
  42. command.Console = tty.SlavePty.Name()
  43. return tty, nil
  44. }
  45. func (t *TtyConsole) Master() *os.File {
  46. return t.MasterPty
  47. }
  48. func (t *TtyConsole) Resize(h, w int) error {
  49. return term.SetWinsize(t.MasterPty.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
  50. }
  51. func (t *TtyConsole) AttachPipes(command *exec.Cmd, pipes *Pipes) error {
  52. command.Stdout = t.SlavePty
  53. command.Stderr = t.SlavePty
  54. go func() {
  55. if wb, ok := pipes.Stdout.(interface {
  56. CloseWriters() error
  57. }); ok {
  58. defer wb.CloseWriters()
  59. }
  60. io.Copy(pipes.Stdout, t.MasterPty)
  61. }()
  62. if pipes.Stdin != nil {
  63. command.Stdin = t.SlavePty
  64. command.SysProcAttr.Setctty = true
  65. go func() {
  66. defer pipes.Stdin.Close()
  67. io.Copy(t.MasterPty, pipes.Stdin)
  68. }()
  69. }
  70. return nil
  71. }
  72. func (t *TtyConsole) Close() error {
  73. t.SlavePty.Close()
  74. return t.MasterPty.Close()
  75. }
  76. type StdConsole struct {
  77. }
  78. func NewStdConsole(command *Command, pipes *Pipes) (*StdConsole, error) {
  79. std := &StdConsole{}
  80. if err := std.AttachPipes(&command.Cmd, pipes); err != nil {
  81. return nil, err
  82. }
  83. return std, nil
  84. }
  85. func (s *StdConsole) AttachPipes(command *exec.Cmd, pipes *Pipes) error {
  86. command.Stdout = pipes.Stdout
  87. command.Stderr = pipes.Stderr
  88. if pipes.Stdin != nil {
  89. stdin, err := command.StdinPipe()
  90. if err != nil {
  91. return err
  92. }
  93. go func() {
  94. defer stdin.Close()
  95. io.Copy(stdin, pipes.Stdin)
  96. }()
  97. }
  98. return nil
  99. }
  100. func (s *StdConsole) Resize(h, w int) error {
  101. // we do not need to reside a non tty
  102. return nil
  103. }
  104. func (s *StdConsole) Close() error {
  105. // nothing to close here
  106. return nil
  107. }