-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathexec_windows.go
64 lines (50 loc) · 1.37 KB
/
exec_windows.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
//go:build windows
package main
import (
"io"
"os"
"os/signal"
"github.com/gorilla/websocket"
"golang.org/x/sys/windows"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
)
// Windows doesn't process ANSI sequences natively, so we wrap
// os.Stdout for improved user experience for Windows client
type WrappedWriteCloser struct {
io.Closer
wrapper io.Writer
}
func (wwc *WrappedWriteCloser) Write(p []byte) (int, error) {
return wwc.wrapper.Write(p)
}
func (c *cmdExec) getTERM() (string, bool) {
return "dumb", true
}
func (c *cmdExec) controlSocketHandler(control *websocket.Conn) {
ch := make(chan os.Signal, 10)
signal.Notify(ch, os.Interrupt)
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
defer control.WriteMessage(websocket.CloseMessage, closeMsg)
for {
sig := <-ch
switch sig {
case os.Interrupt:
logger.Debugf("Received '%s signal', forwarding to executing program.", sig)
err := c.forwardSignal(control, windows.SIGINT)
if err != nil {
logger.Debugf("Failed to forward signal '%s'.", windows.SIGINT)
return
}
default:
break
}
}
}
func (c *cmdExec) forwardSignal(control *websocket.Conn, sig windows.Signal) error {
logger.Debugf("Forwarding signal: %s", sig)
msg := api.InstanceExecControl{}
msg.Command = "signal"
msg.Signal = int(sig)
return control.WriteJSON(msg)
}