UNIX(7) Linux Programmer's Manual UNIX(7)
NAME
unix, AF_UNIX, AF_LOCAL - Sockets for local interprocess communication
SYNOPSIS
#include <sys/socket.h>
#include <sys/un.h>
unix_socket = socket(AF_UNIX, type, 0);
error = socketpair(AF_UNIX, type, 0, int *sv);
DESCRIPTION
The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Tradition-
ally, 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.
Unix sockets support passing file descriptors or process credentials to other processes using ancillary data.
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 con-
tains 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 socket-
pair(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 ('