forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_process_nonwindows.go
40 lines (32 loc) · 959 Bytes
/
server_process_nonwindows.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
//+build !windows
package servermanager
import (
"context"
"os"
"os/exec"
"syscall"
"github.com/sirupsen/logrus"
)
const ServerExecutablePath = "acServer"
func getProcess(cmd *exec.Cmd) *os.Process {
if pgid, err := syscall.Getpgid(cmd.Process.Pid); err != nil {
logrus.WithError(err).Warnf("Failed to get process group for %d. Using pid instead.", pgid)
return cmd.Process
} else if ps, err := os.FindProcess(pgid); err != nil {
logrus.WithError(err).Warnf("Failed to find process for %d. Using pid instead.", pgid)
return cmd.Process
} else {
return ps
}
}
func terminate(ps *os.Process) error {
return syscall.Kill(-ps.Pid, syscall.SIGINT)
}
func kill(ps *os.Process) error {
return syscall.Kill(-ps.Pid, syscall.SIGKILL)
}
func buildCommand(ctx context.Context, command string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, command, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return cmd
}