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 2010-11-15 "Linux" "Linux Programmer's Manual"
17 unix, AF_UNIX, AF_LOCAL \- 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 ('\\0').
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.)
171 The following paragraphs describe domain-specific details and
172 unsupported features of the sockets API for UNIX domain sockets on Linux.
174 UNIX domain sockets do not support the transmission of
175 out-of-band data (the
185 flag is not supported by UNIX domain sockets.
193 is not supported by UNIX domain sockets.
197 socket option does have an effect for UNIX domain sockets, but the
200 For datagram sockets, the
202 value imposes an upper limit on the size of outgoing datagrams.
203 This limit is calculated as the doubled (see
205 option value less 32 bytes used for overhead.
206 .SS Ancillary Messages
207 Ancillary data is sent and received using
211 For historical reasons the ancillary message types listed below
214 type even though they are
226 For more information see
230 Send or receive a set of open file descriptors from another process.
231 The data portion contains an integer array of the file descriptors.
232 The passed file descriptors behave as though they have been created with
236 Send or receive UNIX credentials.
237 This can be used for authentication.
238 The credentials are passed as a
241 Thus structure is defined in
248 pid_t pid; /* process ID of the sending process */
249 uid_t uid; /* user ID of the sending process */
250 gid_t gid; /* group ID of the sending process */
257 feature test macro must be defined (before including
259 header files) in order to obtain the definition
262 The credentials which the sender specifies are checked by the kernel.
263 A process with effective user ID 0 is allowed to specify values that do
265 The sender must specify its own process ID (unless it has the capability
267 its user ID, effective user ID, or saved set-user-ID (unless it has
269 and its group ID, effective group ID, or saved set-group-ID
276 option must be enabled on the socket.
280 calls return information in
282 The correct syntax is:
287 .IB error " = ioctl(" tcp_socket ", " ioctl_type ", &" value ");"
295 Returns the amount of queued unread data in the receive buffer.
296 The socket must not be in LISTEN state, otherwise an error
301 .IR <linux/sockios.h> .
302 .\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
303 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
305 you can use the synonymous
309 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
310 .\" quite what userland might expect. It seems to return the number
311 .\" of bytes allocated for buffers containing pending output.
312 .\" That number is normally larger than the number of bytes of pending
313 .\" output. Since this info is, from userland's point of view, imprecise,
314 .\" and it may well change, probably best not to document this now.
318 The specified local address is already in use or the file system socket
319 object already exists.
322 The remote address specified by
324 was not a listening socket.
325 This error can also occur if the target filename is not a socket.
328 Remote socket was unexpectedly closed.
331 User memory address was not valid.
334 Invalid argument passed.
335 A common cause is that the value
337 was not specified in the
339 field of passed addresses, or the socket was in an
340 invalid state for the applied operation.
344 called on an already connected socket or a target address was
345 specified on a connected socket.
348 The pathname in the remote address specified to
356 Socket operation needs a target address, but the socket is not connected.
359 Stream operation called on non-stream oriented socket or tried to
360 use the out-of-band data option.
363 The sender passed invalid credentials in the
367 Remote socket was closed on a stream socket.
371 This can be avoided by passing the
379 Passed protocol is not
383 Remote socket does not match the local socket type
391 Other errors can be generated by the generic socket layer or
392 by the file system while generating a file system socket object.
393 See the appropriate manual pages for more information.
396 and the abstract namespace were introduced with Linux 2.2 and should not
397 be used in portable programs.
398 (Some BSD-derived systems also support credential passing,
399 but the implementation details differ.)
401 In the Linux implementation, sockets which are visible in the
402 file system honor the permissions of the directory they are in.
403 Their owner, group and their permissions can be changed.
404 Creation of a new socket will fail if the process does not have write and
405 search (execute) permission on the directory the socket is created in.
406 Connecting to the socket object requires read/write permission.
407 This behavior differs from many BSD-derived systems which
408 ignore permissions for UNIX domain sockets.
409 Portable programs should not rely on
410 this feature for security.
412 Binding to a socket with a filename creates a socket
413 in the file system that must be deleted by the caller when it is no
416 The usual UNIX close-behind semantics apply; the socket can be unlinked
417 at any time and will be finally removed from the file system when the last
418 reference to it is closed.
420 To pass file descriptors or credentials over a
423 to send or receive at least one byte of nonancillary data in the same
429 UNIX domain stream sockets do not support the notion of out-of-band data.
439 .BR capabilities (7),