Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
syscall, net: Fix unix socket autobind on Linux.
Browse files Browse the repository at this point in the history
R=rsc, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/7300047
  • Loading branch information
alberts authored and ianlancetaylor committed Feb 6, 2013
1 parent 8c6489b commit 309d88e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/pkg/net/unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"bytes"
"io/ioutil"
"os"
"reflect"
"runtime"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -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())
}
}
7 changes: 5 additions & 2 deletions src/pkg/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,18 @@ 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
for i := 0; i < n; i++ {
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.
Expand Down

0 comments on commit 309d88e

Please sign in to comment.