-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_unix.go
70 lines (59 loc) · 1.3 KB
/
cmd_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package pty
import (
"errors"
"os/exec"
"golang.org/x/sys/unix"
)
func (c *Cmd) start() error {
if c.Process != nil {
return errors.New("exec: already started")
}
pty, ok := c.pty.(*unixPty)
if !ok {
return ErrInvalidCommand
}
cmd := exec.Command(c.Path, c.Args[1:]...)
if c.ctx != nil {
cmd = exec.CommandContext(c.ctx, c.Path, c.Args[1:]...)
if c.Cancel == nil {
c.Cancel = func() error {
return cmd.Process.Kill()
}
}
}
c.sys = cmd
cmd.Dir = c.Dir
cmd.Env = c.Env
cmd.Cancel = c.Cancel
cmd.SysProcAttr = c.SysProcAttr
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &unix.SysProcAttr{}
}
cmd.Stdin = pty.slave
cmd.Stdout = pty.slave
cmd.Stderr = pty.slave
cmd.SysProcAttr.Setsid = true
cmd.SysProcAttr.Setctty = true
if err := cmd.Start(); err != nil {
return err
}
c.Process = cmd.Process
return nil
}
func (c *Cmd) wait() error {
if c.Process == nil {
return errors.New("exec: not started")
}
if c.ProcessState != nil {
return errors.New("exec: Wait was already called")
}
cmd, ok := c.sys.(*exec.Cmd)
if !ok {
return ErrInvalidCommand
}
err := cmd.Wait()
c.ProcessState = cmd.ProcessState
return err
}