forked from mattn/goreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_windows.go
65 lines (57 loc) · 1.33 KB
/
proc_windows.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
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
// spawn command that specified as proc.
func spawnProc(proc string) bool {
logger := createLogger(proc)
cs := []string{"cmd", "/c", procs[proc].cmdline}
cmd := exec.Command(cs[0], cs[1:]...)
cmd.Stdin = nil
cmd.Stdout = logger
cmd.Stderr = logger
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT | 0x00000200,
}
cmd.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", procs[proc].port))
fmt.Fprintf(logger, "Starting %s on port %d\n", proc, procs[proc].port)
err := cmd.Start()
if err != nil {
fmt.Fprintf(logger, "Failed to start %s: %s\n", proc, err)
return true
}
procs[proc].cmd = cmd
procs[proc].quit = true
procs[proc].cmd.Wait()
procs[proc].cmd = nil
fmt.Fprintf(logger, "Terminating %s\n", proc)
return procs[proc].quit
}
func terminateProc(proc string) error {
dll, err := syscall.LoadDLL("kernel32.dll")
if err != nil {
return err
}
defer dll.Release()
pid := procs[proc].cmd.Process.Pid
f, err := dll.FindProc("SetConsoleCtrlHandler")
if err != nil {
return err
}
r1, _, err := f.Call(0, 1)
if r1 == 0 {
return err
}
f, err = dll.FindProc("GenerateConsoleCtrlEvent")
if err != nil {
return err
}
r1, _, err = f.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid))
if r1 == 0 {
return err
}
return nil
}