-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (53 loc) · 1.12 KB
/
main.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
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"strconv"
"syscall"
)
const usage = `
pstalk - Create an arbitrarily deep process tree
usage: pstalk <depth>
args
depth: the depth of the process tree to create
`
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s", usage)
os.Exit(1)
}
depth, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "error: <depth> must be an integer")
os.Exit(1)
}
if depth <= 0 {
os.Exit(1)
}
pid := os.Getpid()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)
exit := make(chan struct{}, 1)
go func() {
for {
s := <-sigs
fmt.Printf("[%d]: info: received signal: %v\n", pid, s)
if s == syscall.SIGINT || s == syscall.SIGTERM || s == syscall.SIGKILL {
exit <- struct{}{}
return
}
}
}()
fmt.Printf("[%d]: info: planted\n", pid)
if depth > 1 {
proc := exec.Command(os.Args[0], fmt.Sprintf("%d", depth-1))
proc.Stderr = os.Stderr
proc.Stdout = os.Stdout
if err := proc.Start(); err != nil {
fmt.Printf("[%d]: error: failed to start child process: %s\n", pid, err)
}
}
<-exit
}