github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/poll/fd_unix.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build unix || (js && wasm) || wasip1 6 7 package poll 8 9 import ( 10 "github.com/shogo82148/std/syscall" 11 ) 12 13 // FD is a file descriptor. The net and os packages use this type as a 14 // field of a larger type representing a network connection or OS file. 15 type FD struct { 16 // Lock sysfd and serialize access to Read and Write methods. 17 fdmu fdMutex 18 19 // System file descriptor. Immutable until Close. 20 Sysfd int 21 22 // Platform dependent state of the file descriptor. 23 SysFile 24 25 // I/O poller. 26 pd pollDesc 27 28 // Semaphore signaled when file is closed. 29 csema uint32 30 31 // Non-zero if this file has been set to blocking mode. 32 isBlocking uint32 33 34 // Whether this is a streaming descriptor, as opposed to a 35 // packet-based descriptor like a UDP socket. Immutable. 36 IsStream bool 37 38 // Whether a zero byte read indicates EOF. This is false for a 39 // message based socket connection. 40 ZeroReadIsEOF bool 41 42 // Whether this is a file rather than a network socket. 43 isFile bool 44 } 45 46 // Init initializes the FD. The Sysfd field should already be set. 47 // This can be called multiple times on a single FD. 48 // The net argument is a network name from the net package (e.g., "tcp"), 49 // or "file". 50 // Set pollable to true if fd should be managed by runtime netpoll. 51 func (fd *FD) Init(net string, pollable bool) error 52 53 // Close closes the FD. The underlying file descriptor is closed by the 54 // destroy method when there are no remaining references. 55 func (fd *FD) Close() error 56 57 // SetBlocking puts the file into blocking mode. 58 func (fd *FD) SetBlocking() error 59 60 // Read implements io.Reader. 61 func (fd *FD) Read(p []byte) (int, error) 62 63 // Pread wraps the pread system call. 64 func (fd *FD) Pread(p []byte, off int64) (int, error) 65 66 // ReadFrom wraps the recvfrom network call. 67 func (fd *FD) ReadFrom(p []byte) (int, syscall.Sockaddr, error) 68 69 // ReadFromInet4 wraps the recvfrom network call for IPv4. 70 func (fd *FD) ReadFromInet4(p []byte, from *syscall.SockaddrInet4) (int, error) 71 72 // ReadFromInet6 wraps the recvfrom network call for IPv6. 73 func (fd *FD) ReadFromInet6(p []byte, from *syscall.SockaddrInet6) (int, error) 74 75 // ReadMsg wraps the recvmsg network call. 76 func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.Sockaddr, error) 77 78 // ReadMsgInet4 is ReadMsg, but specialized for syscall.SockaddrInet4. 79 func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.SockaddrInet4) (int, int, int, error) 80 81 // ReadMsgInet6 is ReadMsg, but specialized for syscall.SockaddrInet6. 82 func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.SockaddrInet6) (int, int, int, error) 83 84 // Write implements io.Writer. 85 func (fd *FD) Write(p []byte) (int, error) 86 87 // Pwrite wraps the pwrite system call. 88 func (fd *FD) Pwrite(p []byte, off int64) (int, error) 89 90 // WriteToInet4 wraps the sendto network call for IPv4 addresses. 91 func (fd *FD) WriteToInet4(p []byte, sa *syscall.SockaddrInet4) (int, error) 92 93 // WriteToInet6 wraps the sendto network call for IPv6 addresses. 94 func (fd *FD) WriteToInet6(p []byte, sa *syscall.SockaddrInet6) (int, error) 95 96 // WriteTo wraps the sendto network call. 97 func (fd *FD) WriteTo(p []byte, sa syscall.Sockaddr) (int, error) 98 99 // WriteMsg wraps the sendmsg network call. 100 func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, error) 101 102 // WriteMsgInet4 is WriteMsg specialized for syscall.SockaddrInet4. 103 func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (int, int, error) 104 105 // WriteMsgInet6 is WriteMsg specialized for syscall.SockaddrInet6. 106 func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (int, int, error) 107 108 // Accept wraps the accept network call. 109 func (fd *FD) Accept() (int, syscall.Sockaddr, string, error) 110 111 // Fchmod wraps syscall.Fchmod. 112 func (fd *FD) Fchmod(mode uint32) error 113 114 // Fstat wraps syscall.Fstat 115 func (fd *FD) Fstat(s *syscall.Stat_t) error 116 117 // DupCloseOnExec dups fd and marks it close-on-exec. 118 func DupCloseOnExec(fd int) (int, string, error) 119 120 // Dup duplicates the file descriptor. 121 func (fd *FD) Dup() (int, string, error) 122 123 // WaitWrite waits until data can be read from fd. 124 func (fd *FD) WaitWrite() error 125 126 // WriteOnce is for testing only. It makes a single write call. 127 func (fd *FD) WriteOnce(p []byte) (int, error) 128 129 // RawRead invokes the user-defined function f for a read operation. 130 func (fd *FD) RawRead(f func(uintptr) bool) error 131 132 // RawWrite invokes the user-defined function f for a write operation. 133 func (fd *FD) RawWrite(f func(uintptr) bool) error