forked from dryark/ios_remote_provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown.go
166 lines (142 loc) · 4.52 KB
/
shutdown.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
//"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"strings"
"strconv"
"time"
log "github.com/sirupsen/logrus"
si "github.com/elastic/go-sysinfo"
)
func coro_sigterm( config *Config, devTracker *DeviceTracker ) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<- c
log.WithFields( log.Fields{
"type": "sigterm",
"state": "begun",
} ).Info("Shutdown started")
devTracker.shutdown()
log.WithFields( log.Fields{
"type": "sigterm",
"state": "stopped",
} ).Info("Normal stop done... cleaning up leftover procs")
cleanup_procs( config )
log.WithFields( log.Fields{
"type": "sigterm",
"state": "done",
} ).Info("Shutdown finished")
os.Exit(0)
}()
}
type Aproc struct {
pid int
cmd string
}
func get_procs() []Aproc {
out, _ := exec.Command( "ps", "-eo", "pid,args" ).Output()
lines := strings.Split( string(out), "\n" )
procs := []Aproc{}
for _,line := range lines {
if line == "" { continue }
i := 0
for ; i<len(line) ; i++ {
if line[i] != ' ' { break }
}
line := line[i:]
parts := strings.Split( line, " " )
//if strings.Contains( parts[1], "iosif" ) {
// fmt.Printf("line:%s %s\n", parts[0], parts[1] )
//}
pid, _ := strconv.Atoi( parts[0] )
procs = append( procs, Aproc{ pid: pid, cmd: parts[1] } )
}
return procs
}
func cleanup_procs( config *Config ) {
plog := log.WithFields( log.Fields{
"type": "proc_cleanup",
} )
procMap := map[string]string {
"iosif": config.iosIfPath,
"xcodebuild": "/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild",
}
procs := get_procs()
var hangingPids []int
for _,proc := range procs {
for k,v := range procMap {
if proc.cmd == v {
pid := proc.pid //proc.PID()
plog.WithFields( log.Fields{
"proc": k,
"pid": pid,
} ).Warn("Leftover " + k + " - Sending SIGTERM")
syscall.Kill( pid, syscall.SIGTERM )
hangingPids = append( hangingPids, pid )
}
}
}
if len( hangingPids ) > 0 {
// Give the processes half a second to shudown cleanly
time.Sleep( time.Millisecond * 500 )
// Send kill to processes still around
for _, pid := range( hangingPids ) {
proc, _ := si.Process( pid )
if proc != nil {
info, infoErr := proc.Info()
arg0 := "unknown"
if infoErr == nil {
args := info.Args
arg0 = args[0]
} else {
// If the process vanished before here; it errors out fetching info
continue
}
plog.WithFields( log.Fields{
"arg0": arg0,
} ).Warn("Leftover Proc - Sending SIGKILL")
syscall.Kill( pid, syscall.SIGKILL )
}
}
// Spend up to 500 ms waiting for killed processes to vanish
i := 0
for {
i = i + 1
time.Sleep( time.Millisecond * 100 )
allGone := 1
for _, pid := range( hangingPids ) {
proc, _ := si.Process( pid )
if proc != nil {
_, infoErr := proc.Info()
if infoErr != nil {
continue
}
allGone = 0
}
}
if allGone == 1 && i > 5 {
break
}
}
// Write out error messages for processes that could not be killed
for _, pid := range( hangingPids ) {
proc, _ := si.Process( pid )
if proc != nil {
info, infoErr := proc.Info()
arg0 := "unknown"
if infoErr != nil {
continue
}
args := info.Args
arg0 = args[0]
plog.WithFields( log.Fields{
"arg0": arg0,
} ).Error("Kill attempted and failed")
}
}
}
}