-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
132 lines (109 loc) · 2.66 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
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
package main
import (
"flag"
"log"
"net"
"os"
"os/exec"
"time"
)
const (
defaultAddr = ":1234"
)
var (
addr string
triggerRace bool
ignoreFiles bool
)
func init() {
log.SetFlags(log.Lmicroseconds)
}
func main() {
// use flags
flag.StringVar(&addr, "addr", defaultAddr, "address on which to listen")
flag.BoolVar(&triggerRace, "triggerRace", false, "trigger race condition")
flag.BoolVar(&ignoreFiles, "ignoreFiles", false, "ignore closing FDs")
flag.Parse()
// if restart, use existing listener to try close/open again
if os.Getenv("IS_CHILD") == "true" {
log.SetPrefix("child | ")
runChild()
} else {
log.SetPrefix("parent | ")
runParent()
}
}
func runParent() {
log.Printf("Using address=`%v`\n", addr)
// create fd to share
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(err)
}
lnFile, err := ln.(*net.TCPListener).File()
if err != nil {
log.Fatal(err)
}
// assemble child process
os.Setenv("IS_CHILD", "true")
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdin = os.Stdin // fd 0
cmd.Stdout = os.Stdout // fd 1
cmd.Stderr = os.Stderr // fd 2
cmd.ExtraFiles = []*os.File{lnFile}
// start child process
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// !!!RACE!!! if the child process tries to rebind before the following
// close operations finish, the port will still be bound.
if triggerRace {
time.Sleep(2 * time.Second)
}
// close our copy of the listener
if err := ln.Close(); err != nil {
log.Fatal(err)
}
log.Printf("Closed listener.\n")
if !ignoreFiles {
if err := lnFile.Close(); err != nil {
log.Fatal(err)
}
log.Printf("Closed file.\n")
}
// TODO: signal child process it's free to rebind to avoid race condition
// wait for child to exit successfully
if err := cmd.Wait(); err != nil {
log.Fatalf("Child process error: %v\n", err)
}
// Victory.
log.Println("PASS")
}
func runChild() {
log.Printf("Using address=`%v`\n", addr)
// assemble shared listener
file := os.NewFile(3, "")
ln, err := net.FileListener(file)
if err != nil {
log.Fatal("FileListener:", err)
}
// close out the original connection
if err = ln.Close(); err != nil {
log.Fatal("ln.Close:", err)
}
log.Printf("Closed listener.\n")
if !ignoreFiles {
if err := file.Close(); err != nil {
log.Fatal("file.Close:", err)
}
log.Printf("Closed file.\n")
}
// !!!RACE!!!; if the parent process has not closed shared stuff before the following Listen operation runs, we'll get an error.
// PROBLEMATIC REBIND
log.Printf("Rebinding...\n")
ln, err = net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Child could not recreate listener: %v", err)
}
log.Println("PASS")
}