Skip to content

Commit 187053b

Browse files
montag451vishvananda
authored andcommitted
Use SyscallConn() instead of Fd() to persist TUN/TAP interface
os.File.Fd() puts back the file descriptor in blocking mode which is pretty annoying as SetDeadline will stop working
1 parent d5e9ae8 commit 187053b

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

link_linux.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,9 +1260,26 @@ func (h *Handle) linkModify(link Link, flags int) error {
12601260

12611261
}
12621262

1263+
control := func(file *os.File, f func(fd uintptr)) error {
1264+
name := file.Name()
1265+
conn, err := file.SyscallConn()
1266+
if err != nil {
1267+
return fmt.Errorf("SyscallConn() failed on %s: %v", name, err)
1268+
}
1269+
if err := conn.Control(f); err != nil {
1270+
return fmt.Errorf("Failed to get file descriptor for %s: %v", name, err)
1271+
}
1272+
return nil
1273+
}
1274+
12631275
// only persist interface if NonPersist is NOT set
12641276
if !tuntap.NonPersist {
1265-
_, _, errno := unix.Syscall(unix.SYS_IOCTL, fds[0].Fd(), uintptr(unix.TUNSETPERSIST), 1)
1277+
var errno syscall.Errno
1278+
if err := control(fds[0], func(fd uintptr) {
1279+
_, _, errno = unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TUNSETPERSIST), 1)
1280+
}); err != nil {
1281+
return err
1282+
}
12661283
if errno != 0 {
12671284
cleanupFds(fds)
12681285
return fmt.Errorf("Tuntap IOCTL TUNSETPERSIST failed, errno %v", errno)
@@ -1279,7 +1296,10 @@ func (h *Handle) linkModify(link Link, flags int) error {
12791296
// un-persist (e.g. allow the interface to be removed) the tuntap
12801297
// should not hurt if not set prior, condition might be not needed
12811298
if !tuntap.NonPersist {
1282-
_, _, _ = unix.Syscall(unix.SYS_IOCTL, fds[0].Fd(), uintptr(unix.TUNSETPERSIST), 0)
1299+
// ignore error
1300+
_ = control(fds[0], func(fd uintptr) {
1301+
_, _, _ = unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TUNSETPERSIST), 0)
1302+
})
12831303
}
12841304
cleanupFds(fds)
12851305
return err

0 commit comments

Comments
 (0)