github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/fdnotifier/poll_unsafe.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build linux 16 17 package fdnotifier 18 19 import ( 20 "unsafe" 21 22 "golang.org/x/sys/unix" 23 "github.com/SagerNet/gvisor/pkg/waiter" 24 ) 25 26 // NonBlockingPoll polls the given FD in non-blocking fashion. It is used just 27 // to query the FD's current state. 28 func NonBlockingPoll(fd int32, mask waiter.EventMask) waiter.EventMask { 29 e := struct { 30 fd int32 31 events int16 32 revents int16 33 }{ 34 fd: fd, 35 events: int16(mask.ToLinux()), 36 } 37 38 ts := unix.Timespec{ 39 Sec: 0, 40 Nsec: 0, 41 } 42 43 for { 44 n, _, err := unix.RawSyscall6(unix.SYS_PPOLL, uintptr(unsafe.Pointer(&e)), 1, 45 uintptr(unsafe.Pointer(&ts)), 0, 0, 0) 46 // Interrupted by signal, try again. 47 if err == unix.EINTR { 48 continue 49 } 50 // If an error occur we'll conservatively say the FD is ready for 51 // whatever is being checked. 52 if err != 0 { 53 return mask 54 } 55 56 // If no FDs were returned, it wasn't ready for anything. 57 if n == 0 { 58 return 0 59 } 60 61 // Otherwise we got the ready events in the revents field. 62 return waiter.EventMaskFromLinux(uint32(e.revents)) 63 } 64 } 65 66 // epollWait performs a blocking wait on epfd. 67 // 68 // Preconditions: len(events) > 0 69 func epollWait(epfd int, events []unix.EpollEvent, msec int) (int, error) { 70 if len(events) == 0 { 71 panic("Empty events passed to EpollWait") 72 } 73 74 // We actually use epoll_pwait with NULL sigmask instead of epoll_wait 75 // since that is what the Go >= 1.11 runtime prefers. 76 r, _, e := unix.Syscall6(unix.SYS_EPOLL_PWAIT, uintptr(epfd), uintptr(unsafe.Pointer(&events[0])), uintptr(len(events)), uintptr(msec), 0, 0) 77 if e != 0 { 78 return 0, e 79 } 80 return int(r), nil 81 }