net: Plan 9: open data file and set remote-addr properly
The data file should be opened when a Conn is first
established, rather than waiting for the first Read or
Write.
Upon Close, we now make sure to try to close both, the
ctl as well as data files and set both to nil, even in
the face of errors, instead of returning early.
The Accept call was not setting the remote address
of the connection properly. Now, we read the correct
file.
Make functions that establish Conn use newTCPConn
or newUDPConn.
R=rsc, rminnich, ality, dave
CC=golang-dev
https://golang.org/cl/7228068
diff --git a/src/pkg/net/fd_plan9.go b/src/pkg/net/fd_plan9.go
index 3462792..dc5e44c 100644
--- a/src/pkg/net/fd_plan9.go
+++ b/src/pkg/net/fd_plan9.go
@@ -29,22 +29,16 @@
return dialTimeoutRace(net, addr, timeout)
}
-func newFD(proto, name string, ctl *os.File, laddr, raddr Addr) *netFD {
- return &netFD{proto, name, "/net/" + proto + "/" + name, ctl, nil, laddr, raddr}
+func newFD(proto, name string, ctl, data *os.File, laddr, raddr Addr) *netFD {
+ return &netFD{proto, name, "/net/" + proto + "/" + name, ctl, data, laddr, raddr}
}
func (fd *netFD) ok() bool { return fd != nil && fd.ctl != nil }
func (fd *netFD) Read(b []byte) (n int, err error) {
- if !fd.ok() {
+ if !fd.ok() || fd.data == nil {
return 0, syscall.EINVAL
}
- if fd.data == nil {
- fd.data, err = os.OpenFile(fd.dir+"/data", os.O_RDWR, 0)
- if err != nil {
- return 0, err
- }
- }
n, err = fd.data.Read(b)
if fd.proto == "udp" && err == io.EOF {
n = 0
@@ -54,15 +48,9 @@
}
func (fd *netFD) Write(b []byte) (n int, err error) {
- if !fd.ok() {
+ if !fd.ok() || fd.data == nil {
return 0, syscall.EINVAL
}
- if fd.data == nil {
- fd.data, err = os.OpenFile(fd.dir+"/data", os.O_RDWR, 0)
- if err != nil {
- return 0, err
- }
- }
return fd.data.Write(b)
}
@@ -85,11 +73,10 @@
return syscall.EINVAL
}
err := fd.ctl.Close()
- if err != nil {
- return err
- }
if fd.data != nil {
- err = fd.data.Close()
+ if err1 := fd.data.Close(); err1 != nil && err == nil {
+ err = err1
+ }
}
fd.ctl = nil
fd.data = nil