Skip to content

Commit 0ce9045

Browse files
net: add CloseRead, CloseWrite methods to UnixConn.
Fixes #3345. R=golang-dev, r, rsc, dave CC=golang-dev, remy https://golang.org/cl/6214061
1 parent 53bc194 commit 0ce9045

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/pkg/net/net_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package net
66

77
import (
88
"io"
9+
"io/ioutil"
10+
"os"
911
"runtime"
1012
"testing"
1113
"time"
@@ -58,6 +60,59 @@ func TestShutdown(t *testing.T) {
5860
}
5961
}
6062

63+
func TestShutdownUnix(t *testing.T) {
64+
if runtime.GOOS == "plan9" {
65+
t.Logf("skipping test on %q", runtime.GOOS)
66+
return
67+
}
68+
f, err := ioutil.TempFile("", "go_net_unixtest")
69+
if err != nil {
70+
t.Fatalf("TempFile: %s", err)
71+
}
72+
f.Close()
73+
tmpname := f.Name()
74+
os.Remove(tmpname)
75+
ln, err := Listen("unix", tmpname)
76+
if err != nil {
77+
t.Fatalf("ListenUnix on %s: %s", tmpname, err)
78+
}
79+
defer os.Remove(tmpname)
80+
81+
go func() {
82+
c, err := ln.Accept()
83+
if err != nil {
84+
t.Fatalf("Accept: %v", err)
85+
}
86+
var buf [10]byte
87+
n, err := c.Read(buf[:])
88+
if n != 0 || err != io.EOF {
89+
t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err)
90+
}
91+
c.Write([]byte("response"))
92+
c.Close()
93+
}()
94+
95+
c, err := Dial("unix", tmpname)
96+
if err != nil {
97+
t.Fatalf("Dial: %v", err)
98+
}
99+
defer c.Close()
100+
101+
err = c.(*UnixConn).CloseWrite()
102+
if err != nil {
103+
t.Fatalf("CloseWrite: %v", err)
104+
}
105+
var buf [10]byte
106+
n, err := c.Read(buf[:])
107+
if err != nil {
108+
t.Fatalf("client Read: %d, %v", n, err)
109+
}
110+
got := string(buf[:n])
111+
if got != "response" {
112+
t.Errorf("read = %q, want \"response\"", got)
113+
}
114+
}
115+
61116
func TestTCPListenClose(t *testing.T) {
62117
ln, err := Listen("tcp", "127.0.0.1:0")
63118
if err != nil {

src/pkg/net/unixsock_plan9.go

+12
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
7272
return
7373
}
7474

75+
// CloseRead shuts down the reading side of the Unix domain connection.
76+
// Most callers should just use Close.
77+
func (c *UnixConn) CloseRead() error {
78+
return syscall.EPLAN9
79+
}
80+
81+
// CloseWrite shuts down the writing side of the Unix domain connection.
82+
// Most callers should just use Close.
83+
func (c *UnixConn) CloseWrite() error {
84+
return syscall.EPLAN9
85+
}
86+
7587
// DialUnix connects to the remote address raddr on the network net,
7688
// which must be "unix" or "unixgram". If laddr is not nil, it is used
7789
// as the local address for the connection.

src/pkg/net/unixsock_posix.go

+18
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err
207207
return c.fd.WriteMsg(b, oob, nil)
208208
}
209209

210+
// CloseRead shuts down the reading side of the Unix domain connection.
211+
// Most callers should just use Close.
212+
func (c *UnixConn) CloseRead() error {
213+
if !c.ok() {
214+
return syscall.EINVAL
215+
}
216+
return c.fd.CloseRead()
217+
}
218+
219+
// CloseWrite shuts down the writing side of the Unix domain connection.
220+
// Most callers should just use Close.
221+
func (c *UnixConn) CloseWrite() error {
222+
if !c.ok() {
223+
return syscall.EINVAL
224+
}
225+
return c.fd.CloseWrite()
226+
}
227+
210228
// DialUnix connects to the remote address raddr on the network net,
211229
// which must be "unix" or "unixgram". If laddr is not nil, it is used
212230
// as the local address for the connection.

0 commit comments

Comments
 (0)