Source file src/net/unixsock.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"context"
     9  	"os"
    10  	"sync"
    11  	"syscall"
    12  	"time"
    13  )
    14  
    15  // BUG(mikio): On JS, WASIP1 and Plan 9, methods and functions related
    16  // to UnixConn and UnixListener are not implemented.
    17  
    18  // BUG(mikio): On Windows, methods and functions related to UnixConn
    19  // and UnixListener don't work for "unixgram" and "unixpacket".
    20  
    21  // UnixAddr represents the address of a Unix domain socket end point.
    22  type UnixAddr struct {
    23  	Name string
    24  	Net  string
    25  }
    26  
    27  // Network returns the address's network name, "unix", "unixgram" or
    28  // "unixpacket".
    29  func (a *UnixAddr) Network() string {
    30  	return a.Net
    31  }
    32  
    33  func (a *UnixAddr) String() string {
    34  	if a == nil {
    35  		return "<nil>"
    36  	}
    37  	return a.Name
    38  }
    39  
    40  func (a *UnixAddr) isWildcard() bool {
    41  	return a == nil || a.Name == ""
    42  }
    43  
    44  func (a *UnixAddr) opAddr() Addr {
    45  	if a == nil {
    46  		return nil
    47  	}
    48  	return a
    49  }
    50  
    51  // ResolveUnixAddr returns an address of Unix domain socket end point.
    52  //
    53  // The network must be a Unix network name.
    54  //
    55  // See func [Dial] for a description of the network and address
    56  // parameters.
    57  func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
    58  	switch network {
    59  	case "unix", "unixgram", "unixpacket":
    60  		return &UnixAddr{Name: address, Net: network}, nil
    61  	default:
    62  		return nil, UnknownNetworkError(network)
    63  	}
    64  }
    65  
    66  // UnixConn is an implementation of the [Conn] interface for connections
    67  // to Unix domain sockets.
    68  type UnixConn struct {
    69  	conn
    70  }
    71  
    72  // SyscallConn returns a raw network connection.
    73  // This implements the [syscall.Conn] interface.
    74  func (c *UnixConn) SyscallConn() (syscall.RawConn, error) {
    75  	if !c.ok() {
    76  		return nil, syscall.EINVAL
    77  	}
    78  	return newRawConn(c.fd), nil
    79  }
    80  
    81  // CloseRead shuts down the reading side of the Unix domain connection.
    82  // Most callers should just use [UnixConn.Close].
    83  func (c *UnixConn) CloseRead() error {
    84  	if !c.ok() {
    85  		return syscall.EINVAL
    86  	}
    87  	if err := c.fd.closeRead(); err != nil {
    88  		return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    89  	}
    90  	return nil
    91  }
    92  
    93  // CloseWrite shuts down the writing side of the Unix domain connection.
    94  // Most callers should just use [UnixConn.Close].
    95  func (c *UnixConn) CloseWrite() error {
    96  	if !c.ok() {
    97  		return syscall.EINVAL
    98  	}
    99  	if err := c.fd.closeWrite(); err != nil {
   100  		return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   101  	}
   102  	return nil
   103  }
   104  
   105  // ReadFromUnix acts like [UnixConn.ReadFrom] but returns a [UnixAddr].
   106  func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error) {
   107  	if !c.ok() {
   108  		return 0, nil, syscall.EINVAL
   109  	}
   110  	n, addr, err := c.readFrom(b)
   111  	if err != nil {
   112  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   113  	}
   114  	return n, addr, err
   115  }
   116  
   117  // ReadFrom implements the [PacketConn].ReadFrom method.
   118  func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) {
   119  	if !c.ok() {
   120  		return 0, nil, syscall.EINVAL
   121  	}
   122  	n, addr, err := c.readFrom(b)
   123  	if err != nil {
   124  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   125  	}
   126  	if addr == nil {
   127  		return n, nil, err
   128  	}
   129  	return n, addr, err
   130  }
   131  
   132  // ReadMsgUnix reads a message from c, copying the payload into b and
   133  // the associated out-of-band data into oob. It returns the number of
   134  // bytes copied into b, the number of bytes copied into oob, the flags
   135  // that were set on the message and the source address of the message.
   136  //
   137  // Note that if len(b) == 0 and len(oob) > 0, this function will still
   138  // read (and discard) 1 byte from the connection.
   139  func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
   140  	if !c.ok() {
   141  		return 0, 0, 0, nil, syscall.EINVAL
   142  	}
   143  	n, oobn, flags, addr, err = c.readMsg(b, oob)
   144  	if err != nil {
   145  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   146  	}
   147  	return
   148  }
   149  
   150  // WriteToUnix acts like [UnixConn.WriteTo] but takes a [UnixAddr].
   151  func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error) {
   152  	if !c.ok() {
   153  		return 0, syscall.EINVAL
   154  	}
   155  	n, err := c.writeTo(b, addr)
   156  	if err != nil {
   157  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
   158  	}
   159  	return n, err
   160  }
   161  
   162  // WriteTo implements the [PacketConn].WriteTo method.
   163  func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error) {
   164  	if !c.ok() {
   165  		return 0, syscall.EINVAL
   166  	}
   167  	a, ok := addr.(*UnixAddr)
   168  	if !ok {
   169  		return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
   170  	}
   171  	n, err := c.writeTo(b, a)
   172  	if err != nil {
   173  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
   174  	}
   175  	return n, err
   176  }
   177  
   178  // WriteMsgUnix writes a message to addr via c, copying the payload
   179  // from b and the associated out-of-band data from oob. It returns the
   180  // number of payload and out-of-band bytes written.
   181  //
   182  // Note that if len(b) == 0 and len(oob) > 0, this function will still
   183  // write 1 byte to the connection.
   184  func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
   185  	if !c.ok() {
   186  		return 0, 0, syscall.EINVAL
   187  	}
   188  	n, oobn, err = c.writeMsg(b, oob, addr)
   189  	if err != nil {
   190  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
   191  	}
   192  	return
   193  }
   194  
   195  func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{conn{fd}} }
   196  
   197  // DialUnix acts like [Dial] for Unix networks.
   198  //
   199  // The network must be a Unix network name; see func [Dial] for details.
   200  //
   201  // If laddr is non-nil, it is used as the local address for the
   202  // connection.
   203  func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
   204  	return dialUnix(context.Background(), nil, network, laddr, raddr)
   205  }
   206  
   207  func dialUnix(ctx context.Context, dialer *Dialer, network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
   208  	switch network {
   209  	case "unix", "unixgram", "unixpacket":
   210  	default:
   211  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
   212  	}
   213  	sd := &sysDialer{network: network, address: raddr.String()}
   214  	if dialer != nil {
   215  		sd.Dialer = *dialer
   216  	}
   217  	c, err := sd.dialUnix(ctx, laddr, raddr)
   218  	if err != nil {
   219  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
   220  	}
   221  	return c, nil
   222  }
   223  
   224  // UnixListener is a Unix domain socket listener. Clients should
   225  // typically use variables of type [Listener] instead of assuming Unix
   226  // domain sockets.
   227  type UnixListener struct {
   228  	fd         *netFD
   229  	path       string
   230  	unlink     bool
   231  	unlinkOnce sync.Once
   232  }
   233  
   234  func (ln *UnixListener) ok() bool { return ln != nil && ln.fd != nil }
   235  
   236  // SyscallConn returns a raw network connection.
   237  // This implements the [syscall.Conn] interface.
   238  //
   239  // The returned [syscall.RawConn] only supports calling Control. Read and
   240  // Write return an error.
   241  func (l *UnixListener) SyscallConn() (syscall.RawConn, error) {
   242  	if !l.ok() {
   243  		return nil, syscall.EINVAL
   244  	}
   245  	return newRawListener(l.fd), nil
   246  }
   247  
   248  // AcceptUnix accepts the next incoming call and returns the new
   249  // connection.
   250  func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
   251  	if !l.ok() {
   252  		return nil, syscall.EINVAL
   253  	}
   254  	c, err := l.accept()
   255  	if err != nil {
   256  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   257  	}
   258  	return c, nil
   259  }
   260  
   261  // Accept implements the Accept method in the [Listener] interface.
   262  // Returned connections will be of type [*UnixConn].
   263  func (l *UnixListener) Accept() (Conn, error) {
   264  	if !l.ok() {
   265  		return nil, syscall.EINVAL
   266  	}
   267  	c, err := l.accept()
   268  	if err != nil {
   269  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   270  	}
   271  	return c, nil
   272  }
   273  
   274  // Close stops listening on the Unix address. Already accepted
   275  // connections are not closed.
   276  func (l *UnixListener) Close() error {
   277  	if !l.ok() {
   278  		return syscall.EINVAL
   279  	}
   280  	if err := l.close(); err != nil {
   281  		return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   282  	}
   283  	return nil
   284  }
   285  
   286  // Addr returns the listener's network address.
   287  // The [Addr] returned is shared by all invocations of Addr, so
   288  // do not modify it.
   289  func (l *UnixListener) Addr() Addr { return l.fd.laddr }
   290  
   291  // SetDeadline sets the deadline associated with the listener.
   292  // A zero time value disables the deadline.
   293  func (l *UnixListener) SetDeadline(t time.Time) error {
   294  	if !l.ok() {
   295  		return syscall.EINVAL
   296  	}
   297  	return l.fd.SetDeadline(t)
   298  }
   299  
   300  // File returns a copy of the underlying [os.File].
   301  // It is the caller's responsibility to close f when finished.
   302  // Closing l does not affect f, and closing f does not affect l.
   303  //
   304  // The returned [os.File]'s file descriptor is different from the
   305  // connection's. Attempting to change properties of the original
   306  // using this duplicate may or may not have the desired effect.
   307  //
   308  // On Windows, the returned os.File's file descriptor is not
   309  // usable on other processes.
   310  func (l *UnixListener) File() (f *os.File, err error) {
   311  	if !l.ok() {
   312  		return nil, syscall.EINVAL
   313  	}
   314  	f, err = l.file()
   315  	if err != nil {
   316  		err = &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   317  	}
   318  	return
   319  }
   320  
   321  // ListenUnix acts like [Listen] for Unix networks.
   322  //
   323  // The network must be "unix" or "unixpacket".
   324  func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error) {
   325  	switch network {
   326  	case "unix", "unixpacket":
   327  	default:
   328  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
   329  	}
   330  	if laddr == nil {
   331  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: errMissingAddress}
   332  	}
   333  	sl := &sysListener{network: network, address: laddr.String()}
   334  	ln, err := sl.listenUnix(context.Background(), laddr)
   335  	if err != nil {
   336  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
   337  	}
   338  	return ln, nil
   339  }
   340  
   341  // ListenUnixgram acts like [ListenPacket] for Unix networks.
   342  //
   343  // The network must be "unixgram".
   344  func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error) {
   345  	switch network {
   346  	case "unixgram":
   347  	default:
   348  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
   349  	}
   350  	if laddr == nil {
   351  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: errMissingAddress}
   352  	}
   353  	sl := &sysListener{network: network, address: laddr.String()}
   354  	c, err := sl.listenUnixgram(context.Background(), laddr)
   355  	if err != nil {
   356  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
   357  	}
   358  	return c, nil
   359  }
   360  

View as plain text