forked from cloudfoundry-attic/garden-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
93 lines (72 loc) · 1.8 KB
/
link.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
package link
import (
"fmt"
"io"
"net"
"os"
"sync"
"syscall"
)
type Link struct {
*Writer
exitStatus io.Reader
streaming *sync.WaitGroup
}
func Create(socketPath string, stdout io.Writer, stderr io.Writer) (*Link, error) {
conn, err := net.Dial("unix", socketPath)
if err != nil {
return nil, fmt.Errorf("failed to connect to i/o daemon: %s", err)
}
var b [2048]byte
var oob [2048]byte
n, oobn, _, _, err := conn.(*net.UnixConn).ReadMsgUnix(b[:], oob[:])
if err != nil {
return nil, fmt.Errorf("failed to read unix msg: %s (read: %d, %d)", err, n, oobn)
}
scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, fmt.Errorf("failed to parse socket control message: %s", err)
}
if len(scms) < 1 {
return nil, fmt.Errorf("no socket control messages sent")
}
scm := scms[0]
fds, err := syscall.ParseUnixRights(&scm)
if err != nil {
return nil, fmt.Errorf("failed to parse unix rights: %s", err)
}
if len(fds) != 3 {
return nil, fmt.Errorf("invalid number of fds; need 3, got %d", len(fds))
}
lstdout := os.NewFile(uintptr(fds[0]), "stdout")
lstderr := os.NewFile(uintptr(fds[1]), "stderr")
lstatus := os.NewFile(uintptr(fds[2]), "status")
streaming := &sync.WaitGroup{}
linkWriter := NewWriter(conn)
streaming.Add(1)
go func() {
io.Copy(stdout, lstdout)
lstdout.Close()
streaming.Done()
}()
streaming.Add(1)
go func() {
io.Copy(stderr, lstderr)
lstderr.Close()
streaming.Done()
}()
return &Link{
Writer: linkWriter,
exitStatus: lstatus,
streaming: streaming,
}, nil
}
func (link *Link) Wait() (int, error) {
link.streaming.Wait()
var exitStatus int
_, err := fmt.Fscanf(link.exitStatus, "%d\n", &exitStatus)
if err != nil {
return -1, fmt.Errorf("could not determine exit status: %s", err)
}
return exitStatus, nil
}