From dd3a3cfa4939e54490d8dd0c1c121869c7eddde3 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Sat, 22 Jun 2013 11:03:40 +1000 Subject: [PATCH] syscall: reduce duplication between *bsd and linux See discussion: https://groups.google.com/forum/#!topic/golang-dev/zSmH0lQxKAs Part 1 of several. Move identical types and functions to syscall_unix.go. R=rsc, mikioh.mikioh, r CC=golang-dev https://golang.org/cl/10392048 --- src/pkg/syscall/syscall_bsd.go | 60 -------------------------------- src/pkg/syscall/syscall_linux.go | 60 -------------------------------- src/pkg/syscall/syscall_unix.go | 60 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 120 deletions(-) diff --git a/src/pkg/syscall/syscall_bsd.go b/src/pkg/syscall/syscall_bsd.go index 7dc57acf9b..a727eb5940 100644 --- a/src/pkg/syscall/syscall_bsd.go +++ b/src/pkg/syscall/syscall_bsd.go @@ -151,20 +151,6 @@ func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) //sys Shutdown(s int, how int) (err error) -// For testing: clients can set this flag to force -// creation of IPv6 sockets to return EAFNOSUPPORT. -var SocketDisableIPv6 bool - -type Sockaddr interface { - sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs -} - -type SockaddrInet4 struct { - Port int - Addr [4]byte - raw RawSockaddrInet4 -} - func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) { if sa.Port < 0 || sa.Port > 0xFFFF { return 0, 0, EINVAL @@ -180,13 +166,6 @@ func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) { return uintptr(unsafe.Pointer(&sa.raw)), _Socklen(sa.raw.Len), nil } -type SockaddrInet6 struct { - Port int - ZoneId uint32 - Addr [16]byte - raw RawSockaddrInet6 -} - func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) { if sa.Port < 0 || sa.Port > 0xFFFF { return 0, 0, EINVAL @@ -203,11 +182,6 @@ func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) { return uintptr(unsafe.Pointer(&sa.raw)), _Socklen(sa.raw.Len), nil } -type SockaddrUnix struct { - Name string - raw RawSockaddrUnix -} - func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) { name := sa.Name n := len(name) @@ -344,42 +318,8 @@ func Getpeername(fd int) (sa Sockaddr, err error) { return anyToSockaddr(&rsa) } -func Bind(fd int, sa Sockaddr) (err error) { - ptr, n, err := sa.sockaddr() - if err != nil { - return err - } - return bind(fd, ptr, n) -} - -func Connect(fd int, sa Sockaddr) (err error) { - ptr, n, err := sa.sockaddr() - if err != nil { - return err - } - return connect(fd, ptr, n) -} - -func Socket(domain, typ, proto int) (fd int, err error) { - if domain == AF_INET6 && SocketDisableIPv6 { - return -1, EAFNOSUPPORT - } - fd, err = socket(domain, typ, proto) - return -} - //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) -func Socketpair(domain, typ, proto int) (fd [2]int, err error) { - var fdx [2]int32 - err = socketpair(domain, typ, proto, &fdx) - if err == nil { - fd[0] = int(fdx[0]) - fd[1] = int(fdx[1]) - } - return -} - func GetsockoptByte(fd, level, opt int) (value byte, err error) { var n byte vallen := _Socklen(1) diff --git a/src/pkg/syscall/syscall_linux.go b/src/pkg/syscall/syscall_linux.go index 6cd047ef1f..21e7775631 100644 --- a/src/pkg/syscall/syscall_linux.go +++ b/src/pkg/syscall/syscall_linux.go @@ -230,20 +230,6 @@ func Mkfifo(path string, mode uint32) (err error) { return Mknod(path, mode|S_IFIFO, 0) } -// For testing: clients can set this flag to force -// creation of IPv6 sockets to return EAFNOSUPPORT. -var SocketDisableIPv6 bool - -type Sockaddr interface { - sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs -} - -type SockaddrInet4 struct { - Port int - Addr [4]byte - raw RawSockaddrInet4 -} - func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) { if sa.Port < 0 || sa.Port > 0xFFFF { return 0, 0, EINVAL @@ -258,13 +244,6 @@ func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) { return uintptr(unsafe.Pointer(&sa.raw)), SizeofSockaddrInet4, nil } -type SockaddrInet6 struct { - Port int - ZoneId uint32 - Addr [16]byte - raw RawSockaddrInet6 -} - func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) { if sa.Port < 0 || sa.Port > 0xFFFF { return 0, 0, EINVAL @@ -280,11 +259,6 @@ func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) { return uintptr(unsafe.Pointer(&sa.raw)), SizeofSockaddrInet6, nil } -type SockaddrUnix struct { - Name string - raw RawSockaddrUnix -} - func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) { name := sa.Name n := len(name) @@ -472,40 +446,6 @@ func Getpeername(fd int) (sa Sockaddr, err error) { return anyToSockaddr(&rsa) } -func Bind(fd int, sa Sockaddr) (err error) { - ptr, n, err := sa.sockaddr() - if err != nil { - return err - } - return bind(fd, ptr, n) -} - -func Connect(fd int, sa Sockaddr) (err error) { - ptr, n, err := sa.sockaddr() - if err != nil { - return err - } - return connect(fd, ptr, n) -} - -func Socket(domain, typ, proto int) (fd int, err error) { - if domain == AF_INET6 && SocketDisableIPv6 { - return -1, EAFNOSUPPORT - } - fd, err = socket(domain, typ, proto) - return -} - -func Socketpair(domain, typ, proto int) (fd [2]int, err error) { - var fdx [2]int32 - err = socketpair(domain, typ, proto, &fdx) - if err == nil { - fd[0] = int(fdx[0]) - fd[1] = int(fdx[1]) - } - return -} - func GetsockoptInt(fd, level, opt int) (value int, err error) { var n int32 vallen := _Socklen(4) diff --git a/src/pkg/syscall/syscall_unix.go b/src/pkg/syscall/syscall_unix.go index f29395b2db..0432b499e7 100644 --- a/src/pkg/syscall/syscall_unix.go +++ b/src/pkg/syscall/syscall_unix.go @@ -152,6 +152,66 @@ func Write(fd int, p []byte) (n int, err error) { return } +// For testing: clients can set this flag to force +// creation of IPv6 sockets to return EAFNOSUPPORT. +var SocketDisableIPv6 bool + +type Sockaddr interface { + sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs +} + +type SockaddrInet4 struct { + Port int + Addr [4]byte + raw RawSockaddrInet4 +} + +type SockaddrInet6 struct { + Port int + ZoneId uint32 + Addr [16]byte + raw RawSockaddrInet6 +} + +type SockaddrUnix struct { + Name string + raw RawSockaddrUnix +} + +func Bind(fd int, sa Sockaddr) (err error) { + ptr, n, err := sa.sockaddr() + if err != nil { + return err + } + return bind(fd, ptr, n) +} + +func Connect(fd int, sa Sockaddr) (err error) { + ptr, n, err := sa.sockaddr() + if err != nil { + return err + } + return connect(fd, ptr, n) +} + +func Socket(domain, typ, proto int) (fd int, err error) { + if domain == AF_INET6 && SocketDisableIPv6 { + return -1, EAFNOSUPPORT + } + fd, err = socket(domain, typ, proto) + return +} + +func Socketpair(domain, typ, proto int) (fd [2]int, err error) { + var fdx [2]int32 + err = socketpair(domain, typ, proto, &fdx) + if err == nil { + fd[0] = int(fdx[0]) + fd[1] = int(fdx[1]) + } + return +} + func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { if raceenabled { raceReleaseMerge(unsafe.Pointer(&ioSync))