unix, AF_UNIX, AF_LOCAL - Sockets for local interprocess communication


#include <sys/socket.h>
#include <sys/un.h>

unix_socket = socket(AF_UNIX, type, 0);


The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, Unix sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is independent of the file system.

Valid types are: SOCK_STREAM, for a stream-oriented socket and SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most Unix implementations, Unix domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.4) SOCK_SEQPACKET, for a connection-oriented socket that preserves message boundaries and delivers messages in the order that they were sent.


Address Format

A Unix domain socket address is represented in the following structure:
 #define UNIX_PATH_MAX    108
 struct sockaddr_un {
     sa_family_t sun_family;               /* AF_UNIX */
     char        sun_path[UNIX_PATH_MAX];  /* pathname */

sun_family always contains AF_UNIX.

Three types of address are distinguished in this structure:

pathname: a Unix domain socket can be bound to a null-terminated file system pathname using bind(2). When the address of the socket is returned by getsockname(2), getpeername(2), and accept(2), its length is sizeof(sa_family_t) + strlen(sun_path) + 1, and sun_path contains the null-terminated pathname.
unnamed: A stream socket that has not been bound to a pathname using bind(2) has no name. Likewise, the two sockets created by socketpair(2) are unnamed. When the address of an unnamed socket is returned by getsockname(2), getpeername(2), and accept(2), its length is sizeof(sa_family_t), and sun_path should not be inspected.
abstract: an abstract socket address is distinguished by the fact that sun_path[0] is a null byte ('\0'). All of the remaining bytes in sun_path define the "name" of the socket. (Null bytes in the name have no special significance.) The name has no connection with file system pathnames. The socket's address in this namespace is given by the rest of the bytes in sun_path. When the address of an abstract socket is returned by getsockname(2), getpeername(2), and accept(2), its length is sizeof(struct sockaddr_un), and sun_path contains the abstract name. The abstract socket namespace is a nonportable Linux extension.

Socket Options

Enables the receiving of the credentials of the sending process ancillary message. When this option is set and the socket is not yet connected a unique name in the abstract namespace will be generated automatically. Expects an integer boolean flag.

Sockets API

The following paragraphs describe domain-specific details and unsupported features of the sockets API for Unix domain sockets on Linux.



The use of MSG_TRUNC in the flags argument of recv(2) is not supported by Unix domain sockets.

The SO_SNDBUF socket option does have an effect for Unix domain sockets, but the SO_RCVBUF option does not. For datagram sockets, the SO_SNDBUF value imposes an upper limit on the size of outgoing datagrams. This limit is calculated as the doubled (see socket(7)) option value less 32 bytes used for overhead.

Ancillary Messages

 struct ucred {

Since glibc 2.8, the _GNU_SOURCE feature test macro must be defined in order to obtain the definition of this structure.



Selected local address is already taken or file system socket object already exists.
connect(2) called with a socket object that isn't listening. This can happen when the remote socket does not exist or the filename is not a socket.
Remote socket was unexpectedly closed.
User memory address was not valid.
Invalid argument passed. A common cause is the missing setting of AF_UNIX in the sun_type field of passed addresses or the socket being in an invalid state for the applied operation.
Out of memory.
The sender passed invalid credentials in the struct ucred.
Passed protocol is not AF_UNIX.
Remote socket does not match the local socket type (SOCK_DGRAM vs. SOCK_STREAM)
Unknown socket type.

Other errors can be generated by the generic socket layer or by the file system while generating a file system socket object. See the appropriate manual pages for more information.





Binding to a socket with a filename creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)). The usual Unix close-behind semantics apply; the socket can be unlinked at any time and will be finally removed from the file system when the last reference to it is closed.




See bind(2).



