-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket.go
More file actions
76 lines (63 loc) · 1.34 KB
/
Copy pathsocket.go
File metadata and controls
76 lines (63 loc) · 1.34 KB
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
package httpserver
import (
"fmt"
"net"
"time"
log "github.com/sirupsen/logrus"
)
func acceptConn(l net.Listener) (c net.Conn, err error) {
chn := make(chan error)
go func() {
defer close(chn)
c, err = l.Accept()
if err != nil {
chn <- err
}
}()
select {
case err = <-chn:
if err != nil {
log.WithError(err).Error("Error occurred when accepting socket connection")
}
case <-time.After(4 * time.Second):
err = fmt.Errorf("Timeout occurred waiting for connection from child")
log.Info(err.Error())
}
return
}
func socketListener(chn chan<- string, errChn chan<- error) {
sockLn, err := net.Listen("unix", cfg.SockFile)
if err != nil {
log.WithError(err).Error("Unable to start unix domain socket")
errChn <- err
return
}
defer sockLn.Close()
chn <- "socket_opened"
c, err := acceptConn(sockLn)
if err != nil {
errChn <- err
return
}
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
log.WithError(err).
Error("Unable to read data from socket")
errChn <- err
return
}
data := buf[0:nr]
switch string(data) {
case "get_listener":
log.Debug("Fork requested listener information")
err := sendListener(c)
if err != nil {
log.WithError(err).
Error("Unable to send http listener socket over the unix domain socket")
errChn <- err
return
}
chn <- "listener_sent"
}
}