-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsv.go
132 lines (123 loc) · 3.52 KB
/
sv.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
// svctl
// Copyright (C) 2015 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import "time"
// svPid Parses process PID from sv status string.
// Returns `0` if process is not running.
func svPid(status []byte) uint {
pid := uint(status[15])
pid <<= 8
pid += uint(status[14])
pid <<= 8
pid += uint(status[13])
pid <<= 8
pid += uint(status[12])
return pid
}
// svStatus Parses process state from sv status string.
func svStatus(status []byte, pid uint) string {
if pid != 0 && status[16] != 0 {
return "PAUSED"
}
switch status[19] {
case 0:
return "STOPPED"
case 1:
return "RUNNING"
case 2:
return "FINISHING"
default:
return "UNKNOWN"
}
}
// svCheck Checks whether process already entered desired state
// after sending it the control action.
func svCheck(action, status []byte, start uint64) bool {
for _, a := range action {
pid := svPid(status)
switch a {
case 'x':
//TODO
case 'u':
if pid == 0 || status[19] != 1 {
return false
}
//TODO: !checkscript():return false
case 'd':
if pid != 0 || status[19] != 0 {
return false
}
case 't', 'k', 'h', 'a', '1', '2':
if pid == 0 && status[17] == 'd' {
break
}
time := svTime(status)
if start > time || pid == 0 || status[18] != 0 { //TODO: ||!checkscript()
return false
}
case 'o':
time := svTime(status)
if (pid == 0 && start > time) || (pid != 0 && status[17] != 'd') {
return false
}
case 'p':
if pid != 0 && status[16] == 0 {
return false
}
case 'c':
if pid != 0 && status[16] != 0 {
return false
}
}
}
return true
}
// svCheckControl Checks whether we should send a control action.
// We should not when process recently got ONCE or TERM.
func svCheckControl(action, status []byte) bool {
return status[17] != action[0] || (action[0] == 'd' && status[18] != 1)
}
// svTimeMod Is a time shift constant used by sv (copied from sv sources).
const svTimeMod = 4611686018427387914
// svTime Parses time from sv status string.
// It is a diff from the last start/stop.
func svTime(status []byte) uint64 {
time := uint64(status[0])
time <<= 8
time += uint64(status[1])
time <<= 8
time += uint64(status[2])
time <<= 8
time += uint64(status[3])
time <<= 8
time += uint64(status[4])
time <<= 8
time += uint64(status[5])
time <<= 8
time += uint64(status[6])
time <<= 8
time += uint64(status[7])
return time
}
// svNow Returns current time shifted with sv constant.
func svNow() uint64 {
return uint64(svTimeMod + time.Now().Unix())
}