-
Notifications
You must be signed in to change notification settings - Fork 17
/
exec.go
49 lines (42 loc) · 885 Bytes
/
exec.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
package main
import (
"os"
"os/exec"
"os/signal"
"syscall"
)
func runCmd(name string, args ...string) (int, error) {
cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
setSysProcAttr(cmd)
err := cmd.Start()
if err != nil {
return 0, err
}
const sigcSize = 8
sigc := make(chan os.Signal, sigcSize)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGABRT,
syscall.SIGUSR1,
syscall.SIGUSR2,
syscall.SIGALRM,
syscall.SIGTERM,
)
go func() {
for sig := range sigc {
// This will duplicate some signals if they're
// sent to all processes in current group (like
// when Ctrl-C is pressed in shell).
_ = cmd.Process.Signal(sig)
}
}()
_ = cmd.Wait()
signal.Stop(sigc)
close(sigc)
return cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
}