forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonblock.go
219 lines (189 loc) · 5.33 KB
/
nonblock.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// +build darwin linux
/*
* Copyright (c) 2017, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package tun
import (
"io"
"sync"
"sync/atomic"
"syscall"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/creack/goselect"
)
// NonblockingIO provides interruptible I/O for non-pollable
// and/or foreign file descriptors that can't use the netpoller
// available in os.OpenFile as of Go 1.9.
//
// A NonblockingIO wraps a file descriptor in an
// io.ReadWriteCloser interface. The underlying implementation
// uses select and a pipe to interrupt Read and Write calls that
// are blocked when Close is called.
//
// Read and write mutexes allow, for each operation, only one
// concurrent goroutine to call syscalls, preventing an unbounded
// number of OS threads from being created by blocked select
// syscalls.
type NonblockingIO struct {
closed int32
ioFD int
controlFDs [2]int
readMutex sync.Mutex
readFDSet *goselect.FDSet
writeMutex sync.Mutex
writeFDSets []*goselect.FDSet
}
// NewNonblockingIO creates a new NonblockingIO with the specified
// file descriptor, which is duplicated and set to nonblocking and
// close-on-exec.
func NewNonblockingIO(ioFD int) (*NonblockingIO, error) {
syscall.ForkLock.RLock()
defer syscall.ForkLock.RUnlock()
newFD, err := syscall.Dup(ioFD)
if err != nil {
return nil, errors.Trace(err)
}
init := func(fd int) error {
syscall.CloseOnExec(fd)
return syscall.SetNonblock(fd, true)
}
err = init(newFD)
if err != nil {
return nil, errors.Trace(err)
}
var controlFDs [2]int
err = syscall.Pipe(controlFDs[:])
if err != nil {
return nil, errors.Trace(err)
}
for _, fd := range controlFDs {
err = init(fd)
if err != nil {
return nil, errors.Trace(err)
}
}
return &NonblockingIO{
ioFD: newFD,
controlFDs: controlFDs,
readFDSet: new(goselect.FDSet),
writeFDSets: []*goselect.FDSet{
new(goselect.FDSet), new(goselect.FDSet)},
}, nil
}
// Read implements the io.Reader interface.
func (nio *NonblockingIO) Read(p []byte) (int, error) {
nio.readMutex.Lock()
defer nio.readMutex.Unlock()
if atomic.LoadInt32(&nio.closed) != 0 {
return 0, io.EOF
}
for {
nio.readFDSet.Zero()
nio.readFDSet.Set(uintptr(nio.controlFDs[0]))
nio.readFDSet.Set(uintptr(nio.ioFD))
max := nio.ioFD
if nio.controlFDs[0] > max {
max = nio.controlFDs[0]
}
err := goselect.Select(max+1, nio.readFDSet, nil, nil, -1)
if err == syscall.EINTR {
continue
} else if err != nil {
return 0, errors.Trace(err)
}
if nio.readFDSet.IsSet(uintptr(nio.controlFDs[0])) {
return 0, io.EOF
}
n, err := syscall.Read(nio.ioFD, p)
if err != nil && err != io.EOF {
return n, errors.Trace(err)
}
if n == 0 && err == nil {
// https://godoc.org/io#Reader:
// "Implementations of Read are discouraged from
// returning a zero byte count with a nil error".
continue
}
return n, err
}
}
// Write implements the io.Writer interface.
func (nio *NonblockingIO) Write(p []byte) (int, error) {
nio.writeMutex.Lock()
defer nio.writeMutex.Unlock()
if atomic.LoadInt32(&nio.closed) != 0 {
return 0, errors.TraceNew("file already closed")
}
n := 0
t := len(p)
for n < t {
nio.writeFDSets[0].Zero()
nio.writeFDSets[0].Set(uintptr(nio.controlFDs[0]))
nio.writeFDSets[1].Zero()
nio.writeFDSets[1].Set(uintptr(nio.ioFD))
max := nio.ioFD
if nio.controlFDs[0] > max {
max = nio.controlFDs[0]
}
err := goselect.Select(max+1, nio.writeFDSets[0], nio.writeFDSets[1], nil, -1)
if err == syscall.EINTR {
continue
} else if err != nil {
return 0, errors.Trace(err)
}
if nio.writeFDSets[0].IsSet(uintptr(nio.controlFDs[0])) {
return 0, errors.TraceNew("file has closed")
}
m, err := syscall.Write(nio.ioFD, p)
n += m
if err != nil && err != syscall.EAGAIN && err != syscall.EWOULDBLOCK {
return n, errors.Trace(err)
}
if n < t {
p = p[m:]
}
}
return n, nil
}
// IsClosed indicates whether the NonblockingIO is closed.
func (nio *NonblockingIO) IsClosed() bool {
return atomic.LoadInt32(&nio.closed) != 0
}
// Close implements the io.Closer interface.
func (nio *NonblockingIO) Close() error {
if !atomic.CompareAndSwapInt32(&nio.closed, 0, 1) {
return nil
}
// Interrupt any Reads/Writes blocked in Select.
var b [1]byte
_, err := syscall.Write(nio.controlFDs[1], b[:])
if err != nil {
return errors.Trace(err)
}
// Lock to ensure concurrent Read/Writes have
// exited and are no longer using the file
// descriptors before closing the file descriptors.
nio.readMutex.Lock()
defer nio.readMutex.Unlock()
nio.writeMutex.Lock()
defer nio.writeMutex.Unlock()
syscall.Close(nio.controlFDs[0])
syscall.Close(nio.controlFDs[1])
syscall.Close(nio.ioFD)
return nil
}