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