UNIXSection: Linux Programmer's Manual (7)
Return to Main Contents
NAMEunix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - Sockets for local interprocess communication.
unix_socket = socket(PF_UNIX, type, 0);
DESCRIPTIONThe PF_UNIX (also known as PF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Unix sockets can be either anonymous (created by socketpair(2)) or associated with a file of socket type. 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. Unix sockets are always reliable and don't reorder datagrams.
Unix sockets support passing file descriptors or process credentials to other processes as ancillary data to datagrams.
ADDRESS FORMATA unix address is defined as a filename in the filesystem or as a unique string in the abstract namespace. Sockets created by socketpair(2) are anonymous. For non-anonymous sockets the target address can be set using connect(2). The local address can be set using bind(2). When a socket is connected and it doesn't already have a local address a unique address in the abstract namespace will be generated automatically.
sun_family always contains AF_UNIX. sun_path contains the zero-terminated pathname of the socket in the file system. If sun_path starts with a zero byte it refers to the abstract namespace maintained by the Unix protocol module. The socket's address in this namespace is given by the rest of the bytes in sun_path. Note that names in the abstract namespace are not zero-terminated.
SOCKET OPTIONSFor historical reasons these socket options are specified with a SOL_SOCKET type even though they are PF_UNIX specific. They can be set with setsockopt(2) and read with getsockopt(2) 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 connected yet an unique name in the abstract namespace will be generated automatically. Expects an integer boolean flag.
ANCILLARY MESSAGESFor historical reasons these ancillary message type are specified with a SOL_SOCKET type even though they are PF_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).
VERSIONSSCM_CREDENTIALS and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs.
NOTESIn the Linux implementation, sockets which are visible in the filesystem honour 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)). 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 you need to send/read at least one byte.
SEE ALSOrecvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), socket(7)
CREDITSThis man page was written by Andi Kleen.
This document was created by man2html, using the manual pages.
Time: 07:00:59 GMT, June 19, 2013