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 2008-12-01 "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 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 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 "sizeof(sa_family_t) + 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 All of the remaining bytes in
114 define the "name" of the socket.
115 (Null bytes in the name have no special significance.)
116 The name has no connection with file system pathnames.
117 The socket's address in this namespace is given by the rest of the
120 When the address of an abstract socket is returned by
126 .IR "sizeof(struct sockaddr_un)" ,
129 contains the abstract name.
130 The abstract socket namespace is a nonportable Linux extension.
132 For historical reasons these socket options are specified with a
134 type even though they are
143 as the socket family.
146 Enables the receiving of the credentials of the sending process
148 When this option is set and the socket is not yet connected
149 a unique name in the abstract namespace will be generated automatically.
150 Expects an integer boolean flag.
152 The following paragraphs describe domain-specific details and
153 unsupported features of the sockets API for Unix domain sockets on Linux.
155 Unix domain sockets do not support the transmission of
156 out-of-band data (the
166 flag is not supported by Unix domain sockets.
174 is not supported by Unix domain sockets.
178 socket option does have an effect for Unix domain sockets, but the
181 For datagram sockets, the
183 value imposes an upper limit on the size of outgoing datagrams.
184 This limit is calculated as the doubled (see
186 option value less 32 bytes used for overhead.
187 .SS Ancillary Messages
188 Ancillary data is sent and received using
192 For historical reasons the ancillary message types listed below
195 type even though they are
207 For more information see
211 Send or receive a set of open file descriptors from another process.
212 The data portion contains an integer array of the file descriptors.
213 The passed file descriptors behave as though they have been created with
217 Send or receive Unix credentials.
218 This can be used for authentication.
219 The credentials are passed as a
222 Thus structure is defined in
229 pid_t pid; /* process ID of the sending process */
230 uid_t uid; /* user ID of the sending process */
231 gid_t gid; /* group ID of the sending process */
238 feature test macro must be defined in order to obtain the definition
241 The credentials which the sender specifies are checked by the kernel.
242 A process with effective user ID 0 is allowed to specify values that do
244 The sender must specify its own process ID (unless it has the capability
246 its user ID, effective user ID, or saved set-user-ID (unless it has
248 and its group ID, effective group ID, or saved set-group-ID
255 option must be enabled on the socket.
259 Selected local address is already taken or file system socket
260 object already exists.
264 called with a socket object that isn't listening.
266 the remote socket does not exist or the filename is not a socket.
269 Remote socket was unexpectedly closed.
272 User memory address was not valid.
275 Invalid argument passed.
276 A common cause is the missing setting of AF_UNIX
279 field of passed addresses or the socket being in an
280 invalid state for the applied operation.
284 called on an already connected socket or a target address was
285 specified on a connected socket.
291 Socket operation needs a target address, but the socket is not connected.
294 Stream operation called on non-stream oriented socket or tried to
295 use the out-of-band data option.
298 The sender passed invalid credentials in the
302 Remote socket was closed on a stream socket.
306 This can be avoided by passing the
314 Passed protocol is not AF_UNIX.
317 Remote socket does not match the local socket type
325 Other errors can be generated by the generic socket layer or
326 by the file system while generating a file system socket object.
327 See the appropriate manual pages for more information.
330 and the abstract namespace were introduced with Linux 2.2 and should not
331 be used in portable programs.
332 (Some BSD-derived systems also support credential passing,
333 but the implementation details differ.)
335 In the Linux implementation, sockets which are visible in the
336 file system honor the permissions of the directory they are in.
337 Their owner, group and their permissions can be changed.
338 Creation of a new socket will fail if the process does not have write and
339 search (execute) permission on the directory the socket is created in.
340 Connecting to the socket object requires read/write permission.
341 This behavior differs from many BSD-derived systems which
342 ignore permissions for Unix sockets.
343 Portable programs should not rely on
344 this feature for security.
346 Binding to a socket with a filename creates a socket
347 in the file system that must be deleted by the caller when it is no
350 The usual Unix close-behind semantics apply; the socket can be unlinked
351 at any time and will be finally removed from the file system when the last
352 reference to it is closed.
354 To pass file descriptors or credentials over a
357 to send or receive at least one byte of nonancillary data in the same
363 Unix domain stream sockets do not support the notion of out-of-band data.
373 .BR capabilities (7),