---
name: "PF_UNIX"
section: "7"
platform: "linux"
package: "manpages"
version: "3.27-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/unix/7"
---

# PF\_UNIX(7)

Sockets for local interprocess communication

Part of [unix(7)](/unix/7.md).

## 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. 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.

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)](/bind/2.md). When the address of the socket is returned by [**getsockname**(2)](/getsockname/2.md), [**getpeername**(2)](/getpeername/2.md), and [**accept**(2)](/accept/2.md), 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)](/bind/2.md) has no name. Likewise, the two sockets created by [**socketpair**(2)](/socketpair/2.md) are unnamed. When the address of an unnamed socket is returned by [**getsockname**(2)](/getsockname/2.md), [**getpeername**(2)](/getpeername/2.md), and [**accept**(2)](/accept/2.md), 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)](/getsockname/2.md), [**getpeername**(2)](/getpeername/2.md), and [**accept**(2)](/accept/2.md), 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

For historical reasons these socket options are specified with a **SOL\_SOCKET** type even though they are **AF\_UNIX** specific. They can be set with [**setsockopt**(2)](/setsockopt/2.md) and read with [**getsockopt**(2)](/getsockopt/2.md) by specifying **SOL\_SOCKET** as the socket family.

- **SO\_PASSCRED**

  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.

Unix domain sockets do not support the transmission of out-of-band data (the **MSG\_OOB** flag for [**send**(2)](/send/2.md) and [**recv**(2)](/recv/2.md)).

The [**send**(2)](/send/2.md) **MSG\_MORE** flag is not supported by Unix domain sockets.

The use of **MSG\_TRUNC** in the *flags* argument of [**recv**(2)](/recv/2.md) 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)](/socket/7.md)) option value less 32 bytes used for overhead.

### Ancillary Messages

Ancillary data is sent and received using [**sendmsg**(2)](/sendmsg/2.md) and [**recvmsg**(2)](/recvmsg/2.md). For historical reasons the ancillary message types listed below are specified with a **SOL\_SOCKET** type even though they are **AF\_UNIX** specific. To send them set the *cmsg\_level* field of the struct *cmsghdr* to **SOL\_SOCKET** and the *cmsg\_type* field to the type. For more information see [**cmsg**(3)](/cmsg/3.md).

- **SCM\_RIGHTS**

  Send or receive a set of open file descriptors from another process. The data portion contains an integer array of the file descriptors. The passed file descriptors behave as though they have been created with [**dup**(2)](/dup/2.md).

- **SCM\_CREDENTIALS**

  Send or receive Unix credentials. This can be used for authentication. The credentials are passed as a *struct ucred* ancillary message. Thus structure is defined in *\<sys/socket.h\>* as follows:

  ```
  struct ucred {
      pid_t pid;    /* process ID of the sending process */
      uid_t uid;    /* user ID of the sending process */
      gid_t gid;    /* group ID of the sending process */
  };
  ```

  Since glibc 2.8, the **\_GNU\_SOURCE** feature test macro must be defined (before including *any* header files) in order to obtain the definition of this structure.

  The credentials which the sender specifies are checked by the kernel. A process with effective user ID 0 is allowed to specify values that do not match its own. The sender must specify its own process ID (unless it has the capability **CAP\_SYS\_ADMIN**), its user ID, effective user ID, or saved set-user-ID (unless it has **CAP\_SETUID**), and its group ID, effective group ID, or saved set-group-ID (unless it has **CAP\_SETGID**). To receive a *struct ucred* message the **SO\_PASSCRED** option must be enabled on the socket.

### Ioctls

The following [**ioctl**(2)](/ioctl/2.md) calls return information in *value*. The correct syntax is:

```
int value;
error = ioctl(tcp_socket, ioctl_type, &value);
```

*ioctl\_type* can be:

- **SIOCINQ**

  Returns the amount of queued unread data in the receive buffer. The socket must not be in LISTEN state, otherwise an error (**EINVAL**) is returned. **SIOCINQ** is defined in *\<linux/sockios.h\>*. Alternatively, you can use the synonymous **FIONREAD**, defined in *\<sys/ioctl.h\>*.

## ERRORS

- **EADDRINUSE**

  Selected local address is already taken or file system socket object already exists.

- **ECONNREFUSED**

  [**connect**(2)](/connect/2.md) 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.

- **ECONNRESET**

  Remote socket was unexpectedly closed.

- **EFAULT**

  User memory address was not valid.

- **EINVAL**

  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.

- **EISCONN**

  [**connect**(2)](/connect/2.md) called on an already connected socket or a target address was specified on a connected socket.

- **ENOMEM**

  Out of memory.

- **ENOTCONN**

  Socket operation needs a target address, but the socket is not connected.

- **EOPNOTSUPP**

  Stream operation called on non-stream oriented socket or tried to use the out-of-band data option.

- **EPERM**

  The sender passed invalid credentials in the *struct ucred*.

- **EPIPE**

  Remote socket was closed on a stream socket. If enabled, a **SIGPIPE** is sent as well. This can be avoided by passing the **MSG\_NOSIGNAL** flag to [**sendmsg**(2)](/sendmsg/2.md) or [**recvmsg**(2)](/recvmsg/2.md).

- **EPROTONOSUPPORT**

  Passed protocol is not AF\_UNIX.

- **EPROTOTYPE**

  Remote socket does not match the local socket type (**SOCK\_DGRAM** vs. **SOCK\_STREAM**)

- **ESOCKTNOSUPPORT**

  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.

## VERSIONS

**SCM\_CREDENTIALS** and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs. (Some BSD-derived systems also support credential passing, but the implementation details differ.)

## NOTES

In the Linux implementation, sockets which are visible in the file system honor the permissions of the directory they are in. Their owner, group and their permissions can be changed. Creation of a new socket will fail if the process does not have write and search (execute) permission on the directory the socket is created in. Connecting to the socket object requires read/write permission. This behavior differs from many BSD-derived systems which ignore permissions for Unix sockets. Portable programs should not rely on this feature for security.

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)](/unlink/2.md)). 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.

To pass file descriptors or credentials over a **SOCK\_STREAM**, you need to send or receive at least one byte of nonancillary data in the same [**sendmsg**(2)](/sendmsg/2.md) or [**recvmsg**(2)](/recvmsg/2.md) call.

Unix domain stream sockets do not support the notion of out-of-band data.

## EXAMPLE

See [**bind**(2)](/bind/2.md).

## SEE ALSO

[**recvmsg**(2)](/recvmsg/2.md), [**sendmsg**(2)](/sendmsg/2.md), [**socket**(2)](/socket/2.md), [**socketpair**(2)](/socketpair/2.md), [**cmsg**(3)](/cmsg/3.md), [**capabilities**(7)](/capabilities/7.md), [**credentials**(7)](/credentials/7.md), [**socket**(7)](/socket/7.md)

## COLOPHON

This page is part of release 3.27 of the Linux *man-pages* project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.
