Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit a7445d1

Browse files
committed
Avoid "tun not pollable": golang/go#38618
1 parent 4fc1a60 commit a7445d1

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

tunnel/intercept/tun/tunif_linux.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"golang.org/x/sys/unix"
3434
"net"
3535
"os"
36-
"time"
3736
"unsafe"
3837
)
3938

@@ -53,12 +52,7 @@ type ifreqFlags struct {
5352

5453
func open(name string, mtu uint) (*tunInterface, error) {
5554
const In6AddrGenModeNone = "1"
56-
f, err := os.OpenFile(tunCloneDevice, os.O_RDWR, 0)
57-
if err != nil {
58-
return nil, err
59-
}
60-
61-
err = f.SetDeadline(time.Time{})
55+
f, err := unix.Open(tunCloneDevice, unix.O_RDWR, 0)
6256
if err != nil {
6357
return nil, err
6458
}
@@ -75,6 +69,7 @@ func open(name string, mtu uint) (*tunInterface, error) {
7569
}
7670

7771
ifName := string(bytes.TrimRight(tunFlags.name[:], "\000"))
72+
unix.SetNonblock(f, true)
7873

7974
// prevent auto-assigned link local IPv6 address when the interface is brought up
8075
// https://www.toradex.com/community/questions/16932/ipv6-addrconfnetdev-up-eth0-link-is-not-ready.html
@@ -96,13 +91,13 @@ func open(name string, mtu uint) (*tunInterface, error) {
9691
return nil, err
9792
}
9893
sock := os.NewFile(uintptr(fd), "raw")
99-
if err = ioctl(sock, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&tunFlags))); err != nil {
94+
if err = ioctl(int(sock.Fd()), unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&tunFlags))); err != nil {
10095
return nil, err
10196
}
10297

10398
iFace, err := net.InterfaceByName(ifName)
10499
tun := &tunInterface{
105-
dev: f,
100+
dev: os.NewFile(uintptr(f), string(tunFlags.name[:])),
106101
iFace: iFace,
107102
}
108103

@@ -191,8 +186,8 @@ func closeNetlink(conn *netlink.Conn) {
191186
}
192187
}
193188

194-
func ioctl(f *os.File, request uint32, argp uintptr) error {
195-
_, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), uintptr(request), argp)
189+
func ioctl(f int, request uint32, argp uintptr) error {
190+
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(f), uintptr(request), argp)
196191
if errno != 0 {
197192
return fmt.Errorf("ioctl failed with '%s'", errno)
198193
}

0 commit comments

Comments
 (0)