This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
stop.go
87 lines (79 loc) · 2.09 KB
/
stop.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
81
82
83
84
85
86
87
package cmd
import (
"encoding/binary"
"fmt"
"io"
"net"
"sync"
"syscall"
"code.cloudfoundry.org/cfdev/cfanalytics"
"code.cloudfoundry.org/cfdev/config"
"code.cloudfoundry.org/cfdev/process"
"github.com/spf13/cobra"
)
func NewStop(Config config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
RunE: func(cmd *cobra.Command, args []string) error {
return runStop(Config)
},
}
return cmd
}
func runStop(Config config.Config) error {
Config.Analytics.Event(cfanalytics.STOP, map[string]interface{}{"type": "cf"})
var reterr error
var all sync.WaitGroup
all.Add(4)
go func() {
defer all.Done()
if err := process.SignalAndCleanup(Config.LinuxkitPidFile, Config.CFDevHome, syscall.SIGTERM); err != nil {
reterr = fmt.Errorf("failed to terminate linuxkit: %s", err)
}
}()
go func() {
defer all.Done()
if err := process.SignalAndCleanup(Config.VpnkitPidFile, Config.CFDevHome, syscall.SIGTERM); err != nil {
reterr = fmt.Errorf("failed to terminate vpnkit: %s", err)
}
}()
go func() {
defer all.Done()
if err := process.SignalAndCleanup(Config.HyperkitPidFile, Config.CFDevHome, syscall.SIGKILL); err != nil {
reterr = fmt.Errorf("failed to terminate hyperkit: %s", err)
}
}()
go func() {
defer all.Done()
command := []byte{uint8(1)}
handshake := append([]byte("CFD3V"), make([]byte, 44, 44)...)
conn, err := net.Dial("unix", Config.CFDevDSocketPath)
if err != nil {
// cfdevd is not running-- do nothing
return
}
if err := binary.Write(conn, binary.LittleEndian, handshake); err != nil {
reterr = err
return
}
if err := binary.Read(conn, binary.LittleEndian, handshake); err != nil {
reterr = err
return
}
if err := binary.Write(conn, binary.LittleEndian, command); err != nil {
reterr = err
return
}
errorCode := make([]byte, 1, 1)
if err := binary.Read(conn, binary.LittleEndian, errorCode); err != nil {
if err != io.EOF {
reterr = err
return
}
} else if errorCode[0] != 0 {
reterr = fmt.Errorf("failed to uninstall cfdevd: errorcode: %d", errorCode[0])
}
}()
all.Wait()
return reterr
}