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 pathhoster.go
332 lines (308 loc) · 12.7 KB
/
hoster.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"bufio"
"context"
"errors"
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
"sync"
"syscall"
"time"
)
var (
nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
)
func spawnRunner(inst *instance) {
if inst == nil {
log.Println("Runner started with nil instance!")
return
}
err := createPipes(inst)
if err != nil {
return
}
args := []string{
inst.BinPath,
"--configdir=" + inst.ConfDir,
"--portmapping=0",
"--nosound",
"--autohost=preset.json",
"--headless",
"--gameport=" + fmt.Sprint(inst.Settings.GamePort),
"--enablelobbyslashcmd",
"--startplayers=" + fmt.Sprint(tryCfgGetD(tryGetIntGen("startPlayers"), inst.Settings.PlayerCount, inst.cfgs...)),
"--gamelog-output=log,cmdinterface",
"--gamelog-outputkey=playerposition",
"--gamelog-frameinterval=1",
"--gametimelimit=" + fmt.Sprint(inst.Settings.TimeLimit),
"--host-chat-config=quickchat",
"--async-join-approve",
"--enablecmdinterface=stdin",
"--host-chat-config=quickchat",
}
inst.logger.Printf("Starting %q with args %#+v", inst.BinPath, args)
pr, err := os.StartProcess(inst.BinPath, args, &os.ProcAttr{
Dir: inst.ConfDir,
Files: []*os.File{
inst.stdin,
inst.stdout,
inst.stderr,
},
Sys: &syscall.SysProcAttr{
Setsid: true, // without it ctrl+c will be sent to wz
Noctty: false, // if enabled it will fail with fork/exec : inappropriate ioctl for device
},
})
if err != nil {
inst.logger.Printf("Failed to start: %s", err.Error())
return
}
inst.Pid = pr.Pid
err = os.WriteFile(path.Join(inst.ConfDir, "cmdline"), append([]byte(strings.Join(args, "\x00")), 0), 0644)
if err != nil {
inst.logger.Println("Error writing cmdline file:", err)
}
err = os.WriteFile(path.Join(inst.ConfDir, "pid"), []byte(fmt.Sprint(inst.Pid)), 0644)
if err != nil {
inst.logger.Println("Error writing pid file:", err)
}
pr.Release()
inst.logger.Printf("Started with pid %d", inst.Pid)
inst.logger.Println("Reopening pipes...")
err = inst.stdin.Close()
if err != nil {
inst.logger.Println("Error closing stdin pipe:", err)
}
err = inst.stdout.Close()
if err != nil {
inst.logger.Println("Error closing stdout pipe:", err)
}
err = inst.stderr.Close()
if err != nil {
inst.logger.Println("Error closing stderr pipe:", err)
}
err = openPipes(inst)
if err != nil {
inst.logger.Println("Error reopening pipes:", err)
}
instanceRunner(inst)
}
func instanceRunner(inst *instance) {
defer func() {
inst.logger.Printf("atomic state store: %d", int64(instanceStateExited))
inst.state.Store(int64(instanceStateExited))
}()
inst.wg.Add(1)
defer inst.wg.Done()
err := recoverSave(inst)
if err != nil {
inst.logger.Printf("Failed to save instance recovery json: %s", err.Error())
}
var wg sync.WaitGroup
exitchan := make(chan struct{})
pidcheckchan := make(chan struct{})
msgchan := make(chan string, 64)
wg.Add(1)
go func() {
defer wg.Done()
for {
time.Sleep(1 * time.Second)
// inst.logger.Printf("Checking pid %d", inst.Pid)
// if inst.recovered {
if !isPidAlive(inst.Pid) {
inst.logger.Printf("pid %d closed", inst.Pid)
close(pidcheckchan)
return
}
// } else {
// No way of unblocking Wait call without process exit it seems
// pr, err := os.FindProcess(int(inst.Pid))
// if err != nil {
// inst.logger.Printf("Failed to find process: %s\n", err.Error())
// continue
// }
// st, err := pr.Wait()
// if err != nil {
// inst.logger.Printf("Failed to wait process: %s\n", err.Error())
// continue
// }
// if st.Exited() {
// inst.logger.Printf("pid %d closed", inst.Pid)
// close(pidcheckchan)
// return
// }
// }
// inst.logger.Printf("pid %d alive", inst.Pid)
select {
case <-exitchan:
inst.logger.Printf("pid checker for %d exited", inst.Pid)
return
default:
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
bufSize := 1024 * 1024 * 64
buf := make([]byte, bufSize)
s := bufio.NewScanner(inst.stderr)
s.Buffer(buf, bufSize)
for s.Scan() {
msgchan <- s.Text()
}
if !errors.Is(s.Err(), os.ErrClosed) {
inst.logger.Printf("stderr scanner exited with error %s", s.Err().Error())
}
}()
wg.Add(1)
go func() {
defer wg.Done()
bufSize := 1024 * 1024 * 64
buf := make([]byte, bufSize)
s := bufio.NewScanner(inst.stdout)
s.Buffer(buf, bufSize)
for s.Scan() {
msgchan <- s.Text()
}
if !errors.Is(s.Err(), os.ErrClosed) {
inst.logger.Printf("stdout scanner exited with error %s", s.Err().Error())
}
}()
pidCheckFailed := false
shutdownOrdered := false
msgloop:
for {
select {
case <-pidcheckchan:
inst.logger.Println("Pid check failed, closing off instance runtime")
pidCheckFailed = true
break msgloop
case cmd := <-inst.commands:
switch cmd.command {
case icNone:
case icBroadcast:
s, ok := cmd.data.(string)
if !ok {
inst.logger.Printf("wrong icBroadcast data type! (%t)", cmd.data)
continue
}
instWriteFmt(inst, "chat bcast "+nonAlphanumericRegex.ReplaceAllString(s, ""))
case icShutdown:
inst.logger.Println("exit sent")
instWriteFmt(inst, "shutdown now")
shutdownOrdered = true
inst.logger.Printf("atomic state store: %d", int64(instanceStateExiting))
inst.state.Store(int64(instanceStateExiting))
err := recoverSave(inst)
if err != nil {
inst.logger.Printf("Failed to save instance recovery json: %s", err.Error())
}
case icRunnerStop:
inst.logger.Println("runner stopping")
inst.logger.Printf("atomic state store: %d", int64(instanceStateExiting))
inst.state.Store(int64(instanceStateExiting))
break msgloop
default:
inst.logger.Printf("unhandled command %#+v", cmd)
}
case msg := <-msgchan:
if processHosterMessage(inst, msg) {
inst.logger.Printf(": %q", msg)
}
}
}
inst.logger.Println("Runner cleaning up runtime...")
close(exitchan)
closePipes(inst)
if !inst.recovered {
var waitStatus syscall.WaitStatus
var rusage syscall.Rusage
wret, err := syscall.Wait4(inst.Pid, &waitStatus, syscall.WNOHANG, &rusage)
if err != nil {
inst.logger.Println("Failed to wait4:", err)
}
if wret != 0 && wret != inst.Pid {
inst.logger.Printf("wait4 returned wrong pid_t, got %d but called for pid %d!", wret, inst.Pid)
}
}
inst.logger.Println("Waiting for subroutines...")
wg.Wait()
if !pidCheckFailed && !shutdownOrdered {
inst.logger.Println("Runner exits without archival")
inst.logger.Printf("atomic state store: %d", int64(instanceStateExited))
inst.state.Store(int64(instanceStateExited))
return
}
if inst.GameId > 0 {
inst.logger.Println("Runner stores replay")
sendReplayToStorage(inst)
}
inst.logger.Println("Runner archives itself")
err = archiveInstance(inst.ConfDir)
if err != nil {
inst.logger.Printf("Runner failed to archive itself: %s", err.Error())
}
inst.logger.Println("Runner exits")
inst.logger.Printf("atomic state store: %d", int64(instanceStateExited))
inst.state.Store(int64(instanceStateExited))
}
func DbLogAction(f string, args ...any) (string, error) {
ecode := "A-" + genRandomString(14)
msg := ecode + " " + fmt.Sprintf(f, args...)
err := addEventLog(msg)
return ecode, err
}
func addEventLog(msg string) error {
tag, err := dbpool.Exec(context.Background(), `insert into eventlog (msg) values ($1)`, msg)
if err != nil {
return err
}
if !tag.Insert() {
return errors.New("not insert return tag")
}
if tag.RowsAffected() != 1 {
return errors.New("rows affected != 1")
}
return nil
}
func instWriteFmt(inst *instance, format string, args ...any) {
str := fmt.Sprintf(format, args...) + "\n"
n, err := inst.stdin.WriteString(str)
if err != nil {
inst.logger.Printf("Failed to write string %q to the stdin: %s", str, err.Error())
}
if n != len(str) {
inst.logger.Printf("Write to stdin n %d does not match %d", n, len(str))
}
}
// reason := "" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█▓░░░░░░░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓████░░░░░░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████▒░░░░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓████████████▓░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓█████████████████░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓█████████████████████░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒█████████████████████░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████▓░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████░█░█░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████▒█░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░█████░░░░░░░░░░░████████████████░░█░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░███████▒░░░░░░░░░░████████████████░░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░▒████████░░░░░░░░░█████████████████░░░░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░██████████████████████████████████████░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░▓██████████████████████████████████▒░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████░░░░░░░░░\\n" +
// "░░░░░░░░░░░░░░░░░░░░░░░░███████████████████████████████▒░░░░░░░\\n"