Lines Matching refs:FD
47 func (fd *netFD) init() error {
48 if err := fd.pd.Init(fd); err != nil {
54 func (fd *netFD) setAddr(laddr, raddr Addr) {
55 fd.laddr = laddr
56 fd.raddr = raddr
57 runtime.SetFinalizer(fd, (*netFD).Close)
60 func (fd *netFD) name() string {
62 if fd.laddr != nil {
63 ls = fd.laddr.String()
65 if fd.raddr != nil {
66 rs = fd.raddr.String()
68 return fd.net + ":" + ls + "->" + rs
71 func (fd *netFD) connect(la, ra syscall.Sockaddr, deadline time.Time) error {
72 // Do not need to call fd.writeLock here,
73 // because fd is not yet accessible to user,
75 switch err := connectFunc(fd.sysfd, ra); err {
81 if err := fd.init(); err != nil {
98 if err := fd.init(); err != nil {
102 fd.setWriteDeadline(deadline)
103 defer fd.setWriteDeadline(noDeadline)
114 if err := fd.pd.WaitWrite(); err != nil {
117 nerr, err := getsockoptIntFunc(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_ERROR)
131 func (fd *netFD) destroy() {
132 // Poller may want to unregister fd in readiness notification mechanism,
134 fd.pd.Close()
135 closeFunc(fd.sysfd)
136 fd.sysfd = -1
137 runtime.SetFinalizer(fd, nil)
140 // Add a reference to this fd.
141 // Returns an error if the fd cannot be used.
142 func (fd *netFD) incref() error {
143 if !fd.fdmu.Incref() {
149 // Remove a reference to this FD and close if we've been asked to do so
151 func (fd *netFD) decref() {
152 if fd.fdmu.Decref() {
153 fd.destroy()
157 // Add a reference to this fd and lock for reading.
158 // Returns an error if the fd cannot be used.
159 func (fd *netFD) readLock() error {
160 if !fd.fdmu.RWLock(true) {
166 // Unlock for reading and remove a reference to this FD.
167 func (fd *netFD) readUnlock() {
168 if fd.fdmu.RWUnlock(true) {
169 fd.destroy()
173 // Add a reference to this fd and lock for writing.
174 // Returns an error if the fd cannot be used.
175 func (fd *netFD) writeLock() error {
176 if !fd.fdmu.RWLock(false) {
182 // Unlock for writing and remove a reference to this FD.
183 func (fd *netFD) writeUnlock() {
184 if fd.fdmu.RWUnlock(false) {
185 fd.destroy()
189 func (fd *netFD) Close() error {
190 if !fd.fdmu.IncrefAndClose() {
194 // so that it cannot be referring to fd.sysfd anymore,
195 // the final decref will close fd.sysfd. This should happen
198 fd.pd.Evict()
199 fd.decref()
203 func (fd *netFD) shutdown(how int) error {
204 if err := fd.incref(); err != nil {
207 defer fd.decref()
208 return os.NewSyscallError("shutdown", syscall.Shutdown(fd.sysfd, how))
211 func (fd *netFD) closeRead() error {
212 return fd.shutdown(syscall.SHUT_RD)
215 func (fd *netFD) closeWrite() error {
216 return fd.shutdown(syscall.SHUT_WR)
219 func (fd *netFD) Read(p []byte) (n int, err error) {
220 if err := fd.readLock(); err != nil {
223 defer fd.readUnlock()
224 if err := fd.pd.PrepareRead(); err != nil {
228 n, err = syscall.Read(fd.sysfd, p)
232 if err = fd.pd.WaitRead(); err == nil {
237 err = fd.eofError(n, err)
246 func (fd *netFD) readFrom(p []byte) (n int, sa syscall.Sockaddr, err error) {
247 if err := fd.readLock(); err != nil {
250 defer fd.readUnlock()
251 if err := fd.pd.PrepareRead(); err != nil {
255 n, sa, err = syscall.Recvfrom(fd.sysfd, p, 0)
259 if err = fd.pd.WaitRead(); err == nil {
264 err = fd.eofError(n, err)
273 func (fd *netFD) readMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.Sockaddr, err error) {
274 if err := fd.readLock(); err != nil {
277 defer fd.readUnlock()
278 if err := fd.pd.PrepareRead(); err != nil {
282 n, oobn, flags, sa, err = syscall.Recvmsg(fd.sysfd, p, oob, 0)
286 if err = fd.pd.WaitRead(); err == nil {
291 err = fd.eofError(n, err)
300 func (fd *netFD) Write(p []byte) (nn int, err error) {
301 if err := fd.writeLock(); err != nil {
304 defer fd.writeUnlock()
305 if err := fd.pd.PrepareWrite(); err != nil {
310 n, err = syscall.Write(fd.sysfd, p[nn:])
318 if err = fd.pd.WaitWrite(); err == nil {
336 func (fd *netFD) writeTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
337 if err := fd.writeLock(); err != nil {
340 defer fd.writeUnlock()
341 if err := fd.pd.PrepareWrite(); err != nil {
345 err = syscall.Sendto(fd.sysfd, p, 0, sa)
347 if err = fd.pd.WaitWrite(); err == nil {
362 func (fd *netFD) writeMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oobn int, err error) {
363 if err := fd.writeLock(); err != nil {
366 defer fd.writeUnlock()
367 if err := fd.pd.PrepareWrite(); err != nil {
371 n, err = syscall.SendmsgN(fd.sysfd, p, oob, sa, 0)
373 if err = fd.pd.WaitWrite(); err == nil {
388 func (fd *netFD) accept() (netfd *netFD, err error) {
389 if err := fd.readLock(); err != nil {
392 defer fd.readUnlock()
396 if err = fd.pd.PrepareRead(); err != nil {
400 s, rsa, err = accept(fd.sysfd)
408 if err = fd.pd.WaitRead(); err == nil {
423 if netfd, err = newFD(s, fd.family, fd.sotype, fd.net); err != nil {
428 fd.Close()
440 func dupCloseOnExec(fd int) (newfd int, err error) {
442 r0, _, e1 := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_DUPFD_CLOEXEC, 0)
448 // in this case because fd is not of the
449 // expected device fd type. Treat it as
467 return dupCloseOnExecOld(fd)
470 // dupCloseOnExecUnixOld is the traditional way to dup an fd and
472 func dupCloseOnExecOld(fd int) (newfd int, err error) {
475 newfd, err = syscall.Dup(fd)
483 func (fd *netFD) dup() (f *os.File, err error) {
484 ns, err := dupCloseOnExec(fd.sysfd)
489 // We want blocking mode for the new fd, hence the double negative.
490 // This also puts the old fd into blocking mode, meaning that
497 return os.NewFile(uintptr(ns), fd.name()), nil