forked from canonical/lxd
/
driver_qemu_cmd.go
80 lines (66 loc) · 1.68 KB
/
driver_qemu_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package drivers
import (
"strconv"
"golang.org/x/sys/unix"
lxdClient "github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/logger"
)
// Cmd represents a running command for an Qemu VM.
type qemuCmd struct {
attachedChildPid int
cmd lxdClient.Operation
dataDone chan bool
controlSendCh chan api.InstanceExecControl
controlResCh chan error
cleanupFunc func()
}
// PID returns the attached child's process ID.
func (c *qemuCmd) PID() int {
return c.attachedChildPid
}
// Signal sends a signal to the command.
func (c *qemuCmd) Signal(sig unix.Signal) error {
command := api.InstanceExecControl{
Command: "signal",
Signal: int(sig),
}
c.controlSendCh <- command
err := <-c.controlResCh
if err != nil {
return err
}
logger.Debugf(`Forwarded signal "%d" to lxd-agent`, sig)
return nil
}
// Wait for the command to end and returns its exit code and any error.
func (c *qemuCmd) Wait() (int, error) {
if c.cleanupFunc != nil {
defer c.cleanupFunc()
}
err := c.cmd.Wait()
if err != nil {
return -1, err
}
opAPI := c.cmd.Get()
<-c.dataDone
exitCode := int(opAPI.Metadata["return"].(float64))
return exitCode, nil
}
// WindowResize resizes the running command's window.
func (c *qemuCmd) WindowResize(fd, winchWidth, winchHeight int) error {
command := api.InstanceExecControl{
Command: "window-resize",
Args: map[string]string{
"width": strconv.Itoa(winchWidth),
"height": strconv.Itoa(winchHeight),
},
}
c.controlSendCh <- command
err := <-c.controlResCh
if err != nil {
return err
}
logger.Debugf(`Forwarded window resize "%dx%d" to lxd-agent`, winchWidth, winchHeight)
return nil
}