Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
unix(7)
Edit
PageHistory
Diff
Info
LikePages
UNIX !!!UNIX NAME SYNOPSIS DESCRIPTION ADDRESS FORMAT SOCKET OPTIONS ANCILLARY MESSAGES VERSIONS NOTES ERRORS SEE ALSO CREDITS ---- !!NAME unix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - Sockets for local interprocess communication. !!SYNOPSIS __#include __ #include __ ''unix_socket'' __= socket(PF_UNIX, type, 0);__'' error'' __= socketpair(PF_UNIX, type, 0, int *__''sv''__);__ !!DESCRIPTION The __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 FORMAT A 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. #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__. __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 OPTIONS For 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 MESSAGES For 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). __SCM_RIGHTS__ Send or receive a set of open file descriptors from another process. The data portion contains a integer array of the file descriptors. The passed file descriptors behave as like they have been created with dup(2). __SCM_CREDENTIALS__ Send or receive unix credentials. This can be used for authentication. The credentials are passed as a __struct ucred__ ancillary message. 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 */ }; 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 his own. The sender must specify its own process id (unless it has __CAP_SYS_ADMIN__), its user id, effective user id or set user id (unless it has __CAP_SETUID__), and its group id, effective group id or set group id (unless it has __CAP_SETGID__). To receive a __struct ucred__ message the __SO_PASSCRED__ option must be enabled on the socket. !!VERSIONS __SCM_CREDENTIALS__ and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs. !!NOTES In 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. !!ERRORS __ENOMEM__ Out of memory. __ECONNREFUSED__ 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. __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. __EOPNOTSUPP__ Stream operation called on non-stream oriented socket or tried to use the out-of-band data option. __EPROTONOSUPPORT__ Passed protocol is not PF_UNIX. __ESOCKTNOSUPPORT__ Unknown socket type. __EPROTOTYPE__ Remote socket does not match the local socket type (SOCK_DGRAM vs. SOCK_STREAM) __EADDRINUSE__ Selected local address is already taken or filesystem socket object already exists. __EISCONN__ connect(2) called on an already connected socket or a target address was specified on a connected socket. __ENOTCONN__ Socket operation needs a target address, but the socket is not connected. __ECONNRESET__ Remote socket was unexpectedly closed. __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) or recvmsg(2). __EFAULT__ User memory address was not valid. __EPERM__ The sender passed invalid credentials in the __struct ucred__. Other errors can be generated by the generic socket layer or by the filesystem while generating a filesystem socket object. See the appropriate manual pages for more information. !!SEE ALSO recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), socket(7), WhyUnix !!CREDITS This man page was written by Andi Kleen. ----
11 pages link to
unix(7)
:
WanDaemon
AddressFamily
Man7u
socketpair(2)
socket(7)
getsockopt(2)
bind(2)
socket(2)
NetworkProgrammingOld
setsockopt(2)
AdvancedUserTips
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.