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.
|
|
|
|
|
2013-08-23 10:18:22 -06:00
|
|
|
// +build darwin dragonfly 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
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-12-15 19:51:47 -07:00
|
|
|
"errors"
|
2011-08-17 11:28:29 -06:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2012-01-18 17:24:06 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2013-08-02 22:32:22 -06:00
|
|
|
func unixSocket(net string, laddr, raddr sockaddr, mode string, deadline time.Time) (*netFD, error) {
|
2012-01-19 15:31:13 -07:00
|
|
|
var sotype int
|
2011-08-17 11:28:29 -06:00
|
|
|
switch 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
|
2012-12-15 19:51:47 -07:00
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "dial":
|
2013-08-02 22:32:22 -06:00
|
|
|
if laddr != nil && laddr.isWildcard() {
|
|
|
|
laddr = nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-08-02 22:32:22 -06:00
|
|
|
if raddr != nil && raddr.isWildcard() {
|
|
|
|
raddr = nil
|
|
|
|
}
|
|
|
|
if raddr == nil && (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":
|
2012-12-15 19:51:47 -07:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown mode: " + mode)
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-02 22:32:22 -06:00
|
|
|
fd, err := socket(net, syscall.AF_UNIX, sotype, 0, false, laddr, raddr, deadline, f)
|
2012-02-22 03:08:19 -07:00
|
|
|
if err != nil {
|
2012-12-15 19:51:47 -07:00
|
|
|
goto error
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
error:
|
2011-08-17 11:28:29 -06:00
|
|
|
addr := raddr
|
2012-12-15 19:51:47 -07:00
|
|
|
switch mode {
|
|
|
|
case "listen":
|
2011-08-17 11:28:29 -06:00
|
|
|
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 {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unix"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unixgram"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unixpacket"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
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_DGRAM:
|
|
|
|
return "unixgram"
|
2013-03-23 07:32:19 -06:00
|
|
|
case syscall.SOCK_SEQPACKET:
|
|
|
|
return "unixpacket"
|
2011-08-17 11:28:29 -06:00
|
|
|
default:
|
2012-01-19 15:31:13 -07:00
|
|
|
panic("sotypeToNet unknown socket type")
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 01:15:07 -06:00
|
|
|
func (a *UnixAddr) family() int {
|
|
|
|
return syscall.AF_UNIX
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) isWildcard() bool {
|
|
|
|
return a == nil || a.Name == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &syscall.SockaddrUnix{Name: a.Name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) toAddr() sockaddr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// UnixConn is an implementation of the Conn interface for connections
|
|
|
|
// to Unix domain sockets.
|
2011-08-17 11:28:29 -06:00
|
|
|
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
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// ReadFromUnix reads a packet from c, copying the payload into b. It
|
|
|
|
// returns the number of bytes copied into b and the source address of
|
|
|
|
// the packet.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2012-12-15 19:51:47 -07:00
|
|
|
// ReadFromUnix can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; 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:
|
2013-01-30 11:02:01 -07:00
|
|
|
if sa.Name != "" {
|
2013-03-23 07:32:19 -06:00
|
|
|
addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
|
2013-01-30 11:02:01 -07:00
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-29 03:11:05 -07:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
2013-01-30 11:02:01 -07:00
|
|
|
func (c *UnixConn) ReadFrom(b []byte) (int, Addr, 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
|
|
|
}
|
2013-01-30 11:02:01 -07:00
|
|
|
n, addr, err := c.ReadFromUnix(b)
|
|
|
|
return n, addr.toAddr(), err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -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.
|
|
|
|
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrUnix:
|
2013-01-30 11:02:01 -07:00
|
|
|
if sa.Name != "" {
|
2013-03-23 07:32:19 -06:00
|
|
|
addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
|
2013-01-30 11:02:01 -07:00
|
|
|
}
|
2012-12-15 19:51:47 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// WriteToUnix writes a packet to addr via c, copying the payload from b.
|
|
|
|
//
|
2012-12-15 19:51:47 -07:00
|
|
|
// WriteToUnix can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetWriteDeadline. 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
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
|
|
|
|
}
|
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-12-15 19:51:47 -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)
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:08:58 -06:00
|
|
|
// CloseRead shuts down the reading side of the Unix domain connection.
|
|
|
|
// Most callers should just use Close.
|
|
|
|
func (c *UnixConn) CloseRead() error {
|
|
|
|
if !c.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.CloseRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWrite shuts down the writing side of the Unix domain connection.
|
|
|
|
// Most callers should just use Close.
|
|
|
|
func (c *UnixConn) CloseWrite() error {
|
|
|
|
if !c.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.CloseWrite()
|
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// DialUnix connects to the remote address raddr on the network net,
|
2012-12-15 19:51:47 -07:00
|
|
|
// which must be "unix", "unixgram" or "unixpacket". 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) {
|
2012-12-15 19:51:47 -07:00
|
|
|
switch net {
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
net: remove redundant argument check from Dial on udp, unix networks
The net package consists of thin three layers like the follwoing;
- Exposed API, that contains net.Dial, net.DialUDP, net.DialUnix
- Socket and network file descriptor, that contains net.netFD and
its methods, helper functions such as dialUDP, dialUnix
- Network pollster, that contains net.pollDesc and its methods
This CL removes redundant argument check which is already done by
API layer.
R=golang-dev, dave, bradfitz
CC=golang-dev
https://golang.org/cl/13092043
2013-08-21 19:33:06 -06:00
|
|
|
return dialUnix(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialUnix(net string, laddr, raddr *UnixAddr, deadline time.Time) (*UnixConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
fd, err := unixSocket(net, laddr, raddr, "dial", deadline)
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newUnixConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// UnixListener is a Unix domain socket listener. Clients should
|
|
|
|
// typically use variables of type Listener instead of assuming Unix
|
|
|
|
// domain sockets.
|
2011-08-17 11:28:29 -06:00
|
|
|
type UnixListener struct {
|
|
|
|
fd *netFD
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// ListenUnix announces on the Unix domain socket laddr and returns a
|
2013-02-08 16:18:32 -07:00
|
|
|
// Unix listener. The network net must be "unix" or "unixpacket".
|
2012-01-13 21:42:18 -07:00
|
|
|
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
|
2012-12-15 19:51:47 -07:00
|
|
|
switch net {
|
2013-02-08 16:18:32 -07:00
|
|
|
case "unix", "unixpacket":
|
2012-12-15 19:51:47 -07:00
|
|
|
default:
|
2011-08-17 11:28:29 -06:00
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2012-12-15 19:51:47 -07:00
|
|
|
if laddr == nil {
|
|
|
|
return nil, &OpError{"listen", net, nil, errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-08 09:35:16 -07:00
|
|
|
fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &UnixListener{fd, laddr.Name}, nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// 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)
|
|
|
|
}
|
2012-11-17 21:31:26 -07:00
|
|
|
return l.fd.Close()
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2013-08-13 05:00:58 -06:00
|
|
|
return l.fd.setDeadline(t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-12-15 19:51:47 -07: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.
|
2013-04-23 17:32:11 -06:00
|
|
|
//
|
|
|
|
// The returned os.File's file descriptor is different from the
|
|
|
|
// connection's. Attempting to change properties of the original
|
|
|
|
// using this duplicate may or may not have the desired effect.
|
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
|
|
|
|
2012-12-15 19:51:47 -07:00
|
|
|
// ListenUnixgram listens for incoming Unix datagram packets addressed
|
2013-03-31 01:48:18 -06:00
|
|
|
// to the local address laddr. The network net must be "unixgram".
|
|
|
|
// The returned connection's ReadFrom and WriteTo methods can be used
|
|
|
|
// to receive and send packets with per-packet addressing.
|
2012-12-15 19:51:47 -07:00
|
|
|
func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, 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-11-08 09:35:16 -07:00
|
|
|
fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-12-15 19:51:47 -07:00
|
|
|
return newUnixConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|