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
18 interprocess communication
20 .B #include <sys/socket.h>
22 .B #include <sys/un.h>
24 .IB unix_socket " = socket(AF_UNIX, type, 0);"
26 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
32 socket family is used to communicate between processes on the same machine
34 Traditionally, UNIX domain sockets can be either unnamed,
35 or bound to a file system pathname (marked as being of type socket).
36 Linux also supports an abstract namespace which is independent of the
41 for a stream-oriented socket and
43 for a datagram-oriented socket that preserves message boundaries
44 (as on most UNIX implementations, UNIX domain datagram
45 sockets are always reliable and don't reorder datagrams);
46 and (since Linux 2.6.4)
48 for a connection-oriented socket that preserves message boundaries
49 and delivers messages in the order that they were sent.
51 UNIX domain sockets support passing file descriptors or process credentials
52 to other processes using ancillary data.
54 A UNIX domain socket address is represented in the following structure:
58 #define UNIX_PATH_MAX 108
61 sa_family_t sun_family; /* AF_UNIX */
62 char sun_path[UNIX_PATH_MAX]; /* pathname */
71 Three types of address are distinguished in this structure:
74 a UNIX domain socket can be bound to a null-terminated file
77 When the address of the socket is returned by
83 .IR "offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1" ,
86 contains the null-terminated pathname.
89 A stream socket that has not been bound to a pathname using
92 Likewise, the two sockets created by
95 When the address of an unnamed socket is returned by
101 .IR "sizeof(sa_family_t)" ,
104 should not be inspected.
105 .\" There is quite some variation across implementations: FreeBSD
106 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
109 an abstract socket address is distinguished by the fact that
111 is a null byte (\(aq\\0\(aq).
112 The socket's address in this namespace is given by the additional
115 that are covered by the specified length of the address structure.
116 (Null bytes in the name have no special significance.)
117 The name has no connection with file system pathnames.
118 When the address of an abstract socket is returned by
126 .IR "sizeof(sa_family_t)"
127 (i.e., greater than 2), and the name of the socket is contained in
129 .IR "(addrlen \- sizeof(sa_family_t))"
132 The abstract socket namespace is a nonportable Linux extension.
134 For historical reasons these socket options are specified with a
136 type even though they are
145 as the socket family.
148 Enables the receiving of the credentials of the sending process in an
150 When this option is set and the socket is not yet connected
151 a unique name in the abstract namespace will be generated automatically.
152 Expects an integer boolean flag.
159 .IR sizeof(sa_family_t) ,
160 .\" i.e. sizeof(short)
163 socket option was specified for a socket that was
164 not explicitly bound to an address,
165 then the socket is autobound to an abstract address.
166 The address consists of a null byte
167 followed by 5 bytes in the character set
169 Thus, there is a limit of 2^20 autobind addresses.
170 (From Linux 2.1.15, when the autobind feature was added,
171 8 bytes were used, and the limit was thus 2^32 autobind addresses.
172 The change to 5 bytes came in Linux 2.3.15.)
174 The following paragraphs describe domain-specific details and
175 unsupported features of the sockets API for UNIX domain sockets on Linux.
177 UNIX domain sockets do not support the transmission of
178 out-of-band data (the
188 flag is not supported by UNIX domain sockets.
196 is not supported by UNIX domain sockets.
200 socket option does have an effect for UNIX domain sockets, but the
203 For datagram sockets, the
205 value imposes an upper limit on the size of outgoing datagrams.
206 This limit is calculated as the doubled (see
208 option value less 32 bytes used for overhead.
209 .SS Ancillary Messages
210 Ancillary data is sent and received using
214 For historical reasons the ancillary message types listed below
217 type even though they are
229 For more information see
233 Send or receive a set of open file descriptors from another process.
234 The data portion contains an integer array of the file descriptors.
235 The passed file descriptors behave as though they have been created with
239 Send or receive UNIX credentials.
240 This can be used for authentication.
241 The credentials are passed as a
244 Thus structure is defined in
251 pid_t pid; /* process ID of the sending process */
252 uid_t uid; /* user ID of the sending process */
253 gid_t gid; /* group ID of the sending process */
260 feature test macro must be defined (before including
262 header files) in order to obtain the definition
265 The credentials which the sender specifies are checked by the kernel.
266 A process with effective user ID 0 is allowed to specify values that do
268 The sender must specify its own process ID (unless it has the capability
270 its user ID, effective user ID, or saved set-user-ID (unless it has
272 and its group ID, effective group ID, or saved set-group-ID
279 option must be enabled on the socket.
283 calls return information in
285 The correct syntax is:
290 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
298 Returns the amount of queued unread data in the receive buffer.
299 The socket must not be in LISTEN state, otherwise an error
304 .IR <linux/sockios.h> .
305 .\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
306 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
308 you can use the synonymous
312 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
313 .\" quite what userland might expect. It seems to return the number
314 .\" of bytes allocated for buffers containing pending output.
315 .\" That number is normally larger than the number of bytes of pending
316 .\" output. Since this info is, from userland's point of view, imprecise,
317 .\" and it may well change, probably best not to document this now.
321 The specified local address is already in use or the file system socket
322 object already exists.
325 The remote address specified by
327 was not a listening socket.
328 This error can also occur if the target filename is not a socket.
331 Remote socket was unexpectedly closed.
334 User memory address was not valid.
337 Invalid argument passed.
338 A common cause is that the value
340 was not specified in the
342 field of passed addresses, or the socket was in an
343 invalid state for the applied operation.
347 called on an already connected socket or a target address was
348 specified on a connected socket.
351 The pathname in the remote address specified to
359 Socket operation needs a target address, but the socket is not connected.
362 Stream operation called on non-stream oriented socket or tried to
363 use the out-of-band data option.
366 The sender passed invalid credentials in the
370 Remote socket was closed on a stream socket.
374 This can be avoided by passing the
382 Passed protocol is not
386 Remote socket does not match the local socket type
394 Other errors can be generated by the generic socket layer or
395 by the file system while generating a file system socket object.
396 See the appropriate manual pages for more information.
399 and the abstract namespace were introduced with Linux 2.2 and should not
400 be used in portable programs.
401 (Some BSD-derived systems also support credential passing,
402 but the implementation details differ.)
404 In the Linux implementation, sockets which are visible in the
405 file system honor the permissions of the directory they are in.
406 Their owner, group and their permissions can be changed.
407 Creation of a new socket will fail if the process does not have write and
408 search (execute) permission on the directory the socket is created in.
409 Connecting to the socket object requires read/write permission.
410 This behavior differs from many BSD-derived systems which
411 ignore permissions for UNIX domain sockets.
412 Portable programs should not rely on
413 this feature for security.
415 Binding to a socket with a filename creates a socket
416 in the file system that must be deleted by the caller when it is no
419 The usual UNIX close-behind semantics apply; the socket can be unlinked
420 at any time and will be finally removed from the file system when the last
421 reference to it is closed.
423 To pass file descriptors or credentials over a
426 to send or receive at least one byte of nonancillary data in the same
432 UNIX domain stream sockets do not support the notion of out-of-band data.
437 For an example of the use of
447 .BR capabilities (7),