-
Notifications
You must be signed in to change notification settings - Fork 71
/
npiperelay.go
137 lines (117 loc) · 2.98 KB
/
npiperelay.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
133
134
135
136
137
package main
import (
"flag"
"io"
"log"
"os"
"sync"
"syscall"
"time"
"golang.org/x/sys/windows"
)
const cERROR_PIPE_NOT_CONNECTED syscall.Errno = 233
var (
poll = flag.Bool("p", false, "poll until the the named pipe exists")
closeWrite = flag.Bool("s", false, "send a 0-byte message to the pipe after EOF on stdin")
closeOnEOF = flag.Bool("ep", false, "terminate on EOF reading from the pipe, even if there is more data to write")
closeOnStdinEOF = flag.Bool("ei", false, "terminate on EOF reading from stdin, even if there is more data to write")
verbose = flag.Bool("v", false, "verbose output on stderr")
)
func dialPipe(p string, poll bool) (*overlappedFile, error) {
p16, err := windows.UTF16FromString(p)
if err != nil {
return nil, err
}
for {
h, err := windows.CreateFile(&p16[0], windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, 0)
if err == nil {
return newOverlappedFile(h), nil
}
if poll && os.IsNotExist(err) {
time.Sleep(200 * time.Millisecond)
continue
}
return nil, &os.PathError{Path: p, Op: "open", Err: err}
}
}
func underlyingError(err error) error {
if serr, ok := err.(*os.SyscallError); ok {
return serr.Err
}
return err
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(1)
}
if *verbose {
log.Println("connecting to", args[0])
}
conn, err := dialPipe(args[0], *poll)
if err != nil {
log.Fatalln(err)
}
if *verbose {
log.Println("connected")
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, err := io.Copy(conn, os.Stdin)
if err != nil {
log.Fatalln("copy from stdin to pipe failed:", err)
}
if *verbose {
log.Println("copy from stdin to pipe finished")
}
if *closeOnStdinEOF {
os.Exit(0)
}
if *closeWrite {
// A zero-byte write on a message pipe indicates that no more data
// is coming.
conn.Write(nil)
}
os.Stdin.Close()
wg.Done()
}()
_, err = io.Copy(os.Stdout, conn)
if underlyingError(err) == windows.ERROR_BROKEN_PIPE || underlyingError(err) == cERROR_PIPE_NOT_CONNECTED {
// The named pipe is closed and there is no more data to read. Since
// named pipes are not bidirectional, there is no way for the other side
// of the pipe to get more data, so do not wait for the stdin copy to
// finish.
if *verbose {
log.Println("copy from pipe to stdout finished: pipe closed")
}
os.Exit(0)
}
if err != nil {
log.Fatalln("copy from pipe to stdout failed:", err)
}
if *verbose {
log.Println("copy from pipe to stdout finished")
}
if !*closeOnEOF {
os.Stdout.Close()
// Keep reading until we get ERROR_BROKEN_PIPE or the copy from stdin
// finishes.
go func() {
for {
_, err := conn.Read(nil)
if underlyingError(err) == windows.ERROR_BROKEN_PIPE {
if *verbose {
log.Println("pipe closed")
}
os.Exit(0)
} else if err != nil {
log.Fatalln("pipe error:", err)
}
}
}()
wg.Wait()
}
}