-
Notifications
You must be signed in to change notification settings - Fork 907
/
driver_lxc_cmd.go
58 lines (46 loc) · 1.27 KB
/
driver_lxc_cmd.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
package drivers
import (
"os/exec"
"golang.org/x/sys/unix"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/logger"
)
// lxcCmd represents a running command for an LXC container.
type lxcCmd struct {
attachedChildPid int
cmd *exec.Cmd
}
// PID returns the attached child's process ID.
func (c *lxcCmd) PID() int {
return c.attachedChildPid
}
// Signal sends a signal to the command.
func (c *lxcCmd) Signal(sig unix.Signal) error {
err := unix.Kill(c.attachedChildPid, sig)
if err != nil {
return err
}
logger.Debugf(`Forwarded signal "%d" to PID "%d"`, sig, c.PID())
return nil
}
// Wait for the command to end and returns its exit code and any error.
func (c *lxcCmd) Wait() (int, error) {
exitStatus, err := shared.ExitStatus(c.cmd.Wait())
// Convert special exit statuses into errors.
switch exitStatus {
case 127:
err = ErrExecCommandNotFound
case 126:
err = ErrExecCommandNotExecutable
}
return exitStatus, err
}
// WindowResize resizes the running command's window.
func (c *lxcCmd) WindowResize(fd, winchWidth, winchHeight int) error {
err := shared.SetSize(fd, winchWidth, winchHeight)
if err != nil {
return err
}
logger.Debugf(`Set window size "%dx%d" of PID "%d"`, winchWidth, winchHeight, c.PID())
return nil
}