2011-08-17 11:28:29 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-12-21 03:44:47 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// Unix domain sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2012-01-18 17:24:06 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err error) {
|
2012-01-19 15:31:13 -07:00
|
|
|
var sotype int
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
case "unix":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_STREAM
|
2011-08-17 11:28:29 -06:00
|
|
|
case "unixgram":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_DGRAM
|
2011-08-17 11:28:29 -06:00
|
|
|
case "unixpacket":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_SEQPACKET
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var la, ra syscall.Sockaddr
|
|
|
|
switch mode {
|
|
|
|
default:
|
|
|
|
panic("unixSocket mode " + mode)
|
|
|
|
|
|
|
|
case "dial":
|
|
|
|
if laddr != nil {
|
|
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
|
|
|
}
|
|
|
|
if raddr != nil {
|
|
|
|
ra = &syscall.SockaddrUnix{Name: raddr.Name}
|
2012-01-19 15:31:13 -07:00
|
|
|
} else if sotype != syscall.SOCK_DGRAM || laddr == nil {
|
2011-11-01 20:05:34 -06:00
|
|
|
return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case "listen":
|
|
|
|
if laddr == nil {
|
|
|
|
return nil, &OpError{mode, net, nil, errMissingAddress}
|
|
|
|
}
|
|
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
|
|
|
if raddr != nil {
|
2011-11-01 20:05:34 -06:00
|
|
|
return nil, &OpError{Op: mode, Net: net, Addr: raddr, Err: &AddrError{Err: "unexpected remote address", Addr: raddr.String()}}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f := sockaddrToUnix
|
2012-01-19 15:31:13 -07:00
|
|
|
if sotype == syscall.SOCK_DGRAM {
|
2011-08-17 11:28:29 -06:00
|
|
|
f = sockaddrToUnixgram
|
2012-01-19 15:31:13 -07:00
|
|
|
} else if sotype == syscall.SOCK_SEQPACKET {
|
2011-08-17 11:28:29 -06:00
|
|
|
f = sockaddrToUnixpacket
|
|
|
|
}
|
|
|
|
|
2012-03-05 08:13:10 -07:00
|
|
|
fd, err = socket(net, syscall.AF_UNIX, sotype, 0, false, la, ra, f)
|
2012-02-22 03:08:19 -07:00
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
|
|
|
|
Error:
|
|
|
|
addr := raddr
|
|
|
|
if mode == "listen" {
|
|
|
|
addr = laddr
|
|
|
|
}
|
2012-02-22 03:08:19 -07:00
|
|
|
return nil, &OpError{Op: mode, Net: net, Addr: addr, Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnix(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
|
|
|
return &UnixAddr{s.Name, "unix"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
|
|
|
return &UnixAddr{s.Name, "unixgram"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
|
|
|
return &UnixAddr{s.Name, "unixpacket"}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-19 15:31:13 -07:00
|
|
|
func sotypeToNet(sotype int) string {
|
|
|
|
switch sotype {
|
2011-08-17 11:28:29 -06:00
|
|
|
case syscall.SOCK_STREAM:
|
|
|
|
return "unix"
|
|
|
|
case syscall.SOCK_SEQPACKET:
|
|
|
|
return "unixpacket"
|
|
|
|
case syscall.SOCK_DGRAM:
|
|
|
|
return "unixgram"
|
|
|
|
default:
|
2012-01-19 15:31:13 -07:00
|
|
|
panic("sotypeToNet unknown socket type")
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnixConn is an implementation of the Conn interface
|
|
|
|
// for connections to Unix domain sockets.
|
|
|
|
type UnixConn struct {
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
conn
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{conn{fd}} }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
// ReadFromUnix reads a packet from c, copying the payload into b.
|
2012-03-08 06:36:40 -07:00
|
|
|
// It returns the number of bytes copied into b and the source address
|
|
|
|
// of the packet.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
|
|
|
// ReadFromUnix can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
2012-01-18 17:24:06 -07:00
|
|
|
// see SetDeadline and SetReadDeadline.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrUnix:
|
2012-01-19 15:31:13 -07:00
|
|
|
addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-29 03:11:05 -07:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
n, uaddr, err := c.ReadFromUnix(b)
|
|
|
|
return n, uaddr.toAddr(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUnix writes a packet to addr via c, copying the payload from b.
|
|
|
|
//
|
|
|
|
// WriteToUnix can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
2012-01-18 17:24:06 -07:00
|
|
|
// see SetDeadline and SetWriteDeadline.
|
2011-08-17 11:28:29 -06:00
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-19 15:31:13 -07:00
|
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EAFNOSUPPORT
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
|
|
|
return c.fd.WriteTo(b, sa)
|
|
|
|
}
|
|
|
|
|
2012-01-29 03:11:05 -07:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
a, ok := addr.(*UnixAddr)
|
|
|
|
if !ok {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return c.WriteToUnix(b, a)
|
|
|
|
}
|
|
|
|
|
2012-03-08 06:36:40 -07:00
|
|
|
// ReadMsgUnix reads a packet from c, copying the payload into b
|
|
|
|
// and the associated out-of-band data into oob.
|
|
|
|
// It returns the number of bytes copied into b, the number of
|
|
|
|
// bytes copied into oob, the flags that were set on the packet,
|
|
|
|
// and the source address of the packet.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, 0, 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrUnix:
|
2012-01-19 15:31:13 -07:00
|
|
|
addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-03-08 06:36:40 -07:00
|
|
|
// WriteMsgUnix writes a packet to addr via c, copying the payload from b
|
|
|
|
// and the associated out-of-band data from oob. It returns the number
|
|
|
|
// of payload and out-of-band bytes written.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if addr != nil {
|
2012-01-19 15:31:13 -07:00
|
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, 0, syscall.EAFNOSUPPORT
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
|
|
|
return c.fd.WriteMsg(b, oob, sa)
|
|
|
|
}
|
|
|
|
return c.fd.WriteMsg(b, oob, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialUnix connects to the remote address raddr on the network net,
|
|
|
|
// which must be "unix" or "unixgram". If laddr is not nil, it is used
|
|
|
|
// as the local address for the connection.
|
2012-01-31 08:36:45 -07:00
|
|
|
func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
|
|
|
|
fd, err := unixSocket(net, laddr, raddr, "dial")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newUnixConn(fd), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnixListener is a Unix domain socket listener.
|
|
|
|
// Clients should typically use variables of type Listener
|
|
|
|
// instead of assuming Unix domain sockets.
|
|
|
|
type UnixListener struct {
|
|
|
|
fd *netFD
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUnix announces on the Unix domain socket laddr and returns a Unix listener.
|
|
|
|
// Net must be "unix" (stream sockets).
|
2012-01-13 21:42:18 -07:00
|
|
|
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if net != "unix" && net != "unixgram" && net != "unixpacket" {
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr != nil {
|
|
|
|
laddr = &UnixAddr{laddr.Name, net} // make our own copy
|
|
|
|
}
|
|
|
|
fd, err := unixSocket(net, laddr, nil, "listen")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-01-13 21:42:18 -07:00
|
|
|
err = syscall.Listen(fd.sysfd, listenerBacklog)
|
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
closesocket(fd.sysfd)
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return &UnixListener{fd, laddr.Name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptUnix accepts the next incoming call and returns the new connection
|
|
|
|
// and the remote address.
|
2012-01-31 08:36:45 -07:00
|
|
|
func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-31 08:36:45 -07:00
|
|
|
fd, err := l.fd.accept(sockaddrToUnix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-31 08:36:45 -07:00
|
|
|
c := newUnixConn(fd)
|
2011-08-17 11:28:29 -06:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Accept method in the Listener interface;
|
|
|
|
// it waits for the next call and returns a generic Conn.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *UnixListener) Accept() (c Conn, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
c1, err := l.AcceptUnix()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the Unix address.
|
|
|
|
// Already accepted connections are not closed.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *UnixListener) Close() error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The operating system doesn't clean up
|
|
|
|
// the file that announcing created, so
|
|
|
|
// we have to clean it up ourselves.
|
|
|
|
// There's a race here--we can't know for
|
|
|
|
// sure whether someone else has come along
|
|
|
|
// and replaced our socket name already--
|
|
|
|
// but this sequence (remove then close)
|
|
|
|
// is at least compatible with the auto-remove
|
|
|
|
// sequence in ListenUnix. It's only non-Go
|
|
|
|
// programs that can mess us up.
|
|
|
|
if l.path[0] != '@' {
|
|
|
|
syscall.Unlink(l.path)
|
|
|
|
}
|
|
|
|
err := l.fd.Close()
|
|
|
|
l.fd = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
|
|
|
func (l *UnixListener) Addr() Addr { return l.fd.laddr }
|
|
|
|
|
2012-01-18 20:25:37 -07:00
|
|
|
// SetDeadline sets the deadline associated with the listener.
|
2012-01-18 17:24:06 -07:00
|
|
|
// A zero time value disables the deadline.
|
|
|
|
func (l *UnixListener) SetDeadline(t time.Time) (err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-18 17:24:06 -07:00
|
|
|
return setDeadline(l.fd, t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
|
|
|
// It is the caller's responsibility to close f when finished.
|
2012-04-23 17:27:19 -06:00
|
|
|
// Closing l does not affect f, and closing f does not affect l.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
// ListenUnixgram listens for incoming Unix datagram packets addressed to the
|
|
|
|
// local address laddr. The returned connection c's ReadFrom
|
|
|
|
// and WriteTo methods can be used to receive and send UDP
|
|
|
|
// packets with per-packet addressing. The network net must be "unixgram".
|
2012-01-31 08:36:45 -07:00
|
|
|
func ListenUnixgram(net string, laddr *UnixAddr) (*UDPConn, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "unixgram":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{"listen", net, nil, errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-31 08:36:45 -07:00
|
|
|
fd, err := unixSocket(net, laddr, nil, "listen")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
}
|