1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
7 .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com>
8 .\" Modified, 2003-09-23, Adam Langley
9 .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
10 .\" Added SOCK_SEQPACKET
11 .\" 2008-05-27, mtk, Provide a clear description of the three types of
12 .\" address that can appear in the sockaddr_un structure: pathname,
13 .\" unnamed, and abstract.
15 .TH UNIX 7 2012-05-10 "Linux" "Linux Programmer's Manual"
17 unix \- sockets for local interprocess communication
19 .B #include <sys/socket.h>
21 .B #include <sys/un.h>
23 .IB unix_socket " = socket(AF_UNIX, type, 0);"
25 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
31 socket family is used to communicate between processes on the same machine
33 Traditionally, UNIX domain sockets can be either unnamed,
34 or bound to a file system pathname (marked as being of type socket).
35 Linux also supports an abstract namespace which is independent of the
40 for a stream-oriented socket and
42 for a datagram-oriented socket that preserves message boundaries
43 (as on most UNIX implementations, UNIX domain datagram
44 sockets are always reliable and don't reorder datagrams);
45 and (since Linux 2.6.4)
47 for a connection-oriented socket that preserves message boundaries
48 and delivers messages in the order that they were sent.
50 UNIX domain sockets support passing file descriptors or process credentials
51 to other processes using ancillary data.
53 A UNIX domain socket address is represented in the following structure:
57 #define UNIX_PATH_MAX 108
60 sa_family_t sun_family; /* AF_UNIX */
61 char sun_path[UNIX_PATH_MAX]; /* pathname */
70 Three types of address are distinguished in this structure:
73 a UNIX domain socket can be bound to a null-terminated file
76 When the address of the socket is returned by
82 .IR "offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1" ,
85 contains the null-terminated pathname.
88 A stream socket that has not been bound to a pathname using
91 Likewise, the two sockets created by
94 When the address of an unnamed socket is returned by
100 .IR "sizeof(sa_family_t)" ,
103 should not be inspected.
104 .\" There is quite some variation across implementations: FreeBSD
105 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
108 an abstract socket address is distinguished by the fact that
110 is a null byte (\(aq\\0\(aq).
111 The socket's address in this namespace is given by the additional
114 that are covered by the specified length of the address structure.
115 (Null bytes in the name have no special significance.)
116 The name has no connection with file system pathnames.
117 When the address of an abstract socket is returned by
125 .IR "sizeof(sa_family_t)"
126 (i.e., greater than 2), and the name of the socket is contained in
128 .IR "(addrlen \- sizeof(sa_family_t))"
131 The abstract socket namespace is a nonportable Linux extension.
133 For historical reasons these socket options are specified with a
135 type even though they are
144 as the socket family.
147 Enables the receiving of the credentials of the sending process in an
149 When this option is set and the socket is not yet connected
150 a unique name in the abstract namespace will be generated automatically.
151 Expects an integer boolean flag.
158 .IR sizeof(sa_family_t) ,
159 .\" i.e. sizeof(short)
162 socket option was specified for a socket that was
163 not explicitly bound to an address,
164 then the socket is autobound to an abstract address.
165 The address consists of a null byte
166 followed by 5 bytes in the character set
168 Thus, there is a limit of 2^20 autobind addresses.
169 (From Linux 2.1.15, when the autobind feature was added,
170 8 bytes were used, and the limit was thus 2^32 autobind addresses.
171 The change to 5 bytes came in Linux 2.3.15.)
173 The following paragraphs describe domain-specific details and
174 unsupported features of the sockets API for UNIX domain sockets on Linux.
176 UNIX domain sockets do not support the transmission of
177 out-of-band data (the
187 flag is not supported by UNIX domain sockets.
195 is not supported by UNIX domain sockets.
199 socket option does have an effect for UNIX domain sockets, but the
202 For datagram sockets, the
204 value imposes an upper limit on the size of outgoing datagrams.
205 This limit is calculated as the doubled (see
207 option value less 32 bytes used for overhead.
208 .SS Ancillary messages
209 Ancillary data is sent and received using
213 For historical reasons the ancillary message types listed below
216 type even though they are
228 For more information see
232 Send or receive a set of open file descriptors from another process.
233 The data portion contains an integer array of the file descriptors.
234 The passed file descriptors behave as though they have been created with
238 Send or receive UNIX credentials.
239 This can be used for authentication.
240 The credentials are passed as a
243 Thus structure is defined in
250 pid_t pid; /* process ID of the sending process */
251 uid_t uid; /* user ID of the sending process */
252 gid_t gid; /* group ID of the sending process */
259 feature test macro must be defined (before including
261 header files) in order to obtain the definition
264 The credentials which the sender specifies are checked by the kernel.
265 A process with effective user ID 0 is allowed to specify values that do
267 The sender must specify its own process ID (unless it has the capability
269 its user ID, effective user ID, or saved set-user-ID (unless it has
271 and its group ID, effective group ID, or saved set-group-ID
278 option must be enabled on the socket.
282 calls return information in
284 The correct syntax is:
289 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
297 Returns the amount of queued unread data in the receive buffer.
298 The socket must not be in LISTEN state, otherwise an error
303 .IR <linux/sockios.h> .
304 .\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
305 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
307 you can use the synonymous
311 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
312 .\" quite what userland might expect. It seems to return the number
313 .\" of bytes allocated for buffers containing pending output.
314 .\" That number is normally larger than the number of bytes of pending
315 .\" output. Since this info is, from userland's point of view, imprecise,
316 .\" and it may well change, probably best not to document this now.
320 The specified local address is already in use or the file system socket
321 object already exists.
324 The remote address specified by
326 was not a listening socket.
327 This error can also occur if the target filename is not a socket.
330 Remote socket was unexpectedly closed.
333 User memory address was not valid.
336 Invalid argument passed.
337 A common cause is that the value
339 was not specified in the
341 field of passed addresses, or the socket was in an
342 invalid state for the applied operation.
346 called on an already connected socket or a target address was
347 specified on a connected socket.
350 The pathname in the remote address specified to
358 Socket operation needs a target address, but the socket is not connected.
361 Stream operation called on non-stream oriented socket or tried to
362 use the out-of-band data option.
365 The sender passed invalid credentials in the
369 Remote socket was closed on a stream socket.
373 This can be avoided by passing the
381 Passed protocol is not
385 Remote socket does not match the local socket type
393 Other errors can be generated by the generic socket layer or
394 by the file system while generating a file system socket object.
395 See the appropriate manual pages for more information.
398 and the abstract namespace were introduced with Linux 2.2 and should not
399 be used in portable programs.
400 (Some BSD-derived systems also support credential passing,
401 but the implementation details differ.)
403 In the Linux implementation, sockets which are visible in the
404 file system honor the permissions of the directory they are in.
405 Their owner, group and their permissions can be changed.
406 Creation of a new socket will fail if the process does not have write and
407 search (execute) permission on the directory the socket is created in.
408 Connecting to the socket object requires read/write permission.
409 This behavior differs from many BSD-derived systems which
410 ignore permissions for UNIX domain sockets.
411 Portable programs should not rely on
412 this feature for security.
414 Binding to a socket with a filename creates a socket
415 in the file system that must be deleted by the caller when it is no
418 The usual UNIX close-behind semantics apply; the socket can be unlinked
419 at any time and will be finally removed from the file system when the last
420 reference to it is closed.
422 To pass file descriptors or credentials over a
425 to send or receive at least one byte of nonancillary data in the same
431 UNIX domain stream sockets do not support the notion of out-of-band data.
436 For an example of the use of
446 .BR capabilities (7),