]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/unix.7
getloadavg.3, openpty.3, operator.7, symlink.7: srcfix: LICENSE: s/BSD_3_CLAUSE_FULL...
[thirdparty/man-pages.git] / man7 / unix.7
CommitLineData
77117f4f 1.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2297bf0e 2.\"
00acdba1 3.\" %%%LICENSE_START(VERBATIM_ONE_PARA)
77117f4f
MK
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.
00acdba1 8.\" %%%END_LICENSE
77117f4f
MK
9.\"
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.
17.\"
eae2dfce 18.TH UNIX 7 2012-05-10 "Linux" "Linux Programmer's Manual"
77117f4f 19.SH NAME
f68512e9 20unix \- sockets for local interprocess communication
77117f4f
MK
21.SH SYNOPSIS
22.B #include <sys/socket.h>
23.br
24.B #include <sys/un.h>
25
d4c8c97c 26.IB unix_socket " = socket(AF_UNIX, type, 0);"
77117f4f 27.br
d4c8c97c 28.IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
77117f4f
MK
29.SH DESCRIPTION
30The
d4c8c97c 31.B AF_UNIX
77117f4f 32(also known as
d4c8c97c 33.BR AF_LOCAL )
77117f4f
MK
34socket family is used to communicate between processes on the same machine
35efficiently.
4891f52a 36Traditionally, UNIX domain sockets can be either unnamed,
77117f4f
MK
37or bound to a file system pathname (marked as being of type socket).
38Linux also supports an abstract namespace which is independent of the
39file system.
40
41Valid types are:
42.BR SOCK_STREAM ,
43for a stream-oriented socket and
44.BR SOCK_DGRAM ,
45for a datagram-oriented socket that preserves message boundaries
008f1ecc 46(as on most UNIX implementations, UNIX domain datagram
77117f4f
MK
47sockets are always reliable and don't reorder datagrams);
48and (since Linux 2.6.4)
49.BR SOCK_SEQPACKET ,
50for a connection-oriented socket that preserves message boundaries
51and delivers messages in the order that they were sent.
52
4891f52a 53UNIX domain sockets support passing file descriptors or process credentials
77117f4f 54to other processes using ancillary data.
c634028a 55.SS Address format
008f1ecc 56A UNIX domain socket address is represented in the following structure:
77117f4f
MK
57.in +4n
58.nf
59
60#define UNIX_PATH_MAX 108
61
62struct sockaddr_un {
63 sa_family_t sun_family; /* AF_UNIX */
64 char sun_path[UNIX_PATH_MAX]; /* pathname */
65};
66.fi
67.in
68.PP
69.I sun_family
70always contains
71.BR AF_UNIX .
72
73Three types of address are distinguished in this structure:
74.IP * 3
75.IR pathname :
008f1ecc 76a UNIX domain socket can be bound to a null-terminated file
77117f4f
MK
77system pathname using
78.BR bind (2).
79When the address of the socket is returned by
80.BR getsockname (2),
81.BR getpeername (2),
82and
83.BR accept (2),
84its length is
6ed236d5 85.IR "offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1" ,
77117f4f
MK
86and
87.I sun_path
88contains the null-terminated pathname.
89.IP *
90.IR unnamed :
91A stream socket that has not been bound to a pathname using
92.BR bind (2)
93has no name.
94Likewise, the two sockets created by
95.BR socketpair (2)
96are unnamed.
97When the address of an unnamed socket is returned by
98.BR getsockname (2),
99.BR getpeername (2),
100and
101.BR accept (2),
102its length is
103.IR "sizeof(sa_family_t)" ,
104and
105.I sun_path
106should not be inspected.
107.\" There is quite some variation across implementations: FreeBSD
108.\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
109.IP *
110.IR abstract :
111an abstract socket address is distinguished by the fact that
112.IR sun_path[0]
836830b4 113is a null byte (\(aq\\0\(aq).
156a0e0d
MK
114The socket's address in this namespace is given by the additional
115bytes in
116.IR sun_path
117that are covered by the specified length of the address structure.
77117f4f
MK
118(Null bytes in the name have no special significance.)
119The name has no connection with file system pathnames.
77117f4f
MK
120When the address of an abstract socket is returned by
121.BR getsockname (2),
122.BR getpeername (2),
123and
124.BR accept (2),
156a0e0d
MK
125the returned
126.I addrlen
127is greater than
128.IR "sizeof(sa_family_t)"
129(i.e., greater than 2), and the name of the socket is contained in
130the first
131.IR "(addrlen \- sizeof(sa_family_t))"
132bytes of
133.IR sun_path .
d603cc27 134The abstract socket namespace is a nonportable Linux extension.
c634028a 135.SS Socket options
77117f4f
MK
136For historical reasons these socket options are specified with a
137.B SOL_SOCKET
138type even though they are
d4c8c97c 139.B AF_UNIX
77117f4f
MK
140specific.
141They can be set with
142.BR setsockopt (2)
143and read with
144.BR getsockopt (2)
145by specifying
146.B SOL_SOCKET
147as the socket family.
148.TP
149.B SO_PASSCRED
6074c3e6 150Enables the receiving of the credentials of the sending process in an
77117f4f
MK
151ancillary message.
152When this option is set and the socket is not yet connected
153a unique name in the abstract namespace will be generated automatically.
154Expects an integer boolean flag.
c634028a 155.SS Autobind feature
0cf2caa4 156If a
0b80cf56 157.BR bind (2)
0cf2caa4
MK
158call specifies
159.I addrlen
160as
161.IR sizeof(sa_family_t) ,
162.\" i.e. sizeof(short)
163or the
164.BR SO_PASSCRED
165socket option was specified for a socket that was
166not explicitly bound to an address,
167then the socket is autobound to an abstract address.
168The address consists of a null byte
169followed by 5 bytes in the character set
170.IR [0-9a-f] .
1e4e3bad
MK
171Thus, there is a limit of 2^20 autobind addresses.
172(From Linux 2.1.15, when the autobind feature was added,
1738 bytes were used, and the limit was thus 2^32 autobind addresses.
174The change to 5 bytes came in Linux 2.3.15.)
19e19f5f 175.SS Sockets API
77117f4f 176The following paragraphs describe domain-specific details and
008f1ecc 177unsupported features of the sockets API for UNIX domain sockets on Linux.
77117f4f 178
008f1ecc 179UNIX domain sockets do not support the transmission of
77117f4f
MK
180out-of-band data (the
181.B MSG_OOB
182flag for
183.BR send (2)
184and
185.BR recv (2)).
186
187The
188.BR send (2)
189.B MSG_MORE
008f1ecc 190flag is not supported by UNIX domain sockets.
77117f4f 191
77e75b90
MK
192The use of
193.B MSG_TRUNC
194in the
195.I flags
196argument of
197.BR recv (2)
008f1ecc 198is not supported by UNIX domain sockets.
77e75b90 199
77117f4f
MK
200The
201.B SO_SNDBUF
008f1ecc 202socket option does have an effect for UNIX domain sockets, but the
77117f4f
MK
203.B SO_RCVBUF
204option does not.
205For datagram sockets, the
206.B SO_SNDBUF
207value imposes an upper limit on the size of outgoing datagrams.
208This limit is calculated as the doubled (see
209.BR socket (7))
210option value less 32 bytes used for overhead.
c634028a 211.SS Ancillary messages
77117f4f
MK
212Ancillary data is sent and received using
213.BR sendmsg (2)
214and
215.BR recvmsg (2).
216For historical reasons the ancillary message types listed below
217are specified with a
218.B SOL_SOCKET
219type even though they are
d4c8c97c 220.B AF_UNIX
77117f4f
MK
221specific.
222To send them set the
223.I cmsg_level
224field of the struct
225.I cmsghdr
226to
227.B SOL_SOCKET
228and the
229.I cmsg_type
230field to the type.
231For more information see
232.BR cmsg (3).
233.TP
234.B SCM_RIGHTS
235Send or receive a set of open file descriptors from another process.
236The data portion contains an integer array of the file descriptors.
237The passed file descriptors behave as though they have been created with
238.BR dup (2).
239.TP
240.B SCM_CREDENTIALS
008f1ecc 241Send or receive UNIX credentials.
77117f4f
MK
242This can be used for authentication.
243The credentials are passed as a
244.I struct ucred
245ancillary message.
b1587ca8
MK
246Thus structure is defined in
247.I <sys/socket.h>
248as follows:
77117f4f
MK
249
250.in +4n
251.nf
252struct ucred {
253 pid_t pid; /* process ID of the sending process */
254 uid_t uid; /* user ID of the sending process */
255 gid_t gid; /* group ID of the sending process */
256};
257.fi
258.in
259
b1587ca8 260Since glibc 2.8, the
1bc510f5 261.B _GNU_SOURCE
e417acb0
MK
262feature test macro must be defined (before including
263.I any
264header files) in order to obtain the definition
b1587ca8
MK
265of this structure.
266
77117f4f
MK
267The credentials which the sender specifies are checked by the kernel.
268A process with effective user ID 0 is allowed to specify values that do
269not match its own.
270The sender must specify its own process ID (unless it has the capability
271.BR CAP_SYS_ADMIN ),
272its user ID, effective user ID, or saved set-user-ID (unless it has
273.BR CAP_SETUID ),
274and its group ID, effective group ID, or saved set-group-ID
275(unless it has
276.BR CAP_SETGID ).
277To receive a
278.I struct ucred
279message the
280.B SO_PASSCRED
281option must be enabled on the socket.
fbea0f81
MK
282.SS Ioctls
283The following
284.BR ioctl (2)
285calls return information in
286.IR value .
287The correct syntax is:
288.PP
289.RS
290.nf
291.BI int " value";
f0d77d97 292.IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
fbea0f81
MK
293.fi
294.RE
295.PP
296.I ioctl_type
297can be:
298.TP
299.B SIOCINQ
300Returns the amount of queued unread data in the receive buffer.
301The socket must not be in LISTEN state, otherwise an error
302.RB ( EINVAL )
303is returned.
304.B SIOCINQ
305is defined in
306.IR <linux/sockios.h> .
307.\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
308.\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
309Alternatively,
310you can use the synonymous
311.BR FIONREAD ,
312defined in
313.IR <sys/ioctl.h> .
7aed61d9 314.\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
fbea0f81
MK
315.\" quite what userland might expect. It seems to return the number
316.\" of bytes allocated for buffers containing pending output.
317.\" That number is normally larger than the number of bytes of pending
318.\" output. Since this info is, from userland's point of view, imprecise,
319.\" and it may well change, probably best not to document this now.
77117f4f
MK
320.SH ERRORS
321.TP
322.B EADDRINUSE
d1c9ea80 323The specified local address is already in use or the file system socket
77117f4f
MK
324object already exists.
325.TP
326.B ECONNREFUSED
1fe284ab 327The remote address specified by
77117f4f 328.BR connect (2)
1fe284ab 329was not a listening socket.
d1c9ea80 330This error can also occur if the target filename is not a socket.
77117f4f
MK
331.TP
332.B ECONNRESET
333Remote socket was unexpectedly closed.
334.TP
335.B EFAULT
336User memory address was not valid.
337.TP
338.B EINVAL
339Invalid argument passed.
1fe284ab 340A common cause is that the value
40656bc7 341.B AF_UNIX
1fe284ab 342was not specified in the
77117f4f 343.I sun_type
1fe284ab 344field of passed addresses, or the socket was in an
77117f4f
MK
345invalid state for the applied operation.
346.TP
347.B EISCONN
348.BR connect (2)
349called on an already connected socket or a target address was
350specified on a connected socket.
351.TP
ec55a2b6
MK
352.B ENOENT
353The pathname in the remote address specified to
9470f355 354.BR connect (2)
ec55a2b6
MK
355did not exist.
356.TP
77117f4f
MK
357.B ENOMEM
358Out of memory.
359.TP
360.B ENOTCONN
361Socket operation needs a target address, but the socket is not connected.
362.TP
363.B EOPNOTSUPP
364Stream operation called on non-stream oriented socket or tried to
365use the out-of-band data option.
366.TP
367.B EPERM
368The sender passed invalid credentials in the
369.IR "struct ucred" .
370.TP
371.B EPIPE
372Remote socket was closed on a stream socket.
373If enabled, a
374.B SIGPIPE
375is sent as well.
376This can be avoided by passing the
377.B MSG_NOSIGNAL
378flag to
379.BR sendmsg (2)
380or
381.BR recvmsg (2).
382.TP
383.B EPROTONOSUPPORT
cd0221ea
MK
384Passed protocol is not
385.BR AF_UNIX .
77117f4f
MK
386.TP
387.B EPROTOTYPE
388Remote socket does not match the local socket type
389.RB ( SOCK_DGRAM
d1c9ea80 390versus
77117f4f
MK
391.BR SOCK_STREAM )
392.TP
393.B ESOCKTNOSUPPORT
394Unknown socket type.
395.PP
396Other errors can be generated by the generic socket layer or
397by the file system while generating a file system socket object.
398See the appropriate manual pages for more information.
399.SH VERSIONS
400.B SCM_CREDENTIALS
401and the abstract namespace were introduced with Linux 2.2 and should not
402be used in portable programs.
403(Some BSD-derived systems also support credential passing,
404but the implementation details differ.)
405.SH NOTES
406In the Linux implementation, sockets which are visible in the
407file system honor the permissions of the directory they are in.
408Their owner, group and their permissions can be changed.
409Creation of a new socket will fail if the process does not have write and
410search (execute) permission on the directory the socket is created in.
411Connecting to the socket object requires read/write permission.
412This behavior differs from many BSD-derived systems which
4891f52a 413ignore permissions for UNIX domain sockets.
77117f4f
MK
414Portable programs should not rely on
415this feature for security.
416
417Binding to a socket with a filename creates a socket
418in the file system that must be deleted by the caller when it is no
419longer needed (using
420.BR unlink (2)).
008f1ecc 421The usual UNIX close-behind semantics apply; the socket can be unlinked
77117f4f
MK
422at any time and will be finally removed from the file system when the last
423reference to it is closed.
424
425To pass file descriptors or credentials over a
426.BR SOCK_STREAM ,
427you need
24b74457 428to send or receive at least one byte of nonancillary data in the same
77117f4f
MK
429.BR sendmsg (2)
430or
431.BR recvmsg (2)
432call.
433
008f1ecc 434UNIX domain stream sockets do not support the notion of out-of-band data.
77117f4f
MK
435.SH EXAMPLE
436See
437.BR bind (2).
c751683c
MK
438
439For an example of the use of
440.BR SCM_RIGHTS
441see
442.BR cmsg (3).
47297adb 443.SH SEE ALSO
77117f4f
MK
444.BR recvmsg (2),
445.BR sendmsg (2),
446.BR socket (2),
447.BR socketpair (2),
448.BR cmsg (3),
449.BR capabilities (7),
450.BR credentials (7),
451.BR socket (7)