run.go 609 B

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