gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/hostfd/hostfd_unsafe.go (about) 1 // Copyright 2020 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 package hostfd 16 17 import ( 18 "unsafe" 19 20 "golang.org/x/sys/unix" 21 ) 22 23 const ( 24 sizeofIovec = unsafe.Sizeof(unix.Iovec{}) 25 sizeofMsghdr = unsafe.Sizeof(unix.Msghdr{}) 26 ) 27 28 func iovecsReadWrite(sysno uintptr, fd int32, iovs []unix.Iovec, offset int64, flags uint32) (uintptr, unix.Errno) { 29 var total uintptr 30 for start := 0; start < len(iovs); start += MaxReadWriteIov { 31 last := true 32 size := len(iovs) - start 33 if size > MaxReadWriteIov { 34 last = false 35 size = MaxReadWriteIov 36 } 37 curOff := offset 38 if offset >= 0 { 39 curOff = offset + int64(total) 40 } 41 cur, _, e := unix.Syscall6(sysno, uintptr(fd), uintptr((unsafe.Pointer)(&iovs[start])), uintptr(size), uintptr(curOff), 0 /* pos_h */, uintptr(flags)) 42 if cur > 0 { 43 total += cur 44 } 45 if e != 0 { 46 return total, e 47 } 48 if last { 49 break 50 } 51 // If this was a short read/write, then break. 52 var curTotal uint64 53 for i := range iovs[start : start+size] { 54 curTotal += iovs[i].Len 55 } 56 if uint64(cur) < curTotal { 57 break 58 } 59 } 60 return total, 0 61 }