1
0
mirror of https://github.com/golang/go synced 2024-11-13 19:00:25 -07:00

syscall, net: Fix unix socket autobind on Linux.

R=rsc, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/7300047
This commit is contained in:
Albert Strasheim 2013-02-06 06:45:57 -08:00 committed by Ian Lance Taylor
parent 8c6489bc27
commit 309d88e28c
2 changed files with 39 additions and 2 deletions

View File

@ -10,6 +10,8 @@ import (
"bytes"
"io/ioutil"
"os"
"reflect"
"runtime"
"syscall"
"testing"
"time"
@ -121,3 +123,35 @@ func TestReadUnixgramWithZeroBytesBuffer(t *testing.T) {
t.Errorf("peer adddress is %v", peer)
}
}
func TestUnixAutobind(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("skipping: autobind is linux only")
}
laddr := &UnixAddr{Name: "", Net: "unixgram"}
c1, err := ListenUnixgram("unixgram", laddr)
if err != nil {
t.Fatalf("ListenUnixgram failed: %v", err)
}
defer c1.Close()
// retrieve the autobind address
autoAddr := c1.LocalAddr().(*UnixAddr)
if len(autoAddr.Name) <= 1 {
t.Fatalf("Invalid autobind address: %v", autoAddr)
}
if autoAddr.Name[0] != '@' {
t.Fatalf("Invalid autobind address: %v", autoAddr)
}
c2, err := DialUnix("unixgram", nil, autoAddr)
if err != nil {
t.Fatalf("DialUnix failed: %v", err)
}
defer c2.Close()
if !reflect.DeepEqual(c1.LocalAddr(), c2.RemoteAddr()) {
t.Fatalf("Expected autobind address %v, got %v", c1.LocalAddr(), c2.RemoteAddr())
}
}

View File

@ -279,7 +279,7 @@ type SockaddrUnix struct {
func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) {
name := sa.Name
n := len(name)
if n >= len(sa.raw.Path) || n == 0 {
if n >= len(sa.raw.Path) {
return 0, 0, EINVAL
}
sa.raw.Family = AF_UNIX
@ -287,7 +287,10 @@ func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) {
sa.raw.Path[i] = int8(name[i])
}
// length is family (uint16), name, NUL.
sl := 2 + _Socklen(n) + 1
sl := _Socklen(2)
if n > 0 {
sl += _Socklen(n) + 1
}
if sa.raw.Path[0] == '@' {
sa.raw.Path[0] = 0
// Don't count trailing NUL for abstract address.