1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com>
11 .\" Modified, 2003-09-23, Adam Langley
12 .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
13 .\" Added SOCK_SEQPACKET
14 .\" 2008-05-27, mtk, Provide a clear description of the three types of
15 .\" address that can appear in the sockaddr_un structure: pathname,
16 .\" unnamed, and abstract.
18 .TH UNIX 7 2012-05-10 "Linux" "Linux Programmer's Manual"
20 unix \- sockets for local interprocess communication
22 .B #include <sys/socket.h>
24 .B #include <sys/un.h>
26 .IB unix_socket " = socket(AF_UNIX, type, 0);"
28 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
34 socket family is used to communicate between processes on the same machine
36 Traditionally, UNIX domain sockets can be either unnamed,
37 or bound to a filesystem pathname (marked as being of type socket).
38 Linux also supports an abstract namespace which is independent of the
43 for a stream-oriented socket and
45 for a datagram-oriented socket that preserves message boundaries
46 (as on most UNIX implementations, UNIX domain datagram
47 sockets are always reliable and don't reorder datagrams);
48 and (since Linux 2.6.4)
50 for a connection-oriented socket that preserves message boundaries
51 and delivers messages in the order that they were sent.
53 UNIX domain sockets support passing file descriptors or process credentials
54 to other processes using ancillary data.
56 A UNIX domain socket address is represented in the following structure:
60 #define UNIX_PATH_MAX 108
63 sa_family_t sun_family; /* AF_UNIX */
64 char sun_path[UNIX_PATH_MAX]; /* pathname */
73 Three types of address are distinguished in this structure:
76 a UNIX domain socket can be bound to a null-terminated
77 filesystem pathname using
79 When the address of the socket is returned by
86 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
90 contains the null-terminated pathname.
93 A stream socket that has not been bound to a pathname using
96 Likewise, the two sockets created by
99 When the address of an unnamed socket is returned by
105 .IR "sizeof(sa_family_t)" ,
108 should not be inspected.
109 .\" There is quite some variation across implementations: FreeBSD
110 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
113 an abstract socket address is distinguished by the fact that
115 is a null byte (\(aq\\0\(aq).
116 The socket's address in this namespace is given by the additional
119 that are covered by the specified length of the address structure.
120 (Null bytes in the name have no special significance.)
121 The name has no connection with filesystem pathnames.
122 When the address of an abstract socket is returned by
130 .IR "sizeof(sa_family_t)"
131 (i.e., greater than 2), and the name of the socket is contained in
133 .IR "(addrlen \- sizeof(sa_family_t))"
136 The abstract socket namespace is a nonportable Linux extension.
138 For historical reasons these socket options are specified with a
140 type even though they are
149 as the socket family.
152 Enables the receiving of the credentials of the sending process in an
154 When this option is set and the socket is not yet connected
155 a unique name in the abstract namespace will be generated automatically.
156 Expects an integer boolean flag.
163 .IR sizeof(sa_family_t) ,
164 .\" i.e., sizeof(short)
167 socket option was specified for a socket that was
168 not explicitly bound to an address,
169 then the socket is autobound to an abstract address.
170 The address consists of a null byte
171 followed by 5 bytes in the character set
173 Thus, there is a limit of 2^20 autobind addresses.
174 (From Linux 2.1.15, when the autobind feature was added,
175 8 bytes were used, and the limit was thus 2^32 autobind addresses.
176 The change to 5 bytes came in Linux 2.3.15.)
178 The following paragraphs describe domain-specific details and
179 unsupported features of the sockets API for UNIX domain sockets on Linux.
181 UNIX domain sockets do not support the transmission of
182 out-of-band data (the
192 flag is not supported by UNIX domain sockets.
200 is not supported by UNIX domain sockets.
204 socket option does have an effect for UNIX domain sockets, but the
207 For datagram sockets, the
209 value imposes an upper limit on the size of outgoing datagrams.
210 This limit is calculated as the doubled (see
212 option value less 32 bytes used for overhead.
213 .SS Ancillary messages
214 Ancillary data is sent and received using
218 For historical reasons the ancillary message types listed below
221 type even though they are
233 For more information see
237 Send or receive a set of open file descriptors from another process.
238 The data portion contains an integer array of the file descriptors.
239 The passed file descriptors behave as though they have been created with
243 Send or receive UNIX credentials.
244 This can be used for authentication.
245 The credentials are passed as a
248 Thus structure is defined in
255 pid_t pid; /* process ID of the sending process */
256 uid_t uid; /* user ID of the sending process */
257 gid_t gid; /* group ID of the sending process */
264 feature test macro must be defined (before including
266 header files) in order to obtain the definition
269 The credentials which the sender specifies are checked by the kernel.
270 A process with effective user ID 0 is allowed to specify values that do
272 The sender must specify its own process ID (unless it has the capability
274 its user ID, effective user ID, or saved set-user-ID (unless it has
276 and its group ID, effective group ID, or saved set-group-ID
283 option must be enabled on the socket.
287 calls return information in
289 The correct syntax is:
294 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
302 Returns the amount of queued unread data in the receive buffer.
303 The socket must not be in LISTEN state, otherwise an error
308 .IR <linux/sockios.h> .
309 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
310 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
312 you can use the synonymous
316 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
317 .\" quite what userland might expect. It seems to return the number
318 .\" of bytes allocated for buffers containing pending output.
319 .\" That number is normally larger than the number of bytes of pending
320 .\" output. Since this info is, from userland's point of view, imprecise,
321 .\" and it may well change, probably best not to document this now.
325 The specified local address is already in use or the filesystem socket
326 object already exists.
329 The remote address specified by
331 was not a listening socket.
332 This error can also occur if the target filename is not a socket.
335 Remote socket was unexpectedly closed.
338 User memory address was not valid.
341 Invalid argument passed.
342 A common cause is that the value
344 was not specified in the
346 field of passed addresses, or the socket was in an
347 invalid state for the applied operation.
351 called on an already connected socket or a target address was
352 specified on a connected socket.
355 The pathname in the remote address specified to
363 Socket operation needs a target address, but the socket is not connected.
366 Stream operation called on non-stream oriented socket or tried to
367 use the out-of-band data option.
370 The sender passed invalid credentials in the
374 Remote socket was closed on a stream socket.
378 This can be avoided by passing the
386 Passed protocol is not
390 Remote socket does not match the local socket type
398 Other errors can be generated by the generic socket layer or
399 by the filesystem while generating a filesystem socket object.
400 See the appropriate manual pages for more information.
403 and the abstract namespace were introduced with Linux 2.2 and should not
404 be used in portable programs.
405 (Some BSD-derived systems also support credential passing,
406 but the implementation details differ.)
408 In the Linux implementation, sockets which are visible in the
409 filesystem honor the permissions of the directory they are in.
410 Their owner, group and their permissions can be changed.
411 Creation of a new socket will fail if the process does not have write and
412 search (execute) permission on the directory the socket is created in.
413 Connecting to the socket object requires read/write permission.
414 This behavior differs from many BSD-derived systems which
415 ignore permissions for UNIX domain sockets.
416 Portable programs should not rely on
417 this feature for security.
419 Binding to a socket with a filename creates a socket
420 in the filesystem that must be deleted by the caller when it is no
423 The usual UNIX close-behind semantics apply; the socket can be unlinked
424 at any time and will be finally removed from the filesystem when the last
425 reference to it is closed.
427 To pass file descriptors or credentials over a
430 to send or receive at least one byte of nonancillary data in the same
436 UNIX domain stream sockets do not support the notion of out-of-band data.
441 For an example of the use of
451 .BR capabilities (7),