-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.go
More file actions
128 lines (114 loc) · 3.5 KB
/
daemon.go
File metadata and controls
128 lines (114 loc) · 3.5 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
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
package main
import (
"bytes"
"crypto/rand"
"fmt"
logger "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"strings"
)
func startDaemon() error {
// open a listener to which the child process will connect when
// it is ready to confirm that it has successfully started
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return fmt.Errorf("opening listener for success confirmation: %v", err)
}
defer ln.Close()
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Args = append(cmd.Args, "--pingback", ln.Addr().String())
stdinpipe, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("creating stdin pipe: %v", err)
}
//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
// generate the random bytes we'll send to the child process
expect := make([]byte, 32)
_, err = rand.Read(expect)
if err != nil {
return fmt.Errorf("generating random confirmation bytes: %v", err)
}
// begin writing the confirmation bytes to the child's
// stdin; use a goroutine since the child hasn't been
// started yet, and writing synchronously would result
// in a deadlock
go func() {
stdinpipe.Write(expect)
stdinpipe.Close()
}()
// start the process
err = cmd.Start()
if err != nil {
return fmt.Errorf("starting prsdata process: %v", err)
}
// there are two ways we know we're done: either
// the process will connect to our listener, or
// it will exit with an error
success, exit := make(chan struct{}), make(chan error)
logPath := ""
// in one goroutine, we await the success of the child process
go func() {
for {
conn, err := ln.Accept()
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
logger.Errorln(fmt.Sprintf("use of closed network connection"))
}
break
}
logPath, err = handlePingbackConn(conn, expect)
if err == nil {
close(success)
break
}
logger.Errorln(fmt.Sprintf("error when handle ping back connection: %s", err))
}
}()
// in another goroutine, we await the failure of the child process
go func() {
err := cmd.Wait() // don't send on this line! Wait blocks, but send starts before it unblocks
exit <- err // sending on separate line ensures select won't trigger until after Wait unblocks
}()
// when one of the goroutines unblocks, we're done and can exit
select {
case <-success:
logger.Infoln(fmt.Sprintf("Successfully started prsdata (pid=%d) - prsdata is running in the background", cmd.Process.Pid))
logger.Infoln(fmt.Sprintf("logger will be redirected to %s", logPath))
case err := <-exit:
return fmt.Errorf("prsdata process exited with error: %v", err)
}
return nil
}
func handlePingbackConn(conn net.Conn, expect []byte) (string, error) {
defer conn.Close()
confirmationBytes, err := ioutil.ReadAll(io.LimitReader(conn, 200))
if err != nil {
return "", err
}
if !bytes.Equal(confirmationBytes[:32], expect) {
return "", fmt.Errorf("wrong confirmation: %x", confirmationBytes)
}
return string(confirmationBytes[32:]), nil
}
func startPingback(pingback string) error {
confirmationBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("reading confirmation bytes from stdin: %v", err)
}
conn, err := net.Dial("tcp", pingback)
if err != nil {
return fmt.Errorf("dialing confirmation address: %v", err)
}
defer conn.Close()
_, err = conn.Write(confirmationBytes)
if err != nil {
return fmt.Errorf("writing confirmation bytes to %s: %v", pingback, err)
}
_, err = conn.Write([]byte(config.daemonLogPath))
return nil
}