Codebase list golang-github-creack-pty / f272787
Add StartWithAttrs to allow bypassing setsid/setctty (#97) Signed-off-by: Guillaume J. Charmes <git+guillaume@charmes.net> Guillaume J. Charmes authored 4 years ago GitHub committed 4 years ago
1 changed file(s) with 29 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
1010 // Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
1111 // and c.Stderr, calls c.Start, and returns the File of the tty's
1212 // corresponding pty.
13 //
14 // Starts the process in a new session and sets the controlling terminal.
1315 func Start(c *exec.Cmd) (pty *os.File, err error) {
1416 return StartWithSize(c, nil)
1517 }
1820 // and c.Stderr, calls c.Start, and returns the File of the tty's
1921 // corresponding pty.
2022 //
21 // This will resize the pty to the specified size before starting the command
23 // This will resize the pty to the specified size before starting the command.
24 // Starts the process in a new session and sets the controlling terminal.
2225 func StartWithSize(c *exec.Cmd, sz *Winsize) (pty *os.File, err error) {
26 if c.SysProcAttr == nil {
27 c.SysProcAttr = &syscall.SysProcAttr{}
28 }
29 c.SysProcAttr.Setsid = true
30 c.SysProcAttr.Setctty = true
31 return StartWithAttrs(c, sz, c.SysProcAttr)
32 }
33
34 // StartWithAttrs assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
35 // and c.Stderr, calls c.Start, and returns the File of the tty's
36 // corresponding pty.
37 //
38 // This will resize the pty to the specified size before starting the command if a size is provided.
39 // The `attrs` parameter overrides the one set in c.SysProcAttr.
40 //
41 // This should generally not be needed. Used in some edge cases where it is needed to create a pty
42 // without a controlling terminal.
43 func StartWithAttrs(c *exec.Cmd, sz *Winsize, attrs *syscall.SysProcAttr) (pty *os.File, err error) {
2344 pty, tty, err := Open()
2445 if err != nil {
2546 return nil, err
2647 }
2748 defer tty.Close()
49
2850 if sz != nil {
29 err = Setsize(pty, sz)
30 if err != nil {
51 if err := Setsize(pty, sz); err != nil {
3152 pty.Close()
3253 return nil, err
3354 }
4162 if c.Stdin == nil {
4263 c.Stdin = tty
4364 }
44 if c.SysProcAttr == nil {
45 c.SysProcAttr = &syscall.SysProcAttr{}
46 }
47 c.SysProcAttr.Setctty = true
48 c.SysProcAttr.Setsid = true
49 c.SysProcAttr.Ctty = int(tty.Fd())
50 err = c.Start()
51 if err != nil {
52 pty.Close()
65
66 c.SysProcAttr = attrs
67
68 if err := c.Start(); err != nil {
69 _ = pty.Close()
5370 return nil, err
5471 }
5572 return pty, err