run.go 629 B

12345678910111213141516171819202122232425262728293031323334
  1. // +build !windows
  2. package pty
  3. import (
  4. "os"
  5. "os/exec"
  6. "syscall"
  7. )
  8. // Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
  9. // and c.Stderr, calls c.Start, and returns the File of the tty's
  10. // corresponding pty.
  11. func Start(c *exec.Cmd) (pty *os.File, err error) {
  12. pty, tty, err := Open()
  13. if err != nil {
  14. return nil, err
  15. }
  16. defer tty.Close()
  17. c.Stdout = tty
  18. c.Stdin = tty
  19. c.Stderr = tty
  20. if c.SysProcAttr == nil {
  21. c.SysProcAttr = &syscall.SysProcAttr{}
  22. }
  23. c.SysProcAttr.Setctty = true
  24. c.SysProcAttr.Setsid = true
  25. err = c.Start()
  26. if err != nil {
  27. pty.Close()
  28. return nil, err
  29. }
  30. return pty, err
  31. }