2012-11-21 08:03:38 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// Network file descriptor.
|
2012-11-21 08:03:38 +01:00
|
|
|
type netFD struct {
|
2014-06-07 00:37:27 +02:00
|
|
|
// locking/lifetime of sysfd + serialize access to Read and Write methods
|
|
|
|
fdmu fdMutex
|
|
|
|
|
|
|
|
// immutable until Close
|
2015-10-31 01:59:47 +01:00
|
|
|
net string
|
2014-06-07 00:37:27 +02:00
|
|
|
n string
|
|
|
|
dir string
|
|
|
|
ctl, data *os.File
|
|
|
|
laddr, raddr Addr
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
var (
|
|
|
|
netdir string // default network
|
|
|
|
)
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
func sysInit() {
|
2014-06-07 00:37:27 +02:00
|
|
|
netdir = "/net"
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func dial(net string, ra Addr, dialer func(time.Time) (Conn, error), deadline time.Time) (Conn, error) {
|
2013-01-29 21:52:43 +01:00
|
|
|
// On plan9, use the relatively inefficient
|
|
|
|
// goroutine-racing implementation.
|
2013-11-06 20:49:01 +01:00
|
|
|
return dialChannel(net, ra, dialer, deadline)
|
2013-01-29 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func newFD(net, name string, ctl, data *os.File, laddr, raddr Addr) (*netFD, error) {
|
|
|
|
return &netFD{net: net, n: name, dir: netdir + "/" + net + "/" + name, ctl: ctl, data: data, laddr: laddr, raddr: raddr}, nil
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) init() error {
|
|
|
|
// stub for future fd.pd.Init(fd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) name() string {
|
|
|
|
var ls, rs string
|
|
|
|
if fd.laddr != nil {
|
|
|
|
ls = fd.laddr.String()
|
|
|
|
}
|
|
|
|
if fd.raddr != nil {
|
|
|
|
rs = fd.raddr.String()
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return fd.net + ":" + ls + "->" + rs
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) ok() bool { return fd != nil && fd.ctl != nil }
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
func (fd *netFD) destroy() {
|
|
|
|
if !fd.ok() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := fd.ctl.Close()
|
|
|
|
if fd.data != nil {
|
|
|
|
if err1 := fd.data.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd.ctl = nil
|
|
|
|
fd.data = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a reference to this fd.
|
|
|
|
// Returns an error if the fd cannot be used.
|
|
|
|
func (fd *netFD) incref() error {
|
|
|
|
if !fd.fdmu.Incref() {
|
|
|
|
return errClosing
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a reference to this FD and close if we've been asked to do so
|
|
|
|
// (and there are no references left).
|
|
|
|
func (fd *netFD) decref() {
|
|
|
|
if fd.fdmu.Decref() {
|
|
|
|
fd.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a reference to this fd and lock for reading.
|
|
|
|
// Returns an error if the fd cannot be used.
|
|
|
|
func (fd *netFD) readLock() error {
|
|
|
|
if !fd.fdmu.RWLock(true) {
|
|
|
|
return errClosing
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock for reading and remove a reference to this FD.
|
|
|
|
func (fd *netFD) readUnlock() {
|
|
|
|
if fd.fdmu.RWUnlock(true) {
|
|
|
|
fd.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a reference to this fd and lock for writing.
|
|
|
|
// Returns an error if the fd cannot be used.
|
|
|
|
func (fd *netFD) writeLock() error {
|
|
|
|
if !fd.fdmu.RWLock(false) {
|
|
|
|
return errClosing
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock for writing and remove a reference to this FD.
|
|
|
|
func (fd *netFD) writeUnlock() {
|
|
|
|
if fd.fdmu.RWUnlock(false) {
|
|
|
|
fd.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
func (fd *netFD) Read(b []byte) (n int, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
if !fd.ok() || fd.data == nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
if err := fd.readLock(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer fd.readUnlock()
|
2012-11-21 08:03:38 +01:00
|
|
|
n, err = fd.data.Read(b)
|
2015-10-31 01:59:47 +01:00
|
|
|
if fd.net == "udp" && err == io.EOF {
|
2012-11-21 08:03:38 +01:00
|
|
|
n = 0
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) Write(b []byte) (n int, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
if !fd.ok() || fd.data == nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
if err := fd.writeLock(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer fd.writeUnlock()
|
2012-11-21 08:03:38 +01:00
|
|
|
return fd.data.Write(b)
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
func (fd *netFD) closeRead() error {
|
2012-11-21 08:03:38 +01:00
|
|
|
if !fd.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
func (fd *netFD) closeWrite() error {
|
2012-11-21 08:03:38 +01:00
|
|
|
if !fd.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) Close() error {
|
2014-06-07 00:37:27 +02:00
|
|
|
if !fd.fdmu.IncrefAndClose() {
|
|
|
|
return errClosing
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
if !fd.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
if fd.net == "tcp" {
|
|
|
|
// The following line is required to unblock Reads.
|
|
|
|
// For some reason, WriteString returns an error:
|
|
|
|
// "write /net/tcp/39/listen: inappropriate use of fd"
|
|
|
|
// But without it, Reads on dead conns hang forever.
|
|
|
|
// See Issue 9554.
|
|
|
|
fd.ctl.WriteString("hangup")
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
err := fd.ctl.Close()
|
|
|
|
if fd.data != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
if err1 := fd.data.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
fd.ctl = nil
|
|
|
|
fd.data = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// This method is only called via Conn.
|
2012-11-21 08:03:38 +01:00
|
|
|
func (fd *netFD) dup() (*os.File, error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
if !fd.ok() || fd.data == nil {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
return fd.file(fd.data, fd.dir+"/data")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *TCPListener) dup() (*os.File, error) {
|
|
|
|
if !l.fd.ok() {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
return l.fd.file(l.fd.ctl, l.fd.dir+"/ctl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
|
|
|
|
syscall.ForkLock.RLock()
|
|
|
|
dfd, err := syscall.Dup(int(f.Fd()), -1)
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, os.NewSyscallError("dup", err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(dfd), s), nil
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setDeadline(t time.Time) error {
|
2012-11-21 08:03:38 +01:00
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setReadDeadline(t time.Time) error {
|
2012-11-21 08:03:38 +01:00
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setWriteDeadline(t time.Time) error {
|
2012-11-21 08:03:38 +01:00
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReadBuffer(fd *netFD, bytes int) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func setWriteBuffer(fd *netFD, bytes int) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|