forked from mrlauer/gofcgisrv
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialer.go
98 lines (89 loc) · 1.86 KB
/
dialer.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
package gofcgisrv
import (
"crypto/rand"
"errors"
"fmt"
"net"
"os"
"os/exec"
"path"
)
type Dialer interface {
Dial() (net.Conn, error)
}
type NetDialer struct {
net string
addr string
}
func (d NetDialer) Dial() (net.Conn, error) {
return net.Dial(d.net, d.addr)
}
// StdinDialer managers an app as a child process, creating a socket and passing it through stdin.
type StdinDialer struct {
app string
args []string
cmd *exec.Cmd
stdin *os.File
listener net.Listener
filename string
}
func (sd *StdinDialer) Dial() (net.Conn, error) {
if sd.stdin == nil {
return nil, errors.New("No file")
}
return net.Dial("unix", sd.filename)
}
func (sd *StdinDialer) Start() error {
// Create a socket.
// We'll use the high-level net API, creating a listener that does all sorts
// of socket stuff, getting its file, and passing that (really just for its FD)
// to the child process.
// We'll rely on crypt/rand to get a unique filename for the socket.
tmpdir := os.TempDir()
rnd := make([]byte, 8)
n, err := rand.Read(rnd)
if err != nil {
return err
}
basename := fmt.Sprintf("fcgi%x", rnd[:n])
filename := path.Join(tmpdir, basename)
listener, err := net.Listen("unix", filename)
if err != nil {
return err
}
socket, err := listener.(*net.UnixListener).File()
if err != nil {
listener.Close()
return err
}
cmd := exec.Command(sd.app, sd.args...)
cmd.Stdin = socket
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
socket.Close()
listener.Close()
return err
}
sd.stdin = socket
sd.listener = listener
sd.cmd = cmd
sd.filename = filename
return nil
}
func (sd *StdinDialer) Close() {
if sd.stdin != nil {
sd.stdin.Close()
sd.stdin = nil
}
if sd.listener != nil {
sd.listener.Close()
sd.listener = nil
}
if sd.cmd != nil {
sd.cmd.Process.Kill()
sd.cmd = nil
}
sd.filename = ""
}