This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecoverinstance.go
199 lines (191 loc) · 4.99 KB
/
recoverinstance.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path"
"strconv"
"strings"
"sync"
"github.com/maxsupermanhd/lac/v2"
)
func recoverInstances() {
instancesPath, ok := cfg.GetString("instancesPath")
if !ok {
log.Fatal("instancesPath not set")
}
drs, err := os.ReadDir(instancesPath)
if err != nil {
log.Println("Failed to open instances directory, trying to create")
err = os.MkdirAll(instancesPath, fs.FileMode(cfg.GetDInt(493, "dirPerms")))
if err != nil {
log.Fatal("Failed to create instances directory")
}
}
log.Printf("Recovering potential %d instances", len(drs))
for _, d := range drs {
if d.Name() == "." || d.Name() == ".." {
continue
}
if !d.IsDir() {
continue
}
confdir := path.Join(instancesPath, d.Name())
needsArchival := recoverRunner(confdir)
if needsArchival {
err := archiveInstance(confdir)
if err != nil {
log.Printf("Error archiving instance %q: %s", confdir, err.Error())
}
}
}
}
func recoverRunner(instpath string) bool {
log.Printf("Recovering instance %q", instpath)
instid, err := strconv.ParseInt(path.Base(instpath), 10, 64)
if err != nil {
log.Printf("Instance path %q does not have valid instance id: %s", instpath, err.Error())
return false
}
inst, err := recoverLoad(path.Join(instpath, "instance.json"))
if err != nil {
log.Printf("Instance from path %q failed to load: %s", instpath, err.Error())
return false
}
if inst.Id != instid {
log.Printf("Instance from path %q has different id (%d) than path (%d)", instpath, inst.Id, instid)
return false
}
if !isPidCmdlineAccurate(inst) {
log.Printf("Instance from path %q has invalid cmdline, assuming dead", instpath)
return true
}
if !isPidAlive(inst.Pid) {
log.Printf("Instance from path %q seems to be not alive", instpath)
return true
}
if !insertInstance(inst) {
log.Printf("Failed to insert instance with id %d", instid)
return false
}
err = openPipes(inst)
if err != nil {
log.Printf("Failed to open pipes for instance %q: %s", instpath, err)
releaseInstance(inst)
return false
}
go instanceRunner(inst)
return false
}
func isPidCmdlineAccurate(inst *instance) bool {
cmdbytes, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", inst.Pid))
if err != nil {
log.Printf("err getting proc cmdline: %s", err)
return false
}
cmdline := string(cmdbytes)
if !strings.Contains(cmdline, fmt.Sprint(inst.Id)) {
log.Printf("no id")
return false
}
if !strings.Contains(cmdline, "--configdir=") {
log.Printf("no configdir")
return false
}
if !strings.Contains(cmdline, "--async-join-approve") {
log.Printf("no async join")
return false
}
recordedCmdlineBytes, err := os.ReadFile(path.Join(inst.ConfDir, "cmdline"))
if err != nil {
log.Printf("err getting confdir cmdline: %s %s", inst.ConfDir, err)
return false
}
if cmdline != string(recordedCmdlineBytes) {
log.Printf("cmdline is not accurate: %q vs %q", cmdline, string(recordedCmdlineBytes))
}
return true
}
func isPidAlive(pid int) bool {
b, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
return false
}
var (
rpid int
rcomm string
rstate rune
)
i, err := fmt.Sscanf(string(b), "%d %s %c", &rpid, &rcomm, &rstate)
if err != nil || i != 3 {
log.Printf("Failed to parse proc stat: %s", err.Error())
return false
}
switch rstate {
case 'R': // Running
return true
case 'S': // Sleeping in an interruptible wait
return true
case 'D': // Waiting in uninterruptible disk sleep
return true
case 'W': // Waking
return true
case 'I': // Idle
return true
default:
// case 'P': // Parked
// case 'Z': // Zombie
// case 'T': // Stopped
// case 't': // Tracing stop
// case 'W': // Paging
// case 'X': // Dead
// case 'x': // Dead
// case 'K': // Wakekill
}
return false
}
func recoverSave(inst *instance) error {
if inst == nil {
return errors.New("inst is nil")
}
loadedAtomic := int(inst.state.Load())
inst.logger.Printf("recoverSave loading atomic: %d", loadedAtomic)
inst.StateSaved = loadedAtomic
b, err := json.MarshalIndent(inst, "", "\t")
if err != nil {
return err
}
return os.WriteFile(path.Join(inst.ConfDir, "instance.json"), b, fs.FileMode(cfg.GetDInt(493, "filePerms")))
}
func recoverLoad(p string) (*instance, error) {
b, err := os.ReadFile(p)
if err != nil {
return nil, err
}
inst := &instance{
commands: make(chan instanceCommand, 32),
OnJoinDispatch: map[string]joinDispatch{},
wg: sync.WaitGroup{},
}
err = json.Unmarshal(b, &inst)
if err != nil {
return nil, err
}
inst.logger = log.New(log.Writer(), fmt.Sprintf("%d ", inst.Id), log.Flags()|log.Lmsgprefix)
if inst.Settings.GamePort == 0 {
return nil, errors.New("loaded instance settings gameport is 0")
}
inst.cfgs = []lac.Conf{}
for _, v := range inst.RestoreCfgs {
c := lac.NewConf()
c.CopyTree(v)
inst.cfgs = append(inst.cfgs, c)
}
inst.logger.Printf("atomic state store: %d", int64(inst.StateSaved))
inst.state.Store(int64(inst.StateSaved))
inst.recovered = true
return inst, nil
}